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