Skip to content

Commit 65bb925

Browse files
Add --only-builtin falg for scancode-reindex-licenses
Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 044f60d commit 65bb925

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

src/licensedcode/cache.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
# See https://github.com/nexB/scancode-toolkit for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
9-
import click
9+
1010
import os
1111
import pickle
12+
from shutil import rmtree
1213

1314
import attr
1415

@@ -53,6 +54,7 @@ class LicenseCache:
5354

5455
@staticmethod
5556
def load_or_build(
57+
only_builtin=False,
5658
licensedcode_cache_dir=licensedcode_cache_dir,
5759
scancode_cache_dir=scancode_cache_dir,
5860
force=False,
@@ -81,6 +83,9 @@ def load_or_build(
8183
directories using the same format that we use for licenses and rules.
8284
"""
8385
idx_cache_dir = os.path.join(licensedcode_cache_dir, LICENSE_INDEX_DIR)
86+
if only_builtin:
87+
rmtree(idx_cache_dir)
88+
8489
create_dir(idx_cache_dir)
8590
cache_file = os.path.join(idx_cache_dir, LICENSE_INDEX_FILENAME)
8691

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

122-
additional_directories = []
123-
plugin_directories = get_paths_to_installed_licenses_and_rules()
124-
if plugin_directories:
125-
additional_directories.extend(plugin_directories)
127+
if only_builtin:
128+
additional_directory = None
129+
additional_directories = []
130+
plugin_directories = []
131+
else:
132+
plugin_directories = get_paths_to_installed_licenses_and_rules()
133+
if plugin_directories:
134+
additional_directories.extend(plugin_directories)
126135

127-
# include installed licenses
128-
if additional_directory:
129-
# additional_directories is originally a tuple
130-
additional_directories.append(additional_directory)
136+
# include installed licenses
137+
if additional_directory:
138+
# additional_directories is originally a tuple
139+
additional_directories.append(additional_directory)
131140

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

357366

358-
def get_cache(force=False, index_all_languages=False, additional_directory=None):
367+
def get_cache(
368+
only_builtin=False,
369+
force=False,
370+
index_all_languages=False,
371+
additional_directory=None
372+
):
359373
"""
360374
Return a LicenseCache either rebuilt, cached or loaded from disk.
361375
@@ -364,20 +378,27 @@ def get_cache(force=False, index_all_languages=False, additional_directory=None)
364378
texts and rules (the default)
365379
"""
366380
return populate_cache(
381+
only_builtin=only_builtin,
367382
force=force,
368383
index_all_languages=index_all_languages,
369384
additional_directory=additional_directory,
370385
)
371386

372387

373-
def populate_cache(force=False, index_all_languages=False, additional_directory=None):
388+
def populate_cache(
389+
only_builtin=False,
390+
force=False,
391+
index_all_languages=False,
392+
additional_directory=None
393+
):
374394
"""
375395
Return, load or build and cache a LicenseCache.
376396
"""
377397
global _LICENSE_CACHE
378398

379399
if force or not _LICENSE_CACHE:
380400
_LICENSE_CACHE = LicenseCache.load_or_build(
401+
only_builtin=only_builtin,
381402
licensedcode_cache_dir=licensedcode_cache_dir,
382403
scancode_cache_dir=scancode_cache_dir,
383404
force=force,
@@ -407,11 +428,17 @@ def load_cache_file(cache_file):
407428
raise Exception(msg) from e
408429

409430

410-
def get_index(force=False, index_all_languages=False, additional_directory=None):
431+
def get_index(
432+
only_builtin=False,
433+
force=False,
434+
index_all_languages=False,
435+
additional_directory=None
436+
):
411437
"""
412438
Return and eventually build and cache a LicenseIndex.
413439
"""
414440
return get_cache(
441+
only_builtin=only_builtin,
415442
force=force,
416443
index_all_languages=index_all_languages,
417444
additional_directory=additional_directory

src/licensedcode/reindex.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,39 @@
99

1010
import click
1111

12+
from commoncode.cliutils import PluggableCommandLineOption
13+
1214

1315
@click.command(name='scancode-reindex-licenses')
1416
@click.option(
1517
'--all-languages',
1618
is_flag=True,
1719
help='[EXPERIMENTAL] Rebuild the license index including texts all '
1820
'languages (and not only English) and exit.',
21+
cls=PluggableCommandLineOption,
22+
)
23+
@click.option(
24+
'--only-builtin',
25+
is_flag=True,
26+
help='Rebuild the license index excluding any additional '
27+
'license directory or additional license plugins which'
28+
'were added previously, i.e. with only builtin scancode '
29+
'license and rules.',
30+
conflicting_options=['additional_directory'],
31+
cls=PluggableCommandLineOption,
1932
)
2033
@click.option(
2134
'--additional-directory',
2235
type=click.Path(exists=True, readable=True, file_okay=False, resolve_path=True, path_type=str),
2336
metavar='DIR',
2437
help='Include this directory with additional custom licenses and license rules '
2538
'in the license detection index.',
39+
conflicting_options=['only_builtin'],
40+
cls=PluggableCommandLineOption,
2641
)
2742
@click.help_option('-h', '--help')
2843
def reindex_licenses(
44+
only_builtin,
2945
all_languages,
3046
additional_directory,
3147
*args,
@@ -35,7 +51,12 @@ def reindex_licenses(
3551

3652
from licensedcode.cache import get_index
3753
click.echo('Rebuilding the license index...')
38-
get_index(force=True, index_all_languages=bool(all_languages), additional_directory=additional_directory)
54+
get_index(
55+
only_builtin=only_builtin,
56+
force=True,
57+
index_all_languages=bool(all_languages),
58+
additional_directory=additional_directory
59+
)
3960
click.echo('Done.')
4061

4162

0 commit comments

Comments
 (0)