#!/usr/bin/env #python """Updates module version based on the status of the git tree.""" import os import time import logging import git class VersionBumper(object): """Tool for automagically updating the module version.""" def __init__(self, module): """Initializes a VersionBumper.""" super(VersionBumper, self).__init__(self) self.module = module self.repo = git.Repo(module.__file__) logging.debug("Created %s for module %s." % (repr(self), module)) def getBranch(self): """Returns the name of the current branch.""" return self.repo.branch().split()[1] branch = property(fget = getBranch, doc = """The current git branch.""") def getTag(self): """Returns the name of the current tag.""" return self.repo.git.tag() tag = property(fget = getTag, doc = """The current git tag.""") def isRelease(self): """Tests if we're currently pointed at a release branch.""" return 'release' in self.branch def getReleaseVersionString(self): """Create a version string for a release version.""" if self.tag: return self.tag def getNightlyVersionString(self): """Create a version string for a nightly version.""" versionStrings = [time.strftime("%Y%m%d")] if self.branch != 'master': versionStrings.append(self.branch) if self.tag: versionStrings.append(self.tag) return '-'.join(versionStrings) def getVersionString(self): if self.isRelease(): return self.getReleaseVersionString() else: return self.getNightlyVersionString() version = property(fget = getTag, doc = """The version string.""") def bump(self): """Bumps the version.""" versionString = "__version__=\"%s\"" % self.version initFile = open(self.module.__file__, "w") newLines = [line for line in initFile if '__version__' not in line else versionString] initFile.writelines(newLines) def bumpVersion(module): """Bumps the version for a module.""" repo = git.Repo(module.__file__) branch = repo.git.branch().split()[1] if 'release' in branch: logging.info("Detected a release branch (%s)..." % branch) version = repo.git.tag() else: logging.info("Detected a nightly branch (%s)..." % branch) version = time.strftime("%Y%m%d") if branch != "master": logging.debug("Branch is not master, adding to version...") version = '%s-%s' % (version, branch) logging.info("Created version string (%s)..." % version)