Skip to content

Commit 31edcde

Browse files
committed
autoincrement version
1 parent 2b90a6b commit 31edcde

File tree

8 files changed

+209
-8
lines changed

8 files changed

+209
-8
lines changed

.github/scripts/increment_version.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#!/bin/env python
2+
import argparse
3+
from dataclasses import dataclass
4+
5+
from packaging.version import Version
6+
7+
PYPROJECT_PATH = "pyproject.toml"
8+
DEFAULT_CHANGELOG_PATH = "CHANGELOG.md"
9+
DEFAULT_YDB_VERSION_FILE = "ydb_dbapi/version.py"
10+
MARKER = "# AUTOVERSION"
11+
12+
13+
@dataclass(init=False)
14+
class VersionLine:
15+
old_line: str
16+
major: int
17+
minor: int
18+
patch: int
19+
pre: int
20+
21+
def __init__(self, old_line: str, version_str: str):
22+
self.old_line = old_line
23+
24+
version = Version(version_str)
25+
self.major = version.major
26+
self.minor = version.minor
27+
self.micro = version.micro
28+
29+
if version.pre is None:
30+
self.pre = 0
31+
else:
32+
self.pre = version.pre[1]
33+
34+
def increment(self, part_name: str, with_beta: bool):
35+
if part_name == "minor":
36+
self.increment_minor(with_beta)
37+
elif part_name == "patch" or part_name == "micro":
38+
self.increment_micro(with_beta)
39+
else:
40+
raise Exception("unexpected increment type: '%s'" % part_name)
41+
42+
def increment_minor(self, with_beta: bool):
43+
if with_beta:
44+
if self.pre == 0 or self.micro != 0:
45+
self.increment_minor(False)
46+
self.pre += 1
47+
return
48+
49+
if self.micro == 0 and self.pre > 0:
50+
self.pre = 0
51+
return
52+
53+
self.minor += 1
54+
self.micro = 0
55+
self.pre = 0
56+
57+
def increment_micro(self, with_beta: bool):
58+
if with_beta:
59+
if self.pre == 0:
60+
self.increment_micro(False)
61+
self.pre += 1
62+
return
63+
64+
if self.pre > 0:
65+
self.pre = 0
66+
return
67+
68+
self.micro += 1
69+
70+
def __str__(self):
71+
if self.pre > 0:
72+
pre = "b%s" % self.pre
73+
else:
74+
pre = ""
75+
76+
return "%s.%s.%s%s" % (self.major, self.minor, self.micro, pre)
77+
78+
def version_line_with_mark(self):
79+
return 'version = "%s" %s' % (str(self), MARKER)
80+
81+
82+
def extract_version(pyproject_content: str):
83+
version_line = ""
84+
for line in pyproject_content.splitlines():
85+
if MARKER in line:
86+
version_line = line
87+
break
88+
89+
if version_line == "":
90+
raise Exception("Not found version line")
91+
92+
version_line = version_line.strip()
93+
94+
parts = version_line.split('"')
95+
version_part = parts[1]
96+
97+
return VersionLine(old_line=version_line, version_str=version_part)
98+
99+
100+
def increment_version_at_pyproject(
101+
pyproject_path: str, inc_type: str, with_beta: bool
102+
) -> str:
103+
with open(pyproject_path, "rt") as f:
104+
setup_content = f.read()
105+
106+
version = extract_version(setup_content)
107+
version.increment(inc_type, with_beta)
108+
setup_content = setup_content.replace(
109+
version.old_line, version.version_line_with_mark()
110+
)
111+
112+
with open(pyproject_path, "w") as f:
113+
f.write(setup_content)
114+
115+
return str(version)
116+
117+
118+
def add_changelog_version(changelog_path, version: str):
119+
with open(changelog_path, "rt") as f:
120+
content = f.read()
121+
content = content.strip()
122+
123+
if content.startswith("##"):
124+
return
125+
126+
content = """## %s ##
127+
%s
128+
""" % (version, content)
129+
with open(changelog_path, "w") as f:
130+
f.write(content)
131+
132+
133+
def set_version_in_version_file(file_path: str, version: str):
134+
with open(file_path, "w") as f:
135+
f.write('VERSION = "%s"\n' % version)
136+
137+
138+
def main():
139+
parser = argparse.ArgumentParser()
140+
parser.add_argument(
141+
"--inc-type",
142+
default="minor",
143+
help="increment version type: patch or minor",
144+
choices=["minor", "patch"],
145+
)
146+
parser.add_argument(
147+
"--beta", choices=["true", "false"], help="is beta version"
148+
)
149+
parser.add_argument(
150+
"--changelog-path",
151+
default=DEFAULT_CHANGELOG_PATH,
152+
help="path to changelog",
153+
type=str,
154+
)
155+
parser.add_argument("--pyproject-path", default=PYPROJECT_PATH)
156+
157+
args = parser.parse_args()
158+
159+
is_beta = args.beta == "true"
160+
161+
new_version = increment_version_at_pyproject(
162+
args.pyproject_path, args.inc_type, is_beta
163+
)
164+
add_changelog_version(args.changelog_path, new_version)
165+
set_version_in_version_file(DEFAULT_YDB_VERSION_FILE, new_version)
166+
print(new_version)
167+
168+
169+
if __name__ == "__main__":
170+
main()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from .increment_version import VersionLine
4+
5+
6+
@pytest.mark.parametrize(
7+
"source,inc_type,with_beta,result",
8+
[
9+
("0.0.0", "patch", False, "0.0.1"),
10+
("0.0.1", "patch", False, "0.0.2"),
11+
("0.0.1b1", "patch", False, "0.0.1"),
12+
("0.0.0", "patch", True, "0.0.1b1"),
13+
("0.0.1", "patch", True, "0.0.2b1"),
14+
("0.0.2b1", "patch", True, "0.0.2b2"),
15+
("0.0.1", "minor", False, "0.1.0"),
16+
("0.0.1b1", "minor", False, "0.1.0"),
17+
("0.1.0b1", "minor", False, "0.1.0"),
18+
("0.1.0", "minor", True, "0.2.0b1"),
19+
("0.1.0b1", "minor", True, "0.1.0b2"),
20+
("0.1.1b1", "minor", True, "0.2.0b1"),
21+
("3.0.0b1", "patch", True, "3.0.0b2"),
22+
],
23+
)
24+
def test_increment_version(source, inc_type, with_beta, result):
25+
version = VersionLine("", source)
26+
version.increment(inc_type, with_beta)
27+
incremented = str(version)
28+
assert incremented == result

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ repos:
2323
hooks:
2424
- id: mypy
2525
name: mypy
26+
27+
exclude: '.github/'

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* YDB DBAPI based on QueryService

poetry.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ydb-dbapi"
3-
version = "0.0.1"
3+
version = "0.0.0" # AUTOVERSION
44
description = "YDB Python DBAPI which complies with PEP 249"
55
authors = ["Yandex LLC <ydb@yandex-team.ru>"]
66
readme = "README.md"
@@ -33,7 +33,7 @@ format = "ruff format"
3333
tests = "pytest"
3434

3535
[tool.ruff]
36-
exclude = [".venv", ".git", "__pycache__", "build", "dist", "venv"]
36+
exclude = [".venv", ".git", ".github/scripts", "__pycache__", "build", "dist", "venv"]
3737
line-length = 79
3838
target-version = "py39"
3939
src = ["ydb_dbapi", "tests"]

ydb_dbapi/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from .cursors import AsyncCursor
77
from .cursors import Cursor
88
from .errors import *
9-
from .version import version
9+
from .version import VERSION
1010

11-
__version__ = version
11+
__version__ = VERSION
1212

1313
apilevel = "2.0"
1414
threadsafety = 0

ydb_dbapi/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "0.0.1"
1+
VERSION = "0.0.1"

0 commit comments

Comments
 (0)