##// END OF EJS Templates
merge with stable
Augie Fackler -
r30417:854190be merge default
parent child Browse files
Show More
@@ -1,1314 +1,1326 b''
1 1 #
2 2 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
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 cgi
11 11 import copy
12 12 import mimetypes
13 13 import os
14 14 import re
15 15
16 16 from ..i18n import _
17 17 from ..node import hex, short
18 18
19 19 from .common import (
20 20 ErrorResponse,
21 21 HTTP_FORBIDDEN,
22 22 HTTP_NOT_FOUND,
23 23 HTTP_OK,
24 24 get_contact,
25 25 paritygen,
26 26 staticfile,
27 27 )
28 28
29 29 from .. import (
30 30 archival,
31 31 encoding,
32 32 error,
33 33 graphmod,
34 34 revset,
35 35 scmutil,
36 36 templatefilters,
37 37 templater,
38 38 util,
39 39 )
40 40
41 41 from . import (
42 42 webutil,
43 43 )
44 44
45 45 __all__ = []
46 46 commands = {}
47 47
48 48 class webcommand(object):
49 49 """Decorator used to register a web command handler.
50 50
51 51 The decorator takes as its positional arguments the name/path the
52 52 command should be accessible under.
53 53
54 54 Usage:
55 55
56 56 @webcommand('mycommand')
57 57 def mycommand(web, req, tmpl):
58 58 pass
59 59 """
60 60
61 61 def __init__(self, name):
62 62 self.name = name
63 63
64 64 def __call__(self, func):
65 65 __all__.append(self.name)
66 66 commands[self.name] = func
67 67 return func
68 68
69 69 @webcommand('log')
70 70 def log(web, req, tmpl):
71 71 """
72 72 /log[/{revision}[/{path}]]
73 73 --------------------------
74 74
75 75 Show repository or file history.
76 76
77 77 For URLs of the form ``/log/{revision}``, a list of changesets starting at
78 78 the specified changeset identifier is shown. If ``{revision}`` is not
79 79 defined, the default is ``tip``. This form is equivalent to the
80 80 ``changelog`` handler.
81 81
82 82 For URLs of the form ``/log/{revision}/{file}``, the history for a specific
83 83 file will be shown. This form is equivalent to the ``filelog`` handler.
84 84 """
85 85
86 86 if 'file' in req.form and req.form['file'][0]:
87 87 return filelog(web, req, tmpl)
88 88 else:
89 89 return changelog(web, req, tmpl)
90 90
91 91 @webcommand('rawfile')
92 92 def rawfile(web, req, tmpl):
93 93 guessmime = web.configbool('web', 'guessmime', False)
94 94
95 95 path = webutil.cleanpath(web.repo, req.form.get('file', [''])[0])
96 96 if not path:
97 97 content = manifest(web, req, tmpl)
98 98 req.respond(HTTP_OK, web.ctype)
99 99 return content
100 100
101 101 try:
102 102 fctx = webutil.filectx(web.repo, req)
103 103 except error.LookupError as inst:
104 104 try:
105 105 content = manifest(web, req, tmpl)
106 106 req.respond(HTTP_OK, web.ctype)
107 107 return content
108 108 except ErrorResponse:
109 109 raise inst
110 110
111 111 path = fctx.path()
112 112 text = fctx.data()
113 113 mt = 'application/binary'
114 114 if guessmime:
115 115 mt = mimetypes.guess_type(path)[0]
116 116 if mt is None:
117 117 if util.binary(text):
118 118 mt = 'application/binary'
119 119 else:
120 120 mt = 'text/plain'
121 121 if mt.startswith('text/'):
122 122 mt += '; charset="%s"' % encoding.encoding
123 123
124 124 req.respond(HTTP_OK, mt, path, body=text)
125 125 return []
126 126
127 127 def _filerevision(web, req, tmpl, fctx):
128 128 f = fctx.path()
129 129 text = fctx.data()
130 130 parity = paritygen(web.stripecount)
131 131
132 132 if util.binary(text):
133 133 mt = mimetypes.guess_type(f)[0] or 'application/octet-stream'
134 134 text = '(binary:%s)' % mt
135 135
136 136 def lines():
137 137 for lineno, t in enumerate(text.splitlines(True)):
138 138 yield {"line": t,
139 139 "lineid": "l%d" % (lineno + 1),
140 140 "linenumber": "% 6d" % (lineno + 1),
141 141 "parity": next(parity)}
142 142
143 143 return tmpl("filerevision",
144 144 file=f,
145 145 path=webutil.up(f),
146 146 text=lines(),
147 147 symrev=webutil.symrevorshortnode(req, fctx),
148 148 rename=webutil.renamelink(fctx),
149 149 permissions=fctx.manifest().flags(f),
150 150 **webutil.commonentry(web.repo, fctx))
151 151
152 152 @webcommand('file')
153 153 def file(web, req, tmpl):
154 154 """
155 155 /file/{revision}[/{path}]
156 156 -------------------------
157 157
158 158 Show information about a directory or file in the repository.
159 159
160 160 Info about the ``path`` given as a URL parameter will be rendered.
161 161
162 162 If ``path`` is a directory, information about the entries in that
163 163 directory will be rendered. This form is equivalent to the ``manifest``
164 164 handler.
165 165
166 166 If ``path`` is a file, information about that file will be shown via
167 167 the ``filerevision`` template.
168 168
169 169 If ``path`` is not defined, information about the root directory will
170 170 be rendered.
171 171 """
172 172 path = webutil.cleanpath(web.repo, req.form.get('file', [''])[0])
173 173 if not path:
174 174 return manifest(web, req, tmpl)
175 175 try:
176 176 return _filerevision(web, req, tmpl, webutil.filectx(web.repo, req))
177 177 except error.LookupError as inst:
178 178 try:
179 179 return manifest(web, req, tmpl)
180 180 except ErrorResponse:
181 181 raise inst
182 182
183 183 def _search(web, req, tmpl):
184 184 MODE_REVISION = 'rev'
185 185 MODE_KEYWORD = 'keyword'
186 186 MODE_REVSET = 'revset'
187 187
188 188 def revsearch(ctx):
189 189 yield ctx
190 190
191 191 def keywordsearch(query):
192 192 lower = encoding.lower
193 193 qw = lower(query).split()
194 194
195 195 def revgen():
196 196 cl = web.repo.changelog
197 197 for i in xrange(len(web.repo) - 1, 0, -100):
198 198 l = []
199 199 for j in cl.revs(max(0, i - 99), i):
200 200 ctx = web.repo[j]
201 201 l.append(ctx)
202 202 l.reverse()
203 203 for e in l:
204 204 yield e
205 205
206 206 for ctx in revgen():
207 207 miss = 0
208 208 for q in qw:
209 209 if not (q in lower(ctx.user()) or
210 210 q in lower(ctx.description()) or
211 211 q in lower(" ".join(ctx.files()))):
212 212 miss = 1
213 213 break
214 214 if miss:
215 215 continue
216 216
217 217 yield ctx
218 218
219 219 def revsetsearch(revs):
220 220 for r in revs:
221 221 yield web.repo[r]
222 222
223 223 searchfuncs = {
224 224 MODE_REVISION: (revsearch, 'exact revision search'),
225 225 MODE_KEYWORD: (keywordsearch, 'literal keyword search'),
226 226 MODE_REVSET: (revsetsearch, 'revset expression search'),
227 227 }
228 228
229 229 def getsearchmode(query):
230 230 try:
231 231 ctx = web.repo[query]
232 232 except (error.RepoError, error.LookupError):
233 233 # query is not an exact revision pointer, need to
234 234 # decide if it's a revset expression or keywords
235 235 pass
236 236 else:
237 237 return MODE_REVISION, ctx
238 238
239 239 revdef = 'reverse(%s)' % query
240 240 try:
241 241 tree = revset.parse(revdef)
242 242 except error.ParseError:
243 243 # can't parse to a revset tree
244 244 return MODE_KEYWORD, query
245 245
246 246 if revset.depth(tree) <= 2:
247 247 # no revset syntax used
248 248 return MODE_KEYWORD, query
249 249
250 250 if any((token, (value or '')[:3]) == ('string', 're:')
251 251 for token, value, pos in revset.tokenize(revdef)):
252 252 return MODE_KEYWORD, query
253 253
254 254 funcsused = revset.funcsused(tree)
255 255 if not funcsused.issubset(revset.safesymbols):
256 256 return MODE_KEYWORD, query
257 257
258 258 mfunc = revset.match(web.repo.ui, revdef)
259 259 try:
260 260 revs = mfunc(web.repo)
261 261 return MODE_REVSET, revs
262 262 # ParseError: wrongly placed tokens, wrongs arguments, etc
263 263 # RepoLookupError: no such revision, e.g. in 'revision:'
264 264 # Abort: bookmark/tag not exists
265 265 # LookupError: ambiguous identifier, e.g. in '(bc)' on a large repo
266 266 except (error.ParseError, error.RepoLookupError, error.Abort,
267 267 LookupError):
268 268 return MODE_KEYWORD, query
269 269
270 270 def changelist(**map):
271 271 count = 0
272 272
273 273 for ctx in searchfunc[0](funcarg):
274 274 count += 1
275 275 n = ctx.node()
276 276 showtags = webutil.showtag(web.repo, tmpl, 'changelogtag', n)
277 277 files = webutil.listfilediffs(tmpl, ctx.files(), n, web.maxfiles)
278 278
279 279 yield tmpl('searchentry',
280 280 parity=next(parity),
281 281 changelogtag=showtags,
282 282 files=files,
283 283 **webutil.commonentry(web.repo, ctx))
284 284
285 285 if count >= revcount:
286 286 break
287 287
288 288 query = req.form['rev'][0]
289 289 revcount = web.maxchanges
290 290 if 'revcount' in req.form:
291 291 try:
292 292 revcount = int(req.form.get('revcount', [revcount])[0])
293 293 revcount = max(revcount, 1)
294 294 tmpl.defaults['sessionvars']['revcount'] = revcount
295 295 except ValueError:
296 296 pass
297 297
298 298 lessvars = copy.copy(tmpl.defaults['sessionvars'])
299 299 lessvars['revcount'] = max(revcount / 2, 1)
300 300 lessvars['rev'] = query
301 301 morevars = copy.copy(tmpl.defaults['sessionvars'])
302 302 morevars['revcount'] = revcount * 2
303 303 morevars['rev'] = query
304 304
305 305 mode, funcarg = getsearchmode(query)
306 306
307 307 if 'forcekw' in req.form:
308 308 showforcekw = ''
309 309 showunforcekw = searchfuncs[mode][1]
310 310 mode = MODE_KEYWORD
311 311 funcarg = query
312 312 else:
313 313 if mode != MODE_KEYWORD:
314 314 showforcekw = searchfuncs[MODE_KEYWORD][1]
315 315 else:
316 316 showforcekw = ''
317 317 showunforcekw = ''
318 318
319 319 searchfunc = searchfuncs[mode]
320 320
321 321 tip = web.repo['tip']
322 322 parity = paritygen(web.stripecount)
323 323
324 324 return tmpl('search', query=query, node=tip.hex(), symrev='tip',
325 325 entries=changelist, archives=web.archivelist("tip"),
326 326 morevars=morevars, lessvars=lessvars,
327 327 modedesc=searchfunc[1],
328 328 showforcekw=showforcekw, showunforcekw=showunforcekw)
329 329
330 330 @webcommand('changelog')
331 331 def changelog(web, req, tmpl, shortlog=False):
332 332 """
333 333 /changelog[/{revision}]
334 334 -----------------------
335 335
336 336 Show information about multiple changesets.
337 337
338 338 If the optional ``revision`` URL argument is absent, information about
339 339 all changesets starting at ``tip`` will be rendered. If the ``revision``
340 340 argument is present, changesets will be shown starting from the specified
341 341 revision.
342 342
343 343 If ``revision`` is absent, the ``rev`` query string argument may be
344 344 defined. This will perform a search for changesets.
345 345
346 346 The argument for ``rev`` can be a single revision, a revision set,
347 347 or a literal keyword to search for in changeset data (equivalent to
348 348 :hg:`log -k`).
349 349
350 350 The ``revcount`` query string argument defines the maximum numbers of
351 351 changesets to render.
352 352
353 353 For non-searches, the ``changelog`` template will be rendered.
354 354 """
355 355
356 356 query = ''
357 357 if 'node' in req.form:
358 358 ctx = webutil.changectx(web.repo, req)
359 359 symrev = webutil.symrevorshortnode(req, ctx)
360 360 elif 'rev' in req.form:
361 361 return _search(web, req, tmpl)
362 362 else:
363 363 ctx = web.repo['tip']
364 364 symrev = 'tip'
365 365
366 366 def changelist():
367 367 revs = []
368 368 if pos != -1:
369 369 revs = web.repo.changelog.revs(pos, 0)
370 370 curcount = 0
371 371 for rev in revs:
372 372 curcount += 1
373 373 if curcount > revcount + 1:
374 374 break
375 375
376 376 entry = webutil.changelistentry(web, web.repo[rev], tmpl)
377 377 entry['parity'] = next(parity)
378 378 yield entry
379 379
380 380 if shortlog:
381 381 revcount = web.maxshortchanges
382 382 else:
383 383 revcount = web.maxchanges
384 384
385 385 if 'revcount' in req.form:
386 386 try:
387 387 revcount = int(req.form.get('revcount', [revcount])[0])
388 388 revcount = max(revcount, 1)
389 389 tmpl.defaults['sessionvars']['revcount'] = revcount
390 390 except ValueError:
391 391 pass
392 392
393 393 lessvars = copy.copy(tmpl.defaults['sessionvars'])
394 394 lessvars['revcount'] = max(revcount / 2, 1)
395 395 morevars = copy.copy(tmpl.defaults['sessionvars'])
396 396 morevars['revcount'] = revcount * 2
397 397
398 398 count = len(web.repo)
399 399 pos = ctx.rev()
400 400 parity = paritygen(web.stripecount)
401 401
402 402 changenav = webutil.revnav(web.repo).gen(pos, revcount, count)
403 403
404 404 entries = list(changelist())
405 405 latestentry = entries[:1]
406 406 if len(entries) > revcount:
407 407 nextentry = entries[-1:]
408 408 entries = entries[:-1]
409 409 else:
410 410 nextentry = []
411 411
412 412 return tmpl(shortlog and 'shortlog' or 'changelog', changenav=changenav,
413 413 node=ctx.hex(), rev=pos, symrev=symrev, changesets=count,
414 414 entries=entries,
415 415 latestentry=latestentry, nextentry=nextentry,
416 416 archives=web.archivelist("tip"), revcount=revcount,
417 417 morevars=morevars, lessvars=lessvars, query=query)
418 418
419 419 @webcommand('shortlog')
420 420 def shortlog(web, req, tmpl):
421 421 """
422 422 /shortlog
423 423 ---------
424 424
425 425 Show basic information about a set of changesets.
426 426
427 427 This accepts the same parameters as the ``changelog`` handler. The only
428 428 difference is the ``shortlog`` template will be rendered instead of the
429 429 ``changelog`` template.
430 430 """
431 431 return changelog(web, req, tmpl, shortlog=True)
432 432
433 433 @webcommand('changeset')
434 434 def changeset(web, req, tmpl):
435 435 """
436 436 /changeset[/{revision}]
437 437 -----------------------
438 438
439 439 Show information about a single changeset.
440 440
441 441 A URL path argument is the changeset identifier to show. See ``hg help
442 442 revisions`` for possible values. If not defined, the ``tip`` changeset
443 443 will be shown.
444 444
445 445 The ``changeset`` template is rendered. Contents of the ``changesettag``,
446 446 ``changesetbookmark``, ``filenodelink``, ``filenolink``, and the many
447 447 templates related to diffs may all be used to produce the output.
448 448 """
449 449 ctx = webutil.changectx(web.repo, req)
450 450
451 451 return tmpl('changeset', **webutil.changesetentry(web, req, tmpl, ctx))
452 452
453 453 rev = webcommand('rev')(changeset)
454 454
455 455 def decodepath(path):
456 456 """Hook for mapping a path in the repository to a path in the
457 457 working copy.
458 458
459 459 Extensions (e.g., largefiles) can override this to remap files in
460 460 the virtual file system presented by the manifest command below."""
461 461 return path
462 462
463 463 @webcommand('manifest')
464 464 def manifest(web, req, tmpl):
465 465 """
466 466 /manifest[/{revision}[/{path}]]
467 467 -------------------------------
468 468
469 469 Show information about a directory.
470 470
471 471 If the URL path arguments are omitted, information about the root
472 472 directory for the ``tip`` changeset will be shown.
473 473
474 474 Because this handler can only show information for directories, it
475 475 is recommended to use the ``file`` handler instead, as it can handle both
476 476 directories and files.
477 477
478 478 The ``manifest`` template will be rendered for this handler.
479 479 """
480 480 if 'node' in req.form:
481 481 ctx = webutil.changectx(web.repo, req)
482 482 symrev = webutil.symrevorshortnode(req, ctx)
483 483 else:
484 484 ctx = web.repo['tip']
485 485 symrev = 'tip'
486 486 path = webutil.cleanpath(web.repo, req.form.get('file', [''])[0])
487 487 mf = ctx.manifest()
488 488 node = ctx.node()
489 489
490 490 files = {}
491 491 dirs = {}
492 492 parity = paritygen(web.stripecount)
493 493
494 494 if path and path[-1] != "/":
495 495 path += "/"
496 496 l = len(path)
497 497 abspath = "/" + path
498 498
499 499 for full, n in mf.iteritems():
500 500 # the virtual path (working copy path) used for the full
501 501 # (repository) path
502 502 f = decodepath(full)
503 503
504 504 if f[:l] != path:
505 505 continue
506 506 remain = f[l:]
507 507 elements = remain.split('/')
508 508 if len(elements) == 1:
509 509 files[remain] = full
510 510 else:
511 511 h = dirs # need to retain ref to dirs (root)
512 512 for elem in elements[0:-1]:
513 513 if elem not in h:
514 514 h[elem] = {}
515 515 h = h[elem]
516 516 if len(h) > 1:
517 517 break
518 518 h[None] = None # denotes files present
519 519
520 520 if mf and not files and not dirs:
521 521 raise ErrorResponse(HTTP_NOT_FOUND, 'path not found: ' + path)
522 522
523 523 def filelist(**map):
524 524 for f in sorted(files):
525 525 full = files[f]
526 526
527 527 fctx = ctx.filectx(full)
528 528 yield {"file": full,
529 529 "parity": next(parity),
530 530 "basename": f,
531 531 "date": fctx.date(),
532 532 "size": fctx.size(),
533 533 "permissions": mf.flags(full)}
534 534
535 535 def dirlist(**map):
536 536 for d in sorted(dirs):
537 537
538 538 emptydirs = []
539 539 h = dirs[d]
540 540 while isinstance(h, dict) and len(h) == 1:
541 541 k, v = h.items()[0]
542 542 if v:
543 543 emptydirs.append(k)
544 544 h = v
545 545
546 546 path = "%s%s" % (abspath, d)
547 547 yield {"parity": next(parity),
548 548 "path": path,
549 549 "emptydirs": "/".join(emptydirs),
550 550 "basename": d}
551 551
552 552 return tmpl("manifest",
553 553 symrev=symrev,
554 554 path=abspath,
555 555 up=webutil.up(abspath),
556 556 upparity=next(parity),
557 557 fentries=filelist,
558 558 dentries=dirlist,
559 559 archives=web.archivelist(hex(node)),
560 560 **webutil.commonentry(web.repo, ctx))
561 561
562 562 @webcommand('tags')
563 563 def tags(web, req, tmpl):
564 564 """
565 565 /tags
566 566 -----
567 567
568 568 Show information about tags.
569 569
570 570 No arguments are accepted.
571 571
572 572 The ``tags`` template is rendered.
573 573 """
574 574 i = list(reversed(web.repo.tagslist()))
575 575 parity = paritygen(web.stripecount)
576 576
577 577 def entries(notip, latestonly, **map):
578 578 t = i
579 579 if notip:
580 580 t = [(k, n) for k, n in i if k != "tip"]
581 581 if latestonly:
582 582 t = t[:1]
583 583 for k, n in t:
584 584 yield {"parity": next(parity),
585 585 "tag": k,
586 586 "date": web.repo[n].date(),
587 587 "node": hex(n)}
588 588
589 589 return tmpl("tags",
590 590 node=hex(web.repo.changelog.tip()),
591 591 entries=lambda **x: entries(False, False, **x),
592 592 entriesnotip=lambda **x: entries(True, False, **x),
593 593 latestentry=lambda **x: entries(True, True, **x))
594 594
595 595 @webcommand('bookmarks')
596 596 def bookmarks(web, req, tmpl):
597 597 """
598 598 /bookmarks
599 599 ----------
600 600
601 601 Show information about bookmarks.
602 602
603 603 No arguments are accepted.
604 604
605 605 The ``bookmarks`` template is rendered.
606 606 """
607 607 i = [b for b in web.repo._bookmarks.items() if b[1] in web.repo]
608 608 sortkey = lambda b: (web.repo[b[1]].rev(), b[0])
609 609 i = sorted(i, key=sortkey, reverse=True)
610 610 parity = paritygen(web.stripecount)
611 611
612 612 def entries(latestonly, **map):
613 613 t = i
614 614 if latestonly:
615 615 t = i[:1]
616 616 for k, n in t:
617 617 yield {"parity": next(parity),
618 618 "bookmark": k,
619 619 "date": web.repo[n].date(),
620 620 "node": hex(n)}
621 621
622 622 if i:
623 623 latestrev = i[0][1]
624 624 else:
625 625 latestrev = -1
626 626
627 627 return tmpl("bookmarks",
628 628 node=hex(web.repo.changelog.tip()),
629 629 lastchange=[{"date": web.repo[latestrev].date()}],
630 630 entries=lambda **x: entries(latestonly=False, **x),
631 631 latestentry=lambda **x: entries(latestonly=True, **x))
632 632
633 633 @webcommand('branches')
634 634 def branches(web, req, tmpl):
635 635 """
636 636 /branches
637 637 ---------
638 638
639 639 Show information about branches.
640 640
641 641 All known branches are contained in the output, even closed branches.
642 642
643 643 No arguments are accepted.
644 644
645 645 The ``branches`` template is rendered.
646 646 """
647 647 entries = webutil.branchentries(web.repo, web.stripecount)
648 648 latestentry = webutil.branchentries(web.repo, web.stripecount, 1)
649 649 return tmpl('branches', node=hex(web.repo.changelog.tip()),
650 650 entries=entries, latestentry=latestentry)
651 651
652 652 @webcommand('summary')
653 653 def summary(web, req, tmpl):
654 654 """
655 655 /summary
656 656 --------
657 657
658 658 Show a summary of repository state.
659 659
660 660 Information about the latest changesets, bookmarks, tags, and branches
661 661 is captured by this handler.
662 662
663 663 The ``summary`` template is rendered.
664 664 """
665 665 i = reversed(web.repo.tagslist())
666 666
667 667 def tagentries(**map):
668 668 parity = paritygen(web.stripecount)
669 669 count = 0
670 670 for k, n in i:
671 671 if k == "tip": # skip tip
672 672 continue
673 673
674 674 count += 1
675 675 if count > 10: # limit to 10 tags
676 676 break
677 677
678 678 yield tmpl("tagentry",
679 679 parity=next(parity),
680 680 tag=k,
681 681 node=hex(n),
682 682 date=web.repo[n].date())
683 683
684 684 def bookmarks(**map):
685 685 parity = paritygen(web.stripecount)
686 686 marks = [b for b in web.repo._bookmarks.items() if b[1] in web.repo]
687 687 sortkey = lambda b: (web.repo[b[1]].rev(), b[0])
688 688 marks = sorted(marks, key=sortkey, reverse=True)
689 689 for k, n in marks[:10]: # limit to 10 bookmarks
690 690 yield {'parity': next(parity),
691 691 'bookmark': k,
692 692 'date': web.repo[n].date(),
693 693 'node': hex(n)}
694 694
695 695 def changelist(**map):
696 696 parity = paritygen(web.stripecount, offset=start - end)
697 697 l = [] # build a list in forward order for efficiency
698 698 revs = []
699 699 if start < end:
700 700 revs = web.repo.changelog.revs(start, end - 1)
701 701 for i in revs:
702 702 ctx = web.repo[i]
703 703
704 704 l.append(tmpl(
705 705 'shortlogentry',
706 706 parity=next(parity),
707 707 **webutil.commonentry(web.repo, ctx)))
708 708
709 709 for entry in reversed(l):
710 710 yield entry
711 711
712 712 tip = web.repo['tip']
713 713 count = len(web.repo)
714 714 start = max(0, count - web.maxchanges)
715 715 end = min(count, start + web.maxchanges)
716 716
717 717 return tmpl("summary",
718 718 desc=web.config("web", "description", "unknown"),
719 719 owner=get_contact(web.config) or "unknown",
720 720 lastchange=tip.date(),
721 721 tags=tagentries,
722 722 bookmarks=bookmarks,
723 723 branches=webutil.branchentries(web.repo, web.stripecount, 10),
724 724 shortlog=changelist,
725 725 node=tip.hex(),
726 726 symrev='tip',
727 727 archives=web.archivelist("tip"),
728 728 labels=web.configlist('web', 'labels'))
729 729
730 730 @webcommand('filediff')
731 731 def filediff(web, req, tmpl):
732 732 """
733 733 /diff/{revision}/{path}
734 734 -----------------------
735 735
736 736 Show how a file changed in a particular commit.
737 737
738 738 The ``filediff`` template is rendered.
739 739
740 740 This handler is registered under both the ``/diff`` and ``/filediff``
741 741 paths. ``/diff`` is used in modern code.
742 742 """
743 743 fctx, ctx = None, None
744 744 try:
745 745 fctx = webutil.filectx(web.repo, req)
746 746 except LookupError:
747 747 ctx = webutil.changectx(web.repo, req)
748 748 path = webutil.cleanpath(web.repo, req.form['file'][0])
749 749 if path not in ctx.files():
750 750 raise
751 751
752 752 if fctx is not None:
753 753 path = fctx.path()
754 754 ctx = fctx.changectx()
755 755
756 756 parity = paritygen(web.stripecount)
757 757 style = web.config('web', 'style', 'paper')
758 758 if 'style' in req.form:
759 759 style = req.form['style'][0]
760 760
761 761 diffs = webutil.diffs(web.repo, tmpl, ctx, None, [path], parity, style)
762 762 if fctx is not None:
763 763 rename = webutil.renamelink(fctx)
764 764 ctx = fctx
765 765 else:
766 766 rename = []
767 767 ctx = ctx
768 768 return tmpl("filediff",
769 769 file=path,
770 770 symrev=webutil.symrevorshortnode(req, ctx),
771 771 rename=rename,
772 772 diff=diffs,
773 773 **webutil.commonentry(web.repo, ctx))
774 774
775 775 diff = webcommand('diff')(filediff)
776 776
777 777 @webcommand('comparison')
778 778 def comparison(web, req, tmpl):
779 779 """
780 780 /comparison/{revision}/{path}
781 781 -----------------------------
782 782
783 783 Show a comparison between the old and new versions of a file from changes
784 784 made on a particular revision.
785 785
786 786 This is similar to the ``diff`` handler. However, this form features
787 787 a split or side-by-side diff rather than a unified diff.
788 788
789 789 The ``context`` query string argument can be used to control the lines of
790 790 context in the diff.
791 791
792 792 The ``filecomparison`` template is rendered.
793 793 """
794 794 ctx = webutil.changectx(web.repo, req)
795 795 if 'file' not in req.form:
796 796 raise ErrorResponse(HTTP_NOT_FOUND, 'file not given')
797 797 path = webutil.cleanpath(web.repo, req.form['file'][0])
798 798
799 799 parsecontext = lambda v: v == 'full' and -1 or int(v)
800 800 if 'context' in req.form:
801 801 context = parsecontext(req.form['context'][0])
802 802 else:
803 803 context = parsecontext(web.config('web', 'comparisoncontext', '5'))
804 804
805 805 def filelines(f):
806 806 if util.binary(f.data()):
807 807 mt = mimetypes.guess_type(f.path())[0]
808 808 if not mt:
809 809 mt = 'application/octet-stream'
810 810 return [_('(binary file %s, hash: %s)') % (mt, hex(f.filenode()))]
811 811 return f.data().splitlines()
812 812
813 813 fctx = None
814 814 parent = ctx.p1()
815 815 leftrev = parent.rev()
816 816 leftnode = parent.node()
817 817 rightrev = ctx.rev()
818 818 rightnode = ctx.node()
819 819 if path in ctx:
820 820 fctx = ctx[path]
821 821 rightlines = filelines(fctx)
822 822 if path not in parent:
823 823 leftlines = ()
824 824 else:
825 825 pfctx = parent[path]
826 826 leftlines = filelines(pfctx)
827 827 else:
828 828 rightlines = ()
829 829 pfctx = ctx.parents()[0][path]
830 830 leftlines = filelines(pfctx)
831 831
832 832 comparison = webutil.compare(tmpl, context, leftlines, rightlines)
833 833 if fctx is not None:
834 834 rename = webutil.renamelink(fctx)
835 835 ctx = fctx
836 836 else:
837 837 rename = []
838 838 ctx = ctx
839 839 return tmpl('filecomparison',
840 840 file=path,
841 841 symrev=webutil.symrevorshortnode(req, ctx),
842 842 rename=rename,
843 843 leftrev=leftrev,
844 844 leftnode=hex(leftnode),
845 845 rightrev=rightrev,
846 846 rightnode=hex(rightnode),
847 847 comparison=comparison,
848 848 **webutil.commonentry(web.repo, ctx))
849 849
850 850 @webcommand('annotate')
851 851 def annotate(web, req, tmpl):
852 852 """
853 853 /annotate/{revision}/{path}
854 854 ---------------------------
855 855
856 856 Show changeset information for each line in a file.
857 857
858 858 The ``fileannotate`` template is rendered.
859 859 """
860 860 fctx = webutil.filectx(web.repo, req)
861 861 f = fctx.path()
862 862 parity = paritygen(web.stripecount)
863 863
864 # parents() is called once per line and several lines likely belong to
865 # same revision. So it is worth caching.
866 # TODO there are still redundant operations within basefilectx.parents()
867 # and from the fctx.annotate() call itself that could be cached.
868 parentscache = {}
864 869 def parents(f):
870 rev = f.rev()
871 if rev not in parentscache:
872 parentscache[rev] = []
865 873 for p in f.parents():
866 yield {
867 "node": p.hex(),
868 "rev": p.rev(),
874 entry = {
875 'node': p.hex(),
876 'rev': p.rev(),
869 877 }
878 parentscache[rev].append(entry)
879
880 for p in parentscache[rev]:
881 yield p
870 882
871 883 def annotate(**map):
872 884 if util.binary(fctx.data()):
873 885 mt = (mimetypes.guess_type(fctx.path())[0]
874 886 or 'application/octet-stream')
875 887 lines = [((fctx.filectx(fctx.filerev()), 1), '(binary:%s)' % mt)]
876 888 else:
877 889 lines = webutil.annotate(fctx, web.repo.ui)
878 890
879 891 previousrev = None
880 892 blockparitygen = paritygen(1)
881 893 for lineno, ((f, targetline), l) in enumerate(lines):
882 894 rev = f.rev()
883 895 if rev != previousrev:
884 896 blockhead = True
885 897 blockparity = next(blockparitygen)
886 898 else:
887 899 blockhead = None
888 900 previousrev = rev
889 901 yield {"parity": next(parity),
890 902 "node": f.hex(),
891 903 "rev": rev,
892 904 "author": f.user(),
893 905 "parents": parents(f),
894 906 "desc": f.description(),
895 907 "extra": f.extra(),
896 908 "file": f.path(),
897 909 "blockhead": blockhead,
898 910 "blockparity": blockparity,
899 911 "targetline": targetline,
900 912 "line": l,
901 913 "lineno": lineno + 1,
902 914 "lineid": "l%d" % (lineno + 1),
903 915 "linenumber": "% 6d" % (lineno + 1),
904 916 "revdate": f.date()}
905 917
906 918 return tmpl("fileannotate",
907 919 file=f,
908 920 annotate=annotate,
909 921 path=webutil.up(f),
910 922 symrev=webutil.symrevorshortnode(req, fctx),
911 923 rename=webutil.renamelink(fctx),
912 924 permissions=fctx.manifest().flags(f),
913 925 **webutil.commonentry(web.repo, fctx))
914 926
915 927 @webcommand('filelog')
916 928 def filelog(web, req, tmpl):
917 929 """
918 930 /filelog/{revision}/{path}
919 931 --------------------------
920 932
921 933 Show information about the history of a file in the repository.
922 934
923 935 The ``revcount`` query string argument can be defined to control the
924 936 maximum number of entries to show.
925 937
926 938 The ``filelog`` template will be rendered.
927 939 """
928 940
929 941 try:
930 942 fctx = webutil.filectx(web.repo, req)
931 943 f = fctx.path()
932 944 fl = fctx.filelog()
933 945 except error.LookupError:
934 946 f = webutil.cleanpath(web.repo, req.form['file'][0])
935 947 fl = web.repo.file(f)
936 948 numrevs = len(fl)
937 949 if not numrevs: # file doesn't exist at all
938 950 raise
939 951 rev = webutil.changectx(web.repo, req).rev()
940 952 first = fl.linkrev(0)
941 953 if rev < first: # current rev is from before file existed
942 954 raise
943 955 frev = numrevs - 1
944 956 while fl.linkrev(frev) > rev:
945 957 frev -= 1
946 958 fctx = web.repo.filectx(f, fl.linkrev(frev))
947 959
948 960 revcount = web.maxshortchanges
949 961 if 'revcount' in req.form:
950 962 try:
951 963 revcount = int(req.form.get('revcount', [revcount])[0])
952 964 revcount = max(revcount, 1)
953 965 tmpl.defaults['sessionvars']['revcount'] = revcount
954 966 except ValueError:
955 967 pass
956 968
957 969 lessvars = copy.copy(tmpl.defaults['sessionvars'])
958 970 lessvars['revcount'] = max(revcount / 2, 1)
959 971 morevars = copy.copy(tmpl.defaults['sessionvars'])
960 972 morevars['revcount'] = revcount * 2
961 973
962 974 count = fctx.filerev() + 1
963 975 start = max(0, fctx.filerev() - revcount + 1) # first rev on this page
964 976 end = min(count, start + revcount) # last rev on this page
965 977 parity = paritygen(web.stripecount, offset=start - end)
966 978
967 979 def entries():
968 980 l = []
969 981
970 982 repo = web.repo
971 983 revs = fctx.filelog().revs(start, end - 1)
972 984 for i in revs:
973 985 iterfctx = fctx.filectx(i)
974 986
975 987 l.append(dict(
976 988 parity=next(parity),
977 989 filerev=i,
978 990 file=f,
979 991 rename=webutil.renamelink(iterfctx),
980 992 **webutil.commonentry(repo, iterfctx)))
981 993 for e in reversed(l):
982 994 yield e
983 995
984 996 entries = list(entries())
985 997 latestentry = entries[:1]
986 998
987 999 revnav = webutil.filerevnav(web.repo, fctx.path())
988 1000 nav = revnav.gen(end - 1, revcount, count)
989 1001 return tmpl("filelog",
990 1002 file=f,
991 1003 nav=nav,
992 1004 symrev=webutil.symrevorshortnode(req, fctx),
993 1005 entries=entries,
994 1006 latestentry=latestentry,
995 1007 revcount=revcount,
996 1008 morevars=morevars,
997 1009 lessvars=lessvars,
998 1010 **webutil.commonentry(web.repo, fctx))
999 1011
1000 1012 @webcommand('archive')
1001 1013 def archive(web, req, tmpl):
1002 1014 """
1003 1015 /archive/{revision}.{format}[/{path}]
1004 1016 -------------------------------------
1005 1017
1006 1018 Obtain an archive of repository content.
1007 1019
1008 1020 The content and type of the archive is defined by a URL path parameter.
1009 1021 ``format`` is the file extension of the archive type to be generated. e.g.
1010 1022 ``zip`` or ``tar.bz2``. Not all archive types may be allowed by your
1011 1023 server configuration.
1012 1024
1013 1025 The optional ``path`` URL parameter controls content to include in the
1014 1026 archive. If omitted, every file in the specified revision is present in the
1015 1027 archive. If included, only the specified file or contents of the specified
1016 1028 directory will be included in the archive.
1017 1029
1018 1030 No template is used for this handler. Raw, binary content is generated.
1019 1031 """
1020 1032
1021 1033 type_ = req.form.get('type', [None])[0]
1022 1034 allowed = web.configlist("web", "allow_archive")
1023 1035 key = req.form['node'][0]
1024 1036
1025 1037 if type_ not in web.archives:
1026 1038 msg = 'Unsupported archive type: %s' % type_
1027 1039 raise ErrorResponse(HTTP_NOT_FOUND, msg)
1028 1040
1029 1041 if not ((type_ in allowed or
1030 1042 web.configbool("web", "allow" + type_, False))):
1031 1043 msg = 'Archive type not allowed: %s' % type_
1032 1044 raise ErrorResponse(HTTP_FORBIDDEN, msg)
1033 1045
1034 1046 reponame = re.sub(r"\W+", "-", os.path.basename(web.reponame))
1035 1047 cnode = web.repo.lookup(key)
1036 1048 arch_version = key
1037 1049 if cnode == key or key == 'tip':
1038 1050 arch_version = short(cnode)
1039 1051 name = "%s-%s" % (reponame, arch_version)
1040 1052
1041 1053 ctx = webutil.changectx(web.repo, req)
1042 1054 pats = []
1043 1055 matchfn = scmutil.match(ctx, [])
1044 1056 file = req.form.get('file', None)
1045 1057 if file:
1046 1058 pats = ['path:' + file[0]]
1047 1059 matchfn = scmutil.match(ctx, pats, default='path')
1048 1060 if pats:
1049 1061 files = [f for f in ctx.manifest().keys() if matchfn(f)]
1050 1062 if not files:
1051 1063 raise ErrorResponse(HTTP_NOT_FOUND,
1052 1064 'file(s) not found: %s' % file[0])
1053 1065
1054 1066 mimetype, artype, extension, encoding = web.archivespecs[type_]
1055 1067 headers = [
1056 1068 ('Content-Disposition', 'attachment; filename=%s%s' % (name, extension))
1057 1069 ]
1058 1070 if encoding:
1059 1071 headers.append(('Content-Encoding', encoding))
1060 1072 req.headers.extend(headers)
1061 1073 req.respond(HTTP_OK, mimetype)
1062 1074
1063 1075 archival.archive(web.repo, req, cnode, artype, prefix=name,
1064 1076 matchfn=matchfn,
1065 1077 subrepos=web.configbool("web", "archivesubrepos"))
1066 1078 return []
1067 1079
1068 1080
1069 1081 @webcommand('static')
1070 1082 def static(web, req, tmpl):
1071 1083 fname = req.form['file'][0]
1072 1084 # a repo owner may set web.static in .hg/hgrc to get any file
1073 1085 # readable by the user running the CGI script
1074 1086 static = web.config("web", "static", None, untrusted=False)
1075 1087 if not static:
1076 1088 tp = web.templatepath or templater.templatepaths()
1077 1089 if isinstance(tp, str):
1078 1090 tp = [tp]
1079 1091 static = [os.path.join(p, 'static') for p in tp]
1080 1092 staticfile(static, fname, req)
1081 1093 return []
1082 1094
1083 1095 @webcommand('graph')
1084 1096 def graph(web, req, tmpl):
1085 1097 """
1086 1098 /graph[/{revision}]
1087 1099 -------------------
1088 1100
1089 1101 Show information about the graphical topology of the repository.
1090 1102
1091 1103 Information rendered by this handler can be used to create visual
1092 1104 representations of repository topology.
1093 1105
1094 1106 The ``revision`` URL parameter controls the starting changeset.
1095 1107
1096 1108 The ``revcount`` query string argument can define the number of changesets
1097 1109 to show information for.
1098 1110
1099 1111 This handler will render the ``graph`` template.
1100 1112 """
1101 1113
1102 1114 if 'node' in req.form:
1103 1115 ctx = webutil.changectx(web.repo, req)
1104 1116 symrev = webutil.symrevorshortnode(req, ctx)
1105 1117 else:
1106 1118 ctx = web.repo['tip']
1107 1119 symrev = 'tip'
1108 1120 rev = ctx.rev()
1109 1121
1110 1122 bg_height = 39
1111 1123 revcount = web.maxshortchanges
1112 1124 if 'revcount' in req.form:
1113 1125 try:
1114 1126 revcount = int(req.form.get('revcount', [revcount])[0])
1115 1127 revcount = max(revcount, 1)
1116 1128 tmpl.defaults['sessionvars']['revcount'] = revcount
1117 1129 except ValueError:
1118 1130 pass
1119 1131
1120 1132 lessvars = copy.copy(tmpl.defaults['sessionvars'])
1121 1133 lessvars['revcount'] = max(revcount / 2, 1)
1122 1134 morevars = copy.copy(tmpl.defaults['sessionvars'])
1123 1135 morevars['revcount'] = revcount * 2
1124 1136
1125 1137 count = len(web.repo)
1126 1138 pos = rev
1127 1139
1128 1140 uprev = min(max(0, count - 1), rev + revcount)
1129 1141 downrev = max(0, rev - revcount)
1130 1142 changenav = webutil.revnav(web.repo).gen(pos, revcount, count)
1131 1143
1132 1144 tree = []
1133 1145 if pos != -1:
1134 1146 allrevs = web.repo.changelog.revs(pos, 0)
1135 1147 revs = []
1136 1148 for i in allrevs:
1137 1149 revs.append(i)
1138 1150 if len(revs) >= revcount:
1139 1151 break
1140 1152
1141 1153 # We have to feed a baseset to dagwalker as it is expecting smartset
1142 1154 # object. This does not have a big impact on hgweb performance itself
1143 1155 # since hgweb graphing code is not itself lazy yet.
1144 1156 dag = graphmod.dagwalker(web.repo, revset.baseset(revs))
1145 1157 # As we said one line above... not lazy.
1146 1158 tree = list(graphmod.colored(dag, web.repo))
1147 1159
1148 1160 def getcolumns(tree):
1149 1161 cols = 0
1150 1162 for (id, type, ctx, vtx, edges) in tree:
1151 1163 if type != graphmod.CHANGESET:
1152 1164 continue
1153 1165 cols = max(cols, max([edge[0] for edge in edges] or [0]),
1154 1166 max([edge[1] for edge in edges] or [0]))
1155 1167 return cols
1156 1168
1157 1169 def graphdata(usetuples, encodestr):
1158 1170 data = []
1159 1171
1160 1172 row = 0
1161 1173 for (id, type, ctx, vtx, edges) in tree:
1162 1174 if type != graphmod.CHANGESET:
1163 1175 continue
1164 1176 node = str(ctx)
1165 1177 age = encodestr(templatefilters.age(ctx.date()))
1166 1178 desc = templatefilters.firstline(encodestr(ctx.description()))
1167 1179 desc = cgi.escape(templatefilters.nonempty(desc))
1168 1180 user = cgi.escape(templatefilters.person(encodestr(ctx.user())))
1169 1181 branch = cgi.escape(encodestr(ctx.branch()))
1170 1182 try:
1171 1183 branchnode = web.repo.branchtip(branch)
1172 1184 except error.RepoLookupError:
1173 1185 branchnode = None
1174 1186 branch = branch, branchnode == ctx.node()
1175 1187
1176 1188 if usetuples:
1177 1189 data.append((node, vtx, edges, desc, user, age, branch,
1178 1190 [cgi.escape(encodestr(x)) for x in ctx.tags()],
1179 1191 [cgi.escape(encodestr(x))
1180 1192 for x in ctx.bookmarks()]))
1181 1193 else:
1182 1194 edgedata = [{'col': edge[0], 'nextcol': edge[1],
1183 1195 'color': (edge[2] - 1) % 6 + 1,
1184 1196 'width': edge[3], 'bcolor': edge[4]}
1185 1197 for edge in edges]
1186 1198
1187 1199 data.append(
1188 1200 {'node': node,
1189 1201 'col': vtx[0],
1190 1202 'color': (vtx[1] - 1) % 6 + 1,
1191 1203 'edges': edgedata,
1192 1204 'row': row,
1193 1205 'nextrow': row + 1,
1194 1206 'desc': desc,
1195 1207 'user': user,
1196 1208 'age': age,
1197 1209 'bookmarks': webutil.nodebookmarksdict(
1198 1210 web.repo, ctx.node()),
1199 1211 'branches': webutil.nodebranchdict(web.repo, ctx),
1200 1212 'inbranch': webutil.nodeinbranch(web.repo, ctx),
1201 1213 'tags': webutil.nodetagsdict(web.repo, ctx.node())})
1202 1214
1203 1215 row += 1
1204 1216
1205 1217 return data
1206 1218
1207 1219 cols = getcolumns(tree)
1208 1220 rows = len(tree)
1209 1221 canvasheight = (rows + 1) * bg_height - 27
1210 1222
1211 1223 return tmpl('graph', rev=rev, symrev=symrev, revcount=revcount,
1212 1224 uprev=uprev,
1213 1225 lessvars=lessvars, morevars=morevars, downrev=downrev,
1214 1226 cols=cols, rows=rows,
1215 1227 canvaswidth=(cols + 1) * bg_height,
1216 1228 truecanvasheight=rows * bg_height,
1217 1229 canvasheight=canvasheight, bg_height=bg_height,
1218 1230 # {jsdata} will be passed to |json, so it must be in utf-8
1219 1231 jsdata=lambda **x: graphdata(True, encoding.fromlocal),
1220 1232 nodes=lambda **x: graphdata(False, str),
1221 1233 node=ctx.hex(), changenav=changenav)
1222 1234
1223 1235 def _getdoc(e):
1224 1236 doc = e[0].__doc__
1225 1237 if doc:
1226 1238 doc = _(doc).partition('\n')[0]
1227 1239 else:
1228 1240 doc = _('(no help text available)')
1229 1241 return doc
1230 1242
1231 1243 @webcommand('help')
1232 1244 def help(web, req, tmpl):
1233 1245 """
1234 1246 /help[/{topic}]
1235 1247 ---------------
1236 1248
1237 1249 Render help documentation.
1238 1250
1239 1251 This web command is roughly equivalent to :hg:`help`. If a ``topic``
1240 1252 is defined, that help topic will be rendered. If not, an index of
1241 1253 available help topics will be rendered.
1242 1254
1243 1255 The ``help`` template will be rendered when requesting help for a topic.
1244 1256 ``helptopics`` will be rendered for the index of help topics.
1245 1257 """
1246 1258 from .. import commands, help as helpmod # avoid cycle
1247 1259
1248 1260 topicname = req.form.get('node', [None])[0]
1249 1261 if not topicname:
1250 1262 def topics(**map):
1251 1263 for entries, summary, _doc in helpmod.helptable:
1252 1264 yield {'topic': entries[0], 'summary': summary}
1253 1265
1254 1266 early, other = [], []
1255 1267 primary = lambda s: s.partition('|')[0]
1256 1268 for c, e in commands.table.iteritems():
1257 1269 doc = _getdoc(e)
1258 1270 if 'DEPRECATED' in doc or c.startswith('debug'):
1259 1271 continue
1260 1272 cmd = primary(c)
1261 1273 if cmd.startswith('^'):
1262 1274 early.append((cmd[1:], doc))
1263 1275 else:
1264 1276 other.append((cmd, doc))
1265 1277
1266 1278 early.sort()
1267 1279 other.sort()
1268 1280
1269 1281 def earlycommands(**map):
1270 1282 for c, doc in early:
1271 1283 yield {'topic': c, 'summary': doc}
1272 1284
1273 1285 def othercommands(**map):
1274 1286 for c, doc in other:
1275 1287 yield {'topic': c, 'summary': doc}
1276 1288
1277 1289 return tmpl('helptopics', topics=topics, earlycommands=earlycommands,
1278 1290 othercommands=othercommands, title='Index')
1279 1291
1280 1292 # Render an index of sub-topics.
1281 1293 if topicname in helpmod.subtopics:
1282 1294 topics = []
1283 1295 for entries, summary, _doc in helpmod.subtopics[topicname]:
1284 1296 topics.append({
1285 1297 'topic': '%s.%s' % (topicname, entries[0]),
1286 1298 'basename': entries[0],
1287 1299 'summary': summary,
1288 1300 })
1289 1301
1290 1302 return tmpl('helptopics', topics=topics, title=topicname,
1291 1303 subindex=True)
1292 1304
1293 1305 u = webutil.wsgiui()
1294 1306 u.verbose = True
1295 1307
1296 1308 # Render a page from a sub-topic.
1297 1309 if '.' in topicname:
1298 1310 # TODO implement support for rendering sections, like
1299 1311 # `hg help` works.
1300 1312 topic, subtopic = topicname.split('.', 1)
1301 1313 if topic not in helpmod.subtopics:
1302 1314 raise ErrorResponse(HTTP_NOT_FOUND)
1303 1315 else:
1304 1316 topic = topicname
1305 1317 subtopic = None
1306 1318
1307 1319 try:
1308 1320 doc = helpmod.help_(u, topic, subtopic=subtopic)
1309 1321 except error.UnknownCommand:
1310 1322 raise ErrorResponse(HTTP_NOT_FOUND)
1311 1323 return tmpl('help', topic=topicname, doc=doc)
1312 1324
1313 1325 # tell hggettext to extract docstrings from these functions:
1314 1326 i18nfunctions = commands.values()
@@ -1,1472 +1,1470 b''
1 1 # scmutil.py - Mercurial core utility functions
2 2 #
3 3 # Copyright 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 contextlib
11 11 import errno
12 12 import glob
13 13 import hashlib
14 14 import os
15 15 import re
16 16 import shutil
17 17 import stat
18 18 import tempfile
19 19 import threading
20 20
21 21 from .i18n import _
22 22 from .node import wdirrev
23 23 from . import (
24 24 encoding,
25 25 error,
26 26 match as matchmod,
27 27 osutil,
28 28 pathutil,
29 29 phases,
30 30 pycompat,
31 31 revset,
32 32 similar,
33 33 util,
34 34 )
35 35
36 36 if os.name == 'nt':
37 37 from . import scmwindows as scmplatform
38 38 else:
39 39 from . import scmposix as scmplatform
40 40
41 41 systemrcpath = scmplatform.systemrcpath
42 42 userrcpath = scmplatform.userrcpath
43 43 termsize = scmplatform.termsize
44 44
45 45 class status(tuple):
46 46 '''Named tuple with a list of files per status. The 'deleted', 'unknown'
47 47 and 'ignored' properties are only relevant to the working copy.
48 48 '''
49 49
50 50 __slots__ = ()
51 51
52 52 def __new__(cls, modified, added, removed, deleted, unknown, ignored,
53 53 clean):
54 54 return tuple.__new__(cls, (modified, added, removed, deleted, unknown,
55 55 ignored, clean))
56 56
57 57 @property
58 58 def modified(self):
59 59 '''files that have been modified'''
60 60 return self[0]
61 61
62 62 @property
63 63 def added(self):
64 64 '''files that have been added'''
65 65 return self[1]
66 66
67 67 @property
68 68 def removed(self):
69 69 '''files that have been removed'''
70 70 return self[2]
71 71
72 72 @property
73 73 def deleted(self):
74 74 '''files that are in the dirstate, but have been deleted from the
75 75 working copy (aka "missing")
76 76 '''
77 77 return self[3]
78 78
79 79 @property
80 80 def unknown(self):
81 81 '''files not in the dirstate that are not ignored'''
82 82 return self[4]
83 83
84 84 @property
85 85 def ignored(self):
86 86 '''files not in the dirstate that are ignored (by _dirignore())'''
87 87 return self[5]
88 88
89 89 @property
90 90 def clean(self):
91 91 '''files that have not been modified'''
92 92 return self[6]
93 93
94 94 def __repr__(self, *args, **kwargs):
95 95 return (('<status modified=%r, added=%r, removed=%r, deleted=%r, '
96 96 'unknown=%r, ignored=%r, clean=%r>') % self)
97 97
98 98 def itersubrepos(ctx1, ctx2):
99 99 """find subrepos in ctx1 or ctx2"""
100 100 # Create a (subpath, ctx) mapping where we prefer subpaths from
101 101 # ctx1. The subpaths from ctx2 are important when the .hgsub file
102 102 # has been modified (in ctx2) but not yet committed (in ctx1).
103 103 subpaths = dict.fromkeys(ctx2.substate, ctx2)
104 104 subpaths.update(dict.fromkeys(ctx1.substate, ctx1))
105 105
106 106 missing = set()
107 107
108 108 for subpath in ctx2.substate:
109 109 if subpath not in ctx1.substate:
110 110 del subpaths[subpath]
111 111 missing.add(subpath)
112 112
113 113 for subpath, ctx in sorted(subpaths.iteritems()):
114 114 yield subpath, ctx.sub(subpath)
115 115
116 116 # Yield an empty subrepo based on ctx1 for anything only in ctx2. That way,
117 117 # status and diff will have an accurate result when it does
118 118 # 'sub.{status|diff}(rev2)'. Otherwise, the ctx2 subrepo is compared
119 119 # against itself.
120 120 for subpath in missing:
121 121 yield subpath, ctx2.nullsub(subpath, ctx1)
122 122
123 123 def nochangesfound(ui, repo, excluded=None):
124 124 '''Report no changes for push/pull, excluded is None or a list of
125 125 nodes excluded from the push/pull.
126 126 '''
127 127 secretlist = []
128 128 if excluded:
129 129 for n in excluded:
130 130 if n not in repo:
131 131 # discovery should not have included the filtered revision,
132 132 # we have to explicitly exclude it until discovery is cleanup.
133 133 continue
134 134 ctx = repo[n]
135 135 if ctx.phase() >= phases.secret and not ctx.extinct():
136 136 secretlist.append(n)
137 137
138 138 if secretlist:
139 139 ui.status(_("no changes found (ignored %d secret changesets)\n")
140 140 % len(secretlist))
141 141 else:
142 142 ui.status(_("no changes found\n"))
143 143
144 144 def checknewlabel(repo, lbl, kind):
145 145 # Do not use the "kind" parameter in ui output.
146 146 # It makes strings difficult to translate.
147 147 if lbl in ['tip', '.', 'null']:
148 148 raise error.Abort(_("the name '%s' is reserved") % lbl)
149 149 for c in (':', '\0', '\n', '\r'):
150 150 if c in lbl:
151 151 raise error.Abort(_("%r cannot be used in a name") % c)
152 152 try:
153 153 int(lbl)
154 154 raise error.Abort(_("cannot use an integer as a name"))
155 155 except ValueError:
156 156 pass
157 157
158 158 def checkfilename(f):
159 159 '''Check that the filename f is an acceptable filename for a tracked file'''
160 160 if '\r' in f or '\n' in f:
161 161 raise error.Abort(_("'\\n' and '\\r' disallowed in filenames: %r") % f)
162 162
163 163 def checkportable(ui, f):
164 164 '''Check if filename f is portable and warn or abort depending on config'''
165 165 checkfilename(f)
166 166 abort, warn = checkportabilityalert(ui)
167 167 if abort or warn:
168 168 msg = util.checkwinfilename(f)
169 169 if msg:
170 170 msg = "%s: %r" % (msg, f)
171 171 if abort:
172 172 raise error.Abort(msg)
173 173 ui.warn(_("warning: %s\n") % msg)
174 174
175 175 def checkportabilityalert(ui):
176 176 '''check if the user's config requests nothing, a warning, or abort for
177 177 non-portable filenames'''
178 178 val = ui.config('ui', 'portablefilenames', 'warn')
179 179 lval = val.lower()
180 180 bval = util.parsebool(val)
181 181 abort = os.name == 'nt' or lval == 'abort'
182 182 warn = bval or lval == 'warn'
183 183 if bval is None and not (warn or abort or lval == 'ignore'):
184 184 raise error.ConfigError(
185 185 _("ui.portablefilenames value is invalid ('%s')") % val)
186 186 return abort, warn
187 187
188 188 class casecollisionauditor(object):
189 189 def __init__(self, ui, abort, dirstate):
190 190 self._ui = ui
191 191 self._abort = abort
192 192 allfiles = '\0'.join(dirstate._map)
193 193 self._loweredfiles = set(encoding.lower(allfiles).split('\0'))
194 194 self._dirstate = dirstate
195 195 # The purpose of _newfiles is so that we don't complain about
196 196 # case collisions if someone were to call this object with the
197 197 # same filename twice.
198 198 self._newfiles = set()
199 199
200 200 def __call__(self, f):
201 201 if f in self._newfiles:
202 202 return
203 203 fl = encoding.lower(f)
204 204 if fl in self._loweredfiles and f not in self._dirstate:
205 205 msg = _('possible case-folding collision for %s') % f
206 206 if self._abort:
207 207 raise error.Abort(msg)
208 208 self._ui.warn(_("warning: %s\n") % msg)
209 209 self._loweredfiles.add(fl)
210 210 self._newfiles.add(f)
211 211
212 212 def filteredhash(repo, maxrev):
213 213 """build hash of filtered revisions in the current repoview.
214 214
215 215 Multiple caches perform up-to-date validation by checking that the
216 216 tiprev and tipnode stored in the cache file match the current repository.
217 217 However, this is not sufficient for validating repoviews because the set
218 218 of revisions in the view may change without the repository tiprev and
219 219 tipnode changing.
220 220
221 221 This function hashes all the revs filtered from the view and returns
222 222 that SHA-1 digest.
223 223 """
224 224 cl = repo.changelog
225 225 if not cl.filteredrevs:
226 226 return None
227 227 key = None
228 228 revs = sorted(r for r in cl.filteredrevs if r <= maxrev)
229 229 if revs:
230 230 s = hashlib.sha1()
231 231 for rev in revs:
232 232 s.update('%s;' % rev)
233 233 key = s.digest()
234 234 return key
235 235
236 236 class abstractvfs(object):
237 237 """Abstract base class; cannot be instantiated"""
238 238
239 239 def __init__(self, *args, **kwargs):
240 240 '''Prevent instantiation; don't call this from subclasses.'''
241 241 raise NotImplementedError('attempted instantiating ' + str(type(self)))
242 242
243 243 def tryread(self, path):
244 244 '''gracefully return an empty string for missing files'''
245 245 try:
246 246 return self.read(path)
247 247 except IOError as inst:
248 248 if inst.errno != errno.ENOENT:
249 249 raise
250 250 return ""
251 251
252 252 def tryreadlines(self, path, mode='rb'):
253 253 '''gracefully return an empty array for missing files'''
254 254 try:
255 255 return self.readlines(path, mode=mode)
256 256 except IOError as inst:
257 257 if inst.errno != errno.ENOENT:
258 258 raise
259 259 return []
260 260
261 261 @util.propertycache
262 262 def open(self):
263 263 '''Open ``path`` file, which is relative to vfs root.
264 264
265 265 Newly created directories are marked as "not to be indexed by
266 266 the content indexing service", if ``notindexed`` is specified
267 267 for "write" mode access.
268 268 '''
269 269 return self.__call__
270 270
271 271 def read(self, path):
272 272 with self(path, 'rb') as fp:
273 273 return fp.read()
274 274
275 275 def readlines(self, path, mode='rb'):
276 276 with self(path, mode=mode) as fp:
277 277 return fp.readlines()
278 278
279 279 def write(self, path, data, backgroundclose=False):
280 280 with self(path, 'wb', backgroundclose=backgroundclose) as fp:
281 281 return fp.write(data)
282 282
283 283 def writelines(self, path, data, mode='wb', notindexed=False):
284 284 with self(path, mode=mode, notindexed=notindexed) as fp:
285 285 return fp.writelines(data)
286 286
287 287 def append(self, path, data):
288 288 with self(path, 'ab') as fp:
289 289 return fp.write(data)
290 290
291 291 def basename(self, path):
292 292 """return base element of a path (as os.path.basename would do)
293 293
294 294 This exists to allow handling of strange encoding if needed."""
295 295 return os.path.basename(path)
296 296
297 297 def chmod(self, path, mode):
298 298 return os.chmod(self.join(path), mode)
299 299
300 300 def dirname(self, path):
301 301 """return dirname element of a path (as os.path.dirname would do)
302 302
303 303 This exists to allow handling of strange encoding if needed."""
304 304 return os.path.dirname(path)
305 305
306 306 def exists(self, path=None):
307 307 return os.path.exists(self.join(path))
308 308
309 309 def fstat(self, fp):
310 310 return util.fstat(fp)
311 311
312 312 def isdir(self, path=None):
313 313 return os.path.isdir(self.join(path))
314 314
315 315 def isfile(self, path=None):
316 316 return os.path.isfile(self.join(path))
317 317
318 318 def islink(self, path=None):
319 319 return os.path.islink(self.join(path))
320 320
321 321 def isfileorlink(self, path=None):
322 322 '''return whether path is a regular file or a symlink
323 323
324 324 Unlike isfile, this doesn't follow symlinks.'''
325 325 try:
326 326 st = self.lstat(path)
327 327 except OSError:
328 328 return False
329 329 mode = st.st_mode
330 330 return stat.S_ISREG(mode) or stat.S_ISLNK(mode)
331 331
332 332 def reljoin(self, *paths):
333 333 """join various elements of a path together (as os.path.join would do)
334 334
335 335 The vfs base is not injected so that path stay relative. This exists
336 336 to allow handling of strange encoding if needed."""
337 337 return os.path.join(*paths)
338 338
339 339 def split(self, path):
340 340 """split top-most element of a path (as os.path.split would do)
341 341
342 342 This exists to allow handling of strange encoding if needed."""
343 343 return os.path.split(path)
344 344
345 345 def lexists(self, path=None):
346 346 return os.path.lexists(self.join(path))
347 347
348 348 def lstat(self, path=None):
349 349 return os.lstat(self.join(path))
350 350
351 351 def listdir(self, path=None):
352 352 return os.listdir(self.join(path))
353 353
354 354 def makedir(self, path=None, notindexed=True):
355 355 return util.makedir(self.join(path), notindexed)
356 356
357 357 def makedirs(self, path=None, mode=None):
358 358 return util.makedirs(self.join(path), mode)
359 359
360 360 def makelock(self, info, path):
361 361 return util.makelock(info, self.join(path))
362 362
363 363 def mkdir(self, path=None):
364 364 return os.mkdir(self.join(path))
365 365
366 366 def mkstemp(self, suffix='', prefix='tmp', dir=None, text=False):
367 367 fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
368 368 dir=self.join(dir), text=text)
369 369 dname, fname = util.split(name)
370 370 if dir:
371 371 return fd, os.path.join(dir, fname)
372 372 else:
373 373 return fd, fname
374 374
375 375 def readdir(self, path=None, stat=None, skip=None):
376 376 return osutil.listdir(self.join(path), stat, skip)
377 377
378 378 def readlock(self, path):
379 379 return util.readlock(self.join(path))
380 380
381 381 def rename(self, src, dst, checkambig=False):
382 382 """Rename from src to dst
383 383
384 384 checkambig argument is used with util.filestat, and is useful
385 385 only if destination file is guarded by any lock
386 386 (e.g. repo.lock or repo.wlock).
387 387 """
388 388 dstpath = self.join(dst)
389 389 oldstat = checkambig and util.filestat(dstpath)
390 390 if oldstat and oldstat.stat:
391 391 ret = util.rename(self.join(src), dstpath)
392 392 newstat = util.filestat(dstpath)
393 393 if newstat.isambig(oldstat):
394 394 # stat of renamed file is ambiguous to original one
395 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
396 os.utime(dstpath, (advanced, advanced))
395 newstat.avoidambig(dstpath, oldstat)
397 396 return ret
398 397 return util.rename(self.join(src), dstpath)
399 398
400 399 def readlink(self, path):
401 400 return os.readlink(self.join(path))
402 401
403 402 def removedirs(self, path=None):
404 403 """Remove a leaf directory and all empty intermediate ones
405 404 """
406 405 return util.removedirs(self.join(path))
407 406
408 407 def rmtree(self, path=None, ignore_errors=False, forcibly=False):
409 408 """Remove a directory tree recursively
410 409
411 410 If ``forcibly``, this tries to remove READ-ONLY files, too.
412 411 """
413 412 if forcibly:
414 413 def onerror(function, path, excinfo):
415 414 if function is not os.remove:
416 415 raise
417 416 # read-only files cannot be unlinked under Windows
418 417 s = os.stat(path)
419 418 if (s.st_mode & stat.S_IWRITE) != 0:
420 419 raise
421 420 os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE)
422 421 os.remove(path)
423 422 else:
424 423 onerror = None
425 424 return shutil.rmtree(self.join(path),
426 425 ignore_errors=ignore_errors, onerror=onerror)
427 426
428 427 def setflags(self, path, l, x):
429 428 return util.setflags(self.join(path), l, x)
430 429
431 430 def stat(self, path=None):
432 431 return os.stat(self.join(path))
433 432
434 433 def unlink(self, path=None):
435 434 return util.unlink(self.join(path))
436 435
437 436 def unlinkpath(self, path=None, ignoremissing=False):
438 437 return util.unlinkpath(self.join(path), ignoremissing)
439 438
440 439 def utime(self, path=None, t=None):
441 440 return os.utime(self.join(path), t)
442 441
443 442 def walk(self, path=None, onerror=None):
444 443 """Yield (dirpath, dirs, files) tuple for each directories under path
445 444
446 445 ``dirpath`` is relative one from the root of this vfs. This
447 446 uses ``os.sep`` as path separator, even you specify POSIX
448 447 style ``path``.
449 448
450 449 "The root of this vfs" is represented as empty ``dirpath``.
451 450 """
452 451 root = os.path.normpath(self.join(None))
453 452 # when dirpath == root, dirpath[prefixlen:] becomes empty
454 453 # because len(dirpath) < prefixlen.
455 454 prefixlen = len(pathutil.normasprefix(root))
456 455 for dirpath, dirs, files in os.walk(self.join(path), onerror=onerror):
457 456 yield (dirpath[prefixlen:], dirs, files)
458 457
459 458 @contextlib.contextmanager
460 459 def backgroundclosing(self, ui, expectedcount=-1):
461 460 """Allow files to be closed asynchronously.
462 461
463 462 When this context manager is active, ``backgroundclose`` can be passed
464 463 to ``__call__``/``open`` to result in the file possibly being closed
465 464 asynchronously, on a background thread.
466 465 """
467 466 # This is an arbitrary restriction and could be changed if we ever
468 467 # have a use case.
469 468 vfs = getattr(self, 'vfs', self)
470 469 if getattr(vfs, '_backgroundfilecloser', None):
471 470 raise error.Abort(
472 471 _('can only have 1 active background file closer'))
473 472
474 473 with backgroundfilecloser(ui, expectedcount=expectedcount) as bfc:
475 474 try:
476 475 vfs._backgroundfilecloser = bfc
477 476 yield bfc
478 477 finally:
479 478 vfs._backgroundfilecloser = None
480 479
481 480 class vfs(abstractvfs):
482 481 '''Operate files relative to a base directory
483 482
484 483 This class is used to hide the details of COW semantics and
485 484 remote file access from higher level code.
486 485 '''
487 486 def __init__(self, base, audit=True, expandpath=False, realpath=False):
488 487 if expandpath:
489 488 base = util.expandpath(base)
490 489 if realpath:
491 490 base = os.path.realpath(base)
492 491 self.base = base
493 492 self.mustaudit = audit
494 493 self.createmode = None
495 494 self._trustnlink = None
496 495
497 496 @property
498 497 def mustaudit(self):
499 498 return self._audit
500 499
501 500 @mustaudit.setter
502 501 def mustaudit(self, onoff):
503 502 self._audit = onoff
504 503 if onoff:
505 504 self.audit = pathutil.pathauditor(self.base)
506 505 else:
507 506 self.audit = util.always
508 507
509 508 @util.propertycache
510 509 def _cansymlink(self):
511 510 return util.checklink(self.base)
512 511
513 512 @util.propertycache
514 513 def _chmod(self):
515 514 return util.checkexec(self.base)
516 515
517 516 def _fixfilemode(self, name):
518 517 if self.createmode is None or not self._chmod:
519 518 return
520 519 os.chmod(name, self.createmode & 0o666)
521 520
522 521 def __call__(self, path, mode="r", text=False, atomictemp=False,
523 522 notindexed=False, backgroundclose=False, checkambig=False):
524 523 '''Open ``path`` file, which is relative to vfs root.
525 524
526 525 Newly created directories are marked as "not to be indexed by
527 526 the content indexing service", if ``notindexed`` is specified
528 527 for "write" mode access.
529 528
530 529 If ``backgroundclose`` is passed, the file may be closed asynchronously.
531 530 It can only be used if the ``self.backgroundclosing()`` context manager
532 531 is active. This should only be specified if the following criteria hold:
533 532
534 533 1. There is a potential for writing thousands of files. Unless you
535 534 are writing thousands of files, the performance benefits of
536 535 asynchronously closing files is not realized.
537 536 2. Files are opened exactly once for the ``backgroundclosing``
538 537 active duration and are therefore free of race conditions between
539 538 closing a file on a background thread and reopening it. (If the
540 539 file were opened multiple times, there could be unflushed data
541 540 because the original file handle hasn't been flushed/closed yet.)
542 541
543 542 ``checkambig`` argument is passed to atomictemplfile (valid
544 543 only for writing), and is useful only if target file is
545 544 guarded by any lock (e.g. repo.lock or repo.wlock).
546 545 '''
547 546 if self._audit:
548 547 r = util.checkosfilename(path)
549 548 if r:
550 549 raise error.Abort("%s: %r" % (r, path))
551 550 self.audit(path)
552 551 f = self.join(path)
553 552
554 553 if not text and "b" not in mode:
555 554 mode += "b" # for that other OS
556 555
557 556 nlink = -1
558 557 if mode not in ('r', 'rb'):
559 558 dirname, basename = util.split(f)
560 559 # If basename is empty, then the path is malformed because it points
561 560 # to a directory. Let the posixfile() call below raise IOError.
562 561 if basename:
563 562 if atomictemp:
564 563 util.makedirs(dirname, self.createmode, notindexed)
565 564 return util.atomictempfile(f, mode, self.createmode,
566 565 checkambig=checkambig)
567 566 try:
568 567 if 'w' in mode:
569 568 util.unlink(f)
570 569 nlink = 0
571 570 else:
572 571 # nlinks() may behave differently for files on Windows
573 572 # shares if the file is open.
574 573 with util.posixfile(f):
575 574 nlink = util.nlinks(f)
576 575 if nlink < 1:
577 576 nlink = 2 # force mktempcopy (issue1922)
578 577 except (OSError, IOError) as e:
579 578 if e.errno != errno.ENOENT:
580 579 raise
581 580 nlink = 0
582 581 util.makedirs(dirname, self.createmode, notindexed)
583 582 if nlink > 0:
584 583 if self._trustnlink is None:
585 584 self._trustnlink = nlink > 1 or util.checknlink(f)
586 585 if nlink > 1 or not self._trustnlink:
587 586 util.rename(util.mktempcopy(f), f)
588 587 fp = util.posixfile(f, mode)
589 588 if nlink == 0:
590 589 self._fixfilemode(f)
591 590
592 591 if checkambig:
593 592 if mode in ('r', 'rb'):
594 593 raise error.Abort(_('implementation error: mode %s is not'
595 594 ' valid for checkambig=True') % mode)
596 595 fp = checkambigatclosing(fp)
597 596
598 597 if backgroundclose:
599 598 if not self._backgroundfilecloser:
600 599 raise error.Abort(_('backgroundclose can only be used when a '
601 600 'backgroundclosing context manager is active')
602 601 )
603 602
604 603 fp = delayclosedfile(fp, self._backgroundfilecloser)
605 604
606 605 return fp
607 606
608 607 def symlink(self, src, dst):
609 608 self.audit(dst)
610 609 linkname = self.join(dst)
611 610 try:
612 611 os.unlink(linkname)
613 612 except OSError:
614 613 pass
615 614
616 615 util.makedirs(os.path.dirname(linkname), self.createmode)
617 616
618 617 if self._cansymlink:
619 618 try:
620 619 os.symlink(src, linkname)
621 620 except OSError as err:
622 621 raise OSError(err.errno, _('could not symlink to %r: %s') %
623 622 (src, err.strerror), linkname)
624 623 else:
625 624 self.write(dst, src)
626 625
627 626 def join(self, path, *insidef):
628 627 if path:
629 628 return os.path.join(self.base, path, *insidef)
630 629 else:
631 630 return self.base
632 631
633 632 opener = vfs
634 633
635 634 class auditvfs(object):
636 635 def __init__(self, vfs):
637 636 self.vfs = vfs
638 637
639 638 @property
640 639 def mustaudit(self):
641 640 return self.vfs.mustaudit
642 641
643 642 @mustaudit.setter
644 643 def mustaudit(self, onoff):
645 644 self.vfs.mustaudit = onoff
646 645
647 646 @property
648 647 def options(self):
649 648 return self.vfs.options
650 649
651 650 @options.setter
652 651 def options(self, value):
653 652 self.vfs.options = value
654 653
655 654 class filtervfs(abstractvfs, auditvfs):
656 655 '''Wrapper vfs for filtering filenames with a function.'''
657 656
658 657 def __init__(self, vfs, filter):
659 658 auditvfs.__init__(self, vfs)
660 659 self._filter = filter
661 660
662 661 def __call__(self, path, *args, **kwargs):
663 662 return self.vfs(self._filter(path), *args, **kwargs)
664 663
665 664 def join(self, path, *insidef):
666 665 if path:
667 666 return self.vfs.join(self._filter(self.vfs.reljoin(path, *insidef)))
668 667 else:
669 668 return self.vfs.join(path)
670 669
671 670 filteropener = filtervfs
672 671
673 672 class readonlyvfs(abstractvfs, auditvfs):
674 673 '''Wrapper vfs preventing any writing.'''
675 674
676 675 def __init__(self, vfs):
677 676 auditvfs.__init__(self, vfs)
678 677
679 678 def __call__(self, path, mode='r', *args, **kw):
680 679 if mode not in ('r', 'rb'):
681 680 raise error.Abort(_('this vfs is read only'))
682 681 return self.vfs(path, mode, *args, **kw)
683 682
684 683 def join(self, path, *insidef):
685 684 return self.vfs.join(path, *insidef)
686 685
687 686 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
688 687 '''yield every hg repository under path, always recursively.
689 688 The recurse flag will only control recursion into repo working dirs'''
690 689 def errhandler(err):
691 690 if err.filename == path:
692 691 raise err
693 692 samestat = getattr(os.path, 'samestat', None)
694 693 if followsym and samestat is not None:
695 694 def adddir(dirlst, dirname):
696 695 match = False
697 696 dirstat = os.stat(dirname)
698 697 for lstdirstat in dirlst:
699 698 if samestat(dirstat, lstdirstat):
700 699 match = True
701 700 break
702 701 if not match:
703 702 dirlst.append(dirstat)
704 703 return not match
705 704 else:
706 705 followsym = False
707 706
708 707 if (seen_dirs is None) and followsym:
709 708 seen_dirs = []
710 709 adddir(seen_dirs, path)
711 710 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
712 711 dirs.sort()
713 712 if '.hg' in dirs:
714 713 yield root # found a repository
715 714 qroot = os.path.join(root, '.hg', 'patches')
716 715 if os.path.isdir(os.path.join(qroot, '.hg')):
717 716 yield qroot # we have a patch queue repo here
718 717 if recurse:
719 718 # avoid recursing inside the .hg directory
720 719 dirs.remove('.hg')
721 720 else:
722 721 dirs[:] = [] # don't descend further
723 722 elif followsym:
724 723 newdirs = []
725 724 for d in dirs:
726 725 fname = os.path.join(root, d)
727 726 if adddir(seen_dirs, fname):
728 727 if os.path.islink(fname):
729 728 for hgname in walkrepos(fname, True, seen_dirs):
730 729 yield hgname
731 730 else:
732 731 newdirs.append(d)
733 732 dirs[:] = newdirs
734 733
735 734 def osrcpath():
736 735 '''return default os-specific hgrc search path'''
737 736 path = []
738 737 defaultpath = os.path.join(util.datapath, 'default.d')
739 738 if os.path.isdir(defaultpath):
740 739 for f, kind in osutil.listdir(defaultpath):
741 740 if f.endswith('.rc'):
742 741 path.append(os.path.join(defaultpath, f))
743 742 path.extend(systemrcpath())
744 743 path.extend(userrcpath())
745 744 path = [os.path.normpath(f) for f in path]
746 745 return path
747 746
748 747 _rcpath = None
749 748
750 749 def rcpath():
751 750 '''return hgrc search path. if env var HGRCPATH is set, use it.
752 751 for each item in path, if directory, use files ending in .rc,
753 752 else use item.
754 753 make HGRCPATH empty to only look in .hg/hgrc of current repo.
755 754 if no HGRCPATH, use default os-specific path.'''
756 755 global _rcpath
757 756 if _rcpath is None:
758 757 if 'HGRCPATH' in encoding.environ:
759 758 _rcpath = []
760 759 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
761 760 if not p:
762 761 continue
763 762 p = util.expandpath(p)
764 763 if os.path.isdir(p):
765 764 for f, kind in osutil.listdir(p):
766 765 if f.endswith('.rc'):
767 766 _rcpath.append(os.path.join(p, f))
768 767 else:
769 768 _rcpath.append(p)
770 769 else:
771 770 _rcpath = osrcpath()
772 771 return _rcpath
773 772
774 773 def intrev(rev):
775 774 """Return integer for a given revision that can be used in comparison or
776 775 arithmetic operation"""
777 776 if rev is None:
778 777 return wdirrev
779 778 return rev
780 779
781 780 def revsingle(repo, revspec, default='.'):
782 781 if not revspec and revspec != 0:
783 782 return repo[default]
784 783
785 784 l = revrange(repo, [revspec])
786 785 if not l:
787 786 raise error.Abort(_('empty revision set'))
788 787 return repo[l.last()]
789 788
790 789 def _pairspec(revspec):
791 790 tree = revset.parse(revspec)
792 791 return tree and tree[0] in ('range', 'rangepre', 'rangepost', 'rangeall')
793 792
794 793 def revpair(repo, revs):
795 794 if not revs:
796 795 return repo.dirstate.p1(), None
797 796
798 797 l = revrange(repo, revs)
799 798
800 799 if not l:
801 800 first = second = None
802 801 elif l.isascending():
803 802 first = l.min()
804 803 second = l.max()
805 804 elif l.isdescending():
806 805 first = l.max()
807 806 second = l.min()
808 807 else:
809 808 first = l.first()
810 809 second = l.last()
811 810
812 811 if first is None:
813 812 raise error.Abort(_('empty revision range'))
814 813 if (first == second and len(revs) >= 2
815 814 and not all(revrange(repo, [r]) for r in revs)):
816 815 raise error.Abort(_('empty revision on one side of range'))
817 816
818 817 # if top-level is range expression, the result must always be a pair
819 818 if first == second and len(revs) == 1 and not _pairspec(revs[0]):
820 819 return repo.lookup(first), None
821 820
822 821 return repo.lookup(first), repo.lookup(second)
823 822
824 823 def revrange(repo, specs):
825 824 """Execute 1 to many revsets and return the union.
826 825
827 826 This is the preferred mechanism for executing revsets using user-specified
828 827 config options, such as revset aliases.
829 828
830 829 The revsets specified by ``specs`` will be executed via a chained ``OR``
831 830 expression. If ``specs`` is empty, an empty result is returned.
832 831
833 832 ``specs`` can contain integers, in which case they are assumed to be
834 833 revision numbers.
835 834
836 835 It is assumed the revsets are already formatted. If you have arguments
837 836 that need to be expanded in the revset, call ``revset.formatspec()``
838 837 and pass the result as an element of ``specs``.
839 838
840 839 Specifying a single revset is allowed.
841 840
842 841 Returns a ``revset.abstractsmartset`` which is a list-like interface over
843 842 integer revisions.
844 843 """
845 844 allspecs = []
846 845 for spec in specs:
847 846 if isinstance(spec, int):
848 847 spec = revset.formatspec('rev(%d)', spec)
849 848 allspecs.append(spec)
850 849 m = revset.matchany(repo.ui, allspecs, repo)
851 850 return m(repo)
852 851
853 852 def meaningfulparents(repo, ctx):
854 853 """Return list of meaningful (or all if debug) parentrevs for rev.
855 854
856 855 For merges (two non-nullrev revisions) both parents are meaningful.
857 856 Otherwise the first parent revision is considered meaningful if it
858 857 is not the preceding revision.
859 858 """
860 859 parents = ctx.parents()
861 860 if len(parents) > 1:
862 861 return parents
863 862 if repo.ui.debugflag:
864 863 return [parents[0], repo['null']]
865 864 if parents[0].rev() >= intrev(ctx.rev()) - 1:
866 865 return []
867 866 return parents
868 867
869 868 def expandpats(pats):
870 869 '''Expand bare globs when running on windows.
871 870 On posix we assume it already has already been done by sh.'''
872 871 if not util.expandglobs:
873 872 return list(pats)
874 873 ret = []
875 874 for kindpat in pats:
876 875 kind, pat = matchmod._patsplit(kindpat, None)
877 876 if kind is None:
878 877 try:
879 878 globbed = glob.glob(pat)
880 879 except re.error:
881 880 globbed = [pat]
882 881 if globbed:
883 882 ret.extend(globbed)
884 883 continue
885 884 ret.append(kindpat)
886 885 return ret
887 886
888 887 def matchandpats(ctx, pats=(), opts=None, globbed=False, default='relpath',
889 888 badfn=None):
890 889 '''Return a matcher and the patterns that were used.
891 890 The matcher will warn about bad matches, unless an alternate badfn callback
892 891 is provided.'''
893 892 if pats == ("",):
894 893 pats = []
895 894 if opts is None:
896 895 opts = {}
897 896 if not globbed and default == 'relpath':
898 897 pats = expandpats(pats or [])
899 898
900 899 def bad(f, msg):
901 900 ctx.repo().ui.warn("%s: %s\n" % (m.rel(f), msg))
902 901
903 902 if badfn is None:
904 903 badfn = bad
905 904
906 905 m = ctx.match(pats, opts.get('include'), opts.get('exclude'),
907 906 default, listsubrepos=opts.get('subrepos'), badfn=badfn)
908 907
909 908 if m.always():
910 909 pats = []
911 910 return m, pats
912 911
913 912 def match(ctx, pats=(), opts=None, globbed=False, default='relpath',
914 913 badfn=None):
915 914 '''Return a matcher that will warn about bad matches.'''
916 915 return matchandpats(ctx, pats, opts, globbed, default, badfn=badfn)[0]
917 916
918 917 def matchall(repo):
919 918 '''Return a matcher that will efficiently match everything.'''
920 919 return matchmod.always(repo.root, repo.getcwd())
921 920
922 921 def matchfiles(repo, files, badfn=None):
923 922 '''Return a matcher that will efficiently match exactly these files.'''
924 923 return matchmod.exact(repo.root, repo.getcwd(), files, badfn=badfn)
925 924
926 925 def origpath(ui, repo, filepath):
927 926 '''customize where .orig files are created
928 927
929 928 Fetch user defined path from config file: [ui] origbackuppath = <path>
930 929 Fall back to default (filepath) if not specified
931 930 '''
932 931 origbackuppath = ui.config('ui', 'origbackuppath', None)
933 932 if origbackuppath is None:
934 933 return filepath + ".orig"
935 934
936 935 filepathfromroot = os.path.relpath(filepath, start=repo.root)
937 936 fullorigpath = repo.wjoin(origbackuppath, filepathfromroot)
938 937
939 938 origbackupdir = repo.vfs.dirname(fullorigpath)
940 939 if not repo.vfs.exists(origbackupdir):
941 940 ui.note(_('creating directory: %s\n') % origbackupdir)
942 941 util.makedirs(origbackupdir)
943 942
944 943 return fullorigpath + ".orig"
945 944
946 945 def addremove(repo, matcher, prefix, opts=None, dry_run=None, similarity=None):
947 946 if opts is None:
948 947 opts = {}
949 948 m = matcher
950 949 if dry_run is None:
951 950 dry_run = opts.get('dry_run')
952 951 if similarity is None:
953 952 similarity = float(opts.get('similarity') or 0)
954 953
955 954 ret = 0
956 955 join = lambda f: os.path.join(prefix, f)
957 956
958 957 wctx = repo[None]
959 958 for subpath in sorted(wctx.substate):
960 959 submatch = matchmod.subdirmatcher(subpath, m)
961 960 if opts.get('subrepos') or m.exact(subpath) or any(submatch.files()):
962 961 sub = wctx.sub(subpath)
963 962 try:
964 963 if sub.addremove(submatch, prefix, opts, dry_run, similarity):
965 964 ret = 1
966 965 except error.LookupError:
967 966 repo.ui.status(_("skipping missing subrepository: %s\n")
968 967 % join(subpath))
969 968
970 969 rejected = []
971 970 def badfn(f, msg):
972 971 if f in m.files():
973 972 m.bad(f, msg)
974 973 rejected.append(f)
975 974
976 975 badmatch = matchmod.badmatch(m, badfn)
977 976 added, unknown, deleted, removed, forgotten = _interestingfiles(repo,
978 977 badmatch)
979 978
980 979 unknownset = set(unknown + forgotten)
981 980 toprint = unknownset.copy()
982 981 toprint.update(deleted)
983 982 for abs in sorted(toprint):
984 983 if repo.ui.verbose or not m.exact(abs):
985 984 if abs in unknownset:
986 985 status = _('adding %s\n') % m.uipath(abs)
987 986 else:
988 987 status = _('removing %s\n') % m.uipath(abs)
989 988 repo.ui.status(status)
990 989
991 990 renames = _findrenames(repo, m, added + unknown, removed + deleted,
992 991 similarity)
993 992
994 993 if not dry_run:
995 994 _markchanges(repo, unknown + forgotten, deleted, renames)
996 995
997 996 for f in rejected:
998 997 if f in m.files():
999 998 return 1
1000 999 return ret
1001 1000
1002 1001 def marktouched(repo, files, similarity=0.0):
1003 1002 '''Assert that files have somehow been operated upon. files are relative to
1004 1003 the repo root.'''
1005 1004 m = matchfiles(repo, files, badfn=lambda x, y: rejected.append(x))
1006 1005 rejected = []
1007 1006
1008 1007 added, unknown, deleted, removed, forgotten = _interestingfiles(repo, m)
1009 1008
1010 1009 if repo.ui.verbose:
1011 1010 unknownset = set(unknown + forgotten)
1012 1011 toprint = unknownset.copy()
1013 1012 toprint.update(deleted)
1014 1013 for abs in sorted(toprint):
1015 1014 if abs in unknownset:
1016 1015 status = _('adding %s\n') % abs
1017 1016 else:
1018 1017 status = _('removing %s\n') % abs
1019 1018 repo.ui.status(status)
1020 1019
1021 1020 renames = _findrenames(repo, m, added + unknown, removed + deleted,
1022 1021 similarity)
1023 1022
1024 1023 _markchanges(repo, unknown + forgotten, deleted, renames)
1025 1024
1026 1025 for f in rejected:
1027 1026 if f in m.files():
1028 1027 return 1
1029 1028 return 0
1030 1029
1031 1030 def _interestingfiles(repo, matcher):
1032 1031 '''Walk dirstate with matcher, looking for files that addremove would care
1033 1032 about.
1034 1033
1035 1034 This is different from dirstate.status because it doesn't care about
1036 1035 whether files are modified or clean.'''
1037 1036 added, unknown, deleted, removed, forgotten = [], [], [], [], []
1038 1037 audit_path = pathutil.pathauditor(repo.root)
1039 1038
1040 1039 ctx = repo[None]
1041 1040 dirstate = repo.dirstate
1042 1041 walkresults = dirstate.walk(matcher, sorted(ctx.substate), True, False,
1043 1042 full=False)
1044 1043 for abs, st in walkresults.iteritems():
1045 1044 dstate = dirstate[abs]
1046 1045 if dstate == '?' and audit_path.check(abs):
1047 1046 unknown.append(abs)
1048 1047 elif dstate != 'r' and not st:
1049 1048 deleted.append(abs)
1050 1049 elif dstate == 'r' and st:
1051 1050 forgotten.append(abs)
1052 1051 # for finding renames
1053 1052 elif dstate == 'r' and not st:
1054 1053 removed.append(abs)
1055 1054 elif dstate == 'a':
1056 1055 added.append(abs)
1057 1056
1058 1057 return added, unknown, deleted, removed, forgotten
1059 1058
1060 1059 def _findrenames(repo, matcher, added, removed, similarity):
1061 1060 '''Find renames from removed files to added ones.'''
1062 1061 renames = {}
1063 1062 if similarity > 0:
1064 1063 for old, new, score in similar.findrenames(repo, added, removed,
1065 1064 similarity):
1066 1065 if (repo.ui.verbose or not matcher.exact(old)
1067 1066 or not matcher.exact(new)):
1068 1067 repo.ui.status(_('recording removal of %s as rename to %s '
1069 1068 '(%d%% similar)\n') %
1070 1069 (matcher.rel(old), matcher.rel(new),
1071 1070 score * 100))
1072 1071 renames[new] = old
1073 1072 return renames
1074 1073
1075 1074 def _markchanges(repo, unknown, deleted, renames):
1076 1075 '''Marks the files in unknown as added, the files in deleted as removed,
1077 1076 and the files in renames as copied.'''
1078 1077 wctx = repo[None]
1079 1078 with repo.wlock():
1080 1079 wctx.forget(deleted)
1081 1080 wctx.add(unknown)
1082 1081 for new, old in renames.iteritems():
1083 1082 wctx.copy(old, new)
1084 1083
1085 1084 def dirstatecopy(ui, repo, wctx, src, dst, dryrun=False, cwd=None):
1086 1085 """Update the dirstate to reflect the intent of copying src to dst. For
1087 1086 different reasons it might not end with dst being marked as copied from src.
1088 1087 """
1089 1088 origsrc = repo.dirstate.copied(src) or src
1090 1089 if dst == origsrc: # copying back a copy?
1091 1090 if repo.dirstate[dst] not in 'mn' and not dryrun:
1092 1091 repo.dirstate.normallookup(dst)
1093 1092 else:
1094 1093 if repo.dirstate[origsrc] == 'a' and origsrc == src:
1095 1094 if not ui.quiet:
1096 1095 ui.warn(_("%s has not been committed yet, so no copy "
1097 1096 "data will be stored for %s.\n")
1098 1097 % (repo.pathto(origsrc, cwd), repo.pathto(dst, cwd)))
1099 1098 if repo.dirstate[dst] in '?r' and not dryrun:
1100 1099 wctx.add([dst])
1101 1100 elif not dryrun:
1102 1101 wctx.copy(origsrc, dst)
1103 1102
1104 1103 def readrequires(opener, supported):
1105 1104 '''Reads and parses .hg/requires and checks if all entries found
1106 1105 are in the list of supported features.'''
1107 1106 requirements = set(opener.read("requires").splitlines())
1108 1107 missings = []
1109 1108 for r in requirements:
1110 1109 if r not in supported:
1111 1110 if not r or not r[0].isalnum():
1112 1111 raise error.RequirementError(_(".hg/requires file is corrupt"))
1113 1112 missings.append(r)
1114 1113 missings.sort()
1115 1114 if missings:
1116 1115 raise error.RequirementError(
1117 1116 _("repository requires features unknown to this Mercurial: %s")
1118 1117 % " ".join(missings),
1119 1118 hint=_("see https://mercurial-scm.org/wiki/MissingRequirement"
1120 1119 " for more information"))
1121 1120 return requirements
1122 1121
1123 1122 def writerequires(opener, requirements):
1124 1123 with opener('requires', 'w') as fp:
1125 1124 for r in sorted(requirements):
1126 1125 fp.write("%s\n" % r)
1127 1126
1128 1127 class filecachesubentry(object):
1129 1128 def __init__(self, path, stat):
1130 1129 self.path = path
1131 1130 self.cachestat = None
1132 1131 self._cacheable = None
1133 1132
1134 1133 if stat:
1135 1134 self.cachestat = filecachesubentry.stat(self.path)
1136 1135
1137 1136 if self.cachestat:
1138 1137 self._cacheable = self.cachestat.cacheable()
1139 1138 else:
1140 1139 # None means we don't know yet
1141 1140 self._cacheable = None
1142 1141
1143 1142 def refresh(self):
1144 1143 if self.cacheable():
1145 1144 self.cachestat = filecachesubentry.stat(self.path)
1146 1145
1147 1146 def cacheable(self):
1148 1147 if self._cacheable is not None:
1149 1148 return self._cacheable
1150 1149
1151 1150 # we don't know yet, assume it is for now
1152 1151 return True
1153 1152
1154 1153 def changed(self):
1155 1154 # no point in going further if we can't cache it
1156 1155 if not self.cacheable():
1157 1156 return True
1158 1157
1159 1158 newstat = filecachesubentry.stat(self.path)
1160 1159
1161 1160 # we may not know if it's cacheable yet, check again now
1162 1161 if newstat and self._cacheable is None:
1163 1162 self._cacheable = newstat.cacheable()
1164 1163
1165 1164 # check again
1166 1165 if not self._cacheable:
1167 1166 return True
1168 1167
1169 1168 if self.cachestat != newstat:
1170 1169 self.cachestat = newstat
1171 1170 return True
1172 1171 else:
1173 1172 return False
1174 1173
1175 1174 @staticmethod
1176 1175 def stat(path):
1177 1176 try:
1178 1177 return util.cachestat(path)
1179 1178 except OSError as e:
1180 1179 if e.errno != errno.ENOENT:
1181 1180 raise
1182 1181
1183 1182 class filecacheentry(object):
1184 1183 def __init__(self, paths, stat=True):
1185 1184 self._entries = []
1186 1185 for path in paths:
1187 1186 self._entries.append(filecachesubentry(path, stat))
1188 1187
1189 1188 def changed(self):
1190 1189 '''true if any entry has changed'''
1191 1190 for entry in self._entries:
1192 1191 if entry.changed():
1193 1192 return True
1194 1193 return False
1195 1194
1196 1195 def refresh(self):
1197 1196 for entry in self._entries:
1198 1197 entry.refresh()
1199 1198
1200 1199 class filecache(object):
1201 1200 '''A property like decorator that tracks files under .hg/ for updates.
1202 1201
1203 1202 Records stat info when called in _filecache.
1204 1203
1205 1204 On subsequent calls, compares old stat info with new info, and recreates the
1206 1205 object when any of the files changes, updating the new stat info in
1207 1206 _filecache.
1208 1207
1209 1208 Mercurial either atomic renames or appends for files under .hg,
1210 1209 so to ensure the cache is reliable we need the filesystem to be able
1211 1210 to tell us if a file has been replaced. If it can't, we fallback to
1212 1211 recreating the object on every call (essentially the same behavior as
1213 1212 propertycache).
1214 1213
1215 1214 '''
1216 1215 def __init__(self, *paths):
1217 1216 self.paths = paths
1218 1217
1219 1218 def join(self, obj, fname):
1220 1219 """Used to compute the runtime path of a cached file.
1221 1220
1222 1221 Users should subclass filecache and provide their own version of this
1223 1222 function to call the appropriate join function on 'obj' (an instance
1224 1223 of the class that its member function was decorated).
1225 1224 """
1226 1225 return obj.join(fname)
1227 1226
1228 1227 def __call__(self, func):
1229 1228 self.func = func
1230 1229 self.name = func.__name__
1231 1230 return self
1232 1231
1233 1232 def __get__(self, obj, type=None):
1234 1233 # if accessed on the class, return the descriptor itself.
1235 1234 if obj is None:
1236 1235 return self
1237 1236 # do we need to check if the file changed?
1238 1237 if self.name in obj.__dict__:
1239 1238 assert self.name in obj._filecache, self.name
1240 1239 return obj.__dict__[self.name]
1241 1240
1242 1241 entry = obj._filecache.get(self.name)
1243 1242
1244 1243 if entry:
1245 1244 if entry.changed():
1246 1245 entry.obj = self.func(obj)
1247 1246 else:
1248 1247 paths = [self.join(obj, path) for path in self.paths]
1249 1248
1250 1249 # We stat -before- creating the object so our cache doesn't lie if
1251 1250 # a writer modified between the time we read and stat
1252 1251 entry = filecacheentry(paths, True)
1253 1252 entry.obj = self.func(obj)
1254 1253
1255 1254 obj._filecache[self.name] = entry
1256 1255
1257 1256 obj.__dict__[self.name] = entry.obj
1258 1257 return entry.obj
1259 1258
1260 1259 def __set__(self, obj, value):
1261 1260 if self.name not in obj._filecache:
1262 1261 # we add an entry for the missing value because X in __dict__
1263 1262 # implies X in _filecache
1264 1263 paths = [self.join(obj, path) for path in self.paths]
1265 1264 ce = filecacheentry(paths, False)
1266 1265 obj._filecache[self.name] = ce
1267 1266 else:
1268 1267 ce = obj._filecache[self.name]
1269 1268
1270 1269 ce.obj = value # update cached copy
1271 1270 obj.__dict__[self.name] = value # update copy returned by obj.x
1272 1271
1273 1272 def __delete__(self, obj):
1274 1273 try:
1275 1274 del obj.__dict__[self.name]
1276 1275 except KeyError:
1277 1276 raise AttributeError(self.name)
1278 1277
1279 1278 def _locksub(repo, lock, envvar, cmd, environ=None, *args, **kwargs):
1280 1279 if lock is None:
1281 1280 raise error.LockInheritanceContractViolation(
1282 1281 'lock can only be inherited while held')
1283 1282 if environ is None:
1284 1283 environ = {}
1285 1284 with lock.inherit() as locker:
1286 1285 environ[envvar] = locker
1287 1286 return repo.ui.system(cmd, environ=environ, *args, **kwargs)
1288 1287
1289 1288 def wlocksub(repo, cmd, *args, **kwargs):
1290 1289 """run cmd as a subprocess that allows inheriting repo's wlock
1291 1290
1292 1291 This can only be called while the wlock is held. This takes all the
1293 1292 arguments that ui.system does, and returns the exit code of the
1294 1293 subprocess."""
1295 1294 return _locksub(repo, repo.currentwlock(), 'HG_WLOCK_LOCKER', cmd, *args,
1296 1295 **kwargs)
1297 1296
1298 1297 def gdinitconfig(ui):
1299 1298 """helper function to know if a repo should be created as general delta
1300 1299 """
1301 1300 # experimental config: format.generaldelta
1302 1301 return (ui.configbool('format', 'generaldelta', False)
1303 1302 or ui.configbool('format', 'usegeneraldelta', True))
1304 1303
1305 1304 def gddeltaconfig(ui):
1306 1305 """helper function to know if incoming delta should be optimised
1307 1306 """
1308 1307 # experimental config: format.generaldelta
1309 1308 return ui.configbool('format', 'generaldelta', False)
1310 1309
1311 1310 class closewrapbase(object):
1312 1311 """Base class of wrapper, which hooks closing
1313 1312
1314 1313 Do not instantiate outside of the vfs layer.
1315 1314 """
1316 1315 def __init__(self, fh):
1317 1316 object.__setattr__(self, '_origfh', fh)
1318 1317
1319 1318 def __getattr__(self, attr):
1320 1319 return getattr(self._origfh, attr)
1321 1320
1322 1321 def __setattr__(self, attr, value):
1323 1322 return setattr(self._origfh, attr, value)
1324 1323
1325 1324 def __delattr__(self, attr):
1326 1325 return delattr(self._origfh, attr)
1327 1326
1328 1327 def __enter__(self):
1329 1328 return self._origfh.__enter__()
1330 1329
1331 1330 def __exit__(self, exc_type, exc_value, exc_tb):
1332 1331 raise NotImplementedError('attempted instantiating ' + str(type(self)))
1333 1332
1334 1333 def close(self):
1335 1334 raise NotImplementedError('attempted instantiating ' + str(type(self)))
1336 1335
1337 1336 class delayclosedfile(closewrapbase):
1338 1337 """Proxy for a file object whose close is delayed.
1339 1338
1340 1339 Do not instantiate outside of the vfs layer.
1341 1340 """
1342 1341 def __init__(self, fh, closer):
1343 1342 super(delayclosedfile, self).__init__(fh)
1344 1343 object.__setattr__(self, '_closer', closer)
1345 1344
1346 1345 def __exit__(self, exc_type, exc_value, exc_tb):
1347 1346 self._closer.close(self._origfh)
1348 1347
1349 1348 def close(self):
1350 1349 self._closer.close(self._origfh)
1351 1350
1352 1351 class backgroundfilecloser(object):
1353 1352 """Coordinates background closing of file handles on multiple threads."""
1354 1353 def __init__(self, ui, expectedcount=-1):
1355 1354 self._running = False
1356 1355 self._entered = False
1357 1356 self._threads = []
1358 1357 self._threadexception = None
1359 1358
1360 1359 # Only Windows/NTFS has slow file closing. So only enable by default
1361 1360 # on that platform. But allow to be enabled elsewhere for testing.
1362 1361 defaultenabled = os.name == 'nt'
1363 1362 enabled = ui.configbool('worker', 'backgroundclose', defaultenabled)
1364 1363
1365 1364 if not enabled:
1366 1365 return
1367 1366
1368 1367 # There is overhead to starting and stopping the background threads.
1369 1368 # Don't do background processing unless the file count is large enough
1370 1369 # to justify it.
1371 1370 minfilecount = ui.configint('worker', 'backgroundcloseminfilecount',
1372 1371 2048)
1373 1372 # FUTURE dynamically start background threads after minfilecount closes.
1374 1373 # (We don't currently have any callers that don't know their file count)
1375 1374 if expectedcount > 0 and expectedcount < minfilecount:
1376 1375 return
1377 1376
1378 1377 # Windows defaults to a limit of 512 open files. A buffer of 128
1379 1378 # should give us enough headway.
1380 1379 maxqueue = ui.configint('worker', 'backgroundclosemaxqueue', 384)
1381 1380 threadcount = ui.configint('worker', 'backgroundclosethreadcount', 4)
1382 1381
1383 1382 ui.debug('starting %d threads for background file closing\n' %
1384 1383 threadcount)
1385 1384
1386 1385 self._queue = util.queue(maxsize=maxqueue)
1387 1386 self._running = True
1388 1387
1389 1388 for i in range(threadcount):
1390 1389 t = threading.Thread(target=self._worker, name='backgroundcloser')
1391 1390 self._threads.append(t)
1392 1391 t.start()
1393 1392
1394 1393 def __enter__(self):
1395 1394 self._entered = True
1396 1395 return self
1397 1396
1398 1397 def __exit__(self, exc_type, exc_value, exc_tb):
1399 1398 self._running = False
1400 1399
1401 1400 # Wait for threads to finish closing so open files don't linger for
1402 1401 # longer than lifetime of context manager.
1403 1402 for t in self._threads:
1404 1403 t.join()
1405 1404
1406 1405 def _worker(self):
1407 1406 """Main routine for worker thread."""
1408 1407 while True:
1409 1408 try:
1410 1409 fh = self._queue.get(block=True, timeout=0.100)
1411 1410 # Need to catch or the thread will terminate and
1412 1411 # we could orphan file descriptors.
1413 1412 try:
1414 1413 fh.close()
1415 1414 except Exception as e:
1416 1415 # Stash so can re-raise from main thread later.
1417 1416 self._threadexception = e
1418 1417 except util.empty:
1419 1418 if not self._running:
1420 1419 break
1421 1420
1422 1421 def close(self, fh):
1423 1422 """Schedule a file for closing."""
1424 1423 if not self._entered:
1425 1424 raise error.Abort(_('can only call close() when context manager '
1426 1425 'active'))
1427 1426
1428 1427 # If a background thread encountered an exception, raise now so we fail
1429 1428 # fast. Otherwise we may potentially go on for minutes until the error
1430 1429 # is acted on.
1431 1430 if self._threadexception:
1432 1431 e = self._threadexception
1433 1432 self._threadexception = None
1434 1433 raise e
1435 1434
1436 1435 # If we're not actively running, close synchronously.
1437 1436 if not self._running:
1438 1437 fh.close()
1439 1438 return
1440 1439
1441 1440 self._queue.put(fh, block=True, timeout=None)
1442 1441
1443 1442 class checkambigatclosing(closewrapbase):
1444 1443 """Proxy for a file object, to avoid ambiguity of file stat
1445 1444
1446 1445 See also util.filestat for detail about "ambiguity of file stat".
1447 1446
1448 1447 This proxy is useful only if the target file is guarded by any
1449 1448 lock (e.g. repo.lock or repo.wlock)
1450 1449
1451 1450 Do not instantiate outside of the vfs layer.
1452 1451 """
1453 1452 def __init__(self, fh):
1454 1453 super(checkambigatclosing, self).__init__(fh)
1455 1454 object.__setattr__(self, '_oldstat', util.filestat(fh.name))
1456 1455
1457 1456 def _checkambig(self):
1458 1457 oldstat = self._oldstat
1459 1458 if oldstat.stat:
1460 1459 newstat = util.filestat(self._origfh.name)
1461 1460 if newstat.isambig(oldstat):
1462 1461 # stat of changed file is ambiguous to original one
1463 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1464 os.utime(self._origfh.name, (advanced, advanced))
1462 newstat.avoidambig(self._origfh.name, oldstat)
1465 1463
1466 1464 def __exit__(self, exc_type, exc_value, exc_tb):
1467 1465 self._origfh.__exit__(exc_type, exc_value, exc_tb)
1468 1466 self._checkambig()
1469 1467
1470 1468 def close(self):
1471 1469 self._origfh.close()
1472 1470 self._checkambig()
@@ -1,3093 +1,3111 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
17 17
18 18 import bz2
19 19 import calendar
20 20 import collections
21 21 import datetime
22 22 import errno
23 23 import gc
24 24 import hashlib
25 25 import imp
26 26 import os
27 27 import re as remod
28 28 import shutil
29 29 import signal
30 30 import socket
31 31 import string
32 32 import subprocess
33 33 import sys
34 34 import tempfile
35 35 import textwrap
36 36 import time
37 37 import traceback
38 38 import zlib
39 39
40 40 from . import (
41 41 encoding,
42 42 error,
43 43 i18n,
44 44 osutil,
45 45 parsers,
46 46 pycompat,
47 47 )
48 48
49 49 for attr in (
50 50 'empty',
51 51 'httplib',
52 52 'httpserver',
53 53 'pickle',
54 54 'queue',
55 55 'urlerr',
56 56 'urlparse',
57 57 # we do import urlreq, but we do it outside the loop
58 58 #'urlreq',
59 59 'stringio',
60 60 'socketserver',
61 61 'xmlrpclib',
62 62 ):
63 63 a = pycompat.sysstr(attr)
64 64 globals()[a] = getattr(pycompat, a)
65 65
66 66 # This line is to make pyflakes happy:
67 67 urlreq = pycompat.urlreq
68 68
69 69 if os.name == 'nt':
70 70 from . import windows as platform
71 71 else:
72 72 from . import posix as platform
73 73
74 74 _ = i18n._
75 75
76 76 bindunixsocket = platform.bindunixsocket
77 77 cachestat = platform.cachestat
78 78 checkexec = platform.checkexec
79 79 checklink = platform.checklink
80 80 copymode = platform.copymode
81 81 executablepath = platform.executablepath
82 82 expandglobs = platform.expandglobs
83 83 explainexit = platform.explainexit
84 84 findexe = platform.findexe
85 85 gethgcmd = platform.gethgcmd
86 86 getuser = platform.getuser
87 87 getpid = os.getpid
88 88 groupmembers = platform.groupmembers
89 89 groupname = platform.groupname
90 90 hidewindow = platform.hidewindow
91 91 isexec = platform.isexec
92 92 isowner = platform.isowner
93 93 localpath = platform.localpath
94 94 lookupreg = platform.lookupreg
95 95 makedir = platform.makedir
96 96 nlinks = platform.nlinks
97 97 normpath = platform.normpath
98 98 normcase = platform.normcase
99 99 normcasespec = platform.normcasespec
100 100 normcasefallback = platform.normcasefallback
101 101 openhardlinks = platform.openhardlinks
102 102 oslink = platform.oslink
103 103 parsepatchoutput = platform.parsepatchoutput
104 104 pconvert = platform.pconvert
105 105 poll = platform.poll
106 106 popen = platform.popen
107 107 posixfile = platform.posixfile
108 108 quotecommand = platform.quotecommand
109 109 readpipe = platform.readpipe
110 110 rename = platform.rename
111 111 removedirs = platform.removedirs
112 112 samedevice = platform.samedevice
113 113 samefile = platform.samefile
114 114 samestat = platform.samestat
115 115 setbinary = platform.setbinary
116 116 setflags = platform.setflags
117 117 setsignalhandler = platform.setsignalhandler
118 118 shellquote = platform.shellquote
119 119 spawndetached = platform.spawndetached
120 120 split = platform.split
121 121 sshargs = platform.sshargs
122 122 statfiles = getattr(osutil, 'statfiles', platform.statfiles)
123 123 statisexec = platform.statisexec
124 124 statislink = platform.statislink
125 125 testpid = platform.testpid
126 126 umask = platform.umask
127 127 unlink = platform.unlink
128 128 unlinkpath = platform.unlinkpath
129 129 username = platform.username
130 130
131 131 # Python compatibility
132 132
133 133 _notset = object()
134 134
135 135 # disable Python's problematic floating point timestamps (issue4836)
136 136 # (Python hypocritically says you shouldn't change this behavior in
137 137 # libraries, and sure enough Mercurial is not a library.)
138 138 os.stat_float_times(False)
139 139
140 140 def safehasattr(thing, attr):
141 141 return getattr(thing, attr, _notset) is not _notset
142 142
143 143 DIGESTS = {
144 144 'md5': hashlib.md5,
145 145 'sha1': hashlib.sha1,
146 146 'sha512': hashlib.sha512,
147 147 }
148 148 # List of digest types from strongest to weakest
149 149 DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
150 150
151 151 for k in DIGESTS_BY_STRENGTH:
152 152 assert k in DIGESTS
153 153
154 154 class digester(object):
155 155 """helper to compute digests.
156 156
157 157 This helper can be used to compute one or more digests given their name.
158 158
159 159 >>> d = digester(['md5', 'sha1'])
160 160 >>> d.update('foo')
161 161 >>> [k for k in sorted(d)]
162 162 ['md5', 'sha1']
163 163 >>> d['md5']
164 164 'acbd18db4cc2f85cedef654fccc4a4d8'
165 165 >>> d['sha1']
166 166 '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'
167 167 >>> digester.preferred(['md5', 'sha1'])
168 168 'sha1'
169 169 """
170 170
171 171 def __init__(self, digests, s=''):
172 172 self._hashes = {}
173 173 for k in digests:
174 174 if k not in DIGESTS:
175 175 raise Abort(_('unknown digest type: %s') % k)
176 176 self._hashes[k] = DIGESTS[k]()
177 177 if s:
178 178 self.update(s)
179 179
180 180 def update(self, data):
181 181 for h in self._hashes.values():
182 182 h.update(data)
183 183
184 184 def __getitem__(self, key):
185 185 if key not in DIGESTS:
186 186 raise Abort(_('unknown digest type: %s') % k)
187 187 return self._hashes[key].hexdigest()
188 188
189 189 def __iter__(self):
190 190 return iter(self._hashes)
191 191
192 192 @staticmethod
193 193 def preferred(supported):
194 194 """returns the strongest digest type in both supported and DIGESTS."""
195 195
196 196 for k in DIGESTS_BY_STRENGTH:
197 197 if k in supported:
198 198 return k
199 199 return None
200 200
201 201 class digestchecker(object):
202 202 """file handle wrapper that additionally checks content against a given
203 203 size and digests.
204 204
205 205 d = digestchecker(fh, size, {'md5': '...'})
206 206
207 207 When multiple digests are given, all of them are validated.
208 208 """
209 209
210 210 def __init__(self, fh, size, digests):
211 211 self._fh = fh
212 212 self._size = size
213 213 self._got = 0
214 214 self._digests = dict(digests)
215 215 self._digester = digester(self._digests.keys())
216 216
217 217 def read(self, length=-1):
218 218 content = self._fh.read(length)
219 219 self._digester.update(content)
220 220 self._got += len(content)
221 221 return content
222 222
223 223 def validate(self):
224 224 if self._size != self._got:
225 225 raise Abort(_('size mismatch: expected %d, got %d') %
226 226 (self._size, self._got))
227 227 for k, v in self._digests.items():
228 228 if v != self._digester[k]:
229 229 # i18n: first parameter is a digest name
230 230 raise Abort(_('%s mismatch: expected %s, got %s') %
231 231 (k, v, self._digester[k]))
232 232
233 233 try:
234 234 buffer = buffer
235 235 except NameError:
236 236 if not pycompat.ispy3:
237 237 def buffer(sliceable, offset=0):
238 238 return sliceable[offset:]
239 239 else:
240 240 def buffer(sliceable, offset=0):
241 241 return memoryview(sliceable)[offset:]
242 242
243 243 closefds = os.name == 'posix'
244 244
245 245 _chunksize = 4096
246 246
247 247 class bufferedinputpipe(object):
248 248 """a manually buffered input pipe
249 249
250 250 Python will not let us use buffered IO and lazy reading with 'polling' at
251 251 the same time. We cannot probe the buffer state and select will not detect
252 252 that data are ready to read if they are already buffered.
253 253
254 254 This class let us work around that by implementing its own buffering
255 255 (allowing efficient readline) while offering a way to know if the buffer is
256 256 empty from the output (allowing collaboration of the buffer with polling).
257 257
258 258 This class lives in the 'util' module because it makes use of the 'os'
259 259 module from the python stdlib.
260 260 """
261 261
262 262 def __init__(self, input):
263 263 self._input = input
264 264 self._buffer = []
265 265 self._eof = False
266 266 self._lenbuf = 0
267 267
268 268 @property
269 269 def hasbuffer(self):
270 270 """True is any data is currently buffered
271 271
272 272 This will be used externally a pre-step for polling IO. If there is
273 273 already data then no polling should be set in place."""
274 274 return bool(self._buffer)
275 275
276 276 @property
277 277 def closed(self):
278 278 return self._input.closed
279 279
280 280 def fileno(self):
281 281 return self._input.fileno()
282 282
283 283 def close(self):
284 284 return self._input.close()
285 285
286 286 def read(self, size):
287 287 while (not self._eof) and (self._lenbuf < size):
288 288 self._fillbuffer()
289 289 return self._frombuffer(size)
290 290
291 291 def readline(self, *args, **kwargs):
292 292 if 1 < len(self._buffer):
293 293 # this should not happen because both read and readline end with a
294 294 # _frombuffer call that collapse it.
295 295 self._buffer = [''.join(self._buffer)]
296 296 self._lenbuf = len(self._buffer[0])
297 297 lfi = -1
298 298 if self._buffer:
299 299 lfi = self._buffer[-1].find('\n')
300 300 while (not self._eof) and lfi < 0:
301 301 self._fillbuffer()
302 302 if self._buffer:
303 303 lfi = self._buffer[-1].find('\n')
304 304 size = lfi + 1
305 305 if lfi < 0: # end of file
306 306 size = self._lenbuf
307 307 elif 1 < len(self._buffer):
308 308 # we need to take previous chunks into account
309 309 size += self._lenbuf - len(self._buffer[-1])
310 310 return self._frombuffer(size)
311 311
312 312 def _frombuffer(self, size):
313 313 """return at most 'size' data from the buffer
314 314
315 315 The data are removed from the buffer."""
316 316 if size == 0 or not self._buffer:
317 317 return ''
318 318 buf = self._buffer[0]
319 319 if 1 < len(self._buffer):
320 320 buf = ''.join(self._buffer)
321 321
322 322 data = buf[:size]
323 323 buf = buf[len(data):]
324 324 if buf:
325 325 self._buffer = [buf]
326 326 self._lenbuf = len(buf)
327 327 else:
328 328 self._buffer = []
329 329 self._lenbuf = 0
330 330 return data
331 331
332 332 def _fillbuffer(self):
333 333 """read data to the buffer"""
334 334 data = os.read(self._input.fileno(), _chunksize)
335 335 if not data:
336 336 self._eof = True
337 337 else:
338 338 self._lenbuf += len(data)
339 339 self._buffer.append(data)
340 340
341 341 def popen2(cmd, env=None, newlines=False):
342 342 # Setting bufsize to -1 lets the system decide the buffer size.
343 343 # The default for bufsize is 0, meaning unbuffered. This leads to
344 344 # poor performance on Mac OS X: http://bugs.python.org/issue4194
345 345 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
346 346 close_fds=closefds,
347 347 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
348 348 universal_newlines=newlines,
349 349 env=env)
350 350 return p.stdin, p.stdout
351 351
352 352 def popen3(cmd, env=None, newlines=False):
353 353 stdin, stdout, stderr, p = popen4(cmd, env, newlines)
354 354 return stdin, stdout, stderr
355 355
356 356 def popen4(cmd, env=None, newlines=False, bufsize=-1):
357 357 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
358 358 close_fds=closefds,
359 359 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
360 360 stderr=subprocess.PIPE,
361 361 universal_newlines=newlines,
362 362 env=env)
363 363 return p.stdin, p.stdout, p.stderr, p
364 364
365 365 def version():
366 366 """Return version information if available."""
367 367 try:
368 368 from . import __version__
369 369 return __version__.version
370 370 except ImportError:
371 371 return 'unknown'
372 372
373 373 def versiontuple(v=None, n=4):
374 374 """Parses a Mercurial version string into an N-tuple.
375 375
376 376 The version string to be parsed is specified with the ``v`` argument.
377 377 If it isn't defined, the current Mercurial version string will be parsed.
378 378
379 379 ``n`` can be 2, 3, or 4. Here is how some version strings map to
380 380 returned values:
381 381
382 382 >>> v = '3.6.1+190-df9b73d2d444'
383 383 >>> versiontuple(v, 2)
384 384 (3, 6)
385 385 >>> versiontuple(v, 3)
386 386 (3, 6, 1)
387 387 >>> versiontuple(v, 4)
388 388 (3, 6, 1, '190-df9b73d2d444')
389 389
390 390 >>> versiontuple('3.6.1+190-df9b73d2d444+20151118')
391 391 (3, 6, 1, '190-df9b73d2d444+20151118')
392 392
393 393 >>> v = '3.6'
394 394 >>> versiontuple(v, 2)
395 395 (3, 6)
396 396 >>> versiontuple(v, 3)
397 397 (3, 6, None)
398 398 >>> versiontuple(v, 4)
399 399 (3, 6, None, None)
400 400
401 401 >>> v = '3.9-rc'
402 402 >>> versiontuple(v, 2)
403 403 (3, 9)
404 404 >>> versiontuple(v, 3)
405 405 (3, 9, None)
406 406 >>> versiontuple(v, 4)
407 407 (3, 9, None, 'rc')
408 408
409 409 >>> v = '3.9-rc+2-02a8fea4289b'
410 410 >>> versiontuple(v, 2)
411 411 (3, 9)
412 412 >>> versiontuple(v, 3)
413 413 (3, 9, None)
414 414 >>> versiontuple(v, 4)
415 415 (3, 9, None, 'rc+2-02a8fea4289b')
416 416 """
417 417 if not v:
418 418 v = version()
419 419 parts = remod.split('[\+-]', v, 1)
420 420 if len(parts) == 1:
421 421 vparts, extra = parts[0], None
422 422 else:
423 423 vparts, extra = parts
424 424
425 425 vints = []
426 426 for i in vparts.split('.'):
427 427 try:
428 428 vints.append(int(i))
429 429 except ValueError:
430 430 break
431 431 # (3, 6) -> (3, 6, None)
432 432 while len(vints) < 3:
433 433 vints.append(None)
434 434
435 435 if n == 2:
436 436 return (vints[0], vints[1])
437 437 if n == 3:
438 438 return (vints[0], vints[1], vints[2])
439 439 if n == 4:
440 440 return (vints[0], vints[1], vints[2], extra)
441 441
442 442 # used by parsedate
443 443 defaultdateformats = (
444 444 '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601
445 445 '%Y-%m-%dT%H:%M', # without seconds
446 446 '%Y-%m-%dT%H%M%S', # another awful but legal variant without :
447 447 '%Y-%m-%dT%H%M', # without seconds
448 448 '%Y-%m-%d %H:%M:%S', # our common legal variant
449 449 '%Y-%m-%d %H:%M', # without seconds
450 450 '%Y-%m-%d %H%M%S', # without :
451 451 '%Y-%m-%d %H%M', # without seconds
452 452 '%Y-%m-%d %I:%M:%S%p',
453 453 '%Y-%m-%d %H:%M',
454 454 '%Y-%m-%d %I:%M%p',
455 455 '%Y-%m-%d',
456 456 '%m-%d',
457 457 '%m/%d',
458 458 '%m/%d/%y',
459 459 '%m/%d/%Y',
460 460 '%a %b %d %H:%M:%S %Y',
461 461 '%a %b %d %I:%M:%S%p %Y',
462 462 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
463 463 '%b %d %H:%M:%S %Y',
464 464 '%b %d %I:%M:%S%p %Y',
465 465 '%b %d %H:%M:%S',
466 466 '%b %d %I:%M:%S%p',
467 467 '%b %d %H:%M',
468 468 '%b %d %I:%M%p',
469 469 '%b %d %Y',
470 470 '%b %d',
471 471 '%H:%M:%S',
472 472 '%I:%M:%S%p',
473 473 '%H:%M',
474 474 '%I:%M%p',
475 475 )
476 476
477 477 extendeddateformats = defaultdateformats + (
478 478 "%Y",
479 479 "%Y-%m",
480 480 "%b",
481 481 "%b %Y",
482 482 )
483 483
484 484 def cachefunc(func):
485 485 '''cache the result of function calls'''
486 486 # XXX doesn't handle keywords args
487 487 if func.__code__.co_argcount == 0:
488 488 cache = []
489 489 def f():
490 490 if len(cache) == 0:
491 491 cache.append(func())
492 492 return cache[0]
493 493 return f
494 494 cache = {}
495 495 if func.__code__.co_argcount == 1:
496 496 # we gain a small amount of time because
497 497 # we don't need to pack/unpack the list
498 498 def f(arg):
499 499 if arg not in cache:
500 500 cache[arg] = func(arg)
501 501 return cache[arg]
502 502 else:
503 503 def f(*args):
504 504 if args not in cache:
505 505 cache[args] = func(*args)
506 506 return cache[args]
507 507
508 508 return f
509 509
510 510 class sortdict(dict):
511 511 '''a simple sorted dictionary'''
512 512 def __init__(self, data=None):
513 513 self._list = []
514 514 if data:
515 515 self.update(data)
516 516 def copy(self):
517 517 return sortdict(self)
518 518 def __setitem__(self, key, val):
519 519 if key in self:
520 520 self._list.remove(key)
521 521 self._list.append(key)
522 522 dict.__setitem__(self, key, val)
523 523 def __iter__(self):
524 524 return self._list.__iter__()
525 525 def update(self, src):
526 526 if isinstance(src, dict):
527 527 src = src.iteritems()
528 528 for k, v in src:
529 529 self[k] = v
530 530 def clear(self):
531 531 dict.clear(self)
532 532 self._list = []
533 533 def items(self):
534 534 return [(k, self[k]) for k in self._list]
535 535 def __delitem__(self, key):
536 536 dict.__delitem__(self, key)
537 537 self._list.remove(key)
538 538 def pop(self, key, *args, **kwargs):
539 539 dict.pop(self, key, *args, **kwargs)
540 540 try:
541 541 self._list.remove(key)
542 542 except ValueError:
543 543 pass
544 544 def keys(self):
545 545 return self._list
546 546 def iterkeys(self):
547 547 return self._list.__iter__()
548 548 def iteritems(self):
549 549 for k in self._list:
550 550 yield k, self[k]
551 551 def insert(self, index, key, val):
552 552 self._list.insert(index, key)
553 553 dict.__setitem__(self, key, val)
554 554 def __repr__(self):
555 555 if not self:
556 556 return '%s()' % self.__class__.__name__
557 557 return '%s(%r)' % (self.__class__.__name__, self.items())
558 558
559 559 class _lrucachenode(object):
560 560 """A node in a doubly linked list.
561 561
562 562 Holds a reference to nodes on either side as well as a key-value
563 563 pair for the dictionary entry.
564 564 """
565 565 __slots__ = (u'next', u'prev', u'key', u'value')
566 566
567 567 def __init__(self):
568 568 self.next = None
569 569 self.prev = None
570 570
571 571 self.key = _notset
572 572 self.value = None
573 573
574 574 def markempty(self):
575 575 """Mark the node as emptied."""
576 576 self.key = _notset
577 577
578 578 class lrucachedict(object):
579 579 """Dict that caches most recent accesses and sets.
580 580
581 581 The dict consists of an actual backing dict - indexed by original
582 582 key - and a doubly linked circular list defining the order of entries in
583 583 the cache.
584 584
585 585 The head node is the newest entry in the cache. If the cache is full,
586 586 we recycle head.prev and make it the new head. Cache accesses result in
587 587 the node being moved to before the existing head and being marked as the
588 588 new head node.
589 589 """
590 590 def __init__(self, max):
591 591 self._cache = {}
592 592
593 593 self._head = head = _lrucachenode()
594 594 head.prev = head
595 595 head.next = head
596 596 self._size = 1
597 597 self._capacity = max
598 598
599 599 def __len__(self):
600 600 return len(self._cache)
601 601
602 602 def __contains__(self, k):
603 603 return k in self._cache
604 604
605 605 def __iter__(self):
606 606 # We don't have to iterate in cache order, but why not.
607 607 n = self._head
608 608 for i in range(len(self._cache)):
609 609 yield n.key
610 610 n = n.next
611 611
612 612 def __getitem__(self, k):
613 613 node = self._cache[k]
614 614 self._movetohead(node)
615 615 return node.value
616 616
617 617 def __setitem__(self, k, v):
618 618 node = self._cache.get(k)
619 619 # Replace existing value and mark as newest.
620 620 if node is not None:
621 621 node.value = v
622 622 self._movetohead(node)
623 623 return
624 624
625 625 if self._size < self._capacity:
626 626 node = self._addcapacity()
627 627 else:
628 628 # Grab the last/oldest item.
629 629 node = self._head.prev
630 630
631 631 # At capacity. Kill the old entry.
632 632 if node.key is not _notset:
633 633 del self._cache[node.key]
634 634
635 635 node.key = k
636 636 node.value = v
637 637 self._cache[k] = node
638 638 # And mark it as newest entry. No need to adjust order since it
639 639 # is already self._head.prev.
640 640 self._head = node
641 641
642 642 def __delitem__(self, k):
643 643 node = self._cache.pop(k)
644 644 node.markempty()
645 645
646 646 # Temporarily mark as newest item before re-adjusting head to make
647 647 # this node the oldest item.
648 648 self._movetohead(node)
649 649 self._head = node.next
650 650
651 651 # Additional dict methods.
652 652
653 653 def get(self, k, default=None):
654 654 try:
655 655 return self._cache[k].value
656 656 except KeyError:
657 657 return default
658 658
659 659 def clear(self):
660 660 n = self._head
661 661 while n.key is not _notset:
662 662 n.markempty()
663 663 n = n.next
664 664
665 665 self._cache.clear()
666 666
667 667 def copy(self):
668 668 result = lrucachedict(self._capacity)
669 669 n = self._head.prev
670 670 # Iterate in oldest-to-newest order, so the copy has the right ordering
671 671 for i in range(len(self._cache)):
672 672 result[n.key] = n.value
673 673 n = n.prev
674 674 return result
675 675
676 676 def _movetohead(self, node):
677 677 """Mark a node as the newest, making it the new head.
678 678
679 679 When a node is accessed, it becomes the freshest entry in the LRU
680 680 list, which is denoted by self._head.
681 681
682 682 Visually, let's make ``N`` the new head node (* denotes head):
683 683
684 684 previous/oldest <-> head <-> next/next newest
685 685
686 686 ----<->--- A* ---<->-----
687 687 | |
688 688 E <-> D <-> N <-> C <-> B
689 689
690 690 To:
691 691
692 692 ----<->--- N* ---<->-----
693 693 | |
694 694 E <-> D <-> C <-> B <-> A
695 695
696 696 This requires the following moves:
697 697
698 698 C.next = D (node.prev.next = node.next)
699 699 D.prev = C (node.next.prev = node.prev)
700 700 E.next = N (head.prev.next = node)
701 701 N.prev = E (node.prev = head.prev)
702 702 N.next = A (node.next = head)
703 703 A.prev = N (head.prev = node)
704 704 """
705 705 head = self._head
706 706 # C.next = D
707 707 node.prev.next = node.next
708 708 # D.prev = C
709 709 node.next.prev = node.prev
710 710 # N.prev = E
711 711 node.prev = head.prev
712 712 # N.next = A
713 713 # It is tempting to do just "head" here, however if node is
714 714 # adjacent to head, this will do bad things.
715 715 node.next = head.prev.next
716 716 # E.next = N
717 717 node.next.prev = node
718 718 # A.prev = N
719 719 node.prev.next = node
720 720
721 721 self._head = node
722 722
723 723 def _addcapacity(self):
724 724 """Add a node to the circular linked list.
725 725
726 726 The new node is inserted before the head node.
727 727 """
728 728 head = self._head
729 729 node = _lrucachenode()
730 730 head.prev.next = node
731 731 node.prev = head.prev
732 732 node.next = head
733 733 head.prev = node
734 734 self._size += 1
735 735 return node
736 736
737 737 def lrucachefunc(func):
738 738 '''cache most recent results of function calls'''
739 739 cache = {}
740 740 order = collections.deque()
741 741 if func.__code__.co_argcount == 1:
742 742 def f(arg):
743 743 if arg not in cache:
744 744 if len(cache) > 20:
745 745 del cache[order.popleft()]
746 746 cache[arg] = func(arg)
747 747 else:
748 748 order.remove(arg)
749 749 order.append(arg)
750 750 return cache[arg]
751 751 else:
752 752 def f(*args):
753 753 if args not in cache:
754 754 if len(cache) > 20:
755 755 del cache[order.popleft()]
756 756 cache[args] = func(*args)
757 757 else:
758 758 order.remove(args)
759 759 order.append(args)
760 760 return cache[args]
761 761
762 762 return f
763 763
764 764 class propertycache(object):
765 765 def __init__(self, func):
766 766 self.func = func
767 767 self.name = func.__name__
768 768 def __get__(self, obj, type=None):
769 769 result = self.func(obj)
770 770 self.cachevalue(obj, result)
771 771 return result
772 772
773 773 def cachevalue(self, obj, value):
774 774 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
775 775 obj.__dict__[self.name] = value
776 776
777 777 def pipefilter(s, cmd):
778 778 '''filter string S through command CMD, returning its output'''
779 779 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
780 780 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
781 781 pout, perr = p.communicate(s)
782 782 return pout
783 783
784 784 def tempfilter(s, cmd):
785 785 '''filter string S through a pair of temporary files with CMD.
786 786 CMD is used as a template to create the real command to be run,
787 787 with the strings INFILE and OUTFILE replaced by the real names of
788 788 the temporary files generated.'''
789 789 inname, outname = None, None
790 790 try:
791 791 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
792 792 fp = os.fdopen(infd, 'wb')
793 793 fp.write(s)
794 794 fp.close()
795 795 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
796 796 os.close(outfd)
797 797 cmd = cmd.replace('INFILE', inname)
798 798 cmd = cmd.replace('OUTFILE', outname)
799 799 code = os.system(cmd)
800 800 if sys.platform == 'OpenVMS' and code & 1:
801 801 code = 0
802 802 if code:
803 803 raise Abort(_("command '%s' failed: %s") %
804 804 (cmd, explainexit(code)))
805 805 return readfile(outname)
806 806 finally:
807 807 try:
808 808 if inname:
809 809 os.unlink(inname)
810 810 except OSError:
811 811 pass
812 812 try:
813 813 if outname:
814 814 os.unlink(outname)
815 815 except OSError:
816 816 pass
817 817
818 818 filtertable = {
819 819 'tempfile:': tempfilter,
820 820 'pipe:': pipefilter,
821 821 }
822 822
823 823 def filter(s, cmd):
824 824 "filter a string through a command that transforms its input to its output"
825 825 for name, fn in filtertable.iteritems():
826 826 if cmd.startswith(name):
827 827 return fn(s, cmd[len(name):].lstrip())
828 828 return pipefilter(s, cmd)
829 829
830 830 def binary(s):
831 831 """return true if a string is binary data"""
832 832 return bool(s and '\0' in s)
833 833
834 834 def increasingchunks(source, min=1024, max=65536):
835 835 '''return no less than min bytes per chunk while data remains,
836 836 doubling min after each chunk until it reaches max'''
837 837 def log2(x):
838 838 if not x:
839 839 return 0
840 840 i = 0
841 841 while x:
842 842 x >>= 1
843 843 i += 1
844 844 return i - 1
845 845
846 846 buf = []
847 847 blen = 0
848 848 for chunk in source:
849 849 buf.append(chunk)
850 850 blen += len(chunk)
851 851 if blen >= min:
852 852 if min < max:
853 853 min = min << 1
854 854 nmin = 1 << log2(blen)
855 855 if nmin > min:
856 856 min = nmin
857 857 if min > max:
858 858 min = max
859 859 yield ''.join(buf)
860 860 blen = 0
861 861 buf = []
862 862 if buf:
863 863 yield ''.join(buf)
864 864
865 865 Abort = error.Abort
866 866
867 867 def always(fn):
868 868 return True
869 869
870 870 def never(fn):
871 871 return False
872 872
873 873 def nogc(func):
874 874 """disable garbage collector
875 875
876 876 Python's garbage collector triggers a GC each time a certain number of
877 877 container objects (the number being defined by gc.get_threshold()) are
878 878 allocated even when marked not to be tracked by the collector. Tracking has
879 879 no effect on when GCs are triggered, only on what objects the GC looks
880 880 into. As a workaround, disable GC while building complex (huge)
881 881 containers.
882 882
883 883 This garbage collector issue have been fixed in 2.7.
884 884 """
885 885 if sys.version_info >= (2, 7):
886 886 return func
887 887 def wrapper(*args, **kwargs):
888 888 gcenabled = gc.isenabled()
889 889 gc.disable()
890 890 try:
891 891 return func(*args, **kwargs)
892 892 finally:
893 893 if gcenabled:
894 894 gc.enable()
895 895 return wrapper
896 896
897 897 def pathto(root, n1, n2):
898 898 '''return the relative path from one place to another.
899 899 root should use os.sep to separate directories
900 900 n1 should use os.sep to separate directories
901 901 n2 should use "/" to separate directories
902 902 returns an os.sep-separated path.
903 903
904 904 If n1 is a relative path, it's assumed it's
905 905 relative to root.
906 906 n2 should always be relative to root.
907 907 '''
908 908 if not n1:
909 909 return localpath(n2)
910 910 if os.path.isabs(n1):
911 911 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
912 912 return os.path.join(root, localpath(n2))
913 913 n2 = '/'.join((pconvert(root), n2))
914 914 a, b = splitpath(n1), n2.split('/')
915 915 a.reverse()
916 916 b.reverse()
917 917 while a and b and a[-1] == b[-1]:
918 918 a.pop()
919 919 b.pop()
920 920 b.reverse()
921 921 return os.sep.join((['..'] * len(a)) + b) or '.'
922 922
923 923 def mainfrozen():
924 924 """return True if we are a frozen executable.
925 925
926 926 The code supports py2exe (most common, Windows only) and tools/freeze
927 927 (portable, not much used).
928 928 """
929 929 return (safehasattr(sys, "frozen") or # new py2exe
930 930 safehasattr(sys, "importers") or # old py2exe
931 931 imp.is_frozen(u"__main__")) # tools/freeze
932 932
933 933 # the location of data files matching the source code
934 934 if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
935 935 # executable version (py2exe) doesn't support __file__
936 936 datapath = os.path.dirname(sys.executable)
937 937 else:
938 938 datapath = os.path.dirname(__file__)
939 939
940 940 if not isinstance(datapath, bytes):
941 941 datapath = pycompat.fsencode(datapath)
942 942
943 943 i18n.setdatapath(datapath)
944 944
945 945 _hgexecutable = None
946 946
947 947 def hgexecutable():
948 948 """return location of the 'hg' executable.
949 949
950 950 Defaults to $HG or 'hg' in the search path.
951 951 """
952 952 if _hgexecutable is None:
953 953 hg = os.environ.get('HG')
954 954 mainmod = sys.modules['__main__']
955 955 if hg:
956 956 _sethgexecutable(hg)
957 957 elif mainfrozen():
958 958 if getattr(sys, 'frozen', None) == 'macosx_app':
959 959 # Env variable set by py2app
960 960 _sethgexecutable(os.environ['EXECUTABLEPATH'])
961 961 else:
962 962 _sethgexecutable(sys.executable)
963 963 elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
964 964 _sethgexecutable(mainmod.__file__)
965 965 else:
966 966 exe = findexe('hg') or os.path.basename(sys.argv[0])
967 967 _sethgexecutable(exe)
968 968 return _hgexecutable
969 969
970 970 def _sethgexecutable(path):
971 971 """set location of the 'hg' executable"""
972 972 global _hgexecutable
973 973 _hgexecutable = path
974 974
975 975 def _isstdout(f):
976 976 fileno = getattr(f, 'fileno', None)
977 977 return fileno and fileno() == sys.__stdout__.fileno()
978 978
979 979 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
980 980 '''enhanced shell command execution.
981 981 run with environment maybe modified, maybe in different dir.
982 982
983 983 if command fails and onerr is None, return status, else raise onerr
984 984 object as exception.
985 985
986 986 if out is specified, it is assumed to be a file-like object that has a
987 987 write() method. stdout and stderr will be redirected to out.'''
988 988 if environ is None:
989 989 environ = {}
990 990 try:
991 991 sys.stdout.flush()
992 992 except Exception:
993 993 pass
994 994 def py2shell(val):
995 995 'convert python object into string that is useful to shell'
996 996 if val is None or val is False:
997 997 return '0'
998 998 if val is True:
999 999 return '1'
1000 1000 return str(val)
1001 1001 origcmd = cmd
1002 1002 cmd = quotecommand(cmd)
1003 1003 if sys.platform == 'plan9' and (sys.version_info[0] == 2
1004 1004 and sys.version_info[1] < 7):
1005 1005 # subprocess kludge to work around issues in half-baked Python
1006 1006 # ports, notably bichued/python:
1007 1007 if not cwd is None:
1008 1008 os.chdir(cwd)
1009 1009 rc = os.system(cmd)
1010 1010 else:
1011 1011 env = dict(os.environ)
1012 1012 env.update((k, py2shell(v)) for k, v in environ.iteritems())
1013 1013 env['HG'] = hgexecutable()
1014 1014 if out is None or _isstdout(out):
1015 1015 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
1016 1016 env=env, cwd=cwd)
1017 1017 else:
1018 1018 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
1019 1019 env=env, cwd=cwd, stdout=subprocess.PIPE,
1020 1020 stderr=subprocess.STDOUT)
1021 1021 for line in iter(proc.stdout.readline, ''):
1022 1022 out.write(line)
1023 1023 proc.wait()
1024 1024 rc = proc.returncode
1025 1025 if sys.platform == 'OpenVMS' and rc & 1:
1026 1026 rc = 0
1027 1027 if rc and onerr:
1028 1028 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
1029 1029 explainexit(rc)[0])
1030 1030 if errprefix:
1031 1031 errmsg = '%s: %s' % (errprefix, errmsg)
1032 1032 raise onerr(errmsg)
1033 1033 return rc
1034 1034
1035 1035 def checksignature(func):
1036 1036 '''wrap a function with code to check for calling errors'''
1037 1037 def check(*args, **kwargs):
1038 1038 try:
1039 1039 return func(*args, **kwargs)
1040 1040 except TypeError:
1041 1041 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1042 1042 raise error.SignatureError
1043 1043 raise
1044 1044
1045 1045 return check
1046 1046
1047 1047 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1048 1048 '''copy a file, preserving mode and optionally other stat info like
1049 1049 atime/mtime
1050 1050
1051 1051 checkambig argument is used with filestat, and is useful only if
1052 1052 destination file is guarded by any lock (e.g. repo.lock or
1053 1053 repo.wlock).
1054 1054
1055 1055 copystat and checkambig should be exclusive.
1056 1056 '''
1057 1057 assert not (copystat and checkambig)
1058 1058 oldstat = None
1059 1059 if os.path.lexists(dest):
1060 1060 if checkambig:
1061 1061 oldstat = checkambig and filestat(dest)
1062 1062 unlink(dest)
1063 1063 # hardlinks are problematic on CIFS, quietly ignore this flag
1064 1064 # until we find a way to work around it cleanly (issue4546)
1065 1065 if False and hardlink:
1066 1066 try:
1067 1067 oslink(src, dest)
1068 1068 return
1069 1069 except (IOError, OSError):
1070 1070 pass # fall back to normal copy
1071 1071 if os.path.islink(src):
1072 1072 os.symlink(os.readlink(src), dest)
1073 1073 # copytime is ignored for symlinks, but in general copytime isn't needed
1074 1074 # for them anyway
1075 1075 else:
1076 1076 try:
1077 1077 shutil.copyfile(src, dest)
1078 1078 if copystat:
1079 1079 # copystat also copies mode
1080 1080 shutil.copystat(src, dest)
1081 1081 else:
1082 1082 shutil.copymode(src, dest)
1083 1083 if oldstat and oldstat.stat:
1084 1084 newstat = filestat(dest)
1085 1085 if newstat.isambig(oldstat):
1086 1086 # stat of copied file is ambiguous to original one
1087 1087 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1088 1088 os.utime(dest, (advanced, advanced))
1089 1089 except shutil.Error as inst:
1090 1090 raise Abort(str(inst))
1091 1091
1092 1092 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
1093 1093 """Copy a directory tree using hardlinks if possible."""
1094 1094 num = 0
1095 1095
1096 1096 if hardlink is None:
1097 1097 hardlink = (os.stat(src).st_dev ==
1098 1098 os.stat(os.path.dirname(dst)).st_dev)
1099 1099 if hardlink:
1100 1100 topic = _('linking')
1101 1101 else:
1102 1102 topic = _('copying')
1103 1103
1104 1104 if os.path.isdir(src):
1105 1105 os.mkdir(dst)
1106 1106 for name, kind in osutil.listdir(src):
1107 1107 srcname = os.path.join(src, name)
1108 1108 dstname = os.path.join(dst, name)
1109 1109 def nprog(t, pos):
1110 1110 if pos is not None:
1111 1111 return progress(t, pos + num)
1112 1112 hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
1113 1113 num += n
1114 1114 else:
1115 1115 if hardlink:
1116 1116 try:
1117 1117 oslink(src, dst)
1118 1118 except (IOError, OSError):
1119 1119 hardlink = False
1120 1120 shutil.copy(src, dst)
1121 1121 else:
1122 1122 shutil.copy(src, dst)
1123 1123 num += 1
1124 1124 progress(topic, num)
1125 1125 progress(topic, None)
1126 1126
1127 1127 return hardlink, num
1128 1128
1129 1129 _winreservednames = '''con prn aux nul
1130 1130 com1 com2 com3 com4 com5 com6 com7 com8 com9
1131 1131 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split()
1132 1132 _winreservedchars = ':*?"<>|'
1133 1133 def checkwinfilename(path):
1134 1134 r'''Check that the base-relative path is a valid filename on Windows.
1135 1135 Returns None if the path is ok, or a UI string describing the problem.
1136 1136
1137 1137 >>> checkwinfilename("just/a/normal/path")
1138 1138 >>> checkwinfilename("foo/bar/con.xml")
1139 1139 "filename contains 'con', which is reserved on Windows"
1140 1140 >>> checkwinfilename("foo/con.xml/bar")
1141 1141 "filename contains 'con', which is reserved on Windows"
1142 1142 >>> checkwinfilename("foo/bar/xml.con")
1143 1143 >>> checkwinfilename("foo/bar/AUX/bla.txt")
1144 1144 "filename contains 'AUX', which is reserved on Windows"
1145 1145 >>> checkwinfilename("foo/bar/bla:.txt")
1146 1146 "filename contains ':', which is reserved on Windows"
1147 1147 >>> checkwinfilename("foo/bar/b\07la.txt")
1148 1148 "filename contains '\\x07', which is invalid on Windows"
1149 1149 >>> checkwinfilename("foo/bar/bla ")
1150 1150 "filename ends with ' ', which is not allowed on Windows"
1151 1151 >>> checkwinfilename("../bar")
1152 1152 >>> checkwinfilename("foo\\")
1153 1153 "filename ends with '\\', which is invalid on Windows"
1154 1154 >>> checkwinfilename("foo\\/bar")
1155 1155 "directory name ends with '\\', which is invalid on Windows"
1156 1156 '''
1157 1157 if path.endswith('\\'):
1158 1158 return _("filename ends with '\\', which is invalid on Windows")
1159 1159 if '\\/' in path:
1160 1160 return _("directory name ends with '\\', which is invalid on Windows")
1161 1161 for n in path.replace('\\', '/').split('/'):
1162 1162 if not n:
1163 1163 continue
1164 1164 for c in n:
1165 1165 if c in _winreservedchars:
1166 1166 return _("filename contains '%s', which is reserved "
1167 1167 "on Windows") % c
1168 1168 if ord(c) <= 31:
1169 1169 return _("filename contains %r, which is invalid "
1170 1170 "on Windows") % c
1171 1171 base = n.split('.')[0]
1172 1172 if base and base.lower() in _winreservednames:
1173 1173 return _("filename contains '%s', which is reserved "
1174 1174 "on Windows") % base
1175 1175 t = n[-1]
1176 1176 if t in '. ' and n not in '..':
1177 1177 return _("filename ends with '%s', which is not allowed "
1178 1178 "on Windows") % t
1179 1179
1180 1180 if os.name == 'nt':
1181 1181 checkosfilename = checkwinfilename
1182 1182 else:
1183 1183 checkosfilename = platform.checkosfilename
1184 1184
1185 1185 def makelock(info, pathname):
1186 1186 try:
1187 1187 return os.symlink(info, pathname)
1188 1188 except OSError as why:
1189 1189 if why.errno == errno.EEXIST:
1190 1190 raise
1191 1191 except AttributeError: # no symlink in os
1192 1192 pass
1193 1193
1194 1194 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
1195 1195 os.write(ld, info)
1196 1196 os.close(ld)
1197 1197
1198 1198 def readlock(pathname):
1199 1199 try:
1200 1200 return os.readlink(pathname)
1201 1201 except OSError as why:
1202 1202 if why.errno not in (errno.EINVAL, errno.ENOSYS):
1203 1203 raise
1204 1204 except AttributeError: # no symlink in os
1205 1205 pass
1206 1206 fp = posixfile(pathname)
1207 1207 r = fp.read()
1208 1208 fp.close()
1209 1209 return r
1210 1210
1211 1211 def fstat(fp):
1212 1212 '''stat file object that may not have fileno method.'''
1213 1213 try:
1214 1214 return os.fstat(fp.fileno())
1215 1215 except AttributeError:
1216 1216 return os.stat(fp.name)
1217 1217
1218 1218 # File system features
1219 1219
1220 1220 def fscasesensitive(path):
1221 1221 """
1222 1222 Return true if the given path is on a case-sensitive filesystem
1223 1223
1224 1224 Requires a path (like /foo/.hg) ending with a foldable final
1225 1225 directory component.
1226 1226 """
1227 1227 s1 = os.lstat(path)
1228 1228 d, b = os.path.split(path)
1229 1229 b2 = b.upper()
1230 1230 if b == b2:
1231 1231 b2 = b.lower()
1232 1232 if b == b2:
1233 1233 return True # no evidence against case sensitivity
1234 1234 p2 = os.path.join(d, b2)
1235 1235 try:
1236 1236 s2 = os.lstat(p2)
1237 1237 if s2 == s1:
1238 1238 return False
1239 1239 return True
1240 1240 except OSError:
1241 1241 return True
1242 1242
1243 1243 try:
1244 1244 import re2
1245 1245 _re2 = None
1246 1246 except ImportError:
1247 1247 _re2 = False
1248 1248
1249 1249 class _re(object):
1250 1250 def _checkre2(self):
1251 1251 global _re2
1252 1252 try:
1253 1253 # check if match works, see issue3964
1254 1254 _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
1255 1255 except ImportError:
1256 1256 _re2 = False
1257 1257
1258 1258 def compile(self, pat, flags=0):
1259 1259 '''Compile a regular expression, using re2 if possible
1260 1260
1261 1261 For best performance, use only re2-compatible regexp features. The
1262 1262 only flags from the re module that are re2-compatible are
1263 1263 IGNORECASE and MULTILINE.'''
1264 1264 if _re2 is None:
1265 1265 self._checkre2()
1266 1266 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
1267 1267 if flags & remod.IGNORECASE:
1268 1268 pat = '(?i)' + pat
1269 1269 if flags & remod.MULTILINE:
1270 1270 pat = '(?m)' + pat
1271 1271 try:
1272 1272 return re2.compile(pat)
1273 1273 except re2.error:
1274 1274 pass
1275 1275 return remod.compile(pat, flags)
1276 1276
1277 1277 @propertycache
1278 1278 def escape(self):
1279 1279 '''Return the version of escape corresponding to self.compile.
1280 1280
1281 1281 This is imperfect because whether re2 or re is used for a particular
1282 1282 function depends on the flags, etc, but it's the best we can do.
1283 1283 '''
1284 1284 global _re2
1285 1285 if _re2 is None:
1286 1286 self._checkre2()
1287 1287 if _re2:
1288 1288 return re2.escape
1289 1289 else:
1290 1290 return remod.escape
1291 1291
1292 1292 re = _re()
1293 1293
1294 1294 _fspathcache = {}
1295 1295 def fspath(name, root):
1296 1296 '''Get name in the case stored in the filesystem
1297 1297
1298 1298 The name should be relative to root, and be normcase-ed for efficiency.
1299 1299
1300 1300 Note that this function is unnecessary, and should not be
1301 1301 called, for case-sensitive filesystems (simply because it's expensive).
1302 1302
1303 1303 The root should be normcase-ed, too.
1304 1304 '''
1305 1305 def _makefspathcacheentry(dir):
1306 1306 return dict((normcase(n), n) for n in os.listdir(dir))
1307 1307
1308 1308 seps = os.sep
1309 1309 if os.altsep:
1310 1310 seps = seps + os.altsep
1311 1311 # Protect backslashes. This gets silly very quickly.
1312 1312 seps.replace('\\','\\\\')
1313 1313 pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1314 1314 dir = os.path.normpath(root)
1315 1315 result = []
1316 1316 for part, sep in pattern.findall(name):
1317 1317 if sep:
1318 1318 result.append(sep)
1319 1319 continue
1320 1320
1321 1321 if dir not in _fspathcache:
1322 1322 _fspathcache[dir] = _makefspathcacheentry(dir)
1323 1323 contents = _fspathcache[dir]
1324 1324
1325 1325 found = contents.get(part)
1326 1326 if not found:
1327 1327 # retry "once per directory" per "dirstate.walk" which
1328 1328 # may take place for each patches of "hg qpush", for example
1329 1329 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
1330 1330 found = contents.get(part)
1331 1331
1332 1332 result.append(found or part)
1333 1333 dir = os.path.join(dir, part)
1334 1334
1335 1335 return ''.join(result)
1336 1336
1337 1337 def checknlink(testfile):
1338 1338 '''check whether hardlink count reporting works properly'''
1339 1339
1340 1340 # testfile may be open, so we need a separate file for checking to
1341 1341 # work around issue2543 (or testfile may get lost on Samba shares)
1342 1342 f1 = testfile + ".hgtmp1"
1343 1343 if os.path.lexists(f1):
1344 1344 return False
1345 1345 try:
1346 1346 posixfile(f1, 'w').close()
1347 1347 except IOError:
1348 1348 try:
1349 1349 os.unlink(f1)
1350 1350 except OSError:
1351 1351 pass
1352 1352 return False
1353 1353
1354 1354 f2 = testfile + ".hgtmp2"
1355 1355 fd = None
1356 1356 try:
1357 1357 oslink(f1, f2)
1358 1358 # nlinks() may behave differently for files on Windows shares if
1359 1359 # the file is open.
1360 1360 fd = posixfile(f2)
1361 1361 return nlinks(f2) > 1
1362 1362 except OSError:
1363 1363 return False
1364 1364 finally:
1365 1365 if fd is not None:
1366 1366 fd.close()
1367 1367 for f in (f1, f2):
1368 1368 try:
1369 1369 os.unlink(f)
1370 1370 except OSError:
1371 1371 pass
1372 1372
1373 1373 def endswithsep(path):
1374 1374 '''Check path ends with os.sep or os.altsep.'''
1375 1375 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
1376 1376
1377 1377 def splitpath(path):
1378 1378 '''Split path by os.sep.
1379 1379 Note that this function does not use os.altsep because this is
1380 1380 an alternative of simple "xxx.split(os.sep)".
1381 1381 It is recommended to use os.path.normpath() before using this
1382 1382 function if need.'''
1383 1383 return path.split(os.sep)
1384 1384
1385 1385 def gui():
1386 1386 '''Are we running in a GUI?'''
1387 1387 if sys.platform == 'darwin':
1388 1388 if 'SSH_CONNECTION' in os.environ:
1389 1389 # handle SSH access to a box where the user is logged in
1390 1390 return False
1391 1391 elif getattr(osutil, 'isgui', None):
1392 1392 # check if a CoreGraphics session is available
1393 1393 return osutil.isgui()
1394 1394 else:
1395 1395 # pure build; use a safe default
1396 1396 return True
1397 1397 else:
1398 1398 return os.name == "nt" or os.environ.get("DISPLAY")
1399 1399
1400 1400 def mktempcopy(name, emptyok=False, createmode=None):
1401 1401 """Create a temporary file with the same contents from name
1402 1402
1403 1403 The permission bits are copied from the original file.
1404 1404
1405 1405 If the temporary file is going to be truncated immediately, you
1406 1406 can use emptyok=True as an optimization.
1407 1407
1408 1408 Returns the name of the temporary file.
1409 1409 """
1410 1410 d, fn = os.path.split(name)
1411 1411 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1412 1412 os.close(fd)
1413 1413 # Temporary files are created with mode 0600, which is usually not
1414 1414 # what we want. If the original file already exists, just copy
1415 1415 # its mode. Otherwise, manually obey umask.
1416 1416 copymode(name, temp, createmode)
1417 1417 if emptyok:
1418 1418 return temp
1419 1419 try:
1420 1420 try:
1421 1421 ifp = posixfile(name, "rb")
1422 1422 except IOError as inst:
1423 1423 if inst.errno == errno.ENOENT:
1424 1424 return temp
1425 1425 if not getattr(inst, 'filename', None):
1426 1426 inst.filename = name
1427 1427 raise
1428 1428 ofp = posixfile(temp, "wb")
1429 1429 for chunk in filechunkiter(ifp):
1430 1430 ofp.write(chunk)
1431 1431 ifp.close()
1432 1432 ofp.close()
1433 1433 except: # re-raises
1434 1434 try: os.unlink(temp)
1435 1435 except OSError: pass
1436 1436 raise
1437 1437 return temp
1438 1438
1439 1439 class filestat(object):
1440 1440 """help to exactly detect change of a file
1441 1441
1442 1442 'stat' attribute is result of 'os.stat()' if specified 'path'
1443 1443 exists. Otherwise, it is None. This can avoid preparative
1444 1444 'exists()' examination on client side of this class.
1445 1445 """
1446 1446 def __init__(self, path):
1447 1447 try:
1448 1448 self.stat = os.stat(path)
1449 1449 except OSError as err:
1450 1450 if err.errno != errno.ENOENT:
1451 1451 raise
1452 1452 self.stat = None
1453 1453
1454 1454 __hash__ = object.__hash__
1455 1455
1456 1456 def __eq__(self, old):
1457 1457 try:
1458 1458 # if ambiguity between stat of new and old file is
1459 1459 # avoided, comparison of size, ctime and mtime is enough
1460 1460 # to exactly detect change of a file regardless of platform
1461 1461 return (self.stat.st_size == old.stat.st_size and
1462 1462 self.stat.st_ctime == old.stat.st_ctime and
1463 1463 self.stat.st_mtime == old.stat.st_mtime)
1464 1464 except AttributeError:
1465 1465 return False
1466 1466
1467 1467 def isambig(self, old):
1468 1468 """Examine whether new (= self) stat is ambiguous against old one
1469 1469
1470 1470 "S[N]" below means stat of a file at N-th change:
1471 1471
1472 1472 - S[n-1].ctime < S[n].ctime: can detect change of a file
1473 1473 - S[n-1].ctime == S[n].ctime
1474 1474 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
1475 1475 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
1476 1476 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
1477 1477 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
1478 1478
1479 1479 Case (*2) above means that a file was changed twice or more at
1480 1480 same time in sec (= S[n-1].ctime), and comparison of timestamp
1481 1481 is ambiguous.
1482 1482
1483 1483 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
1484 1484 timestamp is ambiguous".
1485 1485
1486 1486 But advancing mtime only in case (*2) doesn't work as
1487 1487 expected, because naturally advanced S[n].mtime in case (*1)
1488 1488 might be equal to manually advanced S[n-1 or earlier].mtime.
1489 1489
1490 1490 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
1491 1491 treated as ambiguous regardless of mtime, to avoid overlooking
1492 1492 by confliction between such mtime.
1493 1493
1494 1494 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
1495 1495 S[n].mtime", even if size of a file isn't changed.
1496 1496 """
1497 1497 try:
1498 1498 return (self.stat.st_ctime == old.stat.st_ctime)
1499 1499 except AttributeError:
1500 1500 return False
1501 1501
1502 def avoidambig(self, path, old):
1503 """Change file stat of specified path to avoid ambiguity
1504
1505 'old' should be previous filestat of 'path'.
1506
1507 This skips avoiding ambiguity, if a process doesn't have
1508 appropriate privileges for 'path'.
1509 """
1510 advanced = (old.stat.st_mtime + 1) & 0x7fffffff
1511 try:
1512 os.utime(path, (advanced, advanced))
1513 except OSError as inst:
1514 if inst.errno == errno.EPERM:
1515 # utime() on the file created by another user causes EPERM,
1516 # if a process doesn't have appropriate privileges
1517 return
1518 raise
1519
1502 1520 def __ne__(self, other):
1503 1521 return not self == other
1504 1522
1505 1523 class atomictempfile(object):
1506 1524 '''writable file object that atomically updates a file
1507 1525
1508 1526 All writes will go to a temporary copy of the original file. Call
1509 1527 close() when you are done writing, and atomictempfile will rename
1510 1528 the temporary copy to the original name, making the changes
1511 1529 visible. If the object is destroyed without being closed, all your
1512 1530 writes are discarded.
1513 1531
1514 1532 checkambig argument of constructor is used with filestat, and is
1515 1533 useful only if target file is guarded by any lock (e.g. repo.lock
1516 1534 or repo.wlock).
1517 1535 '''
1518 1536 def __init__(self, name, mode='w+b', createmode=None, checkambig=False):
1519 1537 self.__name = name # permanent name
1520 1538 self._tempname = mktempcopy(name, emptyok=('w' in mode),
1521 1539 createmode=createmode)
1522 1540 self._fp = posixfile(self._tempname, mode)
1523 1541 self._checkambig = checkambig
1524 1542
1525 1543 # delegated methods
1526 1544 self.read = self._fp.read
1527 1545 self.write = self._fp.write
1528 1546 self.seek = self._fp.seek
1529 1547 self.tell = self._fp.tell
1530 1548 self.fileno = self._fp.fileno
1531 1549
1532 1550 def close(self):
1533 1551 if not self._fp.closed:
1534 1552 self._fp.close()
1535 1553 filename = localpath(self.__name)
1536 1554 oldstat = self._checkambig and filestat(filename)
1537 1555 if oldstat and oldstat.stat:
1538 1556 rename(self._tempname, filename)
1539 1557 newstat = filestat(filename)
1540 1558 if newstat.isambig(oldstat):
1541 1559 # stat of changed file is ambiguous to original one
1542 1560 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1543 1561 os.utime(filename, (advanced, advanced))
1544 1562 else:
1545 1563 rename(self._tempname, filename)
1546 1564
1547 1565 def discard(self):
1548 1566 if not self._fp.closed:
1549 1567 try:
1550 1568 os.unlink(self._tempname)
1551 1569 except OSError:
1552 1570 pass
1553 1571 self._fp.close()
1554 1572
1555 1573 def __del__(self):
1556 1574 if safehasattr(self, '_fp'): # constructor actually did something
1557 1575 self.discard()
1558 1576
1559 1577 def __enter__(self):
1560 1578 return self
1561 1579
1562 1580 def __exit__(self, exctype, excvalue, traceback):
1563 1581 if exctype is not None:
1564 1582 self.discard()
1565 1583 else:
1566 1584 self.close()
1567 1585
1568 1586 def makedirs(name, mode=None, notindexed=False):
1569 1587 """recursive directory creation with parent mode inheritance
1570 1588
1571 1589 Newly created directories are marked as "not to be indexed by
1572 1590 the content indexing service", if ``notindexed`` is specified
1573 1591 for "write" mode access.
1574 1592 """
1575 1593 try:
1576 1594 makedir(name, notindexed)
1577 1595 except OSError as err:
1578 1596 if err.errno == errno.EEXIST:
1579 1597 return
1580 1598 if err.errno != errno.ENOENT or not name:
1581 1599 raise
1582 1600 parent = os.path.dirname(os.path.abspath(name))
1583 1601 if parent == name:
1584 1602 raise
1585 1603 makedirs(parent, mode, notindexed)
1586 1604 try:
1587 1605 makedir(name, notindexed)
1588 1606 except OSError as err:
1589 1607 # Catch EEXIST to handle races
1590 1608 if err.errno == errno.EEXIST:
1591 1609 return
1592 1610 raise
1593 1611 if mode is not None:
1594 1612 os.chmod(name, mode)
1595 1613
1596 1614 def readfile(path):
1597 1615 with open(path, 'rb') as fp:
1598 1616 return fp.read()
1599 1617
1600 1618 def writefile(path, text):
1601 1619 with open(path, 'wb') as fp:
1602 1620 fp.write(text)
1603 1621
1604 1622 def appendfile(path, text):
1605 1623 with open(path, 'ab') as fp:
1606 1624 fp.write(text)
1607 1625
1608 1626 class chunkbuffer(object):
1609 1627 """Allow arbitrary sized chunks of data to be efficiently read from an
1610 1628 iterator over chunks of arbitrary size."""
1611 1629
1612 1630 def __init__(self, in_iter):
1613 1631 """in_iter is the iterator that's iterating over the input chunks.
1614 1632 targetsize is how big a buffer to try to maintain."""
1615 1633 def splitbig(chunks):
1616 1634 for chunk in chunks:
1617 1635 if len(chunk) > 2**20:
1618 1636 pos = 0
1619 1637 while pos < len(chunk):
1620 1638 end = pos + 2 ** 18
1621 1639 yield chunk[pos:end]
1622 1640 pos = end
1623 1641 else:
1624 1642 yield chunk
1625 1643 self.iter = splitbig(in_iter)
1626 1644 self._queue = collections.deque()
1627 1645 self._chunkoffset = 0
1628 1646
1629 1647 def read(self, l=None):
1630 1648 """Read L bytes of data from the iterator of chunks of data.
1631 1649 Returns less than L bytes if the iterator runs dry.
1632 1650
1633 1651 If size parameter is omitted, read everything"""
1634 1652 if l is None:
1635 1653 return ''.join(self.iter)
1636 1654
1637 1655 left = l
1638 1656 buf = []
1639 1657 queue = self._queue
1640 1658 while left > 0:
1641 1659 # refill the queue
1642 1660 if not queue:
1643 1661 target = 2**18
1644 1662 for chunk in self.iter:
1645 1663 queue.append(chunk)
1646 1664 target -= len(chunk)
1647 1665 if target <= 0:
1648 1666 break
1649 1667 if not queue:
1650 1668 break
1651 1669
1652 1670 # The easy way to do this would be to queue.popleft(), modify the
1653 1671 # chunk (if necessary), then queue.appendleft(). However, for cases
1654 1672 # where we read partial chunk content, this incurs 2 dequeue
1655 1673 # mutations and creates a new str for the remaining chunk in the
1656 1674 # queue. Our code below avoids this overhead.
1657 1675
1658 1676 chunk = queue[0]
1659 1677 chunkl = len(chunk)
1660 1678 offset = self._chunkoffset
1661 1679
1662 1680 # Use full chunk.
1663 1681 if offset == 0 and left >= chunkl:
1664 1682 left -= chunkl
1665 1683 queue.popleft()
1666 1684 buf.append(chunk)
1667 1685 # self._chunkoffset remains at 0.
1668 1686 continue
1669 1687
1670 1688 chunkremaining = chunkl - offset
1671 1689
1672 1690 # Use all of unconsumed part of chunk.
1673 1691 if left >= chunkremaining:
1674 1692 left -= chunkremaining
1675 1693 queue.popleft()
1676 1694 # offset == 0 is enabled by block above, so this won't merely
1677 1695 # copy via ``chunk[0:]``.
1678 1696 buf.append(chunk[offset:])
1679 1697 self._chunkoffset = 0
1680 1698
1681 1699 # Partial chunk needed.
1682 1700 else:
1683 1701 buf.append(chunk[offset:offset + left])
1684 1702 self._chunkoffset += left
1685 1703 left -= chunkremaining
1686 1704
1687 1705 return ''.join(buf)
1688 1706
1689 1707 def filechunkiter(f, size=131072, limit=None):
1690 1708 """Create a generator that produces the data in the file size
1691 1709 (default 131072) bytes at a time, up to optional limit (default is
1692 1710 to read all data). Chunks may be less than size bytes if the
1693 1711 chunk is the last chunk in the file, or the file is a socket or
1694 1712 some other type of file that sometimes reads less data than is
1695 1713 requested."""
1696 1714 assert size >= 0
1697 1715 assert limit is None or limit >= 0
1698 1716 while True:
1699 1717 if limit is None:
1700 1718 nbytes = size
1701 1719 else:
1702 1720 nbytes = min(limit, size)
1703 1721 s = nbytes and f.read(nbytes)
1704 1722 if not s:
1705 1723 break
1706 1724 if limit:
1707 1725 limit -= len(s)
1708 1726 yield s
1709 1727
1710 1728 def makedate(timestamp=None):
1711 1729 '''Return a unix timestamp (or the current time) as a (unixtime,
1712 1730 offset) tuple based off the local timezone.'''
1713 1731 if timestamp is None:
1714 1732 timestamp = time.time()
1715 1733 if timestamp < 0:
1716 1734 hint = _("check your clock")
1717 1735 raise Abort(_("negative timestamp: %d") % timestamp, hint=hint)
1718 1736 delta = (datetime.datetime.utcfromtimestamp(timestamp) -
1719 1737 datetime.datetime.fromtimestamp(timestamp))
1720 1738 tz = delta.days * 86400 + delta.seconds
1721 1739 return timestamp, tz
1722 1740
1723 1741 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1724 1742 """represent a (unixtime, offset) tuple as a localized time.
1725 1743 unixtime is seconds since the epoch, and offset is the time zone's
1726 1744 number of seconds away from UTC.
1727 1745
1728 1746 >>> datestr((0, 0))
1729 1747 'Thu Jan 01 00:00:00 1970 +0000'
1730 1748 >>> datestr((42, 0))
1731 1749 'Thu Jan 01 00:00:42 1970 +0000'
1732 1750 >>> datestr((-42, 0))
1733 1751 'Wed Dec 31 23:59:18 1969 +0000'
1734 1752 >>> datestr((0x7fffffff, 0))
1735 1753 'Tue Jan 19 03:14:07 2038 +0000'
1736 1754 >>> datestr((-0x80000000, 0))
1737 1755 'Fri Dec 13 20:45:52 1901 +0000'
1738 1756 """
1739 1757 t, tz = date or makedate()
1740 1758 if "%1" in format or "%2" in format or "%z" in format:
1741 1759 sign = (tz > 0) and "-" or "+"
1742 1760 minutes = abs(tz) // 60
1743 1761 q, r = divmod(minutes, 60)
1744 1762 format = format.replace("%z", "%1%2")
1745 1763 format = format.replace("%1", "%c%02d" % (sign, q))
1746 1764 format = format.replace("%2", "%02d" % r)
1747 1765 d = t - tz
1748 1766 if d > 0x7fffffff:
1749 1767 d = 0x7fffffff
1750 1768 elif d < -0x80000000:
1751 1769 d = -0x80000000
1752 1770 # Never use time.gmtime() and datetime.datetime.fromtimestamp()
1753 1771 # because they use the gmtime() system call which is buggy on Windows
1754 1772 # for negative values.
1755 1773 t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
1756 1774 s = t.strftime(format)
1757 1775 return s
1758 1776
1759 1777 def shortdate(date=None):
1760 1778 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1761 1779 return datestr(date, format='%Y-%m-%d')
1762 1780
1763 1781 def parsetimezone(s):
1764 1782 """find a trailing timezone, if any, in string, and return a
1765 1783 (offset, remainder) pair"""
1766 1784
1767 1785 if s.endswith("GMT") or s.endswith("UTC"):
1768 1786 return 0, s[:-3].rstrip()
1769 1787
1770 1788 # Unix-style timezones [+-]hhmm
1771 1789 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
1772 1790 sign = (s[-5] == "+") and 1 or -1
1773 1791 hours = int(s[-4:-2])
1774 1792 minutes = int(s[-2:])
1775 1793 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
1776 1794
1777 1795 # ISO8601 trailing Z
1778 1796 if s.endswith("Z") and s[-2:-1].isdigit():
1779 1797 return 0, s[:-1]
1780 1798
1781 1799 # ISO8601-style [+-]hh:mm
1782 1800 if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and
1783 1801 s[-5:-3].isdigit() and s[-2:].isdigit()):
1784 1802 sign = (s[-6] == "+") and 1 or -1
1785 1803 hours = int(s[-5:-3])
1786 1804 minutes = int(s[-2:])
1787 1805 return -sign * (hours * 60 + minutes) * 60, s[:-6]
1788 1806
1789 1807 return None, s
1790 1808
1791 1809 def strdate(string, format, defaults=[]):
1792 1810 """parse a localized time string and return a (unixtime, offset) tuple.
1793 1811 if the string cannot be parsed, ValueError is raised."""
1794 1812 # NOTE: unixtime = localunixtime + offset
1795 1813 offset, date = parsetimezone(string)
1796 1814
1797 1815 # add missing elements from defaults
1798 1816 usenow = False # default to using biased defaults
1799 1817 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
1800 1818 found = [True for p in part if ("%"+p) in format]
1801 1819 if not found:
1802 1820 date += "@" + defaults[part][usenow]
1803 1821 format += "@%" + part[0]
1804 1822 else:
1805 1823 # We've found a specific time element, less specific time
1806 1824 # elements are relative to today
1807 1825 usenow = True
1808 1826
1809 1827 timetuple = time.strptime(date, format)
1810 1828 localunixtime = int(calendar.timegm(timetuple))
1811 1829 if offset is None:
1812 1830 # local timezone
1813 1831 unixtime = int(time.mktime(timetuple))
1814 1832 offset = unixtime - localunixtime
1815 1833 else:
1816 1834 unixtime = localunixtime + offset
1817 1835 return unixtime, offset
1818 1836
1819 1837 def parsedate(date, formats=None, bias=None):
1820 1838 """parse a localized date/time and return a (unixtime, offset) tuple.
1821 1839
1822 1840 The date may be a "unixtime offset" string or in one of the specified
1823 1841 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1824 1842
1825 1843 >>> parsedate(' today ') == parsedate(\
1826 1844 datetime.date.today().strftime('%b %d'))
1827 1845 True
1828 1846 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
1829 1847 datetime.timedelta(days=1)\
1830 1848 ).strftime('%b %d'))
1831 1849 True
1832 1850 >>> now, tz = makedate()
1833 1851 >>> strnow, strtz = parsedate('now')
1834 1852 >>> (strnow - now) < 1
1835 1853 True
1836 1854 >>> tz == strtz
1837 1855 True
1838 1856 """
1839 1857 if bias is None:
1840 1858 bias = {}
1841 1859 if not date:
1842 1860 return 0, 0
1843 1861 if isinstance(date, tuple) and len(date) == 2:
1844 1862 return date
1845 1863 if not formats:
1846 1864 formats = defaultdateformats
1847 1865 date = date.strip()
1848 1866
1849 1867 if date == 'now' or date == _('now'):
1850 1868 return makedate()
1851 1869 if date == 'today' or date == _('today'):
1852 1870 date = datetime.date.today().strftime('%b %d')
1853 1871 elif date == 'yesterday' or date == _('yesterday'):
1854 1872 date = (datetime.date.today() -
1855 1873 datetime.timedelta(days=1)).strftime('%b %d')
1856 1874
1857 1875 try:
1858 1876 when, offset = map(int, date.split(' '))
1859 1877 except ValueError:
1860 1878 # fill out defaults
1861 1879 now = makedate()
1862 1880 defaults = {}
1863 1881 for part in ("d", "mb", "yY", "HI", "M", "S"):
1864 1882 # this piece is for rounding the specific end of unknowns
1865 1883 b = bias.get(part)
1866 1884 if b is None:
1867 1885 if part[0] in "HMS":
1868 1886 b = "00"
1869 1887 else:
1870 1888 b = "0"
1871 1889
1872 1890 # this piece is for matching the generic end to today's date
1873 1891 n = datestr(now, "%" + part[0])
1874 1892
1875 1893 defaults[part] = (b, n)
1876 1894
1877 1895 for format in formats:
1878 1896 try:
1879 1897 when, offset = strdate(date, format, defaults)
1880 1898 except (ValueError, OverflowError):
1881 1899 pass
1882 1900 else:
1883 1901 break
1884 1902 else:
1885 1903 raise Abort(_('invalid date: %r') % date)
1886 1904 # validate explicit (probably user-specified) date and
1887 1905 # time zone offset. values must fit in signed 32 bits for
1888 1906 # current 32-bit linux runtimes. timezones go from UTC-12
1889 1907 # to UTC+14
1890 1908 if when < -0x80000000 or when > 0x7fffffff:
1891 1909 raise Abort(_('date exceeds 32 bits: %d') % when)
1892 1910 if offset < -50400 or offset > 43200:
1893 1911 raise Abort(_('impossible time zone offset: %d') % offset)
1894 1912 return when, offset
1895 1913
1896 1914 def matchdate(date):
1897 1915 """Return a function that matches a given date match specifier
1898 1916
1899 1917 Formats include:
1900 1918
1901 1919 '{date}' match a given date to the accuracy provided
1902 1920
1903 1921 '<{date}' on or before a given date
1904 1922
1905 1923 '>{date}' on or after a given date
1906 1924
1907 1925 >>> p1 = parsedate("10:29:59")
1908 1926 >>> p2 = parsedate("10:30:00")
1909 1927 >>> p3 = parsedate("10:30:59")
1910 1928 >>> p4 = parsedate("10:31:00")
1911 1929 >>> p5 = parsedate("Sep 15 10:30:00 1999")
1912 1930 >>> f = matchdate("10:30")
1913 1931 >>> f(p1[0])
1914 1932 False
1915 1933 >>> f(p2[0])
1916 1934 True
1917 1935 >>> f(p3[0])
1918 1936 True
1919 1937 >>> f(p4[0])
1920 1938 False
1921 1939 >>> f(p5[0])
1922 1940 False
1923 1941 """
1924 1942
1925 1943 def lower(date):
1926 1944 d = {'mb': "1", 'd': "1"}
1927 1945 return parsedate(date, extendeddateformats, d)[0]
1928 1946
1929 1947 def upper(date):
1930 1948 d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"}
1931 1949 for days in ("31", "30", "29"):
1932 1950 try:
1933 1951 d["d"] = days
1934 1952 return parsedate(date, extendeddateformats, d)[0]
1935 1953 except Abort:
1936 1954 pass
1937 1955 d["d"] = "28"
1938 1956 return parsedate(date, extendeddateformats, d)[0]
1939 1957
1940 1958 date = date.strip()
1941 1959
1942 1960 if not date:
1943 1961 raise Abort(_("dates cannot consist entirely of whitespace"))
1944 1962 elif date[0] == "<":
1945 1963 if not date[1:]:
1946 1964 raise Abort(_("invalid day spec, use '<DATE'"))
1947 1965 when = upper(date[1:])
1948 1966 return lambda x: x <= when
1949 1967 elif date[0] == ">":
1950 1968 if not date[1:]:
1951 1969 raise Abort(_("invalid day spec, use '>DATE'"))
1952 1970 when = lower(date[1:])
1953 1971 return lambda x: x >= when
1954 1972 elif date[0] == "-":
1955 1973 try:
1956 1974 days = int(date[1:])
1957 1975 except ValueError:
1958 1976 raise Abort(_("invalid day spec: %s") % date[1:])
1959 1977 if days < 0:
1960 1978 raise Abort(_("%s must be nonnegative (see 'hg help dates')")
1961 1979 % date[1:])
1962 1980 when = makedate()[0] - days * 3600 * 24
1963 1981 return lambda x: x >= when
1964 1982 elif " to " in date:
1965 1983 a, b = date.split(" to ")
1966 1984 start, stop = lower(a), upper(b)
1967 1985 return lambda x: x >= start and x <= stop
1968 1986 else:
1969 1987 start, stop = lower(date), upper(date)
1970 1988 return lambda x: x >= start and x <= stop
1971 1989
1972 1990 def stringmatcher(pattern):
1973 1991 """
1974 1992 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1975 1993 returns the matcher name, pattern, and matcher function.
1976 1994 missing or unknown prefixes are treated as literal matches.
1977 1995
1978 1996 helper for tests:
1979 1997 >>> def test(pattern, *tests):
1980 1998 ... kind, pattern, matcher = stringmatcher(pattern)
1981 1999 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1982 2000
1983 2001 exact matching (no prefix):
1984 2002 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1985 2003 ('literal', 'abcdefg', [False, False, True])
1986 2004
1987 2005 regex matching ('re:' prefix)
1988 2006 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1989 2007 ('re', 'a.+b', [False, False, True])
1990 2008
1991 2009 force exact matches ('literal:' prefix)
1992 2010 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1993 2011 ('literal', 're:foobar', [False, True])
1994 2012
1995 2013 unknown prefixes are ignored and treated as literals
1996 2014 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1997 2015 ('literal', 'foo:bar', [False, False, True])
1998 2016 """
1999 2017 if pattern.startswith('re:'):
2000 2018 pattern = pattern[3:]
2001 2019 try:
2002 2020 regex = remod.compile(pattern)
2003 2021 except remod.error as e:
2004 2022 raise error.ParseError(_('invalid regular expression: %s')
2005 2023 % e)
2006 2024 return 're', pattern, regex.search
2007 2025 elif pattern.startswith('literal:'):
2008 2026 pattern = pattern[8:]
2009 2027 return 'literal', pattern, pattern.__eq__
2010 2028
2011 2029 def shortuser(user):
2012 2030 """Return a short representation of a user name or email address."""
2013 2031 f = user.find('@')
2014 2032 if f >= 0:
2015 2033 user = user[:f]
2016 2034 f = user.find('<')
2017 2035 if f >= 0:
2018 2036 user = user[f + 1:]
2019 2037 f = user.find(' ')
2020 2038 if f >= 0:
2021 2039 user = user[:f]
2022 2040 f = user.find('.')
2023 2041 if f >= 0:
2024 2042 user = user[:f]
2025 2043 return user
2026 2044
2027 2045 def emailuser(user):
2028 2046 """Return the user portion of an email address."""
2029 2047 f = user.find('@')
2030 2048 if f >= 0:
2031 2049 user = user[:f]
2032 2050 f = user.find('<')
2033 2051 if f >= 0:
2034 2052 user = user[f + 1:]
2035 2053 return user
2036 2054
2037 2055 def email(author):
2038 2056 '''get email of author.'''
2039 2057 r = author.find('>')
2040 2058 if r == -1:
2041 2059 r = None
2042 2060 return author[author.find('<') + 1:r]
2043 2061
2044 2062 def ellipsis(text, maxlength=400):
2045 2063 """Trim string to at most maxlength (default: 400) columns in display."""
2046 2064 return encoding.trim(text, maxlength, ellipsis='...')
2047 2065
2048 2066 def unitcountfn(*unittable):
2049 2067 '''return a function that renders a readable count of some quantity'''
2050 2068
2051 2069 def go(count):
2052 2070 for multiplier, divisor, format in unittable:
2053 2071 if count >= divisor * multiplier:
2054 2072 return format % (count / float(divisor))
2055 2073 return unittable[-1][2] % count
2056 2074
2057 2075 return go
2058 2076
2059 2077 bytecount = unitcountfn(
2060 2078 (100, 1 << 30, _('%.0f GB')),
2061 2079 (10, 1 << 30, _('%.1f GB')),
2062 2080 (1, 1 << 30, _('%.2f GB')),
2063 2081 (100, 1 << 20, _('%.0f MB')),
2064 2082 (10, 1 << 20, _('%.1f MB')),
2065 2083 (1, 1 << 20, _('%.2f MB')),
2066 2084 (100, 1 << 10, _('%.0f KB')),
2067 2085 (10, 1 << 10, _('%.1f KB')),
2068 2086 (1, 1 << 10, _('%.2f KB')),
2069 2087 (1, 1, _('%.0f bytes')),
2070 2088 )
2071 2089
2072 2090 def uirepr(s):
2073 2091 # Avoid double backslash in Windows path repr()
2074 2092 return repr(s).replace('\\\\', '\\')
2075 2093
2076 2094 # delay import of textwrap
2077 2095 def MBTextWrapper(**kwargs):
2078 2096 class tw(textwrap.TextWrapper):
2079 2097 """
2080 2098 Extend TextWrapper for width-awareness.
2081 2099
2082 2100 Neither number of 'bytes' in any encoding nor 'characters' is
2083 2101 appropriate to calculate terminal columns for specified string.
2084 2102
2085 2103 Original TextWrapper implementation uses built-in 'len()' directly,
2086 2104 so overriding is needed to use width information of each characters.
2087 2105
2088 2106 In addition, characters classified into 'ambiguous' width are
2089 2107 treated as wide in East Asian area, but as narrow in other.
2090 2108
2091 2109 This requires use decision to determine width of such characters.
2092 2110 """
2093 2111 def _cutdown(self, ucstr, space_left):
2094 2112 l = 0
2095 2113 colwidth = encoding.ucolwidth
2096 2114 for i in xrange(len(ucstr)):
2097 2115 l += colwidth(ucstr[i])
2098 2116 if space_left < l:
2099 2117 return (ucstr[:i], ucstr[i:])
2100 2118 return ucstr, ''
2101 2119
2102 2120 # overriding of base class
2103 2121 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
2104 2122 space_left = max(width - cur_len, 1)
2105 2123
2106 2124 if self.break_long_words:
2107 2125 cut, res = self._cutdown(reversed_chunks[-1], space_left)
2108 2126 cur_line.append(cut)
2109 2127 reversed_chunks[-1] = res
2110 2128 elif not cur_line:
2111 2129 cur_line.append(reversed_chunks.pop())
2112 2130
2113 2131 # this overriding code is imported from TextWrapper of Python 2.6
2114 2132 # to calculate columns of string by 'encoding.ucolwidth()'
2115 2133 def _wrap_chunks(self, chunks):
2116 2134 colwidth = encoding.ucolwidth
2117 2135
2118 2136 lines = []
2119 2137 if self.width <= 0:
2120 2138 raise ValueError("invalid width %r (must be > 0)" % self.width)
2121 2139
2122 2140 # Arrange in reverse order so items can be efficiently popped
2123 2141 # from a stack of chucks.
2124 2142 chunks.reverse()
2125 2143
2126 2144 while chunks:
2127 2145
2128 2146 # Start the list of chunks that will make up the current line.
2129 2147 # cur_len is just the length of all the chunks in cur_line.
2130 2148 cur_line = []
2131 2149 cur_len = 0
2132 2150
2133 2151 # Figure out which static string will prefix this line.
2134 2152 if lines:
2135 2153 indent = self.subsequent_indent
2136 2154 else:
2137 2155 indent = self.initial_indent
2138 2156
2139 2157 # Maximum width for this line.
2140 2158 width = self.width - len(indent)
2141 2159
2142 2160 # First chunk on line is whitespace -- drop it, unless this
2143 2161 # is the very beginning of the text (i.e. no lines started yet).
2144 2162 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
2145 2163 del chunks[-1]
2146 2164
2147 2165 while chunks:
2148 2166 l = colwidth(chunks[-1])
2149 2167
2150 2168 # Can at least squeeze this chunk onto the current line.
2151 2169 if cur_len + l <= width:
2152 2170 cur_line.append(chunks.pop())
2153 2171 cur_len += l
2154 2172
2155 2173 # Nope, this line is full.
2156 2174 else:
2157 2175 break
2158 2176
2159 2177 # The current line is full, and the next chunk is too big to
2160 2178 # fit on *any* line (not just this one).
2161 2179 if chunks and colwidth(chunks[-1]) > width:
2162 2180 self._handle_long_word(chunks, cur_line, cur_len, width)
2163 2181
2164 2182 # If the last chunk on this line is all whitespace, drop it.
2165 2183 if (self.drop_whitespace and
2166 2184 cur_line and cur_line[-1].strip() == ''):
2167 2185 del cur_line[-1]
2168 2186
2169 2187 # Convert current line back to a string and store it in list
2170 2188 # of all lines (return value).
2171 2189 if cur_line:
2172 2190 lines.append(indent + ''.join(cur_line))
2173 2191
2174 2192 return lines
2175 2193
2176 2194 global MBTextWrapper
2177 2195 MBTextWrapper = tw
2178 2196 return tw(**kwargs)
2179 2197
2180 2198 def wrap(line, width, initindent='', hangindent=''):
2181 2199 maxindent = max(len(hangindent), len(initindent))
2182 2200 if width <= maxindent:
2183 2201 # adjust for weird terminal size
2184 2202 width = max(78, maxindent + 1)
2185 2203 line = line.decode(encoding.encoding, encoding.encodingmode)
2186 2204 initindent = initindent.decode(encoding.encoding, encoding.encodingmode)
2187 2205 hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode)
2188 2206 wrapper = MBTextWrapper(width=width,
2189 2207 initial_indent=initindent,
2190 2208 subsequent_indent=hangindent)
2191 2209 return wrapper.fill(line).encode(encoding.encoding)
2192 2210
2193 2211 def iterfile(fp):
2194 2212 """like fp.__iter__ but does not have issues with EINTR. Python 2.7.12 is
2195 2213 known to have such issues."""
2196 2214 return iter(fp.readline, '')
2197 2215
2198 2216 def iterlines(iterator):
2199 2217 for chunk in iterator:
2200 2218 for line in chunk.splitlines():
2201 2219 yield line
2202 2220
2203 2221 def expandpath(path):
2204 2222 return os.path.expanduser(os.path.expandvars(path))
2205 2223
2206 2224 def hgcmd():
2207 2225 """Return the command used to execute current hg
2208 2226
2209 2227 This is different from hgexecutable() because on Windows we want
2210 2228 to avoid things opening new shell windows like batch files, so we
2211 2229 get either the python call or current executable.
2212 2230 """
2213 2231 if mainfrozen():
2214 2232 if getattr(sys, 'frozen', None) == 'macosx_app':
2215 2233 # Env variable set by py2app
2216 2234 return [os.environ['EXECUTABLEPATH']]
2217 2235 else:
2218 2236 return [sys.executable]
2219 2237 return gethgcmd()
2220 2238
2221 2239 def rundetached(args, condfn):
2222 2240 """Execute the argument list in a detached process.
2223 2241
2224 2242 condfn is a callable which is called repeatedly and should return
2225 2243 True once the child process is known to have started successfully.
2226 2244 At this point, the child process PID is returned. If the child
2227 2245 process fails to start or finishes before condfn() evaluates to
2228 2246 True, return -1.
2229 2247 """
2230 2248 # Windows case is easier because the child process is either
2231 2249 # successfully starting and validating the condition or exiting
2232 2250 # on failure. We just poll on its PID. On Unix, if the child
2233 2251 # process fails to start, it will be left in a zombie state until
2234 2252 # the parent wait on it, which we cannot do since we expect a long
2235 2253 # running process on success. Instead we listen for SIGCHLD telling
2236 2254 # us our child process terminated.
2237 2255 terminated = set()
2238 2256 def handler(signum, frame):
2239 2257 terminated.add(os.wait())
2240 2258 prevhandler = None
2241 2259 SIGCHLD = getattr(signal, 'SIGCHLD', None)
2242 2260 if SIGCHLD is not None:
2243 2261 prevhandler = signal.signal(SIGCHLD, handler)
2244 2262 try:
2245 2263 pid = spawndetached(args)
2246 2264 while not condfn():
2247 2265 if ((pid in terminated or not testpid(pid))
2248 2266 and not condfn()):
2249 2267 return -1
2250 2268 time.sleep(0.1)
2251 2269 return pid
2252 2270 finally:
2253 2271 if prevhandler is not None:
2254 2272 signal.signal(signal.SIGCHLD, prevhandler)
2255 2273
2256 2274 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2257 2275 """Return the result of interpolating items in the mapping into string s.
2258 2276
2259 2277 prefix is a single character string, or a two character string with
2260 2278 a backslash as the first character if the prefix needs to be escaped in
2261 2279 a regular expression.
2262 2280
2263 2281 fn is an optional function that will be applied to the replacement text
2264 2282 just before replacement.
2265 2283
2266 2284 escape_prefix is an optional flag that allows using doubled prefix for
2267 2285 its escaping.
2268 2286 """
2269 2287 fn = fn or (lambda s: s)
2270 2288 patterns = '|'.join(mapping.keys())
2271 2289 if escape_prefix:
2272 2290 patterns += '|' + prefix
2273 2291 if len(prefix) > 1:
2274 2292 prefix_char = prefix[1:]
2275 2293 else:
2276 2294 prefix_char = prefix
2277 2295 mapping[prefix_char] = prefix_char
2278 2296 r = remod.compile(r'%s(%s)' % (prefix, patterns))
2279 2297 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2280 2298
2281 2299 def getport(port):
2282 2300 """Return the port for a given network service.
2283 2301
2284 2302 If port is an integer, it's returned as is. If it's a string, it's
2285 2303 looked up using socket.getservbyname(). If there's no matching
2286 2304 service, error.Abort is raised.
2287 2305 """
2288 2306 try:
2289 2307 return int(port)
2290 2308 except ValueError:
2291 2309 pass
2292 2310
2293 2311 try:
2294 2312 return socket.getservbyname(port)
2295 2313 except socket.error:
2296 2314 raise Abort(_("no port number associated with service '%s'") % port)
2297 2315
2298 2316 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
2299 2317 '0': False, 'no': False, 'false': False, 'off': False,
2300 2318 'never': False}
2301 2319
2302 2320 def parsebool(s):
2303 2321 """Parse s into a boolean.
2304 2322
2305 2323 If s is not a valid boolean, returns None.
2306 2324 """
2307 2325 return _booleans.get(s.lower(), None)
2308 2326
2309 2327 _hextochr = dict((a + b, chr(int(a + b, 16)))
2310 2328 for a in string.hexdigits for b in string.hexdigits)
2311 2329
2312 2330 class url(object):
2313 2331 r"""Reliable URL parser.
2314 2332
2315 2333 This parses URLs and provides attributes for the following
2316 2334 components:
2317 2335
2318 2336 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2319 2337
2320 2338 Missing components are set to None. The only exception is
2321 2339 fragment, which is set to '' if present but empty.
2322 2340
2323 2341 If parsefragment is False, fragment is included in query. If
2324 2342 parsequery is False, query is included in path. If both are
2325 2343 False, both fragment and query are included in path.
2326 2344
2327 2345 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2328 2346
2329 2347 Note that for backward compatibility reasons, bundle URLs do not
2330 2348 take host names. That means 'bundle://../' has a path of '../'.
2331 2349
2332 2350 Examples:
2333 2351
2334 2352 >>> url('http://www.ietf.org/rfc/rfc2396.txt')
2335 2353 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2336 2354 >>> url('ssh://[::1]:2200//home/joe/repo')
2337 2355 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2338 2356 >>> url('file:///home/joe/repo')
2339 2357 <url scheme: 'file', path: '/home/joe/repo'>
2340 2358 >>> url('file:///c:/temp/foo/')
2341 2359 <url scheme: 'file', path: 'c:/temp/foo/'>
2342 2360 >>> url('bundle:foo')
2343 2361 <url scheme: 'bundle', path: 'foo'>
2344 2362 >>> url('bundle://../foo')
2345 2363 <url scheme: 'bundle', path: '../foo'>
2346 2364 >>> url(r'c:\foo\bar')
2347 2365 <url path: 'c:\\foo\\bar'>
2348 2366 >>> url(r'\\blah\blah\blah')
2349 2367 <url path: '\\\\blah\\blah\\blah'>
2350 2368 >>> url(r'\\blah\blah\blah#baz')
2351 2369 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2352 2370 >>> url(r'file:///C:\users\me')
2353 2371 <url scheme: 'file', path: 'C:\\users\\me'>
2354 2372
2355 2373 Authentication credentials:
2356 2374
2357 2375 >>> url('ssh://joe:xyz@x/repo')
2358 2376 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2359 2377 >>> url('ssh://joe@x/repo')
2360 2378 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2361 2379
2362 2380 Query strings and fragments:
2363 2381
2364 2382 >>> url('http://host/a?b#c')
2365 2383 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2366 2384 >>> url('http://host/a?b#c', parsequery=False, parsefragment=False)
2367 2385 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2368 2386
2369 2387 Empty path:
2370 2388
2371 2389 >>> url('')
2372 2390 <url path: ''>
2373 2391 >>> url('#a')
2374 2392 <url path: '', fragment: 'a'>
2375 2393 >>> url('http://host/')
2376 2394 <url scheme: 'http', host: 'host', path: ''>
2377 2395 >>> url('http://host/#a')
2378 2396 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
2379 2397
2380 2398 Only scheme:
2381 2399
2382 2400 >>> url('http:')
2383 2401 <url scheme: 'http'>
2384 2402 """
2385 2403
2386 2404 _safechars = "!~*'()+"
2387 2405 _safepchars = "/!~*'()+:\\"
2388 2406 _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match
2389 2407
2390 2408 def __init__(self, path, parsequery=True, parsefragment=True):
2391 2409 # We slowly chomp away at path until we have only the path left
2392 2410 self.scheme = self.user = self.passwd = self.host = None
2393 2411 self.port = self.path = self.query = self.fragment = None
2394 2412 self._localpath = True
2395 2413 self._hostport = ''
2396 2414 self._origpath = path
2397 2415
2398 2416 if parsefragment and '#' in path:
2399 2417 path, self.fragment = path.split('#', 1)
2400 2418
2401 2419 # special case for Windows drive letters and UNC paths
2402 2420 if hasdriveletter(path) or path.startswith('\\\\'):
2403 2421 self.path = path
2404 2422 return
2405 2423
2406 2424 # For compatibility reasons, we can't handle bundle paths as
2407 2425 # normal URLS
2408 2426 if path.startswith('bundle:'):
2409 2427 self.scheme = 'bundle'
2410 2428 path = path[7:]
2411 2429 if path.startswith('//'):
2412 2430 path = path[2:]
2413 2431 self.path = path
2414 2432 return
2415 2433
2416 2434 if self._matchscheme(path):
2417 2435 parts = path.split(':', 1)
2418 2436 if parts[0]:
2419 2437 self.scheme, path = parts
2420 2438 self._localpath = False
2421 2439
2422 2440 if not path:
2423 2441 path = None
2424 2442 if self._localpath:
2425 2443 self.path = ''
2426 2444 return
2427 2445 else:
2428 2446 if self._localpath:
2429 2447 self.path = path
2430 2448 return
2431 2449
2432 2450 if parsequery and '?' in path:
2433 2451 path, self.query = path.split('?', 1)
2434 2452 if not path:
2435 2453 path = None
2436 2454 if not self.query:
2437 2455 self.query = None
2438 2456
2439 2457 # // is required to specify a host/authority
2440 2458 if path and path.startswith('//'):
2441 2459 parts = path[2:].split('/', 1)
2442 2460 if len(parts) > 1:
2443 2461 self.host, path = parts
2444 2462 else:
2445 2463 self.host = parts[0]
2446 2464 path = None
2447 2465 if not self.host:
2448 2466 self.host = None
2449 2467 # path of file:///d is /d
2450 2468 # path of file:///d:/ is d:/, not /d:/
2451 2469 if path and not hasdriveletter(path):
2452 2470 path = '/' + path
2453 2471
2454 2472 if self.host and '@' in self.host:
2455 2473 self.user, self.host = self.host.rsplit('@', 1)
2456 2474 if ':' in self.user:
2457 2475 self.user, self.passwd = self.user.split(':', 1)
2458 2476 if not self.host:
2459 2477 self.host = None
2460 2478
2461 2479 # Don't split on colons in IPv6 addresses without ports
2462 2480 if (self.host and ':' in self.host and
2463 2481 not (self.host.startswith('[') and self.host.endswith(']'))):
2464 2482 self._hostport = self.host
2465 2483 self.host, self.port = self.host.rsplit(':', 1)
2466 2484 if not self.host:
2467 2485 self.host = None
2468 2486
2469 2487 if (self.host and self.scheme == 'file' and
2470 2488 self.host not in ('localhost', '127.0.0.1', '[::1]')):
2471 2489 raise Abort(_('file:// URLs can only refer to localhost'))
2472 2490
2473 2491 self.path = path
2474 2492
2475 2493 # leave the query string escaped
2476 2494 for a in ('user', 'passwd', 'host', 'port',
2477 2495 'path', 'fragment'):
2478 2496 v = getattr(self, a)
2479 2497 if v is not None:
2480 2498 setattr(self, a, pycompat.urlunquote(v))
2481 2499
2482 2500 def __repr__(self):
2483 2501 attrs = []
2484 2502 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
2485 2503 'query', 'fragment'):
2486 2504 v = getattr(self, a)
2487 2505 if v is not None:
2488 2506 attrs.append('%s: %r' % (a, v))
2489 2507 return '<url %s>' % ', '.join(attrs)
2490 2508
2491 2509 def __str__(self):
2492 2510 r"""Join the URL's components back into a URL string.
2493 2511
2494 2512 Examples:
2495 2513
2496 2514 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
2497 2515 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
2498 2516 >>> str(url('http://user:pw@host:80/?foo=bar&baz=42'))
2499 2517 'http://user:pw@host:80/?foo=bar&baz=42'
2500 2518 >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz'))
2501 2519 'http://user:pw@host:80/?foo=bar%3dbaz'
2502 2520 >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
2503 2521 'ssh://user:pw@[::1]:2200//home/joe#'
2504 2522 >>> str(url('http://localhost:80//'))
2505 2523 'http://localhost:80//'
2506 2524 >>> str(url('http://localhost:80/'))
2507 2525 'http://localhost:80/'
2508 2526 >>> str(url('http://localhost:80'))
2509 2527 'http://localhost:80/'
2510 2528 >>> str(url('bundle:foo'))
2511 2529 'bundle:foo'
2512 2530 >>> str(url('bundle://../foo'))
2513 2531 'bundle:../foo'
2514 2532 >>> str(url('path'))
2515 2533 'path'
2516 2534 >>> str(url('file:///tmp/foo/bar'))
2517 2535 'file:///tmp/foo/bar'
2518 2536 >>> str(url('file:///c:/tmp/foo/bar'))
2519 2537 'file:///c:/tmp/foo/bar'
2520 2538 >>> print url(r'bundle:foo\bar')
2521 2539 bundle:foo\bar
2522 2540 >>> print url(r'file:///D:\data\hg')
2523 2541 file:///D:\data\hg
2524 2542 """
2525 2543 if self._localpath:
2526 2544 s = self.path
2527 2545 if self.scheme == 'bundle':
2528 2546 s = 'bundle:' + s
2529 2547 if self.fragment:
2530 2548 s += '#' + self.fragment
2531 2549 return s
2532 2550
2533 2551 s = self.scheme + ':'
2534 2552 if self.user or self.passwd or self.host:
2535 2553 s += '//'
2536 2554 elif self.scheme and (not self.path or self.path.startswith('/')
2537 2555 or hasdriveletter(self.path)):
2538 2556 s += '//'
2539 2557 if hasdriveletter(self.path):
2540 2558 s += '/'
2541 2559 if self.user:
2542 2560 s += urlreq.quote(self.user, safe=self._safechars)
2543 2561 if self.passwd:
2544 2562 s += ':' + urlreq.quote(self.passwd, safe=self._safechars)
2545 2563 if self.user or self.passwd:
2546 2564 s += '@'
2547 2565 if self.host:
2548 2566 if not (self.host.startswith('[') and self.host.endswith(']')):
2549 2567 s += urlreq.quote(self.host)
2550 2568 else:
2551 2569 s += self.host
2552 2570 if self.port:
2553 2571 s += ':' + urlreq.quote(self.port)
2554 2572 if self.host:
2555 2573 s += '/'
2556 2574 if self.path:
2557 2575 # TODO: similar to the query string, we should not unescape the
2558 2576 # path when we store it, the path might contain '%2f' = '/',
2559 2577 # which we should *not* escape.
2560 2578 s += urlreq.quote(self.path, safe=self._safepchars)
2561 2579 if self.query:
2562 2580 # we store the query in escaped form.
2563 2581 s += '?' + self.query
2564 2582 if self.fragment is not None:
2565 2583 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars)
2566 2584 return s
2567 2585
2568 2586 def authinfo(self):
2569 2587 user, passwd = self.user, self.passwd
2570 2588 try:
2571 2589 self.user, self.passwd = None, None
2572 2590 s = str(self)
2573 2591 finally:
2574 2592 self.user, self.passwd = user, passwd
2575 2593 if not self.user:
2576 2594 return (s, None)
2577 2595 # authinfo[1] is passed to urllib2 password manager, and its
2578 2596 # URIs must not contain credentials. The host is passed in the
2579 2597 # URIs list because Python < 2.4.3 uses only that to search for
2580 2598 # a password.
2581 2599 return (s, (None, (s, self.host),
2582 2600 self.user, self.passwd or ''))
2583 2601
2584 2602 def isabs(self):
2585 2603 if self.scheme and self.scheme != 'file':
2586 2604 return True # remote URL
2587 2605 if hasdriveletter(self.path):
2588 2606 return True # absolute for our purposes - can't be joined()
2589 2607 if self.path.startswith(r'\\'):
2590 2608 return True # Windows UNC path
2591 2609 if self.path.startswith('/'):
2592 2610 return True # POSIX-style
2593 2611 return False
2594 2612
2595 2613 def localpath(self):
2596 2614 if self.scheme == 'file' or self.scheme == 'bundle':
2597 2615 path = self.path or '/'
2598 2616 # For Windows, we need to promote hosts containing drive
2599 2617 # letters to paths with drive letters.
2600 2618 if hasdriveletter(self._hostport):
2601 2619 path = self._hostport + '/' + self.path
2602 2620 elif (self.host is not None and self.path
2603 2621 and not hasdriveletter(path)):
2604 2622 path = '/' + path
2605 2623 return path
2606 2624 return self._origpath
2607 2625
2608 2626 def islocal(self):
2609 2627 '''whether localpath will return something that posixfile can open'''
2610 2628 return (not self.scheme or self.scheme == 'file'
2611 2629 or self.scheme == 'bundle')
2612 2630
2613 2631 def hasscheme(path):
2614 2632 return bool(url(path).scheme)
2615 2633
2616 2634 def hasdriveletter(path):
2617 2635 return path and path[1:2] == ':' and path[0:1].isalpha()
2618 2636
2619 2637 def urllocalpath(path):
2620 2638 return url(path, parsequery=False, parsefragment=False).localpath()
2621 2639
2622 2640 def hidepassword(u):
2623 2641 '''hide user credential in a url string'''
2624 2642 u = url(u)
2625 2643 if u.passwd:
2626 2644 u.passwd = '***'
2627 2645 return str(u)
2628 2646
2629 2647 def removeauth(u):
2630 2648 '''remove all authentication information from a url string'''
2631 2649 u = url(u)
2632 2650 u.user = u.passwd = None
2633 2651 return str(u)
2634 2652
2635 2653 def isatty(fp):
2636 2654 try:
2637 2655 return fp.isatty()
2638 2656 except AttributeError:
2639 2657 return False
2640 2658
2641 2659 timecount = unitcountfn(
2642 2660 (1, 1e3, _('%.0f s')),
2643 2661 (100, 1, _('%.1f s')),
2644 2662 (10, 1, _('%.2f s')),
2645 2663 (1, 1, _('%.3f s')),
2646 2664 (100, 0.001, _('%.1f ms')),
2647 2665 (10, 0.001, _('%.2f ms')),
2648 2666 (1, 0.001, _('%.3f ms')),
2649 2667 (100, 0.000001, _('%.1f us')),
2650 2668 (10, 0.000001, _('%.2f us')),
2651 2669 (1, 0.000001, _('%.3f us')),
2652 2670 (100, 0.000000001, _('%.1f ns')),
2653 2671 (10, 0.000000001, _('%.2f ns')),
2654 2672 (1, 0.000000001, _('%.3f ns')),
2655 2673 )
2656 2674
2657 2675 _timenesting = [0]
2658 2676
2659 2677 def timed(func):
2660 2678 '''Report the execution time of a function call to stderr.
2661 2679
2662 2680 During development, use as a decorator when you need to measure
2663 2681 the cost of a function, e.g. as follows:
2664 2682
2665 2683 @util.timed
2666 2684 def foo(a, b, c):
2667 2685 pass
2668 2686 '''
2669 2687
2670 2688 def wrapper(*args, **kwargs):
2671 2689 start = time.time()
2672 2690 indent = 2
2673 2691 _timenesting[0] += indent
2674 2692 try:
2675 2693 return func(*args, **kwargs)
2676 2694 finally:
2677 2695 elapsed = time.time() - start
2678 2696 _timenesting[0] -= indent
2679 2697 sys.stderr.write('%s%s: %s\n' %
2680 2698 (' ' * _timenesting[0], func.__name__,
2681 2699 timecount(elapsed)))
2682 2700 return wrapper
2683 2701
2684 2702 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
2685 2703 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
2686 2704
2687 2705 def sizetoint(s):
2688 2706 '''Convert a space specifier to a byte count.
2689 2707
2690 2708 >>> sizetoint('30')
2691 2709 30
2692 2710 >>> sizetoint('2.2kb')
2693 2711 2252
2694 2712 >>> sizetoint('6M')
2695 2713 6291456
2696 2714 '''
2697 2715 t = s.strip().lower()
2698 2716 try:
2699 2717 for k, u in _sizeunits:
2700 2718 if t.endswith(k):
2701 2719 return int(float(t[:-len(k)]) * u)
2702 2720 return int(t)
2703 2721 except ValueError:
2704 2722 raise error.ParseError(_("couldn't parse size: %s") % s)
2705 2723
2706 2724 class hooks(object):
2707 2725 '''A collection of hook functions that can be used to extend a
2708 2726 function's behavior. Hooks are called in lexicographic order,
2709 2727 based on the names of their sources.'''
2710 2728
2711 2729 def __init__(self):
2712 2730 self._hooks = []
2713 2731
2714 2732 def add(self, source, hook):
2715 2733 self._hooks.append((source, hook))
2716 2734
2717 2735 def __call__(self, *args):
2718 2736 self._hooks.sort(key=lambda x: x[0])
2719 2737 results = []
2720 2738 for source, hook in self._hooks:
2721 2739 results.append(hook(*args))
2722 2740 return results
2723 2741
2724 2742 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
2725 2743 '''Yields lines for a nicely formatted stacktrace.
2726 2744 Skips the 'skip' last entries.
2727 2745 Each file+linenumber is formatted according to fileline.
2728 2746 Each line is formatted according to line.
2729 2747 If line is None, it yields:
2730 2748 length of longest filepath+line number,
2731 2749 filepath+linenumber,
2732 2750 function
2733 2751
2734 2752 Not be used in production code but very convenient while developing.
2735 2753 '''
2736 2754 entries = [(fileline % (fn, ln), func)
2737 2755 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
2738 2756 if entries:
2739 2757 fnmax = max(len(entry[0]) for entry in entries)
2740 2758 for fnln, func in entries:
2741 2759 if line is None:
2742 2760 yield (fnmax, fnln, func)
2743 2761 else:
2744 2762 yield line % (fnmax, fnln, func)
2745 2763
2746 2764 def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout):
2747 2765 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
2748 2766 Skips the 'skip' last entries. By default it will flush stdout first.
2749 2767 It can be used everywhere and intentionally does not require an ui object.
2750 2768 Not be used in production code but very convenient while developing.
2751 2769 '''
2752 2770 if otherf:
2753 2771 otherf.flush()
2754 2772 f.write('%s at:\n' % msg)
2755 2773 for line in getstackframes(skip + 1):
2756 2774 f.write(line)
2757 2775 f.flush()
2758 2776
2759 2777 class dirs(object):
2760 2778 '''a multiset of directory names from a dirstate or manifest'''
2761 2779
2762 2780 def __init__(self, map, skip=None):
2763 2781 self._dirs = {}
2764 2782 addpath = self.addpath
2765 2783 if safehasattr(map, 'iteritems') and skip is not None:
2766 2784 for f, s in map.iteritems():
2767 2785 if s[0] != skip:
2768 2786 addpath(f)
2769 2787 else:
2770 2788 for f in map:
2771 2789 addpath(f)
2772 2790
2773 2791 def addpath(self, path):
2774 2792 dirs = self._dirs
2775 2793 for base in finddirs(path):
2776 2794 if base in dirs:
2777 2795 dirs[base] += 1
2778 2796 return
2779 2797 dirs[base] = 1
2780 2798
2781 2799 def delpath(self, path):
2782 2800 dirs = self._dirs
2783 2801 for base in finddirs(path):
2784 2802 if dirs[base] > 1:
2785 2803 dirs[base] -= 1
2786 2804 return
2787 2805 del dirs[base]
2788 2806
2789 2807 def __iter__(self):
2790 2808 return self._dirs.iterkeys()
2791 2809
2792 2810 def __contains__(self, d):
2793 2811 return d in self._dirs
2794 2812
2795 2813 if safehasattr(parsers, 'dirs'):
2796 2814 dirs = parsers.dirs
2797 2815
2798 2816 def finddirs(path):
2799 2817 pos = path.rfind('/')
2800 2818 while pos != -1:
2801 2819 yield path[:pos]
2802 2820 pos = path.rfind('/', 0, pos)
2803 2821
2804 2822 class ctxmanager(object):
2805 2823 '''A context manager for use in 'with' blocks to allow multiple
2806 2824 contexts to be entered at once. This is both safer and more
2807 2825 flexible than contextlib.nested.
2808 2826
2809 2827 Once Mercurial supports Python 2.7+, this will become mostly
2810 2828 unnecessary.
2811 2829 '''
2812 2830
2813 2831 def __init__(self, *args):
2814 2832 '''Accepts a list of no-argument functions that return context
2815 2833 managers. These will be invoked at __call__ time.'''
2816 2834 self._pending = args
2817 2835 self._atexit = []
2818 2836
2819 2837 def __enter__(self):
2820 2838 return self
2821 2839
2822 2840 def enter(self):
2823 2841 '''Create and enter context managers in the order in which they were
2824 2842 passed to the constructor.'''
2825 2843 values = []
2826 2844 for func in self._pending:
2827 2845 obj = func()
2828 2846 values.append(obj.__enter__())
2829 2847 self._atexit.append(obj.__exit__)
2830 2848 del self._pending
2831 2849 return values
2832 2850
2833 2851 def atexit(self, func, *args, **kwargs):
2834 2852 '''Add a function to call when this context manager exits. The
2835 2853 ordering of multiple atexit calls is unspecified, save that
2836 2854 they will happen before any __exit__ functions.'''
2837 2855 def wrapper(exc_type, exc_val, exc_tb):
2838 2856 func(*args, **kwargs)
2839 2857 self._atexit.append(wrapper)
2840 2858 return func
2841 2859
2842 2860 def __exit__(self, exc_type, exc_val, exc_tb):
2843 2861 '''Context managers are exited in the reverse order from which
2844 2862 they were created.'''
2845 2863 received = exc_type is not None
2846 2864 suppressed = False
2847 2865 pending = None
2848 2866 self._atexit.reverse()
2849 2867 for exitfunc in self._atexit:
2850 2868 try:
2851 2869 if exitfunc(exc_type, exc_val, exc_tb):
2852 2870 suppressed = True
2853 2871 exc_type = None
2854 2872 exc_val = None
2855 2873 exc_tb = None
2856 2874 except BaseException:
2857 2875 pending = sys.exc_info()
2858 2876 exc_type, exc_val, exc_tb = pending = sys.exc_info()
2859 2877 del self._atexit
2860 2878 if pending:
2861 2879 raise exc_val
2862 2880 return received and suppressed
2863 2881
2864 2882 # compression code
2865 2883
2866 2884 class compressormanager(object):
2867 2885 """Holds registrations of various compression engines.
2868 2886
2869 2887 This class essentially abstracts the differences between compression
2870 2888 engines to allow new compression formats to be added easily, possibly from
2871 2889 extensions.
2872 2890
2873 2891 Compressors are registered against the global instance by calling its
2874 2892 ``register()`` method.
2875 2893 """
2876 2894 def __init__(self):
2877 2895 self._engines = {}
2878 2896 # Bundle spec human name to engine name.
2879 2897 self._bundlenames = {}
2880 2898 # Internal bundle identifier to engine name.
2881 2899 self._bundletypes = {}
2882 2900
2883 2901 def __getitem__(self, key):
2884 2902 return self._engines[key]
2885 2903
2886 2904 def __contains__(self, key):
2887 2905 return key in self._engines
2888 2906
2889 2907 def __iter__(self):
2890 2908 return iter(self._engines.keys())
2891 2909
2892 2910 def register(self, engine):
2893 2911 """Register a compression engine with the manager.
2894 2912
2895 2913 The argument must be a ``compressionengine`` instance.
2896 2914 """
2897 2915 if not isinstance(engine, compressionengine):
2898 2916 raise ValueError(_('argument must be a compressionengine'))
2899 2917
2900 2918 name = engine.name()
2901 2919
2902 2920 if name in self._engines:
2903 2921 raise error.Abort(_('compression engine %s already registered') %
2904 2922 name)
2905 2923
2906 2924 bundleinfo = engine.bundletype()
2907 2925 if bundleinfo:
2908 2926 bundlename, bundletype = bundleinfo
2909 2927
2910 2928 if bundlename in self._bundlenames:
2911 2929 raise error.Abort(_('bundle name %s already registered') %
2912 2930 bundlename)
2913 2931 if bundletype in self._bundletypes:
2914 2932 raise error.Abort(_('bundle type %s already registered by %s') %
2915 2933 (bundletype, self._bundletypes[bundletype]))
2916 2934
2917 2935 # No external facing name declared.
2918 2936 if bundlename:
2919 2937 self._bundlenames[bundlename] = name
2920 2938
2921 2939 self._bundletypes[bundletype] = name
2922 2940
2923 2941 self._engines[name] = engine
2924 2942
2925 2943 @property
2926 2944 def supportedbundlenames(self):
2927 2945 return set(self._bundlenames.keys())
2928 2946
2929 2947 @property
2930 2948 def supportedbundletypes(self):
2931 2949 return set(self._bundletypes.keys())
2932 2950
2933 2951 def forbundlename(self, bundlename):
2934 2952 """Obtain a compression engine registered to a bundle name.
2935 2953
2936 2954 Will raise KeyError if the bundle type isn't registered.
2937 2955 """
2938 2956 return self._engines[self._bundlenames[bundlename]]
2939 2957
2940 2958 def forbundletype(self, bundletype):
2941 2959 """Obtain a compression engine registered to a bundle type.
2942 2960
2943 2961 Will raise KeyError if the bundle type isn't registered.
2944 2962 """
2945 2963 return self._engines[self._bundletypes[bundletype]]
2946 2964
2947 2965 compengines = compressormanager()
2948 2966
2949 2967 class compressionengine(object):
2950 2968 """Base class for compression engines.
2951 2969
2952 2970 Compression engines must implement the interface defined by this class.
2953 2971 """
2954 2972 def name(self):
2955 2973 """Returns the name of the compression engine.
2956 2974
2957 2975 This is the key the engine is registered under.
2958 2976
2959 2977 This method must be implemented.
2960 2978 """
2961 2979 raise NotImplementedError()
2962 2980
2963 2981 def bundletype(self):
2964 2982 """Describes bundle identifiers for this engine.
2965 2983
2966 2984 If this compression engine isn't supported for bundles, returns None.
2967 2985
2968 2986 If this engine can be used for bundles, returns a 2-tuple of strings of
2969 2987 the user-facing "bundle spec" compression name and an internal
2970 2988 identifier used to denote the compression format within bundles. To
2971 2989 exclude the name from external usage, set the first element to ``None``.
2972 2990
2973 2991 If bundle compression is supported, the class must also implement
2974 2992 ``compressstream`` and `decompressorreader``.
2975 2993 """
2976 2994 return None
2977 2995
2978 2996 def compressstream(self, it, opts=None):
2979 2997 """Compress an iterator of chunks.
2980 2998
2981 2999 The method receives an iterator (ideally a generator) of chunks of
2982 3000 bytes to be compressed. It returns an iterator (ideally a generator)
2983 3001 of bytes of chunks representing the compressed output.
2984 3002
2985 3003 Optionally accepts an argument defining how to perform compression.
2986 3004 Each engine treats this argument differently.
2987 3005 """
2988 3006 raise NotImplementedError()
2989 3007
2990 3008 def decompressorreader(self, fh):
2991 3009 """Perform decompression on a file object.
2992 3010
2993 3011 Argument is an object with a ``read(size)`` method that returns
2994 3012 compressed data. Return value is an object with a ``read(size)`` that
2995 3013 returns uncompressed data.
2996 3014 """
2997 3015 raise NotImplementedError()
2998 3016
2999 3017 class _zlibengine(compressionengine):
3000 3018 def name(self):
3001 3019 return 'zlib'
3002 3020
3003 3021 def bundletype(self):
3004 3022 return 'gzip', 'GZ'
3005 3023
3006 3024 def compressstream(self, it, opts=None):
3007 3025 opts = opts or {}
3008 3026
3009 3027 z = zlib.compressobj(opts.get('level', -1))
3010 3028 for chunk in it:
3011 3029 data = z.compress(chunk)
3012 3030 # Not all calls to compress emit data. It is cheaper to inspect
3013 3031 # here than to feed empty chunks through generator.
3014 3032 if data:
3015 3033 yield data
3016 3034
3017 3035 yield z.flush()
3018 3036
3019 3037 def decompressorreader(self, fh):
3020 3038 def gen():
3021 3039 d = zlib.decompressobj()
3022 3040 for chunk in filechunkiter(fh):
3023 3041 yield d.decompress(chunk)
3024 3042
3025 3043 return chunkbuffer(gen())
3026 3044
3027 3045 compengines.register(_zlibengine())
3028 3046
3029 3047 class _bz2engine(compressionengine):
3030 3048 def name(self):
3031 3049 return 'bz2'
3032 3050
3033 3051 def bundletype(self):
3034 3052 return 'bzip2', 'BZ'
3035 3053
3036 3054 def compressstream(self, it, opts=None):
3037 3055 opts = opts or {}
3038 3056 z = bz2.BZ2Compressor(opts.get('level', 9))
3039 3057 for chunk in it:
3040 3058 data = z.compress(chunk)
3041 3059 if data:
3042 3060 yield data
3043 3061
3044 3062 yield z.flush()
3045 3063
3046 3064 def decompressorreader(self, fh):
3047 3065 def gen():
3048 3066 d = bz2.BZ2Decompressor()
3049 3067 for chunk in filechunkiter(fh):
3050 3068 yield d.decompress(chunk)
3051 3069
3052 3070 return chunkbuffer(gen())
3053 3071
3054 3072 compengines.register(_bz2engine())
3055 3073
3056 3074 class _truncatedbz2engine(compressionengine):
3057 3075 def name(self):
3058 3076 return 'bz2truncated'
3059 3077
3060 3078 def bundletype(self):
3061 3079 return None, '_truncatedBZ'
3062 3080
3063 3081 # We don't implement compressstream because it is hackily handled elsewhere.
3064 3082
3065 3083 def decompressorreader(self, fh):
3066 3084 def gen():
3067 3085 # The input stream doesn't have the 'BZ' header. So add it back.
3068 3086 d = bz2.BZ2Decompressor()
3069 3087 d.decompress('BZ')
3070 3088 for chunk in filechunkiter(fh):
3071 3089 yield d.decompress(chunk)
3072 3090
3073 3091 return chunkbuffer(gen())
3074 3092
3075 3093 compengines.register(_truncatedbz2engine())
3076 3094
3077 3095 class _noopengine(compressionengine):
3078 3096 def name(self):
3079 3097 return 'none'
3080 3098
3081 3099 def bundletype(self):
3082 3100 return 'none', 'UN'
3083 3101
3084 3102 def compressstream(self, it, opts=None):
3085 3103 return it
3086 3104
3087 3105 def decompressorreader(self, fh):
3088 3106 return fh
3089 3107
3090 3108 compengines.register(_noopengine())
3091 3109
3092 3110 # convenient shortcut
3093 3111 dst = debugstacktrace
General Comments 0
You need to be logged in to leave comments. Login now