Skip to content

Instantly share code, notes, and snippets.

@cberner
Forked from dcreager/version.py
Created December 3, 2011 19:09
Show Gist options
  • Save cberner/1427859 to your computer and use it in GitHub Desktop.
Save cberner/1427859 to your computer and use it in GitHub Desktop.
Extract a setuptools version from the git repository
# -*- coding: utf-8 -*-
# Author: Douglas Creager <dcreager@dcreager.net>, Christopher Berner <christopherberner@gmail.com>
# This file is placed into the public domain.
# Calculates the current version number. This is the
# output of “git describe”.
#
# To use this script, simply import it your setup.py file, and use the
# results of get_git_version() as your package version:
#
# import gitversion
#
# setup(
# version=gitversion.get_git_version(),
# .
# .
# .
# )
__all__ = ("get_git_version")
from subprocess import Popen, PIPE
def call_git_describe(abbrev=4):
try:
#Need the long option, in case the distance is 0
p = Popen(['git', 'describe', '--abbrev=%d' % abbrev, '--long'],
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 we still don't have anything, that's an error.
if version is None:
raise ValueError("Cannot find the version number!")
# Finally, return the current version.
return version
if __name__ == "__main__":
print get_git_version()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment