Skip to content

Commit 704f08c

Browse files
committed
Fixed #583 - ability to exclude in check and inventory
* Add `--exclude` to check and inventory * Updated docs and tests * Updated changelog and version Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 8980f2f commit 704f08c

File tree

9 files changed

+262
-122
lines changed

9 files changed

+262
-122
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Changelog
33

44
xxxx-xx-xx
5-
Release xx.x.x
5+
Release 11.1.0
66

77
* Drop support for python version earlier than 3.9
8-
8+
* Add ability to "exclude" path in the check and inventory #583
99

1010
2024-09-16
1111
Release 11.0.2

about.ABOUT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
about_resource: .
22
name: AboutCode-toolkit
3-
version: 11.0.2
3+
version: 11.1.0
44
author: Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez
55
copyright: Copyright (c) nexB Inc.
66
description: |

docs/source/reference.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ Options
187187

188188
.. code-block:: none
189189
190+
--exclude PATTERN Exclude the processing of the specified input pattern
191+
(e.g. *tests* or test/).
190192
--license Validate the license_expression value in the input.
191193
--djc api_url api_key Validate license_expression from a DejaCode License
192194
Library API URL using the API KEY.
@@ -204,6 +206,14 @@ Details
204206

205207
.. code-block:: none
206208
209+
--exclude
210+
Exclude the processing of the specified input pattern
211+
212+
It takes a pattern or an exact directory as an argument.
213+
Multiple `--exclude` can be used.
214+
215+
$ about check --exclude ./tests/ --exclude "*sample*" /home/project/about_files/
216+
207217
--license
208218
Validate the license_expression value in the input.
209219
@@ -240,9 +250,25 @@ Details
240250
241251
Special Notes
242252
-------------
253+
`--djc`
254+
^^^^^^^
255+
243256
If no `--djc` option is set, the tool will default to check license_expression from
244257
ScanCode LicenseDB.
245258

259+
`--exclude`
260+
^^^^^^^^^^^
261+
262+
As the `--exclude` option accepts patterns that may include wildcards, the running shell could
263+
expand these patterns into filenames, potentially causing errors. To avoid this,
264+
ensure the pattern is wrapped in quotes.
265+
266+
On Windows, users can either use `^` to escape the `*` or use `--%` before `--exclude` to prevent shell globbing.
267+
268+
$ about check /home/project/about_files/ --exclude "tests^*"
269+
270+
$ about check /home/project/about_files/ --% --exclude "*tests*"
271+
246272
collect_redist_src
247273
==================
248274

@@ -566,6 +592,8 @@ Options
566592

567593
.. code-block:: none
568594
595+
--exclude PATTERN Exclude the processing of the specified input pattern
596+
(e.g. *tests* or test/).
569597
-f, --format [json|csv|excel] Set OUTPUT file format. [default: csv]
570598
-q, --quiet Do not print any error/warning.
571599
--verbose Show all the errors and warning.
@@ -647,6 +675,19 @@ To support multiple license file for a license, the correct format is to separat
647675
648676
Note that if license_name is not provided, the license key will be used as the license name.
649677

678+
`--exclude`
679+
^^^^^^^^^^^
680+
681+
As the `--exclude` option accepts patterns that may include wildcards, the running shell could
682+
expand these patterns into filenames, potentially causing errors. To avoid this,
683+
ensure the pattern is wrapped in quotes.
684+
685+
On Windows, users can either use `^` to escape the `*` or use `--%` before `--exclude` to prevent shell globbing.
686+
687+
$ about check /home/project/about_files/ --exclude "tests^*"
688+
689+
$ about check /home/project/about_files/ --% --exclude "*tests*"
690+
650691
transform
651692
=========
652693

