-
Notifications
You must be signed in to change notification settings - Fork 10
/
set_pypi_version.py
69 lines (54 loc) · 2.25 KB
/
set_pypi_version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
#
# SPDX-License-Identifier: MPL-2.0
# script to set version dynamically
# read VERSION and PyPI, set PYPI_VERSION
import os
from pathlib import Path
from typing import cast
import requests
def set_version(pkg_dir: Path):
with open(pkg_dir / "VERSION") as f:
version = f.read().strip().strip("\n")
major, minor = (int(x) for x in version.split("."))
latest_major, latest_minor, latest_patch = get_pypi_latest()
# get version
version = get_new_version(major, minor, latest_major, latest_minor, latest_patch)
# mutate version in GitHub Actions
if ("GITHUB_SHA" in os.environ) and ("GITHUB_REF" in os.environ) and ("GITHUB_RUN_NUMBER" in os.environ):
sha = os.environ["GITHUB_SHA"]
ref = os.environ["GITHUB_REF"]
build_number = os.environ["GITHUB_RUN_NUMBER"]
# short hash number in numeric
short_hash = f'{int(f"0x{sha[0:6]}", base=16):08}'
if "main" in ref:
# main branch
# major.minor.patch
# do nothing
pass
else:
# feature branch
# major.minor.patch a 1 build_number short_hash
version += f"a1{build_number}{short_hash}"
with open(pkg_dir / "PYPI_VERSION", "w") as f:
f.write(version)
def get_pypi_latest():
request = requests.get("https://pypi.org/pypi/power-grid-model-io/json")
data = request.json()
version = str(data["info"]["version"])
return (int(x) for x in version.split("."))
def get_new_version(major, minor, latest_major, latest_minor, latest_patch):
if (major > latest_major) or ((major == latest_major) and minor > latest_minor):
# brand-new version with patch zero
return f"{major}.{minor}.0"
if major == latest_major and minor == latest_minor:
# current version, increment path
return f"{major}.{minor}.{latest_patch + 1}"
# does not allow building older version
raise ValueError(
"Invalid version number!\n"
f"latest version: {latest_major}.{latest_minor}.{latest_patch}\n"
f"to be built version: {major}.{minor}\n"
)
if __name__ == "__main__":
set_version(Path(__file__).parent)