Skip to content

Commit 7eeb8a8

Browse files
committed
Fixed --mapping issue #304
* Added `--mapping` option for `inventory` * Fixed the mapping issue * Added test code * Update USAGE doc Signed-off-by: Chin Yeung Li <[email protected]>
1 parent b0cf212 commit 7eeb8a8

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

USAGE.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ inventory
245245
::
246246

247247
-f, --format [json|csv] Set OUTPUT file format. [default: csv]
248+
--mapping Use file mapping.config to collect the defined not supported fields in ABOUT files.
248249
--show-all Show all the errors and warning
249250
--validate-about-resource Validate the existence of the about resource.
250251
-q, --quiet Do not print any error/warning.
@@ -263,6 +264,10 @@ Options
263264
264265
Set OUTPUT file format. [default: csv]
265266

267+
--mapping
268+
269+
See mapping.config for details
270+
266271
--show-all
267272

268273
This option ask the tool to show all kind of errors found.

src/attributecode/cmd.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ def cli():
112112
type=click.Choice(['json', 'csv']),
113113
help='Set OUTPUT inventory file format.')
114114

115+
@click.option('--mapping', is_flag=True,
116+
help='Use file mapping.config to collect the defined not supported fields in ABOUT files.')
117+
115118
@click.option('--show-all', is_flag=True, default=False,
116119
help='Show all errors and warnings. '
117120
'By default, the tool only prints these '
@@ -129,7 +132,7 @@ def cli():
129132

130133
@click.help_option('-h', '--help')
131134

132-
def inventory(location, output, quiet, format, show_all, validate_about_resource):
135+
def inventory(location, output, mapping, quiet, format, show_all, validate_about_resource):
133136
"""
134137
Collect a JSON or CSV inventory of components from .ABOUT files.
135138
@@ -152,7 +155,7 @@ def inventory(location, output, quiet, format, show_all, validate_about_resource
152155
# accept zipped ABOUT files as input
153156
location = extract_zip(location)
154157

155-
errors, abouts = model.collect_inventory(location)
158+
errors, abouts = model.collect_inventory(location, use_mapping=mapping)
156159

157160
write_errors = model.write_output(abouts, output, format)
158161
for err in write_errors:
@@ -317,7 +320,7 @@ def attrib(location, output, template, mapping, inventory, quiet, show_all,
317320
if location.lower().endswith('.zip'):
318321
location = extract_zip(location)
319322

320-
inv_errors, abouts = model.collect_inventory(location)
323+
inv_errors, abouts = model.collect_inventory(location, use_mapping=mapping)
321324
no_match_errors = attrib_generate_and_save(
322325
abouts=abouts, output_location=output,
323326
use_mapping=mapping, template_loc=template,
@@ -327,7 +330,7 @@ def attrib(location, output, template, mapping, inventory, quiet, show_all,
327330
# Check for template error
328331
with open(output, 'r') as output_file:
329332
first_line = output_file.readline()
330-
if first_line.startswith('Template processing error'):
333+
if first_line.startswith('Template'):
331334
click.echo(first_line)
332335
sys.exit(errno.ENOEXEC)
333336

src/attributecode/model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ def create_fields(self):
710710
field.name = name
711711
setattr(self, name, field)
712712

713-
def __init__(self, location=None, about_file_path=None):
713+
def __init__(self, location=None, about_file_path=None, use_mapping=False):
714714
self.create_fields()
715715
self.custom_fields = OrderedDict()
716716

@@ -724,7 +724,7 @@ def __init__(self, location=None, about_file_path=None):
724724
self.base_dir = None
725725
if self.location:
726726
self.base_dir = os.path.dirname(location)
727-
self.load(location)
727+
self.load(location, use_mapping)
728728

729729
def __repr__(self):
730730
return repr(self.all_fields())
@@ -965,7 +965,7 @@ def process(self, fields, about_file_path, running_inventory=False,
965965
# self.about_resource.resolve(self.about_file_path)
966966
return errors
967967

968-
def load(self, location):
968+
def load(self, location, use_mapping=False):
969969
"""
970970
Read, parse and process the ABOUT file at location.
971971
Return a list of errors and update self with errors.
@@ -988,7 +988,7 @@ def load(self, location):
988988
and then join with the 'about_resource_path'
989989
"""
990990
running_inventory = True
991-
errs = self.load_dict(saneyaml.load(input_text), base_dir, running_inventory)
991+
errs = self.load_dict(saneyaml.load(input_text), base_dir, running_inventory, use_mapping)
992992
errors.extend(errs)
993993
except Exception as e:
994994
msg = 'Cannot load invalid ABOUT file: %(location)r: %(e)r\n' + str(e)
@@ -1190,7 +1190,7 @@ def parse(lines):
11901190
return errors, fields
11911191

11921192

1193-
def collect_inventory(location):
1193+
def collect_inventory(location, use_mapping=False):
11941194
"""
11951195
Collect ABOUT files at location and return a list of errors and a list of
11961196
About objects.
@@ -1205,7 +1205,7 @@ def collect_inventory(location):
12051205
abouts = []
12061206
for about_loc in about_locations:
12071207
about_file_path = util.get_relative_path(input_location, about_loc)
1208-
about = About(about_loc, about_file_path)
1208+
about = About(about_loc, about_file_path, use_mapping)
12091209
# Insert about_file_path reference to the error
12101210
for severity, message in about.errors:
12111211
msg = (about_file_path + ": " + message)

tests/test_model.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,23 @@ def test_collect_inventory_with_license_expression(self):
11601160
returned_lic = abouts[0].license_expression.value
11611161
assert expected_lic == returned_lic
11621162

1163+
def test_collect_inventory_with_mapping(self):
1164+
test_loc = get_test_loc('parse/name_mapping_test.ABOUT')
1165+
mapping = True
1166+
errors, abouts = model.collect_inventory(test_loc, mapping)
1167+
expected_msg = 'Field resource is a custom field'
1168+
assert len(errors) == 1
1169+
assert expected_msg in errors[0].message
1170+
# The not supported 'resource' value is collected
1171+
assert abouts[0].resource.value
1172+
1173+
def test_collect_inventory_without_mapping(self):
1174+
test_loc = get_test_loc('parse/name_mapping_test.ABOUT')
1175+
errors, abouts = model.collect_inventory(test_loc)
1176+
expected_msg = 'Field resource is not a supported field and is ignored.'
1177+
assert len(errors) == 1
1178+
assert expected_msg in errors[0].message
1179+
11631180
def test_parse_license_expression(self):
11641181
spec_char, returned_lic = model.parse_license_expression(u'mit or apache-2.0')
11651182
expected_lic = [u'mit', u'apache-2.0']
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
about_resource: .
2+
name: test
3+
resource: .

0 commit comments

Comments
 (0)