-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
191 lines (173 loc) · 6.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Create and draw an interactive representation of packages dependencies.
"""
# Copyright (C) 2020 by Papoteur
# Distributed under the GPLv3 license.
# All rights reserved, see LICENSE for details.
__author__ = """Papoteur"""
import dnf
from networkx import Graph, get_node_attributes, set_node_attributes, spring_layout
from bokeh.plotting import figure, from_networkx, curdoc
from bokeh.models import Rect, HoverTool, LabelSet, CustomJSTransform, TextInput, Slider, MultiLine, Div, Button
from bokeh.transform import transform
from bokeh.layouts import column, row
global field
global source
global level
global current_rpm
global sack
global hist
base = dnf.Base()
conf = base.conf
conf.cachedir = '/tmp/my_cache_dir'
# this seems to take time at first launch. Needed to have list of non installed packages
base.read_all_repos()
sack = base.fill_sack(load_system_repo=False)
level = 2
hist = []
current_rpm = "rpm"
field = TextInput(value=current_rpm, title="Select package: ")
level_slider = Slider(start=1, end=5, step=1, value=level, title="Depth")
back_bt = Button(label="Back")
def add_requires(ref, depth, G):
'''
Add all kind od dependencies
'''
global level
global sack
process = [(ref.requires,"blue"),(ref.recommends,"green"),(ref.suggests,"orange"),(ref.supplements, "braun")]
for query, link_color in process:
previous = ""
i = 1
for req in list(query):
# req is a _hawkey.Reldep object
i += 1
f = sack.query().filter(provides=req).latest()
p_name = ""
r = 1
for p in list(f):
r += 1
if (p.name == p_name) :
continue
p_name = p.name
if p.name in G.nodes() :
attrs = get_node_attributes(G, 'req')
attrs[p.name] = f"{attrs[p.name]} {str(req)}/{ref.name}"
set_node_attributes(G,attrs,'req')
else:
G.add_node(p.name,
name=p.name,
version=p.evr,
width=(len(p.name) * 0.018 + 0.05 ),
offset= -len(p.name) * 2.5 - 6,
color= ' cornsilk',
req= f"{str(req)}/{ref.name}" )
G.add_edge(ref.name,p.name,
name=ref.name,
version= ref.version,
req=str(req) ,
color=link_color)
if p.name != previous:
if depth <= level - 2:
add_requires(p, depth + 1, G)
previous = p.name
def graphe(name, G):
global sack
i = sack.query().filter(name=name)
packages = list(i)
for pkg in packages:
G.add_node(pkg.name, name=pkg.name,
version=pkg.version,
width=(len(pkg.name) * 0.018 + 0.05 ),
offset=- len(pkg.name) * 2.5 - 6,
color= ' red',
req="")
add_requires(pkg, 0, G)
return (G.number_of_nodes(), G.number_of_edges())
def update_level(attr, old, new):
# triggerred when level slider is updated
global level
global current_rpm
level = int(new)
update(attr, current_rpm, current_rpm)
def update(attr, old, new):
# triggered when field value is changed or called when level is changed
newplot = render(new)
layout.children[1] = newplot
def render(pkg):
global current_rpm, level, hover, source
current_rpm = pkg
hist.append([pkg,level])
newG = Graph()
nrn, nre = graphe(pkg, newG)
if nrn == 0:
return Div(text="This package is unknown")
newgraph = from_networkx(newG, spring_layout, scale=2, center=(0,0))
newplot = figure(title="RPM network", sizing_mode ="scale_width", aspect_ratio=2, x_range=(-2.2, 2.2), y_range=(-2.1, 2.1),
tools="tap", toolbar_location=None)
newplot.axis.visible = False
newplot.grid.visible = False
newgraph.node_renderer.glyph = Rect(height=0.07, width=0.1, fill_color="color", fill_alpha=0.0, line_alpha=0.0)
if nre != 0:
newgraph.edge_renderer.glyph = MultiLine(line_color="color", line_alpha=0.8)
newplot.renderers.append(newgraph)
source = newgraph.node_renderer.data_source
xcoord = CustomJSTransform(v_func=code % "0", args=dict(provider=newgraph.layout_provider))
ycoord = CustomJSTransform(v_func=code % "1", args=dict(provider=newgraph.layout_provider))
source.selected.on_change('indices', selected)
labels = LabelSet(x=transform('index', xcoord),
y=transform('index', ycoord),
text='name', text_font_size="12px",
y_offset=-6, x_offset= "offset" ,
background_fill_color='color', background_fill_alpha=0.85,
border_line_color='color', border_line_alpha=1.0,
source=source, render_mode='canvas')
newplot.add_tools(hover)
newplot.add_layout(labels)
return newplot
def selected(attr, old, new):
# triggered when package tag is clicked
global source
pkg = source.data['name'][new[0]]
field.value = pkg
def back(event):
# triggered when the back button is pressed
global hist
global level
if len(hist) > 1:
# the last record in hist is the current display. We want the previous. This one will be added again when calling an update, thus we need to delete it from the list before.
pkg, new_level = hist[-2]
del hist[-1]
del hist[-1]
if new_level != level :
level = new_level
# trigger rendering
level_slider.value=level
if pkg != current_rpm :
# trigger rendering
field.value = pkg
# Helper to get position of nodes and puttext label at adequate place
code = """
var result = new Float64Array(xs.length)
for (var i = 0; i < xs.length; i++) {
result[i] = provider.graph_layout[xs[i]][%s]
}
return result
"""
# Add tooltips on each package tag
hover = HoverTool()
hover.tooltips = [
('name','@name'),
('version','@version'),
('link through/by','@req')
]
plot = render(current_rpm)
field.on_change('value', update)
level_slider.on_change('value', update_level)
back_bt.on_click(back)
# put the field, slider, button and plot in a layout and add to the document
layout = column(row(field, level_slider,back_bt), plot)
layout.sizing_mode ="scale_width"
curdoc().add_root(layout)