Skip to content

Commit

Permalink
Harmonize scripts to compute versions
Browse files Browse the repository at this point in the history
Harmonizes the scripts to compute the versions. JSON files are only
written if `--write-json` is passed. Furthermore, `--version-suffix`
can no longer be combined with other release types and gives full
control over defining a suffix to the user. Similar changes are applied
to the scripts used in IREE.
  • Loading branch information
marbre committed Dec 11, 2024
1 parent 63edf36 commit d4d8548
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 63 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build_packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ jobs:
id: version_local
run: |
echo "version_suffix=${version_suffix}" >> $GITHUB_OUTPUT
python3 build_tools/python_deploy/compute_local_version.py --version-suffix=${version_suffix} sharktank
python3 build_tools/python_deploy/compute_local_version.py --version-suffix=${version_suffix} shortfin
python3 build_tools/python_deploy/compute_common_version.py -rc --version-suffix=${version_suffix} --write-json
python3 build_tools/python_deploy/compute_local_version.py --version-suffix=${version_suffix} --write-json sharktank
python3 build_tools/python_deploy/compute_local_version.py --version-suffix=${version_suffix} --write-json shortfin
python3 build_tools/python_deploy/compute_common_version.py --version-suffix=${version_suffix} --write-json
- name: Upload version_local.json files
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
Expand Down
68 changes: 30 additions & 38 deletions build_tools/python_deploy/compute_common_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,65 @@
from pathlib import Path
import json
from datetime import datetime
import sys
import subprocess

from packaging.version import Version


parser = argparse.ArgumentParser()
parser.add_argument("--write-json", action="store_true")
parser.add_argument("--version-suffix", action="store", type=str)

release_type = parser.add_mutually_exclusive_group()
release_type.add_argument("-stable", "--stable-release", action="store_true") # default
release_type = parser.add_mutually_exclusive_group(required=True)
release_type.add_argument("-stable", "--stable-release", action="store_true")
release_type.add_argument("-rc", "--nightly-release", action="store_true")

release_type.add_argument("-dev", "--development-release", action="store_true")
release_type.add_argument("--version-suffix", action="store", type=str)

args = parser.parse_args()

if not (args.stable_release or args.nightly_release):
parser.print_usage(sys.stderr)
sys.stderr.write("error: A release type is required\n")
sys.exit(1)

if args.stable_release and args.version_suffix:
sys.stderr.write("error: A version suffix is only supported for stable releases\n")
sys.exit(1)

THIS_DIR = Path(__file__).parent.resolve()
REPO_ROOT = THIS_DIR.parent.parent

VERSION_FILE_SHARKTANK = REPO_ROOT / "sharktank/version.json"
VERSION_FILE_SHORTFIN = REPO_ROOT / "shortfin/version.json"
VERSION_FILE_LOCAL = REPO_ROOT / "shark-ai/version_local.json"
VERSION_FILE_SHARKTANK_PATH = REPO_ROOT / "sharktank/version.json"
VERSION_FILE_SHORTFIN_PATH = REPO_ROOT / "shortfin/version.json"
VERSION_FILE_LOCAL_PATH = REPO_ROOT / "shark-ai/version_local.json"


def load_version_info(version_file):
with open(version_file, "rt") as f:
return json.load(f)


def write_version_info():
with open(VERSION_FILE_LOCAL, "w") as f:
json.dump(version_local, f, indent=2)
def write_version_info(version_file, version):
with open(version_file, "w") as f:
json.dump({"package-version": version}, f, indent=2)
f.write("\n")


sharktank_version = load_version_info(VERSION_FILE_SHARKTANK)
SHARKTANK_PACKAGE_VERSION = sharktank_version.get("package-version")
SHARKTANK_BASE_VERSION = Version(SHARKTANK_PACKAGE_VERSION).base_version
sharktank_version = load_version_info(VERSION_FILE_SHARKTANK_PATH)
sharktank_package_version = sharktank_version.get("package-version")
sharktank_base_version = Version(sharktank_package_version).base_version

