Skip to content

Commit

Permalink
Add scancode license data dump as a console script #2738
Browse files Browse the repository at this point in the history
* Removes CLI option `--dump-license-data`
* Adds new console script `scancode-license-data --path PATH`
* Updates CHANGELOG

Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
  • Loading branch information
AyanSinhaMahapatra committed Jan 12, 2023
1 parent 9a7693a commit e964f6f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ License detection:

- See https://github.com/nexB/scancode-toolkit/issues/3049

- There is a new ``--get-license-data`` scancode command line option to export
- There is a new console script ``scancode-license-data`` to export
license data in JSON, YAML and HTML, with indexes and a static website for use
in the licensedb web site. This becomes the API way to getr scancode license
data.
Expand Down
1 change: 1 addition & 0 deletions setup-mini.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ packages =
console_scripts =
scancode = scancode.cli:scancode
scancode-reindex-licenses = licensedcode.reindex:reindex_licenses
scancode-license-data = licensedcode.license_db:dump_scancode_license_data

# These are configurations for ScanCode plugins as setuptools entry points.
# Each plugin entry hast this form:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ packages =
console_scripts =
scancode = scancode.cli:scancode
scancode-reindex-licenses = licensedcode.reindex:reindex_licenses
scancode-license-data = licensedcode.license_db:dump_scancode_license_data

# These are configurations for ScanCode plugins as setuptools entry points.
# Each plugin entry hast this form:
Expand Down
39 changes: 31 additions & 8 deletions src/licensedcode/license_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
from os.path import join
from distutils.dir_util import copy_tree

import click
import saneyaml

from commoncode.cliutils import MISC_GROUP
from commoncode.cliutils import PluggableCommandLineOption
from jinja2 import Environment, FileSystemLoader
from licensedcode.models import load_licenses
from licensedcode.models import licenses_data_dir
Expand Down Expand Up @@ -200,19 +204,38 @@ def generate(
return count


def dump_license_data(ctx, param, value):
def scancode_license_data(path):
"""
Dump license data from scancode licenses to the directory ``value`` passed
in from command line.
Dumps data in JSON, YAML and HTML formats and also dumps the .LICENSE file
with the license text and the data as YAML frontmatter.
"""
if not value or ctx.resilient_parsing:
return

import click
click.secho(f'Dumping license data to: {value}', err=True)
count = generate(build_location=value)
click.secho(f'Dumping license data to: {path}', err=True)
count = generate(build_location=path)
click.secho(f'Done dumping #{count} licenses.', err=True)
ctx.exit(0)


@click.command(name='scancode-license-data')
@click.option(
'--path',
type=click.Path(exists=False, readable=True, file_okay=False, resolve_path=True, path_type=str),
metavar='DIR',
help='Dump the license data in this directory in the LicenseDB format and exit. '
'Creates the directory if it does not exist. ',
help_group=MISC_GROUP,
cls=PluggableCommandLineOption,
)
@click.help_option('-h', '--help')
def dump_scancode_license_data(
path,
*args,
**kwargs,
):
"""Reindex scancode licenses and exit"""
scancode_license_data(path=path)


if __name__ == '__main__':
dump_scancode_license_data()
17 changes: 1 addition & 16 deletions src/licensedcode/plugin_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
from functools import partial

import attr
import click
from commoncode.cliutils import PluggableCommandLineOption
from commoncode.cliutils import SCAN_GROUP
from commoncode.cliutils import SCAN_OPTIONS_GROUP
from commoncode.cliutils import MISC_GROUP
from plugincode.scan import ScanPlugin
from plugincode.scan import scan_impl

Expand All @@ -33,7 +31,6 @@
from licensedcode.detection import LicenseDetectionFromResult
from licensedcode.detection import LicenseMatchFromResult
from licensedcode.detection import UniqueDetection
from licensedcode.license_db import dump_license_data
from packagedcode.utils import combine_expressions
from scancode.api import SCANCODE_LICENSEDB_URL

Expand Down Expand Up @@ -121,19 +118,7 @@ class LicenseScanner(ScanPlugin):
required_options=['license'],
help='[EXPERIMENTAL] Detect unknown licenses. ',
help_group=SCAN_OPTIONS_GROUP,
),

# TODO: consider creating a separate comamnd line option exe instead
PluggableCommandLineOption(
('--dump-license-data',),
type=click.Path(exists=False, readable=True, file_okay=False, resolve_path=True, path_type=str),
metavar='DIR',
callback=dump_license_data,
help='Dump the license data in this directory in the LicenseDB format and exit. '
'Creates the directory if it does not exist. ',
help_group=MISC_GROUP,
is_eager=True,
),
)
]

def is_enabled(self, license, **kwargs): # NOQA
Expand Down
5 changes: 0 additions & 5 deletions tests/scancode/data/help/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ Options:
and including the starting directory. Use 0 for no
scan depth limit.

miscellaneous:
--dump-license-data DIR Dump the license data in this directory in the
LicenseDB format and exit. Creates the directory if
it does not exist.

documentation:
-h, --help Show this message and exit.
-A, --about Show information about ScanCode and licensing and exit.
Expand Down

0 comments on commit e964f6f

Please sign in to comment.