forked from deluge-torrent/deluge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.py
executable file
·78 lines (65 loc) · 2.3 KB
/
version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# Authors: Douglas Creager <[email protected]>
# Calum Lind <[email protected]>
#
# This file is placed into the public domain.
#
# Calculates the current version number by first checking output of “git describe”,
# modified to conform to PEP 386 versioning scheme. If “git describe” fails
# (likely due to using release tarball rather than git working copy), then fall
# back on reading the contents of the RELEASE-VERSION file.
#
# Usage: Import in setup.py, and use result of get_version() as package version:
#
# from version import *
#
# setup(
# ...
# version=get_version(),
# ...
# )
#
# Script will automatically update the RELEASE-VERSION file, if needed.
# Note that RELEASE-VERSION file should *not* be checked into git; please add
# it to your top-level .gitignore file.
#
# You'll probably want to distribute the RELEASE-VERSION file in your
# sdist tarballs; to do this, just create a MANIFEST.in file that
# contains the following line:
#
# include RELEASE-VERSION
#
import os
import subprocess
__all__ = ('get_version',)
VERSION_FILE = os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION')
def call_git_describe(prefix='', suffix=''):
cmd = 'git describe --tags --match %s[0-9]*' % prefix
try:
output = subprocess.check_output(cmd.split(), stderr=subprocess.PIPE)
except (OSError, subprocess.CalledProcessError):
return None
else:
version = output.decode('utf-8').strip().replace(prefix, '')
# A dash signifies git commit increments since parent tag.
if '-' in version:
segment = '.dev' if 'dev' in version else '.post'
version = segment.join(version.replace(suffix, '').split('-')[:2])
return version
def get_version(prefix='deluge-', suffix='.dev0'):
try:
with open(VERSION_FILE) as f:
release_version = f.readline().strip()
except OSError:
release_version = None
version = call_git_describe(prefix, suffix)
if not version:
version = release_version
if not version:
raise ValueError('Cannot find the version number!')
if version != release_version:
with open(VERSION_FILE, 'w') as f:
f.write('%s\n' % version)
return version
if __name__ == '__main__':
print(get_version())