-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
33 changed files
with
677 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import glob | ||
import sys | ||
import json | ||
import hashlib | ||
|
||
|
||
IGNORED_PREFIXES = [] | ||
|
||
|
||
def find_fingerprints_and_check_dirs(base_dir): | ||
all_fingerprints = set(glob.glob(os.path.join(base_dir, '**', 'test*', '**', '*.fingerprint'), recursive=True)) | ||
cache_dirs = set(glob.glob(os.path.join(base_dir, '**', 'test-fixtures', 'cache'), recursive=True)) | ||
|
||
all_fingerprints = {os.path.relpath(fp) for fp in all_fingerprints | ||
if not any(fp.startswith(prefix) for prefix in IGNORED_PREFIXES)} | ||
|
||
if not all_fingerprints and not cache_dirs: | ||
show("No .fingerprint files or cache directories found.") | ||
exit(1) | ||
|
||
missing_content = [] | ||
valid_paths = set() | ||
fingerprint_contents = [] | ||
|
||
for fingerprint in all_fingerprints: | ||
path = fingerprint.replace('.fingerprint', '') | ||
|
||
if not os.path.exists(path): | ||
missing_content.append(path) | ||
continue | ||
|
||
if not os.path.isdir(path): | ||
valid_paths.add(path) | ||
continue | ||
|
||
if os.listdir(path): | ||
valid_paths.add(path) | ||
else: | ||
missing_content.append(path) | ||
|
||
with open(fingerprint, 'r') as f: | ||
content = f.read().strip() | ||
fingerprint_contents.append((fingerprint, content)) | ||
|
||
# add cache dirs to valid paths | ||
for cache_dir in cache_dirs: | ||
if os.path.isdir(cache_dir) and os.listdir(cache_dir): | ||
valid_paths.add(os.path.relpath(cache_dir)) | ||
else: | ||
missing_content.append(os.path.relpath(cache_dir)) | ||
|
||
return sorted(valid_paths), missing_content, fingerprint_contents | ||
|
||
|
||
def calculate_sha256(fingerprint_contents): | ||
sorted_fingerprint_contents = sorted(fingerprint_contents, key=lambda x: x[0]) | ||
|
||
concatenated_contents = ''.join(content for _, content in sorted_fingerprint_contents) | ||
|
||
sha256_hash = hashlib.sha256(concatenated_contents.encode()).hexdigest() | ||
|
||
return sha256_hash | ||
|
||
|
||
def show(*s: str): | ||
print(*s, file=sys.stderr) | ||
|
||
|
||
# outputs a input hash and set of paths that should be cached, for example: | ||
# { | ||
# "input": "85ae27de8da54826dbb374e8a302b19c9ecc6361f1714dce88f2e45a60b58870", | ||
# "paths": [ | ||
# "cmd/syft/internal/test/integration/test-fixtures/cache", | ||
# "syft/file/cataloger/executable/test-fixtures/elf/bin", | ||
# "syft/file/cataloger/executable/test-fixtures/shared-info/bin", | ||
# "syft/file/cataloger/filedigest/test-fixtures/cache", | ||
# "syft/file/cataloger/filemetadata/test-fixtures/cache", | ||
# "syft/file/cataloger/internal/test-fixtures/cache", | ||
# "syft/format/internal/testutil/test-fixtures/cache", | ||
# "syft/format/syftjson/test-fixtures/cache", | ||
# "syft/internal/fileresolver/test-fixtures/cache", | ||
# "syft/pkg/cataloger/binary/test-fixtures/cache", | ||
# "syft/pkg/cataloger/binary/test-fixtures/classifiers/bin", | ||
# "syft/pkg/cataloger/binary/test-fixtures/classifiers/dynamic", | ||
# "syft/pkg/cataloger/debian/test-fixtures/cache", | ||
# "syft/pkg/cataloger/golang/test-fixtures/archs/binaries", | ||
# "syft/pkg/cataloger/golang/test-fixtures/cache", | ||
# "syft/pkg/cataloger/java/test-fixtures/jar-metadata/cache", | ||
# "syft/pkg/cataloger/java/test-fixtures/java-builds/packages", | ||
# "syft/pkg/cataloger/kernel/test-fixtures/cache", | ||
# "syft/pkg/cataloger/python/test-fixtures/cache", | ||
# "syft/pkg/cataloger/redhat/test-fixtures/cache", | ||
# "syft/pkg/cataloger/redhat/test-fixtures/rpms", | ||
# "syft/pkg/cataloger/rust/test-fixtures/cache", | ||
# "syft/source/test-fixtures/cache", | ||
# "test/cli/test-fixtures/cache", | ||
# "test/install/cache" | ||
# ] | ||
# } | ||
# | ||
# ...if the input is inconsistent then this script will exit with a non-zero status code instead. | ||
def main(): | ||
base_dir = '.' | ||
valid_paths, missing_content, fingerprint_contents = find_fingerprints_and_check_dirs(base_dir) | ||
|
||
if missing_content: | ||
show("The following paths are missing or have no content, but have corresponding .fingerprint files:") | ||
for path in sorted(missing_content): | ||
show(f"- {path}") | ||
show("Please ensure these paths exist and have content if they are directories.") | ||
exit(1) | ||
|
||
sha256_hash = calculate_sha256(fingerprint_contents) | ||
|
||
output = { | ||
"input": sha256_hash, | ||
"paths": sorted(valid_paths) | ||
} | ||
|
||
print(json.dumps(output, indent=2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.