Coverage for tsfpga/tools/version_number_handler.py: 0%
62 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-12-29 20:52 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-12-29 20:52 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the tsfpga project, a project platform for modern FPGA development.
5# https://tsfpga.com
6# https://github.com/tsfpga/tsfpga
7# --------------------------------------------------------------------------------------------------
8# A set of tools for versions and releases. Should be reusable between projects.
9# --------------------------------------------------------------------------------------------------
11# Standard libraries
12import json
13import re
14import sys
15from pathlib import Path
16from urllib.request import urlopen
18# Third party libraries
19from git.repo import Repo
20from packaging.version import Version, parse
22# First party libraries
23from tsfpga.system_utils import create_file, read_file
25# Contents of unreleased.rst when it is empty
26UNRELEASED_EMPTY = "Nothing here yet.\n"
29class VersionNumberHandler:
30 """
31 Class for handling the version number in __init__.py.
32 """
34 _version_regexp = re.compile(r"\n__version__ = \"(\S+?)\"\n")
36 def __init__(self, repo: Repo, version_file_path: Path) -> None:
37 """
38 Arguments:
39 repo: The git repository to work with.
40 version_file_path: The ``__init__.py`` file that shall be modified.
41 """
42 self._repo = repo
43 self._file_path = version_file_path
45 def update(self, new_version: str) -> None:
46 """
47 Set a new version number.
49 Arguments:
50 new_version: New version number as a string, e.g. "2.3.1".
51 """
52 old_version = self._get_current_version()
53 self._verify_that_newer_version_number_is_greater_than_older(
54 older_version=old_version, newer_version=new_version
55 )
57 self._set_new_version(new_version)
59 def bump_to_prelease(self) -> None:
60 """
61 Bump to next version number. E.g. go from 8.0.2 to 8.0.3-dev.
62 """
63 current_version = self._get_current_version()
64 (current_major, current_minor, current_patch) = current_version.release
66 new_version = f"{current_major}.{current_minor}.{current_patch + 1}-dev"
67 self._verify_that_newer_version_number_is_greater_than_older(
68 older_version=current_version, newer_version=new_version
69 )
71 self._set_new_version(new_version)
73 @staticmethod
74 def _verify_that_newer_version_number_is_greater_than_older(
75 older_version: Version, newer_version: str
76 ) -> None:
77 if older_version >= parse(newer_version):
78 sys.exit(f"New version {newer_version} is not greater than old version {older_version}")
80 def _get_current_version(self) -> Version:
81 data = read_file(self._file_path)
83 match = self._version_regexp.search(data)
84 if match is None:
85 raise RuntimeError(f"Could not find version value in {self._file_path}")
87 version = match.group(1)
88 return parse(version)
90 def _set_new_version(self, new_version: str) -> None:
91 data = read_file(self._file_path)
93 updated_data = self._version_regexp.sub(f'\n__version__ = "{new_version}"\n', data)
94 create_file(self._file_path, updated_data)
96 # Add file so that it gets included in upcoming commit
97 self._repo.index.add(str(self._file_path.resolve()))
100def verify_new_version_number(
101 repo: Repo, pypi_project_name: str, new_version: str, unreleased_notes_file: Path
102) -> str:
103 """
104 Verify that the new version number is sane for this project. Will check git log and PyPI
105 release history.
107 Arguments:
108 repo: The git repository to work with.
109 pypi_project_name: The name of this project on PyPI.
110 version: New version.
111 unreleased_notes_file: Path to the "unreleased.rst" release notes file.
112 Must not be empty.
114 Return:
115 The name of the git tag that corresponds to the new version number.
116 """
117 if repo.is_dirty():
118 sys.exit("Must make release from clean repo")
120 if read_file(unreleased_notes_file) in ["", UNRELEASED_EMPTY]:
121 sys.exit(f"The unreleased notes file {unreleased_notes_file} should not be empty")
123 with urlopen(f"https://pypi.python.org/pypi/{pypi_project_name}/json") as file_handle:
124 json_data = json.load(file_handle)
125 if new_version in json_data["releases"]:
126 sys.exit(f"Release {new_version} already exists in PyPI")
128 git_tag = f"v{new_version}"
129 if git_tag in repo.tags:
130 sys.exit(f"Git release tag already exists: {git_tag}")
132 return git_tag
135def commit_and_tag_release(repo: Repo, version: str, git_tag: str) -> None:
136 """
137 Make a git commit with a "release" message, and tag it.
139 Arguments:
140 repo: The git repository to work with.
141 version: New version.
142 git_tag: New git tag.
143 """
144 make_commit(repo=repo, commit_message=f"Release version {version}")
146 repo.create_tag(git_tag)
147 if git_tag not in repo.tags:
148 sys.exit("Git tag failed")
151def make_commit(repo: Repo, commit_message: str) -> None:
152 """
153 Make a git commit, and check that it worked.
155 Arguments:
156 repo: The git repository to work with.
157 commit_message: Commit message to use.
158 """
159 repo.index.commit(commit_message)
160 if repo.is_dirty():
161 sys.exit("Git commit failed")