99import logging
1010import os
1111import subprocess
12- from auto_version import auto_version_tool
13- from continuous_delivery_scripts . utils . definitions import CommitType
14- from continuous_delivery_scripts . utils . git_helpers import LocalProjectRepository
12+ from continuous_delivery_scripts . utils . versioning import calculate_version , determine_version_string
13+ from typing import Optional , Tuple , Dict
14+
1515from continuous_delivery_scripts .utils .configuration import configuration , ConfigurationVariable
16- from continuous_delivery_scripts .utils .logging import log_exception , set_log_level
16+ from continuous_delivery_scripts .utils .definitions import CommitType
1717from continuous_delivery_scripts .utils .filesystem_helpers import cd
18- from typing import Optional , Tuple , Dict
18+ from continuous_delivery_scripts . utils . logging import log_exception , set_log_level
1919
2020logger = logging .getLogger (__name__ )
2121
@@ -31,87 +31,11 @@ def version_project(commit_type: CommitType) -> Tuple[bool, Optional[str], Dict[
3131 (is new version, the new version)
3232 """
3333 use_news_files = commit_type in [CommitType .BETA , CommitType .RELEASE ]
34- is_new_version , new_version , version_elements = _calculate_version (commit_type , use_news_files )
34+ is_new_version , new_version , version_elements = calculate_version (commit_type , use_news_files )
3535 _generate_changelog (new_version , use_news_files )
3636 return is_new_version , new_version , version_elements
3737
3838
39- def _calculate_version (commit_type : CommitType , use_news_files : bool ) -> Tuple [bool , Optional [str ], Dict [str , str ]]:
40- """Calculates the version for the release.
41-
42- eg. "0.1.2"
43-
44- Args:
45- commit_type:
46- use_news_files: Should the version be dependant on changes recorded in news files
47-
48- Returns:
49- Tuple containing
50- a flag stating whether it is a new version or not
51- A semver-style version for the latest release
52- """
53- BUMP_TYPES = {CommitType .DEVELOPMENT : "build" , CommitType .BETA : "prerelease" }
54- is_release = commit_type == CommitType .RELEASE
55- enable_file_triggers = True if use_news_files else None
56- bump = BUMP_TYPES .get (commit_type )
57- project_config_path = configuration .get_value (ConfigurationVariable .PROJECT_CONFIG )
58- new_version : Optional [str ] = None
59- is_new_version : bool = False
60- with cd (os .path .dirname (project_config_path )):
61- old , new_version , updates = auto_version_tool .main (
62- release = is_release ,
63- enable_file_triggers = enable_file_triggers ,
64- commit_count_as = bump ,
65- config_path = project_config_path ,
66- )
67- # Autoversion second returned value is not actually the new version
68- # There seem to be a bug in autoversion.
69- # This is why the following needs to be done to determine the version
70- version_elements = _get_version_elements (updates )
71- new_version = version_elements .get (auto_version_tool .Constants .VERSION_FIELD , new_version )
72- is_new_version = old != new_version
73- logger .info (":: Determining the new version" )
74- logger .info (f"Version: { new_version } " )
75- return is_new_version , new_version , version_elements
76-
77-
78- def _update_version_string (
79- commit_type : CommitType , new_version : Optional [str ], version_elements : Dict [str , str ]
80- ) -> Optional [str ]:
81- """Updates the version string for development releases.
82-
83- Args:
84- commit_type: commit type
85- new_version: the new version
86- version_elements: version elements
87- """
88- if commit_type == CommitType .DEVELOPMENT :
89- commit_count = version_elements .get (auto_version_tool .Constants .COMMIT_COUNT_FIELD )
90- if not commit_count :
91- with LocalProjectRepository () as git :
92- commit_count = git .get_commit_count ()
93- return "%s-%s.%s+%s" % (
94- new_version ,
95- auto_version_tool .config .BUILD_TOKEN ,
96- commit_count ,
97- version_elements .get (auto_version_tool .Constants .COMMIT_FIELD ),
98- )
99- return new_version
100-
101-
102- def _get_version_elements (native_version_elements : Dict [str , str ]) -> Dict [str , str ]:
103- """Determines the different version elements.
104-
105- Args:
106- native_version_elements: native version elements as understood by autoversion
107- """
108- return {
109- key : native_version_elements [native ]
110- for native , key in auto_version_tool .config .key_aliases .items ()
111- if native in native_version_elements
112- }
113-
114-
11539def _generate_changelog (version : Optional [str ], use_news_files : bool ) -> None :
11640 """Creates a towncrier log of the release.
11741
@@ -141,7 +65,7 @@ def main() -> None:
14165 try :
14266 commit_type = CommitType .parse (args .release_type )
14367 is_new_version , new_version , version_elements = version_project (commit_type )
144- version_to_print = _update_version_string (commit_type , new_version , version_elements )
68+ version_to_print = determine_version_string (commit_type , new_version , version_elements )
14569 print (version_to_print )
14670 except Exception as e :
14771 log_exception (logger , e )
0 commit comments