Test htsworkflow under several different django & python versions
[htsworkflow.git] / version.py
1 # -*- coding: utf-8 -*-
2 # Author: Douglas Creager <dcreager@dcreager.net>
3 # This file is placed into the public domain.
4
5 # Calculates the current version number.  If possible, this is the
6 # output of “git describe”, modified to conform to the versioning
7 # scheme that setuptools uses.  If “git describe” returns an error
8 # (most likely because we're in an unpacked copy of a release tarball,
9 # rather than in a git working copy), then we fall back on reading the
10 # contents of the RELEASE-VERSION file.
11 #
12 # To use this script, simply import it your setup.py file, and use the
13 # results of get_git_version() as your package version:
14 #
15 # from version import *
16 #
17 # setup(
18 #     version=get_git_version(),
19 #     .
20 #     .
21 #     .
22 # )
23 #
24 # This will automatically update the RELEASE-VERSION file, if
25 # necessary.  Note that the RELEASE-VERSION file should *not* be
26 # checked into git; please add it to your top-level .gitignore file.
27 #
28 # You'll probably want to distribute the RELEASE-VERSION file in your
29 # sdist tarballs; to do this, just create a MANIFEST.in file that
30 # contains the following line:
31 #
32 #   include RELEASE-VERSION
33 from __future__ import print_function
34
35 __all__ = ("get_git_version")
36
37 from subprocess import Popen, PIPE
38
39
40 def call_git_describe(abbrev=4):
41     try:
42         p = Popen(['git', 'describe', '--abbrev=%d' % abbrev],
43                   stdout=PIPE, stderr=PIPE)
44         p.stderr.close()
45         line = p.stdout.readlines()[0]
46         return line.strip().decode('utf-8')
47
48     except:
49         return None
50
51
52 def read_release_version():
53     try:
54         f = open("RELEASE-VERSION", "rt")
55
56         try:
57             version = f.readlines()[0]
58             return version.strip()
59
60         finally:
61             f.close()
62
63     except:
64         return None
65
66
67 def write_release_version(version):
68     f = open("RELEASE-VERSION", "wt")
69     f.write("%s\n" % version)
70     f.close()
71
72
73 def get_git_version(abbrev=4):
74     # Read in the version that's currently in RELEASE-VERSION.
75
76     release_version = read_release_version()
77
78     # First try to get the current version using “git describe”.
79
80     version = call_git_describe(abbrev)
81
82     # If that doesn't work, fall back on the value that's in
83     # RELEASE-VERSION.
84
85     if version is None:
86         version = release_version
87
88     # If we still don't have anything, that's an error.
89
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
96     if version != release_version:
97         write_release_version(version)
98
99     # Finally, return the current version.
100
101     return version
102
103
104 if __name__ == "__main__":
105     print(get_git_version())