-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathdump_version.py
78 lines (63 loc) · 2.15 KB
/
dump_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
from __future__ import annotations
import warnings
from pathlib import Path
from .. import _types as _t
from .._log import log as parent_log
from .._version_cls import _version_as_tuple
from ..version import ScmVersion
log = parent_log.getChild("dump_version")
TEMPLATES = {
".py": """\
# file generated by setuptools_scm
# don't change, don't track in version control
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Tuple
__version__ = version = {version!r} # type: str
__version_tuple__ = version_tuple = {version_tuple!r} # type: Tuple[int | str, ...]
""",
".txt": "{version}",
}
def dump_version(
root: _t.PathT,
version: str,
write_to: _t.PathT,
template: str | None = None,
scm_version: ScmVersion | None = None,
) -> None:
assert isinstance(version, str)
# todo: assert write_to doesnt escape
write_to = Path(write_to)
assert not write_to.is_absolute(), f"{write_to=}"
target = Path(root).joinpath(write_to)
write_version_to_path(
target, template=template, version=version, scm_version=scm_version
)
def _validate_template(target: Path, template: str | None) -> str:
if template == "":
warnings.warn(f"{template=} looks like a error, using default instead")
template = None
if template is None:
template = TEMPLATES.get(target.suffix)
if template is None:
raise ValueError(
f"bad file format: {target.suffix!r} (of {target})\n"
"only *.txt and *.py have a default template"
)
else:
return template
def write_version_to_path(
target: Path, template: str | None, version: str, scm_version: ScmVersion | None
) -> None:
final_template = _validate_template(target, template)
log.debug("dump %s into %s", version, target)
version_tuple = _version_as_tuple(version)
if scm_version is not None:
content = final_template.format(
version=version,
version_tuple=version_tuple,
scm_version=scm_version,
)
else:
content = final_template.format(version=version, version_tuple=version_tuple)
target.write_text(content, encoding="utf-8")