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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Tries to find out the repository revision of the current working directory
# using bzr or debian/changelog
import os
import sys
import os.path as p
import subprocess
import re
import string
# Support for bzr local branches
try:
from bzrlib.branch import Branch
from bzrlib.bzrdir import BzrDir
from bzrlib.errors import NotBranchError
__has_bzrlib = True
except ImportError:
__has_bzrlib = False
base_path = p.abspath(p.join(p.dirname(__file__), p.pardir))
def detect_debian_version():
"""Parse bzr revision and branch information from debian/changelog."""
if sys.platform.startswith('win'):
return None
fname = p.join(base_path, 'debian/changelog')
if not p.exists(fname):
return None
f = open(fname)
version = f.readline()
# bzr5905-210307251546
pattern = re.compile('bzr[0-9]+-[0-9]+')
m = pattern.search(version)
if m == None:
return None
version = version[m.start():m.end()]
return version
def detect_git_revision():
if not sys.platform.startswith('linux') and \
not sys.platform.startswith('darwin'):
git_revnum = os.popen(
'git show --pretty=format:%h | head -n 1').read().rstrip()
if git_revnum:
return 'unofficial-git-%s' % (git_revnum,)
else:
return None
is_git_workdir = os.system('git show >/dev/null 2>&1') == 0
if is_git_workdir:
git_revnum = os.popen(
'git show --pretty=format:%h | head -n 1').read().rstrip()
return 'unofficial-git-%s' % (git_revnum,)
def check_for_explicit_version():
"""Checks for a file WL_RELEASE in the root directory.
It then defaults to this version without further trying to find
which revision we're on
"""
fname = p.join(base_path, 'WL_RELEASE')
if os.path.exists(fname):
return open(fname).read().strip()
def detect_bzr_revision():
if __has_bzrlib:
b = BzrDir.open(base_path).open_branch()
revno, nick = b.revno(), b.nick
else:
# Windows stand alone installer do not come with bzrlib. We try to
# parse the output of bzr then directly
try:
run_bzr = lambda subcmd: subprocess.Popen(
['bzr', subcmd], stdout=subprocess.PIPE, cwd=base_path
).stdout.read().strip().decode('utf-8')
revno = run_bzr('revno')
nick = run_bzr('nick')
except OSError:
return None
return 'bzr%s[%s] ' % (revno, nick)
def detect_revision():
for func in (
check_for_explicit_version,
detect_git_revision,
detect_bzr_revision,
detect_debian_version):
rv = func()
if rv:
return rv
return 'REVDETECT-BROKEN-PLEASE-REPORT-THIS'
if __name__ == '__main__':
print(detect_revision())
|