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

62 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 20:51 +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 

11# Standard libraries 

12import json 

13import re 

14import sys 

15from pathlib import Path 

16from urllib.request import urlopen 

17 

18# Third party libraries 

19from git.repo import Repo 

20from packaging.version import Version, parse 

21 

22# First party libraries 

23from tsfpga.system_utils import create_file, read_file 

24 

25# Contents of unreleased.rst when it is empty 

26UNRELEASED_EMPTY = "Nothing here yet.\n" 

27 

28 

29class VersionNumberHandler: 

30 """ 

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

32 """ 

33 

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

35 

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 

44 

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

46 """ 

47 Set a new version number. 

48 

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 ) 

56 

57 self._set_new_version(new_version) 

58 

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 

65 

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 ) 

70 

71 self._set_new_version(new_version) 

72 

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}") 

79 

80 def _get_current_version(self) -> Version: 

81 data = read_file(self._file_path) 

82 

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}") 

86 

87 version = match.group(1) 

88 return parse(version) 

89 

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

91 data = read_file(self._file_path) 

92 

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

94 create_file(self._file_path, updated_data) 

95 

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

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

98 

99 

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. 

106 

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. 

113 

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") 

119 

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") 

122 

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") 

127 

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}") 

131 

132 return git_tag 

133 

134 

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. 

138 

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}") 

145 

146 repo.create_tag(git_tag) 

147 if git_tag not in repo.tags: 

148 sys.exit("Git tag failed") 

149 

150 

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

152 """ 

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

154 

155 Arguments: 

156 repo (`git.Repo`): The git repository to work with. 

157 commit_message (str): Commit message to use. 

158 """ 

159 repo.index.commit(commit_message) 

160 if repo.is_dirty(): 

161 sys.exit("Git commit failed")