analysis: publish one schema for every project
This commit is contained in:
1 parent
8bd33a4615
commit
8bfe0c36ee
19 files changed
+93
-500
No files matched your search
@@ -1,17 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Independent Analysis v2 consumer: no Marqdo/Rust imports."""
|
||||
"""Independent Analysis consumer: no Marqdo/Rust imports."""
|
||||
import json
|
||||
import sys
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
raise SystemExit("usage: analysis_consumer.py ANALYSIS.json")
|
||||
def visit(body, loop_span=None):
|
||||
for statement in body:
|
||||
kind = statement["kind"]
|
||||
if kind == "loop":
|
||||
yield from visit(statement["body"], statement["span"])
|
||||
elif kind == "branch":
|
||||
for arm in statement["arms"]:
|
||||
yield from visit(arm["body"], loop_span)
|
||||
elif kind in ("break", "continue"):
|
||||
if loop_span is None:
|
||||
raise SystemExit(f"{kind} outside a projected loop")
|
||||
span = statement["span"]
|
||||
if not 0 <= loop_span["start"] < span["start"] < span["end"]:
|
||||
raise SystemExit(f"invalid {kind} carrier span")
|
||||
yield kind, loop_span["start"]
|
||||
elif kind not in ("assign", "expression", "return"):
|
||||
raise SystemExit(f"unsupported statement kind {kind}")
|
||||
|
||||
|
||||
if sys.argv[2:] not in ([], ["--require-transfers"]) or len(sys.argv) < 2:
|
||||
raise SystemExit("usage: analysis_consumer.py ANALYSIS.json [--require-transfers]")
|
||||
|
||||
with open(sys.argv[1], encoding="utf-8") as handle:
|
||||
analysis = json.load(handle)
|
||||
|
||||
if analysis.get("schema") != "marqdo.analysis":
|
||||
raise SystemExit("unexpected schema")
|
||||
if analysis.get("schema_version") != 2:
|
||||
if analysis.get("schema_version") != 3:
|
||||
raise SystemExit("unsupported schema_version")
|
||||
|
||||
project = analysis["project"]
|
||||
@@ -21,6 +40,7 @@ if not isinstance(project.get("root_id"), str) or not project["root_id"]:
|
||||
raise SystemExit("bad root_id")
|
||||
|
||||
functions = []
|
||||
transfers = []
|
||||
for module in analysis["modules"]:
|
||||
module_id = module["id"]
|
||||
if "::" not in module_id or not module_id.startswith(("root:", "dep:", "std:")):
|
||||
@@ -45,9 +65,13 @@ for module in analysis["modules"]:
|
||||
if not isinstance(span.get("start"), int) or not isinstance(span.get("end"), int):
|
||||
raise SystemExit("bad resource span")
|
||||
for function in module["functions"]:
|
||||
body = json.dumps(function["body"], ensure_ascii=False)
|
||||
functions.append((function["id"], body))
|
||||
transfers.extend(visit(function["body"]))
|
||||
functions.append(function["id"])
|
||||
|
||||
if not functions:
|
||||
raise SystemExit("no functions")
|
||||
print("functions=" + str(len(functions)))
|
||||
|
||||
if len(sys.argv) == 3 and not transfers:
|
||||
raise SystemExit("missing loop transfers")
|
||||
print(f"transfers={len(transfers)}")
|
||||
@@ -1,45 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read loop transfers from public Analysis JSON using only Python."""
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
|
||||
def visit(body, loop_span=None):
|
||||
for statement in body:
|
||||
kind = statement["kind"]
|
||||
if kind == "loop":
|
||||
yield from visit(statement["body"], statement["span"])
|
||||
elif kind == "branch":
|
||||
for arm in statement["arms"]:
|
||||
yield from visit(arm["body"], loop_span)
|
||||
elif kind in ("break", "continue"):
|
||||
if loop_span is None:
|
||||
raise SystemExit(f"{kind} outside a projected loop")
|
||||
span = statement["span"]
|
||||
if not 0 <= loop_span["start"] < span["start"] < span["end"]:
|
||||
raise SystemExit(f"invalid {kind} carrier span")
|
||||
yield kind, loop_span["start"]
|
||||
elif kind not in ("assign", "expression", "return"):
|
||||
raise SystemExit(f"unsupported statement kind {kind}")
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
raise SystemExit("usage: analysis_v3_consumer.py ANALYSIS.json")
|
||||
analysis = json.loads(pathlib.Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||
if analysis.get("schema") != "marqdo.analysis" or analysis.get("schema_version") != 3:
|
||||
raise SystemExit("unsupported Analysis schema version")
|
||||
transfers = [
|
||||
transfer
|
||||
for module in analysis["modules"]
|
||||
for function in module["functions"]
|
||||
for transfer in visit(function["body"])
|
||||
]
|
||||
if not transfers:
|
||||
raise SystemExit("missing loop transfers")
|
||||
print(f"transfers={len(transfers)} nearest_loops={len({target for _, target in transfers})}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -13,7 +13,7 @@ analysis = json.loads(analysis_bytes)
|
||||
with open(sys.argv[2], encoding="utf-8") as handle:
|
||||
trace = json.load(handle)
|
||||
|
||||
if analysis.get("schema") != "marqdo.analysis" or analysis.get("schema_version") not in (2, 3):
|
||||
if analysis.get("schema") != "marqdo.analysis" or analysis.get("schema_version") != 3:
|
||||
raise SystemExit("unsupported analysis")
|
||||
if trace.get("schema") != "marqdo.trace" or trace.get("schema_version") != 2:
|
||||
raise SystemExit("unsupported trace")
|
||||
|
||||
@@ -48,19 +48,14 @@ def main():
|
||||
trace = json.loads(trace_path.read_text(encoding="utf-8"))
|
||||
tool = json.loads(tool_path.read_text(encoding="utf-8"))
|
||||
|
||||
version = analysis.get("schema_version")
|
||||
if version not in (2, 3):
|
||||
raise SystemExit("unsupported Analysis schema version")
|
||||
analysis_schema = f"analysis-v{version}.schema.json"
|
||||
analysis_schema = "analysis.schema.json"
|
||||
require_valid(analysis_schema, analysis, "analysis")
|
||||
if version == 3:
|
||||
require_invalid("analysis-v2.schema.json", analysis, "legacy schema rejects v3")
|
||||
require_valid("trace-v2.schema.json", trace, "trace")
|
||||
require_valid("tool-v1.schema.json", tool, "tool")
|
||||
|
||||
# Negative mutations: structure, version and traversal must fail closed.
|
||||
bad = copy.deepcopy(analysis)
|
||||
bad["schema_version"] = 999
|
||||
bad["schema_version"] = 2
|
||||
require_invalid(analysis_schema, bad, "analysis unsupported version")
|
||||
|
||||
bad = copy.deepcopy(analysis)
|
||||
|
||||
Reference in new issue
Block a user