88from dataclasses import fields
99from pathlib import Path
1010from sys import exit as sysexit
11- from sys import stdout
11+ from sys import stdin , stdout
1212
1313from fhconfparser import FHConfParser , SimpleConf
1414
@@ -30,17 +30,23 @@ def cli() -> None: # pragma: no cover
3030 "-f" ,
3131 help = f"Output format. one of: { ', ' .join (list (formatter .formatMap ))} . default=simple" ,
3232 )
33+ parser .add_argument (
34+ "--requirements-paths" ,
35+ "-r" ,
36+ help = "Filenames to read from (omit for stdin)" ,
37+ nargs = "+" ,
38+ )
39+ parser .add_argument (
40+ "--groups" ,
41+ "-g" ,
42+ help = "Select groups/extras from supported files" ,
43+ nargs = "+" ,
44+ )
3345 parser .add_argument (
3446 "--file" ,
3547 "-o" ,
3648 help = "Filename to write to (omit for stdout)" ,
3749 )
38- parser .add_argument (
39- "--using" ,
40- "-u" ,
41- help = "Environment to use e.g. requirements.txt. one of: "
42- f"{ ', ' .join (get_deps .USINGS )} . default=poetry" ,
43- )
4450 parser .add_argument (
4551 "--ignore-packages" ,
4652 help = "a list of packages to ignore (compat=True)" ,
@@ -83,7 +89,13 @@ def cli() -> None: # pragma: no cover
8389 action = "store_true" ,
8490 )
8591 args = vars (parser .parse_args ())
86- sysexit (main (args ))
92+ stdin_path = Path ("__stdin__" )
93+ if stdin :
94+ stdin_path .write_text ("\n " .join (stdin .readlines ()), "utf-8" )
95+ ec = main (args )
96+ stdin_path .unlink (missing_ok = True )
97+
98+ sysexit (ec )
8799
88100
89101def main (args : dict ) -> int :
@@ -110,30 +122,33 @@ def main(args: dict) -> int:
110122 simpleConf = SimpleConf (configparser , "licensecheck" , args )
111123
112124 # File
113- textIO = (
125+ requirements_paths = simpleConf .get ("requirements_paths" ) or ["__stdin__" ]
126+ output_file = (
114127 stdout
115128 if simpleConf .get ("file" ) is None
116129 else Path (simpleConf .get ("file" )).open ("w" , encoding = "utf-8" )
117130 )
118131
119132 # Get my license
120- myLiceTxt = args ["license" ] if args .get ("license" ) else packageinfo .getMyPackageLicense ()
121- myLice = license_matrix .licenseType (myLiceTxt )[0 ]
122-
123- # Get list of licenses
124- depsWithLicenses = get_deps .getDepsWithLicenses (
125- simpleConf .get ("using" , "poetry" ),
126- myLice ,
127- list (map (types .ucstr , simpleConf .get ("ignore_packages" , []))),
128- list (map (types .ucstr , simpleConf .get ("fail_packages" , []))),
129- list (map (types .ucstr , simpleConf .get ("ignore_licenses" , []))),
130- list (map (types .ucstr , simpleConf .get ("fail_licenses" , []))),
131- list (map (types .ucstr , simpleConf .get ("only_licenses" , []))),
132- list (map (types .ucstr , simpleConf .get ("skip_dependencies" , []))),
133+ this_license_text = (
134+ args ["license" ] if args .get ("license" ) else packageinfo .getMyPackageLicense ()
135+ )
136+ this_license = license_matrix .licenseType (this_license_text )[0 ]
137+
138+ def getFromConfig (key : str ) -> list [types .ucstr ]:
139+ return list (map (types .ucstr , simpleConf .get (key , [])))
140+
141+ incompatible , depsWithLicenses = get_deps .check (
142+ requirements_paths = requirements_paths ,
143+ groups = simpleConf .get ("groups" , []),
144+ this_license = this_license ,
145+ ignore_packages = getFromConfig ("ignore_packages" ),
146+ fail_packages = getFromConfig ("fail_packages" ),
147+ ignore_licenses = getFromConfig ("ignore_licenses" ),
148+ fail_licenses = getFromConfig ("fail_licenses" ),
149+ only_licenses = getFromConfig ("only_licenses" ),
150+ skip_dependencies = getFromConfig ("skip_dependencies" ),
133151 )
134-
135- # Are any licenses incompatible?
136- incompatible = any (not lice .licenseCompat for lice in depsWithLicenses )
137152
138153 # Format the results
139154 hide_output_parameters = [types .ucstr (x ) for x in simpleConf .get ("hide_output_parameters" , [])]
@@ -147,11 +162,11 @@ def main(args: dict) -> int:
147162 if simpleConf .get ("format" , "simple" ) in formatter .formatMap :
148163 print (
149164 formatter .formatMap [simpleConf .get ("format" , "simple" )](
150- myLice ,
165+ this_license ,
151166 sorted (depsWithLicenses ),
152167 hide_output_parameters ,
153168 ),
154- file = textIO ,
169+ file = output_file ,
155170 )
156171 else :
157172 exitCode = 2
@@ -162,5 +177,5 @@ def main(args: dict) -> int:
162177
163178 # Cleanup + exit
164179 if simpleConf .get ("file" ) is not None :
165- textIO .close ()
180+ output_file .close ()
166181 return exitCode
0 commit comments