|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (c) 2019 nexB Inc. and others. All rights reserved. |
| 4 | +# http://nexb.com and https://github.com/nexB/scancode-toolkit/ |
| 5 | +# The ScanCode software is licensed under the Apache License version 2.0. |
| 6 | +# Data generated with ScanCode require an acknowledgment. |
| 7 | +# ScanCode is a trademark of nexB Inc. |
| 8 | +# |
| 9 | +# You may not use this software except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 11 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 12 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 13 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 14 | +# specific language governing permissions and limitations under the License. |
| 15 | +# |
| 16 | +# When you publish or redistribute any data created with ScanCode or any ScanCode |
| 17 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 18 | +# |
| 19 | +# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 20 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 21 | +# ScanCode should be considered or used as legal advice. Consult an Attorney |
| 22 | +# for any legal advice. |
| 23 | +# ScanCode is a free software code scanning tool from nexB Inc. and others. |
| 24 | +# Visit https://github.com/nexB/scancode-toolkit/ for support and download. |
| 25 | + |
| 26 | +from __future__ import absolute_import |
| 27 | +from __future__ import unicode_literals |
| 28 | +from __future__ import print_function |
| 29 | + |
| 30 | +import os |
| 31 | + |
| 32 | +import click |
| 33 | +click.disable_unicode_literals_warning = True |
| 34 | + |
| 35 | +from licensedcode.models import load_licenses |
| 36 | +from licensedcode.models import FOSS_CATEGORIES |
| 37 | +from scancode.cli_test_utils import run_scan_plain |
| 38 | + |
| 39 | + |
| 40 | +""" |
| 41 | +Generate an SPDX document for each license known in ScanCode that are not usted |
| 42 | +at SPDX. |
| 43 | +Run python genlicspdx.py -h for help. |
| 44 | +
|
| 45 | +NOTE: this is rather inefficient as it is starting a new command line process |
| 46 | +for each license, taking a few seconds each time. |
| 47 | +Upcomming code to call a scan function instead will be more efficient. |
| 48 | +""" |
| 49 | + |
| 50 | +@click.command() |
| 51 | +@click.argument('license_dir', |
| 52 | + type=click.Path(file_okay=False, exists=True, writable=True, |
| 53 | + allow_dash=False, resolve_path=True), |
| 54 | + metavar='DIR') |
| 55 | +@click.option('-v', '--verbose', is_flag=True, default=False, help='Print execution messages.') |
| 56 | +@click.help_option('-h', '--help') |
| 57 | +def cli(license_dir, verbose): |
| 58 | + """ |
| 59 | + Create one SPDX tag-value document for each non-SPDX ScanCode licenses. |
| 60 | + Store these in the DIR directory |
| 61 | + """ |
| 62 | + |
| 63 | + base_args = ['--license', '--license-diag', '--license-text', '--info', |
| 64 | + '--strip-root', '--quiet', '--spdx-tv'] |
| 65 | + |
| 66 | + licenses_by_key = load_licenses(with_deprecated=False) |
| 67 | + |
| 68 | + for lic in licenses_by_key.values(): |
| 69 | + ld = lic.to_dict() |
| 70 | + |
| 71 | + if lic.spdx_license_key: |
| 72 | + if verbose: |
| 73 | + click.echo( |
| 74 | + 'Skipping ScanCode: {key} that is an SPDX license: {spdx_license_key}'.format(**ld)) |
| 75 | + continue |
| 76 | + |
| 77 | + if not lic.text_file or not os.path.exists(lic.text_file): |
| 78 | + if verbose: |
| 79 | + click.echo( |
| 80 | + 'Skipping license without text: {key}'.format(**ld)) |
| 81 | + continue |
| 82 | + |
| 83 | + if lic.category not in FOSS_CATEGORIES: |
| 84 | + if verbose: |
| 85 | + click.echo( |
| 86 | + 'Skipping non FOSS license: {key}'.format(**ld)) |
| 87 | + continue |
| 88 | + |
| 89 | + |
| 90 | + output_file = 'licenseref-scancode-{key}.spdx'.format(**ld) |
| 91 | + output = os.path.join(license_dir, output_file) |
| 92 | + |
| 93 | + if verbose: |
| 94 | + click.echo('Creating SPDX document for license: {key}'.format(**ld)) |
| 95 | + click.echo('at: {output_file}'.format(**locals())) |
| 96 | + |
| 97 | + options = base_args + [output, lic.text_file] |
| 98 | + _, _, _ = run_scan_plain(options, test_mode=False) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + cli() |
0 commit comments