Skip to content

Commit

Permalink
devtools: move stuff out of utils.py that has only one user
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Sep 5, 2024
1 parent 1fab781 commit 5b67afa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 84 deletions.
32 changes: 28 additions & 4 deletions dev/releases/make_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
import sys
import tarfile

from typing import List, Optional

from utils import (
download_with_sha256,
error,
get_makefile_var,
notice,
patchfile,
run_with_log,
safe_git_fetch_tags,
verify_command_available,
verify_git_clean,
verify_git_repo,
Expand All @@ -43,6 +42,27 @@
if sys.version_info < (3, 6):
error("Python 3.6 or newer is required")


# helper for extracting values of variables set in the GAP Makefiles.rules
def get_makefile_var(var: str) -> str:
res = subprocess.run(["make", f"print-{var}"], check=True, capture_output=True)
kv = res.stdout.decode("ascii").strip().split("=")
assert len(kv) == 2
assert kv[0] == var
return kv[1]


# Run what ever <args> command and create appropriate log file
def run_with_log(args: List[str], name: str, msg: Optional[str] = None) -> None:
if not msg:
msg = name
with open("../" + name + ".log", "w", encoding="utf-8") as fp:
try:
subprocess.run(args, check=True, stdout=fp, stderr=fp)
except subprocess.CalledProcessError:
error(msg + " failed. See " + name + ".log.")


notice("Checking prerequisites")
verify_command_available("curl")
verify_command_available("git")
Expand All @@ -52,7 +72,11 @@
verify_git_clean()

# fetch tags, so we can properly detect
safe_git_fetch_tags()
try:
subprocess.run(["git", "fetch", "--tags"], check=True)
except subprocess.CalledProcessError:
error("failed to fetch tags, you may have to do \n" + "git fetch --tags -f")


# Creating tmp directory
tmpdir = os.getcwd() + "/tmp"
Expand Down
45 changes: 43 additions & 2 deletions dev/releases/make_github_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
## This script makes a github release and uploads all tarballs as assets.
##
import sys
import re
import subprocess

import utils
import utils_github
Expand All @@ -19,12 +21,51 @@
if len(sys.argv) != 3:
error("usage: " + sys.argv[0] + " <tag_name> <path_to_release>")


def is_possible_gap_release_tag(tag: str) -> bool:
return re.fullmatch(r"v[1-9]+\.[0-9]+\.[0-9]+(-.+)?", tag) is not None


def verify_is_possible_gap_release_tag(tag: str) -> None:
if not is_possible_gap_release_tag(tag):
error(f"{tag} does not look like the tag of a GAP release version")


# lightweight vs annotated
# https://stackoverflow.com/questions/40479712/how-can-i-tell-if-a-given-git-tag-is-annotated-or-lightweight#40499437
def is_annotated_git_tag(tag: str) -> bool:
res = subprocess.run(
["git", "for-each-ref", "refs/tags/" + tag],
capture_output=True,
text=True,
check=False,
)
return res.returncode == 0 and res.stdout.split()[1] == "tag"


def check_git_tag_for_release(tag: str) -> None:
if not is_annotated_git_tag(tag):
error(f"There is no annotated tag {tag}")
# check that tag points to HEAD
tag_commit = subprocess.run(
["git", "rev-parse", tag + "^{}"], check=True, capture_output=True, text=True
).stdout.strip()
head = subprocess.run(
["git", "rev-parse", "HEAD"], check=True, capture_output=True, text=True
).stdout.strip()
if tag_commit != head:
error(
f"The tag {tag} does not point to the current commit {head} but"
+ f" instead points to {tag_commit}"
)


TAG_NAME = sys.argv[1]
PATH_TO_RELEASE = sys.argv[2]
VERSION = TAG_NAME[1:] # strip 'v' prefix

utils.verify_git_clean()
utils.verify_is_possible_gap_release_tag(TAG_NAME)
verify_is_possible_gap_release_tag(TAG_NAME)
repo = utils_github.initialize_github()

# Error if the tag TAG_NAME hasn't been pushed out yet.
Expand All @@ -35,7 +76,7 @@
# - exists
# - is an annotated tag
# - points to current HEAD
utils.check_git_tag_for_release(TAG_NAME)
check_git_tag_for_release(TAG_NAME)

# Error if this release has been already created on GitHub
if any(r.tag_name == TAG_NAME for r in repo.get_releases()):
Expand Down
9 changes: 8 additions & 1 deletion dev/releases/release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@
from typing import Any, Dict, List, TextIO

import requests
from utils import download_with_sha256, error, is_existing_tag, notice, warning
from utils import download_with_sha256, error, notice, warning


def usage(name: str) -> None:
print(f"Usage: `{name} NEWVERSION`")
sys.exit(1)


def is_existing_tag(tag: str) -> bool:
res = subprocess.run(
["git", "show-ref", "--quiet", "--verify", "refs/tags/" + tag], check=False
)
return res.returncode == 0


def find_previous_version(version: str) -> str:
major, minor, patchlevel = map(int, version.split("."))
if major != 4:
Expand Down
78 changes: 1 addition & 77 deletions dev/releases/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import shutil
import subprocess
import sys
from typing import Iterator, List, NoReturn, Optional
from typing import Iterator, NoReturn

import requests

Expand All @@ -38,9 +38,6 @@ def error(msg: str) -> NoReturn:
def verify_command_available(cmd: str) -> None:
if shutil.which(cmd) is None:
error(f"the '{cmd}' command was not found, please install it")
# TODO: do the analog of this in ReleaseTools bash script:
# command -v curl >/dev/null 2>&1 ||
# error "the 'curl' command was not found, please install it"


def verify_git_repo() -> None:
Expand Down Expand Up @@ -77,15 +74,6 @@ def working_directory(path: str) -> Iterator[None]:
os.chdir(prev_cwd)


# helper for extracting values of variables set in the GAP Makefiles.rules
def get_makefile_var(var: str) -> str:
res = subprocess.run(["make", f"print-{var}"], check=True, capture_output=True)
kv = res.stdout.decode("ascii").strip().split("=")
assert len(kv) == 2
assert kv[0] == var
return kv[1]


# compute the sha256 checksum of a file
def sha256file(path: str) -> str:
h = hashlib.sha256()
Expand Down Expand Up @@ -138,67 +126,3 @@ def download_with_sha256(url: str, dst: str) -> None:
error(
f"checksum for '{dst}' expected to be {expected_checksum} but got {actual_checksum}"
)


# Run what ever <args> command and create appropriate log file
def run_with_log(args: List[str], name: str, msg: Optional[str] = None) -> None:
if not msg:
msg = name
with open("../" + name + ".log", "w", encoding="utf-8") as fp:
try:
subprocess.run(args, check=True, stdout=fp, stderr=fp)
except subprocess.CalledProcessError:
error(msg + " failed. See " + name + ".log.")


def is_possible_gap_release_tag(tag: str) -> bool:
return re.fullmatch(r"v[1-9]+\.[0-9]+\.[0-9]+(-.+)?", tag) is not None


def verify_is_possible_gap_release_tag(tag: str) -> None:
if not is_possible_gap_release_tag(tag):
error(f"{tag} does not look like the tag of a GAP release version")


def is_existing_tag(tag: str) -> bool:
res = subprocess.run(
["git", "show-ref", "--quiet", "--verify", "refs/tags/" + tag], check=False
)
return res.returncode == 0


# Error checked git fetch of tags
def safe_git_fetch_tags() -> None:
try:
subprocess.run(["git", "fetch", "--tags"], check=True)
except subprocess.CalledProcessError:
error("failed to fetch tags, you may have to do \n" + "git fetch --tags -f")


# lightweight vs annotated
# https://stackoverflow.com/questions/40479712/how-can-i-tell-if-a-given-git-tag-is-annotated-or-lightweight#40499437
def is_annotated_git_tag(tag: str) -> bool:
res = subprocess.run(
["git", "for-each-ref", "refs/tags/" + tag],
capture_output=True,
text=True,
check=False,
)
return res.returncode == 0 and res.stdout.split()[1] == "tag"


def check_git_tag_for_release(tag: str) -> None:
if not is_annotated_git_tag(tag):
error(f"There is no annotated tag {tag}")
# check that tag points to HEAD
tag_commit = subprocess.run(
["git", "rev-parse", tag + "^{}"], check=True, capture_output=True, text=True
).stdout.strip()
head = subprocess.run(
["git", "rev-parse", "HEAD"], check=True, capture_output=True, text=True
).stdout.strip()
if tag_commit != head:
error(
f"The tag {tag} does not point to the current commit {head} but"
+ f" instead points to {tag_commit}"
)

0 comments on commit 5b67afa

Please sign in to comment.