From 149dcbae2d12587fb687b81e3a1a4631f04db4d7 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 5 Jun 2013 19:27:03 +0200 Subject: hostinfo: add -V and --version to get the version The version is generated from the git-tags during build (setup.py) or during run time, if executed from the git repository. --- bin/hostinfo | 20 ++++++++++++++++++++ contrib/bash-completion | 2 +- setup.py | 3 ++- version.py | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 version.py diff --git a/bin/hostinfo b/bin/hostinfo index f1453e9..8700be7 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -5,6 +5,7 @@ import sys import argparse import yaml import os +import pkg_resources from dns import resolver, reversename OWN_DIRECTORY = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) @@ -190,6 +191,20 @@ def find_host(basepath, host): return None +def print_version_and_exit(): + ver = None + try: + import version + ver = version.get_git_version() + except: + ver = pkg_resources.require("hostinfo-tools")[0].version + + if ver is None: + sys.stderr.write('Unable to identify the version information.') + sys.exit(1) + print("hostinfo-tools %s" % ver) + sys.exit(0) + def main(): basepath = '/usr/local/share/hostinfo' if 'HOSTINFO_PATH' in os.environ and os.environ['HOSTINFO_PATH'] != '': @@ -220,8 +235,13 @@ def main(): help="only print the hostname of the matching entries") parser.add_argument("-d", "--details", action="store_true", help="print details about matching hosts") + parser.add_argument("-V", "--version", action="store_true", + help="only print the version number and exit") args = parser.parse_args() + if args.version: + print_version_and_exit() + if args.path: basepath = args.path diff --git a/contrib/bash-completion b/contrib/bash-completion index 4d64f08..ac99866 100644 --- a/contrib/bash-completion +++ b/contrib/bash-completion @@ -50,7 +50,7 @@ _hostinfo() { COMPREPLY=( $( compgen -W "-o --oneline -f --file -k --keys -h -? --help -p --path -n --nospaces -l --hosts -s --short -v --verbose --only-names -d --details - $_completion" -- $cur ) ) + -V --version $_completion" -- $cur ) ) fi } complete -F _hostinfo hostinfo diff --git a/setup.py b/setup.py index 5a238e4..44f74a8 100755 --- a/setup.py +++ b/setup.py @@ -1,9 +1,10 @@ #!/usr/bin/env python from distutils.core import setup +from version import * setup(name='hostinfo-tools', - version='0.1.0', + version=get_git_version(), description='Hostinfo database interface scripts', author='Alexander Sulfrian', author_email='alex@spline.inf.fu-berlin.de', diff --git a/version.py b/version.py new file mode 100644 index 0000000..a4c7ed6 --- /dev/null +++ b/version.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# To use this script, simply import it your setup.py file, and use the +# results of get_git_version() as your package version: +# +# from version import * +# +# setup( +# version=get_git_version(), +# . +# . +# . +# ) + +__all__ = ["get_git_version"] + +import os +import re +from subprocess import Popen, PIPE + +OWN_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) + +def call_git_describe(abbrev=4): + try: + p = Popen(['git', 'describe', '--abbrev=%d' % abbrev, '--tags'], + cwd=OWN_DIR, stdout=PIPE, stderr=PIPE) + p.stderr.close() + line = p.stdout.readlines()[0] + return line.strip() + + except: + return None + +def get_git_version(abbrev=4): + version = call_git_describe(abbrev) + if version is None: + raise ValueError("Cannot find the version number!") + + return re.sub('^debian/', '', version) + +if __name__ == "__main__": + print get_git_version() -- cgit v1.2.3-1-g7c22