Source code for superdjango.contrib.releases.library.versions

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

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

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

"""
# Imports

import os
import semver
from superdjango.utils import read_file
from ..constants import VERSION_PATH

# Exports

__all__ = (
    "bump_version",
    "get_current_version",
    "Version",
)

# Functions


[docs]def bump_version(current, major=False, minor=False, patch=False, status=None, build=None): """Bump the version. :param current: The current version. :type current: str :param major: Indicates major version should be increased. Minor and patch are reset to 0. :type major: bool :param minor: Indicates minor version should be increased. Patch is reset to 0. :type minor: bool :param patch: Indicates the patch level should be increased. :param status: The value of the pre-release identifier. :type status: str :param build: The value of the build name. :type build: str :rtype: str :returns: The new version (striped). """ # Create an instance for the current version. version = Version(current) # Create an instance for the new version. new_version = version.bump( major=major, minor=minor, patch=patch, status=status, build=build ) return new_version
[docs]def get_current_version(path=None): """Get the current version. :param path: The path to the version file. :type path: str :rtype: str :returns: The current version. If the version path does not exist, this will be ``0.1.0-d``. """ _path = path or VERSION_PATH if not os.path.exists(_path): return "0.1.0-d" return read_file(_path).strip()
# Classes
[docs]class Version(object): """Represents a version/release string."""
[docs] def __init__(self, identifier): """Initialize a version. :param identifier: The version string in semver.org format. :type identifier: str """ self.identifier = identifier self._current = semver.parse_version_info(identifier)
def __str__(self): return self.identifier
[docs] def bump(self, major=False, minor=False, patch=False, status=None, build=None): """Bump the version. :param major: Indicates major version should be increased. Minor and patch are reset to 0. :type major: bool :param minor: Indicates minor version should be increased. Patch is reset to 0. :type minor: bool :param patch: Indicates the patch level should be increased. :param status: The value of the pre-release identifier. :type status: str :param build: The value of the build name. :type build: str :rtype: str :returns: The new version (striped). """ # Get the new version. if major: new_version = semver.bump_major(self.identifier) elif minor: new_version = semver.bump_minor(self.identifier) elif patch: new_version = semver.bump_patch(self.identifier) else: new_version = self.identifier # Update the status. if status: status = status elif self._current.prerelease: status = self._current.prerelease else: status = None if status: info = semver.parse_version_info(new_version) new_version = "%s.%s.%s-%s" % ( info.major, info.minor, info.patch, status ) # Update the build. if build: info = semver.parse_version_info(new_version) new_version = "%s.%s.%s-%s+%s" % ( info.major, info.minor, info.patch, info.prerelease, build ) # Return the new version. return new_version.strip()