6
6
# See https://github.com/nexB/scancode-toolkit for support or download.
7
7
# See https://aboutcode.org for more information about nexB OSS projects.
8
8
#
9
+ import click
9
10
import os
10
11
import pickle
11
12
35
36
LICENSE_INDEX_FILENAME = 'index_cache'
36
37
LICENSE_LOCKFILE_NAME = 'scancode_license_index_lockfile'
37
38
LICENSE_CHECKSUM_FILE = 'scancode_license_index_tree_checksums'
38
- CACHED_DIRECTORIES_FILENAME = 'cached_directories'
39
39
40
40
41
41
@attr .s (slots = True )
@@ -48,6 +48,8 @@ class LicenseCache:
48
48
licensing = attribute (help = 'Licensing object' )
49
49
spdx_symbols = attribute (help = 'mapping of LicenseSymbol objects by SPDX key' )
50
50
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' )
51
53
52
54
@staticmethod
53
55
def load_or_build (
@@ -72,8 +74,11 @@ def load_or_build(
72
74
directories containing licenses that are not present in the existing cache.
73
75
- If the cache does not exist, a new index is built and cached.
74
76
- 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
76
78
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.
77
82
"""
78
83
idx_cache_dir = os .path .join (licensedcode_cache_dir , LICENSE_INDEX_DIR )
79
84
create_dir (idx_cache_dir )
@@ -114,26 +119,25 @@ def load_or_build(
114
119
# Here, the cache is either stale or non-existing: we need to
115
120
# rebuild all cached data (e.g. mostly the index) and cache it
116
121
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
+
118
127
# include installed licenses
119
128
if additional_directory :
120
129
# additional_directories is originally a tuple
121
130
additional_directories .append (additional_directory )
122
131
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
-
130
132
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 ,
135
135
scancode_license_dir = licenses_data_dir
136
136
)
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
+ )
137
141
138
142
# create a single merged index containing license data from licenses_data_dir
139
143
# and data from additional directories
@@ -142,7 +146,7 @@ def load_or_build(
142
146
licenses_data_dir = licenses_data_dir ,
143
147
rules_data_dir = rules_data_dir ,
144
148
index_all_languages = index_all_languages ,
145
- additional_directories = additional_directories ,
149
+ additional_directories = plugin_directories ,
146
150
)
147
151
148
152
spdx_symbols = build_spdx_symbols (licenses_db = licenses_db )
@@ -155,6 +159,8 @@ def load_or_build(
155
159
licensing = licensing ,
156
160
spdx_symbols = spdx_symbols ,
157
161
unknown_spdx_symbol = unknown_spdx_symbol ,
162
+ additional_license_directory = additional_directory ,
163
+ additional_license_plugins = plugin_directories ,
158
164
)
159
165
160
166
# save the cache as pickle new tree checksum
@@ -211,11 +217,10 @@ def build_index(
211
217
additional_rule_dirs = get_rule_dirs (additional_dirs = additional_directories )
212
218
validate_ignorable_clues (rule_directories = additional_rule_dirs , is_builtin = False )
213
219
# 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
215
220
rules = get_rules_from_multiple_dirs (
216
221
licenses_db = licenses_db ,
217
- rule_directories = combined_rule_directories ,
218
- scancode_rules_dir = rules_data_dir
222
+ additional_rules_data_dirs = additional_rule_dirs ,
223
+ builtin_rule_data_dir = rules_data_dir ,
219
224
)
220
225
221
226
legalese = common_license_words
@@ -360,14 +365,16 @@ def get_cache(force=False, index_all_languages=False, additional_directory=None)
360
365
building the license index. Otherwise, only include the English license \
361
366
texts and rules (the default)
362
367
"""
363
- populate_cache (force = force , index_all_languages = index_all_languages , additional_directory = additional_directory )
364
- global _LICENSE_CACHE
365
- return _LICENSE_CACHE
368
+ return populate_cache (
369
+ force = force ,
370
+ index_all_languages = index_all_languages ,
371
+ additional_directory = additional_directory ,
372
+ )
366
373
367
374
368
375
def populate_cache (force = False , index_all_languages = False , additional_directory = None ):
369
376
"""
370
- Load or build and cache a LicenseCache. Return None .
377
+ Return, load or build and cache a LicenseCache.
371
378
"""
372
379
global _LICENSE_CACHE
373
380
@@ -381,6 +388,7 @@ def populate_cache(force=False, index_all_languages=False, additional_directory=
381
388
timeout = LICENSE_INDEX_LOCK_TIMEOUT ,
382
389
additional_directory = additional_directory ,
383
390
)
391
+ return _LICENSE_CACHE
384
392
385
393
386
394
def load_cache_file (cache_file ):
0 commit comments