Skip to content

Commit

Permalink
Change publish to fetch prebuilt wheels
Browse files Browse the repository at this point in the history
  • Loading branch information
softwaredoug committed Oct 27, 2024
1 parent 05f7a87 commit 934f48a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ twine:

publish: twine
@echo "Publishing..."
twine upload --skip-existing dist/*
python scripts/publish.py


help:
Expand All @@ -149,10 +149,10 @@ help:
@echo "Targets:"
@echo " deps Install dependencies"
@echo " test Run tests"
@echo " build Build package"
@echo " build Build package (local)"
@echo " clean Clean build files"
@echo " destroy Completely destroy the dev env"
@echo " help Show this help message"
@echo " publish Publish package to PyPI"
@echo " publish Fetch current sha wheels github and publish to pypi"

.DEFAULT_GOAL := build
76 changes: 76 additions & 0 deletions scripts/publish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os
import subprocess
import requests
import tempfile
import zipfile


GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')


def fetch_workflow_actions(sha):
print(f"Fetching workflow actions for {sha}")
url = "https://api.github.com/repos/softwaredoug/searcharray/actions/runs"
params = {
"branch": "main",
"status": "completed",
"head_sha": sha,
}
headers = {
"Authorization": f"token {GITHUB_TOKEN}",
}
response = requests.get(url, headers=headers,
params=params)
response.raise_for_status()
return response.json()


def fetch_artifacts(artifact_url, dest="build/"):
headers = {
"Authorization": f"token {GITHUB_TOKEN}",
}
response = requests.get(artifact_url, headers=headers)
response.raise_for_status()
artifacts = response.json()
download_dir = tempfile.mkdtemp()
for artifact in artifacts['artifacts']:
download_url = artifact['archive_download_url']
response = requests.get(download_url, headers=headers)
response.raise_for_status()
print(f"Downloading {artifact['name']} to {download_dir}")
with open(download_dir + artifact['name'] + '.zip', 'wb') as f:
f.write(response.content)
with zipfile.ZipFile(download_dir + artifact['name'] + '.zip', 'r') as zip_ref:
print(f"Extracting {artifact['name']} to {dest}")
zip_ref.extractall(dest)


def wheels(git_sha):
actions = fetch_workflow_actions(git_sha)
name = "Build Wheels"
artifact_urls = []
for run in actions['workflow_runs']:
if run['name'] == name:
artifact_urls.append(run['artifacts_url'])
return artifact_urls


def twine_upload(from_dir):
subprocess.run(["twine", "upload", "--skip-existing", f"{from_dir}"], check=True)


def main():
git_sha = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf-8').strip()
git_sha_short = git_sha[:7]
dest = f"build/{git_sha_short}/"
os.makedirs(dest, exist_ok=True)
wheels_to_fetch = wheels(git_sha)
if not wheels_to_fetch:
raise ValueError("No artifacts found")
for artifact_url in wheels_to_fetch:
fetch_artifacts(artifact_url, dest)
twine_upload(dest + "*.whl")


if __name__ == "__main__":
main()

0 comments on commit 934f48a

Please sign in to comment.