Skip to content

Commit 6412039

Browse files
Add external licenses info in header
Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent a477e54 commit 6412039

File tree

15 files changed

+129
-167
lines changed

15 files changed

+129
-167
lines changed

azure-pipelines.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ jobs:
3333
--ignore=tests/licensedcode/test_detection_datadriven2.py \
3434
--ignore=tests/licensedcode/test_detection_datadriven3.py \
3535
--ignore=tests/licensedcode/test_detection_datadriven4.py
36-
tests/licensedcode
3736
3837
license_datadriven1_2: |
3938
venv/bin/pytest -n 3 -vvs --test-suite=all \

src/licensedcode/cache.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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
910
import os
1011
import pickle
1112

@@ -35,7 +36,6 @@
3536
LICENSE_INDEX_FILENAME = 'index_cache'
3637
LICENSE_LOCKFILE_NAME = 'scancode_license_index_lockfile'
3738
LICENSE_CHECKSUM_FILE = 'scancode_license_index_tree_checksums'
38-
CACHED_DIRECTORIES_FILENAME = 'cached_directories'
3939

4040

4141
@attr.s(slots=True)
@@ -48,6 +48,8 @@ class LicenseCache:
4848
licensing = attribute(help='Licensing object')
4949
spdx_symbols = attribute(help='mapping of LicenseSymbol objects by SPDX key')
5050
unknown_spdx_symbol = attribute(help='LicenseSymbol object')
51+
additional_license_directory = attribute(help='Path to an additional license directory used in the license detection')
52+
additional_license_plugins = attribute(help='Path to additional license plugins used in the license detection')
5153

