|
| 1 | +import os |
| 2 | +import re |
| 3 | +from typing import TypeVar, Optional |
| 4 | +from argparse import ArgumentParser, Namespace |
| 5 | +from urllib.request import urlopen |
| 6 | +from pathlib import Path |
| 7 | +from hashlib import sha256 |
| 8 | + |
| 9 | +ARTIFACTS = [ |
| 10 | + "opa_darwin_amd64", |
| 11 | + "opa_darwin_arm64_static", |
| 12 | + "opa_linux_amd64", |
| 13 | + "opa_linux_amd64_static", |
| 14 | + "opa_linux_arm64_static", |
| 15 | + "opa_windows_amd64", |
| 16 | +] |
| 17 | + |
| 18 | +T = TypeVar('T') |
| 19 | + |
| 20 | + |
| 21 | +def required(value: Optional[T], name: str) -> T: |
| 22 | + if value is None: |
| 23 | + raise ValueError(f"Expected value for {name}") |
| 24 | + |
| 25 | + return value |
| 26 | + |
| 27 | + |
| 28 | +def get_sha256(artifact_name: str, version: str) -> str: |
| 29 | + ext = ".exe" if "windows" in artifact_name else "" |
| 30 | + |
| 31 | + with urlopen(f"https://github.com/open-policy-agent/opa/releases/download/v{version}/{artifact_name}{ext}.sha256") as res: |
| 32 | + return res.read().decode().split(' ')[0] |
| 33 | + |
| 34 | + |
| 35 | +def get_sha256_file(file: str, version: str) -> str: |
| 36 | + file_url = f"https://raw.githubusercontent.com/open-policy-agent/opa/v{version}/{file}" |
| 37 | + |
| 38 | + with urlopen(file_url) as res: |
| 39 | + return sha256(res.read()).hexdigest() |
| 40 | + |
| 41 | + |
| 42 | +def main(args: Namespace): |
| 43 | + d = dict([ |
| 44 | + (artifact_name, get_sha256(artifact_name, args.version)) |
| 45 | + for artifact_name in ARTIFACTS |
| 46 | + ]) |
| 47 | + |
| 48 | + bzl_path = WORKSPACE_ROOT.joinpath( |
| 49 | + 'opa', 'private', 'opa_rules_dependencies.bzl') |
| 50 | + |
| 51 | + with open(bzl_path) as bzl_file: |
| 52 | + bzl_content = bzl_file.read() |
| 53 | + |
| 54 | + version_match = re.search( |
| 55 | + r"DEFAULT_VERSION\s*=\s*[\"'](.*?)[\"']", bzl_content) |
| 56 | + |
| 57 | + if version_match is None: |
| 58 | + raise ValueError(f"Could not find DEFAULT_VERSION in file {bzl_path}") |
| 59 | + |
| 60 | + start_version = version_match.start(1) |
| 61 | + end_version = version_match.end(1) |
| 62 | + |
| 63 | + bzl_content = bzl_content[:start_version] + \ |
| 64 | + args.version + bzl_content[end_version:] |
| 65 | + |
| 66 | + dict_match = re.search( |
| 67 | + r"^_OPA_SHA256\s*=\s*{\s*$", bzl_content, re.MULTILINE) |
| 68 | + |
| 69 | + if dict_match is None: |
| 70 | + raise ValueError(f"Could not find _OPA_SHA256 in file {bzl_path}") |
| 71 | + |
| 72 | + bzl_content = bzl_content[:dict_match.end( |
| 73 | + )] + f"\n \"{args.version}\": {{\n" + '\n'.join([ |
| 74 | + f" \"{artifact_name}\": \"{sha256}\"," |
| 75 | + for artifact_name, sha256 in d.items() |
| 76 | + ] + [ |
| 77 | + f" \"opa_capabilities_json\": \"{get_sha256_file('capabilities.json', args.version)}\",", |
| 78 | + f" \"opa_builtin_metadata_json\": \"{get_sha256_file('builtin_metadata.json', args.version)}\",", |
| 79 | + ]) + "\n }," + bzl_content[dict_match.end():] |
| 80 | + |
| 81 | + with open(bzl_path, "w", encoding="utf-8") as bzl_file: |
| 82 | + bzl_file.write(bzl_content) |
| 83 | + |
| 84 | + |
| 85 | +def get_workspace_root(wd: Path) -> Path: |
| 86 | + while not wd.joinpath("WORKSPACE").exists(): |
| 87 | + wd = wd.parent |
| 88 | + |
| 89 | + return wd |
| 90 | + |
| 91 | + |
| 92 | +BUILD_WORKING_DIRECTORY = Path( |
| 93 | + required(os.getenv("BUILD_WORKING_DIRECTORY"), "BUILD_WORKING_DIRECTORY")) |
| 94 | +WORKSPACE_ROOT = get_workspace_root(BUILD_WORKING_DIRECTORY) |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + |
| 98 | + parser = ArgumentParser() |
| 99 | + parser.add_argument('--version', '-v', required=True) |
| 100 | + |
| 101 | + main(parser.parse_args()) |
0 commit comments