Skip to content

Commit 097d73a

Browse files
committed
Rename resource_cache_index to results_cache_index
Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 6f0150d commit 097d73a

4 files changed

Lines changed: 122 additions & 108 deletions

File tree

src/scancode/cli.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ class WindowsError(Exception):
6565
from scancode import notice
6666
from scancode import print_about
6767
from scancode import Scanner
68-
from scancode import resource_cache
68+
from scancode import results_cache
6969
from scancode.help import epilog_text
7070
from scancode.help import examples_text
7171
from scancode.interrupt import DEFAULT_TIMEOUT
7272
from scancode.interrupt import fake_interruptible
7373
from scancode.interrupt import interruptible
7474
from scancode.pool import ScanCodeTimeoutError
75-
from scancode_config import USE_CACHED_RESULTS
7675

7776
# Tracing flags
7877
TRACE = False
@@ -394,6 +393,13 @@ def default_processes():
394393
# not yet supported in Click 6.7 but added in PluggableCommandLineOption
395394
hidden=True,
396395
help_group=cliutils.MISC_GROUP, sort_order=1000, cls=PluggableCommandLineOption)
396+
397+
@click.option('--no-cached-results',
398+
is_flag=True,
399+
default=False,
400+
hidden=True,
401+
help='ScanCode will not use cached results during scan time.',
402+
help_group=cliutils.CORE_GROUP, sort_order=250, cls=PluggableCommandLineOption)
397403
def scancode(
398404
ctx,
399405
input, # NOQA
@@ -412,6 +418,7 @@ def scancode(
412418
test_error_mode,
413419
keep_temp_files,
414420
check_version,
421+
no_cached_results,
415422
echo_func=echo_stderr,
416423
*args,
417424
**kwargs,
@@ -526,6 +533,7 @@ def scancode(
526533
return_results=False,
527534
echo_func=echo_func,
528535
outdated=outdated,
536+
use_cached_results=not no_cached_results,
529537
*args,
530538
**kwargs
531539
)
@@ -569,6 +577,7 @@ def run_scan(
569577
pretty_params=None,
570578
plugin_options=plugin_options,
571579
outdated=None,
580+
use_cached_results=False,
572581
*args,
573582
**kwargs
574583
):
@@ -973,6 +982,7 @@ def echo_func(*_args, **_kwargs):
973982
verbose=verbose,
974983
kwargs=requested_options,
975984
echo_func=echo_func,
985+
use_cached_results=use_cached_results,
976986
)
977987
success = success and scan_success
978988

@@ -1165,6 +1175,7 @@ def run_scanners(
11651175
verbose=False,
11661176
kwargs=None,
11671177
echo_func=echo_stderr,
1178+
use_cached_results=False,
11681179
):
11691180
"""
11701181
Run the list of `stage` ScanPlugin `plugins` on `codebase`.
@@ -1206,7 +1217,8 @@ def run_scanners(
12061217
# TODO: add CLI option to bypass cache entirely?
12071218
scan_success = scan_codebase(
12081219
codebase, scanners, processes, timeout,
1209-
with_timing=timing, progress_manager=progress_manager)
1220+
with_timing=timing, progress_manager=progress_manager,
1221+
use_cached_results=use_cached_results)
12101222

12111223
# TODO: add progress indicator
12121224
# run the process codebase of each scan plugin (most often a no-op)
@@ -1244,6 +1256,7 @@ def scan_codebase(
12441256
with_timing=False,
12451257
progress_manager=None,
12461258
echo_func=echo_stderr,
1259+
use_cached_results=False,
12471260
):
12481261
"""
12491262
Run the `scanners` Scanner objects on the `codebase` Codebase. Return True
@@ -1273,7 +1286,8 @@ def scan_codebase(
12731286
scanners=scanners,
12741287
timeout=timeout,
12751288
with_timing=with_timing,
1276-
with_threading=use_threading
1289+
with_threading=use_threading,
1290+
use_cached_results=use_cached_results,
12771291
)
12781292

12791293
if TRACE:
@@ -1406,6 +1420,7 @@ def scan_resource(
14061420
timeout=DEFAULT_TIMEOUT,
14071421
with_timing=False,
14081422
with_threading=True,
1423+
use_cached_results=False,
14091424
):
14101425
"""
14111426
Given a ``location_path`` tuple pf (location, path), return a tuple of:
@@ -1443,16 +1458,17 @@ def scan_resource(
14431458
# and start returning values. The kill timeout is otherwise there
14441459
# as a gatekeeper for runaway processes.
14451460

1446-
# compute resource_cache_index
1447-
resource_cache_index = resource_cache.compute_resource_cache_index(location=location, path=path)
1461+
results_cache_index = ''
1462+
if use_cached_results:
1463+
# compute results_cache_index
1464+
results_cache_index = results_cache.compute_results_cache_index(location=location, path=path)
14481465

1449-
if USE_CACHED_RESULTS:
14501466
# update `results` with cached data or add scanner to scanners_to_run if no
14511467
# cache data is available
14521468
for scanner in scanners:
14531469
# get resource_cache_data
1454-
resource_cache_data = resource_cache.get_resource_cache_data(
1455-
resource_cache_index=resource_cache_index,
1470+
resource_cache_data = results_cache.get_results_cache_data(
1471+
results_cache_index=results_cache_index,
14561472
plugin_name=scanner.name
14571473
)
14581474
if resource_cache_data:
@@ -1480,9 +1496,9 @@ def scan_resource(
14801496
# the return value of a scanner fun MUST be a mapping
14811497
if values_mapping:
14821498
results.update(values_mapping)
1483-
if USE_CACHED_RESULTS:
1484-
resource_cache.update_resource_cache_data(
1485-
resource_cache_index=resource_cache_index,
1499+
if use_cached_results:
1500+
results_cache.update_results_cache_data(
1501+
results_cache_index=results_cache_index,
14861502
plugin_name=scanner.name,
14871503
results=values_mapping,
14881504
)

src/scancode/resource_cache.py

Lines changed: 0 additions & 93 deletions
This file was deleted.

src/scancode/results_cache.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# ScanCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/scancode-toolkit for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import hashlib
11+
import json
12+
import os
13+
14+
from commoncode.fileutils import create_dir
15+
from commoncode.hash import binary_chunks
16+
from scancode_config import results_cache_dir
17+
18+
19+
def hasher_from_chunks(chunks):
20+
"""
21+
Return a sha256 hasher loaded with `chunks`.
22+
"""
23+
hasher = hashlib.sha256()
24+
for chunk in chunks:
25+
hasher.update(chunk)
26+
return hasher
27+
28+
29+
def compute_results_cache_index(location, path):
30+
"""
31+
Compute results_cache_index value for a Resource at `location`.
32+
"""
33+
chunks = binary_chunks(location=location)
34+
sha256_hasher = hasher_from_chunks(chunks=chunks)
35+
# TODO: consider using filename instead of path
36+
sha256_hasher.update(path.encode('utf-8', 'surrogateescape'))
37+
return sha256_hasher.hexdigest()
38+
39+
40+
def get_results_cache_directory_location(results_cache_index):
41+
"""
42+
Return the location of the directory containing the cache files for a given
43+
`results_cache_index` hexstring.
44+
"""
45+
# Split the hash into two subdirectories using the first two prefix pairs
46+
prefix1 = results_cache_index[:2]
47+
prefix2 = results_cache_index[2:4]
48+
directory_name = results_cache_index[4:]
49+
return os.path.join(results_cache_dir, prefix1, prefix2, directory_name)
50+
51+
52+
def get_results_cache_file_location(results_cache_index, plugin_name):
53+
"""
54+
Return the location of the file containing the cached results of the scanner
55+
`plugin_name` for a resource keyed by `results_cache_index` hexstring.
56+
"""
57+
results_cache_directory_location = get_results_cache_directory_location(results_cache_index=results_cache_index)
58+
return os.path.join(results_cache_directory_location, plugin_name)
59+
60+
61+
def get_results_cache_data(results_cache_index, plugin_name):
62+
"""
63+
Return a mapping containing the results of scan plugin, `plugin_name`, for a
64+
resource keyed by `resource_cache_index` hexstring. If the cache file does
65+
not exist, an empty mapping is returned.
66+
"""
67+
results_cache_file_location = get_results_cache_file_location(
68+
results_cache_index=results_cache_index,
69+
plugin_name=plugin_name
70+
)
71+
if os.path.exists(results_cache_file_location):
72+
with open(results_cache_file_location) as f:
73+
return json.load(f)
74+
else:
75+
return {}
76+
77+
78+
def update_results_cache_data(results_cache_index, plugin_name, results):
79+
"""
80+
Update the results cache with the `results` of the scanner `plugin_name`
81+
for the resource keyed by `results_cache_index`.
82+
"""
83+
results_cache_directory_location = get_results_cache_directory_location(
84+
results_cache_index=results_cache_index
85+
)
86+
if not os.path.exists(results_cache_directory_location):
87+
create_dir(results_cache_directory_location)
88+
results_cache_file_location = os.path.join(results_cache_directory_location, plugin_name)
89+
with open(results_cache_file_location, 'w') as f:
90+
json.dump(results, f)

src/scancode_config.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,14 @@ def _create_dir(location):
190190
__env_package_cache_dir = os.getenv('SCANCODE_PACKAGE_INDEX_CACHE')
191191
packagedcode_cache_dir = (__env_package_cache_dir or std_package_cache_dir)
192192

193+
std_results_cache_dir = join(scancode_cache_dir, 'results')
194+
__env_results_cache_dir = os.getenv('SCANCODE_RESULTS_CACHE')
195+
results_cache_dir = (__env_results_cache_dir or std_results_cache_dir)
193196

194197
_create_dir(licensedcode_cache_dir)
195198
_create_dir(packagedcode_cache_dir)
196199
_create_dir(scancode_cache_dir)
200+
_create_dir(results_cache_dir)
197201

198202
# - scancode_temp_dir: for short-lived temporary files which are import- or run-
199203
# specific that may live for the duration of a function call or for the duration
@@ -221,6 +225,3 @@ def _create_dir(location):
221225

222226
# Used for tests to regenerate fixtures with regen=True
223227
REGEN_TEST_FIXTURES = SCANCODE_REGEN_TEST_FIXTURES = os.getenv('SCANCODE_REGEN_TEST_FIXTURES', False)
224-
225-
# Used to control whether or not we use cached results during scan time
226-
USE_CACHED_RESULTS = SCANCODE_USE_CACHED_RESULTS = os.getenv('SCANCODE_USE_CACHED_RESULTS', True)

0 commit comments

Comments
 (0)