-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsci_py_test_style
executable file
·79 lines (63 loc) · 2.41 KB
/
sci_py_test_style
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
#!/usr/bin/env python
import fnmatch
import glob
import os
from itertools import chain
from sys import argv
import pycodestyle
EXTS = ('.py', '.pyx')
SKIP_ERRORS = {'a_intro/sequences.py': ('E402', 'E711', 'E712')}
SCRIPTS = ('sci_py_example', 'sci_py_import_all', 'sci_py_test_style')
def check_style(fpath):
for exception_path, errors in SKIP_ERRORS.items():
if fpath.endswith(exception_path):
style = pycodestyle.StyleGuide(ignore=errors)
break
else:
style = pycodestyle.StyleGuide()
return style.check_files([fpath])
def package_files(package_dir):
# It would be better to use glob.iglob('**/*.py', recursive=True) but it
# isn't available before Python 3.5
def fpath_gen(ext):
for root, dirnames, filenames in os.walk(package_dir):
for filename in fnmatch.filter(filenames, '*'+ext):
yield os.path.join(root, filename)
fpaths = list(chain.from_iterable(fpath_gen(ext) for ext in EXTS))
setup_py = os.path.join(package_dir, '../setup.py')
fpaths.append(setup_py)
return fpaths
def main():
dir_name = argv[1]
script_dir = os.path.join(dir_name, 'bin')
package_dir = os.path.join(dir_name, 'scientific_python')
book_dir = os.path.join(dir_name, 'misc/book')
sample_proj_dir = os.path.join(dir_name, 'misc/sample_project/ser')
sample_proj_test_dir = os.path.join(dir_name, 'misc/sample_project/test')
fpaths = [os.path.join(script_dir, script) for script in SCRIPTS]
fpaths += package_files(package_dir)
fpaths += package_files(sample_proj_dir)
fpaths += package_files(sample_proj_test_dir)
fpaths += glob.glob(os.path.join(book_dir, '*.py'))
print(fpaths)
results = dict(filter(lambda x: x[1].total_errors != 0,
((fpath, check_style(fpath)) for fpath in fpaths)))
if len(results) > 0:
errors_per_file = (
(fpath, '\n'.join(r.get_statistics()))
for fpath, r in results.items()
)
errors = '\n'.join(
(' {}:\n{}'.format(fpath, errors)
for fpath, errors in errors_per_file)
)
msg = 'Style checker has found error{s}:\n{errors}'.format(
s='s'*bool(len(results) != 1),
errors=errors
)
print(msg)
raise RuntimeError(msg)
else:
print("Style check hasn't found any errors")
if __name__ == '__main__':
main()