#!/usr/bin/env python"""Updates module version based on the status of the git tree."""import osimport timeimport loggingimport gitclass 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): """Creates a suitable version string.""" if self.isRelease(): return self.getReleaseVersionString() else: return self.getNightlyVersionString() version = property(fget = getTag, doc = """The version string.""") def bump(self): """Bumps the version.""" logging.info("Bumping version for '%s'..." % self.module.__name__) versionString = "__version__=\"%s\"" % self.version initFile = open(self.module.__file__, "r") tempFile = tempfile.NamedTemporaryFile(delete = False) hasVersion = False for line in initFile: if '__version__' in line: hasVersion = True line = versionString tempFile.writeline(line) if not hasVersion: tempFile.writeline(versionString) initFile.close() shutil.move(tempFile.name, self.module.__file__)