Debian Codegraph @

rpython/tool/version.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 # Try to see if we can get info from Git if hgexe is not specified.
35 if not hgexe:
36 gitfile = os.path.join(root, '.git')
37 if os.path.isdir(gitfile):
38 return _get_git_version(root)
39 elif os.path.exists(gitfile):
40 # Can be a file with a relative path like
41 # gitdir: ../.git/modules/pypy
42 with open(gitfile) as fid:
43 contents = fid.read()
44 if contents.startswith('gitdir:'):
45 return _get_git_version(root)
46
47 # Fallback to trying Mercurial.
48 if hgexe is None:
49 hgexe = py.path.local.sysfind('hg')
50
51 if os.path.isfile(os.path.join(root, '.hg_archival.txt')):
52 return _get_hg_archive_version(os.path.join(root, '.hg_archival.txt'))
53 elif not os.path.isdir(os.path.join(root, '.hg')):
54 maywarn('Not running from a Mercurial repository!')
55 return default_retval
56 elif not hgexe:
57 maywarn('Cannot find Mercurial command!')
58 return default_retval
59 else:
60 return _get_hg_version(hgexe, root)
61
62
63 def _get_hg_version(hgexe, root):
64 env = dict(os.environ)
65 # get Mercurial into scripting mode
66 env['HGPLAIN'] = '1'
67 # disable user configuration, extensions, etc.
68 env['HGRCPATH'] = os.devnull
69
70 try:
71 p = Popen([str(hgexe), 'version', '-q'],
72 stdout=PIPE, stderr=PIPE, env=env, universal_newlines=True)
73 except OSError as e:
74 maywarn(e)
75 return default_retval
76
77 if not p.stdout.read().startswith('Mercurial Distributed SCM'):
78 maywarn('command does not identify itself as Mercurial')
79 return default_retval
80
81 p = Popen([str(hgexe), 'id', '--template', r"{id}\n{tags}\n{branch}\n", root],
82 stdout=PIPE, stderr=PIPE, env=env,
83 universal_newlines=True)
84 hgout = p.stdout.read().strip()
85 if p.wait() != 0:
86 maywarn(p.stderr.read())
87 hgout = '?\n?\n?'
88 hgid, hgtags, hgbranch = hgout.strip().split("\n", 3)
89 hgtags = [t for t in hgtags.strip().split() if t != 'tip']
90
91 if hgtags:
92 return hgtags[0], hgid
93 else:
94 return hgbranch, hgid
95
96
97 def _get_hg_archive_version(path):
98 with open(path) as fp:
99 # reverse the order since there may be more than one tag
100 # and the latest tag will be first, so make it last instead
101 data = dict((x.split(': ', 1) for x in fp.read().splitlines()[::-1]))
102 if 'tag' in data:
103 return data['tag'], data['node']
104 else:
105 return data['branch'], data['node']
106
107
108 def _get_git_version(root):
109 #XXX: this function is a untested hack,
110 # so the git mirror tav made will work
111 gitexe = py.path.local.sysfind('git')
112 if not gitexe:
113 return default_retval
114
115 try:
116 p = Popen(
117 [str(gitexe), 'rev-parse', 'HEAD'],
118 stdout=PIPE, stderr=PIPE, cwd=root,
119 universal_newlines=True,
120 )
121 except OSError as e:
122 maywarn(e, 'Git')
123 return default_retval
124 if p.wait() != 0:
125 maywarn(p.stderr.read(), 'Git')
126 return default_retval
127 revision_id = p.stdout.read().strip()[:12]
128 p = Popen(
129 [str(gitexe), 'describe', '--tags', '--exact-match'],
130 stdout=PIPE, stderr=PIPE, cwd=root,
131 universal_newlines=True,
132 )
133 if p.wait() != 0:
134 p = Popen(
135 [str(gitexe), 'branch'], stdout=PIPE, stderr=PIPE,
136 cwd=root, universal_newlines=True,
137 )
138 if p.wait() != 0:
139 maywarn(p.stderr.read(), 'Git')
140 return 'pypy-HEAD', revision_id
141 branch = '?'
142 for line in p.stdout.read().strip().split('\n'):
143 if line.startswith('* '):
144 branch = line[1:].strip()
145 if branch == '(no branch)':
146 branch = 'pypy-HEAD'
147 if branch.startswith("(HEAD detached"):
148 branch = 'pypy-HEAD'
149 break
150 return branch, revision_id
151 return p.stdout.read().strip(), revision_id
152
153
154 if __name__ == '__main__':
155 print(get_repo_version_info())