Coverage for tsfpga/tools/version_number_handler.py: 0%

62 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-26 09: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# -------------------------------------------------------------------------------------------------- 

10 

11import json 

12import re 

13import sys 

14from pathlib import Path 

15from urllib.request import urlopen 

16 

17from git.repo import Repo 

18from packaging.version import Version, parse 

19 

20from tsfpga.system_utils import create_file, read_file 

21 

22# Contents of unreleased.rst when it is empty 

23UNRELEASED_EMPTY = "Nothing here yet.\n" 

24 

25 

26class VersionNumberHandler: 

27 """ 

28 Class for handling the version number in __init__.py. 

29 """ 

30 

31 _version_regexp = re.compile(r"\n__version__ = \"(\S+?)\"\n") 

32 

33 def __init__(self, repo: Repo, version_file_path: Path) -> None: 

34 """ 

35 Arguments: 

36 repo: The git repository to work with. 

37 version_file_path: The ``__init__.py`` file that shall be modified. 

38 """ 

39 self._repo = repo 

40 self._file_path = version_file_path 

41 

42 def update(self, new_version: str) -> None: 

43 """ 

44 Set a new version number. 

45 

46 Arguments: 

47 new_version: New version number as a string, e.g. "2.3.1". 

48 """ 

49 old_version = self._get_current_version() 

50 self._verify_that_newer_version_number_is_greater_than_older( 

51 older_version=old_version, newer_version=new_version 

52 ) 

53 

54 self._set_new_version(new_version) 

55 

56 def bump_to_prelease(self) -> None: 

57 """ 

58 Bump to next version number. E.g. go from 8.0.2 to 8.0.3-dev. 

59 """ 

60 current_version = self._get_current_version() 

61 (current_major, current_minor, current_patch) = current_version.release 

62 

63 new_version = f"{current_major}.{current_minor}.{current_patch + 1}-dev" 

64 self._verify_that_newer_version_number_is_greater_than_older( 

65 older_version=current_version, newer_version=new_version 

66 ) 

67 

68 self._set_new_version(new_version) 

69 

70 @staticmethod 

71 def _verify_that_newer_version_number_is_greater_than_older( 

72 older_version: Version, newer_version: str 

73 ) -> None: 

74 if older_version >= parse(newer_version): 

75 sys.exit(f"New version {newer_version} is not greater than old version {older_version}") 

76 

77 def _get_current_version(self) -> Version: 

78 data = read_file(self._file_path) 

79 

80 match = self._version_regexp.search(data) 

81 if match is None: 

82 raise RuntimeError(f"Could not find version value in {self._file_path}") 

83 

84 version = match.group(1) 

85 return parse(version) 

86 

87 def _set_new_version(self, new_version: str) -> None: 

88 data = read_file(self._file_path) 

89 

90 updated_data = self._version_regexp.sub(f'\n__version__ = "{new_version}"\n', data) 

91 create_file(self._file_path, updated_data) 

92 

93 # Add file so that it gets included in upcoming commit 

94 self._repo.index.add(str(self._file_path.resolve())) 

95 

96 

97def verify_new_version_number( 

98 repo: Repo, pypi_project_name: str, new_version: str, unreleased_notes_file: Path 

99) -> str: 

100 """ 

101 Verify that the new version number is sane for this project. Will check git log and PyPI 

102 release history. 

103 

104 Arguments: 

105 repo: The git repository to work with. 

106 pypi_project_name: The name of this project on PyPI. 

107 new_version: New version. 

108 unreleased_notes_file: Path to the "unreleased.rst" release notes file. 

109 Must not be empty. 

110 

111 Return: 

112 The name of the git tag that corresponds to the new version number. 

113 """ 

114 if repo.is_dirty(): 

115 sys.exit("Must make release from clean repo") 

116 

117 if read_file(unreleased_notes_file) in ["", UNRELEASED_EMPTY]: 

118 sys.exit(f"The unreleased notes file {unreleased_notes_file} should not be empty") 

119 

120 with urlopen(f"https://pypi.python.org/pypi/{pypi_project_name}/json") as file_handle: 

121 json_data = json.load(file_handle) 

122 if new_version in json_data["releases"]: 

123 sys.exit(f"Release {new_version} already exists in PyPI") 

124 

125 git_tag = f"v{new_version}" 

126 if git_tag in repo.tags: 

127 sys.exit(f"Git release tag already exists: {git_tag}") 

128 

129 return git_tag 

130 

131 

132def commit_and_tag_release(repo: Repo, version: str, git_tag: str) -> None: 

133 """ 

134 Make a git commit with a "release" message, and tag it. 

135 

136 Arguments: 

137 repo: The git repository to work with. 

138 version: New version. 

139 git_tag: New git tag. 

140 """ 

141 make_commit(repo=repo, commit_message=f"Release version {version}") 

142 

143 repo.create_tag(git_tag) 

144 if git_tag not in repo.tags: 

145 sys.exit("Git tag failed") 

146 

147 

148def make_commit(repo: Repo, commit_message: str) -> None: 

149 """ 

150 Make a git commit, and check that it worked. 

151 

152 Arguments: 

153 repo: The git repository to work with. 

154 commit_message: Commit message to use. 

155 """ 

156 repo.index.commit(commit_message) 

157 if repo.is_dirty(): 

158 sys.exit("Git commit failed")