Repository navigation
Expand file tree
/
Copy pathcheck_python_shebang.py
More file actions
157 lines (136 loc) · 4.63 KB
/
Copy pathcheck_python_shebang.py
File metadata and controls
157 lines (136 loc) · 4.63 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
import collections
import pathlib
import re
import typing
import sys
WarningType = collections.namedtuple(
"WarningType",
field_names=[
"file",
"function",
"line_number",
"text",
],
)
class CheckSomethingTemplate:
#
# CONSTRUCTOR
#
def __init__(
self,
root: pathlib.Path,
known_exceptions: typing.List[typing.Union[pathlib.Path, str]],
):
self.__root: pathlib.Path = root
self.__warnings: typing.List[WarningType] = []
self.__number_of_files_checked: int = 0
self.__known_exceptions: typing.List[pathlib.Path] = known_exceptions
#
# PRIVATE
#
#
# PUBLIC
#
def get_number_of_files_checked(self) -> int:
return self.__number_of_files_checked
def get_warnings(self) -> typing.List[WarningType]:
return self.__warnings
def perform_check_on_directory(self) -> None:
stk_todo = [self.__root]
stk_done = []
while len(stk_todo) > 0:
f = stk_todo.pop(0)
if f.is_dir():
# IF we have reached the /.github directory
# THEN do not delve into it
if f.name == ".github":
continue
# IF we have reached the /.venv directory
# THEN do not delve into it
if f.name == ".venv":
continue
# IF we have reached the /tests directory
# THEN do not delve into it
if f.name == "tests":
continue
stk_todo += [subdir for subdir in f.iterdir()]
else:
if f.name.endswith(".py"):
stk_done += [f]
# filter out known_exceptions
# fmt: off
known_exceptions_01 = [x for x in self.__known_exceptions if isinstance(x, pathlib.Path)]
known_exceptions_02 = [re.compile(x) for x in self.__known_exceptions if isinstance(x, str)]
stk_done = [x for x in stk_done if x not in known_exceptions_01]
stk_done = [x for x in stk_done if not any([y.match(x.name) is not None for y in known_exceptions_02])]
# fmt: on
# perform_check_on_file for each item in stk_done
stk_done.sort(key=lambda x: x.name)
for f in stk_done:
self.perform_check_on_file(f)
def perform_check_on_file(self, file: pathlib.Path) -> None:
self.__number_of_files_checked += 1
lines: typing.List[str] = []
try:
with open(file, "r") as fh:
lines = fh.readlines()
except:
return
if len(lines) < 2 or lines[0] != "#!/usr/bin/env python\n":
self.__warnings += [
WarningType(
file=file,
function=None,
line_number=0,
text=f"Missing #!/usr/bin/env python",
)
]
return
if lines[1] != "# -*- coding: utf-8 -*-\n":
self.__warnings += [
WarningType(
file=file,
function=None,
line_number=1,
text=f"Missing # -*- coding: utf-8 -*-",
)
]
return
if __name__ == "__main__":
checker = CheckSomethingTemplate(
root=pathlib.Path(sys.argv[1]),
known_exceptions=[],
)
checker.perform_check_on_directory()
warnings: typing.List[WarningType] = checker.get_warnings()
# IF everything passed
# THEN display pass text
if len(warnings) == 0:
print(
f"Finished checking {checker.get_number_of_files_checked()} file(s), everything OK"
)
# IF there are warnings
# THEN display them
if len(warnings) > 0:
print(
f"Finished checking {checker.get_number_of_files_checked()} file(s), found {len(warnings)} warnings:"
)
for w in warnings:
if (
w.file is not None
and w.function is not None
and w.line_number is not None
and w.text is not None
):
print(f"{w.file}:{w.function}:{w.line_number} {w.text}")
continue
if w.file is not None and w.line_number is not None and w.text is not None:
print(f"{w.file}:{w.line_number} {w.text}")
continue
if w.file is not None and w.text is not None:
print(f"{w.file} {w.text}")
continue
if w.file is not None:
print(f"{w.file}")
continue
sys.exit(1)