forked from microsoft/azure-quantum-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_version.py
111 lines (82 loc) · 4.51 KB
/
set_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import os
from typing import List
from urllib.request import urlopen
import json
ALLOWED_RELEASE_TYPES = ["major", "minor", "patch"]
ALLOWED_BUILD_TYPES = ["stable", "rc", "dev"]
PACKAGE_NAME = "azure-quantum"
PYPI_URL = f"https://pypi.python.org/pypi/{PACKAGE_NAME}/json"
RELEASE_TYPE = os.environ.get("RELEASE_TYPE") or "patch"
BUILD_TYPE = os.environ.get("BUILD_TYPE") or "dev"
if RELEASE_TYPE not in ALLOWED_RELEASE_TYPES:
print(f"RELEASE_TYPE environment variable must be {', '.join(ALLOWED_RELEASE_TYPES)}. Current value: {RELEASE_TYPE}")
exit(1)
if BUILD_TYPE not in ALLOWED_BUILD_TYPES:
print(f"BUILD_TYPE environment variable must be {', '.join(ALLOWED_BUILD_TYPES)}. Current value: {BUILD_TYPE}")
exit(1)
def _get_build_version(version_type: str, build_type: str, package_versions: List[str]) -> str:
stable_version_parts = None
# find last stable version
for version in package_versions:
version_parts = str(version).split(".")
if len(version_parts) == 3:
stable_version_parts = version_parts
break
if stable_version_parts is None:
stable_version_parts = ["0", "0", "0"]
if version_type == "major":
next_stable_version = f"{int(stable_version_parts[0]) + 1}.0.0"
elif version_type == "minor":
next_stable_version = f"{stable_version_parts[0]}.{int(stable_version_parts[1]) + 1}.0"
elif version_type == "patch":
next_stable_version = f"{stable_version_parts[0]}.{stable_version_parts[1]}.{int(stable_version_parts[2]) + 1}"
else:
raise ValueError(f"Version type \"{version_type}\" is not supported.")
if build_type == "stable":
return next_stable_version
# in case the build type is not "stable" find last "rc"/"dev" release and bump up it's suffix-number
for i in range(0, 100):
next_version = f"{next_stable_version}.{build_type}{i}"
if next_version not in package_versions:
return next_version
raise RuntimeError(f"Build version could not be determined for version type \"{version_type}\" and build type \"{build_type}\"")
def get_build_version(version_type: str, build_type: str) -> str:
"""Get build version by analysing released versions in PyPi and figuring out the next version.
Example:
- If the last stable version in PyPi was "1.1.0" and version_type = "major" and build_type = "stable", then returned version will be "2.0.0".
- If the last stable version in PyPi was "1.1.0" and the last dev version was "1.2.0.dev0" and version_type = "patch" and build_type = "dev",
then returned version will be "1.1.1.dev0".
- If the last stable version in PyPi was "1.1.0" and the last dev version was "1.2.0.dev0" and version_type = "minor" and build_type = "dev",
then returned version will be "1.2.0.dev1".
:param version_type: SYMVER type ("major"/"minor"/"patch")
:type version_type: str
:param build_type: Build type ("stable", "dev", "rc")
:type build_type: str
:return: build version
:rtype: str
"""
# get all releases from PyPi
with urlopen(PYPI_URL) as response:
if response.status == 200:
response_content = response.read()
response = json.loads(response_content.decode("utf-8"))
else:
raise RuntimeError(f"Request \"GET:{PYPI_URL}\" failed. Status code: \"{response.status}\"")
# Note: assuming versions are SYMVER (major.minor.patch[.dev0|.rc0]) and in chronological order:
# "1.0.0", "1.0.1", "1.1.0", "1.1.0.dev0", "1.1.0.dev1", "1.1.0.rc0"
# The next "rc" and "dev" version must follow the last "stable" version.
# sorting by time in reverse order to find the last releases, so we could assume the next version of certain "build_type"
package_versions_sorted = sorted(response["releases"].items(), key=lambda k: k[1][0]["upload_time_iso_8601"], reverse=True)
package_versions = [version[0] for version in package_versions_sorted]
return _get_build_version(version_type, build_type, package_versions)
if __name__ == "__main__":
build_version = get_build_version(RELEASE_TYPE, BUILD_TYPE)
print(f"Package version: {build_version}")
# Set PYTHON_VERSION variable for steps in same job to reference as $(PYTHON_VERSION)
print(f"##vso[task.setvariable variable=PYTHON_VERSION;]{build_version}")
# Set build tags
print(f"##vso[build.addbuildtag]v{build_version}")
print(f"##vso[build.addbuildtag]{BUILD_TYPE}")