-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_analysis_final.py
More file actions
337 lines (303 loc) · 13.2 KB
/
batch_analysis_final.py
File metadata and controls
337 lines (303 loc) · 13.2 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
"""
batch_analysis.py
Usage:
python3 batch_analysis.py npm_top_10k/extracted
This script:
1. Iterates through every subdirectory (package) inside <extracted_root>
and runs extract_features.py to populate:
<parent>/Analysis/<pkgname>/
2. Runs compile_scores.py on <parent>/Analysis (and passes <extracted_root>)
to produce a consolidated per-package score TSV, with PACKAGE_SIZE_BYTES
computed from the real extracted packages when possible.
3. Runs generate_package_features.py on that TSV to produce an ML-ready
features CSV.
4. Runs generate_scan_results.py on the features CSV, using the trained
model found in:
Analysis Codes/classification_configuration/
to classify each package as LOW / MEDIUM / HIGH risk.
5. Runs merge_preinstall_risk.py to fold preinstall-related risk into
batch_analysis_result.csv (overwriting it in-place).
6. Runs analyse_contributing_feature.py to produce per-package feature
contribution explanations.
Outputs:
- Per-package Analysis/<pkg>/...
- Consolidated TSV (from compile_scores.py) in Analysis/
- Features CSV (from generate_package_features.py) in Analysis/
- batch_analysis_result.csv (from generate_scan_results.py + merge_preinstall_risk.py)
- contributing_features.csv (from analyse_contributing_feature.py) in Analysis/
"""
import os
import sys
import subprocess
def log(msg: str) -> None:
print(msg, flush=True)
def find_single_tsv(analysis_root: str) -> str:
"""
Look for exactly one .tsv file directly under analysis_root.
Return its path, or "" if not found / ambiguous.
"""
candidates = [
os.path.join(analysis_root, f)
for f in os.listdir(analysis_root)
if f.lower().endswith(".tsv")
]
if len(candidates) == 1:
return candidates[0]
if len(candidates) == 0:
log("[!] No .tsv file found in Analysis/; cannot build ML features.")
else:
log(
"[!] Multiple .tsv files found in Analysis/; please clean up or "
"adapt batch_analysis.py to select the right one."
)
for c in candidates:
log(f" - {c}")
return ""
def main():
if len(sys.argv) != 2:
print("Usage: python3 batch_analysis.py <extracted_root>")
sys.exit(1)
extracted_root = os.path.abspath(sys.argv[1])
if not os.path.isdir(extracted_root):
print(f"[!] Error: Not a directory: {extracted_root}")
sys.exit(1)
# Parent of extracted_root, where Analysis/ will live
parent = os.path.dirname(extracted_root)
analysis_root = os.path.join(parent, "Analysis")
os.makedirs(analysis_root, exist_ok=True)
# Location of scripts (inside "Analysis Codes")
repo_root = os.path.dirname(os.path.abspath(__file__))
analysis_codes_dir = os.path.join(repo_root, "Analysis Codes")
extract_script = os.path.join(analysis_codes_dir, "extract_features.py")
if not os.path.isfile(extract_script):
print(f"[!] extract_features.py not found at:\n {extract_script}")
sys.exit(1)
compile_script = os.path.join(analysis_codes_dir, "compile_scores.py")
if not os.path.isfile(compile_script):
log(f"[!] Warning: compile_scores.py not found at:\n {compile_script}")
log(
" Batch per-package analysis will still run, "
"but scores will not be consolidated."
)
# New scripts for ML features & classification
gen_features_script = os.path.join(
analysis_codes_dir, "generate_package_features.py"
)
scan_results_script = os.path.join(
analysis_codes_dir, "generate_scan_results.py"
)
# NEW: merge preinstall risk script
merge_preinstall_script = os.path.join(
analysis_codes_dir, "merge_preinstall_risk.py"
)
# NEW: analyse_contributing_feature script
analyse_contrib_script = os.path.join(
analysis_codes_dir, "analyse_contributing_feature.py"
)
model_dir = os.path.join(analysis_codes_dir, "classification_configuration")
# Packages under extracted_root
packages = [d for d in os.scandir(extracted_root) if d.is_dir()]
packages.sort(key=lambda e: e.name)
total = len(packages)
log(f"[+] Found {total} packages in {extracted_root}")
done = 0
failed = 0
skipped = 0
for idx, pkg in enumerate(packages, start=1):
pkg_name = pkg.name
pkg_dir = pkg.path
# This is where extract_features.py will write:
# <parent>/Analysis/<pkg_name>/
per_pkg_analysis = os.path.join(analysis_root, pkg_name)
# Resume-friendly: skip if per-package Analysis dir already has content
if os.path.isdir(per_pkg_analysis) and os.listdir(per_pkg_analysis):
log(f"[{idx}/{total}] {pkg_name}: already analyzed (skipping).")
skipped += 1
continue
log(f"[{idx}/{total}] {pkg_name}: starting analysis")
log(f" package dir : {pkg_dir}")
log(f" analysis dir (expected): {per_pkg_analysis}")
try:
# IMPORTANT: cwd is the parent directory, NOT per-package Analysis
subprocess.run(
["python3", extract_script, pkg_dir],
cwd=parent,
check=True,
)
done += 1
log(" [+] completed.")
except subprocess.CalledProcessError as e:
failed += 1
log(f" [!] FAILED (exit {e.returncode})")
except Exception as e:
failed += 1
log(f" [!] Unexpected error: {e}")
log("\n=== Batch Completed ===")
log(f"Processed: {done}")
log(f"Skipped: {skipped}")
log(f"Failed: {failed}")
log(f"Output: {analysis_root}")
# ------------------------------------------------------------------
# 2. Run compile_scores.py to aggregate all per-package Scores
# ------------------------------------------------------------------
results_tsv = ""
if os.path.isfile(compile_script):
log("\n[+] Running compile_scores.py to consolidate scores...")
try:
# NOTE: pass both analysis_root and extracted_root so that
# PACKAGE_SIZE_BYTES can be computed from the real packages.
subprocess.run(
["python3", compile_script, analysis_root, extracted_root],
check=True,
)
log("[+] compile_scores.py completed successfully.")
# Try to locate the consolidated TSV
results_tsv = find_single_tsv(analysis_root)
except subprocess.CalledProcessError as e:
log(f"[!] compile_scores.py FAILED (exit {e.returncode}).")
except Exception as e:
log(f"[!] Unexpected error while running compile_scores.py: {e}")
else:
log("\n[!] Skipping score consolidation (compile_scores.py not found).")
# ------------------------------------------------------------------
# 3. Build ML features via generate_package_features.py
# ------------------------------------------------------------------
features_csv = ""
if results_tsv and os.path.isfile(gen_features_script):
features_csv = os.path.join(
analysis_root, "ml_features.csv"
)
log(f"\n[+] Building ML features from TSV:")
log(f" Input TSV : {results_tsv}")
log(f" Output CSV: {features_csv}")
try:
subprocess.run(
[
"python3",
gen_features_script,
results_tsv,
features_csv,
"--fill-missing",
],
check=True,
)
log("[+] generate_package_features.py completed successfully.")
except subprocess.CalledProcessError as e:
log(f"[!] generate_package_features.py FAILED (exit {e.returncode}).")
features_csv = ""
except Exception as e:
log(
f"[!] Unexpected error while running generate_package_features.py: {e}"
)
features_csv = ""
elif results_tsv and not os.path.isfile(gen_features_script):
log(f"\n[!] generate_package_features.py not found at:\n {gen_features_script}")
# If no results_tsv, features step is skipped automatically.
# ------------------------------------------------------------------
# 4. Run generate_scan_results.py to classify packages
# ------------------------------------------------------------------
if features_csv and os.path.isfile(scan_results_script):
if not os.path.isdir(model_dir):
log(f"\n[!] Model directory not found: {model_dir}")
else:
log("\n[+] Running generate_scan_results.py to classify packages...")
log(f" Features CSV: {features_csv}")
log(f" Model dir : {model_dir}")
try:
output_csv = os.path.join(analysis_root, "batch_analysis_result.csv")
subprocess.run(
[
"python3",
scan_results_script,
"--features",
features_csv,
"--model-dir",
model_dir,
"--output",
output_csv,
],
check=True,
)
log(f"[+] Saved final batch result CSV to: {output_csv}.")
log("[+] generate_scan_results.py completed successfully.")
except subprocess.CalledProcessError as e:
log(f"[!] generate_scan_results.py FAILED (exit {e.returncode}).")
except Exception as e:
log(f"[!] Unexpected error while running generate_scan_results.py: {e}")
# ------------------------------------------------------------------
# 5. merge_preinstall_risk.py (overwrite batch_analysis_result.csv)
# ------------------------------------------------------------------
if os.path.isfile(merge_preinstall_script):
result_csv_path = os.path.join(analysis_root, "batch_analysis_result.csv")
if os.path.isfile(result_csv_path):
log("\n[+] Running merge_preinstall_risk.py to merge preinstall risk...")
try:
subprocess.run(
[
"python3",
merge_preinstall_script,
analysis_root,
"-overwrite",
],
check=True,
)
log("[+] merge_preinstall_risk.py completed successfully.")
except subprocess.CalledProcessError as e:
log(f"[!] merge_preinstall_risk.py FAILED (exit {e.returncode}).")
except Exception as e:
log(
f"[!] Unexpected error while running merge_preinstall_risk.py: {e}"
)
else:
log(
"\n[!] Skipping merge_preinstall_risk.py "
"(batch_analysis_result.csv not found)."
)
else:
log(
f"\n[!] merge_preinstall_risk.py not found at:\n"
f" {merge_preinstall_script}"
)
# ------------------------------------------------------------------
# 6. analyse_contributing_feature.py (--analysis-dir <analysis_root>)
# ------------------------------------------------------------------
if os.path.isfile(analyse_contrib_script):
log(
"\n[+] Running analyse_contributing_feature.py "
"to compute contributing features..."
)
try:
subprocess.run(
[
"python3",
analyse_contrib_script,
"--analysis-dir",
analysis_root,
],
check=True,
)
log("[+] analyse_contributing_feature.py completed successfully.")
except subprocess.CalledProcessError as e:
log(
f"[!] analyse_contributing_feature.py FAILED "
f"(exit {e.returncode})."
)
except Exception as e:
log(
"[!] Unexpected error while running "
f"analyse_contributing_feature.py: {e}"
)
else:
log(
f"\n[!] analyse_contributing_feature.py not found at:\n"
f" {analyse_contrib_script}"
)
elif features_csv and not os.path.isfile(scan_results_script):
log(f"\n[!] generate_scan_results.py not found at:\n {scan_results_script}")
else:
if not features_csv:
log("\n[!] Skipping classification step (no features CSV).")
log("\n[+] Full pipeline finished.")
if __name__ == "__main__":
main()