timecs 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. app.py +3014 -0
  2. ast_scanner.py +36187 -0
  3. banner.py +146 -0
  4. ci_reporter.py +570 -0
  5. cli.py +2347 -0
  6. config_loader.py +307 -0
  7. cross_file_engine.py +1781 -0
  8. delivery_models.py +145 -0
  9. delivery_repository.py +78 -0
  10. diff_scan.py +127 -0
  11. durable_delivery_sink.py +103 -0
  12. event_bus.py +155 -0
  13. event_publisher.py +345 -0
  14. event_taxonomy.py +313 -0
  15. function_summarizer.py +354 -0
  16. html_auditor.py +274 -0
  17. iac_auditor.py +400 -0
  18. in_app_adapter.py +115 -0
  19. js_scanner.py +895 -0
  20. manifest_parser.py +960 -0
  21. master_rules_bank.py +389 -0
  22. notification_models.py +346 -0
  23. notification_policy.py +309 -0
  24. notification_repository.py +118 -0
  25. notification_routes.py +394 -0
  26. notification_service.py +199 -0
  27. osv_client.py +437 -0
  28. remediation/__init__.py +39 -0
  29. remediation/base.py +102 -0
  30. remediation/contracts.py +196 -0
  31. remediation/cwe1188_binding.py +78 -0
  32. remediation/cwe1336_jinja.py +91 -0
  33. remediation/cwe22_path_traversal.py +348 -0
  34. remediation/cwe295_ssl.py +87 -0
  35. remediation/cwe327_weak_hash.py +78 -0
  36. remediation/cwe377_tempfile.py +77 -0
  37. remediation/cwe489_debug.py +80 -0
  38. remediation/cwe502_yaml.py +89 -0
  39. remediation/cwe614_cookie.py +91 -0
  40. remediation/cwe78_cmdi.py +415 -0
  41. remediation/cwe798_credentials.py +153 -0
  42. remediation/cwe89_sqli.py +425 -0
  43. remediation/cwe916_password_hash.py +131 -0
  44. remediation/cwe95_eval.py +77 -0
  45. remediation/dispatcher.py +114 -0
  46. remediation/orchestrator.py +541 -0
  47. remediation/patch_engine.py +213 -0
  48. remediation/verification.py +253 -0
  49. rule_engine.py +1535 -0
  50. sarif_adapter.py +719 -0
  51. sarif_exporter.py +88 -0
  52. sca_reachability/__init__.py +28 -0
  53. sca_reachability/ast_analyzer.py +366 -0
  54. sca_reachability/call_graph.py +203 -0
  55. sca_reachability/contracts.py +365 -0
  56. sca_reachability/dist_import_map.py +46 -0
  57. sca_reachability/engine.py +603 -0
  58. sca_reachability/resolver.py +169 -0
  59. scanners/__init__.py +70 -0
  60. scanners/container_scanner.py +323 -0
  61. scanners/cve_index.py +521 -0
  62. scanners/iac_scanner.py +328 -0
  63. scanners/sbom.py +183 -0
  64. scanners/sca_scanner.py +667 -0
  65. scanners/secret_scanner.py +345 -0
  66. scanners/yaml_lite.py +217 -0
  67. secret_filters.py +435 -0
  68. secret_scanner.py +943 -0
  69. sqlite_delivery_repository.py +277 -0
  70. sqlite_notification_repository.py +441 -0
  71. ssrf_guard.py +276 -0
  72. staged_scanner.py +492 -0
  73. suppression_resolver.py +282 -0
  74. symbol_indexer.py +424 -0
  75. tcs/__init__.py +1 -0
  76. tcs/analysis/__init__.py +1 -0
  77. tcs/analysis/def_use.py +408 -0
  78. tcs/analysis/function_contracts.py +290 -0
  79. tcs/parallel_analyzer.py +276 -0
  80. tcs/parallel_scanner.py +630 -0
  81. tcs/treesitter_packer.py +1446 -0
  82. tcs_cli.py +2086 -0
  83. tcs_config.py +218 -0
  84. tcs_gui.py +2123 -0
  85. timecs-2.0.0.dist-info/METADATA +142 -0
  86. timecs-2.0.0.dist-info/RECORD +94 -0
  87. timecs-2.0.0.dist-info/WHEEL +5 -0
  88. timecs-2.0.0.dist-info/entry_points.txt +4 -0
  89. timecs-2.0.0.dist-info/licenses/LICENSE +201 -0
  90. timecs-2.0.0.dist-info/top_level.txt +49 -0
  91. version_matcher.py +934 -0
  92. webhook_adapter.py +495 -0
  93. webhook_client.py +483 -0
  94. webhook_formatters.py +245 -0
