|
48 | 48 | from attributecode.util import filter_errors |
49 | 49 | from attributecode.util import get_temp_dir |
50 | 50 | from attributecode.util import get_file_text |
| 51 | +from attributecode.util import write_licenses |
51 | 52 |
|
52 | 53 | __copyright__ = """ |
53 | 54 | Copyright (c) nexB Inc and others. All rights reserved. |
@@ -276,6 +277,85 @@ def gen(location, output, android, fetch_license, fetch_license_djc, reference, |
276 | 277 | click.echo(msg) |
277 | 278 | sys.exit(errors_count) |
278 | 279 |
|
| 280 | + |
| 281 | +###################################################################### |
| 282 | +# gen_license subcommand |
| 283 | +###################################################################### |
| 284 | + |
| 285 | +@about.command(cls=AboutCommand, |
| 286 | + short_help='Fetch and save all the licenses in the license_expression field to a directory.') |
| 287 | + |
| 288 | +@click.argument('location', |
| 289 | + required=True, |
| 290 | + metavar='LOCATION', |
| 291 | + type=click.Path( |
| 292 | + exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True)) |
| 293 | + |
| 294 | +@click.argument('output', |
| 295 | + required=True, |
| 296 | + metavar='OUTPUT', |
| 297 | + type=click.Path(exists=True, file_okay=False, writable=True, resolve_path=True)) |
| 298 | + |
| 299 | +@click.option('--djc', |
| 300 | + nargs=2, |
| 301 | + type=str, |
| 302 | + metavar='api_url api_key', |
| 303 | + help='Fetch licenses from a DejaCode License Library.') |
| 304 | + |
| 305 | +@click.option('--verbose', |
| 306 | + is_flag=True, |
| 307 | + help='Show all error and warning messages.') |
| 308 | + |
| 309 | +@click.help_option('-h', '--help') |
| 310 | +def gen_license(location, output, djc, verbose): |
| 311 | + """ |
| 312 | +Fetch licenses in the license_expression field and save to the output location. |
| 313 | +
|
| 314 | +LOCATION: Path to a JSON/CSV/Excel/.ABOUT file(s) |
| 315 | +
|
| 316 | +OUTPUT: Path to a directory where license files are saved. |
| 317 | + """ |
| 318 | + print_version() |
| 319 | + |
| 320 | + if location.endswith('.csv') or location.endswith('.json') or location.endswith('.xlsx'): |
| 321 | + _errors, abouts = load_inventory( |
| 322 | + location=location |
| 323 | + ) |
| 324 | + else: |
| 325 | + _errors, abouts = collect_inventory(location) |
| 326 | + |
| 327 | + |
| 328 | + log_file_loc = os.path.join(output, 'error.log') |
| 329 | + api_url = '' |
| 330 | + api_key = '' |
| 331 | + errors = [] |
| 332 | + if djc: |
| 333 | + # Strip the ' and " for api_url, and api_key from input |
| 334 | + api_url = djc[0].strip("'").strip('"') |
| 335 | + api_key = djc[1].strip("'").strip('"') |
| 336 | + |
| 337 | + click.echo('Fetching licenses...') |
| 338 | + license_dict, lic_errors = pre_process_and_fetch_license_dict(abouts, api_url, api_key) |
| 339 | + if lic_errors: |
| 340 | + errors.extend(lic_errors) |
| 341 | + |
| 342 | + # A dictionary with license file name as the key and context as the value |
| 343 | + lic_dict_output = {} |
| 344 | + for key in license_dict: |
| 345 | + if not key in lic_dict_output: |
| 346 | + lic_filename = license_dict[key][1] |
| 347 | + lic_context = license_dict[key][2] |
| 348 | + lic_dict_output[lic_filename] = lic_context |
| 349 | + |
| 350 | + write_errors = write_licenses(lic_dict_output, output) |
| 351 | + if write_errors: |
| 352 | + errors.extend(write_errors) |
| 353 | + |
| 354 | + errors = unique(errors) |
| 355 | + severe_errors_count = report_errors(errors, quiet=False, verbose=verbose, log_file_loc=log_file_loc) |
| 356 | + sys.exit(severe_errors_count) |
| 357 | + |
| 358 | + |
279 | 359 | ###################################################################### |
280 | 360 | # attrib subcommand |
281 | 361 | ###################################################################### |
@@ -741,6 +821,7 @@ def report_errors(errors, quiet, verbose, log_file_loc=None): |
741 | 821 | log_msgs, _ = get_error_messages(errors, quiet=False, verbose=True) |
742 | 822 | with io.open(log_file_loc, 'w', encoding='utf-8', errors='replace') as lf: |
743 | 823 | lf.write('\n'.join(log_msgs)) |
| 824 | + click.echo("Error log: " + log_file_loc) |
744 | 825 | return severe_errors_count |
745 | 826 |
|
746 | 827 |
|
|
0 commit comments