src/attributecode/cmd.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ def validate_extensions(ctx, param, value, extensions=tuple(('.csv', '.json',)))
153153
required=True,
154154
metavar='OUTPUT',
155155
type=click.Path(exists=False, dir_okay=False, writable=True, resolve_path=True))
156+
@click.option('--exclude',
157+
multiple=True,
158+
metavar='PATTERN',
159+
help='Exclude the processing of the specified input pattern (e.g. *tests* or test/).')
156160
@click.option('-f', '--format',
157161
is_flag=False,
158162
default='csv',
@@ -166,7 +170,7 @@ def validate_extensions(ctx, param, value, extensions=tuple(('.csv', '.json',)))
166170
is_flag=True,
167171
help='Show all error and warning messages.')
168172
@click.help_option('-h', '--help')
169-
def inventory(location, output, format, quiet, verbose): # NOQA
173+
def inventory(location, output, exclude, format, quiet, verbose): # NOQA
170174
"""
171175
Collect the inventory of .ABOUT files to a CSV/JSON/XLSX file.
172176
@@ -181,7 +185,7 @@ def inventory(location, output, format, quiet, verbose): # NOQA
181185
if location.lower().endswith('.zip'):
182186
# accept zipped ABOUT files as input
183187
location = extract_zip(location)
184-
errors, abouts = collect_inventory(location)
188+
errors, abouts = collect_inventory(location, exclude)
185189
write_output(abouts=abouts, location=output, format=format)
186190

187191
errors_count = report_errors(
@@ -675,7 +679,6 @@ def collect_redist_src(location, output, from_inventory, with_structures, zip, q
675679

676680
# FIXME: This is really only a dupe of the Inventory command
677681

678-
679682
@about.command(cls=AboutCommand,
680683
short_help='Validate that the format of .ABOUT files is correct and report '
681684
'errors and warnings.')
@@ -684,6 +687,10 @@ def collect_redist_src(location, output, from_inventory, with_structures, zip, q
684687
metavar='LOCATION',
685688
type=click.Path(
686689
exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True))
690+
@click.option('--exclude',
691+
multiple=True,
692+
metavar='PATTERN',
693+
help='Exclude the processing of the specified input pattern (e.g. *tests* or test/).')
687694
@click.option('--license',
688695
is_flag=True,
689696
help='Validate the license_expression value in the input.')
@@ -701,7 +708,7 @@ def collect_redist_src(location, output, from_inventory, with_structures, zip, q
701708
is_flag=True,
702709
help='Show all error and warning messages.')
703710
@click.help_option('-h', '--help')
704-
def check(location, license, djc, log, verbose):
711+
def check(location, exclude, license, djc, log, verbose):
705712
"""
706713
Check .ABOUT file(s) at LOCATION for validity and print error messages.
707714
@@ -722,7 +729,8 @@ def check(location, license, djc, log, verbose):
722729
api_url = djc[0].strip("'").strip('"')
723730
api_key = djc[1].strip("'").strip('"')
724731
click.echo('Checking ABOUT files...')
725-
errors, abouts = collect_inventory(location)
732+
733+
errors, abouts = collect_inventory(location, exclude)
726734

727735
# Validate license_expression
728736
if license:

src/attributecode/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,14 +1553,14 @@ def dump_lic(self, location, license_dict):
15531553
return license_key_name_context_url
15541554

15551555

1556-
def collect_inventory(location):
1556+
def collect_inventory(location, exclude=None):
15571557
"""
15581558
Collect ABOUT files at location and return a list of errors and a list of
15591559
About objects.
15601560
"""
15611561
errors = []
15621562
input_location = util.get_absolute(location)
1563-
about_locations = list(util.get_about_locations(input_location))
1563+
about_locations = list(util.get_about_locations(input_location, exclude))
15641564

15651565
name_errors = util.check_file_names(about_locations)
15661566
errors.extend(name_errors)
@@ -1572,7 +1572,7 @@ def collect_inventory(location):
15721572
for severity, message in about.errors:
15731573
if 'Custom Field' in message:
15741574
field_name = message.replace('Custom Field: ', '').strip()
1575-
if not field_name in custom_fields_list:
1575+
if field_name not in custom_fields_list:
15761576
custom_fields_list.append(field_name)
15771577
else:
15781578
msg = (about_file_path + ": " + message)

0 commit comments

Comments
 (0)