Skip to content

Commit e2e5e93

Browse files
committed
Get version number from git
1 parent 79e86aa commit e2e5e93

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/docs/_build
44
/python_bioformats.egg-info
55
/dist
6+
/bioformats/_version.py

bioformats/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
'''Bioformats package - wrapper for loci.bioformats java code
99
1010
'''
11-
__version__ = "$Revision$"
11+
12+
try:
13+
from _version import __version__
14+
except ImportError:
15+
# We're running in a tree that doesn't have a _version.py, so we don't know what our version is.
16+
__version__ = "0.0.0"
1217

1318
import os.path
1419
import javabridge

setup.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1+
import os.path
2+
import re
3+
import subprocess
14
from setuptools import setup, find_packages
25

6+
def get_version():
7+
"""Get version from git or file system.
8+
9+
If this is a git repository, try to get the version number by
10+
running ``git describe``, then store it in
11+
bioformats/_version.py. Otherwise, try to load the version number
12+
from that file. If both methods fail, quietly return None.
13+
14+
"""
15+
git_version = None
16+
if os.path.exists(os.path.join(os.path.dirname(__file__), '.git')):
17+
import subprocess
18+
try:
19+
git_version = subprocess.check_output(['git', 'describe']).strip()
20+
except:
21+
pass
22+
23+
version_file = os.path.join(os.path.dirname(__file__), 'bioformats',
24+
'_version.py')
25+
if os.path.exists(version_file):
26+
with open(version_file) as f:
27+
cached_version_line = f.read().strip()
28+
try:
29+
# From http://stackoverflow.com/a/3619714/17498
30+
cached_version = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
31+
cached_version_line, re.M).group(1)
32+
except:
33+
raise RuntimeError("Unable to find version in %s" % version_file)
34+
else:
35+
cached_version = None
36+
37+
if git_version and git_version != cached_version:
38+
with open(version_file, 'w') as f:
39+
print >>f, '__version__ = "%s"' % git_version
40+
41+
return git_version or cached_version
42+
343
setup(
444
name="python-bioformats",
5-
version="1.0.0pr3",
45+
version=get_version(),
646
description="Read and write life sciences file formats",
747
long_description='''Python-bioformats is a Python wrapper for Bio-Formats, a standalone
848
Java library for reading and writing life sciences image file

0 commit comments

Comments
 (0)