scripts: Clean up build_release

This commit is contained in:
Florian Bruhin 2025-10-24 15:01:20 +02:00
parent afa456f396
commit 0c2a673e27
2 changed files with 40 additions and 28 deletions

View File

@ -42,6 +42,7 @@ exclude = .*,__pycache__,resources.py
# W503: like break before binary operator
# W504: line break after binary operator
# FI18: __future__ import "annotations" missing
# FI58: __future__ import "annotations" present
# PT004: fixture '{name}' does not return anything, add leading underscore
# PT011: pytest.raises(ValueError) is too broad, set the match parameter or use a more specific exception
# PT012: pytest.raises() block should contain a single simple statement
@ -54,7 +55,7 @@ ignore =
D102,D103,D106,D107,D104,D105,D209,D211,D401,D402,D403,D412,D413,
A003,
W503, W504,
FI18,
FI18,FI58,
PT004,
PT011,
PT012

View File

@ -6,6 +6,7 @@
"""Build a new release."""
from __future__ import annotations
import os
import sys
@ -21,7 +22,7 @@ import collections
import dataclasses
import re
import http
from typing import Optional
from typing import Optional, TYPE_CHECKING
from collections.abc import Iterable
try:
@ -29,6 +30,10 @@ try:
except ImportError:
pass
if TYPE_CHECKING:
import github3
import github3.repos.release
import requests
REPO_ROOT = pathlib.Path(__file__).resolve().parents[2]
@ -564,6 +569,30 @@ def read_github_token(
return token
def _github_find_release(
gh: github3.GitHub, tag: str, experimental: bool
) -> github3.repos.release.Release:
if experimental:
repo = gh.repository('qutebrowser', 'experiments')
else:
repo = gh.repository('qutebrowser', 'qutebrowser')
assert repo is not None
for release in repo.releases():
if release.tag_name == tag:
return release
releases = ", ".join(r.tag_name for r in repo.releases())
raise Exception( # pylint: disable=broad-exception-raised
f"No release found for {tag!r} in {repo.full_name}, found: {releases}")
def _github_assets(
release: github3.repos.release.Release, artifact: Artifact
) -> list[github3.repos.release.Asset]:
return [asset for asset in release.assets() if asset.name == artifact.path.name]
def github_upload(
artifacts: list[Artifact],
tag: str,
@ -580,41 +609,23 @@ def github_upload(
experimental: Upload to the experiments repo
skip_if_exists: Skip uploading artifacts that already exist
"""
# pylint: disable=broad-exception-raised
import github3
import github3.exceptions
utils.print_title("Uploading to github...")
gh = github3.login(token=gh_token)
if experimental:
repo = gh.repository('qutebrowser', 'experiments')
else:
repo = gh.repository('qutebrowser', 'qutebrowser')
release = None # to satisfy pylint
for release in repo.releases():
if release.tag_name == tag:
break
else:
releases = ", ".join(r.tag_name for r in repo.releases())
raise Exception(
f"No release found for {tag!r} in {repo.full_name}, found: {releases}")
assert gh is not None
release = _github_find_release(gh=gh, tag=tag, experimental=experimental)
for artifact in artifacts:
assets = [
asset for asset in release.assets() if asset.name == artifact.path.name
]
if assets and skip_if_exists:
if _github_assets(release, artifact) and skip_if_exists:
print(f"Artifact {artifact.path.name} already exists, skipping")
continue
while True:
print(f"Uploading {artifact.path}")
assets = [asset for asset in release.assets()
if asset.name == artifact.path.name]
if assets:
if (assets := _github_assets(release, artifact)):
print(f"Assets already exist: {assets}")
if utils.ON_CI:
@ -642,9 +653,7 @@ def github_upload(
print("Retrying!")
assets = [asset for asset in release.assets()
if asset.name == artifact.path.name]
if assets:
if (assets := _github_assets(release, artifact)):
stray_asset = assets[0]
print(f"Deleting stray asset {stray_asset.name}")
stray_asset.delete()
@ -654,7 +663,9 @@ def github_upload(
def check_pypi_exists(version: str) -> bool:
"""Check whether the given version exists on PyPI."""
response = requests.get(f"https://pypi.org/pypi/qutebrowser/{version}/json")
response = requests.get(
f"https://pypi.org/pypi/qutebrowser/{version}/json", timeout=30
)
if response.status_code == http.HTTPStatus.NOT_FOUND:
return False
response.raise_for_status()