summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2021-05-24 00:35:17 +0200
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2021-05-26 17:23:40 +0100
commitd1c7e6a0941955456cfa048b3d4b93bef8ea0999 (patch)
treefb9f8f79adb26c9fd01fc548f0469ed258c26519
parent9b91b09f9c02d4e98a4e12c3047d7af454d3f84f (diff)
downloadpsycopg2-d1c7e6a0941955456cfa048b3d4b93bef8ea0999.tar.gz
Add script to download packages from GitHub
-rw-r--r--.gitignore1
-rwxr-xr-xscripts/build/download_packages.py77
2 files changed, 78 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 9a3bb03..457c9ab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,3 +14,4 @@ env?
.vscode/
/rel
/wheels
+/packages
diff --git a/scripts/build/download_packages.py b/scripts/build/download_packages.py
new file mode 100755
index 0000000..46ce149
--- /dev/null
+++ b/scripts/build/download_packages.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+"""Download packages from github actions artifacts
+"""
+
+import io
+import os
+import sys
+import logging
+from pathlib import Path
+from zipfile import ZipFile
+
+import requests
+
+logger = logging.getLogger()
+logging.basicConfig(
+ level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s"
+)
+
+REPOS = "psycopg/psycopg2"
+WORKFLOW_NAME = "Build packages"
+
+
+class ScriptError(Exception):
+ """Controlled exception raised by the script."""
+
+
+def main():
+ try:
+ token = os.environ["GITHUB_TOKEN"]
+ except KeyError:
+ raise ScriptError("please set a GITHUB_TOKEN to download artifacts")
+
+ s = requests.Session()
+ s.headers["Accept"] = "application/vnd.github.v3+json"
+ s.headers["Authorization"] = f"token {token}"
+
+ logger.info("looking for recent runs")
+ resp = s.get(f"https://api.github.com/repos/{REPOS}/actions/runs?per_page=10")
+ resp.raise_for_status()
+ for run in resp.json()["workflow_runs"]:
+ if run["name"] == WORKFLOW_NAME:
+ break
+ else:
+ raise ScriptError(f"couldn't find {WORKFLOW_NAME!r} in recent runs")
+
+ logger.info(f"looking for run {run['id']} artifacts")
+ resp = s.get(f"{run['url']}/artifacts")
+ resp.raise_for_status()
+ artifacts = resp.json()["artifacts"]
+
+ dest = Path("packages")
+ if not dest.exists():
+ logger.info(f"creating dir {dest}")
+ dest.mkdir()
+
+ for artifact in artifacts:
+ logger.info(f"downloading {artifact['name']} archive")
+ zip_url = artifact["archive_download_url"]
+ resp = s.get(zip_url)
+ with ZipFile(io.BytesIO(resp.content)) as zf:
+ logger.info("extracting archive content")
+ zf.extractall(dest)
+
+ logger.info(f"now you can run: 'twine upload -s {dest}/*'")
+
+
+if __name__ == "__main__":
+ try:
+ sys.exit(main())
+
+ except ScriptError as e:
+ logger.error("%s", e)
+ sys.exit(1)
+
+ except KeyboardInterrupt:
+ logger.info("user interrupt")
+ sys.exit(1)