File: version.py

package info (click to toggle)
python-beanie 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 14,427; makefile: 7; sh: 6
file content (125 lines) | stat: -rw-r--r-- 3,876 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import subprocess
import sys
from pathlib import Path

import requests  # type: ignore

if sys.version_info >= (3, 11):
    import tomllib
else:
    import tomli as tomllib
import tomli_w
from gh import GitHubHandler


class SemVer:
    def __init__(self, version: str):
        self.version = version
        self.major, self.minor, self.patch = map(int, self.version.split("."))

    def increment_minor(self):
        return SemVer(f"{self.major}.{self.minor + 1}.0")

    def __str__(self):
        return self.version

    def __eq__(self, other):
        return self.version == other.version

    def __gt__(self, other):
        return (
            (self.major > other.major)
            or (self.major == other.major and self.minor > other.minor)
            or (
                self.major == other.major
                and self.minor == other.minor
                and self.patch > other.patch
            )
        )


class VersionHandler:
    PACKAGE_NAME = "beanie"
    ROOT_PATH = Path(__file__).parent.parent.parent.parent

    def __init__(self):
        self.pyproject = self.ROOT_PATH / "pyproject.toml"
        self.init_py = self.ROOT_PATH / "beanie" / "__init__.py"
        self.changelog = self.ROOT_PATH / "docs" / "changelog.md"

        self.current_version = self.parse_version_from_pyproject(
            self.pyproject
        )
        self.pypi_version = self.get_version_from_pypi()

        if self.current_version < self.pypi_version:
            raise ValueError("Current version is less than pypi version")

        if self.current_version == self.pypi_version:
            self.current_version = self.current_version.increment_minor()
            self.update_files()
        else:
            self.flit_publish()

    @staticmethod
    def parse_version_from_pyproject(pyproject: Path) -> SemVer:
        with pyproject.open("rb") as f:
            toml_data = tomllib.load(f)
        return SemVer(toml_data["project"]["version"])

    def get_version_from_pypi(self) -> SemVer:
        response = requests.get(
            f"https://pypi.org/pypi/{self.PACKAGE_NAME}/json"
        )
        if response.status_code == 200:
            return SemVer(response.json()["info"]["version"])
        raise ValueError("Can't get version from pypi")

    def update_files(self):
        self.update_pyproject_version()
        self.update_file_versions([self.init_py])
        self.update_changelog()

    def update_pyproject_version(self):
        with self.pyproject.open("rb") as f:
            pyproject = tomllib.load(f)
        pyproject["project"]["version"] = str(self.current_version)
        with self.pyproject.open("wb") as f:
            tomli_w.dump(pyproject, f)

    def update_file_versions(self, files_to_update):
        for file_path in files_to_update:
            content = file_path.read_text()
            content = content.replace(
                str(self.pypi_version), str(self.current_version)
            )
            file_path.write_text(content)

    def update_changelog(self):
        handler = GitHubHandler(
            "BeanieODM",
            "beanie",
            str(self.pypi_version),
            str(self.current_version),
        )
        changelog_content = handler.build_markdown_for_many_prs()

        changelog_lines = self.changelog.read_text().splitlines()
        new_changelog_lines = []
        inserted = False

        for line in changelog_lines:
            new_changelog_lines.append(line)
            if line.strip() == "# Changelog" and not inserted:
                new_changelog_lines.append(changelog_content)
                inserted = True

        self.changelog.write_text("\n".join(new_changelog_lines))
        handler.commit_changes()

    def flit_publish(self):
        subprocess.run(["flit", "publish"], check=True)


if __name__ == "__main__":
    VersionHandler()