-
Notifications
You must be signed in to change notification settings - Fork 17
/
check_print_and_pdb.py
executable file
·53 lines (48 loc) · 2.05 KB
/
check_print_and_pdb.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
#!/usr/bin/env python
import ast
import sys
import os
import logging
"""
This script check py file to get "print" or "pdb" sentence.
If you need print:
you can change the sentence "print" by sys.stdout.write()
More info here:
http://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python
Fix changing " print 'spam' " by
import sys
sys.stdout.write('spam\n')
"""
def check_custom_lint(dir_path):
for dirname, dirnames, filenames in os.walk(dir_path):
for filename in filenames:
fext = os.path.splitext(filename)[1]
if fext == '.py':
fname_path = os.path.join(dirname, filename)
try:
with open(fname_path) as fin:
parsed = ast.parse(fin.read())
except:
parsed = None
pass
if parsed:
for node in ast.walk(parsed):
if isinstance(node, ast.Print):
#logging.warning( '{}:{}: [print sentence] "print" sentence detected'.format(\
#fname_path, node.lineno) )
print '{}:{}: [print sentence] "print" sentence detected'.format(\
fname_path, node.lineno)
elif isinstance(node, ast.Import):
for import_name in node.names:
if import_name.name == 'pdb':
#logging.warning( '{}:{}: [import pdb sentence] "import pdb" sentence detected'.format(\
#fname_path, node.lineno) )
print '{}:{}: [import pdb sentence] "import pdb" sentence detected'.format(\
fname_path, node.lineno)
def main():
if len( sys.argv ) == 2 and os.path.isdir(sys.argv[1]):
check_custom_lint(sys.argv[1])
else:
logging.warning("First param should be directoy path to check")
if __name__ == '__main__':
exit(main())