Skip to content

Commit 686ee22

Browse files
committed
Merge changed from develop branch
Signed-off-by: Chin Yeung Li <[email protected]>
2 parents b085676 + b5b1e9b commit 686ee22

File tree

23 files changed

+267
-60
lines changed

23 files changed

+267
-60
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ docs/_build
4444
.ve
4545
*.bak
4646
/.cache/
47-
/.settings/
47+
/.settings/
48+
/tcl/

USAGE.rst

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ attrib
4545
names - mapping.config
4646
--mapping-file Use a custom mapping file with mapping between input
4747
keys and ABOUT field names.
48-
--verbose Show all the errors and warning.
4948
--template PATH Path to a custom attribution template.
49+
--vartext TEXT Variable texts to the attribution template
50+
--verbose Show all the errors and warning.
5051
-q, --quiet Do not print any error/warning.
5152
-h, --help Show this message and exit.
5253

@@ -55,7 +56,7 @@ Purpose
5556
Generate an attribution file which contains the all license information
5657
from the LOCATION along with the license text.
5758

58-
Assuming the follow:
59+
Assume the following:
5960

6061
::
6162

@@ -90,11 +91,6 @@ Options
9091

9192
$ about attrib --mapping-file CUSTOM_MAPPING_FILE_PATH LOCATION OUTPUT
9293

93-
--verbose
94-
95-
This option tells the tool to show all errors found.
96-
The default behavior will only show 'CRITICAL', 'ERROR', and 'WARNING'
97-
9894
--template
9995

10096
This option allows you to use your own template for attribution generation.
@@ -103,6 +99,21 @@ Options
10399

104100
$ about attrib --template /home/custom_template/template.html LOCATION OUTPUT
105101

102+
--vartext
103+
104+
This option allow you to pass variable texts to the attribution template
105+
106+
$ about attrib --vartext title="Attribution Notice" --vartext header="Product 101" LOCATION OUTPUT
107+
108+
Users can use the following in the template to get the vartext:
109+
{{ vartext_dict['title'] }}
110+
{{ vartext_dict['header'] }}
111+
112+
--verbose
113+
114+
This option tells the tool to show all errors found.
115+
The default behavior will only show 'CRITICAL', 'ERROR', and 'WARNING'
116+
106117

107118
The following data are passed to jinja2 and, therefore, can be used for a custom template:
108119
* about object: the about objects
@@ -251,10 +262,13 @@ inventory
251262

252263
::
253264

265+
--filter TEXT Filter for the output inventory.
254266
-f, --format [json|csv] Set OUTPUT file format. [default: csv]
255267
--mapping Use file mapping.config to collect the defined not supported fields in ABOUT files.
256268
--mapping-file Use a custom mapping file with mapping between input
257269
keys and ABOUT field names.
270+
--mapping-output FILE Use a custom mapping file with mapping between
271+
ABOUT field names and output keys
258272
--verbose Show all the errors and warning.
259273
-q, --quiet Do not print any error/warning.
260274
-h, --help Show this message and exit.
@@ -268,6 +282,14 @@ Options
268282

269283
::
270284

285+
-filter TEXT
286+
287+
Filter for the output inventory.
288+
289+
$ about inventory --filter "license_expression=gpl-2.0" LOCATION OUTPUT
290+
291+
The above command will only inventory the ABOUT files which have the "license_expression: gpl-2.0"
292+
271293
-f, --format [json|csv]
272294
273295
Set OUTPUT file format. [default: csv]
@@ -282,7 +304,19 @@ Options
282304

283305
Same behavior as `--mapping` but with custom mapping file
284306

285-
$ about attrib --mapping-file CUSTOM_MAPPING_FILE_PATH LOCATION OUTPUT
307+
$ about inventory --mapping-file CUSTOM_MAPPING_FILE_PATH LOCATION OUTPUT
308+
309+
--mapping-output
310+
311+
Same behavior as `--mapping-file` but with custom mapping file
312+
In the custom mapping file, the left side is the custom key name where
313+
the right side is the ABOUT field name. For instance,
314+
Component: name
315+
316+
The "Component" is a custom field name for the output
317+
The "name" is one of the defaul ABOUT field name that user want to convert
318+
319+
$ about inventory --mapping-output CUSTOM_MAPPING_FILE_PATH LOCATION OUTPUT
286320

287321
--verbose
288322

about

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ fi
1616

1717
source $ABOUT_ROOT_DIR/bin/activate
1818

19-
$ABOUT_ROOT_DIR/bin/about-code "$@"
19+
$ABOUT_ROOT_DIR/bin/about "$@"

about.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ goto about
2929

