Skip to content

Commit 0eb8825

Browse files
committed
Fixed #166 Filter for the inventory output
* Add the `--filter` option for the `inventory` Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 8fbeecf commit 0eb8825

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

USAGE.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ inventory
262262

263263
::
264264

265+
--filter TEXT Filter for the output inventory.
265266
-f, --format [json|csv] Set OUTPUT file format. [default: csv]
266267
--mapping Use file mapping.config to collect the defined not supported fields in ABOUT files.
267268
--mapping-file Use a custom mapping file with mapping between input
@@ -279,6 +280,14 @@ Options
279280

280281
::
281282

283+
-filter TEXT
284+
285+
Filter for the output inventory.
286+
287+
$ about inventory --filter "license_expression=gpl-2.0" LOCATION OUTPUT
288+
289+
The above command will only inventory the ABOUT files which have the "license_expression: gpl-2.0"
290+
282291
-f, --format [json|csv]
283292
284293
Set OUTPUT file format. [default: csv]

docs/CHANGELOG.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
Release 3.1.2
44

5-
* New `--vartext` option
5+
* New `--vartext` option for `attrib`
66
* Add support for `checksum_sha256`
77
* `check` command will not counted INFO message as error when `--verbose` is set
88
* Update `track_change` to `track_changes`
9-
* Support `author_file`
9+
* Add support for `author_file`
10+
* New `--filter` option for `inventory`
1011

1112
2018-6-25
1213

src/attributecode/cmd.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from attributecode.util import extract_zip
3838
from attributecode.util import update_severity_level_about_resource_path_not_exist_error
3939
from attributecode.util import to_posix
40+
from attributecode.util import inventory_filter
4041

4142

4243
__copyright__ = """
@@ -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.')
@@ -132,7 +136,7 @@ def cli():
132136

133137
@click.help_option('-h', '--help')
134138

135-
def inventory(location, output, mapping, mapping_file, quiet, format, verbose):
139+
def inventory(location, output, mapping, mapping_file, filter, quiet, format, verbose):
136140
"""
137141
Collect a JSON or CSV inventory of components from .ABOUT files.
138142
@@ -157,6 +161,22 @@ def inventory(location, output, mapping, mapping_file, quiet, format, verbose):
157161

158162
errors, abouts = model.collect_inventory(location, use_mapping=mapping, mapping_file=mapping_file)
159163

164+
updated_abouts = []
165+
if filter:
166+
filter_dict = {}
167+
# Parse the filter and save to the filter dictionary with a list of value
168+
for element in filter:
169+
key = element.partition('=')[0]
170+
value = element.partition('=')[2]
171+
if key in filter_dict:
172+
filter_dict[key].append(value)
173+
else:
174+
value_list = [value]
175+
filter_dict[key] = value_list
176+
updated_abouts = inventory_filter(abouts, filter_dict)
177+
else:
178+
updated_abouts = abouts
179+
160180
# Do not write the output if one of the ABOUT files has duplicated key names
161181
dup_error_msg = u'Duplicated key name(s)'
162182
halt_output = False
@@ -166,7 +186,7 @@ def inventory(location, output, mapping, mapping_file, quiet, format, verbose):
166186
break
167187

168188
if not halt_output:
169-
write_errors = model.write_output(abouts, output, format)
189+
write_errors = model.write_output(updated_abouts, output, format)
170190
for err in write_errors:
171191
errors.append(err)
172192
else:

src/attributecode/util.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,4 +560,21 @@ def update_severity_level_about_resource_path_not_exist_error(errors):
560560
#continue
561561
else:
562562
updated_errors.append(err)
563-
return updated_errors
563+
return updated_errors
564+
565+
566+
def inventory_filter(abouts, filter_dict):
567+
updated_abouts = []
568+
for key in filter_dict:
569+
for about in abouts:
570+
try:
571+
# Check if the about object has the filtered attribute and if the
572+
# attributed value is the same as the defined in the filter
573+
for value in filter_dict[key]:
574+
if vars(about)[key].value == value:
575+
if not about in updated_abouts:
576+
updated_abouts.append(about)
577+
except:
578+
# The current about object does not have the defined attribute
579+
continue
580+
return updated_abouts

tests/test_util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from attributecode import CRITICAL
3232
from attributecode import ERROR, INFO
3333
from attributecode import Error
34+
from attributecode import model
3435
from attributecode import util
3536

3637

@@ -471,3 +472,13 @@ def test_check_duplicate_keys_about_file_with_multiline(self):
471472
expected = ['owner', 'notes']
472473
assert expected == util.check_duplicate_keys_about_file(test)
473474

475+
def test_inventory_filter(self):
476+
test_loc = get_test_loc('basic')
477+
_errors, abouts = model.collect_inventory(test_loc)
478+
479+
filter_dict = {'name': ['simple']}
480+
# The test loc has 2 .about files, only the simple.about is taken after
481+
# the filtering
482+
updated_abouts = util.inventory_filter(abouts, filter_dict)
483+
for about in updated_abouts:
484+
assert about.name.value == 'simple'

0 commit comments

Comments
 (0)