|
| 1 | +# This script reads a checksums.txt file and generates a JSON file containing |
| 2 | +# the checksums and URLs for each file. The generated JSON file is then merged |
| 3 | +# with an existing JSON file containing previous checksums. If the version |
| 4 | +# already exists in the existing JSON file, an exception is raised. |
| 5 | +# |
| 6 | +# Usage: python generate_checksum.py <checksums.txt> <old_checksums.json> |
| 7 | +# |
| 8 | +# checksums.txt format: |
| 9 | +# 2ef0560c3c88908a22d1f302e5b0119160e72380e25fb58c2d7b153e9397a04c notation_1.0.0-rc.1_linux_arm64.tar.gz |
| 10 | +# 3b5239d68810fec349807aa9eb90fcb9cd972cdb540ecfd4fcf3631d7ad4be06 notation_1.0.0-rc.1_darwin_amd64.tar.gz |
| 11 | +# 7607c8de3b6c1435b2dc4c012e9c0486849ce7b4b5e0fbbee2dd9ed7aab084a7 notation_1.0.0-rc.1_linux_amd64.tar.gz |
| 12 | +# 7d091cbd62886d1b47b60519a5b56314e794caf18751b1cccab2f54387a0d5c4 notation_1.0.0-rc.1_windows_amd64.zip |
| 13 | +# eaa7b0c7c8d18e504766ce8d3ac5e46da2e97f4fdcead8be997e0ae74b146b00 notation_1.0.0-rc.1_darwin_arm64.tar.gz |
| 14 | +# |
| 15 | +# Note: This script may be integrated to pipeline in the future. |
| 16 | +# |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import json |
| 20 | + |
| 21 | +def build_url(name, version, filename): |
| 22 | + return { |
| 23 | + "notation": lambda : f'https://github.com/notaryproject/notation/releases/download/v{version}/{filename}', |
| 24 | + "notation-azure-kv": lambda :f'https://github.com/Azure/notation-azure-kv/releases/download/v{version}/{filename}' |
| 25 | + }[name]() |
| 26 | + |
| 27 | +def process_checksum(filepath): |
| 28 | + verionInfo = {} |
| 29 | + with open(filepath, 'r') as f: |
| 30 | + for line in f.readlines(): |
| 31 | + line = line.rstrip('\n') |
| 32 | + parts = line.split(' ') |
| 33 | + checksum = parts[0] |
| 34 | + filename = parts[2] |
| 35 | + name_parts = filename.split('_') |
| 36 | + name = name_parts[0] |
| 37 | + version = name_parts[1] |
| 38 | + osName = name_parts[2] |
| 39 | + arch = name_parts[3].split('.')[0] |
| 40 | + |
| 41 | + # generate checksum |
| 42 | + verionInfo.setdefault('version', version) |
| 43 | + verionInfo.setdefault(osName, {}) |
| 44 | + verionInfo[osName].setdefault(arch, {}) |
| 45 | + verionInfo[osName][arch] = { |
| 46 | + "url": build_url(name, version, filename), |
| 47 | + "checksum": checksum |
| 48 | + } |
| 49 | + |
| 50 | + return verionInfo |
| 51 | + |
| 52 | +def update_checksums(filepath, checksums): |
| 53 | + # read old checksums |
| 54 | + versionList = [] |
| 55 | + if os.path.exists(filepath): |
| 56 | + f = open(filepath, 'r') |
| 57 | + versionList = json.load(f) |
| 58 | + f.close() |
| 59 | + |
| 60 | + # check if version exists |
| 61 | + for versionInfo in versionList: |
| 62 | + if versionInfo['version'] == checksums['version']: |
| 63 | + raise Exception(f'Version {checksums["version"]} already exists in {filepath}') |
| 64 | + |
| 65 | + # update checksums |
| 66 | + with open(filepath, 'w') as f: |
| 67 | + json.dump([checksums] + versionList, f, indent=4, sort_keys=True) |
| 68 | + |
| 69 | +def main(): |
| 70 | + if len(sys.argv) < 3: |
| 71 | + print('Usage: python generate_checksum.py <checksums.txt> <old_checksums.json>') |
| 72 | + sys.exit(1) |
| 73 | + |
| 74 | + filepath = sys.argv[1] |
| 75 | + old_checksums = sys.argv[2] |
| 76 | + |
| 77 | + checksums = process_checksum(filepath) |
| 78 | + update_checksums(old_checksums, checksums) |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + main() |
0 commit comments