-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_requires.py
executable file
·293 lines (260 loc) · 10.3 KB
/
graph_requires.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
# for python3.6 (as some limitation compare to 3.7 or 3.11....)
"""
quick tool to find package which requires another
this will draw a graph
"""
import subprocess
import xml.etree.ElementTree as ET
from pathlib import Path
import sys
import shutil
import os
from datetime import date
def cmd_exists(cmd):
"""
check a command exist
"""
return shutil.which(cmd) is not None
def check_binairies():
"""
check everything is available on the system
"""
if cmd_exists("zypper") is False:
print("Sounds like you are not using a SUSE/openSUSE system....\n")
exit(1)
if cmd_exists("dot") is False:
print("dot is not available, please install graphviz\n")
exit(1)
def get_pretty_name() -> str:
"""
grab the os name
"""
try:
with open('/etc/os-release', 'r') as file:
for line in file:
if line.startswith('PRETTY_NAME='):
# Remove the 'PRETTY_NAME=' part and strip surrounding quotes
pretty_name = line.strip().split('=', 1)[1].strip('"')
return pretty_name
except FileNotFoundError:
return "The file '/etc/os-release' does not exist."
def get_version() -> str:
"""
grab the version name
"""
try:
with open('/etc/os-release', 'r') as file:
for line in file:
if line.startswith('VERSION='):
version = line.strip().split('=', 1)[1].strip('"')
return version
except FileNotFoundError:
return "The file '/etc/os-release' does not exist."
def get_package_dependencies(package, depth=0, seen_packages=None):
"""
Retrieve the list of package dependencies
"""
if seen_packages is None:
seen_packages = set()
# avoid infinite loop
if depth > 1:
return []
try:
# command could be an option
#command = ["zypper", "-q", "-x", "search", "--requires", package]
command = ["zypper", "-q", "-x", "search", "--requires-pkg", package]
#print(command)
result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, _error = result.communicate()
if result.returncode != 0:
return []
root = ET.fromstring(output.decode())
dependencies = {}
for solvable in root.findall(".//solvable"):
name = solvable.get("name")
# avoid same name package
if name == package:
pass
# avoid debuginfo package
if name and "debuginfo" in name:
pass
elif name and name not in seen_packages:
print(f"check deps of {name}")
seen_packages.add(name)
requires_pkg = get_package_dependencies(name, depth + 1, seen_packages)
dependencies[name] = requires_pkg
return dependencies
except subprocess.CalledProcessError as err:
print(f"Error executing zypper command: {err}")
return []
except ET.ParseError as err:
print(f"Error parsing XML: {err}")
return []
def check_seen_before(filename, pattern):
"""
check if there is a need to recreate a deps between two packages
"""
try:
with open(filename, 'r') as file:
line_number = 0
found = False
for line in file:
line_number += 1
if pattern in line:
print(f"{pattern} found in line {line_number}: {line.strip()}")
found = True
return True
if not found:
print(f"{pattern} not found in the file {filename}.")
return False
except FileNotFoundError:
print(f"The file '{filename}' does not exist.")
def pre_dot(package_n, filename, directory="/tmp/graph"):
"""
prepare the dot file
"""
filed = directory +"/"+ filename
with open(filed, "w") as dotf:
dotf.write("digraph PackageDependencies {\n")
osrelease = get_pretty_name()
current_date = date.today()
label = "Requires "+package_n+" - "+osrelease+" ("+str(current_date)+")"
dotf.write(f"graph [label=\"{label}\", layout=fdp, fontsize=32, fontname=\"Arial\"];\n")
dotf.write("node [shape=box, style=filled, fillcolor=lightgreen];\n")
#dotf.write(f"\"{package_n}\" [shape=circle, style=filled, fillcolor=lightblue];\n")
dotf.write("edge [arrowhead=vee];\n")
dotf.close()
def close_dot(filename, directory="/tmp/graph"):
"""
close the dot file
"""
filea = directory +"/"+ filename
with open(filea, "a") as dotf:
dotf.write("}\n")
dotf.close()
print(f"DOT file '{filea}' generated successfully.")
def generate_dot_file(deps, package_n, filename, directory="/tmp/graph"):
"""
Generate a DOT file from the package dependencies.
"""
fileb = directory +"/"+ filename
with open(fileb, 'w') as dotf:
pass
dotf.close()
if isinstance(deps, dict):
for package, requires_pkg in deps.items():
if isinstance(requires_pkg, dict):
if requires_pkg:
for dep_package in requires_pkg:
if package_n == package:
# first level of requires
with open(fileb, "a") as dotf:
dotf.write(f'"{dep_package}" -> "{package}" [color=red, style=dotted];\n')
dotf.close()
else:
# second level of requires so graph as an ellipse
pattern = '"'+dep_package+'"'+' [shape=ellipse' #, style=filled, fillcolor=pink];'
if check_seen_before(fileb, pattern) is False:
with open(fileb, "a") as dotf:
dotf.write(f"\"{dep_package}\" [shape=ellipse, style=filled, fillcolor=pink];\n")
dotf.close()
pattern = "\"" + package + "\"" + " -> " + "\"" + package_n + "\""
if check_seen_before(fileb, pattern) is False:
with open(fileb, "a") as dotf:
dotf.write(f'"{dep_package}" -> "{package}" -> "{package_n}" [color=green, color=red, style=dotted];\n')
dotf.close()
else:
with open(fileb, "a") as dotf:
dotf.write(f'"{dep_package}" -> "{package}" [color=blue, style=dotted];\n')
dotf.close()
#else:
# print(f"{deps} is neither a dictionary nor a list.")
def remove_suffix(text, suffix):
"""
remove the end
"""
if text.endswith(suffix):
return text[:-len(suffix)]
return text
def clean_up_dot_file(tag, filename="dependencies", directory="/tmp/graph"):
"""
remove unwanted link in dot file
"""
file = directory +"/"+ filename+".dot"
with open(file, 'r') as dotf:
lines = dotf.readlines()
tag_with_semicolon = f'-> "{tag}";'
modified_lines = [remove_suffix(line.rstrip(), tag_with_semicolon) + '\n' for line in lines]
with open(file, 'w') as dotf:
dotf.writelines(modified_lines)
def generate_image(filename="dependencies", extension="jpg", directory="/tmp/graph"):
"""
generate the image file
the py graphviz package in not available on SLES for py3.6
so keeping it by command line
"""
version = get_version()
image_filename = directory+"/"+filename+"_"+version+"."+extension
try:
command = ["dot", "-T"+extension, directory+"/"+filename+".dot", "-o", image_filename]
result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_output, _error = result.communicate()
my_file = Path(image_filename)
if my_file.is_file():
print(f"Image file '{image_filename}' generated successfully.")
else:
print(f"Error generating Image file: {image_filename}, \ncommand was: {command}")
except Exception as err:
print(f"Error generating Image file: {err}")
if __name__ == "__main__":
"""
Main stuff which do everything
"""
check_binairies()
print("\nGenerate a graph of all packages that require any of the provides of a package.")
print("This use zypper on current system, so using the actual Repositories set on the system.\n")
if len(sys.argv) < 2:
print("Usage: python3 graph.py PACKAGE_NAME,PACKAGE_NAME1,PACKAGE_NAME2 [OPTIONNAL_DIRECTORY]")
sys.exit(1)
PACKAGE_N = sys.argv[1].strip().split(",")
PACKAGE_NAMES = [pkg.strip() for pkg in PACKAGE_N]
if len(sys.argv) > 2:
WDIR = sys.argv[2].strip()
if not os.path.exists(WDIR):
os.makedirs(WDIR)
else:
WDIR = "/tmp/graph"
if not PACKAGE_NAMES:
print("Please enter a valid package name (or multiple separate by comma)")
exit(1)
RESULT = '_'.join(PACKAGE_NAMES)
# limit name to 64 characters...
max_length = 64
if len(RESULT) > max_length:
RESULT = RESULT[:max_length]
print("Name limited to 64 characters.")
ALL_DOT_FILES_LIST = []
# calculate deps and generates dict
for pkg in PACKAGE_NAMES:
print(f"Working with package {pkg}")
DEPENDENCIES_PKG = get_package_dependencies(pkg)
print(f"Generating {WDIR}/{pkg}.dot file")
generate_dot_file(DEPENDENCIES_PKG, pkg, pkg+"_tmp.dot", directory=WDIR)
if os.path.exists(WDIR+"/"+pkg+"_tmp.dot"):
ALL_DOT_FILES_LIST.append(WDIR+"/"+pkg+"_tmp.dot")
# generate dot files for each packages and merge content in one file
pre_dot(RESULT, RESULT+".dot", directory=WDIR)
with open(WDIR+"/"+RESULT+".dot", 'a') as merged:
for file_path in ALL_DOT_FILES_LIST:
with open(file_path, 'r') as file:
file_contents = file.read()
merged.write(file_contents)
merged.close()
close_dot(RESULT+".dot", directory=WDIR)
# generate the image
generate_image(filename=RESULT, extension="jpg", directory=WDIR)
# clean up removing pkg dot file
for file_path in ALL_DOT_FILES_LIST:
os.remove(file_path)