|
| 1 | +import hashlib |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +import platform |
| 5 | +import shutil |
| 6 | + |
| 7 | + |
| 8 | +def sha256sum(path: str): |
| 9 | + h = hashlib.sha256() |
| 10 | + paths = [] |
| 11 | + |
| 12 | + # Note: this won't works if the given path is a directory with a subdirectory |
| 13 | + if os.path.isdir(path): |
| 14 | + for file in os.listdir(path): |
| 15 | + paths.append(os.path.join(path, file)) |
| 16 | + else: |
| 17 | + paths.append(path) |
| 18 | + |
| 19 | + for path in paths: |
| 20 | + with open(path, "rb") as f: |
| 21 | + data = f.read() |
| 22 | + h.update(data) |
| 23 | + return h.hexdigest() |
| 24 | + |
| 25 | + |
| 26 | +def dir_path(path): |
| 27 | + if os.path.isdir(path): |
| 28 | + return path |
| 29 | + else: |
| 30 | + raise NotADirectoryError(path) |
| 31 | + |
| 32 | + |
| 33 | +def build_archive(input_dir, destination_dir, archive_basename): |
| 34 | + input_files = os.listdir(args.input) |
| 35 | + print(f"packing mithril distribution {args.version}-{args.target} with files: {input_files}") |
| 36 | + |
| 37 | + if platform.system() == "Windows": |
| 38 | + import zipfile |
| 39 | + |
| 40 | + archive_name = os.path.join(destination_dir, f"{archive_basename}.zip") |
| 41 | + with zipfile.ZipFile(archive_name, mode="x") as archive: |
| 42 | + for filename in input_files: |
| 43 | + archive.write(os.path.join(input_dir, filename), arcname=filename) |
| 44 | + else: |
| 45 | + import tarfile |
| 46 | + |
| 47 | + archive_name = os.path.join(destination_dir, f"{archive_basename}.tar.gz") |
| 48 | + with tarfile.open(archive_name, "x:gz") as archive: |
| 49 | + for filename in input_files: |
| 50 | + archive.add(os.path.join(input_dir, filename), arcname=filename) |
| 51 | + |
| 52 | + print(f"successfully packed mithril distribution into: {archive_name}") |
| 53 | + return archive_name |
| 54 | + |
| 55 | + |
| 56 | +def check_archive(archive, original_input_dir): |
| 57 | + print(f"checking archive ...") |
| 58 | + test_dir = "./unpack-test" |
| 59 | + shutil.unpack_archive(archive, test_dir) |
| 60 | + original_checksum = sha256sum(original_input_dir) |
| 61 | + archive_content_checksum = sha256sum(test_dir) |
| 62 | + if original_checksum != archive_content_checksum: |
| 63 | + print( |
| 64 | + f"mithril distribution checksum mismatch: before {original_checksum} != after {archive_content_checksum}" |
| 65 | + ) |
| 66 | + exit(1) |
| 67 | + print("OK ! Checksum of the archive files matches original files") |
| 68 | + |
| 69 | + shutil.rmtree(test_dir) |
| 70 | + |
| 71 | + |
| 72 | +def compute_sha256_checksum(archive): |
| 73 | + print(f"computing archive checksum...") |
| 74 | + archive_checksum = sha256sum(archive) |
| 75 | + checksum_filename = f"{archive}.sha256" |
| 76 | + with open(checksum_filename, "x") as f: |
| 77 | + f.write(archive_checksum) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + parser = argparse.ArgumentParser() |
| 82 | + parser.add_argument("--input", type=dir_path, help="input folder which content will be archived", required=True) |
| 83 | + parser.add_argument("--dest", type=dir_path, help="destination folder for the archive, default to current folder", |
| 84 | + default="./") |
| 85 | + parser.add_argument("--version", help="version of the distribution to package", required=True) |
| 86 | + parser.add_argument("--target", help="target os & architecture of the package", required=True) |
| 87 | + args = parser.parse_args() |
| 88 | + |
| 89 | + archive_path = build_archive(args.input, args.dest, archive_basename=f"mithril-{args.version}-{args.target}") |
| 90 | + check_archive(archive_path, args.input) |
| 91 | + compute_sha256_checksum(archive_path) |
0 commit comments