File: increment_version_dev.py

package info (click to toggle)
python-ovoenergy 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 352 kB
  • sloc: python: 1,519; makefile: 4
file content (43 lines) | stat: -rw-r--r-- 1,247 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
"""Increment development version in setup.py.

If version is X.Y.Z.devN -> X.Y.Z.dev(N+1)
If version is X.Y.Z -> X.Y.(Z+1).dev0
"""

from __future__ import annotations

from pathlib import Path
import re


def main() -> None:
    """Increment development version in setup.py."""
    project_root = Path(__file__).resolve().parents[1]
    setup_path = project_root / "setup.py"
    contents = setup_path.read_text(encoding="utf-8")

    match = re.search(r'version="([^"]+)"', contents)
    if not match:
        raise SystemExit("version field not found in setup.py")

    version = match.group(1)
    dev_match = re.search(r"^(\d+)\.(\d+)\.(\d+)(?:\.dev(\d+))?$", version)
    if not dev_match:
        raise SystemExit(f"Unexpected version format: {version}")

    major, minor, patch, dev = dev_match.groups()
    if dev is None:
        new_version = f"{major}.{minor}.{int(patch) + 1}.dev0"
    else:
        new_version = f"{major}.{minor}.{patch}.dev{int(dev) + 1}"

    new_contents = contents.replace(
        f'version="{version}"', f'version="{new_version}"', 1
    )
    setup_path.write_text(new_contents, encoding="utf-8")
    print(f"Updated version to {new_version}")


if __name__ == "__main__":
    main()