5254
@staticmethod
5355
def load_or_build(
@@ -72,8 +74,11 @@ def load_or_build(
7274
directories containing licenses that are not present in the existing cache.
7375
- If the cache does not exist, a new index is built and cached.
7476
- If ``index_all_languages`` is True, include texts in all languages when
75-
building the license index. Otherwise, only include the English license \
77+
building the license index. Otherwise, only include the English license
7678
texts and rules (the default)
79+
- ``additional_directory`` is an optional additional directory
80+
that contain additional licenses and rules in a /licenses and a /rules
81+
directories using the same format that we use for licenses and rules.
7782
"""
7883
idx_cache_dir = os.path.join(licensedcode_cache_dir, LICENSE_INDEX_DIR)
7984
create_dir(idx_cache_dir)
@@ -114,26 +119,25 @@ def load_or_build(
114119
# Here, the cache is either stale or non-existing: we need to
115120
# rebuild all cached data (e.g. mostly the index) and cache it
116121

117-
additional_directories = get_paths_to_installed_licenses_and_rules()
122+
additional_directories = []
123+
plugin_directories = get_paths_to_installed_licenses_and_rules()
124+
if plugin_directories:
125+
additional_directories.extend(plugin_directories)
126+
118127
# include installed licenses
119128
if additional_directory:
120129
# additional_directories is originally a tuple
121130
additional_directories.append(additional_directory)
122131

123-
# persist additional directories as a file so that we can include it in the scancode output as extra info
124-
# only persist when we're generating a new license cache
125-
idx_cache_dir = os.path.join(licensedcode_cache_dir, LICENSE_INDEX_DIR)
126-
cached_directories_file = os.path.join(idx_cache_dir, CACHED_DIRECTORIES_FILENAME)
127-
with open(cached_directories_file, 'wb') as file:
128-
pickle.dump(additional_directories, file, protocol=PICKLE_PROTOCOL)
129-
130132
additional_license_dirs = get_license_dirs(additional_dirs=additional_directories)
131-
validate_additional_license_data(additional_directories=additional_license_dirs, scancode_license_dir=licenses_data_dir)
132-
combined_directories = [licenses_data_dir] + additional_license_dirs
133-
licenses_db = load_licenses_from_multiple_dirs(
134-
license_directories=combined_directories,
133+
validate_additional_license_data(
134+
additional_directories=additional_license_dirs,
135135
scancode_license_dir=licenses_data_dir
136136
)
137+
licenses_db = load_licenses_from_multiple_dirs(
138+
additional_license_data_dirs=additional_license_dirs,
139+
builtin_license_data_dir=licenses_data_dir,
140+
)
137141

138142
# create a single merged index containing license data from licenses_data_dir
139143
# and data from additional directories
@@ -142,7 +146,7 @@ def load_or_build(
142146
licenses_data_dir=licenses_data_dir,
143147
rules_data_dir=rules_data_dir,
144148
index_all_languages=index_all_languages,
145-
additional_directories=additional_directories,
149+
additional_directories=plugin_directories,
146150
)
147151

148152
spdx_symbols = build_spdx_symbols(licenses_db=licenses_db)
@@ -155,6 +159,8 @@ def load_or_build(
155159
licensing=licensing,
156160
spdx_symbols=spdx_symbols,
157161
unknown_spdx_symbol=unknown_spdx_symbol,
162+
additional_license_directory=additional_directory,
163+
additional_license_plugins=plugin_directories,
158164
)
159165

160166
# save the cache as pickle new tree checksum
@@ -186,13 +192,11 @@ def build_index(
186192
from licensedcode.index import LicenseIndex
187193
from licensedcode.models import get_license_dirs
188194
from licensedcode.models import get_rule_dirs
189-
from licensedcode.models import get_rules
190195
from licensedcode.models import get_rules_from_multiple_dirs
191196
from licensedcode.models import get_all_spdx_key_tokens
192197
from licensedcode.models import get_license_tokens
193198
from licensedcode.models import licenses_data_dir as ldd
194199
from licensedcode.models import rules_data_dir as rdd
195-
from licensedcode.models import load_licenses
196200
from licensedcode.models import load_licenses_from_multiple_dirs
197201
from licensedcode.models import validate_ignorable_clues
198202
from licensedcode.legalese import common_license_words
@@ -211,11 +215,10 @@ def build_index(
211215
additional_rule_dirs = get_rule_dirs(additional_dirs=additional_directories)
212216
validate_ignorable_clues(rule_directories=additional_rule_dirs, is_builtin=False)
213217
# then combine the rules in these additional directories with the rules in the original rules directory
214-
combined_rule_directories = [rules_data_dir] + additional_rule_dirs
215218
rules = get_rules_from_multiple_dirs(
216219
licenses_db=licenses_db,
217-
rule_directories=combined_rule_directories,
218-
scancode_rules_dir=rules_data_dir
220+
additional_rules_data_dirs=additional_rule_dirs,
221+
builtin_rule_data_dir=rules_data_dir,
219222
)
220223

221224
legalese = common_license_words
@@ -360,14 +363,16 @@ def get_cache(force=False, index_all_languages=False, additional_directory=None)
360363
building the license index. Otherwise, only include the English license \
361364
texts and rules (the default)
362365
"""
363-
populate_cache(force=force, index_all_languages=index_all_languages, additional_directory=additional_directory)
364-
global _LICENSE_CACHE
365-
return _LICENSE_CACHE
366+
return populate_cache(
367+
force=force,
368+
index_all_languages=index_all_languages,
369+
additional_directory=additional_directory,
370+
)
366371

367372

368373
def populate_cache(force=False, index_all_languages=False, additional_directory=None):
369374
"""
370-
Load or build and cache a LicenseCache. Return None.
375+
Return, load or build and cache a LicenseCache.
371376
"""
372377
global _LICENSE_CACHE
373378

@@ -381,6 +386,7 @@ def populate_cache(force=False, index_all_languages=False, additional_directory=
381386
timeout=LICENSE_INDEX_LOCK_TIMEOUT,
382387
additional_directory=additional_directory,
383388
)
389+
return _LICENSE_CACHE
384390

385391

386392
def load_cache_file(cache_file):

0 commit comments

Comments
 (0)