-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpoetry_build.py
76 lines (59 loc) · 1.92 KB
/
poetry_build.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
from __future__ import annotations
import pathlib
import re
import subprocess
try:
import tomllib
except ImportError:
import tomli as tomllib
ROOT = pathlib.Path()
PYPROJECT = tomllib.loads((ROOT / "pyproject.toml").read_text())
try:
VERSION: str = PYPROJECT["tool"]["poetry"]["version"]
except KeyError:
raise RuntimeError("Version is not set") from None
RELEASE_LEVELS = {
"a": "alpha",
"b": "beta",
"rc": "candidate",
}
try:
end_char: str = re.findall(r"\d+.\d+.\d+([^\d]+).*", VERSION)[0]
except IndexError:
release_level = "final"
end_char = " " # if this was empty, it would raise a ValueError
else:
release_level = RELEASE_LEVELS[end_char]
# try to find out the commit hash if checked out from git, and append it to __version__ (since we use this value
# from setup.py, it gets automatically propagated to an installed copy as well)
try:
if (
commit_count := subprocess.check_output(["git", "rev-list", "--count", "HEAD"]).decode("utf-8").strip()
) and (commit_hash := subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).decode("utf-8").strip()):
VERSION = f"{VERSION}{commit_count}+g{commit_hash}"
except Exception:
pass
major, minor, micro = VERSION.split(".")
micro = micro.split(end_char, maxsplit=1)[0]
file = f"""\
from typing import Final, Literal, NamedTuple
__all__ = (
"__title__",
"__author__",
"__license__",
"__version__",
"version_info",
)
class VersionInfo(NamedTuple):
major: int
minor: int
micro: int
releaselevel: Literal["alpha", "beta", "candidate", "final"]
__title__: Final = "steam"
__author__: Final = "Gobot1234"
__license__: Final = "MIT"
__version__: Final = "{VERSION}"
version_info: Final = VersionInfo(major={major}, minor={minor}, micro={micro}, releaselevel="{release_level}")
"""
metadata = ROOT / "steam" / "__metadata__.py"
metadata.write_text(file)