-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_code_analyzer.py
More file actions
902 lines (745 loc) · 25.2 KB
/
script_code_analyzer.py
File metadata and controls
902 lines (745 loc) · 25.2 KB
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
"""
Description: This script analyze python files in a directory,
and extract the depency graph of the classes between themselves.
There are three type of dependencies processed here :
- First, the direct inheritance Child Class --[depend of parent]--> Parent Class
- Second, the direct class functions arguments types dependency
- Third, the indirect class functions attributes types dependency that are not initialized directly from the class functions arguments.
Author: Nathan Cerisara (https://github.com/nath54/)
License: MIT
"""
#
### Import modules ###
#
from typing import Any, Callable
#
import argparse
import ast
import os
import sys
import time
### Decorator to analyze time of function when called. ###
#
def decorator_timer(fn_wrapped: Callable[..., Any]) -> Callable[..., Any]:
"""
Decorator to measure execution time of a function.
Args:
fn_wrapped (Callable[..., Any]): Function to wrap and analyze time.
Returns:
Callable[..., Any]: Returns the wrapped function with the decorator.
"""
#
def wrapper(*args: list[Any], **kwargs: dict[str, Any]) -> Any:
#
### Init time of the function. ###
#
start_time: float = time.time()
#
### Call the contained function. ###
#
result: Any = fn_wrapped(*args, **kwargs)
#
### Calculate & display the execution time of the wrapped function. ###
#
tot_time: float = time.time() - start_time
#
name_of_wrapped_function: str = fn_wrapped.__name__
#
print(f"Function {name_of_wrapped_function} with the arguments (args={args}, kwargs={kwargs}) took {tot_time} to execute.")
#
### Returns the original return value of the wrapped function. ###
#
return result
#
### Return the wrapped function. ###
#
return wrapper
#
### Function to parse arguments for script. ###
#
def parse_args() -> argparse.Namespace:
"""
Function to parse arguments for script.
Returns:
argparse.Namespace: parsed arguments with default values.
"""
#
### Initialize parser ###
#
parser: argparse.ArgumentParser = argparse.ArgumentParser()
#
parser.add_argument('--path', type=str, default="./", help='Path to the folder with all the python files.')
parser.add_argument('--recursive', type=int, default=0, help='Path to the folder with all the python files.')
parser.add_argument('--ignore', type=list[str], default=[], nargs="+", help="List of Files & folder to ignore if they starts like this.")
parser.add_argument('--allow_duplicate_class_names', action="store_true", help="Allow duplicate class names instead of raising an error. But display a warning nevertheless.")
#
### Return parsed arguments ###
#
return parser.parse_args()
#
### Function to get the list of file paths to analyze. ###
#
@decorator_timer
def get_all_files_to_analyze(args: argparse.Namespace) -> list[str]:
"""
Get all Python file paths under the given path, optionally recursively, ignoring specified prefixes and this script file.
Args:
args (argparse.Namespace): Arguments given by the user to the program.
Raises:
FileNotFoundError: Error if the given path to list the files at doesn't exists.
Returns:
list[str]: The list of all the files to analyze (with their absolute full path each).
"""
#
### Initialize empty container for all the files to analyze result. ###
#
files_to_analyze: list[str] = []
#
### Calculate the absolute path from the arguments path of the folder to explore. ###
#
base_path: str = os.path.abspath(args.path)
#
### Determine the script's own filename to skip. ###
#
try:
#
script_filename = os.path.basename(__file__)
#
except NameError:
#
script_filename = os.path.basename(sys.argv[0])
#
### Prepare function to filter file names to ignore. ###
#
def should_ignore(name: str) -> bool:
#
return any(name.startswith(prefix) for prefix in args.ignore)
#
### If recursive exploration. ###
#
if args.recursive:
#
###
#
for root, dirs, files in os.walk(base_path):
#
### Filter directories to ignore. ###
#
dirs[:] = [d for d in dirs if not should_ignore(d)]
#
### Explore all the files. ###
#
for file in files:
#
### Filter files that are not python files. ###
#
if not file.endswith('.py'):
#
continue
#
### Filter files that asked to avoid. ###
#
if should_ignore(file):
#
continue
#
### Filter this script file. ###
#
if file == script_filename:
#
continue
#
### Add the file to the result list if it passed the filters. ###
#
files_to_analyze.append(os.path.join(root, file))
#
### Non recursive file listing. ###
#
else:
#
try:
#
### For all the files in the asked folded. ###
#
for entry in os.listdir(base_path):
#
### Create the full path by joining the folder with the file name. ###
#
fullpath = os.path.join(base_path, entry)
#
### Filter with files, and ensure they are python files. ###
#
if os.path.isfile(fullpath) and entry.endswith('.py'):
#
### Filter files that asked to avoid. ###
#
if should_ignore(entry):
#
continue
#
### Filter this script file. ###
#
if entry == script_filename:
#
continue
#
### Add the file to the result list if it passed the filters. ###
#
files_to_analyze.append(fullpath)
#
### Raise error if path doesn't exists. ###
#
except FileNotFoundError:
#
raise FileNotFoundError(f"Path '{base_path}' does not exist.")
#
### Return the final results of all the files that will be analyzed. ###
#
return files_to_analyze
#
### Function that pre-process the file content to analyze. ###
#
def pre_process_file_content(content: str) -> ast.Module:
"""
Pre-process file content by parsing into an AST.
Args:
content (str): Python text code content to analyze.
Raises:
SyntaxError: If the python code has a syntax error, raise it.
Returns:
ast.Module: Returns the Abstract Syntax Tree.
"""
#
try:
#
### Parse the python file content and extract its Abstract Syntax Tree. ###
#
tree: ast.Module = ast.parse(content)
#
### Return the final parsed Abstract Syntax Tree. ###
#
return tree
#
### Raise error if there was an error during parsing. ###
#
except SyntaxError as e:
#
raise SyntaxError(f"Error parsing content to AST: {e}")
#
### Function that extract all the class name from pre-processed content. ###
#
def get_file_class_names(pre_process: ast.Module) -> list[str]:
"""
Extract all class names defined in the AST.
Args:
pre_process (ast.Module): The pre-processed Astract Syntax Tree.
Returns:
list[str]: The list of all the classes that were defined in this file python code.
"""
#
### Init file classes names. ###
#
class_names_in_file: list[str] = []
#
### Explore all the nodes in the AST. ###
#
for node in ast.walk(pre_process):
#
### If the node is a class definition. ###
#
if isinstance(node, ast.ClassDef):
#
### Extract the node name. ###
#
class_names_in_file.append(node.name)
#
### Return file class names. ###
#
return class_names_in_file
#
### Helper function to extract recursively the type names from AST Annotations. ###
#
def _extract_annotation_names(node: ast.AST) -> list[str]:
"""
Helper to extract type names from annotation AST nodes.
Args:
node (ast.AST): annotation ast node to extract types from.
Returns:
list[str]: List of classes from given annotation ast node.
"""
#
### Initialize the list of all the type names from this annotation. ###
#
names: list[str] = []
#
### If it is directly a non recursive type name. ###
#
if isinstance(node, ast.Name):
names.append(node.id)
#
### If it is directly another non recursive type name. ###
#
elif isinstance(node, ast.Attribute):
#
### For attribute like module.ClassName, take ClassName. ###
#
names.append(node.attr)
#
### If it is a recursive type name. ###
#
elif isinstance(node, ast.Subscript):
#
### e.g., Optional[Type], List[Type], ... ###
#
value: ast.expr = node.value
slice_node: ast.expr = node.slice
#
names.extend(_extract_annotation_names(value))
#
### For Python 3.9+, slice can be ast.Constant or ast.Index ###
#
if hasattr(ast, 'Index') and isinstance(slice_node, ast.Index): # type: ignore
#
sub: ast.AST = slice_node.value # type: ignore
#
names.extend(_extract_annotation_names(sub)) # type: ignore
#
else:
#
names.extend(_extract_annotation_names(slice_node))
#
### If it is an iterable of types names. ###
#
elif isinstance(node, (ast.Tuple, ast.List)):
#
for elt in node.elts:
#
names.extend(_extract_annotation_names(elt))
#
### Return the final types from this type annotation node. ###
#
return names
#
### Function that extract all the depencies of rank 1 from pre-processed content. ###
#
def get_file_dependency_1(pre_process: ast.Module) -> dict[str, list[str]]:
"""
Extract inheritance dependencies: child class -> list of parent class names.
Args:
pre_process (ast.Module): The pre-processed Astract Syntax Tree.
Returns:
dict[str, list[str]]: Dependency of rank 1 graph.
"""
#
### Init file dependencies of rank 1. ###
#
dependency_1_in_file: dict[str, list[str]] = {}
#
### Explore all the nodes of the AST. ###
#
for node in ast.walk(pre_process):
#
### If the node is a Class Definition. ###
#
if isinstance(node, ast.ClassDef):
#
### Get the defined child class name. ###
#
child = node.name
#
### Initialize the list of all the parent class this child inherits from. ###
#
parents: list[str] = []
#
### Explore all the class names betweens the parenthesis of class def. ###
#
for base in node.bases:
#
if isinstance(base, ast.Name):
#
parents.append(base.id)
#
elif isinstance(base, ast.Attribute):
#
parents.append(base.attr)
# Other base types (e.g., Subscript) are less common for inheritance
#
if parents:
#
dependency_1_in_file[child] = parents
#
### Return file dependencies of rank 1. ###
#
return dependency_1_in_file
#
### Function that extract all the depencies of rank 2 from pre-processed content. ###
#
def get_file_dependency_2(pre_process: ast.Module) -> dict[str, list[str]]:
"""
Extract direct class functions argument type dependencies: class -> list of annotated type names in functions parameters.
Args:
pre_process (ast.Module): The pre-processed Astract Syntax Tree.
Returns:
dict[str, list[str]]: Dependency of rank 2 graph.
"""
#
### Init file dependencies of rank 2. ###
#
dependency_2_in_file: dict[str, list[str]] = {}
#
### Explore all the nodes of the AST. ###
#
for node in ast.walk(pre_process):
#
### If the node is a class definition. ###
#
if isinstance(node, ast.ClassDef):
#
### Get the name of the class. ###
#
class_name = node.name
#
### Explore all the elements of the class. ###
#
for item in node.body:
#
### Explore only the functions. ###
#
if isinstance(item, ast.FunctionDef):
#
### Initialize the type names list. ###
#
type_names: list[str] = []
#
### Skip 'self', and process function arguments. ###
#
for arg in item.args.args[1:]:
#
### Check if argument has type hint. ###
#
if arg.annotation:
#
extracted = _extract_annotation_names(arg.annotation)
#
type_names.extend(extracted)
#
### If type names where found, add them to the dependency list. ###
#
if type_names:
#
if class_name not in dependency_2_in_file:
#
dependency_2_in_file[class_name] = []
#
dependency_2_in_file[class_name].extend( type_names )
#
dependency_2_in_file[class_name] = list( set(dependency_2_in_file[class_name]) )
#
### Return file dependencies of rank 2. ###
#
return dependency_2_in_file
#
### Function that extract all the depencies of rank 3 from pre-processed content. ###
#
def get_file_dependency_3(pre_process: ast.Module) -> dict[str, list[str]]:
"""
Extract indirect class functions attribute type dependencies: class -> list of instantiated class names in function body, excluding direct assignments from arguments.
Args:
pre_process (ast.Module): The pre-processed Astract Syntax Tree.
Returns:
dict[str, list[str]]: Dependency of rank 3 graph.
"""
#
### Init file dependencies of rank 3. ###
#
dependency_3_in_file: dict[str, list[str]] = {}
#
### Process all the nodes of the AST. ###
#
for node in ast.walk(pre_process):
#
### If the node is a class definition. ###
#
if isinstance(node, ast.ClassDef):
#
### Get the class name. ###
#
class_name = node.name
#
### Explore all the elements of the class. ###
#
for item in node.body:
#
### If the element is a function definition. ###
#
if isinstance(item, ast.FunctionDef):
#
### Get all the function arguments. ###
#
init_args = {arg.arg for arg in item.args.args[1:]} # skip self
#
### Initialize the list of all the instiantiated variables that are not from function arguments. ###
#
instantiated: list[str] = []
#
### Explore all the elements of the function. ###
#
for stmt in item.body:
#
### Look for assignments ###
#
if isinstance(stmt, ast.Assign):
#
### Explore all the targets of the assignment. ###
#
for target in stmt.targets:
#
### Skip direct assignment from init args: self.attr = arg. ###
#
if isinstance(stmt.value, ast.Name) and stmt.value.id in init_args:
#
continue
#
### For instantiation: self.attr = SomeClass(...) ###
#
if isinstance(stmt.value, ast.Call):
#
func = stmt.value.func
#
if isinstance(func, ast.Name):
#
instantiated.append(func.id)
#
elif isinstance(func, ast.Attribute):
#
instantiated.append(func.attr)
# Could also handle annotated assignment: self.attr: Type = ... but skipping here
#
### Add all the types found to the final dictionary result. ###
#
if instantiated:
#
if class_name not in dependency_3_in_file:
#
dependency_3_in_file[class_name] = []
#
dependency_3_in_file[class_name].extend( instantiated )
#
dependency_3_in_file[class_name] = list( set(dependency_3_in_file[class_name]) )
#
### Return file dependencies of rank 3. ###
#
return dependency_3_in_file
#
### Main Function. ###
#
@decorator_timer
def main(args: argparse.Namespace) -> None:
"""
Main function to orchestrate analysis, display results, and optionally visualize.
Args:
args (argparse.Namespace): Parsed command lines arguments of this script.
Raises:
UserWarning: Error if duplicate class names.
"""
#
### Get all the files to analyze. ###
#
files_to_analyze: list[str] = get_all_files_to_analyze(args=args)
#
### All the analyzed classes names.
# Class names as keys, files where there are as value.
# Error if class name already exists. ###
#
all_class_names: dict[str, str] = {}
#
### Init the first dependency Graph.
# Child class names as keys, Parent class names as values. ###
#
dependency_1_direct_child: dict[str, list[str]] = {}
#
### Init the second dependency Graph.
# Class names that contains the attribute variable as keys,
# The Types class names as values. ###
#
dependency_2_argument_type: dict[str, list[str]] = {}
#
### Init the third dependency Graph.
# Class names that contains the attribute variable as keys,
# The Types class names as values. ###
#
dependency_3_attribute_type: dict[str, list[str]] = {}
#
### For all the file to analyze... ###
#
file_to_analyze: str
#
for file_to_analyze in files_to_analyze:
#
### Read the file content. ###
#
with open(file=file_to_analyze, mode="r", encoding="utf-8") as f:
#
content: str = f.read()
#
### Content Pre-Processing. ###
#
pre_process: Any = pre_process_file_content(content=content)
#
### Extract all the class names. ###
#
class_names_in_file: list[str] = get_file_class_names(pre_process=pre_process)
#
### Extract all the depencies of rank 1. ###
#
dependency_1_in_file: dict[str, list[str]] = get_file_dependency_1(pre_process=pre_process)
#
### Extract all the depencies of rank 2. ###
#
dependency_2_in_file: dict[str, list[str]] = get_file_dependency_2(pre_process=pre_process)
#
### Extract all the depencies of rank 3. ###
#
dependency_3_in_file: dict[str, list[str]] = get_file_dependency_3(pre_process=pre_process)
#
### Merge the class names and dependencies. ###
#
new_class_name: str
#
for new_class_name in class_names_in_file:
#
### Ensure class name doesn't exists. ###
#
if new_class_name in all_class_names:
#
if args.allow_duplicate_class_names:
#
print(f"\nWARNING: Duplicate class name detected: `{new_class_name}` !")
#
else:
#
raise UserWarning(f"Error: detected duplicate class names : `{new_class_name}` !")
#
### Add the new class name to visited classes. ###
#
all_class_names[new_class_name] = file_to_analyze
#
### Fusion the dictionnaries. ###
#
k: str
v: list[str]
#
for k, v in dependency_1_in_file.items():
#
dependency_1_direct_child[k] = v
#
for k, v in dependency_2_in_file.items():
#
dependency_2_argument_type[k] = v
#
for k, v in dependency_3_in_file.items():
#
dependency_3_attribute_type[k] = v
#
### Creating inverse class names dictionary. ###
#
inverse_class_dict: dict[str, list[str]] = {}
#
class_name: str
file_name: str
#
for class_name, file_name in all_class_names.items():
#
if file_name not in inverse_class_dict:
#
inverse_class_dict[file_name] = []
#
inverse_class_dict[file_name].append( class_name )
#
### Filtering everything here. ###
#
for class_name in dependency_1_direct_child:
#
dependency_1_direct_child[class_name] = [t for t in dependency_1_direct_child[class_name] if t in all_class_names]
#
for class_name in dependency_2_argument_type:
#
dependency_2_argument_type[class_name] = [t for t in dependency_2_argument_type[class_name] if t in all_class_names]
#
for class_name in dependency_3_attribute_type:
#
dependency_3_attribute_type[class_name] = [t for t in dependency_3_attribute_type[class_name] if t in all_class_names]
#
dependency_1_direct_child = {k: v for k, v in dependency_1_direct_child.items() if v}
dependency_2_argument_type = {k: v for k, v in dependency_2_argument_type.items() if v}
dependency_3_attribute_type = {k: v for k, v in dependency_3_attribute_type.items() if v}
#
### End of analyzis, display the results. ###
#
res_txt: str = ""
#
### Prepare result text. ###
#
lines: list[str] = []
#
lines.append("\nClasses found and their file locations:")
#
for fn, cls in inverse_class_dict.items():
#
lines.append(f"\n- {fn}:\n\t * {'\n\t * '.join(cls)}\n")
#
lines.append("\nInheritance dependencies (Child -> Parents):")
#
if dependency_1_direct_child:
#
for child, parents in dependency_1_direct_child.items():
#
lines.append(f"\n- {child}:\n\t * {'\n\t * '.join(parents)}\n")
#
else:
#
lines.append("- None detected.")
#
lines.append("\nDirect class functions argument type dependencies (Class -> Types):")
#
if dependency_2_argument_type:
#
for cls, types in dependency_2_argument_type.items():
#
lines.append(f"\n- {cls}:\n\t * {'\n\t * '.join(types)}\n")
#
else:
#
lines.append("- None detected.")
#
lines.append("\nIndirect class functions attribute instantiation dependencies (Class -> Types):")
#
if dependency_3_attribute_type:
#
for cls, types in dependency_3_attribute_type.items():
#
lines.append(f"\n- {cls}:\n\t * {'\n\t * '.join(types)}\n")
#
else:
#
lines.append("- None detected.")
#
res_txt = "\n".join(lines)
#
print(res_txt)
#
### Main Entry Point of the Program. ###
#
if __name__ == "__main__":
#
### Get the command line arguments for the script. ###
#
args: argparse.Namespace = parse_args()
#
### Call the Main function. ###
#
main(args=args)
#
### End of the program. ###
#
print(f"\nEnd of program.\n")