3737from attributecode .util import extract_zip
3838from attributecode .util import update_severity_level_about_resource_path_not_exist_error
3939from attributecode .util import to_posix
40+ from attributecode .util import inventory_filter
4041
4142
4243__copyright__ = """
@@ -90,7 +91,7 @@ def cli():
9091
9192Read, write and collect provenance and license inventories from .ABOUT files to and from JSON or CSV files.
9293
93- Use about-code <command> --help for help on a command.
94+ Use about <command> --help for help on a command.
9495 """
9596
9697
@@ -108,6 +109,9 @@ def cli():
108109@click .argument ('output' , nargs = 1 , required = True ,
109110 type = click .Path (exists = False , dir_okay = False , resolve_path = True ))
110111
112+ @click .option ('--filter' , nargs = 1 , multiple = True ,
113+ help = 'Filter for the output inventory.' )
114+
111115@click .option ('-f' , '--format' , is_flag = False , default = 'csv' , show_default = True ,
112116 type = click .Choice (['json' , 'csv' ]),
113117 help = 'Set OUTPUT inventory file format.' )
@@ -119,6 +123,10 @@ def cli():
119123 type = click .Path (exists = True , dir_okay = True , readable = True , resolve_path = True ),
120124 help = 'Use a custom mapping file with mapping between input keys and ABOUT field names.' )
121125
126+ @click .option ('--mapping-output' , metavar = 'FILE' , nargs = 1 ,
127+ type = click .Path (exists = True , dir_okay = True , readable = True , resolve_path = True ),
128+ help = 'Use a custom mapping file with mapping between ABOUT field names and output keys' )
129+
122130@click .option ('--verbose' , is_flag = True , default = False ,
123131 help = 'Show all errors and warnings. '
124132 'By default, the tool only prints these '
@@ -132,7 +140,7 @@ def cli():
132140
133141@click .help_option ('-h' , '--help' )
134142
135- def inventory (location , output , mapping , mapping_file , quiet , format , verbose ):
143+ def inventory (location , output , mapping , mapping_file , mapping_output , filter , quiet , format , verbose ):
136144 """
137145Collect a JSON or CSV inventory of components from .ABOUT files.
138146
@@ -157,6 +165,22 @@ def inventory(location, output, mapping, mapping_file, quiet, format, verbose):
157165
158166 errors , abouts = model .collect_inventory (location , use_mapping = mapping , mapping_file = mapping_file )
159167
168+ updated_abouts = []
169+ if filter :
170+ filter_dict = {}
171+ # Parse the filter and save to the filter dictionary with a list of value
172+ for element in filter :
173+ key = element .partition ('=' )[0 ]
174+ value = element .partition ('=' )[2 ]
175+ if key in filter_dict :
176+ filter_dict [key ].append (value )
177+ else :
178+ value_list = [value ]
179+ filter_dict [key ] = value_list
180+ updated_abouts = inventory_filter (abouts , filter_dict )
181+ else :
182+ updated_abouts = abouts
183+
160184 # Do not write the output if one of the ABOUT files has duplicated key names
161185 dup_error_msg = u'Duplicated key name(s)'
162186 halt_output = False
@@ -166,7 +190,7 @@ def inventory(location, output, mapping, mapping_file, quiet, format, verbose):
166190 break
167191
168192 if not halt_output :
169- write_errors = model .write_output (abouts , output , format )
193+ write_errors = model .write_output (updated_abouts , output , format , mapping_output )
170194 for err in write_errors :
171195 errors .append (err )
172196 else :
@@ -290,6 +314,12 @@ def gen(location, output, mapping, mapping_file, license_notice_text_location, f
290314 type = click .Path (exists = True , dir_okay = True , readable = True , resolve_path = True ),
291315 help = 'Use a custom mapping file with mapping between input keys and ABOUT field names.' )
292316
317+ @click .option ('--template' , type = click .Path (exists = True ), nargs = 1 ,
318+ help = 'Path to an optional custom attribution template used for generation.' )
319+
320+ @click .option ('--vartext' , nargs = 1 , multiple = True ,
321+ help = 'Variable texts to the attribution template.' )
322+
293323@click .option ('--verbose' , is_flag = True , default = False ,
294324 help = 'Show all errors and warnings. '
295325 'By default, the tool only prints these '
@@ -298,15 +328,12 @@ def gen(location, output, mapping, mapping_file, license_notice_text_location, f
298328 'for any level.'
299329)
300330
301- @click .option ('--template' , type = click .Path (exists = True ), nargs = 1 ,
302- help = 'Path to an optional custom attribution template used for generation.' )
303-
304331@click .option ('-q' , '--quiet' , is_flag = True ,
305332 help = 'Do not print error or warning messages.' )
306333
307334@click .help_option ('-h' , '--help' )
308335
309- def attrib (location , output , template , mapping , mapping_file , inventory , quiet , verbose ):
336+ def attrib (location , output , template , mapping , mapping_file , inventory , vartext , quiet , verbose ):
310337 """
311338Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.
312339
@@ -325,7 +352,7 @@ def attrib(location, output, template, mapping, mapping_file, inventory, quiet,
325352 no_match_errors = attrib_generate_and_save (
326353 abouts = abouts , output_location = output ,
327354 use_mapping = mapping , mapping_file = mapping_file , template_loc = template ,
328- inventory_location = inventory )
355+ inventory_location = inventory , vartext = vartext )
329356
330357 if not no_match_errors :
331358 # Check for template error
@@ -378,21 +405,24 @@ def check(location, verbose):
378405
379406 msg_format = '%(sever)s: %(message)s'
380407 print_errors = []
408+ number_of_errors = 0
381409 for severity , message in errors :
382410 sever = severities [severity ]
411+ # Only problematic_errors should be counted.
412+ # Others such as INFO should not be counted as error.
413+ if sever in problematic_errors :
414+ number_of_errors = number_of_errors + 1
383415 if verbose :
384416 print_errors .append (msg_format % locals ())
385417 elif sever in problematic_errors :
386418 print_errors .append (msg_format % locals ())
387419
388- number_of_errors = len (print_errors )
389-
390420 for err in print_errors :
391421 print (err )
392422
393423 if print_errors :
394424 click .echo ('Found {} errors.' .format (number_of_errors ))
395- # FIXME: not sure this is the right way to exit with a retrun code
425+ # FIXME: not sure this is the right way to exit with a return code
396426 sys .exit (1 )
397427 else :
398428 click .echo ('No error found.' )
0 commit comments