-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_analyzer.py
188 lines (153 loc) · 6.91 KB
/
code_analyzer.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
import argparse
import ast
import re
import os
from collections import defaultdict
class PepAnalyzer(ast.NodeVisitor):
def __init__(self):
self.stats: dict[str, dict[int, list]] = {
"variables": defaultdict(list),
"parameters": defaultdict(list),
"is_constant_default": defaultdict(list),
}
def visit_Name(self, node):
if isinstance(node.ctx, ast.Store):
self.stats["variables"][node.lineno].append(node.id)
self.generic_visit(node)
def visit_FunctionDef(self, node):
for a in node.args.args:
self.stats["parameters"][node.lineno].append(a.arg)
for a in node.args.defaults:
self.stats["is_constant_default"][node.lineno].append(isinstance(a, ast.Constant))
self.generic_visit(node)
def get_parameters(self, lineno: int) -> list:
return self.stats["parameters"][lineno]
def get_variables(self, lineno: int) -> list:
return self.stats["variables"][lineno]
def get_mutable_defaults(self, lineno: int) -> str:
for param_name, is_default in zip(self.stats["parameters"][lineno], self.stats["is_constant_default"][lineno]):
if not is_default:
return param_name
return ""
def input_path() -> str:
parser = argparse.ArgumentParser(usage="Static Code Analyzer")
parser.add_argument("files", help="takes a single file or folder path")
args = parser.parse_args()
return args.files
def analyze_pathname(pathname: str):
if os.path.isfile(pathname):
return analyze_file(pathname)
if os.path.isdir(pathname):
scripts: list = os.listdir(pathname)
for script in scripts:
script_path: str = os.path.join(pathname, script)
analyze_file(script_path)
def analyze_file(filename: str):
preceding_blank_line_counter: int = 0
with open(filename) as f:
tree = ast.parse(f.read())
pep_analyzer = PepAnalyzer()
pep_analyzer.visit(tree)
f.seek(0)
for i, line in enumerate(f, start=1):
if line == "\n":
preceding_blank_line_counter += 1
continue
error_source: str = f"{filename}: Line {i}:"
if len(line) > 79:
print(error_source, "S001 Too long")
if re.match(r"(?!^( {4})*[^ ])", line):
print(error_source, "S002 Indentation is not a multiple of four")
if re.search(r"^([^#])*;(?!\S)", line):
print(error_source, "S003 Unnecessary semicolon")
if re.match(r"[^#]*[^ ]( ?#)", line):
print(error_source, "S004 At least two spaces before inline comment required")
if re.search(r"(?i)# *todo", line):
print(error_source, "S005 TODO found")
if preceding_blank_line_counter > 2:
print(error_source, "S006 More than two blank lines used before this line")
preceding_blank_line_counter = 0
if re.match(r"^([ ]*(?:class|def) ( )+)", line):
print(error_source, "S007 Too many spaces after construction_name (def or class)")
if matches := re.match(r"^(?:[ ]*class (?P<name>\w+))", line):
if not re.match(r"(?:[A-Z][a-z0-9]+)+", matches["name"]):
print(error_source, f'S008 Class name {matches["name"]} should use CamelCase')
if matches := re.match(r"^(?:[ ]*def (?P<name>\w+))", line):
if not re.match(r"[a-z_]+", matches["name"]):
print(error_source, f'S009 Function name {matches["name"]} should use snake_case')
for parameter in pep_analyzer.get_parameters(i):
if not re.match(r"[a-z_]+", parameter):
print(error_source, f"S010 Argument name '{parameter}' should be snake_case")
break
for variable in pep_analyzer.get_variables(i):
if not re.match(r"[a-z_]+", variable):
print(error_source, f"S011 Variable '{variable}' in function should be snake_case")
break
if pep_analyzer.get_mutable_defaults(i):
print(error_source, "S012 Default argument value is mutable")
def main():
analyze_pathname(input_path())
if __name__ == "__main__":
main()
# import re
# import argparse
# import os
# import glob
#
# def main():
# def check_file(file):
# with open(file, 'r') as f:
# preceding_blank_line_counter: int = 0
# for i, line in enumerate(f, start=1):
# if line == "\n":
# preceding_blank_line_counter += 1
# continue
#
# if len(line) > 79:
# print(f"{file}: Line {i}:", "S001 Too long")
#
# if re.match(r"(?!^( {4})*[^ ])", line):
# print(f"{file}: Line {i}:", "S002 Indentation is not a multiple of four")
#
# if re.search(r"^([^#])*;(?!\S)", line):
# print(f"{file}: Line {i}:", "S003 Unnecessary semicolon")
#
# if re.match(r"[^#]*[^ ]( ?#)", line):
# print(f"{file}: Line {i}:", "S004 At least two spaces before inline comment required")
#
# if re.search(r"(?i)# *todo", line):
# print(f"{file}: Line {i}:", "S005 TODO found")
#
# if preceding_blank_line_counter > 2:
# print(f"{file}: Line {i}:", "S006 More than two blank lines used before this line")
# preceding_blank_line_counter = 0
#
# if len(re.findall(r"class ", line)) != 0:
# construction_name = re.findall(r"class +(\w+)", line)[0]
# print(f"{file}: Line {i}: S007 Too many spaces after {construction_name}")
# if len(re.findall(r"def (\w+)", line)) != 0:
# construction_name = re.findall(r"def (\w+)", line)[0]
# print(f"{file}: Line {i}: S007 Too many spaces after {construction_name}")
#
# if re.match(r"class +[a-z][\w]*", line):
# class_name = re.findall(r"class +([a-z][\w]*)", line)[0]
# print(f"{file}: Line {i}: S008 Class name '{class_name}' should use CamelCase")
# if re.match(r"def +[A-Z][\w]*", line):
# func_name = re.findall(r"def +([A-Z][\w]*)", line)[0]
# print(f"{file}: Line {i}: S009 Function name '{func_name}' should use snake_case")
#
# if line != "\n":
# preceding_blank_line_counter = 0
#
# parser = argparse.ArgumentParser()
# parser.add_argument('name', type=str)
# args = parser.parse_args()
# path = args.name
# if os.path.isfile(path):
# paths = [path]
# else:
# paths = glob.glob(os.path.join(path, '*.py'))
# for p in paths:
# check_file(p)
# if __name__ == "__main__":
# main()