From 8129865433b72f987410d762d9f0d00cfdb22287 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Fri, 10 Oct 2014 17:14:27 +0100 Subject: [PATCH] Pick up versioning using pkg_resources or pbr Use pkg_resources to extract the version from PKG_INFO or fallback to using pbr presumably if within a source checkout to determine the correct version to report. Change-Id: Id70cc4a1fc6e66114461d469cef65b196acec079 --- git_upstream/__init__.py | 18 +++++++++ git_upstream/main.py | 4 +- git_upstream/version.py | 87 ---------------------------------------- 3 files changed, 20 insertions(+), 89 deletions(-) delete mode 100644 git_upstream/version.py diff --git a/git_upstream/__init__.py b/git_upstream/__init__.py index 6cbcb3f..9ef6e61 100644 --- a/git_upstream/__init__.py +++ b/git_upstream/__init__.py @@ -14,3 +14,21 @@ # See the License for the specific language governing permissions and # limitations under the License. # + +import pkg_resources + + +try: + # First, try to get our version out of PKG-INFO. If we're installed, + # this'll let us find our version without pulling in pbr. After all, if + # we're installed on a system, we're not in a Git-managed source tree, so + # pbr doesn't really buy us anything. + __version__ = __canonical_version__ = pkg_resources.get_provider( + pkg_resources.Requirement.parse('git-upstream')).version +except pkg_resources.DistributionNotFound: + # No PKG-INFO? We're probably running from a checkout, then. Let pbr do + # its thing to figure out a version number. + import pbr.version + _version_info = pbr.version.VersionInfo('git-upstream') + __version__ = _version_info.release_string() + __canonical_version__ = _version_info.version_string() diff --git a/git_upstream/main.py b/git_upstream/main.py index e525e43..83e1204 100644 --- a/git_upstream/main.py +++ b/git_upstream/main.py @@ -25,7 +25,7 @@ import git import git_upstream.commands as commands from git_upstream.errors import GitUpstreamError import git_upstream.log as log -import git_upstream.version +from git_upstream import __version__ import subcommand import argparse @@ -45,7 +45,7 @@ def get_parser(): epilog='See "%(prog)s help COMMAND" for help on a specific command.', add_help=False) parser.add_argument('--version', action='version', - version='%(prog)s ' + git_upstream.version.version) + version='%(prog)s ' + __version__) parser.add_argument('-h', '--help', action='help', help='show this help message and exit') group = parser.add_mutually_exclusive_group() diff --git a/git_upstream/version.py b/git_upstream/version.py deleted file mode 100644 index a579564..0000000 --- a/git_upstream/version.py +++ /dev/null @@ -1,87 +0,0 @@ -# -# Copyright (c) 2011, 2012 OpenStack LLC. -# Copyright (c) 2012, 2013, 2014 Hewlett-Packard Development Company, L.P. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import datetime -import os -import subprocess - -VERBOSE = os.environ.get('VERBOSE', False) - - -class CommandException(Exception): - pass - - -# following function is taken from git-review -def run_command(cmd, status=False, env=None): - if not env: - env = {} - if VERBOSE: - print(datetime.datetime.now(), "Running:", cmd) - new_env = os.environ - new_env.update(env) - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, env=new_env) - (out, nothing) = p.communicate() - if p.returncode: - raise CommandException - out = out.decode('utf-8') - if status: - return (p.returncode, out.strip()) - return out.strip() - - -def git_describe_version(): - try: - v = run_command(['git', 'describe', '--tags', '--dirty']) - except CommandException: - raise - - return v - - -def git_upstream_version(): - try: - from git_upstream import git_upstream_version - except ImportError: - raise - return git_upstream_version.version - - -def write_version_file(): - try: - v = git_describe_version() - except: - return - print __name__ - with open(os.path.join(os.path.dirname(__file__), - "git_upstream_version.py"), 'w') as f: - f.write("# Auto-generated file, do not edit by hand") - f.write("version = %s" % v) - - -def get_version(): - for vfunc in git_upstream_version, git_describe_version: - try: - return vfunc() - except: - pass - - return 'unknown-version' - - -version = get_version()