Skip to content

Commit

Permalink
Add --only-builtin falg for scancode-reindex-licenses
Browse files Browse the repository at this point in the history
Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
  • Loading branch information
AyanSinhaMahapatra committed Oct 21, 2022
1 parent 044f60d commit 65bb925
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
51 changes: 39 additions & 12 deletions src/licensedcode/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import click

import os
import pickle
from shutil import rmtree

import attr

Expand Down Expand Up @@ -53,6 +54,7 @@ class LicenseCache:

@staticmethod
def load_or_build(
only_builtin=False,
licensedcode_cache_dir=licensedcode_cache_dir,
scancode_cache_dir=scancode_cache_dir,
force=False,
Expand Down Expand Up @@ -81,6 +83,9 @@ def load_or_build(
directories using the same format that we use for licenses and rules.
"""
idx_cache_dir = os.path.join(licensedcode_cache_dir, LICENSE_INDEX_DIR)
if only_builtin:
rmtree(idx_cache_dir)

create_dir(idx_cache_dir)
cache_file = os.path.join(idx_cache_dir, LICENSE_INDEX_FILENAME)

Expand Down Expand Up @@ -119,15 +124,19 @@ def load_or_build(
# Here, the cache is either stale or non-existing: we need to
# rebuild all cached data (e.g. mostly the index) and cache it

additional_directories = []
plugin_directories = get_paths_to_installed_licenses_and_rules()
if plugin_directories:
additional_directories.extend(plugin_directories)
if only_builtin:
additional_directory = None
additional_directories = []
plugin_directories = []
else:
plugin_directories = get_paths_to_installed_licenses_and_rules()
if plugin_directories:
additional_directories.extend(plugin_directories)

# include installed licenses
if additional_directory:
# additional_directories is originally a tuple
additional_directories.append(additional_directory)
# include installed licenses
if additional_directory:
# additional_directories is originally a tuple
additional_directories.append(additional_directory)

additional_license_dirs = get_license_dirs(additional_dirs=additional_directories)
validate_additional_license_data(
Expand Down Expand Up @@ -355,7 +364,12 @@ def build_unknown_spdx_symbol(licenses_db=None):
return LicenseSymbolLike(licenses_db['unknown-spdx'])


def get_cache(force=False, index_all_languages=False, additional_directory=None):
def get_cache(
only_builtin=False,
force=False,
index_all_languages=False,
additional_directory=None
):
"""
Return a LicenseCache either rebuilt, cached or loaded from disk.
Expand All @@ -364,20 +378,27 @@ def get_cache(force=False, index_all_languages=False, additional_directory=None)
texts and rules (the default)
"""
return populate_cache(
only_builtin=only_builtin,
force=force,
index_all_languages=index_all_languages,
additional_directory=additional_directory,
)


def populate_cache(force=False, index_all_languages=False, additional_directory=None):
def populate_cache(
only_builtin=False,
force=False,
index_all_languages=False,
additional_directory=None
):
"""
Return, load or build and cache a LicenseCache.
"""
global _LICENSE_CACHE

if force or not _LICENSE_CACHE:
_LICENSE_CACHE = LicenseCache.load_or_build(
only_builtin=only_builtin,
licensedcode_cache_dir=licensedcode_cache_dir,
scancode_cache_dir=scancode_cache_dir,
force=force,
Expand Down Expand Up @@ -407,11 +428,17 @@ def load_cache_file(cache_file):
raise Exception(msg) from e


def get_index(force=False, index_all_languages=False, additional_directory=None):
def get_index(
only_builtin=False,
force=False,
index_all_languages=False,
additional_directory=None
):
"""
Return and eventually build and cache a LicenseIndex.
"""
return get_cache(
only_builtin=only_builtin,
force=force,
index_all_languages=index_all_languages,
additional_directory=additional_directory
Expand Down
23 changes: 22 additions & 1 deletion src/licensedcode/reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,39 @@

import click

from commoncode.cliutils import PluggableCommandLineOption


@click.command(name='scancode-reindex-licenses')
@click.option(
'--all-languages',
is_flag=True,
help='[EXPERIMENTAL] Rebuild the license index including texts all '
'languages (and not only English) and exit.',
cls=PluggableCommandLineOption,
)
@click.option(
'--only-builtin',
is_flag=True,
help='Rebuild the license index excluding any additional '
'license directory or additional license plugins which'
'were added previously, i.e. with only builtin scancode '
'license and rules.',
conflicting_options=['additional_directory'],
cls=PluggableCommandLineOption,
)
@click.option(
'--additional-directory',
type=click.Path(exists=True, readable=True, file_okay=False, resolve_path=True, path_type=str),
metavar='DIR',
help='Include this directory with additional custom licenses and license rules '
'in the license detection index.',
conflicting_options=['only_builtin'],
cls=PluggableCommandLineOption,
)
@click.help_option('-h', '--help')
def reindex_licenses(
only_builtin,
all_languages,
additional_directory,
*args,
Expand All @@ -35,7 +51,12 @@ def reindex_licenses(

from licensedcode.cache import get_index
click.echo('Rebuilding the license index...')
get_index(force=True, index_all_languages=bool(all_languages), additional_directory=additional_directory)
get_index(
only_builtin=only_builtin,
force=True,
index_all_languages=bool(all_languages),
additional_directory=additional_directory
)
click.echo('Done.')


Expand Down

0 comments on commit 65bb925

Please sign in to comment.