|
| 1 | +#!usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import re |
| 6 | +from pathlib import Path |
| 7 | +from urllib.request import urlopen |
| 8 | + |
| 9 | +FILENAME_LOOKUP = { |
| 10 | + "linux32": "Linux-32bit.tar.gz", |
| 11 | + "linux64": "Linux-64bit.tar.gz", |
| 12 | + "linuxarm64": "Linux-arm64.tar.gz", |
| 13 | + "linuxppc64le": "Linux-ppc64le.tar.gz", |
| 14 | + "macos64": "macOS-64bit.tar.gz", |
| 15 | + "macosarm64": "macOS-arm64.tar.gz", |
| 16 | + "win32": "Windows-32bit.zip", |
| 17 | + "win64": "Windows-64bit.zip", |
| 18 | + "winarm64": "Windows-arm64.zip", |
| 19 | +} |
| 20 | +VERSION_RE = r"^s5cmd_([\d\.]+)_" |
| 21 | +TEXT_RE = r"\# The section.+\n\# Do not edit this[\S\s]+\# The section.+\n\# Do not edit this.+\n" |
| 22 | + |
| 23 | + |
| 24 | +def parse_args() -> argparse.Namespace: |
| 25 | + parser = argparse.ArgumentParser( |
| 26 | + description="Update s5cmd files and hashes in `s5cmdUrls.cmake`.", |
| 27 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 28 | + ) |
| 29 | + |
| 30 | + parser.add_argument( |
| 31 | + "-c", |
| 32 | + "--checksum-file", |
| 33 | + type=str, |
| 34 | + help="Path or URL to the checksum file", |
| 35 | + required=True, |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + "-s", |
| 39 | + "--s5cmd-file", |
| 40 | + type=str, |
| 41 | + help="Path to the s5cmd file", |
| 42 | + default=Path(__file__).parents[1].joinpath("s5cmdUrls.cmake").as_posix(), |
| 43 | + ) |
| 44 | + return parser.parse_args() |
| 45 | + |
| 46 | + |
| 47 | +def read_checksum_text(path: str) -> str: |
| 48 | + if path.startswith("http"): |
| 49 | + response = urlopen(path) |
| 50 | + return response.read().decode("utf-8") |
| 51 | + |
| 52 | + _path = Path(path) |
| 53 | + if not _path.exists() or not _path.is_file(): |
| 54 | + msg = f"Checksum file `{_path}` does not exist." |
| 55 | + raise FileNotFoundError(msg) |
| 56 | + return _path.read_text(encoding="utf-8") |
| 57 | + |
| 58 | + |
| 59 | +def generate_cmake_text(text: str) -> str: |
| 60 | + checksums = [line.split() for line in text.rstrip("\n").split("\n")] |
| 61 | + |
| 62 | + version = re.search(VERSION_RE, checksums[0][1]).group(1) |
| 63 | + if version is None: |
| 64 | + msg = f"Cannot find version in {checksums[0][1]}" |
| 65 | + raise ValueError(msg) |
| 66 | + |
| 67 | + output_lines = [ |
| 68 | + "# The section below is auto-generated by scripts/update_s5cmdUrls.cmake.py", |
| 69 | + "# Do not edit this section directly.", |
| 70 | + "", |
| 71 | + f'set(version "{version}")', |
| 72 | + "", |
| 73 | + ] |
| 74 | + |
| 75 | + for shortname, postfix in FILENAME_LOOKUP.items(): |
| 76 | + hash_ = None |
| 77 | + for c in checksums: |
| 78 | + _filename = f"s5cmd_{version}_{postfix}" |
| 79 | + if _filename == c[1]: |
| 80 | + hash_ = c[0] |
| 81 | + break |
| 82 | + if hash_ is None: |
| 83 | + msg = f"Cannot find hash for `{_filename}` in {checksums}" |
| 84 | + raise ValueError(msg) |
| 85 | + |
| 86 | + output_lines.append( |
| 87 | + f"set({shortname}_filename".ljust(26) + f'"s5cmd_${{version}}_{postfix}")' |
| 88 | + ) |
| 89 | + output_lines.append(f"set({shortname}_sha256".ljust(26) + f'"{hash_}")') |
| 90 | + output_lines.append("") |
| 91 | + |
| 92 | + output_lines.append( |
| 93 | + "# The section above is auto-generated by scripts/update_s5cmdUrls.cmake.py" |
| 94 | + ) |
| 95 | + output_lines.append("# Do not edit this section directly.") |
| 96 | + output_lines.append("") |
| 97 | + |
| 98 | + return "\n".join(output_lines) |
| 99 | + |
| 100 | + |
| 101 | +def overwrite_cmake_file(text: str, path: str) -> None: |
| 102 | + _path = Path(path) |
| 103 | + if not _path.exists() or not _path.is_file(): |
| 104 | + msg = f"File `{_path}` does not exist." |
| 105 | + raise FileNotFoundError(msg) |
| 106 | + |
| 107 | + cmake_text = _path.read_text(encoding="utf-8") |
| 108 | + if not re.search(TEXT_RE, cmake_text): |
| 109 | + msg = f"Unable to find text in `{_path}`. Please check the regex." |
| 110 | + raise ValueError(msg) |
| 111 | + |
| 112 | + replaced_text = re.sub(TEXT_RE, text, cmake_text) |
| 113 | + _path.write_text(replaced_text, encoding="utf-8") |
| 114 | + |
| 115 | + print(f"Checksum file `{_path}` updated.") # noqa: T201 |
| 116 | + |
| 117 | + |
| 118 | +def main(): |
| 119 | + args = parse_args() |
| 120 | + text = read_checksum_text(args.checksum_file) |
| 121 | + cmake_text = generate_cmake_text(text) |
| 122 | + overwrite_cmake_file(cmake_text, args.s5cmd_file) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments