Debian Codegraph @

rpython/tool/version.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 from __future__ import print_function
2 import time
3 import py
4 import os
5 from subprocess import Popen, PIPE
6 import rpython
7 rpythondir = os.path.dirname(os.path.realpath(rpython.__file__))
8 rpythonroot = os.path.dirname(rpythondir)
9 default_retval = '?', '?'
10
11 def maywarn(err, repo_type='Mercurial'):
12 if not err:
13 return
14
15 from rpython.tool.ansi_print import AnsiLogger
16 log = AnsiLogger("version")
17 log.WARNING('Errors getting %s information: %s' % (repo_type, err))
18
19 CACHED_RESULT = None
20 CACHED_ARGS = None
21
22 def get_repo_version_info(hgexe=None, root=rpythonroot):
23 '''Obtain version information by invoking the 'hg' or 'git' commands.'''
24 global CACHED_RESULT, CACHED_ARGS
25 key = (hgexe, root)
26 if CACHED_RESULT is not None and CACHED_ARGS == key:
27 return CACHED_RESULT
28 res = _get_repo_version_info(hgexe, root)
29 CACHED_RESULT = res
30 CACHED_ARGS = key
31 return res
32
33 def _get_repo_version_info(hgexe, root):
•34 # Debian: built from a source tarball
35 p = Popen(('dpkg-parsechangelog',), stdout=PIPE, cwd=rpythonroot)
36 if p.wait() != 0:
37 maywarn(p.stderr.read(), 'dpkg-parsechangelog')
38 return default_retval
39 for line in p.stdout.read().split('\n'):
40 if line.split(':', 1)[0].strip() == 'Version':
41 version = line.split(':', 1)[1].strip()
42 return '', version
43
44 # Try to see if we can get info from Git if hgexe is not specified.
45 if not hgexe:
46 gitfile = os.path.join(root, '.git')
47 if os.path.isdir(gitfile):
48 return _get_git_version(root)
49 elif os.path.exists(gitfile):
50 # Can be a file with a relative path like
51 # gitdir: ../.git/modules/pypy
52 with open(gitfile) as fid:
53 contents = fid.read()
54 if contents.startswith('gitdir:'):
55 return _get_git_version(root)
56
57 # Fallback to trying Mercurial.
58 if hgexe is None:
59 hgexe = py.path.local.sysfind('hg')
60
61 if os.path.isfile(os.path.join(root, '.hg_archival.txt')):
62 return _get_hg_archive_version(os.path.join(root, '.hg_archival.txt'))
63 elif not os.path.isdir(os.path.join(root, '.hg')):
64 maywarn('Not running from a Mercurial repository!')
65 return default_retval
66 elif not hgexe:
67 maywarn('Cannot find Mercurial command!')
68 return default_retval
69 else:
70 return _get_hg_version(hgexe, root)
71
72
73 def _get_hg_version(hgexe, root):
74 env = dict(os.environ)
75 # get Mercurial into scripting mode
76 env['HGPLAIN'] = '1'
77 # disable user configuration, extensions, etc.
78 env['HGRCPATH'] = os.devnull
79
80 try:
81 p = Popen([str(hgexe), 'version', '-q'],
82 stdout=PIPE, stderr=PIPE, env=env, universal_newlines=True)
83 except OSError as e:
84 maywarn(e)
85 return default_retval
86
87 if not p.stdout.read().startswith('Mercurial Distributed SCM'):
88 maywarn('command does not identify itself as Mercurial')
89 return default_retval
90
91 p = Popen([str(hgexe), 'id', '--template', r"{id}\n{tags}\n{branch}\n", root],
92 stdout=PIPE, stderr=PIPE, env=env,
93 universal_newlines=True)
94 hgout = p.stdout.read().strip()
95 if p.wait() != 0:
96 maywarn(p.stderr.read())
97 hgout = '?\n?\n?'
98 hgid, hgtags, hgbranch = hgout.strip().split("\n", 3)
99 hgtags = [t for t in hgtags.strip().split() if t != 'tip']
100
101 if hgtags:
102 return hgtags[0], hgid
103 else:
104 return hgbranch, hgid
105
106
107 def _get_hg_archive_version(path):
108 with open(path) as fp:
109 # reverse the order since there may be more than one tag
110 # and the latest tag will be first, so make it last instead
111 data = dict((x.split(': ', 1) for x in fp.read().splitlines()[::-1]))
112 if 'tag' in data:
113 return data['tag'], data['node']
114 else:
115 return data['branch'], data['node']
116
117
118 def _get_git_version(root):
119 #XXX: this function is a untested hack,
120 # so the git mirror tav made will work
121 gitexe = py.path.local.sysfind('git')
122 if not gitexe:
123 return default_retval
124
125 try:
126 p = Popen(
127 [str(gitexe), 'rev-parse', 'HEAD'],
128 stdout=PIPE, stderr=PIPE, cwd=root,
129 universal_newlines=True,
130 )
131 except OSError as e:
132 maywarn(e, 'Git')
133 return default_retval
134 if p.wait() != 0:
135 maywarn(p.stderr.read(), 'Git')
136 return default_retval
137 revision_id = p.stdout.read().strip()[:12]
138 p = Popen(
139 [str(gitexe), 'describe', '--tags', '--exact-match'],
140 stdout=PIPE, stderr=PIPE, cwd=root,
141 universal_newlines=True,
142 )
143 if p.wait() != 0:
144 p = Popen(
145 [str(gitexe), 'branch'], stdout=PIPE, stderr=PIPE,
146 cwd=root, universal_newlines=True,
147 )
148 if p.wait() != 0:
149 maywarn(p.stderr.read(), 'Git')
150 return 'pypy-HEAD', revision_id
151 branch = '?'
152 for line in p.stdout.read().strip().split('\n'):
153 if line.startswith('* '):
154 branch = line[1:].strip()
155 if branch == '(no branch)':
156 branch = 'pypy-HEAD'
157 if branch.startswith("(HEAD detached"):
158 branch = 'pypy-HEAD'
159 break
160 return branch, revision_id
161 return p.stdout.read().strip(), revision_id
162
163
164 if __name__ == '__main__':
165 print(get_repo_version_info())