22
33import argparse
44import sys
5+ import zipfile
56from pathlib import Path
67
78from coretrace_python import __version__ , engine
89from coretrace_python .analysis import AnalysisError
910from coretrace_python .cache import ProjectCache
1011from coretrace_python .cfg import CFGError
11- from coretrace_python .dependency import render_sbom
12+ from coretrace_python .dependency import dump_advisories , import_osv , read_osv , render_sbom
1213from coretrace_python .frontend import HIRBuildError , ParseError , build_hir
1314from coretrace_python .ir .lowering import LoweringError , lower_module
1415from coretrace_python .ir .printer import format_module
@@ -42,7 +43,9 @@ def build_parser() -> argparse.ArgumentParser:
4243 prog = engine .TOOL_NAME ,
4344 description = "Analyze Python source with CoreTrace." ,
4445 )
45- parser .add_argument ("path" , type = Path , help = "Python source file, or a directory for --check" )
46+ parser .add_argument (
47+ "path" , type = Path , nargs = "?" , help = "Python source file, or a directory for --check"
48+ )
4649 parser .add_argument (
4750 "--emit-ir" ,
4851 action = "store_true" ,
@@ -94,14 +97,51 @@ def build_parser() -> argparse.ArgumentParser:
9497 metavar = "PATH" ,
9598 help = "with --check on a directory, write a CycloneDX bill of materials to PATH" ,
9699 )
100+ parser .add_argument (
101+ "--advisories" ,
102+ action = "append" ,
103+ type = Path ,
104+ default = [],
105+ metavar = "FILE" ,
106+ help = "with --check on a directory, a local advisory file to read in addition to "
107+ "advisories.json at the root (repeatable)" ,
108+ )
109+ parser .add_argument (
110+ "--policy" ,
111+ type = Path ,
112+ default = None ,
113+ metavar = "FILE" ,
114+ help = "with --check on a directory, the dependency policy to apply instead of "
115+ "coretrace-policy.toml at the root" ,
116+ )
117+ parser .add_argument (
118+ "--import-advisories" ,
119+ nargs = 2 ,
120+ type = Path ,
121+ default = None ,
122+ metavar = ("SRC" , "OUT" ),
123+ help = "convert an OSV dump (a JSON file, a directory of them or a zip archive) "
124+ "into the local advisory file OUT, then exit" ,
125+ )
97126 return parser
98127
99128
100129def main (argv : list [str ] | None = None ) -> int :
101130 args = build_parser ().parse_args (argv )
131+ if args .import_advisories is not None :
132+ source , out = args .import_advisories
133+ try :
134+ out .write_text (dump_advisories (import_osv (read_osv (source ))), encoding = "utf-8" )
135+ except (OSError , ValueError , zipfile .BadZipFile ) as error :
136+ print (f"error: { error } " , file = sys .stderr )
137+ return EXIT_ERROR
138+ return EXIT_CLEAN
102139 if not (args .emit_ir or args .check ):
103140 print ("error: no action selected; pass --emit-ir or --check" , file = sys .stderr )
104141 return EXIT_ERROR
142+ if args .path is None :
143+ print ("error: a path is required" , file = sys .stderr )
144+ return EXIT_ERROR
105145 if args .format is not None and not args .check :
106146 print ("error: --format only applies to --check" , file = sys .stderr )
107147 return EXIT_ERROR
@@ -117,6 +157,12 @@ def main(argv: list[str] | None = None) -> int:
117157 if args .sbom is not None and not (args .check and args .path .is_dir ()):
118158 print ("error: --sbom only applies to --check on a directory" , file = sys .stderr )
119159 return EXIT_ERROR
160+ if args .advisories and not (args .check and args .path .is_dir ()):
161+ print ("error: --advisories only applies to --check on a directory" , file = sys .stderr )
162+ return EXIT_ERROR
163+ if args .policy is not None and not (args .check and args .path .is_dir ()):
164+ print ("error: --policy only applies to --check on a directory" , file = sys .stderr )
165+ return EXIT_ERROR
120166
121167 if args .path .is_dir () and args .emit_ir :
122168 print (f"error: { args .path } is a directory; --emit-ir needs a file" , file = sys .stderr )
@@ -130,7 +176,12 @@ def main(argv: list[str] | None = None) -> int:
130176 if args .path .is_dir ():
131177 cache = None if args .cache is None else ProjectCache (args .cache )
132178 analysis = engine .analyze_project (
133- args .path , args .plugins , cache = cache , jobs = args .jobs or 1
179+ args .path ,
180+ args .plugins ,
181+ cache = cache ,
182+ jobs = args .jobs or 1 ,
183+ advisory_files = args .advisories ,
184+ policy_file = args .policy ,
134185 )
135186 findings = analysis .findings
136187 if args .sbom is not None :
0 commit comments