Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2This utility is based upon `Semantic Versioning`_. By default it operates on ``VERSION.txt``. 

3 

4.. _Semantic Versioning: http://semver.org 

5 

6Calling the command with no arguments will simply print the current version. 

7 

8""" 

9# Imports 

10 

11import os 

12import semver 

13from superdjango.utils import read_file 

14from ..constants import VERSION_PATH 

15 

16# Exports 

17 

18__all__ = ( 

19 "bump_version", 

20 "get_current_version", 

21 "Version", 

22) 

23 

24# Functions 

25 

26 

27def bump_version(current, major=False, minor=False, patch=False, status=None, build=None): 

28 """Bump the version. 

29 

30 :param current: The current version. 

31 :type current: str 

32 

33 :param major: Indicates major version should be increased. Minor and patch are reset to 0. 

34 :type major: bool 

35 

36 :param minor: Indicates minor version should be increased. Patch is reset to 0. 

37 :type minor: bool 

38 

39 :param patch: Indicates the patch level should be increased. 

40 

41 :param status: The value of the pre-release identifier. 

42 :type status: str 

43 

44 :param build: The value of the build name. 

45 :type build: str 

46 

47 :rtype: str 

48 :returns: The new version (striped). 

49 

50 """ 

51 # Create an instance for the current version. 

52 version = Version(current) 

53 

54 # Create an instance for the new version. 

55 new_version = version.bump( 

56 major=major, 

57 minor=minor, 

58 patch=patch, 

59 status=status, 

60 build=build 

61 ) 

62 

63 return new_version 

64 

65 

66def get_current_version(path=None): 

67 """Get the current version. 

68 

69 :param path: The path to the version file. 

70 :type path: str 

71 

72 :rtype: str 

73 :returns: The current version. If the version path does not exist, this will be ``0.1.0-d``. 

74 

75 """ 

76 _path = path or VERSION_PATH 

77 if not os.path.exists(_path): 

78 return "0.1.0-d" 

79 

80 return read_file(_path).strip() 

81 

82# Classes 

83 

84 

85class Version(object): 

86 """Represents a version/release string.""" 

87 

88 def __init__(self, identifier): 

89 """Initialize a version. 

90 

91 :param identifier: The version string in semver.org format. 

92 :type identifier: str 

93 

94 """ 

95 self.identifier = identifier 

96 self._current = semver.parse_version_info(identifier) 

97 

98 def __str__(self): 

99 return self.identifier 

100 

101 def bump(self, major=False, minor=False, patch=False, status=None, build=None): 

102 """Bump the version. 

103 

104 :param major: Indicates major version should be increased. Minor and patch are reset to 0. 

105 :type major: bool 

106 

107 :param minor: Indicates minor version should be increased. Patch is reset to 0. 

108 :type minor: bool 

109 

110 :param patch: Indicates the patch level should be increased. 

111 

112 :param status: The value of the pre-release identifier. 

113 :type status: str 

114 

115 :param build: The value of the build name. 

116 :type build: str 

117 

118 :rtype: str 

119 :returns: The new version (striped). 

120 

121 """ 

122 

123 # Get the new version. 

124 if major: 

125 new_version = semver.bump_major(self.identifier) 

126 elif minor: 

127 new_version = semver.bump_minor(self.identifier) 

128 elif patch: 

129 new_version = semver.bump_patch(self.identifier) 

130 else: 

131 new_version = self.identifier 

132 

133 # Update the status. 

134 if status: 

135 status = status 

136 elif self._current.prerelease: 

137 status = self._current.prerelease 

138 else: 

139 status = None 

140 

141 if status: 

142 info = semver.parse_version_info(new_version) 

143 

144 new_version = "%s.%s.%s-%s" % ( 

145 info.major, 

146 info.minor, 

147 info.patch, 

148 status 

149 ) 

150 

151 # Update the build. 

152 if build: 

153 info = semver.parse_version_info(new_version) 

154 

155 new_version = "%s.%s.%s-%s+%s" % ( 

156 info.major, 

157 info.minor, 

158 info.patch, 

159 info.prerelease, 

160 build 

161 ) 

162 

163 # Return the new version. 

164 return new_version.strip()