-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
34 lines (26 loc) · 1.13 KB
/
util.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
import os
import subprocess
from typing import Final, Optional
def get_git_sha(cwd: str) -> str:
try:
return subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=cwd).decode("ascii").strip()
except Exception:
return "unknown"
def write_version_info(package_name: str) -> str:
build_version: Final[Optional[str]] = os.getenv(f"{package_name.upper()}_BUILD_VERSION")
build_number: Final[Optional[str]] = os.getenv(f"{package_name.upper()}_BUILD_NUMBER")
cwd: Final[str] = os.getcwd()
version: str = open("version.txt", "r").read().strip()
sha: str = get_git_sha(cwd)
if build_version is not None:
assert build_number is not None, "A build number must accompany a build version"
if int(build_number) > 1:
version += ".post" + build_number
version = build_version
elif sha != "unknown":
version += "+" + sha[:7]
version_path = os.path.join(cwd, package_name, "version.py")
with open(version_path, "w") as f:
f.write("__version__ = '{}'\n".format(version))
f.write("git_commit_hash = {}\n".format(repr(sha)))
return version