|
| 1 | +import dataclasses |
| 2 | +import enum |
| 3 | +import os.path |
| 4 | +import time |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +""" |
| 9 | +Fetches the latest five releases for RPM and the single latest release for DEB. |
| 10 | +RPM files are placed in the destination folder where the DNF repo will expect them. |
| 11 | +DEB files are placed in a temporary location from where they will be copied by the |
| 12 | +reprepro tool. |
| 13 | +""" |
| 14 | + |
| 15 | +RELEASE_URL = "https://api.github.com/repos/dragonflydb/dragonfly/releases" |
| 16 | + |
| 17 | + |
| 18 | +class AssetKind(enum.Enum): |
| 19 | + RPM = 1 |
| 20 | + DEB = 2 |
| 21 | + |
| 22 | + |
| 23 | +@dataclasses.dataclass |
| 24 | +class Package: |
| 25 | + kind: AssetKind |
| 26 | + download_url: str |
| 27 | + version: str |
| 28 | + filename: str |
| 29 | + arch: str |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def from_url(url: str) -> "Package": |
| 33 | + tokens = url.split("/") |
| 34 | + filename = tokens[-1] |
| 35 | + kind = AssetKind.RPM if filename.endswith(".rpm") else AssetKind.DEB |
| 36 | + if kind == AssetKind.DEB: |
| 37 | + arch = filename.split(".")[0].split("_")[1] |
| 38 | + else: |
| 39 | + arch = filename.split(".")[1] |
| 40 | + return Package( |
| 41 | + kind=kind, download_url=url, version=tokens[-2], filename=filename, arch=arch |
| 42 | + ) |
| 43 | + |
| 44 | + def storage_path(self, root: str) -> str: |
| 45 | + match self.kind: |
| 46 | + case AssetKind.RPM: |
| 47 | + return os.path.join(root, "rpm", self.version) |
| 48 | + case AssetKind.DEB: |
| 49 | + # Debian packages are stored in a temporary path. |
| 50 | + # The reprepro tool will copy them later to the final path. |
| 51 | + return os.path.join("deb_tmp", self.arch, self.version) |
| 52 | + |
| 53 | + |
| 54 | +def collect_download_urls() -> list[Package]: |
| 55 | + packages = [] |
| 56 | + # TODO retry logic |
| 57 | + response = requests.get(RELEASE_URL) |
| 58 | + releases = response.json() |
| 59 | + for release in releases[:5]: |
| 60 | + for asset in release["assets"]: |
| 61 | + if asset["name"].endswith(".rpm") or asset["name"].endswith(".deb"): |
| 62 | + packages.append(Package.from_url(asset["browser_download_url"])) |
| 63 | + return packages |
| 64 | + |
| 65 | + |
| 66 | +def download_packages(root: str, packages: list[Package]): |
| 67 | + # The debian repository building tool, reprepo, only supports a single package per version by default. |
| 68 | + # The ability to support multiple versions has been added but is not present in ubuntu-latest on |
| 69 | + # github action runners yet. So we only download one package, the latest, for ubuntu. |
| 70 | + # The rest of the scripts work on a set of packages, so that when the Limit parameter is supported, |
| 71 | + # we can remove this flag and start hosting more than the latest versions. |
| 72 | + # Another alternative would be to use the components feature of reprepo, but it would involve updating |
| 73 | + # the repository definition itself for each release, which is a bad experience for end users. |
| 74 | + deb_done = False |
| 75 | + for package in packages: |
| 76 | + if package.kind == AssetKind.DEB and deb_done: |
| 77 | + continue |
| 78 | + |
| 79 | + print(f"Downloading {package.download_url}") |
| 80 | + path = package.storage_path(root) |
| 81 | + if not os.path.exists(path): |
| 82 | + os.makedirs(path) |
| 83 | + |
| 84 | + target = os.path.join(path, package.filename) |
| 85 | + # TODO retry logic |
| 86 | + response = requests.get(package.download_url) |
| 87 | + with open(target, "wb") as f: |
| 88 | + f.write(response.content) |
| 89 | + print(f"Downloaded {package.download_url}") |
| 90 | + time.sleep(0.5) |
| 91 | + if package.kind == AssetKind.DEB: |
| 92 | + deb_done = True |
| 93 | + |
| 94 | + |
| 95 | +def main(root: str): |
| 96 | + packages = collect_download_urls() |
| 97 | + download_packages(root, packages) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + import sys |
| 102 | + |
| 103 | + if len(sys.argv) == 1: |
| 104 | + print(f"Usage: {sys.argv[0]} <site folder>") |
| 105 | + sys.exit(1) |
| 106 | + main(sys.argv[1]) |
0 commit comments