From 6499bc0f3ce59ef4248a090bd34a6eae9cc5c150 Mon Sep 17 00:00:00 2001 From: mulhern Date: Wed, 6 Sep 2023 09:37:53 -0400 Subject: [PATCH] Rewrite releasing scripts for packit compatibility Signed-off-by: mulhern --- .github/workflows/main.yml | 1 + .github/workflows/weekly.yml | 1 + mockbuild_test/generate_sourcerpms.sh | 17 ++--- mockbuild_test/mockbuild.sh | 4 +- mockbuild_test/stratis-cli.spec | 2 +- mockbuild_test/stratisd.spec | 2 +- release_management/_utils.py | 38 +++++++++++- release_management/create_artifacts.py | 86 ++++++++++++++++++++------ 8 files changed, 111 insertions(+), 40 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5d9629a..fd279c4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,6 +26,7 @@ jobs: python3-pygithub python3-requests python3-semantic_version + python3-specfile task: lint - dependencies: > black diff --git a/.github/workflows/weekly.yml b/.github/workflows/weekly.yml index af01dfc..f3e050d 100644 --- a/.github/workflows/weekly.yml +++ b/.github/workflows/weekly.yml @@ -24,6 +24,7 @@ jobs: python3-pygithub python3-requests python3-semantic_version + python3-specfile task: lint - dependencies: > black diff --git a/mockbuild_test/generate_sourcerpms.sh b/mockbuild_test/generate_sourcerpms.sh index defff99..42089ba 100755 --- a/mockbuild_test/generate_sourcerpms.sh +++ b/mockbuild_test/generate_sourcerpms.sh @@ -8,7 +8,6 @@ if ! groups | grep mock; then exit 1 fi -DATECODE=$(date +%Y%m%d) DIST_RELEASE=$1 STRATISD_SPEC_VERSION=$(rpmspec -q --srpm --qf "%{version}\n" stratisd.spec) @@ -67,22 +66,14 @@ cd upstream git clone https://github.com/stratis-storage/stratisd git clone https://github.com/stratis-storage/stratis-cli cd stratisd -STRATISD_HEADREV=$(git rev-parse --short HEAD) -STRATISD_SUFFIX="~${DATECODE}git${STRATISD_HEADREV}" -../../../release_management/create_artifacts.py ../../SOURCES/ --pre-release-suffix="${STRATISD_SUFFIX}" stratisd "$STRATISD_SPEC_VERSION" +../../../release_management/create_artifacts.py ../../SOURCES/ --pre-release --specfile-path=../../SPECS/stratisd.spec stratisd "$STRATISD_SPEC_VERSION" cd .. cd stratis-cli -STRATISCLI_HEADREV=$(git rev-parse --short HEAD) -STRATISCLI_SUFFIX="~${DATECODE}git${STRATISCLI_HEADREV}" -../../../release_management/create_artifacts.py ../../SOURCES/ --pre-release-suffix="${STRATISCLI_SUFFIX}" stratis-cli "$STRATISCLI_SPEC_VERSION" +../../../release_management/create_artifacts.py ../../SOURCES/ --pre-release --specfile-path=../../SPECS/stratis-cli.spec stratis-cli "$STRATISCLI_SPEC_VERSION" cd ../.. -# Fix the "Requires: stratisd" line in stratis-cli.spec. -sed --in-place -E "s/(^Requires.*stratisd.*)${STRATISD_SPEC_VERSION}/\1${STRATISD_SPEC_VERSION}${STRATISD_SUFFIX}/g" SPECS/stratis-cli.spec - -# Before running mock, the spec versions need to be changed. -sed --in-place -E "s/(^Version.*)${STRATISD_SPEC_VERSION}/\1${STRATISD_SPEC_VERSION}${STRATISD_SUFFIX}/g" SPECS/stratisd.spec -sed --in-place -E "s/(^Version.*)${STRATISCLI_SPEC_VERSION}/\1${STRATISCLI_SPEC_VERSION}${STRATISCLI_SUFFIX}/g" SPECS/stratis-cli.spec +# Remove the "Requires: stratisd" line in stratis-cli.spec. +sed -i "/Requires.*stratisd/d" SPECS/stratis-cli.spec mock --buildsrpm -r $MOCKCONFIG --spec SPECS/stratisd.spec --sources SOURCES/ --resultdir=SRPMS/stratisd/ mock --buildsrpm -r $MOCKCONFIG --spec SPECS/stratis-cli.spec --sources SOURCES/ --resultdir=SRPMS/stratis-cli/ diff --git a/mockbuild_test/mockbuild.sh b/mockbuild_test/mockbuild.sh index c07265a..17c185f 100755 --- a/mockbuild_test/mockbuild.sh +++ b/mockbuild_test/mockbuild.sh @@ -87,8 +87,8 @@ cd ../.. mock --buildsrpm -r $MOCKCONFIG --spec SPECS/stratisd.spec --sources SOURCES/ --resultdir=SRPMS/stratisd/ mock --buildsrpm -r $MOCKCONFIG --spec SPECS/stratis-cli.spec --sources SOURCES/ --resultdir=SRPMS/stratis-cli/ -mock --rebuild -r $MOCKCONFIG SRPMS/stratisd/stratisd-"$STRATISD_SPEC_VERSION"-dev.$DIST.src.rpm --resultdir=RPMS/stratisd/ -mock --rebuild -r $MOCKCONFIG SRPMS/stratis-cli/stratis-cli-"$STRATISCLI_SPEC_VERSION"-dev.$DIST.src.rpm --resultdir=RPMS/stratis-cli/ +mock --rebuild -r $MOCKCONFIG SRPMS/stratisd/stratisd-"$STRATISD_SPEC_VERSION"-D.$DIST.src.rpm --resultdir=RPMS/stratisd/ +mock --rebuild -r $MOCKCONFIG SRPMS/stratis-cli/stratis-cli-"$STRATISCLI_SPEC_VERSION"-D.$DIST.src.rpm --resultdir=RPMS/stratis-cli/ for package in stratisd stratis-cli; do find RPMS -name "$package*.rpm" -exec cp -v -t output/$package {} + diff --git a/mockbuild_test/stratis-cli.spec b/mockbuild_test/stratis-cli.spec index d245a92..905bcf7 100644 --- a/mockbuild_test/stratis-cli.spec +++ b/mockbuild_test/stratis-cli.spec @@ -1,6 +1,6 @@ Name: stratis-cli Version: 3.6.0 -Release: dev%{?dist} +Release: D%{?dist} Summary: Command-line tool for interacting with the Stratis daemon License: Apache-2.0 diff --git a/mockbuild_test/stratisd.spec b/mockbuild_test/stratisd.spec index bac4bc5..6038c4e 100644 --- a/mockbuild_test/stratisd.spec +++ b/mockbuild_test/stratisd.spec @@ -5,7 +5,7 @@ Name: stratisd Version: 3.6.0 -Release: dev%{?dist} +Release: D%{?dist} Summary: Daemon that manages block devices to create filesystems License: MPL-2.0 diff --git a/release_management/_utils.py b/release_management/_utils.py index 5c2d835..f1dc961 100755 --- a/release_management/_utils.py +++ b/release_management/_utils.py @@ -22,12 +22,14 @@ import os import subprocess import tarfile +from datetime import datetime from getpass import getpass from urllib.parse import urlparse # isort: THIRDPARTY import requests from github import Github +from specfile import specfile MANIFEST_PATH = "./Cargo.toml" @@ -45,16 +47,16 @@ def __init__(self, base, *, suffix=None): :type suffix: str or Nonetype """ self.base = base - self.suffix = "" if suffix is None else suffix + self.suffix = suffix def __str__(self): - return self.base + self.suffix + return f"{self.base}{'' if self.suffix is None else '~' + self.suffix}" def to_crate_str(self): """ Return the release version in a crates.io-friendly string. """ - return (self.base + self.suffix).replace("~", "-") + return f"{self.base}{'' if self.suffix is None else '-' + self.suffix}" def base_only(self): """ @@ -63,6 +65,36 @@ def base_only(self): return self.base +def calc_pre_release_suffix(): + """ + Return a standard value for the pre-release suffix for the version + :rtype: str + :returns: standard pre-release suffix + """ + command = ["git", "rev-parse", "--short", "HEAD"] + with subprocess.Popen(command, stdout=subprocess.PIPE) as proc: + commit_hash = proc.stdout.readline().strip().decode("utf-8") + return f"{datetime.today():%Y%m%d%H%M}git{commit_hash}" + + +def edit_specfile(specfile_path, *, release_version=None, sources=None): + """ + Edit the specfile in place + :param specfile_path: abspath of specfile + :type specfile_path: str or NoneType + :param ReleaseVersion release_version: release version to set in spec file + :param sources: local source files + :type sources: list of str or NoneType + """ + if specfile_path is not None: + with specfile.Specfile(specfile_path) as spec: + spec.version = str(release_version) + if sources is not None: + with spec.sources() as entries: # pylint: disable=not-context-manager + for index, value in enumerate(sources): + entries[index].location = value + + def get_python_package_info(name): """ Get info about the python package. diff --git a/release_management/create_artifacts.py b/release_management/create_artifacts.py index e51926d..5466d52 100755 --- a/release_management/create_artifacts.py +++ b/release_management/create_artifacts.py @@ -20,13 +20,14 @@ # isort: STDLIB import argparse import os -import subprocess import sys # isort: LOCAL from _utils import ( MANIFEST_PATH, ReleaseVersion, + calc_pre_release_suffix, + edit_specfile, get_package_info, get_python_package_info, make_source_tarball, @@ -54,9 +55,17 @@ def main(): type=os.path.abspath, ) parser.add_argument( - "--pre-release-suffix", + "--specfile-path", action="store", - help="pre-release suffix to add to the version", + default=None, + help="path to specfile to edit", + type=lambda p: p if p is None else os.path.abspath(p), + ) + parser.add_argument( + "--pre-release", + action="store_true", + default=False, + help="do automatic actions for a pre-release version", ) subparsers = parser.add_subparsers(title="subcommands") @@ -97,26 +106,45 @@ def _stratisd_artifacts(namespace): output_path = namespace.output_dir os.makedirs(output_path, exist_ok=True) - (release_version, _) = get_package_info(manifest_abs_path, "stratisd") + (source_version, _) = get_package_info(manifest_abs_path, "stratisd") - if release_version != namespace.version: + if source_version != namespace.version: raise RuntimeError("Version mismatch.") - r_v = ReleaseVersion(release_version, suffix=namespace.pre_release_suffix) + pre_release_suffix = calc_pre_release_suffix() if namespace.pre_release else None + + specfile_path = namespace.specfile_path + if specfile_path is None and pre_release_suffix is not None: + raise RuntimeError("must specify specfile using --specfile-path option") + + release_version = ReleaseVersion(source_version, suffix=pre_release_suffix) + + source_tarfile_path = make_source_tarball("stratisd", release_version, output_path) + print(os.path.relpath(source_tarfile_path)) + + (vendor_tarfile_name, cargo_crate_path) = vendor( + manifest_abs_path, + release_version, + ) + + crate_path = os.path.join( + output_path, f"stratisd-{release_version.to_crate_str()}.crate" + ) - source_tarfile = make_source_tarball("stratisd", r_v, output_path) - (vendor_tarfile_name, crate_path) = vendor(manifest_abs_path, r_v) - os.rename(vendor_tarfile_name, os.path.join(output_path, vendor_tarfile_name)) + os.rename(cargo_crate_path, crate_path) - source_vendor_tarfile = os.path.join(output_path, vendor_tarfile_name) + vendor_tarfile_path = os.path.join(output_path, vendor_tarfile_name) - crate_suffix_name = f"stratisd-{r_v.to_crate_str()}.crate" - source_crate = os.path.join(output_path, crate_suffix_name) - os.rename(crate_path, source_crate) - subprocess.run(["sha512sum", source_crate], check=True) + os.rename(vendor_tarfile_name, vendor_tarfile_path) - subprocess.run(["sha512sum", source_tarfile], check=True) - subprocess.run(["sha512sum", source_vendor_tarfile], check=True) + edit_specfile( + specfile_path, + release_version=release_version, + sources=[ + os.path.basename(path) + for path in [source_tarfile_path, vendor_tarfile_path, crate_path] + ], + ) def _stratis_cli_artifacts(namespace): @@ -126,14 +154,32 @@ def _stratis_cli_artifacts(namespace): output_path = namespace.output_dir os.makedirs(output_path, exist_ok=True) - (release_version, _) = get_python_package_info("stratis-cli") + (source_version, _) = get_python_package_info("stratis-cli") - if release_version != namespace.version: + if source_version != namespace.version: raise RuntimeError("Version mismatch.") - r_v = ReleaseVersion(release_version, suffix=namespace.pre_release_suffix) + pre_release_suffix = calc_pre_release_suffix() if namespace.pre_release else None + specfile_path = namespace.specfile_path + + if specfile_path is None and pre_release_suffix is not None: + raise RuntimeError("must specify specfile using --specfile-path option") + + release_version = ReleaseVersion(source_version, suffix=pre_release_suffix) + + source_tarfile = make_source_tarball( + "stratis-cli", + release_version, + output_path, + ) + + edit_specfile( + specfile_path, + release_version=release_version, + sources=[os.path.basename(source_tarfile)], + ) - make_source_tarball("stratis-cli", r_v, output_path) + print(os.path.relpath(source_tarfile)) if __name__ == "__main__":