Skip to content

Commit e606322

Browse files
committed
Add versioning back in.
1 parent c1c5ef5 commit e606322

File tree

4 files changed

+196
-1
lines changed

4 files changed

+196
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ sbp_out.*
6262

6363
# Virtual Envs
6464
/*env
65+
66+
RELEASE-VERSION

python/MANIFEST.IN

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include *.txt
77
include .coveragerc
88
include .gitignore
99
include LICENSE
10+
include RELEASE-VERSION
1011
include tox.ini
1112
recursive-include sbp/ *.py
1213
prune docs/_build

python/sbp/version.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Author: Douglas Creager <dcreager@dcreager.net>
4+
# This file is placed into the public domain.
5+
# Adapted from Gist: https://gist.github.com/3067886
6+
7+
# Calculates the current version number. If possible, this is the
8+
# output of “git describe”, modified to conform to the versioning
9+
# scheme that setuptools uses. If “git describe” returns an error
10+
# (most likely because we're in an unpacked copy of a release tarball,
11+
# rather than in a git working copy), then we fall back on reading the
12+
# contents of the RELEASE-VERSION file.
13+
#
14+
# To use this script, simply import it your setup.py file, and use the
15+
# results of get_git_version() as your package version:
16+
#
17+
# from version import *
18+
#
19+
# setup(
20+
# version=get_git_version(),
21+
# .
22+
# .
23+
# .
24+
# )
25+
#
26+
# This will automatically update the RELEASE-VERSION file, if
27+
# necessary. Note that the RELEASE-VERSION file should *not* be
28+
# checked into git; please add it to your top-level .gitignore file.
29+
#
30+
# You'll probably want to distribute the RELEASE-VERSION file in your
31+
# sdist tarballs; to do this, just create a MANIFEST.in file that
32+
# contains the following line:
33+
#
34+
# include RELEASE-VERSION
35+
36+
__all__ = ("get_git_version")
37+
38+
from subprocess import Popen, PIPE
39+
import os
40+
41+
def call_git_describe():
42+
try:
43+
p = Popen(['git', 'describe', '--tags', '--dirty', '--always'],
44+
stdout=PIPE, stderr=PIPE)
45+
p.stderr.close()
46+
line = p.stdout.readlines()[0]
47+
return line.strip()
48+
except:
49+
return None
50+
51+
52+
def read_release_version():
53+
try:
54+
f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "r")
55+
try:
56+
version = f.readlines()[0]
57+
return version.strip()
58+
finally:
59+
f.close()
60+
except:
61+
return None
62+
63+
64+
def write_release_version(version):
65+
f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "w")
66+
f.write("%s\n" % version)
67+
f.close()
68+
69+
70+
def get_git_version():
71+
# Read in the version that's currently in RELEASE-VERSION.
72+
release_version = read_release_version()
73+
74+
# First try to get the current version using “git describe”.
75+
version = call_git_describe()
76+
77+
# Take off the leading if present.
78+
if version[0] == 'v':
79+
version = version[1:]
80+
81+
#adapt to PEP 386 compatible versioning scheme
82+
version = pep386adapt(version)
83+
84+
# If that doesn't work, fall back on the value that's in
85+
# RELEASE-VERSION.
86+
if version is None:
87+
version = release_version
88+
89+
# If we still don't have anything, that's an error.
90+
if version is None:
91+
raise ValueError("Cannot find the version number!")
92+
93+
# If the current version is different from what's in the
94+
# RELEASE-VERSION file, update the file to be current.
95+
if version != release_version:
96+
write_release_version(version)
97+
98+
# Finally, return the current version.
99+
return version
100+
101+
102+
def pep386adapt(version):
103+
if version is not None and '-' in version:
104+
# adapt git-describe version to be in line with PEP 386
105+
# Break PEP 386 a bit here and append the Git hash
106+
parts = version.split('-')
107+
if len(parts) > 2:
108+
version = '%s.post%s-%s' % (
109+
parts[0], parts[1],
110+
'-'.join(parts[2:])
111+
)
112+
return version
113+
else:
114+
return version
115+
116+
VERSION = get_git_version()
117+
118+
if __name__ == "__main__":
119+
print get_git_version()
120+

python/setup.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,81 @@
11
#!/usr/bin/env python
22

33
from setuptools import setup
4+
from subprocess import Popen, PIPE
45
import os
56

6-
VERSION = "0.44"
7+
def call_git_describe():
8+
try:
9+
p = Popen(['git', 'describe', '--tags', '--dirty', '--always'],
10+
stdout=PIPE, stderr=PIPE)
11+
p.stderr.close()
12+
line = p.stdout.readlines()[0]
13+
return line.strip()
14+
except:
15+
return None
16+
17+
def read_release_version():
18+
try:
19+
f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "r")
20+
try:
21+
version = f.readlines()[0]
22+
return version.strip()
23+
finally:
24+
f.close()
25+
except:
26+
return None
27+
28+
def write_release_version(version):
29+
f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "w")
30+
f.write("%s\n" % version)
31+
f.close()
32+
33+
def pep386adapt(version):
34+
if version is not None and '-' in version:
35+
# adapt git-describe version to be in line with PEP 386
36+
# Break PEP 386 a bit here and append the Git hash
37+
parts = version.split('-')
38+
if len(parts) > 2:
39+
version = '%s.post%s-%s' % (
40+
parts[0], parts[1],
41+
'-'.join(parts[2:])
42+
)
43+
return version
44+
else:
45+
return version
46+
47+
def get_git_version():
48+
# Read in the version that's currently in RELEASE-VERSION.
49+
release_version = read_release_version()
50+
51+
# First try to get the current version using 'git describe'.
52+
version = call_git_describe()
53+
54+
# Take off the leading if present.
55+
if version[0] == 'v':
56+
version = version[1:]
57+
58+
#adapt to PEP 386 compatible versioning scheme
59+
version = pep386adapt(version)
60+
61+
# If that doesn't work, fall back on the value that's in
62+
# RELEASE-VERSION.
63+
if version is None:
64+
version = release_version
65+
66+
# If we still don't have anything, that's an error.
67+
if version is None:
68+
raise ValueError("Cannot find the version number!")
69+
70+
# If the current version is different from what's in the
71+
# RELEASE-VERSION file, update the file to be current.
72+
if version != release_version:
73+
write_release_version(version)
74+
75+
# Finally, return the current version.
76+
return version
77+
78+
VERSION = get_git_version()
779

880
CLASSIFIERS = [
981
'Intended Audience :: Developers',

0 commit comments

Comments
 (0)