44import sys
55from pathlib import Path
66
7+ from coretrace_python import engine
8+ from coretrace_python .analysis import AnalysisError
9+ from coretrace_python .cfg import CFGError
710from coretrace_python .frontend import HIRBuildError , ParseError , build_hir
811from coretrace_python .ir .lowering import LoweringError , lower_module
912from coretrace_python .ir .printer import format_module
13+ from coretrace_python .plugins import IncompatiblePluginError , ManifestError
14+ from coretrace_python .reporters import FORMATS , render
1015from coretrace_python .semantic .imports import ImportResolutionError
1116from coretrace_python .semantic .scopes import ScopeError
1217from coretrace_python .source import SourceManager
1318
19+ EXIT_CLEAN = 0
20+ EXIT_FINDINGS = 1
21+ EXIT_ERROR = 2
22+
23+ _ANALYSIS_ERRORS = (
24+ OSError ,
25+ UnicodeError ,
26+ ParseError ,
27+ HIRBuildError ,
28+ ImportResolutionError ,
29+ ScopeError ,
30+ CFGError ,
31+ LoweringError ,
32+ AnalysisError ,
33+ ManifestError ,
34+ IncompatiblePluginError ,
35+ )
36+
1437
1538def build_parser () -> argparse .ArgumentParser :
1639 parser = argparse .ArgumentParser (
17- prog = "coretrace-python-analyzer" ,
40+ prog = engine . TOOL_NAME ,
1841 description = "Analyze Python source with CoreTrace." ,
1942 )
2043 parser .add_argument ("path" , type = Path , help = "Python source file to analyze" )
@@ -28,32 +51,49 @@ def build_parser() -> argparse.ArgumentParser:
2851 action = "store_true" ,
2952 help = "with --emit-ir, print the static single assignment form" ,
3053 )
54+ parser .add_argument (
55+ "--check" ,
56+ action = "store_true" ,
57+ help = "run the loaded plugins and report their findings" ,
58+ )
59+ parser .add_argument (
60+ "--plugins" ,
61+ action = "append" ,
62+ type = Path ,
63+ default = [],
64+ metavar = "DIR" ,
65+ help = "with --check, a directory searched recursively for plugin.toml (repeatable)" ,
66+ )
67+ parser .add_argument (
68+ "--format" ,
69+ choices = sorted (FORMATS ),
70+ default = None ,
71+ help = "with --check, the report format (default: text)" ,
72+ )
3173 return parser
3274
3375
3476def main (argv : list [str ] | None = None ) -> int :
3577 args = build_parser ().parse_args (argv )
36- if not args .emit_ir :
37- print ("error: no action selected; pass --emit-ir" , file = sys .stderr )
38- return 2
78+ if not (args .emit_ir or args .check ):
79+ print ("error: no action selected; pass --emit-ir or --check" , file = sys .stderr )
80+ return EXIT_ERROR
81+ if args .format is not None and not args .check :
82+ print ("error: --format only applies to --check" , file = sys .stderr )
83+ return EXIT_ERROR
3984
4085 try :
4186 source = SourceManager ().load_file (args .path )
42- module = lower_module (build_hir (source ), ssa = args .ssa )
43- except (
44- OSError ,
45- UnicodeError ,
46- ParseError ,
47- HIRBuildError ,
48- ImportResolutionError ,
49- ScopeError ,
50- LoweringError ,
51- ) as error :
87+ if args .emit_ir :
88+ print (format_module (lower_module (build_hir (source ), ssa = args .ssa )))
89+ if args .check :
90+ findings = engine .check (source , args .plugins )
91+ print (render (args .format or "text" , engine .report (findings )), end = "" )
92+ return EXIT_FINDINGS if findings else EXIT_CLEAN
93+ except _ANALYSIS_ERRORS as error :
5294 print (f"error: { error } " , file = sys .stderr )
53- return 1
54-
55- print (format_module (module ))
56- return 0
95+ return EXIT_ERROR
96+ return EXIT_CLEAN
5797
5898
5999if __name__ == "__main__" :
0 commit comments