|
| 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 scancode.cli import run_scan |
| 37 | + |
| 38 | + |
| 39 | +""" |
| 40 | +Generate an SPDX document for each license known in ScanCode that are not usted |
| 41 | +at SPDX. |
| 42 | +Run python genlicspdx.py -h for help. |
| 43 | +
|
| 44 | +NOTE: this is rather inefficient as it is starting a new command line process |
| 45 | +for each license, taking a few seconds each time. |
| 46 | +Upcomming code to call a scan function instead will be more efficient. |
| 47 | +""" |
| 48 | + |
| 49 | +FOSS_CATEGORIES = set([ |
| 50 | + 'Copyleft', |
| 51 | + 'Copyleft Limited', |
| 52 | + 'Patent License', |
| 53 | + 'Permissive', |
| 54 | + 'Public Domain', |
| 55 | +]) |
| 56 | + |
| 57 | + |
| 58 | +@click.command() |
| 59 | +@click.argument('license_dir', |
| 60 | + type=click.Path(file_okay=False, exists=True, writable=True, |
| 61 | + allow_dash=False, resolve_path=True), |
| 62 | + metavar='DIR') |
| 63 | +@click.option('-v', '--verbose', is_flag=True, default=False, help='Print execution messages.') |
| 64 | +@click.help_option('-h', '--help') |
| 65 | +def cli(license_dir, verbose): |
| 66 | + """ |
| 67 | + Create one SPDX tag-value document for each non-SPDX ScanCode licenses. |
| 68 | + Store these in the DIR directory |
| 69 | + """ |
| 70 | + |
| 71 | + base_kwargs = dict( |
| 72 | + license=True, license_diag=True, license_text=True, info=True, |
| 73 | + strip_root=True, quiet=True, return_results=False) |
| 74 | + |
| 75 | + licenses_by_key = load_licenses(with_deprecated=False) |
| 76 | + |
| 77 | + |
| 78 | + for i, lic in enumerate(licenses_by_key.values()): |
| 79 | + ld = lic.to_dict() |
| 80 | + |
| 81 | + if lic.spdx_license_key: |
| 82 | + if verbose: |
| 83 | + click.echo( |
| 84 | + 'Skipping ScanCode: {key} that is an SPDX license: {spdx_license_key}'.format(**ld)) |
| 85 | + continue |
| 86 | + |
| 87 | + if not lic.text_file or not os.path.exists(lic.text_file): |
| 88 | + if verbose: |
| 89 | + click.echo( |
| 90 | + 'Skipping license without text: {key}'.format(**ld)) |
| 91 | + continue |
| 92 | + |
| 93 | + if lic.category not in FOSS_CATEGORIES: |
| 94 | + if verbose: |
| 95 | + click.echo( |
| 96 | + 'Skipping non FOSS license: {key}'.format(**ld)) |
| 97 | + continue |
| 98 | + |
| 99 | + output = 'licenseref-scancode-{key}.spdx'.format(**ld) |
| 100 | + output = os.path.join(license_dir, output) |
| 101 | + |
| 102 | + if verbose: |
| 103 | + click.echo('Creating SPDX document for license: {key}'.format(**ld)) |
| 104 | + click.echo('at: {output}'.format(**locals())) |
| 105 | + |
| 106 | + with open(output, 'wb') as ouput_file: |
| 107 | + kwargs = dict(input=lic.text_file, spdx_tv=ouput_file) |
| 108 | + kwargs.update(base_kwargs) |
| 109 | + run_scan(**kwargs) |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + cli() |
0 commit comments