|
| 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.hash import binary_chunks |
| 15 | +from scancode_config import scancode_cache_dir |
| 16 | + |
| 17 | + |
| 18 | +RESOURCE_CACHE_DIR = os.path.join(scancode_cache_dir, "resource_cache_index") |
| 19 | + |
| 20 | + |
| 21 | +def hasher_from_chunks(chunks): |
| 22 | + """ |
| 23 | + Return a sha256 hasher loaded with `chunks`. |
| 24 | + """ |
| 25 | + hasher = hashlib.sha256() |
| 26 | + for chunk in chunks: |
| 27 | + hasher.update(chunk) |
| 28 | + return hasher |
| 29 | + |
| 30 | + |
| 31 | +def compute_resource_cache_index(location, path): |
| 32 | + """ |
| 33 | + Compute resource_cache_index value for Resource at `location`. |
| 34 | + """ |
| 35 | + chunks = binary_chunks(location=location) |
| 36 | + sha256_hasher = hasher_from_chunks(chunks=chunks) |
| 37 | + # TODO: consider using filename instead of path |
| 38 | + sha256_hasher.update(path.encode('utf-8', 'surrogateescape')) |
| 39 | + return sha256_hasher.hexdigest() |
| 40 | + |
| 41 | + |
| 42 | +def get_resource_cache_directory_location(resource_cache_index): |
| 43 | + """ |
| 44 | + Return the location of the directory containing the cache files for a given |
| 45 | + `resource_cache_index` hexstring. |
| 46 | + """ |
| 47 | + # Split the hash into two subdirectories using the first two prefix pairs |
| 48 | + prefix1 = resource_cache_index[:2] |
| 49 | + prefix2 = resource_cache_index[2:4] |
| 50 | + directory_name = resource_cache_index[4:] |
| 51 | + return os.path.join(RESOURCE_CACHE_DIR, prefix1, prefix2, directory_name) |
| 52 | + |
| 53 | + |
| 54 | +def get_resource_cache_file_location(resource_cache_index, plugin_name): |
| 55 | + """ |
| 56 | + Return the location of the file containing the cached results of the scanner |
| 57 | + `plugin_name` for a resource keyed by `resource_cache_index` hexstring. |
| 58 | + """ |
| 59 | + resource_cache_directory_location = get_resource_cache_directory_location(resource_cache_index=resource_cache_index) |
| 60 | + return os.path.join(resource_cache_directory_location, plugin_name) |
| 61 | + |
| 62 | + |
| 63 | +def get_resource_cache_data(resource_cache_index, plugin_name): |
| 64 | + """ |
| 65 | + Return a mapping containing the results of scan plugin, `plugin_name`, for a |
| 66 | + resource keyed by `resource_cache_index` hexstring. If the cache file does |
| 67 | + not exist, an empty mapping is returned. |
| 68 | + """ |
| 69 | + resource_cache_file_location = get_resource_cache_file_location( |
| 70 | + resource_cache_index=resource_cache_index, |
| 71 | + plugin_name=plugin_name |
| 72 | + ) |
| 73 | + if os.path.exists(resource_cache_file_location): |
| 74 | + with open(resource_cache_file_location) as f: |
| 75 | + return json.load(f) |
| 76 | + else: |
| 77 | + return {} |
| 78 | + |
| 79 | + |
| 80 | +def update_resource_cache_data(resource_cache_index, plugin_name, results): |
| 81 | + """ |
| 82 | + Update the resource cache with the `results` of the scanner `plugin_name` |
| 83 | + for the resource keyed by `resource_cache_index`. |
| 84 | + """ |
| 85 | + resource_cache_file_location = get_resource_cache_file_location( |
| 86 | + resource_cache_index=resource_cache_index, |
| 87 | + plugin_name=plugin_name |
| 88 | + ) |
| 89 | + with open(resource_cache_file_location, 'w') as f: |
| 90 | + json.dump(results, f) |
0 commit comments