shortfin_version = load_version_info(VERSION_FILE_SHORTFIN)
SHORTFIN_PACKAGE_VERSION = shortfin_version.get("package-version")
SHORTFIN_BASE_VERSION = Version(SHORTFIN_PACKAGE_VERSION).base_version
shortfin_version = load_version_info(VERSION_FILE_SHORTFIN_PATH)
shortfin_package_version = shortfin_version.get("package-version")
shortfin_base_version = Version(shortfin_package_version).base_version

if SHARKTANK_BASE_VERSION > SHORTFIN_BASE_VERSION:
COMMON_VERSION = SHARKTANK_BASE_VERSION
if sharktank_base_version > shortfin_base_version:
common_version = sharktank_base_version
else:
COMMON_VERSION = SHORTFIN_BASE_VERSION
common_version = shortfin_base_version

if args.nightly_release:
if args.version_suffix:
VERSION_SUFFIX = args.version_suffix
else:
VERSION_SUFFIX = "rc" + datetime.today().strftime("%Y%m%d")

COMMON_VERSION += VERSION_SUFFIX
common_version += "rc" + datetime.today().strftime("%Y%m%d")
elif args.development_release:
common_version += (
".dev0+"
+ subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()
)
elif args.version_suffix:
common_version += args.version_suffix

if args.write_json:
version_local = {"package-version": COMMON_VERSION}
write_version_info()
write_version_info(VERSION_FILE_LOCAL_PATH, common_version)

print(COMMON_VERSION)
print(common_version)
55 changes: 33 additions & 22 deletions build_tools/python_deploy/compute_local_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,61 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# This scripts grabs the X.Y.Z[.dev]` version identifier from a
# `version.json` and writes the corresponding
# `version.json` and writes a version identifier for a stable,
# nightly or development release, or a release with an arbitrary
# `X.Y.ZrcYYYYMMDD` version identifier to `version_local.json`.

import argparse
from pathlib import Path
import json
from datetime import datetime
import subprocess

from packaging.version import Version


parser = argparse.ArgumentParser()
parser.add_argument("path", type=Path)
parser.add_argument("--version-suffix", action="store", type=str)
parser.add_argument("--write-json", action="store_true")

release_type = parser.add_mutually_exclusive_group(required=True)
release_type.add_argument("-stable", "--stable-release", action="store_true")
release_type.add_argument("-rc", "--nightly-release", action="store_true")
release_type.add_argument("-dev", "--development-release", action="store_true")
release_type.add_argument("--version-suffix", action="store", type=str)

args = parser.parse_args()

VERSION_FILE = args.path / "version.json"
VERSION_FILE_LOCAL = args.path / "version_local.json"
VERSION_FILE_PATH = args.path / "version.json"
VERSION_FILE_LOCAL_PATH = args.path / "version_local.json"


def load_version_info():
with open(VERSION_FILE, "rt") as f:
def load_version_info(version_file):
with open(version_file, "rt") as f:
return json.load(f)


def write_version_info():
with open(VERSION_FILE_LOCAL, "w") as f:
json.dump(version_local, f, indent=2)
def write_version_info(version_file, version):
with open(version_file, "w") as f:
json.dump({"package-version": version}, f, indent=2)
f.write("\n")


version_info = load_version_info()

if args.version_suffix:
VERSION_SUFFIX = args.version_suffix
else:
VERSION_SUFFIX = "rc" + datetime.today().strftime("%Y%m%d")

PACKAGE_VERSION = version_info.get("package-version")
PACKAGE_BASE_VERSION = Version(PACKAGE_VERSION).base_version
PACKAGE_LOCAL_VERSION = PACKAGE_BASE_VERSION + VERSION_SUFFIX
version_info = load_version_info(VERSION_FILE_PATH)
package_version = version_info.get("package-version")
current_version = Version(package_version).base_version

version_local = {"package-version": PACKAGE_LOCAL_VERSION}
if args.nightly_release:
current_version += "rc" + datetime.today().strftime("%Y%m%d")
elif args.development_release:
current_version += (
".dev0+"
+ subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()
)
elif args.version_suffix:
current_version += args.version_suffix

write_version_info()
if args.write_json:
write_version_info(VERSION_FILE_LOCAL_PATH, current_version)

print(PACKAGE_LOCAL_VERSION)
print(current_version)

0 comments on commit d4d8548

Please sign in to comment.