-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathcmd.py
More file actions
671 lines (526 loc) · 21.7 KB
/
Copy pathcmd.py
File metadata and controls
671 lines (526 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#!/usr/bin/env python
# -*- coding: utf8 -*-
# ============================================================================
# Copyright (c) 2013-2018 nexB Inc. http://www.nexb.com/ - All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from collections import defaultdict
from functools import partial
import io
import logging
import os
import sys
import click
# silence unicode literals warnings
click.disable_unicode_literals_warning = True
from attributecode import WARNING
from attributecode.util import unique
from attributecode import __about_spec_version__
from attributecode import __version__
from attributecode import DEFAULT_MAPPING
from attributecode import severities
from attributecode.attrib import check_template
from attributecode.attrib import DEFAULT_TEMPLATE_FILE
from attributecode.attrib import generate_and_save as generate_attribution_doc
from attributecode.gen import generate as generate_about_files
from attributecode.model import collect_inventory
from attributecode.model import write_output
from attributecode.util import extract_zip
from attributecode.util import inventory_filter
__copyright__ = """
Copyright (c) 2013-2018 nexB Inc and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""
prog_name = 'AboutCode-toolkit'
intro = '''%(prog_name)s version %(__version__)s
ABOUT spec version: %(__about_spec_version__)s
https://aboutcode.org
%(__copyright__)s
''' % locals()
def print_version():
click.echo('Running aboutcode-toolkit version ' + __version__)
class AboutCommand(click.Command):
"""
An enhanced click Command working around some Click quirk.
"""
def main(self, args=None, prog_name=None, complete_var=None,
standalone_mode=True, **extra):
"""
Workaround click bug https://github.com/mitsuhiko/click/issues/365
"""
return click.Command.main(
self, args=args, prog_name=self.name,
complete_var=complete_var, standalone_mode=standalone_mode, **extra)
# we define a main entry command with subcommands
@click.group(name='about')
@click.version_option(version=__version__, prog_name=prog_name, message=intro)
@click.help_option('-h', '--help')
def about():
"""
Generate licensing attribution and credit notices from .ABOUT files and inventories.
Read, write and collect provenance and license inventories from .ABOUT files to and from JSON or CSV files.
Use about <command> --help for help on a command.
"""
######################################################################
# option validators
######################################################################
def validate_key_values(ctx, param, value):
"""
Return the a mapping of {key: [values,...] if valid or raise a UsageError
otherwise.
"""
if not value:
return
kvals, errors = parse_key_values(value)
if errors:
ive = '\n'.join(sorted(' ' + x for x in errors))
msg = ('Invalid {param} option(s):\n'
'{ive}'.format(**locals()))
raise click.UsageError(msg)
return kvals
def validate_mapping(mapping, mapping_file):
"""
Return a mapping_file or None.
Raise a UsageError on errors.
"""
if mapping and mapping_file:
raise click.UsageError(
'Invalid options combination: '
'--mapping and --mapping-file are ultually exclusive.')
if mapping:
return DEFAULT_MAPPING
return mapping_file or None
def validate_extensions(ctx, param, value, extensions=tuple(('.csv', '.json',))):
if not value:
return
if not value.endswith(extensions):
msg = ' '.join(extensions)
raise click.UsageError(
'Invalid {param} file extension: must be one of: {msg}'.format(**locals()))
return value
######################################################################
# inventory subcommand
######################################################################
@about.command(cls=AboutCommand,
short_help='Collect the inventory of .ABOUT files to a CSV or JSON file.')
@click.argument('location',
required=True,
metavar='LOCATION',
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True))
@click.argument('output',
required=True,
metavar='OUTPUT',
type=click.Path(exists=False, dir_okay=False, writable=True, resolve_path=True))
# fIXME: this is too complex and should be removed
@click.option('--filter',
multiple=True,
metavar='<key>=<value>',
callback=validate_key_values,
help='Filter the inventory to ABOUT matching these key=value e.g. "license_expression=gpl-2.0')
@click.option('-f', '--format',
is_flag=False,
default='csv',
show_default=True,
type=click.Choice(['json', 'csv']),
help='Set OUTPUT inventory file format.')
@click.option('--mapping',
is_flag=True,
help='Use the default built-in "mapping.config" file '
'with mapping between input keys and .ABOUT field names.'
'Cannot be combined with the --mapping-file option.')
@click.option('--mapping-file',
metavar='FILE',
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
help='Path to an optional custom mapping FILE '
'with mapping between input keys and .ABOUT field names. '
'Cannot be combined with the --mapping option.')
@click.option('-q', '--quiet',
is_flag=True,
help='Do not print error or warning messages.')
@click.option('--verbose',
is_flag=True,
help='Show all error and warning messages.')
@click.help_option('-h', '--help')
def inventory(location, output, mapping, mapping_file,
format, filter, quiet, verbose): # NOQA
"""
Collect the inventory of .ABOUT file data as CSV or JSON.
LOCATION: Path to an .ABOUT file or a directory with .ABOUT files.
OUTPUT: Path to the JSON or CSV inventory file to create.
"""
if not quiet:
print_version()
click.echo('Collecting inventory from ABOUT files...')
# FIXME: do we really want to continue support zip as an input?
if location.lower().endswith('.zip'):
# accept zipped ABOUT files as input
location = extract_zip(location)
mapping_file = validate_mapping(mapping, mapping_file)
errors, abouts = collect_inventory(location, mapping_file=mapping_file)
# FIXME: this is too complex
if filter:
abouts = inventory_filter(abouts, filter)
# Do not write the output if one of the ABOUT files has duplicated keys
# TODO: why do this check here?? Also if this is the place, we should list what the errors are.
dup_error_msg = u'Duplicated keys'
halt_output = False
for err in errors:
if dup_error_msg in err.message:
halt_output = True
break
if not halt_output:
write_errors = write_output(abouts=abouts, location=output, format=format)
for err in write_errors:
errors.append(err)
else:
if not quiet:
msg = u'Duplicated keys are not supported.\nPlease correct and re-run.'
click.echo(msg)
errors_count = report_errors(errors, quiet, verbose, log_file_loc=output + '-error.log')
if not quiet:
msg = 'Inventory collected in {output}.'.format(**locals())
click.echo(msg)
sys.exit(errors_count)
######################################################################
# gen subcommand
######################################################################
@about.command(cls=AboutCommand,
short_help='Generate .ABOUT files from an inventory as CSV or JSON.')
@click.argument('location',
required=True,
metavar='LOCATION',
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True))
@click.argument('output',
required=True,
metavar='OUTPUT',
type=click.Path(exists=True, file_okay=False, writable=True, resolve_path=True))
# FIXME: the CLI UX should be improved with two separate options for API key and URL
@click.option('--fetch-license',
nargs=2,
type=str,
metavar='URL KEY',
help='Fetch license data and text files from a DejaCode License Library '
'API URL using the API KEY.')
@click.option('--reference',
metavar='DIR',
type=click.Path(exists=True, file_okay=False, readable=True, resolve_path=True),
help='Path to a directory with reference license data and text files.')
@click.option('--mapping',
is_flag=True,
help='Use the default built-in "mapping.config" file '
'with mapping between input keys and .ABOUT field names.'
'Cannot be combined with the --mapping-file option.')
@click.option('--mapping-file',
metavar='FILE',
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
help='Path to an optional custom mapping FILE '
'with mapping between input keys and .ABOUT field names. '
'Cannot be combined with the --mapping option.')
@click.option('-q', '--quiet',
is_flag=True,
help='Do not print error or warning messages.')
@click.option('--verbose',
is_flag=True,
help='Show all error and warning messages.')
@click.help_option('-h', '--help')
def gen(location, output,
fetch_license,
reference,
mapping, mapping_file,
quiet, verbose):
"""
Generate .ABOUT files in OUTPUT from an inventory of .ABOUT files at LOCATION.
LOCATION: Path to a JSON or CSV inventory file.
OUTPUT: Path to a directory where ABOUT files are generated.
"""
if not quiet:
print_version()
click.echo('Generating .ABOUT files...')
mapping_file = validate_mapping(mapping, mapping_file)
if not location.endswith(('.csv', '.json',)):
raise click.UsageError('ERROR: Invalid input file extension: must be one .csv or .json.')
errors, abouts = generate_about_files(
location=location,
base_dir=output,
reference_dir=reference,
fetch_license=fetch_license,
mapping_file=mapping_file
)
errors_count = report_errors(errors, quiet, verbose, log_file_loc=output + '-error.log')
if not quiet:
abouts_count = len(abouts)
msg = '{abouts_count} .ABOUT files generated in {output}.'.format(**locals())
click.echo(msg)
sys.exit(errors_count)
######################################################################
# attrib subcommand
######################################################################
def validate_template(ctx, param, value):
if not value:
return DEFAULT_TEMPLATE_FILE
with io.open(value, encoding='utf-8') as templatef:
template_error = check_template(templatef.read())
if template_error:
lineno, message = template_error
raise click.UsageError(
'Template syntax error at line: '
'{lineno}: "{message}"'.format(**locals()))
return value
@about.command(cls=AboutCommand,
short_help='Generate an attribution document from .ABOUT files.')
@click.argument('location',
required=True,
metavar='LOCATION',
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True))
@click.argument('output',
required=True,
metavar='OUTPUT',
type=click.Path(exists=False, dir_okay=False, writable=True, resolve_path=True))
@click.option('--template',
metavar='FILE',
callback=validate_template,
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
help='Path to an optional custom attribution template to generate the '
'attribution document. If not provided the default built-in template is used.')
@click.option('--vartext',
multiple=True,
callback=validate_key_values,
metavar='<key>=<value>',
help='Add variable text as key=value for use in a custom attribution template.')
@click.option('--inventory',
metavar='FILE',
type=click.Path(exists=True, dir_okay=False, resolve_path=True),
help='Path to an optional JSON or CSV inventory FILE listing the '
'subset of .ABOUT files paths to consider when generating the attribution document.')
@click.option('--mapping',
is_flag=True,
help='Use the default built-in "mapping.config" file '
'with mapping between input keys and .ABOUT field names.'
'Cannot be combined with the --mapping-file option.')
@click.option('--mapping-file',
metavar='FILE',
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
help='Path to an optional custom mapping FILE '
'with mapping between input keys and .ABOUT field names. '
'Cannot be combined with the --mapping option.')
@click.option('-q', '--quiet',
is_flag=True,
help='Do not print error or warning messages.')
@click.option('--verbose',
is_flag=True,
help='Show all error and warning messages.')
@click.help_option('-h', '--help')
def attrib(location, output, template, vartext,
inventory, mapping, mapping_file,
quiet, verbose):
"""
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.
LOCATION: Path to a file, directory or .zip archive containing .ABOUT files.
OUTPUT: Path where to write the attribution document.
"""
if not quiet:
print_version()
click.echo('Generating attribution...')
mapping_file = validate_mapping(mapping, mapping_file)
# accept zipped ABOUT files as input
if location.lower().endswith('.zip'):
location = extract_zip(location)
errors, abouts = collect_inventory(location, mapping_file=mapping_file)
attrib_errors = generate_attribution_doc(
abouts=abouts,
output_location=output,
template_loc=template,
variables=vartext,
mapping_file=mapping_file,
inventory_location=inventory,
)
errors.extend(attrib_errors)
errors_count = report_errors(errors, quiet, verbose, log_file_loc=output + '-error.log')
if not quiet:
msg = 'Attribution generated in: {output}'.format(**locals())
click.echo(msg)
sys.exit(errors_count)
######################################################################
# check subcommand
######################################################################
# FIXME: This is really only a dupe of the Inventory command
@about.command(cls=AboutCommand,
short_help='Validate that the format of .ABOUT files is correct and report '
'errors and warnings.')
@click.argument('location',
required=True,
metavar='LOCATION',
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True))
@click.option('--verbose',
is_flag=True,
help='Show all error and warning messages.')
@click.help_option('-h', '--help')
def check(location, verbose):
"""
Check .ABOUT file(s) at LOCATION for validity and print error messages.
LOCATION: Path to a file or directory containing .ABOUT files.
"""
print_version()
click.echo('Checking ABOUT files...')
errors, _abouts = collect_inventory(location)
severe_errors_count = report_errors(errors, quiet=False, verbose=verbose)
sys.exit(severe_errors_count)
######################################################################
# transform subcommand
######################################################################
def print_config_help(ctx, param, value):
if not value or ctx.resilient_parsing:
return
from attributecode.transform import tranformer_config_help
click.echo(tranformer_config_help)
ctx.exit()
@about.command(cls=AboutCommand,
short_help='Transform a CSV by applying renamings, filters and checks.')
@click.argument('location',
required=True,
callback=partial(validate_extensions, extensions=('.csv',)),
metavar='LOCATION',
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True))
@click.argument('output',
required=True,
callback=partial(validate_extensions, extensions=('.csv',)),
metavar='OUTPUT',
type=click.Path(exists=False, dir_okay=False, writable=True, resolve_path=True))
@click.option('-c', '--configuration',
metavar='FILE',
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
help='Path to an optional YAML configuration file. See --help-format for '
'format help.')
@click.option('--help-format',
is_flag=True, is_eager=True, expose_value=False,
callback=print_config_help,
help='Show configuration file format help and exit.')
@click.option('-q', '--quiet',
is_flag=True,
help='Do not print error or warning messages.')
@click.option('--verbose',
is_flag=True,
help='Show all error and warning messages.')
@click.help_option('-h', '--help')
def transform(location, output, configuration, quiet, verbose): # NOQA
"""
Transform the CSV file at LOCATION by applying renamings, filters and checks
and write a new CSV to OUTPUT.
LOCATION: Path to a CSV file.
OUTPUT: Path to CSV inventory file to create.
"""
from attributecode.transform import transform_csv_to_csv
from attributecode.transform import Transformer
if not quiet:
print_version()
click.echo('Transforming CSV...')
if not configuration:
transformer = Transformer.default()
else:
transformer = Transformer.from_file(configuration)
errors = transform_csv_to_csv(location, output, transformer)
errors_count = report_errors(errors, quiet, verbose)
if not quiet:
msg = 'Transformed CSV written to {output}.'.format(**locals())
click.echo(msg)
sys.exit(errors_count)
######################################################################
# Error management
######################################################################
def report_errors(errors, quiet, verbose, log_file_loc=None):
"""
Report the `errors` list of Error objects to screen based on the `quiet` and
`verbose` flags.
If `log_file_loc` file location is provided also write a verbose log to this
file.
Return True if there were severe error reported.
"""
errors = unique(errors)
messages, severe_errors_count = get_error_messages(errors, quiet, verbose)
for msg in messages:
click.echo(msg)
if log_file_loc:
log_msgs, _ = get_error_messages(errors, quiet=False, verbose=True)
with io.open(log_file_loc, 'w', encoding='utf-8') as lf:
lf.write('\n'.join(log_msgs))
return severe_errors_count
def get_error_messages(errors, quiet=False, verbose=False):
"""
Return a tuple of (list of error message strings to report,
severe_errors_count) given an `errors` list of Error objects and using the
`quiet` and `verbose` flags.
"""
errors = unique(errors)
severe_errors = filter_errors(errors, WARNING)
severe_errors_count = len(severe_errors)
messages = []
if severe_errors and not quiet:
error_msg = 'Command completed with {} errors or warnings.'.format(severe_errors_count)
messages.append(error_msg)
for severity, message in errors:
sevcode = severities.get(severity) or 'UNKNOWN'
msg = '{sevcode}: {message}'.format(**locals())
if not quiet:
if verbose:
messages .append(msg)
elif severity >= WARNING:
messages .append(msg)
return messages, severe_errors_count
def filter_errors(errors, minimum_severity=WARNING):
"""
Return a list of unique `errors` Error object filtering errors that have a
severity below `minimum_severity`.
"""
return unique([e for e in errors if e.severity >= minimum_severity])
######################################################################
# Misc
######################################################################
def parse_key_values(key_values):
"""
Given a list of "key=value" strings, return:
- a mapping {key: [value, value, ...]}
- a sorted list of unique error messages for invalid entries where there is
a missing a key or value.
"""
if not key_values:
return {}, []
errors = set()
parsed_key_values = defaultdict(list)
for key_value in key_values:
key, _, value = key_value.partition('=')
key = key.strip().lower()
if not key:
errors.add('missing <key> in "{key_value}".'.format(**locals()))
continue
value = value.strip()
if not value:
errors.add('missing <value> in "{key_value}".'.format(**locals()))
continue
values = parsed_key_values[key]
if value not in values:
parsed_key_values[key].append(value)
return dict(parsed_key_values), sorted(errors)
if __name__ == '__main__':
about()