Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
60 lines (50 loc) · 1.71 KB
/
Copy pathcli.py
File metadata and controls
60 lines (50 loc) · 1.71 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
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from coretrace_python.frontend import HIRBuildError, ParseError, build_hir
from coretrace_python.ir.lowering import LoweringError, lower_module
from coretrace_python.ir.printer import format_module
from coretrace_python.semantic.imports import ImportResolutionError
from coretrace_python.semantic.scopes import ScopeError
from coretrace_python.source import SourceManager
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="coretrace-python-analyzer",
description="Analyze Python source with CoreTrace.",
)
parser.add_argument("path", type=Path, help="Python source file to analyze")
parser.add_argument(
"--emit-ir",
action="store_true",
help="print the lowered Python intermediate representation",
)
parser.add_argument(
"--ssa",
action="store_true",
help="with --emit-ir, print the static single assignment form",
)
return parser
def main(argv: list[str] | None = None) -> int:
args = build_parser().parse_args(argv)
if not args.emit_ir:
print("error: no action selected; pass --emit-ir", file=sys.stderr)
return 2
try:
source = SourceManager().load_file(args.path)
module = lower_module(build_hir(source), ssa=args.ssa)
except (
OSError,
UnicodeError,
ParseError,
HIRBuildError,
ImportResolutionError,
ScopeError,
LoweringError,
) as error:
print(f"error: {error}", file=sys.stderr)
return 1
print(format_module(module))
return 0
if __name__ == "__main__":
raise SystemExit(main())