manifest_parser.py ADDED
@@ -0,0 +1,960 @@
1
+ """
2
+ TimeCodeSecurity Manifest Parser.
3
+ Extracts normalized Python dependency records from:
4
+ - requirements.txt (PEP 508 / pip format)
5
+ - Pipfile.lock (Pipenv JSON lockfile)
6
+ - poetry.lock (Poetry TOML lockfile)
7
+
8
+ Deterministic, offline-only parsing with NO external network access.
9
+ """
10
+
11
+ from __future__ import annotations
12
+ import os
13
+ import re
14
+ import json
15
+ from dataclasses import dataclass, field
16
+ from typing import Dict, List, Optional, Tuple, Any, Set
17
+
18
+ # Standard library TOML support (Python 3.11+) with fallback
19
+ try:
20
+ import tomllib
21
+ except ImportError:
22
+ try:
23
+ import tomli as tomllib
24
+ except ImportError:
25
+ tomllib = None
26
+
27
+ # Optional packaging library for standard PEP 508 parsing if installed
28
+ try:
29
+ from packaging.requirements import Requirement as PackagingRequirement
30
+ from packaging.utils import canonicalize_name
31
+ HAS_PACKAGING = True
32
+ except ImportError:
33
+ HAS_PACKAGING = False
34
+
35
+
36
+ def normalize_package_name(name: str) -> str:
37
+ """
38
+ Normalizes a package name according to PEP 503 / PEP 685:
39
+ Lowercased with runs of [-_.] replaced by a single '-'.
40
+ """
41
+ if not name:
42
+ return ""
43
+ if HAS_PACKAGING:
44
+ return canonicalize_name(name)
45
+ return re.sub(r"[-_.]+", "-", name.strip()).lower()
46
+
47
+
48
+ @dataclass(frozen=True)
49
+ class DependencyRecord:
50
+ """
51
+ Typed, immutable representation of a single normalized dependency requirement
52
+ or locked package record.
53
+ """
54
+ name: str # Normalized package name (e.g. "requests", "django")
55
+ version: Optional[str] = None # Exact pinned/resolved version (e.g. "2.25.0") or None if range/unpinned
56
+ version_specifier: Optional[str] = None # Full original specifier (e.g. "==2.25.0", ">=1.26.0,<2.0", "~=1.2")
57
+ source: str = "" # Manifest source identifier ("requirements.txt", "Pipfile.lock", "poetry.lock")
58
+ pinned: bool = False # True if exact single version is pinned (==x.y.z)
59
+ environment_marker: Optional[str] = None # Preserved environment marker (e.g. 'python_version < "3.8"')
60
+ extras: Tuple[str, ...] = () # Normalized tuple of extras (e.g. ("security",))
61
+ editable: bool = False # True if editable requirement (-e)
62
+ group: Optional[str] = None # Dependency group/category (e.g. "default", "develop", "main", "dev")
63
+ raw: str = "" # Raw original declaration string
64
+ line_number: Optional[int] = None # Line number in source manifest (1-based if applicable)
65
+
66
+
67
+ @dataclass
68
+ class ParseIssue:
69
+ """Represents a malformed line, syntax error, or unparseable item in a manifest."""
70
+ message: str
71
+ line_number: Optional[int] = None
72
+ raw: str = ""
73
+ issue_type: str = "malformed" # "malformed", "missing_field", "syntax_error"
74
+
75
+
76
+ @dataclass
77
+ class ManifestParseResult:
78
+ """Container for the results of parsing any manifest file."""
79
+ source_type: str # "requirements.txt", "Pipfile.lock", "poetry.lock"
80
+ source_path: Optional[str] = None # Path to manifest file if provided
81
+ dependencies: List[DependencyRecord] = field(default_factory=list)
82
+ issues: List[ParseIssue] = field(default_factory=list)
83
+ ignored_directives: List[str] = field(default_factory=list)
84
+
85
+ @property
86
+ def has_issues(self) -> bool:
87
+ return len(self.issues) > 0
88
+
89
+ def get_unique_dependencies(self) -> Dict[str, List[DependencyRecord]]:
90
+ """Group dependencies by normalized package name."""
91
+ by_name: Dict[str, List[DependencyRecord]] = {}
92
+ for dep in self.dependencies:
93
+ by_name.setdefault(dep.name, []).append(dep)
94
+ return by_name
95
+
96
+
97
+ # Commands and flags in requirements.txt that are not package dependencies
98
+ IGNORED_REQUIREMENTS_DIRECTIVES = (
99
+ "-r", "--requirement",
100
+ "-c", "--constraint",
101
+ "-f", "--find-links",
102
+ "-i", "--index-url",
103
+ "--extra-index-url",
104
+ "--no-index",
105
+ "--trusted-host",
106
+ "--prefer-binary",
107
+ "--no-binary",
108
+ "--only-binary"
109
+ )
110
+
111
+
112
+ def _strip_inline_comment(line: str) -> str:
113
+ """
114
+ Safely strips trailing inline comments while preserving #egg= inside URLs.
115
+ """
116
+ if "#egg=" in line:
117
+ return line.strip()
118
+ if "#" in line:
119
+ return line.split("#", 1)[0].strip()
120
+ return line.strip()
121
+
122
+
123
+ def _parse_pep508_requirement(
124
+ clean_line: str,
125
+ filename: str,
126
+ raw_line: str,
127
+ group: Optional[str] = None,
128
+ line_number: Optional[int] = None,
129
+ ) -> Tuple[Optional[DependencyRecord], Optional[ParseIssue]]:
130
+ """
131
+ Parses a single PEP 508 requirement string into a DependencyRecord.
132
+ Returns (record, None) on success, or (None, issue) on malformed syntax.
133
+ """
134
+ if HAS_PACKAGING:
135
+ try:
136
+ req = PackagingRequirement(clean_line)
137
+ norm_name = normalize_package_name(req.name)
138
+ extras_tuple = tuple(sorted(canonicalize_name(e) for e in req.extras)) if req.extras else ()
139
+ marker_str = str(req.marker) if req.marker else None
140
+ spec_str = str(req.specifier) if req.specifier else None
141
+ pinned_flag = False
142
+ version_val = None
143
+
144
+ if req.specifier:
145
+ specs = list(req.specifier)
146
+ if (
147
+ len(specs) == 1
148
+ and specs[0].operator in ("==", "===")
149
+ and not specs[0].version.endswith(".*")
150
+ and "*" not in specs[0].version
151
+ ):
152
+ pinned_flag = True
153
+ version_val = specs[0].version
154
+
155
+ record = DependencyRecord(
156
+ name=norm_name,
157
+ version=version_val,
158
+ version_specifier=spec_str,
159
+ source=filename,
160
+ pinned=pinned_flag,
161
+ environment_marker=marker_str,
162
+ extras=extras_tuple,
163
+ editable=False,
164
+ group=group,
165
+ raw=raw_line,
166
+ line_number=line_number
167
+ )
168
+ return record, None
169
+ except Exception as pe:
170
+ return None, ParseIssue(
171
+ message=f"Malformed requirement: {pe}",
172
+ line_number=line_number,
173
+ raw=raw_line,
174
+ issue_type="malformed"
175
+ )
176
+ else:
177
+ req_match = re.match(
178
+ r"^([A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?)"
179
+ r"(?:\[([^\]]+)\])?"
180
+ r"(?:\s*([~!=><~=]+[^;]+))?"
181
+ r"(?:\s*;\s*(.+))?$",
182
+ clean_line
183
+ )
184
+ if not req_match:
185
+ return None, ParseIssue(
186
+ message="Malformed requirement line syntax",
187
+ line_number=line_number,
188
+ raw=raw_line,
189
+ issue_type="malformed"
190
+ )
191
+
192
+ raw_name, raw_extras, raw_spec, raw_marker = req_match.groups()
193
+ norm_name = normalize_package_name(raw_name)
194
+ extras_tuple = tuple(sorted(normalize_package_name(e.strip()) for e in raw_extras.split(","))) if raw_extras else ()
195
+ spec_str = raw_spec.strip() if raw_spec else None
196
+ marker_str = raw_marker.strip() if raw_marker else None
197
+ pinned_flag = False
198
+ version_val = None
199
+
200
+ if spec_str:
201
+ if spec_str.startswith("==") and not spec_str.endswith(".*") and "," not in spec_str and "*" not in spec_str:
202
+ pinned_flag = True
203
+ version_val = spec_str[2:].strip()
204
+
205
+ record = DependencyRecord(
206
+ name=norm_name,
207
+ version=version_val,
208
+ version_specifier=spec_str,
209
+ source=filename,
210
+ pinned=pinned_flag,
211
+ environment_marker=marker_str,
212
+ extras=extras_tuple,
213
+ editable=False,
214
+ group=group,
215
+ raw=raw_line,
216
+ line_number=line_number
217
+ )
218
+ return record, None
219
+
220
+
221
+ def parse_requirements_txt(content_or_path: str, filename: str = "requirements.txt") -> ManifestParseResult:
222
+ """
223
+ Deterministically parses a requirements.txt file or string content.
224
+ Extracts pinned versions, version ranges, markers, extras, and editable flags.
225
+ Captures non-dependency directives without raising errors.
226
+ """
227
+ source_path = None
228
+ if "\n" not in content_or_path and os.path.isfile(content_or_path):
229
+ source_path = content_or_path
230
+ filename = os.path.basename(content_or_path)
231
+ with open(content_or_path, "r", encoding="utf-8", errors="replace") as f:
232
+ content = f.read()
233
+ else:
234
+ content = content_or_path
235
+
236
+ result = ManifestParseResult(source_type="requirements.txt", source_path=source_path)
237
+
238
+ lines = content.splitlines()
239
+ for idx, raw_line in enumerate(lines, 1):
240
+ stripped = raw_line.strip()
241
+ if not stripped or stripped.startswith("#"):
242
+ continue
243
+
244
+ clean_line = _strip_inline_comment(stripped)
245
+ if not clean_line:
246
+ continue
247
+
248
+ # Check for non-dependency directives (-r, -c, --index-url, etc.)
249
+ first_token = clean_line.split()[0]
250
+ if first_token in IGNORED_REQUIREMENTS_DIRECTIVES or any(clean_line.startswith(f"{d} ") or clean_line.startswith(f"{d}=") for d in IGNORED_REQUIREMENTS_DIRECTIVES):
251
+ result.ignored_directives.append(stripped)
252
+ continue
253
+
254
+ # Check for editable requirements (-e, --editable)
255
+ if first_token in ("-e", "--editable") or clean_line.startswith("-e ") or clean_line.startswith("--editable "):
256
+ editable_target = clean_line.split(None, 1)[1].strip() if len(clean_line.split(None, 1)) > 1 else ""
257
+ pkg_name = ""
258
+ if "#egg=" in editable_target:
259
+ raw_name = editable_target.split("#egg=")[1].split("&")[0].strip()
260
+ pkg_name = normalize_package_name(raw_name)
261
+ else:
262
+ pkg_name = editable_target
263
+
264
+ record = DependencyRecord(
265
+ name=pkg_name,
266
+ version=None,
267
+ version_specifier=None,
268
+ source=filename,
269
+ pinned=False,
270
+ environment_marker=None,
271
+ extras=(),
272
+ editable=True,
273
+ group=None,
274
+ raw=stripped,
275
+ line_number=idx
276
+ )
277
+ result.dependencies.append(record)
278
+ continue
279
+
280
+ # General unsupported command line option starting with -- or -
281
+ if clean_line.startswith("-"):
282
+ result.ignored_directives.append(stripped)
283
+ continue
284
+
285
+ # Standard requirement specification
286
+ record, issue = _parse_pep508_requirement(
287
+ clean_line=clean_line,
288
+ filename=filename,
289
+ raw_line=stripped,
290
+ group=None,
291
+ line_number=idx
292
+ )
293
+ if record:
294
+ result.dependencies.append(record)
295
+ if issue:
296
+ result.issues.append(issue)
297
+
298
+ return result
299
+
300
+
301
+ def parse_pipfile_lock(content_or_path: str, filename: str = "Pipfile.lock") -> ManifestParseResult:
302
+ """
303
+ Parses a Pipfile.lock JSON document into normalized DependencyRecords.
304
+ Tolerates optional fields and extracts both 'default' and 'develop' packages.
305
+ """
306
+ source_path = None
307
+ if "\n" not in content_or_path and os.path.isfile(content_or_path):
308
+ source_path = content_or_path
309
+ filename = os.path.basename(content_or_path)
310
+ with open(content_or_path, "r", encoding="utf-8", errors="replace") as f:
311
+ content = f.read()
312
+ else:
313
+ content = content_or_path
314
+
315
+ result = ManifestParseResult(source_type="Pipfile.lock", source_path=source_path)
316
+
317
+ try:
318
+ data = json.loads(content)
319
+ except Exception as je:
320
+ result.issues.append(ParseIssue(
321
+ message=f"Malformed JSON in Pipfile.lock: {je}",
322
+ issue_type="malformed",
323
+ raw=content[:200]
324
+ ))
325
+ return result
326
+
327
+ if not isinstance(data, dict):
328
+ result.issues.append(ParseIssue(
329
+ message="Pipfile.lock root must be a JSON object",
330
+ issue_type="malformed"
331
+ ))
332
+ return result
333
+
334
+ for section_name, packages in data.items():
335
+ if section_name == "_meta":
336
+ continue
337
+ if not isinstance(packages, dict):
338
+ continue
339
+
340
+ for pkg_name, pkg_info in packages.items():
341
+ if not isinstance(pkg_info, dict):
342
+ result.issues.append(ParseIssue(
343
+ message=f"Package entry '{pkg_name}' must be an object",
344
+ issue_type="malformed"
345
+ ))
346
+ continue
347
+
348
+ raw_ver = pkg_info.get("version")
349
+ markers = pkg_info.get("markers")
350
+ extras = pkg_info.get("extras", [])
351
+ editable = bool(pkg_info.get("editable", False))
352
+ extras_tuple = tuple(sorted(normalize_package_name(e) for e in extras)) if isinstance(extras, list) else ()
353
+
354
+ pinned_flag = False
355
+ version_val = None
356
+ spec_str = None
357
+
358
+ if raw_ver:
359
+ raw_ver_str = str(raw_ver).strip()
360
+ spec_str = raw_ver_str
361
+ if (
362
+ raw_ver_str.startswith("==")
363
+ and not raw_ver_str.endswith(".*")
364
+ and "*" not in raw_ver_str
365
+ and "," not in raw_ver_str
366
+ ):
367
+ pinned_flag = True
368
+ version_val = raw_ver_str[2:].strip()
369
+ elif (
370
+ raw_ver_str
371
+ and not any(op in raw_ver_str for op in ["<", ">", "=", "!", "~"])
372
+ ):
373
+ pinned_flag = True
374
+ version_val = raw_ver_str
375
+ spec_str = f"=={raw_ver_str}"
376
+
377
+ norm_name = normalize_package_name(pkg_name)
378
+ record = DependencyRecord(
379
+ name=norm_name,
380
+ version=version_val,
381
+ version_specifier=spec_str,
382
+ source=filename,
383
+ pinned=pinned_flag,
384
+ environment_marker=str(markers) if markers else None,
385
+ extras=extras_tuple,
386
+ editable=editable,
387
+ group=section_name,
388
+ raw=f'"{pkg_name}": {json.dumps(pkg_info)}'
389
+ )
390
+ result.dependencies.append(record)
391
+
392
+ return result
393
+
394
+
395
+ def _parse_poetry_lock_simple_toml(content: str) -> List[Dict[str, Any]]:
396
+ """
397
+ Minimal state-machine fallback TOML parser for poetry.lock [[package]] tables
398
+ if tomllib/tomli is not available in the environment.
399
+ """
400
+ packages = []
401
+ current_pkg = None
402
+ in_subtable = False
403
+
404
+ for line in content.splitlines():
405
+ line_clean = line.strip()
406
+ if not line_clean or line_clean.startswith("#"):
407
+ continue
408
+
409
+ if line_clean == "[[package]]":
410
+ if current_pkg and "name" in current_pkg:
411
+ packages.append(current_pkg)
412
+ current_pkg = {}
413
+ in_subtable = False
414
+ continue
415
+
416
+ if line_clean.startswith("[") and not line_clean.startswith("[["):
417
+ in_subtable = True
418
+ continue
419
+
420
+ if current_pkg is not None and not in_subtable and "=" in line_clean:
421
+ k, v = line_clean.split("=", 1)
422
+ k = k.strip()
423
+ v = v.strip().strip('"').strip("'")
424
+ current_pkg[k] = v
425
+
426
+ if current_pkg and "name" in current_pkg:
427
+ packages.append(current_pkg)
428
+
429
+ return packages
430
+
431
+
432
+ def parse_poetry_lock(content_or_path: str, filename: str = "poetry.lock") -> ManifestParseResult:
433
+ """
434
+ Parses a poetry.lock TOML document into normalized DependencyRecords.
435
+ Extracts locked packages, exact versions, and category/groups.
436
+ """
437
+ source_path = None
438
+ if "\n" not in content_or_path and os.path.isfile(content_or_path):
439
+ source_path = content_or_path
440
+ filename = os.path.basename(content_or_path)
441
+ with open(content_or_path, "r", encoding="utf-8", errors="replace") as f:
442
+ content = f.read()
443
+ else:
444
+ content = content_or_path
445
+
446
+ result = ManifestParseResult(source_type="poetry.lock", source_path=source_path)
447
+
448
+ packages = []
449
+ if tomllib is not None:
450
+ try:
451
+ data = tomllib.loads(content)
452
+ raw_packages = data.get("package", [])
453
+ if not isinstance(raw_packages, list):
454
+ result.issues.append(ParseIssue(
455
+ message="poetry.lock 'package' field must be an array of tables",
456
+ issue_type="malformed"
457
+ ))
458
+ return result
459
+ packages = raw_packages
460
+ except Exception as te:
461
+ result.issues.append(ParseIssue(
462
+ message=f"Malformed TOML in poetry.lock: {te}",
463
+ issue_type="malformed",
464
+ raw=content[:200]
465
+ ))
466
+ return result
467
+ else:
468
+ packages = _parse_poetry_lock_simple_toml(content)
469
+
470
+ for pkg in packages:
471
+ if not isinstance(pkg, dict):
472
+ result.issues.append(ParseIssue(
473
+ message="Package record in poetry.lock must be a table",
474
+ issue_type="malformed"
475
+ ))
476
+ continue
477
+
478
+ name = pkg.get("name")
479
+ if not name:
480
+ result.issues.append(ParseIssue(
481
+ message="Package record in poetry.lock missing required 'name' field",
482
+ issue_type="missing_field"
483
+ ))
484
+ continue
485
+
486
+ version = pkg.get("version")
487
+ if not version:
488
+ result.issues.append(ParseIssue(
489
+ message=f"Package record '{name}' in poetry.lock missing required 'version' field",
490
+ issue_type="missing_field"
491
+ ))
492
+ continue
493
+
494
+ version_str = str(version).strip()
495
+ category = pkg.get("category")
496
+ groups = pkg.get("groups")
497
+ group_str = category if category else (", ".join(groups) if isinstance(groups, list) else None)
498
+ python_versions = pkg.get("python-versions")
499
+ extras_dict = pkg.get("extras", {})
500
+ extras_tuple = tuple(sorted(normalize_package_name(e) for e in extras_dict.keys())) if isinstance(extras_dict, dict) else ()
501
+
502
+ norm_name = normalize_package_name(name)
503
+ record = DependencyRecord(
504
+ name=norm_name,
505
+ version=version_str,
506
+ version_specifier=f"=={version_str}",
507
+ source=filename,
508
+ pinned=True,
509
+ environment_marker=str(python_versions) if python_versions else None,
510
+ extras=extras_tuple,
511
+ editable=False,
512
+ group=group_str,
513
+ raw=f'name = "{name}", version = "{version_str}"'
514
+ )
515
+ result.dependencies.append(record)
516
+
517
+ return result
518
+
519
+
520
+ def _parse_poetry_dependency_dict(
521
+ dep_dict: Dict[str, Any],
522
+ filename: str,
523
+ group: Optional[str],
524
+ result: ManifestParseResult
525
+ ) -> None:
526
+ """Helper to parse a Poetry dependencies dictionary into DependencyRecords."""
527
+ for pkg_name, constraint in dep_dict.items():
528
+ if not isinstance(pkg_name, str) or not pkg_name.strip():
529
+ result.issues.append(ParseIssue(
530
+ message="Package name in poetry dependencies must be a non-empty string",
531
+ issue_type="malformed",
532
+ raw=str(pkg_name)
533
+ ))
534
+ continue
535
+
536
+ raw_pkg = pkg_name.strip()
537
+ norm_name = normalize_package_name(raw_pkg)
538
+
539
+ # In Poetry, python is a runtime version constraint, not an installable package
540
+ if norm_name == "python":
541
+ result.ignored_directives.append(f"python = {constraint}")
542
+ continue
543
+
544
+ if isinstance(constraint, str):
545
+ raw_ver = constraint.strip()
546
+ pinned = False
547
+ version_val = None
548
+ spec_str = raw_ver
549
+
550
+ if raw_ver.startswith("==") and not raw_ver.endswith(".*") and "*" not in raw_ver and "," not in raw_ver:
551
+ pinned = True
552
+ version_val = raw_ver[2:].strip()
553
+ elif raw_ver and not any(op in raw_ver for op in ["<", ">", "=", "!", "~", "^", "*"]):
554
+ # In Poetry, bare version "2.25.0" means exact pin
555
+ pinned = True
556
+ version_val = raw_ver
557
+ spec_str = f"=={raw_ver}"
558
+
559
+ record = DependencyRecord(
560
+ name=norm_name,
561
+ version=version_val,
562
+ version_specifier=spec_str,
563
+ source=filename,
564
+ pinned=pinned,
565
+ group=group,
566
+ raw=f'{raw_pkg} = "{raw_ver}"'
567
+ )
568
+ result.dependencies.append(record)
569
+
570
+ elif isinstance(constraint, dict):
571
+ raw_ver = constraint.get("version")
572
+ extras = constraint.get("extras", [])
573
+ markers = constraint.get("markers") or constraint.get("python")
574
+ extras_tuple = tuple(sorted(normalize_package_name(e) for e in extras)) if isinstance(extras, list) else ()
575
+
576
+ pinned = False
577
+ version_val = None
578
+ spec_str = None
579
+
580
+ if raw_ver is not None:
581
+ raw_ver_str = str(raw_ver).strip()
582
+ spec_str = raw_ver_str
583
+ if raw_ver_str.startswith("==") and not raw_ver_str.endswith(".*") and "*" not in raw_ver_str and "," not in raw_ver_str:
584
+ pinned = True
585
+ version_val = raw_ver_str[2:].strip()
586
+ elif raw_ver_str and not any(op in raw_ver_str for op in ["<", ">", "=", "!", "~", "^", "*"]):
587
+ pinned = True
588
+ version_val = raw_ver_str
589
+ spec_str = f"=={raw_ver_str}"
590
+
591
+ record = DependencyRecord(
592
+ name=norm_name,
593
+ version=version_val,
594
+ version_specifier=spec_str,
595
+ source=filename,
596
+ pinned=pinned,
597
+ environment_marker=str(markers) if markers else None,
598
+ extras=extras_tuple,
599
+ group=group,
600
+ raw=f'{raw_pkg} = {json.dumps(constraint)}'
601
+ )
602
+ result.dependencies.append(record)
603
+
604
+ else:
605
+ result.issues.append(ParseIssue(
606
+ message=f"Invalid dependency specification for '{raw_pkg}': {constraint}",
607
+ issue_type="malformed",
608
+ raw=f"{raw_pkg} = {constraint}"
609
+ ))
610
+
611
+
612
+ def _parse_pep735_dependency_groups(
613
+ dep_groups: Any,
614
+ filename: str,
615
+ result: ManifestParseResult
616
+ ) -> None:
617
+ """
618
+ Parses and resolves PEP 735 [dependency-groups] with support for:
619
+ - Group name normalization (PEP 503 / canonicalize_name)
620
+ - Collision detection on normalized group names
621
+ - Direct dependency specifiers (strings)
622
+ - Dependency group includes: { "include-group": "<group>" }
623
+ - Recursive includes
624
+ - Cycle detection (2-level and multi-level)
625
+ - Validation against unexpected keys or malformed tables
626
+ """
627
+ if not isinstance(dep_groups, dict):
628
+ result.issues.append(ParseIssue(
629
+ message="dependency-groups must be a table",
630
+ issue_type="malformed",
631
+ raw=str(dep_groups)[:100]
632
+ ))
633
+ return
634
+
635
+ # Track normalized names to detect collisions
636
+ normalized_names: Dict[str, str] = {}
637
+ group_items_map: Dict[str, List[Any]] = {}
638
+
639
+ for raw_name, items in dep_groups.items():
640
+ if not isinstance(raw_name, str) or not raw_name.strip():
641
+ result.issues.append(ParseIssue(
642
+ message=f"dependency-groups key must be a non-empty string: {raw_name}",
643
+ issue_type="malformed",
644
+ raw=str(raw_name)
645
+ ))
646
+ continue
647
+
648
+ norm_name = normalize_package_name(raw_name)
649
+ if norm_name in normalized_names:
650
+ first_raw = normalized_names[norm_name]
651
+ result.issues.append(ParseIssue(
652
+ message=f"Duplicate or colliding dependency-group name after normalization: '{raw_name}' collides with '{first_raw}'",
653
+ issue_type="malformed",
654
+ raw=raw_name
655
+ ))
656
+ continue
657
+ normalized_names[norm_name] = raw_name
658
+
659
+ if not isinstance(items, list):
660
+ result.issues.append(ParseIssue(
661
+ message=f"dependency-groups.{raw_name} must be a list",
662
+ issue_type="malformed",
663
+ raw=str(items)[:100]
664
+ ))
665
+ continue
666
+
667
+ group_items_map[norm_name] = items
668
+
669
+ parsed_group_items: Dict[str, List[Tuple[str, Any, Any]]] = {}
670
+
671
+ for norm_grp, items in group_items_map.items():
672
+ parsed_group_items[norm_grp] = []
673
+ raw_grp_name = normalized_names[norm_grp]
674
+
675
+ for item in items:
676
+ if isinstance(item, str):
677
+ clean = _strip_inline_comment(item)
678
+ if clean:
679
+ parsed_group_items[norm_grp].append(("req", clean, item))
680
+ elif isinstance(item, dict):
681
+ if "include-group" not in item:
682
+ result.issues.append(ParseIssue(
683
+ message=f"Dictionary in dependency-groups.{raw_grp_name} missing 'include-group' key: {item}",
684
+ issue_type="malformed",
685
+ raw=str(item)
686
+ ))
687
+ continue
688
+ if len(item) > 1:
689
+ result.issues.append(ParseIssue(
690
+ message=f"include-group table in dependency-groups.{raw_grp_name} contains unexpected keys (only 'include-group' is allowed): {item}",
691
+ issue_type="malformed",
692
+ raw=str(item)
693
+ ))
694
+ continue
695
+ inc_target = item["include-group"]
696
+ if not isinstance(inc_target, str) or not inc_target.strip():
697
+ result.issues.append(ParseIssue(
698
+ message=f"include-group target in dependency-groups.{raw_grp_name} must be a non-empty string: {inc_target}",
699
+ issue_type="malformed",
700
+ raw=str(item)
701
+ ))
702
+ continue
703
+ norm_target = normalize_package_name(inc_target)
704
+ parsed_group_items[norm_grp].append(("include", norm_target, inc_target))
705
+ else:
706
+ result.issues.append(ParseIssue(
707
+ message=f"Item in dependency-groups.{raw_grp_name} must be a string or include-group table: {item}",
708
+ issue_type="malformed",
709
+ raw=str(item)
710
+ ))
711
+
712
+ # Resolve each root group with active call-stack cycle detection.
713
+ # In accordance with PEP 735, dependency duplication across includes or within
714
+ # groups is strictly preserved (pure syntactic expansion without deduplication).
715
+ for root_norm_grp in group_items_map.keys():
716
+ root_raw_name = normalized_names[root_norm_grp]
717
+ resolved_reqs: List[Tuple[str, str]] = []
718
+
719
+ def dfs(current_norm: str, call_stack: List[str]) -> None:
720
+ if current_norm in call_stack:
721
+ cycle_str = " -> ".join([normalized_names.get(g, g) for g in call_stack] + [normalized_names.get(current_norm, current_norm)])
722
+ result.issues.append(ParseIssue(
723
+ message=f"Circular dependency-group include detected: {cycle_str}",
724
+ issue_type="malformed",
725
+ raw=cycle_str
726
+ ))
727
+ return
728
+
729
+ if current_norm not in group_items_map:
730
+ result.issues.append(ParseIssue(
731
+ message=f"Included dependency group '{current_norm}' not found in dependency-groups",
732
+ issue_type="malformed",
733
+ raw=current_norm
734
+ ))
735
+ return
736
+
737
+ next_stack = call_stack + [current_norm]
738
+ for item_type, val1, val2 in parsed_group_items.get(current_norm, []):
739
+ if item_type == "req":
740
+ resolved_reqs.append((val1, val2))
741
+ elif item_type == "include":
742
+ dfs(val1, next_stack)
743
+
744
+ dfs(root_norm_grp, [])
745
+
746
+ for clean_line, raw_line in resolved_reqs:
747
+ rec, issue = _parse_pep508_requirement(
748
+ clean_line=clean_line,
749
+ filename=filename,
750
+ raw_line=raw_line,
751
+ group=root_raw_name,
752
+ line_number=None
753
+ )
754
+ if rec:
755
+ result.dependencies.append(rec)
756
+ if issue:
757
+ result.issues.append(issue)
758
+
759
+
760
+ def parse_pyproject_toml(content_or_path: str, filename: str = "pyproject.toml") -> ManifestParseResult:
761
+ """
762
+ Deterministically parses a pyproject.toml document into normalized DependencyRecords.
763
+ Supports:
764
+ - PEP 621: [project.dependencies] and [project.optional-dependencies]
765
+ - PEP 735: [dependency-groups]
766
+ - Poetry: [tool.poetry.dependencies], [tool.poetry.dev-dependencies], and [tool.poetry.group.<name>.dependencies]
767
+ """
768
+ source_path = None
769
+ if "\n" not in content_or_path and os.path.isfile(content_or_path):
770
+ source_path = content_or_path
771
+ filename = os.path.basename(content_or_path)
772
+ with open(content_or_path, "r", encoding="utf-8", errors="replace") as f:
773
+ content = f.read()
774
+ else:
775
+ content = content_or_path
776
+
777
+ result = ManifestParseResult(source_type="pyproject.toml", source_path=source_path)
778
+
779
+ if tomllib is None:
780
+ result.issues.append(ParseIssue(
781
+ message="TOML parser unavailable (requires Python 3.11+ or tomli)",
782
+ issue_type="malformed"
783
+ ))
784
+ return result
785
+
786
+ try:
787
+ data = tomllib.loads(content)
788
+ except Exception as te:
789
+ result.issues.append(ParseIssue(
790
+ message=f"Malformed TOML in pyproject.toml: {te}",
791
+ issue_type="malformed",
792
+ raw=content[:200]
793
+ ))
794
+ return result
795
+
796
+ if not isinstance(data, dict):
797
+ result.issues.append(ParseIssue(
798
+ message="pyproject.toml root must be a table",
799
+ issue_type="malformed"
800
+ ))
801
+ return result
802
+
803
+ # 1. PEP 621 Declarations: [project]
804
+ project = data.get("project")
805
+ if isinstance(project, dict):
806
+ # [project.dependencies]
807
+ deps = project.get("dependencies")
808
+ if isinstance(deps, list):
809
+ for dep_item in deps:
810
+ if not isinstance(dep_item, str):
811
+ result.issues.append(ParseIssue(
812
+ message=f"Dependency item in project.dependencies must be string: {dep_item}",
813
+ issue_type="malformed",
814
+ raw=str(dep_item)
815
+ ))
816
+ continue
817
+ clean = _strip_inline_comment(dep_item)
818
+ if not clean:
819
+ continue
820
+ rec, issue = _parse_pep508_requirement(
821
+ clean_line=clean,
822
+ filename=filename,
823
+ raw_line=dep_item,
824
+ group=None,
825
+ line_number=None
826
+ )
827
+ if rec:
828
+ result.dependencies.append(rec)
829
+ if issue:
830
+ result.issues.append(issue)
831
+ elif deps is not None:
832
+ result.issues.append(ParseIssue(
833
+ message="project.dependencies must be a list",
834
+ issue_type="malformed",
835
+ raw=str(deps)[:100]
836
+ ))
837
+
838
+ # [project.optional-dependencies]
839
+ opt_deps = project.get("optional-dependencies")
840
+ if isinstance(opt_deps, dict):
841
+ for grp_name, grp_items in opt_deps.items():
842
+ if isinstance(grp_items, list):
843
+ for dep_item in grp_items:
844
+ if not isinstance(dep_item, str):
845
+ result.issues.append(ParseIssue(
846
+ message=f"Dependency item in optional-dependencies.{grp_name} must be string: {dep_item}",
847
+ issue_type="malformed",
848
+ raw=str(dep_item)
849
+ ))
850
+ continue
851
+ clean = _strip_inline_comment(dep_item)
852
+ if not clean:
853
+ continue
854
+ rec, issue = _parse_pep508_requirement(
855
+ clean_line=clean,
856
+ filename=filename,
857
+ raw_line=dep_item,
858
+ group=grp_name,
859
+ line_number=None
860
+ )
861
+ if rec:
862
+ result.dependencies.append(rec)
863
+ if issue:
864
+ result.issues.append(issue)
865
+ else:
866
+ result.issues.append(ParseIssue(
867
+ message=f"optional-dependencies.{grp_name} must be a list",
868
+ issue_type="malformed",
869
+ raw=str(grp_items)[:100]
870
+ ))
871
+ elif opt_deps is not None:
872
+ result.issues.append(ParseIssue(
873
+ message="project.optional-dependencies must be a table",
874
+ issue_type="malformed",
875
+ raw=str(opt_deps)[:100]
876
+ ))
877
+
878
+ # PEP 735: [dependency-groups]
879
+ if "dependency-groups" in data:
880
+ dep_groups = data.get("dependency-groups")
881
+ _parse_pep735_dependency_groups(dep_groups, filename, result)
882
+
883
+ # 2. Poetry Declarations: [tool.poetry]
884
+ tool = data.get("tool")
885
+ if isinstance(tool, dict):
886
+ poetry = tool.get("poetry")
887
+ if isinstance(poetry, dict):
888
+ # [tool.poetry.dependencies]
889
+ p_deps = poetry.get("dependencies")
890
+ if isinstance(p_deps, dict):
891
+ _parse_poetry_dependency_dict(p_deps, filename, group="main", result=result)
892
+ elif p_deps is not None:
893
+ result.issues.append(ParseIssue(
894
+ message="tool.poetry.dependencies must be a table",
895
+ issue_type="malformed",
896
+ raw=str(p_deps)[:100]
897
+ ))
898
+
899
+ # [tool.poetry.dev-dependencies] (legacy)
900
+ p_dev = poetry.get("dev-dependencies")
901
+ if isinstance(p_dev, dict):
902
+ _parse_poetry_dependency_dict(p_dev, filename, group="dev", result=result)
903
+ elif p_dev is not None:
904
+ result.issues.append(ParseIssue(
905
+ message="tool.poetry.dev-dependencies must be a table",
906
+ issue_type="malformed",
907
+ raw=str(p_dev)[:100]
908
+ ))
909
+
910
+ # [tool.poetry.group.<name>.dependencies]
911
+ p_groups = poetry.get("group")
912
+ if isinstance(p_groups, dict):
913
+ for grp_name, grp_table in p_groups.items():
914
+ if isinstance(grp_table, dict):
915
+ sub_deps = grp_table.get("dependencies")
916
+ if isinstance(sub_deps, dict):
917
+ _parse_poetry_dependency_dict(sub_deps, filename, group=grp_name, result=result)
918
+ elif sub_deps is not None:
919
+ result.issues.append(ParseIssue(
920
+ message=f"tool.poetry.group.{grp_name}.dependencies must be a table",
921
+ issue_type="malformed",
922
+ raw=str(sub_deps)[:100]
923
+ ))
924
+
925
+ return result
926
+
927
+
928
+ def detect_manifest_type(filename: str) -> Optional[str]:
929
+ """Infers manifest type ('requirements.txt', 'Pipfile.lock', 'poetry.lock', 'pyproject.toml') from a filename."""
930
+ base = os.path.basename(filename).lower()
931
+ if "requirements" in base or base.endswith(".txt"):
932
+ return "requirements.txt"
933
+ if base == "pipfile.lock":
934
+ return "Pipfile.lock"
935
+ if base == "poetry.lock":
936
+ return "poetry.lock"
937
+ if base == "pyproject.toml":
938
+ return "pyproject.toml"
939
+ return None
940
+
941
+
942
+ def parse_manifest(file_path_or_content: str, manifest_type: Optional[str] = None, filename: Optional[str] = None) -> ManifestParseResult:
943
+ """
944
+ Unified entrypoint: Parses requirements.txt, Pipfile.lock, poetry.lock, or pyproject.toml.
945
+ Infers manifest type from file path if manifest_type is omitted.
946
+ """
947
+ effective_name = filename or ("manifest" if "\n" in file_path_or_content else os.path.basename(file_path_or_content))
948
+ mtype = manifest_type or detect_manifest_type(effective_name)
949
+
950
+ if mtype == "requirements.txt":
951
+ return parse_requirements_txt(file_path_or_content, filename=effective_name)
952
+ elif mtype == "Pipfile.lock":
953
+ return parse_pipfile_lock(file_path_or_content, filename=effective_name)
954
+ elif mtype == "poetry.lock":
955
+ return parse_poetry_lock(file_path_or_content, filename=effective_name)
956
+ elif mtype == "pyproject.toml":
957
+ return parse_pyproject_toml(file_path_or_content, filename=effective_name)
958
+ else:
959
+ # Fallback to requirements.txt
960
+ return parse_requirements_txt(file_path_or_content, filename=effective_name)