-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add build_info.py including content hash verification for static buil…
…d directories: - Removed the previous build information generation method and replaced it with a new Python script (scripts/build_info.py) that creates a build info file with content hashes for static assets and locales. - Updated Dockerfile to utilize the new build_info.py script for generating build information during the Docker build process. - Adjusted the static check in the Django app to validate the content hash of static files against the expected hash. - Cleaned up .dockerignore and .gitignore files by removing the exclusion of build*.py files. - Added unit tests for the new build_info.py script and its functionality.
- Loading branch information
Showing
7 changed files
with
313 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,6 @@ | |
.tox/ | ||
.vscode | ||
backups | ||
build*.py | ||
buildx-bake-metadata.json | ||
deps/* | ||
docker*.yml | ||
|
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 |
---|---|---|
|
@@ -25,7 +25,6 @@ | |
.tox/ | ||
.vscode | ||
backups | ||
build*.py | ||
buildx-bake-metadata.json | ||
deps/* | ||
docker*.yml | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import hashlib | ||
import json | ||
from pathlib import Path | ||
|
||
|
||
root = Path(__file__).parent.parent | ||
|
||
|
||
def hash_directory( | ||
directory: Path, | ||
exclude_patterns: list[str] = None, | ||
verbose: bool = False, | ||
) -> str: | ||
if not directory.exists(): | ||
return None | ||
|
||
hasher = hashlib.blake2b(digest_size=32) | ||
|
||
exclude_patterns = exclude_patterns or [] | ||
|
||
files = sorted(f for f in directory.rglob('*')) | ||
|
||
for f in files: | ||
is_hidden = any(part.startswith('.') for part in f.parts) | ||
is_excluded = any(f.match(pattern) for pattern in exclude_patterns) | ||
if not is_hidden and not is_excluded: | ||
if verbose: | ||
print(f'Hashing {f}') | ||
hasher.update(f.name.encode()) | ||
if f.is_file(): | ||
hasher.update(f.read_bytes()) | ||
elif verbose: | ||
print(f'Skipping {f}') | ||
|
||
return hasher.hexdigest() | ||
|
||
|
||
def build_info( | ||
commit: str = None, version: str = None, build: str = None, target: str = None | ||
): | ||
""" | ||
Create a build info file with the current build information from the environment. | ||
""" | ||
return { | ||
'commit': commit, | ||
'version': version, | ||
'build': build, | ||
'target': target, | ||
'source': 'https://github.com/mozilla/addons-server', | ||
'content_hash': { | ||
'site_static_hash': hash_directory( | ||
(root / 'site-static'), exclude_patterns=['staticfiles.json'] | ||
), | ||
'locale_hash': hash_directory((root / 'locale')), | ||
}, | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--output', type=str, required=False) | ||
|
||
parser.add_argument('--commit', type=str, required=False) | ||
parser.add_argument('--version', type=str, required=False) | ||
parser.add_argument('--build', type=str, required=False) | ||
# Docker target is used to determine build time and runtime behavior | ||
parser.add_argument('--target', type=str, required=True) | ||
|
||
args = parser.parse_args() | ||
|
||
version = build_info(args.commit, args.version, args.build, args.target) | ||
|
||
if args.output: | ||
with open(args.output, 'w') as f: | ||
json.dump(version, f) | ||
else: | ||
print(json.dumps(version)) |
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.