-
Notifications
You must be signed in to change notification settings - Fork 2
/
lastpymile.py
138 lines (106 loc) · 3.75 KB
/
lastpymile.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
from argparse import ArgumentParser,ArgumentTypeError
import logging, coloredlogs
import os, pathlib
import json
from lastpymile.utils import Utils
from lastpymile.maliciouscodepackageanalyzer import MaliciousCodePackageAnalyzer
class LastPyMileApplication():
@staticmethod
def __packageType(package):
if len(package.split(":")) >2:
raise ArgumentTypeError("Invlaid package name ")
return package
@staticmethod
def __logLevelType(x):
x = int(x)
if x==0:
return 100
elif x==1:
return logging.CRITICAL
elif x==2:
return logging.ERROR
elif x==3:
return logging.WARNING
elif x==4:
return logging.INFO
elif x==5:
return logging.DEBUG
else:
raise ArgumentTypeError("Log level must be between 0 and 5")
def __init__(self):
parser = ArgumentParser()
parser.add_argument(
'package',
type=str,
help='Package name can be in the form <package_name>:<package_version>. If no version is specified the latest version is retrieved.'
)
parser.add_argument(
'-lv', '--loglevel',
type=LastPyMileApplication.__logLevelType,
default=logging.INFO,
help='Log level. From 0(no log) to 5(debug). default(3)',
)
parser.add_argument(
'-f', '--reportfile',
type=str,
default=None,
help='Write the report to the specified file',
)
parser.add_argument(
'-o',
action='store_true',
help='Print the report to the screen',
)
args = parser.parse_args()
l=logging.getLogger("lastpymile")
coloredlogs.install(logger=l,level=args.loglevel)
rl=logging.getLogger("lastpymile_report")
rl.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(CustomFormatter())
rl.addHandler(ch)
try:
pakage=args.package.split(":")
pakage_name=pakage[0]
pakage_version=pakage[1] if len(pakage)==2 else None
current_folder=pathlib.Path().resolve()
tmp_folder=os.path.join(current_folder,"tmp")
if not os.path.exists(tmp_folder):
os.makedirs(tmp_folder)
package_analysis = MaliciousCodePackageAnalyzer.createAnaliysisForPackage(pakage_name,pakage_version, checked=True)
if package_analysis is not None:
analysis_report=package_analysis.startAnalysis()
json_report=json.dumps(analysis_report,indent=3)
if args.reportfile is not None:
with open(args.reportfile, "w") as f:
f.write(json_report)
if args.reportfile is None or args.o is True:
print(json_report)
except Exception as e:
import traceback
l.critical("Exception in main code: {}\n{}".format(e,traceback.format_exc()))
class CustomFormatter(logging.Formatter):
white= "\u001b[37m"
grey = "\x1b[38;21m"
green = "\u001b[32m"
orange = "\u001b[35m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
format = "Lastymile Report: %(message)s"
FORMATS = {
logging.DEBUG: white + format + reset,
logging.INFO: green + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
if __name__ == "__main__":
LastPyMileApplication()