-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_manager.py
586 lines (486 loc) · 22.2 KB
/
solution_manager.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import os
import xmltodict
import json
import package_manager
import nuget_api_manager
from pyvis.network import Network
def initialize_pyvis_network():
net = Network(
height="1080",
width="1920",
bgcolor="#fbfbfb",
font_color="black",
select_menu=True,
directed=True,
filter_menu=True,
)
net.set_options(
"""
const options = {
"edges": {
"arrows": {
"to": {
"enabled": true
}
},
"smooth": false
},
"layout": {
"hierarchical": {
"enabled": true,
"levelSeparation": 250,
"nodeSpacing": 350,
"treeSpacing": 650,
"edgeMinimization": true,
"sortMethod": "directed",
"parentCentralization": true
}
},
"interaction": {
"hover": true
},
"manipulation": {
"enabled": true
},
"physics": {
"enabled": false,
"hierarchicalRepulsion": {
"nodeDistance": 150,
"avoidOverlap": 1
},
"solver": "hierarchicalRepulsion"
}
}"""
)
return net
def generate_csproj_basic_graph(csproj_data):
print(f"Generating Basic Graph for {csproj_data.Name} project")
net = initialize_pyvis_network()
net.add_node(csproj_data.Name, label=csproj_data.Name, color="#88d184", level=0)
# add the project dependencies
project_references = csproj_data.ProjectReferences
if len(project_references) > 0:
for project in project_references:
net.add_node(project, label=project, color="#88d184", level=1)
# net.add_edge(csproj_data.Name, to=package)
net.add_edge(project, to=csproj_data.Name)
# add the assembly dependencies
package_references = csproj_data.PackageReferences
if len(package_references) > 0:
for package in package_references:
net.add_node(package, label=package, color="#b4c6ca", level=2)
# net.add_edge(csproj_data.Name, to=package)
net.add_edge(package, to=csproj_data.Name)
# add the nuget dependencies
nuget_dependencies = csproj_data.NuGetDependencies[csproj_data.Framework]
for dependency in nuget_dependencies:
net.add_node(
dependency.PackageName + "_" + dependency.VersionRange.Base,
label=dependency.PackageName + " : " + dependency.VersionRange.Base,
color="#2383ce",
level=3,
)
# net.add_edge(csproj_data.Name, to=dependency.PackageName + "_" + dependency.VersionRange.Base)
net.add_edge(
dependency.PackageName + "_" + dependency.VersionRange.Base,
to=csproj_data.Name,
)
print(net.get_nodes())
print(net.get_edges())
net.show(csproj_data.Name + "_basic_dependency_graph.html")
def generate_soln_basic_graph(solution_file_name, csproj_package_mapper):
print(f"Generating Basic Graph for {solution_file_name} solution")
net = initialize_pyvis_network()
net.add_node(
solution_file_name + "_sln", label=solution_file_name, color="#bd8bf3", level=0
)
for csproj_name in csproj_package_mapper:
net.add_node(csproj_name, label=csproj_name, color="#88d184", level=1)
net.add_edge(solution_file_name + "_sln", to=csproj_name)
# add the project dependencies
for csproj_name in csproj_package_mapper:
project_references = csproj_package_mapper[csproj_name].ProjectReferences
if len(project_references) > 0:
for project in project_references:
# net.add_edge(csproj_name, to=package)
net.add_edge(project, to=csproj_name)
# add the assembly dependencies
for csproj_name in csproj_package_mapper:
package_references = csproj_package_mapper[csproj_name].PackageReferences
if len(package_references) > 0:
for package in package_references:
net.add_node(package, label=package, color="#b4c6ca", level=2)
# net.add_edge(csproj_data.Name, to=package)
net.add_edge(package, to=csproj_name)
# add the nuget dependencies
for csproj_name in csproj_package_mapper:
pkg = csproj_package_mapper[csproj_name]
nuget_dependencies = pkg.NuGetDependencies[pkg.Framework]
for dependency in nuget_dependencies:
net.add_node(
dependency.PackageName + "_" + dependency.VersionRange.Base,
label=dependency.PackageName + " : " + dependency.VersionRange.Base,
color="#2383ce",
level=3,
)
package_label = ""
if pkg.DataType == "nuget":
package_label = pkg.Name + "_" + pkg.Version
elif pkg.DataType == "csproj":
package_label = pkg.Name
# net.add_edge(package_label, to=dependency.PackageName + "_" + dependency.VersionRange.Base)
net.add_edge(
dependency.PackageName + "_" + dependency.VersionRange.Base,
to=package_label,
)
print(net.get_nodes())
print(net.get_edges())
net.show(solution_file_name + "_basic_dependency_graph.html")
def generate_soln_deep_graph(solution_file_name, csproj_package_mapper, sub_packages):
print(f"Generating Deep Graph for {solution_file_name} solution")
net = initialize_pyvis_network()
net.add_node(
solution_file_name + "_sln", label=solution_file_name, color="#bd8bf3", level=0
)
for csproj_name in csproj_package_mapper:
net.add_node(csproj_name, label=csproj_name, color="#88d184", level=1)
net.add_edge(solution_file_name + "_sln", to=csproj_name)
# add the project dependencies
for csproj_name in csproj_package_mapper:
pkg = csproj_package_mapper[csproj_name]
net = generate_csproj_deep_net(net, pkg, sub_packages)
print(net.get_nodes())
print(net.get_edges())
net.show(solution_file_name + "_deep_dependency_graph.html")
def generate_csproj_deep_graph(csproj_package, sub_packages):
net = initialize_pyvis_network()
net = generate_csproj_deep_net(net, csproj_package, sub_packages)
print(net.get_nodes())
print(net.get_edges())
net.show(csproj_package.Name + "_deep_dependency_graph.html")
def generate_csproj_deep_net(net, csproj_package, sub_packages):
print(f"Generating Deep Graph for {csproj_package.Name} project")
net.add_node(
csproj_package.Name, label=csproj_package.Name, color="#88d184", level=0
)
# add the project dependencies
project_references = csproj_package.ProjectReferences
if len(project_references) > 0:
for project in project_references:
net.add_node(project, label=project, color="#88d184", level=1)
# net.add_edge(csproj_data.Name, to=package)
net.add_edge(project, to=csproj_package.Name)
net.add_edge(project, to=csproj_package.Name)
# add the assembly dependencies
package_references = csproj_package.PackageReferences
if len(package_references) > 0:
for package in package_references:
net.add_node(package, label=package, color="#b4c6ca", level=2)
# net.add_edge(csproj_data.Name, to=package)
net.add_edge(package, to=csproj_package.Name)
# add the nuget dependencies
def pd(pkg, sub_pkg, lvl):
# iterate over the keys. There will be just one key here
print(pkg.Name + " " + pkg.Framework)
if pkg.Framework != "":
nuget_dependencies = pkg.NuGetDependencies[pkg.Framework]
for dependency in nuget_dependencies:
net.add_node(
dependency.PackageName + "_" + dependency.VersionRange.Base,
label=dependency.PackageName + " : " + dependency.VersionRange.Base,
level=lvl,
)
if pkg.DataType == "nuget":
package_label = pkg.Name + "_" + pkg.Version
elif pkg.DataType == "csproj":
package_label = pkg.Name
# net.add_edge(package_label, to=dependency.PackageName + "_" + dependency.VersionRange.Base)
net.add_edge(
dependency.PackageName + "_" + dependency.VersionRange.Base,
to=package_label,
)
if (
dependency.PackageName + "_" + dependency.VersionRange.Base
) in sub_packages:
pd(
sub_packages[
dependency.PackageName + "_" + dependency.VersionRange.Base
],
sub_pkg,
lvl + 1,
)
pd(csproj_package, sub_packages, 3)
return net
def search_and_auto_generate_data(query, version):
package_data = nuget_api_manager.search_and_auto_generate_data(query)
if package_data is None or len(package_data) == 0:
return None
pkg_manager = package_manager.PackageManager("NuGet")
pkg_manager.initialize_from_nuget_data(package_data)
generate_catalog_data(pkg_manager, version)
return pkg_manager
def print_csproj_dependency(master_package, sub_packages, target_framework):
def pd(pkg, sub_pkg, fw, print_indent):
tabs = "\t" * print_indent
if target_framework not in pkg.NuGetDependencies:
# print(tabs + "N/A")
return
for dependency in pkg.NuGetDependencies[target_framework]:
print(tabs + dependency.PackageName + " : " + dependency.VersionRange.Base)
pd(sub_packages[dependency.PackageName], sub_pkg, fw, print_indent + 1)
print("Printing Dependency Tree ")
print(master_package.Name + " : " + master_package.Version)
pd(master_package, sub_packages, target_framework, 1)
def read_and_retrieve_csproj_data(solution_file, solution_path):
last_index = solution_path.rfind("\\")
solution_dir = solution_path[:last_index]
csproj_map = {}
for line in solution_file.readlines():
if 'Project("{' in line:
# hacking the code for now using spilt and replace
line = line.split("=")
# first would be the project and its identifier and second would contain string with ,
second = line[1]
second = second.split(",")
csproj = second[0].replace('"', "")
csproj = csproj.replace(" ", "")
csproj_path = second[1].replace('"', "")
csproj_path = csproj_path.replace(" ", "")
if ".csproj" in csproj_path:
csproj_map[csproj] = os.path.join(solution_dir, csproj_path)
return csproj_map
def read_and_deserialize_solution(soln_path):
soln_file = open(soln_path, "r")
csproj_map = read_and_retrieve_csproj_data(soln_file, soln_path)
soln_file.close()
return csproj_map
def generate_for_solution():
soln_path = ""
while True:
choice = input("Enter the full path of solution: ")
if not os.path.exists(choice):
print(choice + " not found")
else:
soln_path = choice
break
csproj_path_mapper = read_and_deserialize_solution(soln_path)
last_index = soln_path.rfind("\\")
solution_file_name = soln_path[last_index + 1 :]
solution_file_name = solution_file_name.replace(".sln", "")
# read all the csproj and put in map
csproj_package_mapper = {}
for csproj_name in csproj_path_mapper:
print("Reading " + csproj_name)
csproj_package_mapper[csproj_name] = read_and_deserialize_csproj(
csproj_path_mapper[csproj_name]
)
print("What would you like to do next:")
while True:
choice = input(
"1. Generate Package/NuGet Reference Graph for solution\n2. Generate Deep Graph\n3. Return to previous menu: "
)
if choice == "1":
# generate the basic graph
generate_soln_basic_graph(solution_file_name, csproj_package_mapper)
elif choice == "2":
# call the function:
# should take package list
# or should take whole as argument
# need to refactor
sub_packages = {}
framemworks_type_map = {"nuget": [], "csproj": []}
for csproj_name in csproj_path_mapper:
generate_nuget_dependency_for_csproj(
csproj_package_mapper[csproj_name], sub_packages, framemworks_type_map
)
generate_soln_deep_graph(
solution_file_name, csproj_package_mapper, sub_packages
)
elif choice == "3":
return
else:
print("Invalid Choice. Try Again!")
def read_and_deserialize_csproj(csproj_path):
csproj_file = open(csproj_path, "rb")
ordered_data_dict = xmltodict.parse(csproj_file)
csproj_file.close()
csproj_data = json.loads(json.dumps(ordered_data_dict))
csproj = package_manager.PackageManager(data_type="csproj")
csproj.initialize_from_csproj(csproj_data)
if csproj.Name is None or csproj.Name == "":
# get name from path
dirs = csproj_path.split("\\")
csproj.Name = dirs[-1].replace(".csproj", "")
return csproj
def generate_catalog_data(pkg_mgr, version):
if version == "":
version = pkg_mgr.LatestVersion
# set the version here
pkg_mgr.set_package_version(version)
# get the catalog data for the version
catalog_data = nuget_api_manager.get_catalog_entry_by_version_url(
pkg_mgr.PackageVersionURL
)
if catalog_data is None:
return "Invalid catalog data"
pkg_mgr.set_catalog_data_json(catalog_data)
def generate_nuget_dependency_for_csproj(csproj_data, sub_packages, frameworks_type_map):
# get frameworks for csproj
frameworks = frameworks_type_map["csproj"]
found = False
for framework in frameworks:
if framework in csproj_data.SupportedFrameworks:
csproj_data.Framework = framework
found = True
if not found:
print(f"[INTERRUPT] Choose framework to generate dependency for {csproj_data.Name}. Please enter the serial number: ")
csproj_data.print_available_frameworks_with_index()
while True:
choice = input("Enter your choice: ")
if not choice.isnumeric():
print("[WARN] Please enter the serial number!")
elif 1 <= int(choice) <= len(csproj_data.SupportedFrameworks):
csproj_data.Framework = csproj_data.SupportedFrameworks[int(choice) - 1]
if csproj_data.Framework in frameworks_type_map["csproj"]:
frameworks_type_map["csproj"].remove(csproj_data.Framework)
# add to type map
frameworks_type_map["csproj"].insert(0, csproj_data.Framework)
print("[INFO] Framework " + csproj_data.Framework + " selected for " + csproj_data.Name)
break
else:
print("[ERROR] Invalid Choice!")
visited_packages = {}
package_stack = []
# add the NuGet dependencies in stack
package_stack.extend(csproj_data.NuGetDependencies[csproj_data.Framework])
# generate dependencies:
while package_stack:
temp_package_list = []
while package_stack:
package = package_stack.pop(0)
if (
package.PackageName + "_" + package.VersionRange.Base
) not in sub_packages:
# search for package in nuget
sub_package_data = nuget_api_manager.search_and_auto_generate_data(
package.PackageName, package.VersionRange.Base
)
if sub_package_data is None or len(sub_package_data) == 0:
print(f"[WARN] No results found for {package.PackageName}")
else:
# refactor it
sub_pkg_mgr = package_manager.PackageManager("NuGet")
sub_pkg_mgr.initialize_from_nuget_data(sub_package_data)
# generate the catalog data
generate_catalog_data(sub_pkg_mgr, package.VersionRange.Base)
# add the name and version to the dictionary
# key:
# name_version
sub_packages[sub_pkg_mgr.Name + "_" + sub_pkg_mgr.Version] = sub_pkg_mgr
# get the dependencies and add to a list
# use the recently used fw
found = False
frameworks = frameworks_type_map["nuget"]
for framework in frameworks:
if framework in sub_pkg_mgr.NuGetDependencies:
sub_pkg_mgr.Framework = framework
# add the related dependencies
for dependency in sub_pkg_mgr.NuGetDependencies[sub_pkg_mgr.Framework]:
if dependency.PackageName not in sub_packages:
temp_package_list.append(dependency)
found = True
break
if not found:
if len(sub_pkg_mgr.SupportedFrameworks) == 1:
sub_pkg_mgr.Framework = sub_pkg_mgr.SupportedFrameworks[0]
# remove the entry from list
if sub_pkg_mgr.Framework in frameworks_type_map["nuget"]:
frameworks_type_map["nuget"].remove(sub_pkg_mgr.Framework)
# add to type map
frameworks_type_map["nuget"].insert(0, sub_pkg_mgr.Framework)
print(sub_pkg_mgr.Framework + " framework selected")
for dependency in sub_pkg_mgr.NuGetDependencies[sub_pkg_mgr.Framework]:
if dependency.PackageName not in sub_packages:
temp_package_list.append(dependency)
elif len(sub_pkg_mgr.SupportedFrameworks) > 1:
# get the list and add:
print(f"[INTERRUPT] Choose framework to generate dependency for {sub_pkg_mgr.Name}. Please enter the serial number:")
print(f"Frameworks selected for {csproj_data.Name}: ", frameworks_type_map["csproj"])
sub_pkg_mgr.print_available_frameworks_with_index()
while True:
choice = input("Enter your choice: ")
if not choice.isnumeric():
print("Please enter the serial number!")
elif 1 <= int(choice) <= len(sub_pkg_mgr.SupportedFrameworks):
sub_pkg_mgr.Framework = sub_pkg_mgr.SupportedFrameworks[int(choice) - 1]
# remove the entry from list
if sub_pkg_mgr.Framework in frameworks_type_map["nuget"]:
frameworks_type_map["nuget"].remove(sub_pkg_mgr.Framework)
# add to type map
frameworks_type_map["nuget"].insert(0, sub_pkg_mgr.Framework)
print(sub_pkg_mgr.Framework + " framework selected")
for dependency in sub_pkg_mgr.NuGetDependencies[sub_pkg_mgr.Framework]:
if dependency.PackageName not in sub_packages:
temp_package_list.append(dependency)
break
else:
print("[ERROR] Invalid Choice!")
else:
print(f"Framework not present for {sub_pkg_mgr.Name}")
if len(temp_package_list) > 0:
package_stack.extend(temp_package_list)
def generate_for_csproj():
csproj_path = ""
while True:
choice = input("Enter the csproj path or press E to return: ")
if str.lower(choice) == 'e':
return
if not os.path.exists(choice):
print("ERROR" + choice + " not found. Please try again!")
else:
csproj_path = choice
break
csproj_data = read_and_deserialize_csproj(csproj_path)
print("[INTERRUPT] What would you like to do: ")
while True:
choice = input(
"1. Display Package Details\n2. Generate Dependency Tree\n3. Return to Main Menu: "
)
if choice == "1":
csproj_data.print_package_data()
elif choice == "2":
sub_choice = ""
while True:
sub_choice = input(
"1. Generate Package/NuGet Reference Graph for project\n2. Generate Deep Graph\n3. Return to previous menu:"
)
if sub_choice == "1":
# generate the basic graph
generate_csproj_basic_graph(csproj_data)
elif sub_choice == "2":
sub_packages = {}
frameworks_type_map = {"nuget": [], "csproj": []}
generate_nuget_dependency_for_csproj(csproj_data, sub_packages, frameworks_type_map)
# framework_from_nuget = ""
# print_csproj_dependency(csproj_data, sub_packages, framework_from_nuget)
generate_csproj_deep_graph(csproj_data, sub_packages)
elif sub_choice == "3":
break
elif choice == "3":
return
else:
print("[ERROR] Choice. Please enter the correct choice:")
continue
def options():
print("[INTERRUPT] Generate for:")
while True:
choice = input("1. Solution\n2. csproj\n3. Return to previous menu: ")
if choice == "1":
generate_for_solution()
return
elif choice == "2":
generate_for_csproj()
elif choice == "3":
return
else:
print("[ERROR] Invalid Choice. Try again!")