codeanalyzer-python 1.4.0__tar.gz → 1.4.1__tar.gz
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.
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/PKG-INFO +13 -4
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/README.md +12 -3
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/builder.py +3 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/identity.py +3 -3
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/matching.py +36 -7
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/pipeline.py +52 -4
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/rules.py +12 -1
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/rules.yml +34 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/neo4j/bolt.py +83 -56
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/neo4j/cypher.py +10 -4
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/neo4j/project.py +97 -41
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/neo4j/rows.py +36 -1
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/neo4j/schema.py +9 -24
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/schema/ids.py +17 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/schema/l1_body.py +2 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/schema/py_schema.py +6 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/pyproject.toml +1 -1
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/schema.neo4j.json +11 -18
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/.gitignore +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/LICENSE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/NOTICE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/__main__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/artifacts/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/artifacts/config_keys.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/artifacts/config_use.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/artifacts/config_use_rules.yml +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/artifacts/dependencies.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/artifacts/discovery.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/artifacts/parsers.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/core.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/access_paths.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/alias.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/cfg.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/defuse.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/dominance.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/pdg.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/LICENSE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/SSA/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/SSA/const.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/cfg/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/cfg/builder.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/cfg/model.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/core/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/core/func_call_visitor.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel/core/vars_visitor.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scalpel_oracle.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/scc.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/sdg.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/slicing.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/summaries.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/dataflow/syntactic.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/detect.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/jedi/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/jedi/jedi.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/neo4j/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/neo4j/emit.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/options/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/options/options.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/provenance.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/py.typed +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/schema/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/schema/assign_ids.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/schema/call_graph_ids.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/schema/l2_callees.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/semantic_analysis/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/semantic_analysis/call_graph.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/semantic_analysis/defuse_linker.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/syntactic_analysis/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/syntactic_analysis/exceptions.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/syntactic_analysis/import_resolver.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/syntactic_analysis/symbol_table_builder.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/utils/__init__.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/utils/logging.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/utils/progress_bar.py +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/docs/handoff/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/docs/handoff/schema.neo4j.json +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/schema-uml.drawio +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/flask/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/flask/examples/celery/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/flask/requirements-skip/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/flask/src/flask/sansio/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/requests/LICENSE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/requests/NOTICE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/requests/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/requests/docs/_themes/LICENSE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/requests/ext/LICENSE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/xarray/LICENSE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/xarray/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/xarray/properties/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/xarray/xarray/datatree_/LICENSE +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/xarray/xarray/datatree_/README.md +0 -0
- {codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/test/fixtures/whole_applications/xarray/xarray/datatree_/docs/README.md +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: codeanalyzer-python
|
|
3
|
-
Version: 1.4.
|
|
3
|
+
Version: 1.4.1
|
|
4
4
|
Summary: Static analysis for Python — canonical schema v2 (symbol table, call graph, and native CFG/PDG/SDG dataflow) as analysis.json or a Neo4j property graph.
|
|
5
5
|
Author-email: Rahul Krishna <i.m.ralk@gmail.com>
|
|
6
6
|
License-File: LICENSE
|
|
@@ -459,7 +459,7 @@ levels are cumulative and additive — `analysis.json(-a 1) ⊆ … ⊆ analysis
|
|
|
459
459
|
| **1** | `-a 1` (default) | Symbol table, Jedi call graph, and `call` nodes in each callable's `body` | `body` calls (`callee: null`) |
|
|
460
460
|
| **2** | `-a 2` | Defuse-linker call-graph enrichment; each call's `callee` backfilled to a `can://` id | `call_graph`, `body` callees |
|
|
461
461
|
| **3** | `-a 3` | Native **intraprocedural** CFG/CDG/DDG (syntactic, name-equality, `prov: ["ssa"]`) | `cfg`, `cdg`, `ddg`, `@entry`/`@exit` on each callable |
|
|
462
|
-
| **4** | `-a 4` | **Interprocedural** SDG: synthetic param vertices, alias-aware DDG (`prov: ["points-to"]`) | `param_in`, `param_out`, `summary`, semantic `ddg` |
|
|
462
|
+
| **4** | `-a 4` | **Interprocedural** SDG: synthetic param vertices, alias-aware DDG (`prov: ["points-to"]`), port-wiring DDG between statements and param vertices (`prov: ["reaching-defs"]`) | `param_in`, `param_out`, `summary`, semantic `ddg` |
|
|
463
463
|
|
|
464
464
|
`-a 1`/`-a 2` timings and output are unaffected by the heavier levels — nothing at level 3+ runs
|
|
465
465
|
unless requested. Flag gating: `--graphs sdg` requires `-a 4`; `--graphs cfg,dfg,pdg` and
|
|
@@ -481,7 +481,11 @@ symbol-table signature by construction
|
|
|
481
481
|
- **Points-to oracle (level 4):** the **Scalpel** may-alias oracle — `ScalpelAliasOracle`
|
|
482
482
|
(`codeanalyzer/dataflow/scalpel_oracle.py`) — consumes Scalpel's SSA copy/const facts to answer
|
|
483
483
|
`may_alias(path_a, path_b)`, adding the alias-aware DDG edges (`prov: ["points-to"]`) and the
|
|
484
|
-
interprocedural summaries.
|
|
484
|
+
interprocedural summaries. Level 4 also wires the statement-level DDG to the param
|
|
485
|
+
vertices (def → `actual_in`, `actual_out` → call site, `formal_in` → use, def → `formal_out`)
|
|
486
|
+
with `prov: ["reaching-defs"]`; without those the SDG would be two disconnected graphs. So
|
|
487
|
+
`prov` takes three values: `ssa` (syntactic, L3), `reaching-defs` (port wiring, L4) and
|
|
488
|
+
`points-to` (alias-derived, L4). Scalpel is **vendored** — a `typed_ast`-free slice built into the
|
|
485
489
|
package under `codeanalyzer/dataflow/scalpel/` — so it is the **default** level-4 oracle with no
|
|
486
490
|
external dependency to install; the analyzer falls back to the built-in `TypeBasedAliasOracle`
|
|
487
491
|
(Jedi-inferred types; unknown types conservatively alias) only when Scalpel can't resolve a
|
|
@@ -546,7 +550,8 @@ A **callable** (function or method) carries its own CPG, keyed by node id:
|
|
|
546
550
|
"body": { // node id → node
|
|
547
551
|
"@entry": { "kind": "entry" },
|
|
548
552
|
"6:4": { "kind": "statement", "span": { … } },
|
|
549
|
-
"6:8": { "kind": "call", "span": { … },
|
|
553
|
+
"6:8": { "id": "can://…/main()@6:8", "kind": "call", "span": { … },
|
|
554
|
+
"callee": "can://…/helper(x)" }, // callee null until L2
|
|
550
555
|
"@formal_in:0": { "kind": "formal_in", "of": "a" }, // L4 param vertices
|
|
551
556
|
"6:4/actual_in:0": { "kind": "actual_in", "of": "a", "parent": "6:4" },
|
|
552
557
|
"@exit": { "kind": "exit" }
|
|
@@ -686,6 +691,10 @@ RETURN DISTINCT c.id
|
|
|
686
691
|
MATCH (m:PyCallable {is_entrypoint: true})
|
|
687
692
|
RETURN m.id, m.entrypoint_frameworks
|
|
688
693
|
|
|
694
|
+
// did the entrypoint pass find anything? (no entrypoints vs. nothing detected)
|
|
695
|
+
MATCH (a:PyApplication)
|
|
696
|
+
RETURN a.entrypoint_frameworks, a.entrypoint_report_json
|
|
697
|
+
|
|
689
698
|
// data dependences into one statement (level 3+)
|
|
690
699
|
MATCH (s:PyBodyNode {id: $stmt})<-[d:PY_DDG]-(src:PyBodyNode)
|
|
691
700
|
RETURN src.id, d.var, d.prov
|
|
@@ -426,7 +426,7 @@ levels are cumulative and additive — `analysis.json(-a 1) ⊆ … ⊆ analysis
|
|
|
426
426
|
| **1** | `-a 1` (default) | Symbol table, Jedi call graph, and `call` nodes in each callable's `body` | `body` calls (`callee: null`) |
|
|
427
427
|
| **2** | `-a 2` | Defuse-linker call-graph enrichment; each call's `callee` backfilled to a `can://` id | `call_graph`, `body` callees |
|
|
428
428
|
| **3** | `-a 3` | Native **intraprocedural** CFG/CDG/DDG (syntactic, name-equality, `prov: ["ssa"]`) | `cfg`, `cdg`, `ddg`, `@entry`/`@exit` on each callable |
|
|
429
|
-
| **4** | `-a 4` | **Interprocedural** SDG: synthetic param vertices, alias-aware DDG (`prov: ["points-to"]`) | `param_in`, `param_out`, `summary`, semantic `ddg` |
|
|
429
|
+
| **4** | `-a 4` | **Interprocedural** SDG: synthetic param vertices, alias-aware DDG (`prov: ["points-to"]`), port-wiring DDG between statements and param vertices (`prov: ["reaching-defs"]`) | `param_in`, `param_out`, `summary`, semantic `ddg` |
|
|
430
430
|
|
|
431
431
|
`-a 1`/`-a 2` timings and output are unaffected by the heavier levels — nothing at level 3+ runs
|
|
432
432
|
unless requested. Flag gating: `--graphs sdg` requires `-a 4`; `--graphs cfg,dfg,pdg` and
|
|
@@ -448,7 +448,11 @@ symbol-table signature by construction
|
|
|
448
448
|
- **Points-to oracle (level 4):** the **Scalpel** may-alias oracle — `ScalpelAliasOracle`
|
|
449
449
|
(`codeanalyzer/dataflow/scalpel_oracle.py`) — consumes Scalpel's SSA copy/const facts to answer
|
|
450
450
|
`may_alias(path_a, path_b)`, adding the alias-aware DDG edges (`prov: ["points-to"]`) and the
|
|
451
|
-
interprocedural summaries.
|
|
451
|
+
interprocedural summaries. Level 4 also wires the statement-level DDG to the param
|
|
452
|
+
vertices (def → `actual_in`, `actual_out` → call site, `formal_in` → use, def → `formal_out`)
|
|
453
|
+
with `prov: ["reaching-defs"]`; without those the SDG would be two disconnected graphs. So
|
|
454
|
+
`prov` takes three values: `ssa` (syntactic, L3), `reaching-defs` (port wiring, L4) and
|
|
455
|
+
`points-to` (alias-derived, L4). Scalpel is **vendored** — a `typed_ast`-free slice built into the
|
|
452
456
|
package under `codeanalyzer/dataflow/scalpel/` — so it is the **default** level-4 oracle with no
|
|
453
457
|
external dependency to install; the analyzer falls back to the built-in `TypeBasedAliasOracle`
|
|
454
458
|
(Jedi-inferred types; unknown types conservatively alias) only when Scalpel can't resolve a
|
|
@@ -513,7 +517,8 @@ A **callable** (function or method) carries its own CPG, keyed by node id:
|
|
|
513
517
|
"body": { // node id → node
|
|
514
518
|
"@entry": { "kind": "entry" },
|
|
515
519
|
"6:4": { "kind": "statement", "span": { … } },
|
|
516
|
-
"6:8": { "kind": "call", "span": { … },
|
|
520
|
+
"6:8": { "id": "can://…/main()@6:8", "kind": "call", "span": { … },
|
|
521
|
+
"callee": "can://…/helper(x)" }, // callee null until L2
|
|
517
522
|
"@formal_in:0": { "kind": "formal_in", "of": "a" }, // L4 param vertices
|
|
518
523
|
"6:4/actual_in:0": { "kind": "actual_in", "of": "a", "parent": "6:4" },
|
|
519
524
|
"@exit": { "kind": "exit" }
|
|
@@ -653,6 +658,10 @@ RETURN DISTINCT c.id
|
|
|
653
658
|
MATCH (m:PyCallable {is_entrypoint: true})
|
|
654
659
|
RETURN m.id, m.entrypoint_frameworks
|
|
655
660
|
|
|
661
|
+
// did the entrypoint pass find anything? (no entrypoints vs. nothing detected)
|
|
662
|
+
MATCH (a:PyApplication)
|
|
663
|
+
RETURN a.entrypoint_frameworks, a.entrypoint_report_json
|
|
664
|
+
|
|
656
665
|
// data dependences into one statement (level 3+)
|
|
657
666
|
MATCH (s:PyBodyNode {id: $stmt})<-[d:PY_DDG]-(src:PyBodyNode)
|
|
658
667
|
RETURN src.id, d.var, d.prov
|
|
@@ -43,6 +43,7 @@ from codeanalyzer.dataflow.alias import TypeBasedAliasOracle
|
|
|
43
43
|
from codeanalyzer.dataflow.pdg import build_pdg
|
|
44
44
|
from codeanalyzer.dataflow.sdg import ProgramGraphsIR, assemble_sdg
|
|
45
45
|
from codeanalyzer.dataflow.summaries import CallSite, FunctionInfo, compute_summaries
|
|
46
|
+
from codeanalyzer.schema.ids import stamp_body_ids
|
|
46
47
|
from codeanalyzer.schema.py_schema import PyApplication, PyCallable, PyClass, PyModule
|
|
47
48
|
from codeanalyzer.utils import logger
|
|
48
49
|
|
|
@@ -333,6 +334,7 @@ def emit_l3_body(
|
|
|
333
334
|
for e in pdg.edges
|
|
334
335
|
if e.type == "DDG"
|
|
335
336
|
]
|
|
337
|
+
stamp_body_ids(pycallable)
|
|
336
338
|
|
|
337
339
|
|
|
338
340
|
def build_program_graphs(
|
|
@@ -538,6 +540,7 @@ def emit_l4(
|
|
|
538
540
|
pycallable.body[im.local(pn.id)] = BodyNode(
|
|
539
541
|
kind=pn.kind, of=pn.var, parent=parent
|
|
540
542
|
)
|
|
543
|
+
stamp_body_ids(pycallable)
|
|
541
544
|
|
|
542
545
|
# (b/c/d) SDG edges → summary / param_in / param_out; CALL dropped.
|
|
543
546
|
for e in ir.sdg_edges:
|
|
@@ -16,6 +16,8 @@ from __future__ import annotations
|
|
|
16
16
|
from collections import defaultdict
|
|
17
17
|
from typing import Dict, Iterable, Optional, Tuple
|
|
18
18
|
|
|
19
|
+
from codeanalyzer.schema.ids import global_ordinal
|
|
20
|
+
|
|
19
21
|
|
|
20
22
|
class IdentityMap:
|
|
21
23
|
def __init__(self, callable_id: str, id_to_local: Dict[int, str]):
|
|
@@ -83,9 +85,7 @@ class IdentityMap:
|
|
|
83
85
|
|
|
84
86
|
def global_id(self, node_id: int) -> str:
|
|
85
87
|
"""Fully addressable id: ``"<callable-id>@<local>"``."""
|
|
86
|
-
|
|
87
|
-
# local statements are "line:col"; bookends already carry the leading "@"
|
|
88
|
-
return f"{self._callable_id}{loc}" if loc.startswith("@") else f"{self._callable_id}@{loc}"
|
|
88
|
+
return global_ordinal(self._callable_id, self._map[node_id])
|
|
89
89
|
|
|
90
90
|
def node_ids(self) -> Iterable[int]:
|
|
91
91
|
return self._map.keys()
|
{codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/matching.py
RENAMED
|
@@ -64,7 +64,8 @@ def _compile(pattern: str) -> str:
|
|
|
64
64
|
if ch == "{":
|
|
65
65
|
j = pattern.index("}", i)
|
|
66
66
|
alts = pattern[i + 1 : j].split(",")
|
|
67
|
-
|
|
67
|
+
# `*` keeps its meaning inside an alternative, so `{route,*.route}` works.
|
|
68
|
+
out.append("(?:" + "|".join(_compile(a.strip()) for a in alts) + ")")
|
|
68
69
|
i = j + 1
|
|
69
70
|
elif ch == "*":
|
|
70
71
|
out.append(r"[^.\s]*")
|
|
@@ -93,15 +94,17 @@ def _route_of(dec, spec: Optional[Dict[str, Any]]) -> Optional[str]:
|
|
|
93
94
|
if idx >= len(args):
|
|
94
95
|
return None
|
|
95
96
|
value = _literal(args[idx])
|
|
97
|
+
if isinstance(value, (list, tuple)):
|
|
98
|
+
value = next((v for v in value if isinstance(v, str)), None)
|
|
96
99
|
return value if isinstance(value, str) else None
|
|
97
100
|
|
|
98
101
|
|
|
99
|
-
def _methods_of(dec, spec: Optional[Dict[str, Any]]) -> List[str]:
|
|
102
|
+
def _methods_of(dec, spec: Optional[Dict[str, Any]], qualified: Optional[str] = None) -> List[str]:
|
|
100
103
|
if not spec:
|
|
101
104
|
return []
|
|
102
105
|
source = spec.get("from")
|
|
103
106
|
if source == "match_suffix":
|
|
104
|
-
verb = (dec.qualified_name or "").rsplit(".", 1)[-1]
|
|
107
|
+
verb = (qualified or dec.qualified_name or "").rsplit(".", 1)[-1]
|
|
105
108
|
return [verb.upper()]
|
|
106
109
|
if source == "keyword":
|
|
107
110
|
raw = (dec.keyword_arguments or {}).get(spec.get("name", ""))
|
|
@@ -113,12 +116,27 @@ def _methods_of(dec, spec: Optional[Dict[str, Any]]) -> List[str]:
|
|
|
113
116
|
|
|
114
117
|
|
|
115
118
|
def entrypoints_from_decorators(
|
|
116
|
-
node,
|
|
119
|
+
node,
|
|
120
|
+
framework: str,
|
|
121
|
+
rules: Iterable["DecoratorRule"],
|
|
122
|
+
resolve: Optional[Callable[[str], str]] = None,
|
|
123
|
+
on_written: bool = False,
|
|
117
124
|
) -> List[PyEntrypoint]:
|
|
125
|
+
"""``resolve`` is the module's import-table resolver (#177): when Jedi could
|
|
126
|
+
not resolve a decorator (the framework is not importable in the analysis
|
|
127
|
+
environment -- every ``--no-venv`` run), ``@http.route`` still resolves to
|
|
128
|
+
``odoo.http.route`` from ``from odoo import http`` alone, the same way base
|
|
129
|
+
classes already do. Jedi's definition path wins when it exists.
|
|
130
|
+
|
|
131
|
+
``on_written`` is the heuristic tier: rules match the decorator's spelling
|
|
132
|
+
as WRITTEN (``http.route``, ``router.post``), no resolution at all, so a
|
|
133
|
+
shape that reads as an HTTP entrypoint is recorded whether or not any
|
|
134
|
+
framework rule knows the library behind it."""
|
|
118
135
|
out: List[PyEntrypoint] = []
|
|
119
136
|
for dec in getattr(node, "decorators", []) or []:
|
|
137
|
+
qualified = dec.name if on_written else decorator_qualified_name(dec, resolve)
|
|
120
138
|
for rule in rules:
|
|
121
|
-
if not match_pattern(rule.match,
|
|
139
|
+
if not match_pattern(rule.match, qualified):
|
|
122
140
|
continue
|
|
123
141
|
out.append(
|
|
124
142
|
PyEntrypoint(
|
|
@@ -126,14 +144,25 @@ def entrypoints_from_decorators(
|
|
|
126
144
|
confidence=rule.confidence,
|
|
127
145
|
rule=rule.id,
|
|
128
146
|
ruleset=rule.origin,
|
|
129
|
-
evidence=
|
|
147
|
+
evidence=qualified,
|
|
130
148
|
route=_route_of(dec, rule.route),
|
|
131
|
-
http_methods=_methods_of(dec, rule.methods),
|
|
149
|
+
http_methods=_methods_of(dec, rule.methods, qualified),
|
|
132
150
|
)
|
|
133
151
|
)
|
|
134
152
|
return out
|
|
135
153
|
|
|
136
154
|
|
|
155
|
+
def decorator_qualified_name(dec, resolve: Optional[Callable[[str], str]]) -> Optional[str]:
|
|
156
|
+
"""Jedi's resolution, else the import-table resolution of the written
|
|
157
|
+
spelling, else ``None`` (a spelling the import table cannot map either)."""
|
|
158
|
+
if dec.qualified_name:
|
|
159
|
+
return dec.qualified_name
|
|
160
|
+
if resolve is None:
|
|
161
|
+
return None
|
|
162
|
+
resolved = resolve(dec.name)
|
|
163
|
+
return resolved if resolved != dec.name else None
|
|
164
|
+
|
|
165
|
+
|
|
137
166
|
def entrypoints_from_bases(
|
|
138
167
|
cls,
|
|
139
168
|
framework: str,
|
{codeanalyzer_python-1.4.0 → codeanalyzer_python-1.4.1}/codeanalyzer/entrypoints/pipeline.py
RENAMED
|
@@ -6,11 +6,14 @@ loses flags, never the analysis.
|
|
|
6
6
|
"""
|
|
7
7
|
from __future__ import annotations
|
|
8
8
|
|
|
9
|
+
import builtins
|
|
9
10
|
from pathlib import Path
|
|
10
|
-
from typing import Dict, Iterable, Iterator
|
|
11
|
+
from typing import Dict, Iterable, Iterator, Set
|
|
11
12
|
|
|
12
13
|
from codeanalyzer.entrypoints.detect import detected_frameworks
|
|
13
|
-
from codeanalyzer.entrypoints.matching import
|
|
14
|
+
from codeanalyzer.entrypoints.matching import (
|
|
15
|
+
decorator_qualified_name, entrypoints_from_bases, entrypoints_from_decorators,
|
|
16
|
+
)
|
|
14
17
|
from codeanalyzer.entrypoints.rules import RuleSet, load_rules
|
|
15
18
|
from codeanalyzer.schema.py_schema import PyApplication, PyCallable, PyClass, PyModule
|
|
16
19
|
from codeanalyzer.utils import logger
|
|
@@ -56,18 +59,34 @@ def _run_stages(app: PyApplication, project_dir: Path, rules: RuleSet) -> None:
|
|
|
56
59
|
"""
|
|
57
60
|
for node in _walk(app):
|
|
58
61
|
node.entrypoints = []
|
|
62
|
+
app.entrypoint_report.unresolved = {}
|
|
59
63
|
|
|
60
64
|
app.entrypoint_report.rulesets = list(rules.rulesets)
|
|
61
65
|
frameworks = detected_frameworks(app, project_dir, rules)
|
|
62
66
|
app.entrypoint_report.frameworks_detected = sorted(frameworks)
|
|
63
67
|
|
|
64
68
|
names = sorted(frameworks)
|
|
69
|
+
unresolved = app.entrypoint_report.unresolved
|
|
65
70
|
for mod in app.symbol_table.values():
|
|
66
71
|
resolve = _base_resolver(mod)
|
|
72
|
+
known = _known_heads(mod)
|
|
67
73
|
for node in _walk_module(mod):
|
|
74
|
+
# #177: what neither Jedi nor the import table could name. This is
|
|
75
|
+
# the counter that makes silence visible; it was never written before.
|
|
76
|
+
# A builtin, a declared class, or a name whose head is imported is
|
|
77
|
+
# nameable and is not counted (`object`, `Exception`, `typing.Generic[T]`).
|
|
78
|
+
for dec in getattr(node, "decorators", None) or []:
|
|
79
|
+
if decorator_qualified_name(dec, resolve) is None and _unnameable(dec.name, known):
|
|
80
|
+
unresolved[dec.name] = unresolved.get(dec.name, 0) + 1
|
|
81
|
+
if isinstance(node, PyClass):
|
|
82
|
+
for base in node.base_classes or []:
|
|
83
|
+
if _unnameable(base, known):
|
|
84
|
+
unresolved[base] = unresolved.get(base, 0) + 1
|
|
68
85
|
for name in names:
|
|
69
86
|
fw = rules.frameworks[name]
|
|
70
|
-
node.entrypoints.extend(
|
|
87
|
+
node.entrypoints.extend(
|
|
88
|
+
entrypoints_from_decorators(node, name, fw.decorators, resolve)
|
|
89
|
+
)
|
|
71
90
|
if isinstance(node, PyClass) and fw.bases:
|
|
72
91
|
class_eps, method_eps = entrypoints_from_bases(
|
|
73
92
|
node, name, fw.bases, resolve
|
|
@@ -77,6 +96,34 @@ def _run_stages(app: PyApplication, project_dir: Path, rules: RuleSet) -> None:
|
|
|
77
96
|
target = (node.callables or {}).get(method_name)
|
|
78
97
|
if target is not None:
|
|
79
98
|
target.entrypoints.extend(eps)
|
|
99
|
+
# Heuristic tier: the written spelling, no framework needed. Runs
|
|
100
|
+
# last so a node a framework rule already claimed keeps one record.
|
|
101
|
+
if not node.entrypoints and rules.heuristics:
|
|
102
|
+
node.entrypoints.extend(
|
|
103
|
+
entrypoints_from_decorators(
|
|
104
|
+
node, "heuristic", rules.heuristics, resolve, on_written=True
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
_BUILTIN_NAMES: Set[str] = set(dir(builtins))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _known_heads(mod: PyModule) -> Set[str]:
|
|
113
|
+
"""Names that can head a nameable spelling in this module: its declared classes
|
|
114
|
+
and every imported name or alias."""
|
|
115
|
+
heads = {cl.name for cl in (mod.types or {}).values()}
|
|
116
|
+
for imp in mod.imports or []:
|
|
117
|
+
heads.add(imp.alias or imp.name)
|
|
118
|
+
return heads
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _unnameable(written: str, known: Set[str]) -> bool:
|
|
122
|
+
"""Whether a written base/decorator spelling maps to nothing this module can
|
|
123
|
+
name: not a builtin, not a declared class, and its head is not imported.
|
|
124
|
+
Subscripts (`Generic[T]`, `dict[K, V]`) are stripped before the check."""
|
|
125
|
+
head = written.split("[", 1)[0].split(".", 1)[0].strip()
|
|
126
|
+
return bool(head) and head not in _BUILTIN_NAMES and head not in known
|
|
80
127
|
|
|
81
128
|
|
|
82
129
|
def _base_resolver(mod: PyModule):
|
|
@@ -90,7 +137,8 @@ def _base_resolver(mod: PyModule):
|
|
|
90
137
|
aliases: Dict[str, str] = {}
|
|
91
138
|
for imp in mod.imports or []:
|
|
92
139
|
original = imp.alias or imp.name
|
|
93
|
-
|
|
140
|
+
module = imp.resolved_module or imp.module
|
|
141
|
+
aliases[imp.name] = module if imp.module == original else f"{module}.{original}"
|
|
94
142
|
|
|
95
143
|
def resolve(written: str) -> str:
|
|
96
144
|
head, _, rest = written.partition(".")
|
|
@@ -22,7 +22,7 @@ _CONFIDENCE = {"declared", "certain", "heuristic"}
|
|
|
22
22
|
# blocks (Units 4-5) not implemented yet; they are deliberately absent here
|
|
23
23
|
# rather than accepted-and-ignored, so a user file using them fails loudly
|
|
24
24
|
# instead of loading clean and doing nothing.
|
|
25
|
-
_TOP_LEVEL_KEYS = {"version", "frameworks", "disable"}
|
|
25
|
+
_TOP_LEVEL_KEYS = {"version", "frameworks", "heuristics", "disable"}
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
class RulesError(Exception):
|
|
@@ -60,6 +60,10 @@ class Framework:
|
|
|
60
60
|
@dataclass
|
|
61
61
|
class RuleSet:
|
|
62
62
|
frameworks: Dict[str, Framework] = field(default_factory=dict)
|
|
63
|
+
# Framework-independent decorator rules matched on the WRITTEN spelling,
|
|
64
|
+
# confidence `heuristic` by default. They run on every node regardless of
|
|
65
|
+
# `frameworks_detected` and never double a record a framework rule made.
|
|
66
|
+
heuristics: List[DecoratorRule] = field(default_factory=list)
|
|
63
67
|
rulesets: List[str] = field(default_factory=list)
|
|
64
68
|
|
|
65
69
|
|
|
@@ -103,9 +107,16 @@ def _merge(out: RuleSet, data: Dict[str, Any], origin: str) -> None:
|
|
|
103
107
|
for raw in body.get("bases") or []:
|
|
104
108
|
fw.bases.append(_base_rule(raw, origin))
|
|
105
109
|
|
|
110
|
+
heuristics = data.get("heuristics") or {}
|
|
111
|
+
if not isinstance(heuristics, dict):
|
|
112
|
+
raise RulesError(f"{origin}: `heuristics` must be a mapping")
|
|
113
|
+
for raw in heuristics.get("decorators") or []:
|
|
114
|
+
out.heuristics.append(_decorator_rule({"confidence": "heuristic", **raw}, origin))
|
|
115
|
+
|
|
106
116
|
for fw in out.frameworks.values():
|
|
107
117
|
fw.decorators = [r for r in fw.decorators if r.id not in disabled]
|
|
108
118
|
fw.bases = [r for r in fw.bases if r.id not in disabled]
|
|
119
|
+
out.heuristics = [r for r in out.heuristics if r.id not in disabled]
|
|
109
120
|
|
|
110
121
|
|
|
111
122
|
def _disable_list(data: Dict[str, Any], origin: str) -> List[str]:
|
|
@@ -22,6 +22,24 @@ frameworks:
|
|
|
22
22
|
transitive: true
|
|
23
23
|
dispatch: [get, post, put, delete, patch]
|
|
24
24
|
|
|
25
|
+
odoo:
|
|
26
|
+
detect: [odoo]
|
|
27
|
+
decorators:
|
|
28
|
+
# `route` is a plain function in odoo/http.py, so Jedi's definition path
|
|
29
|
+
# and the import-table fallback (`from odoo import http` + `@http.route`,
|
|
30
|
+
# the shape every --no-venv run sees) both spell it `odoo.http.route`.
|
|
31
|
+
# The first positional may be one route or a list of them. Odoo serves
|
|
32
|
+
# GET and POST on a route unless `methods=` narrows it (json-typed routes
|
|
33
|
+
# are POST), so the default is both, not GET.
|
|
34
|
+
- id: odoo.route
|
|
35
|
+
match: "odoo.http.route"
|
|
36
|
+
route: {from: positional, index: 0}
|
|
37
|
+
methods: {from: keyword, name: methods, default: [GET, POST]}
|
|
38
|
+
bases:
|
|
39
|
+
- id: odoo.controller
|
|
40
|
+
match: "odoo.http.Controller"
|
|
41
|
+
transitive: true
|
|
42
|
+
|
|
25
43
|
fastapi:
|
|
26
44
|
detect: [fastapi]
|
|
27
45
|
decorators:
|
|
@@ -86,3 +104,19 @@ frameworks:
|
|
|
86
104
|
match: "django.views.generic.*"
|
|
87
105
|
transitive: true
|
|
88
106
|
dispatch: [get, post, put, patch, delete, head, options]
|
|
107
|
+
|
|
108
|
+
# Framework-independent tier. Matched on the decorator's WRITTEN spelling, never
|
|
109
|
+
# on a resolved name, so a shape that reads as an HTTP entrypoint is flagged even
|
|
110
|
+
# when the library behind it has no `frameworks:` block above (or is not
|
|
111
|
+
# importable). Confidence `heuristic`; a consumer wanting only certain hits
|
|
112
|
+
# filters on it. A node a framework rule already matched gets no heuristic record.
|
|
113
|
+
heuristics:
|
|
114
|
+
decorators:
|
|
115
|
+
- id: heuristic.http-route
|
|
116
|
+
match: "{route,*.route,*.*.route}"
|
|
117
|
+
route: {from: positional, index: 0}
|
|
118
|
+
methods: {from: keyword, name: methods}
|
|
119
|
+
- id: heuristic.http-verb
|
|
120
|
+
match: "{*,*.*}.{get,post,put,patch,delete,head,options,websocket}"
|
|
121
|
+
route: {from: positional, index: 0}
|
|
122
|
+
methods: {from: match_suffix}
|
|
@@ -37,13 +37,16 @@ opt-in under the same flag that already forces a clean analysis rebuild.
|
|
|
37
37
|
|
|
38
38
|
Nodes are MERGE-upserted, never blindly deleted, so a declaration another
|
|
39
39
|
(unchanged) module still references survives and its incoming edges stay valid.
|
|
40
|
-
``:PyExternal`` / ``:PyPackage`` / ``:PyDecorator``
|
|
40
|
+
``:PyExternal`` / ``:PyPackage`` / ``:PyDecorator`` have no owning module and are
|
|
41
41
|
MERGE-only.
|
|
42
42
|
|
|
43
|
-
Every
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
**Every destructive statement is scoped on the ``can://`` id prefix** (#173). The id is a
|
|
44
|
+
path — ``can://python/<app>/<file>/...`` — so ``id = <module-id> OR id STARTS WITH
|
|
45
|
+
<module-id> + '/'`` is containment, and it is one language, one application and one
|
|
46
|
+
module at once. That is what neither a label anchor nor the retired ``_module`` property
|
|
47
|
+
could give: two python applications sharing ``src/foo.py`` carry identical labels and an
|
|
48
|
+
identical file key, and only the id tells them apart. ``:PyCanNode`` anchors the predicate
|
|
49
|
+
so it seeks an index instead of scanning the store; it carries no safety claim.
|
|
47
50
|
|
|
48
51
|
The ``neo4j`` driver is imported lazily so it stays an optional dependency and
|
|
49
52
|
off the default (json) output path entirely.
|
|
@@ -53,16 +56,34 @@ from __future__ import annotations
|
|
|
53
56
|
from dataclasses import dataclass
|
|
54
57
|
from typing import Dict, List, Optional
|
|
55
58
|
|
|
56
|
-
from codeanalyzer.neo4j.rows import
|
|
57
|
-
|
|
59
|
+
from codeanalyzer.neo4j.rows import (
|
|
60
|
+
CAN_NODE, EdgeRow, GraphRows, NodeRow, application_prefix, chunk, descendant_prefix,
|
|
61
|
+
)
|
|
62
|
+
from codeanalyzer.neo4j.schema import CONSTRAINTS, INDEXES
|
|
58
63
|
from codeanalyzer.utils import logger
|
|
59
64
|
|
|
60
|
-
DESCENDANTS = (
|
|
61
|
-
"[:PY_DECLARES|PY_HAS_METHOD|PY_HAS_ATTRIBUTE|PY_DECLARES_VAR"
|
|
62
|
-
"|PY_HAS_CALLSITE|PY_HAS_BODY_NODE*1..]"
|
|
63
|
-
)
|
|
64
65
|
BATCH = 1000
|
|
65
66
|
|
|
67
|
+
# The per-module purge (#173): the module by equality, its subtree by prefix. Anchored
|
|
68
|
+
# on :PyCanNode only so the predicate can seek (see ``rows.CAN_NODE``).
|
|
69
|
+
PURGE_MODULE_EDGES = (
|
|
70
|
+
f"MATCH (x:{CAN_NODE}) WHERE x.id = $mid OR x.id STARTS WITH $pre "
|
|
71
|
+
"MATCH (x)-[r]->() DELETE r"
|
|
72
|
+
)
|
|
73
|
+
PURGE_VANISHED_NODES = (
|
|
74
|
+
f"MATCH (x:{CAN_NODE}) WHERE (x.id = $mid OR x.id STARTS WITH $pre) "
|
|
75
|
+
"AND NOT x.id IN $keys DETACH DELETE x"
|
|
76
|
+
)
|
|
77
|
+
# The orphan prune: modules inside this application's prefix that the run no longer
|
|
78
|
+
# emits, and everything under each. Batched — deleting a large application in one
|
|
79
|
+
# transaction exhausts dbms.memory.transaction.total.max (typescript#116).
|
|
80
|
+
PRUNE_VANISHED_MODULES = (
|
|
81
|
+
f"MATCH (m:PyModule:{CAN_NODE}) WHERE m.id STARTS WITH $app AND NOT m.id IN $present "
|
|
82
|
+
f"CALL {{ WITH m MATCH (x:{CAN_NODE}) WHERE x.id = m.id OR x.id STARTS WITH m.id + '/' "
|
|
83
|
+
"DETACH DELETE x } IN TRANSACTIONS OF 1000 ROWS "
|
|
84
|
+
"RETURN count(*) AS pruned"
|
|
85
|
+
)
|
|
86
|
+
|
|
66
87
|
|
|
67
88
|
@dataclass
|
|
68
89
|
class BoltConfig:
|
|
@@ -93,35 +114,44 @@ def bolt_writer(rows: GraphRows, cfg: BoltConfig, full_run: bool, eager: bool =
|
|
|
93
114
|
for stmt in [*CONSTRAINTS, *INDEXES]:
|
|
94
115
|
s.run(stmt)
|
|
95
116
|
|
|
96
|
-
# The application anchor
|
|
97
|
-
#
|
|
117
|
+
# The application anchor. Every destructive statement below is scoped to
|
|
118
|
+
# ``can://python/<app>/``; an empty application id is refused up front rather
|
|
119
|
+
# than becoming ``STARTS WITH ''`` (every node in the database).
|
|
98
120
|
app_name = next(
|
|
99
121
|
(n.value for n in rows.nodes if n.labels and n.labels[0] == "PyApplication"),
|
|
100
122
|
None,
|
|
101
123
|
)
|
|
124
|
+
app_prefix = application_prefix(app_name)
|
|
102
125
|
|
|
103
|
-
# Partition nodes by owning module
|
|
126
|
+
# Partition nodes by owning module (an in-memory field, never emitted, #173);
|
|
127
|
+
# shared nodes have none.
|
|
104
128
|
by_module: Dict[str, List[NodeRow]] = {}
|
|
105
129
|
shared: List[NodeRow] = []
|
|
106
130
|
module_of: Dict[str, str] = {} # node value → owning module
|
|
107
131
|
for n in rows.nodes:
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
module_of[n.value] = m
|
|
132
|
+
if n.module is not None:
|
|
133
|
+
by_module.setdefault(n.module, []).append(n)
|
|
134
|
+
module_of[n.value] = n.module
|
|
112
135
|
else:
|
|
113
136
|
shared.append(n)
|
|
114
137
|
|
|
115
|
-
# 2. diff content_hash.
|
|
138
|
+
# 2. diff content_hash, keyed by module id inside this application's prefix.
|
|
139
|
+
# Keyed by file key it was application-blind: a second application whose
|
|
140
|
+
# module shares the path and the hash looked "unchanged" and was never written.
|
|
116
141
|
db_hash: Dict[str, Optional[str]] = {}
|
|
117
142
|
with session() as s:
|
|
118
|
-
res = s.run(
|
|
143
|
+
res = s.run(
|
|
144
|
+
f"MATCH (m:PyModule:{CAN_NODE}) WHERE m.id STARTS WITH $app "
|
|
145
|
+
"RETURN m.id AS k, m.content_hash AS h",
|
|
146
|
+
app=app_prefix,
|
|
147
|
+
)
|
|
119
148
|
for rec in res:
|
|
120
149
|
db_hash[rec["k"]] = rec["h"]
|
|
121
150
|
changed = set()
|
|
122
151
|
for m, nodes in by_module.items():
|
|
123
|
-
|
|
124
|
-
|
|
152
|
+
mid = _module_id_of(nodes)
|
|
153
|
+
row_hash = _hash_of(nodes)
|
|
154
|
+
if mid not in db_hash or row_hash is None or row_hash != db_hash.get(mid):
|
|
125
155
|
changed.add(m)
|
|
126
156
|
logger.info(
|
|
127
157
|
f"neo4j(bolt): {len(by_module)} modules ({len(changed)} changed), "
|
|
@@ -139,23 +169,19 @@ def bolt_writer(rows: GraphRows, cfg: BoltConfig, full_run: bool, eager: bool =
|
|
|
139
169
|
if not eager:
|
|
140
170
|
_upsert_nodes(session, neo4j, nodes)
|
|
141
171
|
continue
|
|
172
|
+
# The module id comes from the module's own row, never by splitting a
|
|
173
|
+
# declaration's id: a file key may itself contain '/'.
|
|
174
|
+
module_id = _module_id_of(nodes)
|
|
175
|
+
if module_id is None or not module_id.startswith(app_prefix):
|
|
176
|
+
raise ValueError(
|
|
177
|
+
f"neo4j: module {m!r} has no can:// id under {app_prefix!r}; "
|
|
178
|
+
"refusing to purge"
|
|
179
|
+
)
|
|
142
180
|
with session() as s:
|
|
143
|
-
def _purge(tx,
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
tx.run(
|
|
148
|
-
f"MATCH (x:{MODULE_OWNED_PATTERN}) WHERE x._module = $m "
|
|
149
|
-
"MATCH (x)-[r]->() DELETE r",
|
|
150
|
-
m=module,
|
|
151
|
-
)
|
|
152
|
-
tx.run(
|
|
153
|
-
f"MATCH (x:{MODULE_OWNED_PATTERN}) WHERE x._module = $m "
|
|
154
|
-
"AND NOT coalesce(x.signature, x.id, x.file_key) IN $keys "
|
|
155
|
-
"DETACH DELETE x",
|
|
156
|
-
m=module,
|
|
157
|
-
keys=node_keys,
|
|
158
|
-
)
|
|
181
|
+
def _purge(tx, mid=module_id, node_keys=keys):
|
|
182
|
+
params = {"mid": mid, "pre": descendant_prefix(mid)}
|
|
183
|
+
tx.run(PURGE_MODULE_EDGES, **params)
|
|
184
|
+
tx.run(PURGE_VANISHED_NODES, keys=node_keys, **params)
|
|
159
185
|
|
|
160
186
|
s.execute_write(_purge)
|
|
161
187
|
_upsert_nodes(session, neo4j, nodes)
|
|
@@ -169,19 +195,13 @@ def bolt_writer(rows: GraphRows, cfg: BoltConfig, full_run: bool, eager: bool =
|
|
|
169
195
|
_upsert_edges(session, neo4j, edges)
|
|
170
196
|
|
|
171
197
|
# 6. orphan prune — only safe on a full run (a targeted run can't tell deleted from untargeted).
|
|
172
|
-
#
|
|
173
|
-
#
|
|
174
|
-
|
|
175
|
-
|
|
198
|
+
# Scoped to ``can://python/<app>/`` so a full run for application B never deletes
|
|
199
|
+
# application A's modules from a shared database — even when both are python and
|
|
200
|
+
# share a module path.
|
|
201
|
+
if full_run and eager:
|
|
202
|
+
present = [mid for mid in (_module_id_of(ns) for ns in by_module.values()) if mid]
|
|
176
203
|
with session() as s:
|
|
177
|
-
res = s.run(
|
|
178
|
-
"MATCH (:PyApplication {name: $app})-[:PY_HAS_MODULE]->(m:PyModule) "
|
|
179
|
-
"WHERE NOT m.file_key IN $present "
|
|
180
|
-
f"OPTIONAL MATCH (m)-{DESCENDANTS}->(x) DETACH DELETE x, m "
|
|
181
|
-
"RETURN count(m) AS pruned",
|
|
182
|
-
app=app_name,
|
|
183
|
-
present=present,
|
|
184
|
-
)
|
|
204
|
+
res = s.run(PRUNE_VANISHED_MODULES, app=app_prefix, present=present)
|
|
185
205
|
pruned = res.single()
|
|
186
206
|
pruned_count = pruned["pruned"] if pruned else 0
|
|
187
207
|
logger.info(f"neo4j(bolt): pruned {pruned_count} vanished module(s)")
|
|
@@ -263,12 +283,19 @@ def _upsert_edges(session, neo4j, edges: List[EdgeRow]) -> None:
|
|
|
263
283
|
# ----------------------------------------------------------------------------------------------
|
|
264
284
|
|
|
265
285
|
|
|
266
|
-
def
|
|
267
|
-
for n in nodes
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
286
|
+
def _module_row(nodes: List[NodeRow]) -> Optional[NodeRow]:
|
|
287
|
+
return next((n for n in nodes if n.labels[0] == "PyModule"), None)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _module_id_of(nodes: List[NodeRow]) -> Optional[str]:
|
|
291
|
+
row = _module_row(nodes)
|
|
292
|
+
return row.value if row is not None else None
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def _hash_of(nodes: List[NodeRow]) -> Optional[str]:
|
|
296
|
+
row = _module_row(nodes)
|
|
297
|
+
h = row.props.get("content_hash") if row is not None else None
|
|
298
|
+
return h if isinstance(h, str) else None
|
|
272
299
|
|
|
273
300
|
|
|
274
301
|
def _to_params(props, neo4j) -> dict:
|
|
@@ -28,9 +28,11 @@ from __future__ import annotations
|
|
|
28
28
|
from typing import Dict, List
|
|
29
29
|
|
|
30
30
|
from codeanalyzer.neo4j.rows import (
|
|
31
|
+
CAN_NODE,
|
|
31
32
|
EdgeRow,
|
|
32
33
|
GraphRows,
|
|
33
34
|
NodeRow,
|
|
35
|
+
application_prefix,
|
|
34
36
|
chunk,
|
|
35
37
|
cypher_map,
|
|
36
38
|
cypher_value,
|
|
@@ -66,13 +68,17 @@ def render_cypher(rows: GraphRows, app_name: str) -> str:
|
|
|
66
68
|
|
|
67
69
|
|
|
68
70
|
def _wipe(app_name: str) -> str:
|
|
71
|
+
"""Everything under ``can://python/<app>/`` plus the application anchor (#173).
|
|
72
|
+
Scoped by id prefix, so it is one language and one application by construction —
|
|
73
|
+
a second python app sharing a module path, a sibling analyzer's graph, and the
|
|
74
|
+
cross-language :Artifact / :Package nodes are all outside it."""
|
|
75
|
+
prefix = cypher_value(application_prefix(app_name))
|
|
69
76
|
name = cypher_value(app_name)
|
|
70
77
|
return "\n".join(
|
|
71
78
|
[
|
|
72
|
-
f"MATCH (
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"DETACH DELETE x, m, a;",
|
|
79
|
+
f"MATCH (x:{CAN_NODE}) WHERE x.id STARTS WITH {prefix}",
|
|
80
|
+
"CALL { WITH x DETACH DELETE x } IN TRANSACTIONS OF 1000 ROWS;",
|
|
81
|
+
f"MATCH (a:PyApplication {{name: {name}}}) DETACH DELETE a;",
|
|
76
82
|
]
|
|
77
83
|
)
|
|
78
84
|
|