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``.
4.. _Semantic Versioning: http://semver.org
6Calling the command with no arguments will simply print the current version.
8"""
9# Imports
11import os
12import semver
13from superdjango.utils import read_file
14from ..constants import VERSION_PATH
16# Exports
18__all__ = (
19 "bump_version",
20 "get_current_version",
21 "Version",
22)
24# Functions
27def bump_version(current, major=False, minor=False, patch=False, status=None, build=None):
28 """Bump the version.
30 :param current: The current version.
31 :type current: str
33 :param major: Indicates major version should be increased. Minor and patch are reset to 0.
34 :type major: bool
36 :param minor: Indicates minor version should be increased. Patch is reset to 0.
37 :type minor: bool
39 :param patch: Indicates the patch level should be increased.
41 :param status: The value of the pre-release identifier.
42 :type status: str
44 :param build: The value of the build name.
45 :type build: str
47 :rtype: str
48 :returns: The new version (striped).
50 """
51 # Create an instance for the current version.
52 version = Version(current)
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 )
63 return new_version
66def get_current_version(path=None):
67 """Get the current version.
69 :param path: The path to the version file.
70 :type path: str
72 :rtype: str
73 :returns: The current version. If the version path does not exist, this will be ``0.1.0-d``.
75 """
76 _path = path or VERSION_PATH
77 if not os.path.exists(_path):
78 return "0.1.0-d"
80 return read_file(_path).strip()
82# Classes
85class Version(object):
86 """Represents a version/release string."""
88 def __init__(self, identifier):
89 """Initialize a version.
91 :param identifier: The version string in semver.org format.
92 :type identifier: str
94 """
95 self.identifier = identifier
96 self._current = semver.parse_version_info(identifier)
98 def __str__(self):
99 return self.identifier
101 def bump(self, major=False, minor=False, patch=False, status=None, build=None):
102 """Bump the version.
104 :param major: Indicates major version should be increased. Minor and patch are reset to 0.
105 :type major: bool
107 :param minor: Indicates minor version should be increased. Patch is reset to 0.
108 :type minor: bool
110 :param patch: Indicates the patch level should be increased.
112 :param status: The value of the pre-release identifier.
113 :type status: str
115 :param build: The value of the build name.
116 :type build: str
118 :rtype: str
119 :returns: The new version (striped).
121 """
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
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
141 if status:
142 info = semver.parse_version_info(new_version)
144 new_version = "%s.%s.%s-%s" % (
145 info.major,
146 info.minor,
147 info.patch,
148 status
149 )
151 # Update the build.
152 if build:
153 info = semver.parse_version_info(new_version)
155 new_version = "%s.%s.%s-%s+%s" % (
156 info.major,
157 info.minor,
158 info.patch,
159 info.prerelease,
160 build
161 )
163 # Return the new version.
164 return new_version.strip()