Skip to content

Commit

Permalink
feat: replace ghr uploading with httpx
Browse files Browse the repository at this point in the history
* `volare push` no longer requires ghr to be installed. this removes ghr and thus go as a dependency for PDK building and pushing.
  • Loading branch information
donn committed Jan 9, 2025
1 parent 0813b53 commit 6aa43f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "volare"
version = "0.20.4"
version = "0.21.0"
description = "An PDK builder/version manager for PDKs in the open_pdks format"
authors = ["Efabless Corporation and Contributors <[email protected]>"]
readme = "Readme.md"
Expand Down
82 changes: 52 additions & 30 deletions volare/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import uuid
import pathlib
import tarfile
import tempfile
import importlib
import subprocess
from typing import Optional, List, Dict

import click
import httpx
import zstandard as zstd
from rich.console import Console
from rich.progress import Progress
Expand Down Expand Up @@ -159,8 +158,8 @@ def push(
if not os.path.isdir(version_directory):
raise FileNotFoundError(f"Version {version} not found.")

tempdir = tempfile.gettempdir()
tarball_directory = os.path.join(tempdir, "volare", f"{uuid.uuid4()}", version)
tempdir = tempfile.TemporaryDirectory("volare")
tarball_directory = tempdir.name
mkdirp(tarball_directory)

final_tarballs = []
Expand Down Expand Up @@ -191,42 +190,67 @@ def push(
progress.update(task, completed=i + 1)
path_in_tarball = os.path.relpath(file, version_directory)
tf.add(file, arcname=path_in_tarball)
console.log(f"\nCompressed to {tarball_path}.")
progress.remove_task(task)
final_tarballs.append(tarball_path)

tag = f"{pdk}-{version}"

# If someone wants to rewrite this to not use ghr, please, by all means.
console.log("Starting upload…")

body = f"{pdk} variants built using volare"
date = get_commit_date(version, family.repo, session)
if date is not None:
body = f"{pdk} variants (released on {date_to_iso8601(date)})"

for tarball_path in final_tarballs:
subprocess.check_call(
[
"ghr",
"-owner",
owner,
"-repository",
repository,
"-token",
session.github_token,
"-body",
body,
"-commitish",
"releases",
"-replace",
]
+ (["-prerelease"] if pre else [])
+ [
tag,
tarball_path,
]
)
release_url = f"{volare_repo.api}/releases"
response = session.post(
release_url,
json={
"target_commitish": "releases",
"tag_name": tag,
"name": tag,
"body": body,
"prerelease": pre,
"draft": False,
},
)

try:
response.raise_for_status()
except httpx.HTTPStatusError as exc:
if exc.response.status_code == 422:
raise RuntimeError(
f"HTTP response 422: release with tag {tag} might already exist"
)
else:
raise exc from None
release = response.json()
upload_url = release["upload_url"].split("{")[0]

with Progress() as progress:
# Upload assets to the release
for tarball_path in final_tarballs:
filename = tarball_path.split("/")[-1]
size = os.path.getsize(tarball_path)
with open(tarball_path, "rb") as f:
tid = progress.add_task(f"Uploading {filename}…", total=size)

def stream():
nonlocal f, tid, progress
while data := f.read(4096):
progress.advance(tid, len(data))
yield data

upload_params = {"name": filename}
upload_response = session.post(
upload_url,
params=upload_params,
headers={"Content-Type": "application/octet-stream"},
content=stream(),
)
upload_response.raise_for_status()
progress.update(tid, total=size)

console.log("Done.")


Expand All @@ -247,8 +271,6 @@ def push_cmd(
"""
For maintainers: Package and release a build to the public.
Requires ghr: github.com/tcnksm/ghr
Parameters: <version> (required)
"""
console = Console()
Expand Down
1 change: 1 addition & 0 deletions volare/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def __init__(
}
if github_token is not None:
raw_headers["Authorization"] = f"Bearer {github_token}"
raw_headers["Accept"] = "application/vnd.github+json"
self.headers = httpx.Headers(raw_headers)

def api(
Expand Down

0 comments on commit 6aa43f1

Please sign in to comment.