Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
45 lines (35 loc) · 1.34 KB
/
Copy pathcli.py
File metadata and controls
45 lines (35 loc) · 1.34 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
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from coretrace_python.frontend.imports import ImportResolutionError
from coretrace_python.frontend.lowering import LoweringError, lower_module
from coretrace_python.frontend.parser import ParseError, parse_file
from coretrace_python.ir.printer import format_module
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",
)
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:
tree = parse_file(args.path)
module = lower_module(tree, filename=str(args.path))
except (OSError, ParseError, ImportResolutionError, LoweringError) as error:
print(f"error: {error}", file=sys.stderr)
return 1
print(format_module(module))
return 0
if __name__ == "__main__":
raise SystemExit(main())