forked from DTN-Public/intake-pattern-catalog
/
next_version.py
55 lines (34 loc) · 1.44 KB
/
next_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
#!/usr/bin/env python3
"""Tool for generating next version number based on Git tags.
Version number scheme is `YYYY.MM.INC0` (CalVer with incrementing micro version.)
Also includes function for tagging git with the new version and pushing to
Typically used by CI (Bitbucket Pipelines).
"""
import datetime as dt
from packaging.version import Version, parse
import intake_pattern_catalog
NOW = dt.datetime.now()
CURRENT_VERSION = parse(intake_pattern_catalog.__version__)
def next_version(current_version: Version = CURRENT_VERSION, now=NOW):
if now.year == current_version.major and now.month == current_version.minor:
micro = current_version.micro + 1
else:
micro = 0
return f"{now.year}.{now.month}.{micro}"
def push_new_version():
from git import Repo
obj = Repo(".")
current_version = intake_pattern_catalog.__version__
if current_version.startswith("unknown"):
current_version = "0000.0.0"
print(f"Current version: {current_version}")
new_version = next_version(parse(current_version))
print(f"New version: {new_version}")
obj.create_tag(new_version, message="Version release")
obj.remote().push(new_version)
if __name__ == "__main__":
print(next_version())
def test_new_month():
assert next_version(parse("2020.1.1"), dt.datetime(2020, 2, 3)) == "2020.2.0"
def test_this_month():
assert next_version(parse("2020.1.1"), dt.datetime(2020, 1, 27)) == "2020.1.2"