Load or build and save and return a LicenseCache object. We either load a cached LicenseIndex or build and cache the index. On the side, we load cached or build license db, SPDX symbols and other license-related data structures. - If the cache exists, it is
(
only_builtin=False,
licensedcode_cache_dir=licensedcode_cache_dir,
scancode_cache_dir=scancode_cache_dir,
force=False,
index_all_languages=False,
# used for testing only
timeout=LICENSE_INDEX_LOCK_TIMEOUT,
licenses_data_dir=None,
rules_data_dir=None,
additional_directory=None,
)
| 67 | |
| 68 | @staticmethod |
| 69 | def load_or_build( |
| 70 | only_builtin=False, |
| 71 | licensedcode_cache_dir=licensedcode_cache_dir, |
| 72 | scancode_cache_dir=scancode_cache_dir, |
| 73 | force=False, |
| 74 | index_all_languages=False, |
| 75 | # used for testing only |
| 76 | timeout=LICENSE_INDEX_LOCK_TIMEOUT, |
| 77 | licenses_data_dir=None, |
| 78 | rules_data_dir=None, |
| 79 | additional_directory=None, |
| 80 | ): |
| 81 | """ |
| 82 | Load or build and save and return a LicenseCache object. |
| 83 | |
| 84 | We either load a cached LicenseIndex or build and cache the index. |
| 85 | On the side, we load cached or build license db, SPDX symbols and other |
| 86 | license-related data structures. |
| 87 | |
| 88 | - If the cache exists, it is returned unless corrupted. |
| 89 | - If ``force`` is True, or if the cache does not exist a new index is built |
| 90 | and cached. |
| 91 | - If ``index_all_languages`` is True, include texts in all languages when |
| 92 | building the license index. Otherwise, only include the English license |
| 93 | texts and rules (the default) |
| 94 | - ``additional_directory`` is an optional additional directory |
| 95 | that contain additional licenses and rules in a /licenses and a /rules |
| 96 | directories using the same format that we use for licenses and rules. |
| 97 | """ |
| 98 | idx_cache_dir = os.path.join(licensedcode_cache_dir, LICENSE_INDEX_DIR) |
| 99 | if only_builtin: |
| 100 | rmtree(idx_cache_dir) |
| 101 | |
| 102 | create_dir(idx_cache_dir) |
| 103 | cache_file = os.path.join(idx_cache_dir, LICENSE_INDEX_FILENAME) |
| 104 | |
| 105 | has_cache = os.path.exists(cache_file) and os.path.getsize(cache_file) |
| 106 | |
| 107 | # bypass build if cache exists |
| 108 | if has_cache and not force: |
| 109 | try: |
| 110 | # save the list of additional directories included in the cache, or None if the cache does not |
| 111 | # include any additional directories |
| 112 | return load_cache_file(cache_file) |
| 113 | except Exception as e: |
| 114 | # work around some rare Windows quirks |
| 115 | import traceback |
| 116 | print('Inconsistent License cache: rebuilding index.') |
| 117 | print(str(e)) |
| 118 | print(traceback.format_exc()) |
| 119 | |
| 120 | from licensedcode.models import licenses_data_dir as ldd |
| 121 | from licensedcode.models import rules_data_dir as rdd |
| 122 | from licensedcode.models import load_licenses_from_multiple_dirs |
| 123 | from licensedcode.models import get_license_dirs |
| 124 | from licensedcode.models import validate_additional_license_data |
| 125 | from licensedcode.models import get_paths_to_installed_licenses_and_rules |
| 126 | from scancode import lockfile |