-
Notifications
You must be signed in to change notification settings - Fork 2
/
appnalyser_driver.py
executable file
·98 lines (77 loc) · 2.69 KB
/
appnalyser_driver.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
#!/usr/bin/env python3
import argparse
import os
import json
import subprocess
import driver
from bundle.bundle import Bundle
class AppnalyseDriver(driver.Driver):
def __init__(self) -> None:
super().__init__('appnalyser_driver')
def analyse(self, app: Bundle, out_dir: str) -> driver.Result:
out_fn = os.path.join(out_dir, 'appnalyse.json')
# Skip application if we already obtained results
if os.path.exists(out_fn):
self.logger.warning(f"App already appnalysed: {app.filepath}. Skipping.")
return driver.Result.SKIPPED
# Run appnalyser for the given application
try:
subprocess.run
appnalyser = subprocess.run(
['./appnalyser.py', app.filepath],
capture_output=True,
text=True,
timeout=60,
)
except subprocess.TimeoutExpired:
self.logger.error(f"Timed out: {app.filepath}")
return driver.Result.ERROR
if appnalyser.returncode != 0:
self.logger.error(
"appnalyser returned {} for {}: {}".format(
appnalyser.returncode,
app.filepath,
appnalyser.stderr,
)
)
return driver.Result.ERROR
try:
json.loads(appnalyser.stdout)
except json.JSONDecodeError:
self.logger.error(
f"appnalyzer did not produce valid JSON for {app.filepath}: {appnalyser.stdout}"
)
return driver.Result.ERROR
# Store results
os.makedirs(out_dir, exist_ok=True)
with open(out_fn, 'w') as fp:
fp.write(appnalyser.stdout)
self.logger.info(f"Successfully appnalysed {app.filepath}")
return driver.Result.OK
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
'-t', '--type',
choices=driver.Selection.choices(),
default=driver.Selection.ALL,
help="Only analyse applications specified by type. (default 'all')",
)
parser.add_argument(
'applications',
help="""
Path to the directory, where applications are installed. This
folder will be traversed recursively.
""",
)
parser.add_argument(
'output',
help="Path to where the results should be stored",
)
args = parser.parse_args()
selection = driver.Selection(args.type)
apps_dir = os.path.expanduser(args.applications)
out_dir = os.path.expanduser(args.output)
appnalyse = AppnalyseDriver()
appnalyse.run(apps_dir, out_dir, selection)
if __name__ == '__main__':
main()