Skip to content

Commit

Permalink
Update Python package to use the version from the git tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Cribbs committed Oct 7, 2013
1 parent 2adf207 commit 2a9209d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include version.py
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python

from setuptools import setup
import version

setup(name='riak_pb',
version='2.0.0.7',
version=version.get_version(),
description='Riak Protocol Buffers Messages',
packages=['riak_pb'],
requires=['protobuf(==2.4.1)'],
Expand Down
89 changes: 89 additions & 0 deletions version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# This program is placed into the public domain.

"""
Gets the current version number.
If in a git repository, it is the current git tag.
Otherwise it is the one contained in the PKG-INFO file.
To use this script, simply import it in your setup.py file
and use the results of get_version() as your package version:
from version import *
setup(
...
version=get_version(),
...
)
"""

__all__ = ('get_version')

from os.path import dirname, isdir, join
import re
from subprocess import CalledProcessError, Popen, PIPE

try:
from subprocess import check_output
except ImportError:
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use stderr=STDOUT.
>>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd, output=output)
return output

version_re = re.compile('^Version: (.+)$', re.M)


def get_version():
d = dirname(__file__)

if isdir(join(d, '.git')):
# Get the version using "git describe".
cmd = 'git describe --tags --match [0-9]*'.split()
try:
version = check_output(cmd).decode().strip()
except CalledProcessError:
print('Unable to get version number from git tags')
exit(1)

# PEP 386 compatibility
if '-' in version:
version = '.post'.join(version.split('-')[:2])

else:
# Extract the version from the PKG-INFO file.
with open(join(d, 'PKG-INFO')) as f:
version = version_re.search(f.read()).group(1)

return version


if __name__ == '__main__':
print(get_version())

0 comments on commit 2a9209d

Please sign in to comment.