Diff of /.github/release_utils.py [000000] .. [87e8bf]

Switch to unified view

a b/.github/release_utils.py
1
import argparse
2
import re
3
from typing import Tuple
4
5
# https://packaging.python.org/guides/single-sourcing-package-version/
6
def find_version(version_file_path) -> str:
7
    with open(version_file_path) as version_file:
8
        version_match = re.search(r"^__version_tuple__ = (.*)", version_file.read(), re.M)
9
        if version_match:
10
            ver_tup = eval(version_match.group(1))
11
            ver_str = ".".join([str(x) for x in ver_tup])
12
            return ver_str
13
        raise RuntimeError("Unable to find version tuple.")
14
15
16
def get_next_version(release_type) -> Tuple[Tuple[int, int, int], str, str]:
17
    current_ver = find_version("myosuite/version.py")
18
    version_list = [int(x) for x in current_ver.strip("'").split(".")]
19
    major, minor, patch = version_list[0], version_list[1], version_list[2]
20
    if release_type == "patch":
21
        patch += 1
22
    elif release_type == "minor":
23
        minor += 1
24
        patch = 0
25
    elif release_type == "major":
26
        major += 1
27
        minor = patch = 0
28
    else:
29
        raise ValueError("Incorrect release type specified. Acceptable types are major, minor and patch.")
30
31
    new_version_tuple = (major, minor, patch)
32
    new_version_str = ".".join([str(x) for x in new_version_tuple])
33
    new_tag_str = "v" + new_version_str
34
    return new_version_tuple, new_version_str, new_tag_str
35
36
37
def update_version(new_version_tuple) -> None:
38
    """
39
    given the current version, update the version to the
40
    next version depending on the type of release.
41
    """
42
43
    with open("myosuite/version.py", "r") as reader:
44
        current_version_data = reader.read()
45
46
    # for line in current_version_data:
47
    version_match = re.search(r"^__version_tuple__ ", current_version_data)
48
49
    if version_match:
50
        new_version_data = "__version_tuple__ = %s\n" % str(new_version_tuple)
51
        current_version_data = current_version_data.replace(version_match.string, new_version_data)
52
53
        with open("myosuite/version.py", "w") as writer:
54
            writer.write(current_version_data)
55
    else:
56
        raise RuntimeError("__version_tuple__ not found in version.py")
57
58
59
def main(args):
60
    if args.release_type in ["major", "minor", "patch"]:
61
        new_version_tuple, new_version, new_tag = get_next_version(args.release_type)
62
    else:
63
        raise ValueError("Incorrect release type specified")
64
65
    if args.update_version:
66
        update_version(new_version_tuple)
67
68
    print(new_version, new_tag)
69
70
71
if __name__ == "__main__":
72
    parser = argparse.ArgumentParser(description="Versioning utils")
73
    parser.add_argument("--release-type", type=str, required=True, help="type of release = major/minor/patch")
74
    parser.add_argument(
75
        "--update-version", action="store_true", required=False, help="updates the version in fairscale/version.py"
76
    )
77
78
    args = parser.parse_args()
79
    main(args)