-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.py
More file actions
641 lines (519 loc) · 21.8 KB
/
extractor.py
File metadata and controls
641 lines (519 loc) · 21.8 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import re
import json
from filters import is_known_package, NODE_BUILTINS, CSS_PROPERTIES
# ── Regex patterns for extracting package names ──────────────────────────
# Pattern 1: node_modules paths (webpack bundles — highest value)
RE_NODE_MODULES = re.compile(
r'node_modules/(@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9._-]+)'
)
# Pattern 2: require() calls
RE_REQUIRE = re.compile(
r'''require\s*\(\s*['"](@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9][a-zA-Z0-9._-]*)['"]'''
)
# Pattern 3: ES import from statements (must have import before from)
# Matches: import x from "pkg", import {x} from "pkg", export {x} from "pkg"
# Does NOT match: "inherit properties from "gfeedfetcher" class" (comment text)
RE_IMPORT_FROM = re.compile(
r'''(?:import|export)\s+.*?\s+from\s+['"](@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9][a-zA-Z0-9._-]*)['"]'''
)
# Pattern 4: ES import statements (import "pkg" — side-effect import)
RE_IMPORT_DIRECT = re.compile(
r'''import\s+['"](@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9][a-zA-Z0-9._-]*)['"]'''
)
# Pattern 5: Dynamic imports
RE_DYNAMIC_IMPORT = re.compile(
r'''import\s*\(\s*['"](@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9][a-zA-Z0-9._-]*)['"]'''
)
# Pattern 6: Webpack chunk/module maps
RE_WEBPACK_MODULE = re.compile(
r'''["']/?node_modules/(@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9._-]+)'''
)
# Pattern 7: Dependency entry (ONLY used inside parsed dep blocks, never blind)
RE_DEP_ENTRY = re.compile(
r'''"(@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9][a-zA-Z0-9._-]*)"\s*:\s*"([\^~>=<*|]?\s*\d+\.\d+[\d.]*)'''
)
# Pattern 8: Python requirements.txt format
RE_PYTHON_REQ = re.compile(
r'^([a-zA-Z0-9][a-zA-Z0-9._-]*)\s*(?:[=!<>~]=|[<>])', re.MULTILINE
)
# Pattern 9: Gemfile gem declarations
RE_RUBY_GEM = re.compile(
r'''gem\s+['"]([a-zA-Z0-9][a-zA-Z0-9._-]*)['"]'''
)
# Pattern 10: setup.py install_requires
RE_SETUP_PY = re.compile(
r'''['"]([a-zA-Z0-9][a-zA-Z0-9._-]*)(?:[=!<>~]=.*?)?['"]'''
)
# Pattern 11: pyproject.toml dependencies
RE_PYPROJECT = re.compile(
r'''['"]([a-zA-Z0-9][a-zA-Z0-9._-]*)(?:\s*[=!<>~]=.*?)?['"]'''
)
# ── Embedded dependency block keywords (for JS files) ────────────────────
# When we find these keywords in JS content, we extract the JSON block after
# them and parse it to get package names + versions.
JS_DEP_BLOCK_KEYS = [
# npm / Node.js
"dependencies", "devDependencies", "peerDependencies",
"optionalDependencies", "bundleDependencies", "bundledDependencies",
"resolutions", "overrides",
# PHP / Composer
"require", "require-dev",
]
# ── Manifest dependency block keywords ───────────────────────────────────
DEP_BLOCK_KEYWORDS = [
'"dependencies"', "'dependencies'",
'"devDependencies"', "'devDependencies'",
'"peerDependencies"', "'peerDependencies'",
'"optionalDependencies"', "'optionalDependencies'",
'"bundleDependencies"', "'bundleDependencies'",
'"bundledDependencies"', "'bundledDependencies'",
"install_requires",
"setup_requires",
"tests_require",
"extras_require",
]
def _clean_package_name(raw_name):
"""Clean a raw extracted string into a valid package name.
Returns the cleaned name or None if invalid.
"""
if not raw_name:
return None
name = raw_name.strip()
# Strip quotes
name = name.strip("'\"")
# Strip trailing colons, commas, semicolons
name = name.rstrip(":,;")
# Strip version suffixes (e.g., @^1.2.3, @latest, @1.0.0)
# But preserve scoped names like @scope/pkg
if name.startswith("@") and "/" in name:
# Scoped package: @scope/pkg@version → @scope/pkg
scope_and_name = name.split("/", 1)
if "@" in scope_and_name[1]:
scope_and_name[1] = scope_and_name[1].split("@")[0]
name = "/".join(scope_and_name)
elif "@" in name and not name.startswith("@"):
# Unscoped with version: pkg@1.0.0 → pkg
name = name.split("@")[0]
# Strip trailing path segments: pkg/dist/index.js → pkg
# For scoped: @scope/pkg/dist/index.js → @scope/pkg
if name.startswith("@") and "/" in name:
parts = name.split("/")
if len(parts) > 2:
name = parts[0] + "/" + parts[1]
elif "/" in name:
name = name.split("/")[0]
# Strip any remaining whitespace
name = name.strip()
# Validate: must not be empty
if not name:
return None
# Validate: minimum length
if len(name) < 2 and not name.startswith("@"):
return None
# Validate: max length (npm limit is 214)
if len(name) > 214:
return None
# Validate: must not start with . or _
if name.startswith(".") or name.startswith("_"):
return None
# Validate: no spaces
if " " in name:
return None
# Validate: only allowed characters
# npm: lowercase, numbers, hyphens, dots, underscores, @, /
if not re.match(r'^(@[a-zA-Z0-9._-]+/)?[a-zA-Z0-9._-]+$', name):
return None
# Convert to lowercase (npm packages are always lowercase)
name = name.lower()
# Block obvious non-packages
# Filenames with extensions (popper.553719d0.js)
if name.endswith(".js") or name.endswith(".ts") or name.endswith(".css") or name.endswith(".json") or name.endswith(".html"):
return None
# Literal directory name
if name == "node_modules":
return None
return name
def _extract_json_block(content, start_pos):
"""Extract a JSON object {...} or array [...] starting from start_pos.
Returns the parsed object/list or None.
"""
# Find the opening brace/bracket after the key
i = start_pos
while i < len(content) and content[i] in ' \t\n\r:':
i += 1
if i >= len(content):
return None
opener = content[i]
if opener == '{':
closer = '}'
elif opener == '[':
closer = ']'
else:
return None
# Track nesting to find the matching closer
depth = 0
j = i
while j < len(content):
ch = content[j]
if ch == '"':
# Skip string content
j += 1
while j < len(content) and content[j] != '"':
if content[j] == '\\':
j += 1 # skip escaped char
j += 1
elif ch == opener:
depth += 1
elif ch == closer:
depth -= 1
if depth == 0:
block = content[i:j + 1]
try:
return json.loads(block)
except (json.JSONDecodeError, ValueError):
return None
j += 1
return None
def _extract_dep_blocks(content):
"""Find embedded dependency blocks in JS content and extract packages with versions.
Searches for keywords like "dependencies":{...}, "devDependencies":{...} etc.
Parses the JSON block to get exact package names and versions.
Returns dict of {package_name: version_string}.
"""
packages = {}
for key in JS_DEP_BLOCK_KEYS:
# Search for "key": or 'key': patterns
for quote in ['"', "'"]:
pattern = f'{quote}{key}{quote}'
search_start = 0
while True:
pos = content.find(pattern, search_start)
if pos == -1:
break
# Move past the key and find the colon
after_key = pos + len(pattern)
parsed = _extract_json_block(content, after_key)
if isinstance(parsed, dict):
for pkg_name, version in parsed.items():
# Real dep values are version strings ("^1.0.0", "~2.3", "*", "latest")
# Reject non-strings (JSON Schema objects/arrays/bools)
if not isinstance(version, str):
continue
# Reject values that aren't version-like
# Valid: "^1.0.0", "~2.3", ">=1.0", "*", "latest", "1.x", "npm:pkg@1"
# Invalid: "object", "string", "array", "boolean" (JSON Schema types)
v = version.strip()
if v and not re.match(r'^[\^~>=<*|0-9]|^latest$|^next$|^npm:', v):
continue
cleaned = _clean_package_name(pkg_name)
if cleaned:
packages[cleaned] = version
elif isinstance(parsed, list):
# bundleDependencies is an array of strings
for item in parsed:
if isinstance(item, str):
cleaned = _clean_package_name(item)
if cleaned:
packages[cleaned] = ""
search_start = after_key
return packages
def _is_bundled_js(content):
"""Detect if a JS file is a bundle (webpack, AMD, Browserify, etc.).
In bundles, ALL original require/import/export statements are compiled
away by the bundler. Any remaining require("name") or import "name"
text is inside STRING LITERALS (error messages, docs, etc.) — not real
code. For example, webpack compiles import to __webpack_require__(id).
Real npm packages in bundles show up via:
- node_modules/ paths in webpack module maps (always extracted)
- Embedded "dependencies":{...} blocks (always extracted)
Returns True if the file is a bundle (skip require AND import patterns).
"""
# Large files are always bundles — no raw source file served from a
# website is this big. 200K chars catches most bundles.
if len(content) > 200000:
return True
# Module system / bundler patterns that mean require/import is NOT real code.
# Python's `in` on strings uses fast C-level search — microseconds per check.
skip_patterns = [
# AMD / RequireJS
"define.amd",
"define([",
'define("',
"define('",
'typeof define==="function"',
"typeof define === 'function'",
'typeof define=="function"',
# Webpack (compiles require/import → __webpack_require__(id))
"__webpack_require__",
"__webpack_modules__",
"webpackChunk",
"webpackJsonp",
# SystemJS
"System.register(",
# Browserify
"_dereq_(",
# Rollup / generic bundler markers
"Object.defineProperty(exports",
# Minified bundles (single-letter variable names in function chains)
"!function(e,t){",
"!function(t,e){",
"!function(e,n){",
"(function(e,t){",
]
for pattern in skip_patterns:
if pattern in content:
return True
return False
def extract_from_js(content):
"""Extract package names from JavaScript file content.
Uses two approaches:
1. Code references: node_modules paths, import/export, require() (safe)
2. Embedded dep blocks: "dependencies":{...} parsed as JSON (accurate)
In bundled JS (webpack/AMD/Browserify), require() AND import/export are
skipped — bundlers compile them away, so any remaining text with these
keywords is inside string literals (error messages, docs), not real code.
Only node_modules/ paths and embedded dep blocks are used in bundles.
Returns a dict of {package_name: version_string}.
"""
packages = {}
is_bundle = _is_bundled_js(content)
# ── Method 1: Code references ──
# node_modules/ paths — always safe regardless of module system
for regex in [RE_NODE_MODULES, RE_WEBPACK_MODULE]:
for match in regex.finditer(content):
name = _clean_package_name(match.group(1))
if name:
if name not in packages:
packages[name] = ""
# require() and import/export — SKIP in bundles
# Bundlers compile these away; any remaining text is inside string literals
if not is_bundle:
for match in RE_REQUIRE.finditer(content):
name = _clean_package_name(match.group(1))
if name:
if name not in packages:
packages[name] = ""
for regex in [RE_IMPORT_FROM, RE_IMPORT_DIRECT, RE_DYNAMIC_IMPORT]:
for match in regex.finditer(content):
name = _clean_package_name(match.group(1))
if name:
if name not in packages:
packages[name] = ""
# ── Method 2: Embedded dependency blocks (JSON parsed) ──
dep_block_packages = _extract_dep_blocks(content)
for name, version in dep_block_packages.items():
packages[name] = version # overwrite with version if found
# ── Filter ──
filtered = {}
for name, version in packages.items():
if not is_known_package(name) and name not in CSS_PROPERTIES:
filtered[name] = version
return filtered
def extract_from_package_json(content):
"""Extract package names from package.json content.
Returns a dict of {package_name: version_string}.
"""
packages = {}
# Primary: parse JSON properly
try:
data = json.loads(content)
dep_keys = ["dependencies", "devDependencies", "peerDependencies",
"optionalDependencies", "resolutions", "overrides"]
for key in dep_keys:
deps = data.get(key, {})
if isinstance(deps, dict):
for name, version in deps.items():
cleaned = _clean_package_name(name)
if cleaned:
packages[cleaned] = str(version) if version else ""
# bundleDependencies is an array
for key in ["bundleDependencies", "bundledDependencies"]:
deps = data.get(key, [])
if isinstance(deps, list):
for name in deps:
if isinstance(name, str):
cleaned = _clean_package_name(name)
if cleaned and cleaned not in packages:
packages[cleaned] = ""
except (json.JSONDecodeError, AttributeError):
# Fallback: regex extraction if JSON parsing fails
for match in RE_DEP_ENTRY.finditer(content):
name = _clean_package_name(match.group(1))
version = match.group(2) if match.lastindex >= 2 else ""
if name:
packages[name] = version
# Also extract from node_modules refs if present
for match in RE_NODE_MODULES.finditer(content):
name = _clean_package_name(match.group(1))
if name and name not in packages:
packages[name] = ""
# Filter
filtered = {}
for name, version in packages.items():
if not is_known_package(name) and name not in CSS_PROPERTIES:
filtered[name] = version
return filtered
def extract_from_requirements_txt(content):
"""Extract package names from requirements.txt content.
Returns a dict of {package_name: version_string}.
"""
packages = {}
for line in content.splitlines():
line = line.strip()
if not line or line.startswith("#") or line.startswith("-"):
continue
match = RE_PYTHON_REQ.match(line)
if match:
name = match.group(1).strip().lower()
# Extract version from the line
ver_match = re.search(r'[=!<>~]=\s*([\d][\d.]*)', line)
version = ver_match.group(1) if ver_match else ""
if name and not is_known_package(name):
packages[name] = version
else:
if re.match(r'^[a-zA-Z0-9][a-zA-Z0-9._-]*$', line):
name = line.strip().lower()
if name and not is_known_package(name):
packages[name] = ""
return packages
def extract_from_gemfile(content):
"""Extract gem names from Gemfile content.
Returns a dict of {gem_name: version_string}.
"""
packages = {}
# Match gem "name", "~> 1.0"
re_gem_ver = re.compile(
r'''gem\s+['"]([a-zA-Z0-9][a-zA-Z0-9._-]*)['"](?:\s*,\s*['"]([~>=<!\s\d.]+)['"])?'''
)
for match in re_gem_ver.finditer(content):
name = match.group(1).strip().lower()
version = match.group(2).strip() if match.group(2) else ""
if name and not is_known_package(name):
packages[name] = version
return packages
def extract_from_setup_py(content):
"""Extract package names from setup.py content.
Returns a dict of {package_name: version_string}.
"""
packages = {}
for key in ["install_requires", "setup_requires", "tests_require"]:
req_match = re.search(
rf'{key}\s*=\s*\[(.*?)\]', content, re.DOTALL
)
if req_match:
block = req_match.group(1)
for match in RE_SETUP_PY.finditer(block):
name = match.group(1).strip().lower()
# Try to get version from full match
full = match.group(0)
ver_match = re.search(r'[=!<>~]=\s*([\d][\d.]*)', full)
version = ver_match.group(1) if ver_match else ""
if name and not is_known_package(name):
packages[name] = version
return packages
def extract_from_pipfile(content):
"""Extract package names from Pipfile content.
Returns a dict of {package_name: version_string}.
"""
packages = {}
in_packages_section = False
for line in content.splitlines():
line = line.strip()
if line == "[packages]" or line == "[dev-packages]":
in_packages_section = True
continue
elif line.startswith("["):
in_packages_section = False
continue
if in_packages_section and "=" in line:
parts = line.split("=", 1)
name = parts[0].strip().strip('"').strip("'").lower()
version = parts[1].strip().strip('"').strip("'") if len(parts) > 1 else ""
if version == "*":
version = ""
if name and re.match(r'^[a-zA-Z0-9._-]+$', name):
if not is_known_package(name):
packages[name] = version
return packages
def extract_from_pyproject_toml(content):
"""Extract package names from pyproject.toml content.
Returns a dict of {package_name: version_string}.
"""
packages = {}
in_deps = False
for line in content.splitlines():
stripped = line.strip()
if "dependencies" in stripped and "=" in stripped:
match = re.search(r'\[(.+)\]', stripped)
if match:
items = match.group(1)
for dep_match in re.finditer(r'''['"]([a-zA-Z0-9][a-zA-Z0-9._-]*)(?:[>=<~!]+\s*([\d][\d.]*))?''', items):
name = dep_match.group(1).strip().lower()
version = dep_match.group(2) if dep_match.group(2) else ""
if name and not is_known_package(name):
packages[name] = version
elif stripped == "[project.dependencies]" or stripped == "[project.optional-dependencies]":
in_deps = True
continue
elif stripped.startswith("["):
in_deps = False
continue
elif in_deps and stripped and not stripped.startswith("#"):
dep_match = re.match(r'''['"]?([a-zA-Z0-9][a-zA-Z0-9._-]*)(?:[>=<~!]+\s*([\d][\d.]*))?''', stripped)
if dep_match:
name = dep_match.group(1).strip().lower()
version = dep_match.group(2) if dep_match.group(2) else ""
if name and not is_known_package(name):
packages[name] = version
return packages
def extract_from_yarn_lock(content):
"""Extract package names from yarn.lock content.
Returns a dict of {package_name: version_string}.
"""
packages = {}
for match in re.finditer(
r'^"?(@[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+|[a-zA-Z0-9._-]+)@([^\s:]+)',
content,
re.MULTILINE
):
name = _clean_package_name(match.group(1))
version = match.group(2).strip('"').strip("'") if match.group(2) else ""
if name and not is_known_package(name):
packages[name] = version
return packages
def extract_from_package_lock_json(content):
"""Extract package names from package-lock.json content.
Returns a dict of {package_name: version_string}.
"""
return extract_from_package_json(content)
def detect_manifest_type(filename, content=""):
"""Detect the type of manifest file and return the appropriate extractor.
Returns (ecosystem, extractor_function) or (None, None) if unknown.
"""
filename_lower = filename.lower()
if filename_lower.endswith("package.json"):
return "npm", extract_from_package_json
elif filename_lower.endswith("package-lock.json"):
return "npm", extract_from_package_lock_json
elif filename_lower.endswith("yarn.lock"):
return "npm", extract_from_yarn_lock
elif filename_lower.endswith("requirements.txt") or filename_lower.endswith("-requirements.txt"):
return "pypi", extract_from_requirements_txt
elif filename_lower.endswith("setup.py"):
return "pypi", extract_from_setup_py
elif filename_lower.endswith("pipfile"):
return "pypi", extract_from_pipfile
elif filename_lower.endswith("pipfile.lock"):
return "pypi", extract_from_pipfile
elif filename_lower.endswith("pyproject.toml"):
return "pypi", extract_from_pyproject_toml
elif filename_lower.endswith("gemfile") or filename_lower == ".gemfile":
return "rubygems", extract_from_gemfile
elif filename_lower.endswith("gemfile.lock"):
return "rubygems", extract_from_gemfile
elif filename_lower.endswith(".gemspec"):
return "rubygems", extract_from_gemfile
return None, None
def detect_ecosystem_from_js(packages):
"""All packages from JS files are assumed npm/yarn ecosystem."""
return "npm"