3030
:about
3131
call %ABOUT_ROOT_DIR%\Scripts\activate
32-
echo %ABOUT_ROOT_DIR%\bin\about-code %CMD_LINE_ARGS%
33-
%ABOUT_ROOT_DIR%\bin\about-code %CMD_LINE_ARGS%
32+
echo %ABOUT_ROOT_DIR%\bin\about %CMD_LINE_ARGS%
33+
%ABOUT_ROOT_DIR%\bin\about %CMD_LINE_ARGS%
3434

3535
:EOS

docs/CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2018-09-19
2+
3+
Release 3.1.2
4+
5+
* New `--vartext` option for `attrib`
6+
* Add support for `checksum_sha256` and `author_file`
7+
* `check` command will not count INFO message as error when `--verbose` is set
8+
* Update `track_change` to `track_changes`
9+
* New `--filter` and `--mapping-output` options for `inventory`
10+
111
2018-6-25
212

313
Release 3.1.1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def read(*names, **kwargs):
2424

2525
setup(
2626
name='aboutcode-toolkit',
27-
version='3.1.1',
27+
version='3.1.2',
2828
license='Apache-2.0',
2929
description=(
3030
'AboutCode-toolkit is a tool to document the provenance (origin and license) of '

src/attributecode/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
basestring = str # Python 3
2828

2929

30-
__version__ = '3.1.1'
30+
__version__ = '3.1.2'
3131

3232

3333
__about_spec_version__ = '3.0.0'

src/attributecode/attrib.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from attributecode.util import add_unc
3838

3939

40-
def generate(abouts, template_string=None):
40+
def generate(abouts, template_string=None, vartext_dict=None):
4141
"""
4242
Generate and return attribution text from a list of About objects and a
4343
template string.
@@ -110,7 +110,7 @@ def generate(abouts, template_string=None):
110110
license_file_name_and_key=license_file_name_and_key,
111111
license_key_to_license_name=license_key_to_license_name,
112112
license_name_to_license_key=license_name_to_license_key,
113-
utcnow=utcnow)
113+
utcnow=utcnow, vartext_dict=vartext_dict)
114114
except Exception as e:
115115
line = getattr(e, 'lineno', None)
116116
ln_msg = ' at line: %r' % line if line else ''
@@ -134,7 +134,7 @@ def check_template(template_string):
134134
default_template = join(os.path.dirname(os.path.realpath(__file__)),
135135
'templates', 'default_html.template')
136136

137-
def generate_from_file(abouts, template_loc=None):
137+
def generate_from_file(abouts, template_loc=None, vartext_dict=None):
138138
"""
139139
Generate and return attribution string from a list of About objects and a
140140
template location.
@@ -144,11 +144,11 @@ def generate_from_file(abouts, template_loc=None):
144144
template_loc = add_unc(template_loc)
145145
with codecs.open(template_loc, 'rb', encoding='utf-8') as tplf:
146146
tpls = tplf.read()
147-
return generate(abouts, template_string=tpls)
147+
return generate(abouts, template_string=tpls, vartext_dict=vartext_dict)
148148

149149

150150
def generate_and_save(abouts, output_location, use_mapping=False, mapping_file=None,
151-
template_loc=None, inventory_location=None):
151+
template_loc=None, inventory_location=None, vartext=None):
152152
"""
153153
Generate attribution file using the `abouts` list of About object
154154
at `output_location`.
@@ -167,6 +167,7 @@ def generate_and_save(abouts, output_location, use_mapping=False, mapping_file=N
167167
afp_list = []
168168
not_match_path = []
169169
errors = []
170+
vartext_dict = {}
170171

171172
if not inventory_location:
172173
updated_abouts = abouts
@@ -239,7 +240,14 @@ def generate_and_save(abouts, output_location, use_mapping=False, mapping_file=N
239240
else:
240241
about.license.value = lic_list
241242

242-
rendered = generate_from_file(updated_abouts, template_loc=template_loc)
243+
# Parse the vartext and save to the vartext dictionary
244+
if vartext:
245+
for var in vartext:
246+
key = var.partition('=')[0]
247+
value = var.partition('=')[2]
248+
vartext_dict[key] = value
249+
250+
rendered = generate_from_file(updated_abouts, template_loc=template_loc, vartext_dict=vartext_dict)
243251

244252
if rendered:
245253
output_location = add_unc(output_location)

src/attributecode/cmd.py

Lines changed: 41 additions & 11 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__ = """
@@ -90,7 +91,7 @@ def cli():
9091
9192
Read, 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
"""
137145
Collect 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
"""
311338
Generate 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

Comments
 (0)