Skip to content

Instantly share code, notes, and snippets.

@cjmeyer
Created March 7, 2012 15:10
Show Gist options
  • Save cjmeyer/1993716 to your computer and use it in GitHub Desktop.
Save cjmeyer/1993716 to your computer and use it in GitHub Desktop.
Python: Generate version number from Git commit description.
import time
from subprocess import Popen, PIPE
try:
describe = 'git describe --abbrev=8 --dirty'
version = Popen(describe.split(), stdout=PIPE).communicate()[0].strip()
except:
version = None
else:
if version.endswith('dirty'):
version = version[:-5] + time.strftime('%Y%m%d%H%M')
with open('app/__version__.py', 'w') as f:
f.write('# this file is autogenerated by setup.py\n')
f.write('version = "{0}"\n'.format(version))
try:
from app import __version__
version = __version__.version
except ImportError:
version = 'unknown'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment