From f7d240801cc2ee0b60b0280e9a7ef7723571490f Mon Sep 17 00:00:00 2001 From: jfwood Date: Fri, 25 Jul 2014 08:43:58 -0500 Subject: [PATCH] Replace hard-coded setup version setting The pbr library is used to generate version numbers for Barbican, and should typically do so by forming the version using the latest tag (such as '2014.2.b2') as a base version, and then appending git commit info if after the tag. However, if setup.cfg has a 'version' property set, this overrides the base version essentially 'hard coding' it. This CR removes that override and instead lets the versionbuild.py script, used during deployment processing, generate this version override based on the current PBR generated version with a timestamp injected into it. Closes-Bug: #1349238 Change-Id: I32cf9b32265569c1e2ae182efa32a0d1449de15c --- bin/versionbuild.py | 81 +++++++++++++++++++++++++++++++++++---------- setup.cfg | 1 - 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/bin/versionbuild.py b/bin/versionbuild.py index e1c35c457..b4928a46e 100755 --- a/bin/versionbuild.py +++ b/bin/versionbuild.py @@ -33,6 +33,8 @@ import os from datetime import datetime from time import mktime +import pbr.version + # Determine version of this application. SETUP_FILE = 'setup.cfg' @@ -54,29 +56,74 @@ def get_patch(): def update_versionfile(patch): - """Update the 'patch' version information per the provided patch.""" + """Update the version information in setup.cfg per the provided patch. + + PBR will generate a version stamp per the docstring of _get_pbr_version() + below, which then stamps the version on source tarballs used for + packaging. This version stamp is not packaging friendly as it is not + monotonically increasing alphabetically. If a 'version' attribute is added + to setup.cfg, PBR will override the output major, minor and build + versions of the stamped version. By injecting a patch into this version + structure per this function, the desired monotonic version number can + be created. + """ temp_name = VERSIONFILE + '~' - file_new = open(temp_name, 'w') - try: - with open(VERSIONFILE, 'r') as file_old: + with open(VERSIONFILE, 'r') as file_old: + with open(temp_name, 'w') as file_new: for line in file_old: - if line.startswith('version ='): - subs = line.split('.') - if len(subs) <= 2: - file_new.write(''.join([line[:-1], '.', - str(patch), '\n'])) + if line.startswith('[metadata]'): + file_new.write(line) + + # Add a 'version =' line to override the version info. + base, extension = _get_pbr_version() + if extension: + file_new.write('version = ' + '{0}.{1}.{2}\n'.format(base, patch, + extension)) else: - subs[2] = str(patch) - file_new.write('.'.join(subs)) - if len(subs) == 3: - file_new.write('\n') + file_new.write('version = {0}.{1}\n'.format(base, patch)) + + elif line.startswith('version'): + raise ValueError("The file 'setup.cfg' must not already " + "contain a 'version =' line.") else: file_new.write(line) - finally: - file_new.close() - os.rename(temp_name, VERSIONFILE) + + # Replace the original setup.cfg with the modified one. + os.rename(temp_name, VERSIONFILE) + + +def _get_pbr_version(): + """Returns the version stamp from PBR. + + PBR versions are either of the form yyyy.s.bm.devx.gitsha (for milestone + releases) or yyyy.s.devx.gitsha for series releases. This function returns + the base part (yyyy.s) and the optional extension without the devx.gitsha + portions (so either None or bm). The devx.gitsha portion should not be + returned, as it will be supplied by PBR as part of its version generation + process when 'python setup.py sdist' is later invoked. + """ + version_info = pbr.version.VersionInfo('barbican') + base = version_info.version_string() + full = version_info.release_string() + if base != full: + extension = _trim_base_from_version(full, base) + if _is_milestone_release(extension): + return base, extension.split('.')[0] + + return base, None + + +def _trim_base_from_version(full_version, base_version): + """Removes the base version information from the full version.""" + return full_version[len(base_version) + 1:] + + +def _is_milestone_release(extension): + """Tests if extension corresponds to an OpenStack milestone release.""" + return extension.startswith('b') + if __name__ == '__main__': patch = get_patch() - print 'patch: ', patch update_versionfile(patch) diff --git a/setup.cfg b/setup.cfg index 70f291afb..9715f232a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,5 @@ [metadata] name = barbican -version = 2014.1 description = Service for storing sensitive client information for OpenStack description-file = README.md