Skip to content

Commit 6f0150d

Browse files
committed
Create directories before opening or creating cache files
* Create envvar for controlling whether or not the scan results cache is used Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 86ecf42 commit 6f0150d

4 files changed

Lines changed: 30 additions & 21 deletions

File tree

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ packages = "packagedcode.plugin_package:PackageScanner"
276276
emails = "cluecode.plugin_email:EmailScanner"
277277
urls = "cluecode.plugin_url:UrlScanner"
278278
generated = "summarycode.generated:GeneratedCodeDetector"
279-
resource_cache_index = "scancode.plugin_resource_cache:ResourceCacheIndexScanner"
280279

281280

282281
# scancode_post_scan is the entry point for post_scan plugins executed after the

src/scancode/cli.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class WindowsError(Exception):
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
7576

7677
# Tracing flags
7778
TRACE = False
@@ -1445,21 +1446,22 @@ def scan_resource(
14451446
# compute resource_cache_index
14461447
resource_cache_index = resource_cache.compute_resource_cache_index(location=location, path=path)
14471448

1448-
# update `results` with cached data or add scanner to scanners_to_run if no
1449-
# cache data is available
1450-
for scanner in scanners:
1451-
# get resource_cache_data
1452-
resource_cache_data = resource_cache.get_resource_cache_data(
1453-
resource_cache_index=resource_cache_index,
1454-
plugin_name=scanner.name
1455-
)
1456-
if resource_cache_data:
1457-
results.update(resource_cache_data)
1458-
else:
1459-
scanners_to_run.append(scanner)
1449+
if USE_CACHED_RESULTS:
1450+
# update `results` with cached data or add scanner to scanners_to_run if no
1451+
# cache data is available
1452+
for scanner in scanners:
1453+
# get resource_cache_data
1454+
resource_cache_data = resource_cache.get_resource_cache_data(
1455+
resource_cache_index=resource_cache_index,
1456+
plugin_name=scanner.name
1457+
)
1458+
if resource_cache_data:
1459+
results.update(resource_cache_data)
1460+
else:
1461+
scanners_to_run.append(scanner)
14601462

14611463
# run each scanner in sequence in its own interruptible
1462-
for scanner in scanners_to_run:
1464+
for scanner in scanners_to_run or scanners:
14631465
if with_timing:
14641466
start = time()
14651467

@@ -1478,10 +1480,12 @@ def scan_resource(
14781480
# the return value of a scanner fun MUST be a mapping
14791481
if values_mapping:
14801482
results.update(values_mapping)
1481-
resource_cache.update_resource_cache_data(
1482-
resource_cache_index=resource_cache_index,
1483-
plugin_name=scanner.name
1484-
)
1483+
if USE_CACHED_RESULTS:
1484+
resource_cache.update_resource_cache_data(
1485+
resource_cache_index=resource_cache_index,
1486+
plugin_name=scanner.name,
1487+
results=values_mapping,
1488+
)
14851489

14861490
except Exception:
14871491
msg = 'ERROR: for scanner: ' + scanner.name + ':\n' + traceback.format_exc()

src/scancode/resource_cache.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import json
1212
import os
1313

14+
from commoncode.fileutils import create_dir
1415
from commoncode.hash import binary_chunks
1516
from scancode_config import scancode_cache_dir
1617

@@ -82,9 +83,11 @@ def update_resource_cache_data(resource_cache_index, plugin_name, results):
8283
Update the resource cache with the `results` of the scanner `plugin_name`
8384
for the resource keyed by `resource_cache_index`.
8485
"""
85-
resource_cache_file_location = get_resource_cache_file_location(
86-
resource_cache_index=resource_cache_index,
87-
plugin_name=plugin_name
86+
resource_cache_directory_location = get_resource_cache_directory_location(
87+
resource_cache_index=resource_cache_index
8888
)
89+
if not os.path.exists(resource_cache_directory_location):
90+
create_dir(resource_cache_directory_location)
91+
resource_cache_file_location = os.path.join(resource_cache_directory_location, plugin_name)
8992
with open(resource_cache_file_location, 'w') as f:
9093
json.dump(results, f)

src/scancode_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,6 @@ def _create_dir(location):
221221

222222
# Used for tests to regenerate fixtures with regen=True
223223
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)