forked from infobyte/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
py3-checker.py
133 lines (110 loc) · 4.25 KB
/
py3-checker.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
#!/usr/bin/env python3
# Faraday Penetration Test IDE
# Copyright (C) 2019 Infobyte LLC (http://www.infobytesec.com/)
# See the file 'doc/LICENSE' for the license information
"""
Internal script used to detect if the auto-claimer v3 python files
are actually executable with python3
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import argparse
import logging
import os
import re
from pylint import epylint as lint
BLACK_LIST = ["build"]
# Pylint code Message Final return code
# 0 Ok 0
# 1 Fatal message issued 1
# 2 Error message issued 0
# 4 Warning message issued 0
# 8 Refactor message issued 0
# 16 Convention message issued 0
# 32 Usage error 1
OK = 0
FATAL = 1
ERROR = 2
WARNING = 4
REFACTOR = 8
CONVENTION = 16
USAGE = 32
PY3_MSG = r"# I'm Py3"
def find_py3_msg(path):
with open(path) as origin_file:
for line in origin_file:
line = re.search(PY3_MSG, line)
if line:
return True
return False
class Analyser:
def __init__(self):
logging.getLogger().setLevel(getattr(logging, args.log_level.upper()))
self.logger = logging # TODO FIXME
def analyse_file(self, path):
if path[-3:] != ".py":
return 0, 0, [], []
find_py3_ok_result = find_py3_msg(path)
lint_ok_result = lint.lint(path, ["--py3k"]) in [OK]
error_list = []
if find_py3_ok_result and not lint_ok_result:
self.logger.error("The auto-claimed python file {path} as python is not python3".format(path=path))
error_list.append(path)
if not find_py3_ok_result and lint_ok_result:
self.logger.info("The file {path} is python3, adding the signature comment in the last line".format(path=path))
with open(path,"a+") as py3_file:
py3_file.writelines(["\n\n", PY3_MSG, "\n"])
if not lint_ok_result:
self.logger.info("The file {path} is python2".format(path=path))
return 1 if lint_ok_result else 0, 1, [], error_list
def analyse_folder(self, parent_path):
are3, total, strs, error_files = 0, 0, [], []
for subpath in os.listdir(parent_path):
if subpath[0] != '.' and subpath not in BLACK_LIST:
path = os.path.join(parent_path, subpath)
s_are3, s_total, s_strs, s_error_files = \
self.analyse_folder(path) \
if not os.path.isfile(path) \
else self.analyse_file(path)
are3 += s_are3
total += s_total
strs.extend(s_strs)
error_files.extend(s_error_files)
if are3 != total and total > 0:
prtg = 100.0*are3/total
strs.append('Analysed {path}, {are3}/{total} {prtg}%'
.format(path=parent_path, are3=are3, total=total, prtg=prtg))
return are3, total, strs, error_files
def run(self):
are3, total, strs, error_files = self.analyse_folder(os.getcwd())
for s in strs:
self.logger.info(s)
if len(error_files) > 0:
for error_file in error_files:
self.logger.error("The auto-claimed python file {path} as python is not python3".format(path=error_file))
raise Exception("One or more auto-claimed python file(s) as python is(are) not python3")
if are3 == total:
print("100% coverage")
def main(filename):
PYTLINT = ".pylintrc"
RENAMED = ".to_be_renamed"
os.rename(PYTLINT, RENAMED)
if filename:
import sys
sys.stdout = open(filename, 'w')
try:
Analyser().run()
finally:
os.rename(RENAMED, PYTLINT)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--only-coverage', default=False)
parser.add_argument('-l', '--log-level', default='debug')
parser.add_argument('-o', '--output-file', dest='filename', default=None)
args = parser.parse_args()
import time
t = time.time()
main(args.filename)
print(t - time.time())
# pylint --py3k