author | Gregory Szorc <gregory.szorc@gmail.com> |
Wed, 29 Jun 2016 19:43:27 -0700 | |
changeset 29449 | 5b71a8d7f7ff |
parent 29433 | 33770d2b6cf9 |
child 29455 | 0c741fd6158a |
permissions | -rw-r--r-- |
17515 | 1 |
# util.py - Mercurial utility functions and platform specific implementations |
8226
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
2 |
# |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
3 |
# Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 |
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
5 |
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
6 |
# |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
7 |
# This software may be used and distributed according to the terms of the |
10263 | 8 |
# GNU General Public License version 2 or any later version. |
1082 | 9 |
|
17515 | 10 |
"""Mercurial utility functions and platform specific implementations. |
1082 | 11 |
|
8227
0a9542703300
turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents:
8226
diff
changeset
|
12 |
This contains helper routines that are independent of the SCM core and |
0a9542703300
turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents:
8226
diff
changeset
|
13 |
hide platform-specific details from the core. |
1082 | 14 |
""" |
419
28511fc21073
[PATCH] file seperator handling for the other 'OS'
mpm@selenic.com
parents:
diff
changeset
|
15 |
|
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
16 |
from __future__ import absolute_import |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
17 |
|
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
18 |
import bz2 |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
19 |
import calendar |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
20 |
import collections |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
21 |
import datetime |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
22 |
import errno |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
23 |
import gc |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
24 |
import hashlib |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
25 |
import imp |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
26 |
import os |
21907
7e5dfa00e3c2
util: rename 're' to 'remod'
Siddharth Agarwal <sid0@fb.com>
parents:
21857
diff
changeset
|
27 |
import re as remod |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
28 |
import shutil |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
29 |
import signal |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
30 |
import socket |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
31 |
import subprocess |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
32 |
import sys |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
33 |
import tempfile |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
34 |
import textwrap |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
35 |
import time |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
36 |
import traceback |
26266
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
37 |
import zlib |
3769 | 38 |
|
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
39 |
from . import ( |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
40 |
encoding, |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
41 |
error, |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
42 |
i18n, |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
43 |
osutil, |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
44 |
parsers, |
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
45 |
pycompat, |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
46 |
) |
3769 | 47 |
|
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
48 |
for attr in ( |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
49 |
'empty', |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29298
diff
changeset
|
50 |
'pickle', |
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
51 |
'queue', |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28865
diff
changeset
|
52 |
'urlerr', |
29431
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29394
diff
changeset
|
53 |
'urlparse', |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
54 |
# we do import urlreq, but we do it outside the loop |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
55 |
#'urlreq', |
28835
68a946e83188
pycompat: add util.stringio to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28832
diff
changeset
|
56 |
'stringio', |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29432
diff
changeset
|
57 |
'socketserver', |
29432
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
58 |
'xmlrpclib', |
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
59 |
): |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
60 |
globals()[attr] = getattr(pycompat, attr) |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
61 |
|
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
62 |
# This line is to make pyflakes happy: |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
63 |
urlreq = pycompat.urlreq |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
64 |
|
14912
ec46a7da9f2c
util: move windows and posix wildcard imports to begin of file
Adrian Buehlmann <adrian@cadifra.com>
parents:
14911
diff
changeset
|
65 |
if os.name == 'nt': |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
66 |
from . import windows as platform |
14912
ec46a7da9f2c
util: move windows and posix wildcard imports to begin of file
Adrian Buehlmann <adrian@cadifra.com>
parents:
14911
diff
changeset
|
67 |
else: |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
68 |
from . import posix as platform |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
69 |
|
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
70 |
_ = i18n._ |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
71 |
|
14927
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
72 |
cachestat = platform.cachestat |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
73 |
checkexec = platform.checkexec |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
74 |
checklink = platform.checklink |
15011
5e44e4b3a0a3
util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
15010
diff
changeset
|
75 |
copymode = platform.copymode |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
76 |
executablepath = platform.executablepath |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
77 |
expandglobs = platform.expandglobs |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
78 |
explainexit = platform.explainexit |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
79 |
findexe = platform.findexe |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
80 |
gethgcmd = platform.gethgcmd |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
81 |
getuser = platform.getuser |
28027
14033c5dd261
util: enable getpid to be replaced
timeless <timeless@mozdev.org>
parents:
27785
diff
changeset
|
82 |
getpid = os.getpid |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
83 |
groupmembers = platform.groupmembers |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
84 |
groupname = platform.groupname |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
85 |
hidewindow = platform.hidewindow |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
86 |
isexec = platform.isexec |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
87 |
isowner = platform.isowner |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
88 |
localpath = platform.localpath |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
89 |
lookupreg = platform.lookupreg |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
90 |
makedir = platform.makedir |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
91 |
nlinks = platform.nlinks |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
92 |
normpath = platform.normpath |
15488
6eff984d8e76
dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents:
15392
diff
changeset
|
93 |
normcase = platform.normcase |
24605
98744856b7d3
util: add normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24439
diff
changeset
|
94 |
normcasespec = platform.normcasespec |
98744856b7d3
util: add normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24439
diff
changeset
|
95 |
normcasefallback = platform.normcasefallback |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
96 |
openhardlinks = platform.openhardlinks |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
97 |
oslink = platform.oslink |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
98 |
parsepatchoutput = platform.parsepatchoutput |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
99 |
pconvert = platform.pconvert |
25420
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25406
diff
changeset
|
100 |
poll = platform.poll |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
101 |
popen = platform.popen |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
102 |
posixfile = platform.posixfile |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
103 |
quotecommand = platform.quotecommand |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21914
diff
changeset
|
104 |
readpipe = platform.readpipe |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
105 |
rename = platform.rename |
24692
144883a8d0d4
util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
106 |
removedirs = platform.removedirs |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
107 |
samedevice = platform.samedevice |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
108 |
samefile = platform.samefile |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
109 |
samestat = platform.samestat |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
110 |
setbinary = platform.setbinary |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
111 |
setflags = platform.setflags |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
112 |
setsignalhandler = platform.setsignalhandler |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
113 |
shellquote = platform.shellquote |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
114 |
spawndetached = platform.spawndetached |
17560
9ee25d7b1aed
util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents:
17537
diff
changeset
|
115 |
split = platform.split |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
116 |
sshargs = platform.sshargs |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18013
diff
changeset
|
117 |
statfiles = getattr(osutil, 'statfiles', platform.statfiles) |
18868
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18775
diff
changeset
|
118 |
statisexec = platform.statisexec |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18775
diff
changeset
|
119 |
statislink = platform.statislink |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
120 |
termwidth = platform.termwidth |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
121 |
testpid = platform.testpid |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
122 |
umask = platform.umask |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
123 |
unlink = platform.unlink |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
124 |
unlinkpath = platform.unlinkpath |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
125 |
username = platform.username |
14912
ec46a7da9f2c
util: move windows and posix wildcard imports to begin of file
Adrian Buehlmann <adrian@cadifra.com>
parents:
14911
diff
changeset
|
126 |
|
6470
ac0bcd951c2c
python 2.6 compatibility: compatibility wrappers for hash functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6339
diff
changeset
|
127 |
# Python compatibility |
3769 | 128 |
|
15656
4f5a78fa4917
util: clean up function ordering
Matt Mackall <mpm@selenic.com>
parents:
15611
diff
changeset
|
129 |
_notset = object() |
4f5a78fa4917
util: clean up function ordering
Matt Mackall <mpm@selenic.com>
parents:
15611
diff
changeset
|
130 |
|
27015
341cb90ffd18
util: disable floating point stat times (issue4836)
Matt Mackall <mpm@selenic.com>
parents:
26847
diff
changeset
|
131 |
# disable Python's problematic floating point timestamps (issue4836) |
341cb90ffd18
util: disable floating point stat times (issue4836)
Matt Mackall <mpm@selenic.com>
parents:
26847
diff
changeset
|
132 |
# (Python hypocritically says you shouldn't change this behavior in |
341cb90ffd18
util: disable floating point stat times (issue4836)
Matt Mackall <mpm@selenic.com>
parents:
26847
diff
changeset
|
133 |
# libraries, and sure enough Mercurial is not a library.) |
341cb90ffd18
util: disable floating point stat times (issue4836)
Matt Mackall <mpm@selenic.com>
parents:
26847
diff
changeset
|
134 |
os.stat_float_times(False) |
341cb90ffd18
util: disable floating point stat times (issue4836)
Matt Mackall <mpm@selenic.com>
parents:
26847
diff
changeset
|
135 |
|
15656
4f5a78fa4917
util: clean up function ordering
Matt Mackall <mpm@selenic.com>
parents:
15611
diff
changeset
|
136 |
def safehasattr(thing, attr): |
4f5a78fa4917
util: clean up function ordering
Matt Mackall <mpm@selenic.com>
parents:
15611
diff
changeset
|
137 |
return getattr(thing, attr, _notset) is not _notset |
4f5a78fa4917
util: clean up function ordering
Matt Mackall <mpm@selenic.com>
parents:
15611
diff
changeset
|
138 |
|
22962
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
139 |
DIGESTS = { |
29342
c27dc3c31222
util: drop local aliases for md5, sha1, sha256, and sha512
Augie Fackler <raf@durin42.com>
parents:
29324
diff
changeset
|
140 |
'md5': hashlib.md5, |
c27dc3c31222
util: drop local aliases for md5, sha1, sha256, and sha512
Augie Fackler <raf@durin42.com>
parents:
29324
diff
changeset
|
141 |
'sha1': hashlib.sha1, |
c27dc3c31222
util: drop local aliases for md5, sha1, sha256, and sha512
Augie Fackler <raf@durin42.com>
parents:
29324
diff
changeset
|
142 |
'sha512': hashlib.sha512, |
22962
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
143 |
} |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
144 |
# List of digest types from strongest to weakest |
27357
7f5a0bd4c9aa
util: make hashlib import unconditional
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27112
diff
changeset
|
145 |
DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5'] |
22962
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
146 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
147 |
for k in DIGESTS_BY_STRENGTH: |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
148 |
assert k in DIGESTS |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
149 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
150 |
class digester(object): |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
151 |
"""helper to compute digests. |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
152 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
153 |
This helper can be used to compute one or more digests given their name. |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
154 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
155 |
>>> d = digester(['md5', 'sha1']) |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
156 |
>>> d.update('foo') |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
157 |
>>> [k for k in sorted(d)] |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
158 |
['md5', 'sha1'] |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
159 |
>>> d['md5'] |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
160 |
'acbd18db4cc2f85cedef654fccc4a4d8' |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
161 |
>>> d['sha1'] |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
162 |
'0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
163 |
>>> digester.preferred(['md5', 'sha1']) |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
164 |
'sha1' |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
165 |
""" |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
166 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
167 |
def __init__(self, digests, s=''): |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
168 |
self._hashes = {} |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
169 |
for k in digests: |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
170 |
if k not in DIGESTS: |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
171 |
raise Abort(_('unknown digest type: %s') % k) |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
172 |
self._hashes[k] = DIGESTS[k]() |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
173 |
if s: |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
174 |
self.update(s) |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
175 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
176 |
def update(self, data): |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
177 |
for h in self._hashes.values(): |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
178 |
h.update(data) |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
179 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
180 |
def __getitem__(self, key): |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
181 |
if key not in DIGESTS: |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
182 |
raise Abort(_('unknown digest type: %s') % k) |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
183 |
return self._hashes[key].hexdigest() |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
184 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
185 |
def __iter__(self): |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
186 |
return iter(self._hashes) |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
187 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
188 |
@staticmethod |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
189 |
def preferred(supported): |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
190 |
"""returns the strongest digest type in both supported and DIGESTS.""" |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
191 |
|
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
192 |
for k in DIGESTS_BY_STRENGTH: |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
193 |
if k in supported: |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
194 |
return k |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
195 |
return None |
4d58f4083148
util: add a helper class to compute digests
Mike Hommey <mh@glandium.org>
parents:
22958
diff
changeset
|
196 |
|
22963
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
197 |
class digestchecker(object): |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
198 |
"""file handle wrapper that additionally checks content against a given |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
199 |
size and digests. |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
200 |
|
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
201 |
d = digestchecker(fh, size, {'md5': '...'}) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
202 |
|
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
203 |
When multiple digests are given, all of them are validated. |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
204 |
""" |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
205 |
|
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
206 |
def __init__(self, fh, size, digests): |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
207 |
self._fh = fh |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
208 |
self._size = size |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
209 |
self._got = 0 |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
210 |
self._digests = dict(digests) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
211 |
self._digester = digester(self._digests.keys()) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
212 |
|
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
213 |
def read(self, length=-1): |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
214 |
content = self._fh.read(length) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
215 |
self._digester.update(content) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
216 |
self._got += len(content) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
217 |
return content |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
218 |
|
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
219 |
def validate(self): |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
220 |
if self._size != self._got: |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
221 |
raise Abort(_('size mismatch: expected %d, got %d') % |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
222 |
(self._size, self._got)) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
223 |
for k, v in self._digests.items(): |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
224 |
if v != self._digester[k]: |
23076
c312ef382033
i18n: add hint to digest mismatch message
Wagner Bruna <wbruna@softwareexpress.com.br>
parents:
23030
diff
changeset
|
225 |
# i18n: first parameter is a digest name |
22963
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
226 |
raise Abort(_('%s mismatch: expected %s, got %s') % |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
227 |
(k, v, self._digester[k])) |
56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
Mike Hommey <mh@glandium.org>
parents:
22962
diff
changeset
|
228 |
|
11565
7546d4a272c8
util: improved the check for the existence of the 'buffer' builtin
Renato Cunha <renatoc@gmail.com>
parents:
11469
diff
changeset
|
229 |
try: |
15657
d976b1ef6760
util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents:
15656
diff
changeset
|
230 |
buffer = buffer |
11565
7546d4a272c8
util: improved the check for the existence of the 'buffer' builtin
Renato Cunha <renatoc@gmail.com>
parents:
11469
diff
changeset
|
231 |
except NameError: |
15657
d976b1ef6760
util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents:
15656
diff
changeset
|
232 |
if sys.version_info[0] < 3: |
d976b1ef6760
util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents:
15656
diff
changeset
|
233 |
def buffer(sliceable, offset=0): |
d976b1ef6760
util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents:
15656
diff
changeset
|
234 |
return sliceable[offset:] |
d976b1ef6760
util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents:
15656
diff
changeset
|
235 |
else: |
d976b1ef6760
util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents:
15656
diff
changeset
|
236 |
def buffer(sliceable, offset=0): |
d976b1ef6760
util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents:
15656
diff
changeset
|
237 |
return memoryview(sliceable)[offset:] |
10756
cb681cc59a8d
util: fake the builtin buffer if it's missing (jython)
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
10487
diff
changeset
|
238 |
|
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
239 |
closefds = os.name == 'posix' |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
240 |
|
25406
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
241 |
_chunksize = 4096 |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
242 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
243 |
class bufferedinputpipe(object): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
244 |
"""a manually buffered input pipe |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
245 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
246 |
Python will not let us use buffered IO and lazy reading with 'polling' at |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
247 |
the same time. We cannot probe the buffer state and select will not detect |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
248 |
that data are ready to read if they are already buffered. |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
249 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
250 |
This class let us work around that by implementing its own buffering |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
251 |
(allowing efficient readline) while offering a way to know if the buffer is |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
252 |
empty from the output (allowing collaboration of the buffer with polling). |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
253 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
254 |
This class lives in the 'util' module because it makes use of the 'os' |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
255 |
module from the python stdlib. |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
256 |
""" |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
257 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
258 |
def __init__(self, input): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
259 |
self._input = input |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
260 |
self._buffer = [] |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
261 |
self._eof = False |
25672
050dc6eabc92
bufferedinputpipe: remove N^2 computation of buffer length (issue4735)
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25671
diff
changeset
|
262 |
self._lenbuf = 0 |
25406
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
263 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
264 |
@property |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
265 |
def hasbuffer(self): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
266 |
"""True is any data is currently buffered |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
267 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
268 |
This will be used externally a pre-step for polling IO. If there is |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
269 |
already data then no polling should be set in place.""" |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
270 |
return bool(self._buffer) |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
271 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
272 |
@property |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
273 |
def closed(self): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
274 |
return self._input.closed |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
275 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
276 |
def fileno(self): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
277 |
return self._input.fileno() |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
278 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
279 |
def close(self): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
280 |
return self._input.close() |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
281 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
282 |
def read(self, size): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
283 |
while (not self._eof) and (self._lenbuf < size): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
284 |
self._fillbuffer() |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
285 |
return self._frombuffer(size) |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
286 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
287 |
def readline(self, *args, **kwargs): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
288 |
if 1 < len(self._buffer): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
289 |
# this should not happen because both read and readline end with a |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
290 |
# _frombuffer call that collapse it. |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
291 |
self._buffer = [''.join(self._buffer)] |
25672
050dc6eabc92
bufferedinputpipe: remove N^2 computation of buffer length (issue4735)
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25671
diff
changeset
|
292 |
self._lenbuf = len(self._buffer[0]) |
25406
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
293 |
lfi = -1 |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
294 |
if self._buffer: |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
295 |
lfi = self._buffer[-1].find('\n') |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
296 |
while (not self._eof) and lfi < 0: |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
297 |
self._fillbuffer() |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
298 |
if self._buffer: |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
299 |
lfi = self._buffer[-1].find('\n') |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
300 |
size = lfi + 1 |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
301 |
if lfi < 0: # end of file |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
302 |
size = self._lenbuf |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
303 |
elif 1 < len(self._buffer): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
304 |
# we need to take previous chunks into account |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
305 |
size += self._lenbuf - len(self._buffer[-1]) |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
306 |
return self._frombuffer(size) |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
307 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
308 |
def _frombuffer(self, size): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
309 |
"""return at most 'size' data from the buffer |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
310 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
311 |
The data are removed from the buffer.""" |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
312 |
if size == 0 or not self._buffer: |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
313 |
return '' |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
314 |
buf = self._buffer[0] |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
315 |
if 1 < len(self._buffer): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
316 |
buf = ''.join(self._buffer) |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
317 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
318 |
data = buf[:size] |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
319 |
buf = buf[len(data):] |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
320 |
if buf: |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
321 |
self._buffer = [buf] |
25672
050dc6eabc92
bufferedinputpipe: remove N^2 computation of buffer length (issue4735)
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25671
diff
changeset
|
322 |
self._lenbuf = len(buf) |
25406
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
323 |
else: |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
324 |
self._buffer = [] |
25672
050dc6eabc92
bufferedinputpipe: remove N^2 computation of buffer length (issue4735)
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25671
diff
changeset
|
325 |
self._lenbuf = 0 |
25406
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
326 |
return data |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
327 |
|
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
328 |
def _fillbuffer(self): |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
329 |
"""read data to the buffer""" |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
330 |
data = os.read(self._input.fileno(), _chunksize) |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
331 |
if not data: |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
332 |
self._eof = True |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
333 |
else: |
25672
050dc6eabc92
bufferedinputpipe: remove N^2 computation of buffer length (issue4735)
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25671
diff
changeset
|
334 |
self._lenbuf += len(data) |
25406
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
335 |
self._buffer.append(data) |
be930f16a52a
util: introduce a bufferedinputpipe utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25245
diff
changeset
|
336 |
|
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
337 |
def popen2(cmd, env=None, newlines=False): |
9089
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
338 |
# Setting bufsize to -1 lets the system decide the buffer size. |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
339 |
# The default for bufsize is 0, meaning unbuffered. This leads to |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
340 |
# poor performance on Mac OS X: http://bugs.python.org/issue4194 |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
341 |
p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
9083
ec171737aaf1
Backed out changeset fce065538bcf: it caused a 5x performance regression on OS X
Bryan O'Sullivan <bos@serpentine.com>
parents:
8340
diff
changeset
|
342 |
close_fds=closefds, |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
343 |
stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
344 |
universal_newlines=newlines, |
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
345 |
env=env) |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
346 |
return p.stdin, p.stdout |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
347 |
|
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
348 |
def popen3(cmd, env=None, newlines=False): |
18759
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
349 |
stdin, stdout, stderr, p = popen4(cmd, env, newlines) |
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
350 |
return stdin, stdout, stderr |
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
351 |
|
25245
504ef9c49f4a
util: allow to specify buffer size in popen4
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25211
diff
changeset
|
352 |
def popen4(cmd, env=None, newlines=False, bufsize=-1): |
504ef9c49f4a
util: allow to specify buffer size in popen4
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25211
diff
changeset
|
353 |
p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, |
9083
ec171737aaf1
Backed out changeset fce065538bcf: it caused a 5x performance regression on OS X
Bryan O'Sullivan <bos@serpentine.com>
parents:
8340
diff
changeset
|
354 |
close_fds=closefds, |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
355 |
stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
356 |
stderr=subprocess.PIPE, |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
357 |
universal_newlines=newlines, |
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
358 |
env=env) |
18759
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
359 |
return p.stdin, p.stdout, p.stderr, p |
7106
4674706b5b95
python2.6: use subprocess if available
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
360 |
|
7632 | 361 |
def version(): |
362 |
"""Return version information if available.""" |
|
363 |
try: |
|
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
364 |
from . import __version__ |
7632 | 365 |
return __version__.version |
366 |
except ImportError: |
|
367 |
return 'unknown' |
|
368 |
||
27112
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
369 |
def versiontuple(v=None, n=4): |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
370 |
"""Parses a Mercurial version string into an N-tuple. |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
371 |
|
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
372 |
The version string to be parsed is specified with the ``v`` argument. |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
373 |
If it isn't defined, the current Mercurial version string will be parsed. |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
374 |
|
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
375 |
``n`` can be 2, 3, or 4. Here is how some version strings map to |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
376 |
returned values: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
377 |
|
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
378 |
>>> v = '3.6.1+190-df9b73d2d444' |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
379 |
>>> versiontuple(v, 2) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
380 |
(3, 6) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
381 |
>>> versiontuple(v, 3) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
382 |
(3, 6, 1) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
383 |
>>> versiontuple(v, 4) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
384 |
(3, 6, 1, '190-df9b73d2d444') |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
385 |
|
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
386 |
>>> versiontuple('3.6.1+190-df9b73d2d444+20151118') |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
387 |
(3, 6, 1, '190-df9b73d2d444+20151118') |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
388 |
|
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
389 |
>>> v = '3.6' |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
390 |
>>> versiontuple(v, 2) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
391 |
(3, 6) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
392 |
>>> versiontuple(v, 3) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
393 |
(3, 6, None) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
394 |
>>> versiontuple(v, 4) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
395 |
(3, 6, None, None) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
396 |
""" |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
397 |
if not v: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
398 |
v = version() |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
399 |
parts = v.split('+', 1) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
400 |
if len(parts) == 1: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
401 |
vparts, extra = parts[0], None |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
402 |
else: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
403 |
vparts, extra = parts |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
404 |
|
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
405 |
vints = [] |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
406 |
for i in vparts.split('.'): |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
407 |
try: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
408 |
vints.append(int(i)) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
409 |
except ValueError: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
410 |
break |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
411 |
# (3, 6) -> (3, 6, None) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
412 |
while len(vints) < 3: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
413 |
vints.append(None) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
414 |
|
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
415 |
if n == 2: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
416 |
return (vints[0], vints[1]) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
417 |
if n == 3: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
418 |
return (vints[0], vints[1], vints[2]) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
419 |
if n == 4: |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
420 |
return (vints[0], vints[1], vints[2], extra) |
39c14e89b881
util: add versiontuple() for returning parsed version information
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27066
diff
changeset
|
421 |
|
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
422 |
# used by parsedate |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
423 |
defaultdateformats = ( |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
424 |
'%Y-%m-%d %H:%M:%S', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
425 |
'%Y-%m-%d %I:%M:%S%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
426 |
'%Y-%m-%d %H:%M', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
427 |
'%Y-%m-%d %I:%M%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
428 |
'%Y-%m-%d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
429 |
'%m-%d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
430 |
'%m/%d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
431 |
'%m/%d/%y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
432 |
'%m/%d/%Y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
433 |
'%a %b %d %H:%M:%S %Y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
434 |
'%a %b %d %I:%M:%S%p %Y', |
4708
01f9ee4de1ad
Add support for RFC2822 to util.parsedate().
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents:
4686
diff
changeset
|
435 |
'%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
436 |
'%b %d %H:%M:%S %Y', |
3812 | 437 |
'%b %d %I:%M:%S%p %Y', |
438 |
'%b %d %H:%M:%S', |
|
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
439 |
'%b %d %I:%M:%S%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
440 |
'%b %d %H:%M', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
441 |
'%b %d %I:%M%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
442 |
'%b %d %Y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
443 |
'%b %d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
444 |
'%H:%M:%S', |
9383
7116494c48ab
util: Fix date format for 12-hour time.
Carey Evans <carey@carey.geek.nz>
parents:
9097
diff
changeset
|
445 |
'%I:%M:%S%p', |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
446 |
'%H:%M', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
447 |
'%I:%M%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
448 |
) |
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
449 |
|
3812 | 450 |
extendeddateformats = defaultdateformats + ( |
451 |
"%Y", |
|
452 |
"%Y-%m", |
|
453 |
"%b", |
|
454 |
"%b %Y", |
|
455 |
) |
|
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
456 |
|
3145
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
457 |
def cachefunc(func): |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
458 |
'''cache the result of function calls''' |
3147
97420a49188d
add comments in cachefunc
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3145
diff
changeset
|
459 |
# XXX doesn't handle keywords args |
28832
f5ff10f6fa6b
util: use __code__ (available since py2.6)
timeless <timeless@mozdev.org>
parents:
28826
diff
changeset
|
460 |
if func.__code__.co_argcount == 0: |
20835
0e8417131a29
util: add the code path to "cachefunc()" for the function taking no arguments
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20679
diff
changeset
|
461 |
cache = [] |
0e8417131a29
util: add the code path to "cachefunc()" for the function taking no arguments
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20679
diff
changeset
|
462 |
def f(): |
0e8417131a29
util: add the code path to "cachefunc()" for the function taking no arguments
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20679
diff
changeset
|
463 |
if len(cache) == 0: |
0e8417131a29
util: add the code path to "cachefunc()" for the function taking no arguments
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20679
diff
changeset
|
464 |
cache.append(func()) |
0e8417131a29
util: add the code path to "cachefunc()" for the function taking no arguments
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20679
diff
changeset
|
465 |
return cache[0] |
0e8417131a29
util: add the code path to "cachefunc()" for the function taking no arguments
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20679
diff
changeset
|
466 |
return f |
3145
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
467 |
cache = {} |
28832
f5ff10f6fa6b
util: use __code__ (available since py2.6)
timeless <timeless@mozdev.org>
parents:
28826
diff
changeset
|
468 |
if func.__code__.co_argcount == 1: |
3147
97420a49188d
add comments in cachefunc
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3145
diff
changeset
|
469 |
# we gain a small amount of time because |
97420a49188d
add comments in cachefunc
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3145
diff
changeset
|
470 |
# we don't need to pack/unpack the list |
3145
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
471 |
def f(arg): |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
472 |
if arg not in cache: |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
473 |
cache[arg] = func(arg) |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
474 |
return cache[arg] |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
475 |
else: |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
476 |
def f(*args): |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
477 |
if args not in cache: |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
478 |
cache[args] = func(*args) |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
479 |
return cache[args] |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
480 |
|
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
481 |
return f |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
482 |
|
21813
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
483 |
class sortdict(dict): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
484 |
'''a simple sorted dictionary''' |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
485 |
def __init__(self, data=None): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
486 |
self._list = [] |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
487 |
if data: |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
488 |
self.update(data) |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
489 |
def copy(self): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
490 |
return sortdict(self) |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
491 |
def __setitem__(self, key, val): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
492 |
if key in self: |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
493 |
self._list.remove(key) |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
494 |
self._list.append(key) |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
495 |
dict.__setitem__(self, key, val) |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
496 |
def __iter__(self): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
497 |
return self._list.__iter__() |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
498 |
def update(self, src): |
24236
de14c3972c2f
sortdict: have update() accept either dict or iterable of key/value pairs
Yuya Nishihara <yuya@tcha.org>
parents:
24188
diff
changeset
|
499 |
if isinstance(src, dict): |
de14c3972c2f
sortdict: have update() accept either dict or iterable of key/value pairs
Yuya Nishihara <yuya@tcha.org>
parents:
24188
diff
changeset
|
500 |
src = src.iteritems() |
de14c3972c2f
sortdict: have update() accept either dict or iterable of key/value pairs
Yuya Nishihara <yuya@tcha.org>
parents:
24188
diff
changeset
|
501 |
for k, v in src: |
de14c3972c2f
sortdict: have update() accept either dict or iterable of key/value pairs
Yuya Nishihara <yuya@tcha.org>
parents:
24188
diff
changeset
|
502 |
self[k] = v |
21813
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
503 |
def clear(self): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
504 |
dict.clear(self) |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
505 |
self._list = [] |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
506 |
def items(self): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
507 |
return [(k, self[k]) for k in self._list] |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
508 |
def __delitem__(self, key): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
509 |
dict.__delitem__(self, key) |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
510 |
self._list.remove(key) |
22643
3b1c0e1ede4c
util: fix sorteddict.pop
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
22638
diff
changeset
|
511 |
def pop(self, key, *args, **kwargs): |
3b1c0e1ede4c
util: fix sorteddict.pop
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
22638
diff
changeset
|
512 |
dict.pop(self, key, *args, **kwargs) |
3b1c0e1ede4c
util: fix sorteddict.pop
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
22638
diff
changeset
|
513 |
try: |
3b1c0e1ede4c
util: fix sorteddict.pop
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
22638
diff
changeset
|
514 |
self._list.remove(key) |
3b1c0e1ede4c
util: fix sorteddict.pop
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
22638
diff
changeset
|
515 |
except ValueError: |
3b1c0e1ede4c
util: fix sorteddict.pop
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
22638
diff
changeset
|
516 |
pass |
21813
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
517 |
def keys(self): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
518 |
return self._list |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
519 |
def iterkeys(self): |
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
520 |
return self._list.__iter__() |
23260
565f97e71ce3
sortdict: add iteritems method
Sean Farley <sean.michael.farley@gmail.com>
parents:
23139
diff
changeset
|
521 |
def iteritems(self): |
565f97e71ce3
sortdict: add iteritems method
Sean Farley <sean.michael.farley@gmail.com>
parents:
23139
diff
changeset
|
522 |
for k in self._list: |
565f97e71ce3
sortdict: add iteritems method
Sean Farley <sean.michael.farley@gmail.com>
parents:
23139
diff
changeset
|
523 |
yield k, self[k] |
23261
79858e66a7ce
sortdict: add insert method
Sean Farley <sean.michael.farley@gmail.com>
parents:
23260
diff
changeset
|
524 |
def insert(self, index, key, val): |
79858e66a7ce
sortdict: add insert method
Sean Farley <sean.michael.farley@gmail.com>
parents:
23260
diff
changeset
|
525 |
self._list.insert(index, key) |
79858e66a7ce
sortdict: add insert method
Sean Farley <sean.michael.farley@gmail.com>
parents:
23260
diff
changeset
|
526 |
dict.__setitem__(self, key, val) |
21813
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
21046
diff
changeset
|
527 |
|
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
528 |
class _lrucachenode(object): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
529 |
"""A node in a doubly linked list. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
530 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
531 |
Holds a reference to nodes on either side as well as a key-value |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
532 |
pair for the dictionary entry. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
533 |
""" |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
534 |
__slots__ = ('next', 'prev', 'key', 'value') |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
535 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
536 |
def __init__(self): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
537 |
self.next = None |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
538 |
self.prev = None |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
539 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
540 |
self.key = _notset |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
541 |
self.value = None |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
542 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
543 |
def markempty(self): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
544 |
"""Mark the node as emptied.""" |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
545 |
self.key = _notset |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
546 |
|
18603
2251b3184e6e
util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
18537
diff
changeset
|
547 |
class lrucachedict(object): |
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
548 |
"""Dict that caches most recent accesses and sets. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
549 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
550 |
The dict consists of an actual backing dict - indexed by original |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
551 |
key - and a doubly linked circular list defining the order of entries in |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
552 |
the cache. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
553 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
554 |
The head node is the newest entry in the cache. If the cache is full, |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
555 |
we recycle head.prev and make it the new head. Cache accesses result in |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
556 |
the node being moved to before the existing head and being marked as the |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
557 |
new head node. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
558 |
""" |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
559 |
def __init__(self, max): |
18603
2251b3184e6e
util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
18537
diff
changeset
|
560 |
self._cache = {} |
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
561 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
562 |
self._head = head = _lrucachenode() |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
563 |
head.prev = head |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
564 |
head.next = head |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
565 |
self._size = 1 |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
566 |
self._capacity = max |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
567 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
568 |
def __len__(self): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
569 |
return len(self._cache) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
570 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
571 |
def __contains__(self, k): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
572 |
return k in self._cache |
18603
2251b3184e6e
util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
18537
diff
changeset
|
573 |
|
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
574 |
def __iter__(self): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
575 |
# We don't have to iterate in cache order, but why not. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
576 |
n = self._head |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
577 |
for i in range(len(self._cache)): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
578 |
yield n.key |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
579 |
n = n.next |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
580 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
581 |
def __getitem__(self, k): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
582 |
node = self._cache[k] |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
583 |
self._movetohead(node) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
584 |
return node.value |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
585 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
586 |
def __setitem__(self, k, v): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
587 |
node = self._cache.get(k) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
588 |
# Replace existing value and mark as newest. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
589 |
if node is not None: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
590 |
node.value = v |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
591 |
self._movetohead(node) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
592 |
return |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
593 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
594 |
if self._size < self._capacity: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
595 |
node = self._addcapacity() |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
596 |
else: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
597 |
# Grab the last/oldest item. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
598 |
node = self._head.prev |
18603
2251b3184e6e
util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
18537
diff
changeset
|
599 |
|
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
600 |
# At capacity. Kill the old entry. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
601 |
if node.key is not _notset: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
602 |
del self._cache[node.key] |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
603 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
604 |
node.key = k |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
605 |
node.value = v |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
606 |
self._cache[k] = node |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
607 |
# And mark it as newest entry. No need to adjust order since it |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
608 |
# is already self._head.prev. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
609 |
self._head = node |
18603
2251b3184e6e
util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
18537
diff
changeset
|
610 |
|
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
611 |
def __delitem__(self, k): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
612 |
node = self._cache.pop(k) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
613 |
node.markempty() |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
614 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
615 |
# Temporarily mark as newest item before re-adjusting head to make |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
616 |
# this node the oldest item. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
617 |
self._movetohead(node) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
618 |
self._head = node.next |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
619 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
620 |
# Additional dict methods. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
621 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
622 |
def get(self, k, default=None): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
623 |
try: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
624 |
return self._cache[k] |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
625 |
except KeyError: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
626 |
return default |
18603
2251b3184e6e
util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
18537
diff
changeset
|
627 |
|
19710
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
19461
diff
changeset
|
628 |
def clear(self): |
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
629 |
n = self._head |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
630 |
while n.key is not _notset: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
631 |
n.markempty() |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
632 |
n = n.next |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
633 |
|
19710
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
19461
diff
changeset
|
634 |
self._cache.clear() |
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
635 |
|
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
636 |
def copy(self): |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
637 |
result = lrucachedict(self._capacity) |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
638 |
n = self._head.prev |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
639 |
# Iterate in oldest-to-newest order, so the copy has the right ordering |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
640 |
for i in range(len(self._cache)): |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
641 |
result[n.key] = n.value |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
642 |
n = n.prev |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
643 |
return result |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27391
diff
changeset
|
644 |
|
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
645 |
def _movetohead(self, node): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
646 |
"""Mark a node as the newest, making it the new head. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
647 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
648 |
When a node is accessed, it becomes the freshest entry in the LRU |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
649 |
list, which is denoted by self._head. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
650 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
651 |
Visually, let's make ``N`` the new head node (* denotes head): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
652 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
653 |
previous/oldest <-> head <-> next/next newest |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
654 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
655 |
----<->--- A* ---<->----- |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
656 |
| | |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
657 |
E <-> D <-> N <-> C <-> B |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
658 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
659 |
To: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
660 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
661 |
----<->--- N* ---<->----- |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
662 |
| | |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
663 |
E <-> D <-> C <-> B <-> A |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
664 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
665 |
This requires the following moves: |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
666 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
667 |
C.next = D (node.prev.next = node.next) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
668 |
D.prev = C (node.next.prev = node.prev) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
669 |
E.next = N (head.prev.next = node) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
670 |
N.prev = E (node.prev = head.prev) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
671 |
N.next = A (node.next = head) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
672 |
A.prev = N (head.prev = node) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
673 |
""" |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
674 |
head = self._head |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
675 |
# C.next = D |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
676 |
node.prev.next = node.next |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
677 |
# D.prev = C |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
678 |
node.next.prev = node.prev |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
679 |
# N.prev = E |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
680 |
node.prev = head.prev |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
681 |
# N.next = A |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
682 |
# It is tempting to do just "head" here, however if node is |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
683 |
# adjacent to head, this will do bad things. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
684 |
node.next = head.prev.next |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
685 |
# E.next = N |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
686 |
node.next.prev = node |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
687 |
# A.prev = N |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
688 |
node.prev.next = node |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
689 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
690 |
self._head = node |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
691 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
692 |
def _addcapacity(self): |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
693 |
"""Add a node to the circular linked list. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
694 |
|
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
695 |
The new node is inserted before the head node. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
696 |
""" |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
697 |
head = self._head |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
698 |
node = _lrucachenode() |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
699 |
head.prev.next = node |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
700 |
node.prev = head.prev |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
701 |
node.next = head |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
702 |
head.prev = node |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
703 |
self._size += 1 |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27363
diff
changeset
|
704 |
return node |
19710
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
19461
diff
changeset
|
705 |
|
9097
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
706 |
def lrucachefunc(func): |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
707 |
'''cache most recent results of function calls''' |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
708 |
cache = {} |
25113
0ca8410ea345
util: drop alias for collections.deque
Martin von Zweigbergk <martinvonz@google.com>
parents:
25112
diff
changeset
|
709 |
order = collections.deque() |
28832
f5ff10f6fa6b
util: use __code__ (available since py2.6)
timeless <timeless@mozdev.org>
parents:
28826
diff
changeset
|
710 |
if func.__code__.co_argcount == 1: |
9097
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
711 |
def f(arg): |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
712 |
if arg not in cache: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
713 |
if len(cache) > 20: |
16803
107a3270a24a
cleanup: use the deque type where appropriate
Bryan O'Sullivan <bryano@fb.com>
parents:
16769
diff
changeset
|
714 |
del cache[order.popleft()] |
9097
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
715 |
cache[arg] = func(arg) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
716 |
else: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
717 |
order.remove(arg) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
718 |
order.append(arg) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
719 |
return cache[arg] |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
720 |
else: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
721 |
def f(*args): |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
722 |
if args not in cache: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
723 |
if len(cache) > 20: |
16803
107a3270a24a
cleanup: use the deque type where appropriate
Bryan O'Sullivan <bryano@fb.com>
parents:
16769
diff
changeset
|
724 |
del cache[order.popleft()] |
9097
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
725 |
cache[args] = func(*args) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
726 |
else: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
727 |
order.remove(args) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
728 |
order.append(args) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
729 |
return cache[args] |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
730 |
|
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
731 |
return f |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
732 |
|
8207
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
733 |
class propertycache(object): |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
734 |
def __init__(self, func): |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
735 |
self.func = func |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
736 |
self.name = func.__name__ |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
737 |
def __get__(self, obj, type=None): |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
738 |
result = self.func(obj) |
18013
98c867ac1330
clfilter: add a propertycache that must be unfiltered
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
17962
diff
changeset
|
739 |
self.cachevalue(obj, result) |
8207
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
740 |
return result |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
741 |
|
18013
98c867ac1330
clfilter: add a propertycache that must be unfiltered
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
17962
diff
changeset
|
742 |
def cachevalue(self, obj, value): |
19951
d51c4d85ec23
spelling: random spell checker fixes
Mads Kiilerich <madski@unity3d.com>
parents:
19852
diff
changeset
|
743 |
# __dict__ assignment required to bypass __setattr__ (eg: repoview) |
19845
a1237a4b437d
repoview: make propertycache.setcache compatible with repoview
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
19461
diff
changeset
|
744 |
obj.__dict__[self.name] = value |
18013
98c867ac1330
clfilter: add a propertycache that must be unfiltered
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
17962
diff
changeset
|
745 |
|
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
746 |
def pipefilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
747 |
'''filter string S through command CMD, returning its output''' |
8302
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
748 |
p = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
749 |
stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
750 |
pout, perr = p.communicate(s) |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
751 |
return pout |
419
28511fc21073
[PATCH] file seperator handling for the other 'OS'
mpm@selenic.com
parents:
diff
changeset
|
752 |
|
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
753 |
def tempfilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
754 |
'''filter string S through a pair of temporary files with CMD. |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
755 |
CMD is used as a template to create the real command to be run, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
756 |
with the strings INFILE and OUTFILE replaced by the real names of |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
757 |
the temporary files generated.''' |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
758 |
inname, outname = None, None |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
759 |
try: |
2165
d821918e3bee
Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2153
diff
changeset
|
760 |
infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
761 |
fp = os.fdopen(infd, 'wb') |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
762 |
fp.write(s) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
763 |
fp.close() |
2165
d821918e3bee
Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2153
diff
changeset
|
764 |
outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
765 |
os.close(outfd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
766 |
cmd = cmd.replace('INFILE', inname) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
767 |
cmd = cmd.replace('OUTFILE', outname) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
768 |
code = os.system(cmd) |
4720
72fb6f10fac1
OpenVMS patches
Jean-Francois PIERONNE <jf.pieronne@laposte.net>
parents:
4708
diff
changeset
|
769 |
if sys.platform == 'OpenVMS' and code & 1: |
72fb6f10fac1
OpenVMS patches
Jean-Francois PIERONNE <jf.pieronne@laposte.net>
parents:
4708
diff
changeset
|
770 |
code = 0 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
771 |
if code: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
772 |
raise Abort(_("command '%s' failed: %s") % |
14234
600e64004eb5
rename explain_exit to explainexit
Adrian Buehlmann <adrian@cadifra.com>
parents:
14230
diff
changeset
|
773 |
(cmd, explainexit(code))) |
27768
5ef99738a562
util: replace file I/O with readfile
Bryan O'Sullivan <bryano@fb.com>
parents:
27766
diff
changeset
|
774 |
return readfile(outname) |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
775 |
finally: |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
776 |
try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
777 |
if inname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
778 |
os.unlink(inname) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13985
diff
changeset
|
779 |
except OSError: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
780 |
pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
781 |
try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
782 |
if outname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
783 |
os.unlink(outname) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13985
diff
changeset
|
784 |
except OSError: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
785 |
pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
786 |
|
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
787 |
filtertable = { |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
788 |
'tempfile:': tempfilter, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
789 |
'pipe:': pipefilter, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
790 |
} |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
791 |
|
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
792 |
def filter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
793 |
"filter a string through a command that transforms its input to its output" |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
794 |
for name, fn in filtertable.iteritems(): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
795 |
if cmd.startswith(name): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
796 |
return fn(s, cmd[len(name):].lstrip()) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
797 |
return pipefilter(s, cmd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
798 |
|
1015
22571b8d35d3
Add automatic binary file detection to diff and export
mpm@selenic.com
parents:
917
diff
changeset
|
799 |
def binary(s): |
6507
9699864de219
Let util.binary check entire data for \0 (issue1066, issue1079)
Christian Ebert <blacktrash@gmx.net>
parents:
6501
diff
changeset
|
800 |
"""return true if a string is binary data""" |
8118
35f7fda52c92
util: return boolean result directly in util.binary
Martin Geisler <mg@lazybytes.net>
parents:
8011
diff
changeset
|
801 |
return bool(s and '\0' in s) |
6762 | 802 |
|
7396
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
803 |
def increasingchunks(source, min=1024, max=65536): |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
804 |
'''return no less than min bytes per chunk while data remains, |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
805 |
doubling min after each chunk until it reaches max''' |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
806 |
def log2(x): |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
807 |
if not x: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
808 |
return 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
809 |
i = 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
810 |
while x: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
811 |
x >>= 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
812 |
i += 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
813 |
return i - 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
814 |
|
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
815 |
buf = [] |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
816 |
blen = 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
817 |
for chunk in source: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
818 |
buf.append(chunk) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
819 |
blen += len(chunk) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
820 |
if blen >= min: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
821 |
if min < max: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
822 |
min = min << 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
823 |
nmin = 1 << log2(blen) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
824 |
if nmin > min: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
825 |
min = nmin |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
826 |
if min > max: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
827 |
min = max |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
828 |
yield ''.join(buf) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
829 |
blen = 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
830 |
buf = [] |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
831 |
if buf: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
832 |
yield ''.join(buf) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
833 |
|
7947
a454eeb1b827
move util.Abort to error.py
Matt Mackall <mpm@selenic.com>
parents:
7913
diff
changeset
|
834 |
Abort = error.Abort |
508 | 835 |
|
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
836 |
def always(fn): |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
837 |
return True |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
838 |
|
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
839 |
def never(fn): |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
840 |
return False |
724
1c0c413cccdd
Get add and locate to use new repo and dirstate walk code.
Bryan O'Sullivan <bos@serpentine.com>
parents:
705
diff
changeset
|
841 |
|
23495
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
842 |
def nogc(func): |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
843 |
"""disable garbage collector |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
844 |
|
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
845 |
Python's garbage collector triggers a GC each time a certain number of |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
846 |
container objects (the number being defined by gc.get_threshold()) are |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
847 |
allocated even when marked not to be tracked by the collector. Tracking has |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
848 |
no effect on when GCs are triggered, only on what objects the GC looks |
23543
4dd8a6a1240d
spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents:
23495
diff
changeset
|
849 |
into. As a workaround, disable GC while building complex (huge) |
23495
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
850 |
containers. |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
851 |
|
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
852 |
This garbage collector issue have been fixed in 2.7. |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
853 |
""" |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
854 |
def wrapper(*args, **kwargs): |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
855 |
gcenabled = gc.isenabled() |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
856 |
gc.disable() |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
857 |
try: |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
858 |
return func(*args, **kwargs) |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
859 |
finally: |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
860 |
if gcenabled: |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
861 |
gc.enable() |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
862 |
return wrapper |
b25f07cb5399
util: add a 'nogc' decorator to disable the garbage collection
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23370
diff
changeset
|
863 |
|
4229
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
864 |
def pathto(root, n1, n2): |
886
509de8ab6f31
Fix walk path handling on Windows
Bryan O'Sullivan <bos@serpentine.com>
parents:
884
diff
changeset
|
865 |
'''return the relative path from one place to another. |
4229
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
866 |
root should use os.sep to separate directories |
3669
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
867 |
n1 should use os.sep to separate directories |
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
868 |
n2 should use "/" to separate directories |
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
869 |
returns an os.sep-separated path. |
4229
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
870 |
|
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
871 |
If n1 is a relative path, it's assumed it's |
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
872 |
relative to root. |
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
873 |
n2 should always be relative to root. |
3669
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
874 |
''' |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
875 |
if not n1: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
876 |
return localpath(n2) |
4230
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
877 |
if os.path.isabs(n1): |
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
878 |
if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
879 |
return os.path.join(root, localpath(n2)) |
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
880 |
n2 = '/'.join((pconvert(root), n2)) |
5844
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
881 |
a, b = splitpath(n1), n2.split('/') |
1541
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
882 |
a.reverse() |
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
883 |
b.reverse() |
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
878
diff
changeset
|
884 |
while a and b and a[-1] == b[-1]: |
1541
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
885 |
a.pop() |
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
886 |
b.pop() |
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
878
diff
changeset
|
887 |
b.reverse() |
6111
213ea6eed412
util.pathto: return '.' instead of an empty string
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6091
diff
changeset
|
888 |
return os.sep.join((['..'] * len(a)) + b) or '.' |
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
878
diff
changeset
|
889 |
|
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
890 |
def mainfrozen(): |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
891 |
"""return True if we are a frozen executable. |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
892 |
|
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
893 |
The code supports py2exe (most common, Windows only) and tools/freeze |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
894 |
(portable, not much used). |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
895 |
""" |
14968
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
896 |
return (safehasattr(sys, "frozen") or # new py2exe |
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
897 |
safehasattr(sys, "importers") or # old py2exe |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
898 |
imp.is_frozen("__main__")) # tools/freeze |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
899 |
|
22633
92b54547ac5d
util: introduce datapath for getting the location of supporting data files
Mads Kiilerich <madski@unity3d.com>
parents:
22632
diff
changeset
|
900 |
# the location of data files matching the source code |
27764
dd0c5f4d1b53
util: adjust 'datapath' to be correct in a frozen OS X package
Matt Harbison <matt_harbison@yahoo.com>
parents:
27755
diff
changeset
|
901 |
if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app': |
22633
92b54547ac5d
util: introduce datapath for getting the location of supporting data files
Mads Kiilerich <madski@unity3d.com>
parents:
22632
diff
changeset
|
902 |
# executable version (py2exe) doesn't support __file__ |
92b54547ac5d
util: introduce datapath for getting the location of supporting data files
Mads Kiilerich <madski@unity3d.com>
parents:
22632
diff
changeset
|
903 |
datapath = os.path.dirname(sys.executable) |
92b54547ac5d
util: introduce datapath for getting the location of supporting data files
Mads Kiilerich <madski@unity3d.com>
parents:
22632
diff
changeset
|
904 |
else: |
92b54547ac5d
util: introduce datapath for getting the location of supporting data files
Mads Kiilerich <madski@unity3d.com>
parents:
22632
diff
changeset
|
905 |
datapath = os.path.dirname(__file__) |
92b54547ac5d
util: introduce datapath for getting the location of supporting data files
Mads Kiilerich <madski@unity3d.com>
parents:
22632
diff
changeset
|
906 |
|
22638
0d0350cfc7ab
i18n: use datapath for i18n like for templates and help
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
907 |
i18n.setdatapath(datapath) |
0d0350cfc7ab
i18n: use datapath for i18n like for templates and help
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
908 |
|
22632
db15bb2d6323
util: move _hgexecutable a few lines, closer to where it is used
Mads Kiilerich <madski@unity3d.com>
parents:
22245
diff
changeset
|
909 |
_hgexecutable = None |
db15bb2d6323
util: move _hgexecutable a few lines, closer to where it is used
Mads Kiilerich <madski@unity3d.com>
parents:
22245
diff
changeset
|
910 |
|
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
911 |
def hgexecutable(): |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
912 |
"""return location of the 'hg' executable. |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
913 |
|
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
914 |
Defaults to $HG or 'hg' in the search path. |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
915 |
""" |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
916 |
if _hgexecutable is None: |
6500 | 917 |
hg = os.environ.get('HG') |
15106
76cd1964519c
util: fix finding of hgexecutable
Simon Heimberg <simohe@besonet.ch>
parents:
15081
diff
changeset
|
918 |
mainmod = sys.modules['__main__'] |
6500 | 919 |
if hg: |
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
920 |
_sethgexecutable(hg) |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
921 |
elif mainfrozen(): |
27765
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
922 |
if getattr(sys, 'frozen', None) == 'macosx_app': |
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
923 |
# Env variable set by py2app |
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
924 |
_sethgexecutable(os.environ['EXECUTABLEPATH']) |
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
925 |
else: |
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
926 |
_sethgexecutable(sys.executable) |
15106
76cd1964519c
util: fix finding of hgexecutable
Simon Heimberg <simohe@besonet.ch>
parents:
15081
diff
changeset
|
927 |
elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg': |
76cd1964519c
util: fix finding of hgexecutable
Simon Heimberg <simohe@besonet.ch>
parents:
15081
diff
changeset
|
928 |
_sethgexecutable(mainmod.__file__) |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
929 |
else: |
14271
4030630fb59c
rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents:
14262
diff
changeset
|
930 |
exe = findexe('hg') or os.path.basename(sys.argv[0]) |
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
931 |
_sethgexecutable(exe) |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
932 |
return _hgexecutable |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
933 |
|
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
934 |
def _sethgexecutable(path): |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
935 |
"""set location of the 'hg' executable""" |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
936 |
global _hgexecutable |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
937 |
_hgexecutable = path |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
938 |
|
26450
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
939 |
def _isstdout(f): |
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
940 |
fileno = getattr(f, 'fileno', None) |
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
941 |
return fileno and fileno() == sys.__stdout__.fileno() |
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
942 |
|
26311
60dd8e3977f0
util: avoid mutable default arguments
Siddharth Agarwal <sid0@fb.com>
parents:
26267
diff
changeset
|
943 |
def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None): |
1882
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
944 |
'''enhanced shell command execution. |
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
945 |
run with environment maybe modified, maybe in different dir. |
508 | 946 |
|
23271
76302f5ceba4
util.system: remove unused handling of onerr=ui
Yuya Nishihara <yuya@tcha.org>
parents:
23261
diff
changeset
|
947 |
if command fails and onerr is None, return status, else raise onerr |
76302f5ceba4
util.system: remove unused handling of onerr=ui
Yuya Nishihara <yuya@tcha.org>
parents:
23261
diff
changeset
|
948 |
object as exception. |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
949 |
|
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
950 |
if out is specified, it is assumed to be a file-like object that has a |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
951 |
write() method. stdout and stderr will be redirected to out.''' |
26311
60dd8e3977f0
util: avoid mutable default arguments
Siddharth Agarwal <sid0@fb.com>
parents:
26267
diff
changeset
|
952 |
if environ is None: |
60dd8e3977f0
util: avoid mutable default arguments
Siddharth Agarwal <sid0@fb.com>
parents:
26267
diff
changeset
|
953 |
environ = {} |
13439
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
954 |
try: |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
955 |
sys.stdout.flush() |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
956 |
except Exception: |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
957 |
pass |
2601
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
958 |
def py2shell(val): |
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
959 |
'convert python object into string that is useful to shell' |
8534
22ec9cf4d0ce
util: use "is" for True/False/None comparisons
Martin Geisler <mg@lazybytes.net>
parents:
8516
diff
changeset
|
960 |
if val is None or val is False: |
2601
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
961 |
return '0' |
8534
22ec9cf4d0ce
util: use "is" for True/False/None comparisons
Martin Geisler <mg@lazybytes.net>
parents:
8516
diff
changeset
|
962 |
if val is True: |
2601
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
963 |
return '1' |
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
964 |
return str(val) |
3905
a8c0365b2ace
util.system: fix quoting on windows
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3860
diff
changeset
|
965 |
origcmd = cmd |
13188
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
13128
diff
changeset
|
966 |
cmd = quotecommand(cmd) |
19729
dfefb719eb92
plan9: update util.py for cpython 2.7 build
Jeff Sickel <jas@corpus-callosum.com>
parents:
19710
diff
changeset
|
967 |
if sys.platform == 'plan9' and (sys.version_info[0] == 2 |
dfefb719eb92
plan9: update util.py for cpython 2.7 build
Jeff Sickel <jas@corpus-callosum.com>
parents:
19710
diff
changeset
|
968 |
and sys.version_info[1] < 7): |
16383
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
969 |
# subprocess kludge to work around issues in half-baked Python |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
970 |
# ports, notably bichued/python: |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
971 |
if not cwd is None: |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
972 |
os.chdir(cwd) |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
973 |
rc = os.system(cmd) |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
974 |
else: |
16383
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
975 |
env = dict(os.environ) |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
976 |
env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
977 |
env['HG'] = hgexecutable() |
26450
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
978 |
if out is None or _isstdout(out): |
16383
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
979 |
rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
980 |
env=env, cwd=cwd) |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
981 |
else: |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
982 |
proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
983 |
env=env, cwd=cwd, stdout=subprocess.PIPE, |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
984 |
stderr=subprocess.STDOUT) |
23030
a0e0aa12b672
util.system: avoid buffering of subprocess output when it is piped
Yuya Nishihara <yuya@tcha.org>
parents:
22963
diff
changeset
|
985 |
while True: |
a0e0aa12b672
util.system: avoid buffering of subprocess output when it is piped
Yuya Nishihara <yuya@tcha.org>
parents:
22963
diff
changeset
|
986 |
line = proc.stdout.readline() |
a0e0aa12b672
util.system: avoid buffering of subprocess output when it is piped
Yuya Nishihara <yuya@tcha.org>
parents:
22963
diff
changeset
|
987 |
if not line: |
a0e0aa12b672
util.system: avoid buffering of subprocess output when it is piped
Yuya Nishihara <yuya@tcha.org>
parents:
22963
diff
changeset
|
988 |
break |
16383
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
989 |
out.write(line) |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
990 |
proc.wait() |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
991 |
rc = proc.returncode |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
992 |
if sys.platform == 'OpenVMS' and rc & 1: |
f5dd179bfa4a
plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents:
16360
diff
changeset
|
993 |
rc = 0 |
9517
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
994 |
if rc and onerr: |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
995 |
errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), |
14234
600e64004eb5
rename explain_exit to explainexit
Adrian Buehlmann <adrian@cadifra.com>
parents:
14230
diff
changeset
|
996 |
explainexit(rc)[0]) |
9517
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
997 |
if errprefix: |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
998 |
errmsg = '%s: %s' % (errprefix, errmsg) |
23271
76302f5ceba4
util.system: remove unused handling of onerr=ui
Yuya Nishihara <yuya@tcha.org>
parents:
23261
diff
changeset
|
999 |
raise onerr(errmsg) |
9517
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
1000 |
return rc |
1880
05c7d75be925
fix broken environment save/restore when a hook runs.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1877
diff
changeset
|
1001 |
|
7388
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1002 |
def checksignature(func): |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1003 |
'''wrap a function with code to check for calling errors''' |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1004 |
def check(*args, **kwargs): |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1005 |
try: |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1006 |
return func(*args, **kwargs) |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1007 |
except TypeError: |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1008 |
if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
7646 | 1009 |
raise error.SignatureError |
7388
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1010 |
raise |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1011 |
|
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1012 |
return check |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
1013 |
|
29204
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1014 |
def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): |
27369
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1015 |
'''copy a file, preserving mode and optionally other stat info like |
29367
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1016 |
atime/mtime |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1017 |
|
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1018 |
checkambig argument is used with filestat, and is useful only if |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1019 |
destination file is guarded by any lock (e.g. repo.lock or |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1020 |
repo.wlock). |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1021 |
|
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1022 |
copystat and checkambig should be exclusive. |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1023 |
''' |
29204
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1024 |
assert not (copystat and checkambig) |
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1025 |
oldstat = None |
18326
614f769e6aa7
util: copyfile: remove dest before copying
Mads Kiilerich <mads@kiilerich.com>
parents:
18026
diff
changeset
|
1026 |
if os.path.lexists(dest): |
29204
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1027 |
if checkambig: |
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1028 |
oldstat = checkambig and filestat(dest) |
18326
614f769e6aa7
util: copyfile: remove dest before copying
Mads Kiilerich <mads@kiilerich.com>
parents:
18026
diff
changeset
|
1029 |
unlink(dest) |
24155
e5ce49a30146
transaction: disable hardlink backups (issue4546)
Matt Mackall <mpm@selenic.com>
parents:
23917
diff
changeset
|
1030 |
# hardlinks are problematic on CIFS, quietly ignore this flag |
e5ce49a30146
transaction: disable hardlink backups (issue4546)
Matt Mackall <mpm@selenic.com>
parents:
23917
diff
changeset
|
1031 |
# until we find a way to work around it cleanly (issue4546) |
24164
07a92bbd02e5
transaction: really disable hardlink backups (issue4546)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24155
diff
changeset
|
1032 |
if False and hardlink: |
23899
4e451d1359de
copyfile: allow optional hardlinking
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23864
diff
changeset
|
1033 |
try: |
4e451d1359de
copyfile: allow optional hardlinking
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23864
diff
changeset
|
1034 |
oslink(src, dest) |
4e451d1359de
copyfile: allow optional hardlinking
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23864
diff
changeset
|
1035 |
return |
4e451d1359de
copyfile: allow optional hardlinking
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23864
diff
changeset
|
1036 |
except (IOError, OSError): |
4e451d1359de
copyfile: allow optional hardlinking
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23864
diff
changeset
|
1037 |
pass # fall back to normal copy |
4271
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
1038 |
if os.path.islink(src): |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
1039 |
os.symlink(os.readlink(src), dest) |
27369
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1040 |
# copytime is ignored for symlinks, but in general copytime isn't needed |
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1041 |
# for them anyway |
4271
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
1042 |
else: |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
1043 |
try: |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
1044 |
shutil.copyfile(src, dest) |
27369
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1045 |
if copystat: |
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1046 |
# copystat also copies mode |
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1047 |
shutil.copystat(src, dest) |
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1048 |
else: |
c48ecc0b5bc9
copyfile: add an optional parameter to copy other stat data
Siddharth Agarwal <sid0@fb.com>
parents:
26665
diff
changeset
|
1049 |
shutil.copymode(src, dest) |
29204
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1050 |
if oldstat and oldstat.stat: |
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1051 |
newstat = filestat(dest) |
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1052 |
if newstat.isambig(oldstat): |
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1053 |
# stat of copied file is ambiguous to original one |
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1054 |
advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff |
ce2d81aafbae
util: make copyfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29201
diff
changeset
|
1055 |
os.utime(dest, (advanced, advanced)) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25420
diff
changeset
|
1056 |
except shutil.Error as inst: |
4271
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
1057 |
raise Abort(str(inst)) |
3629
4cfb72bcb978
util: add copyfile function
Matt Mackall <mpm@selenic.com>
parents:
3568
diff
changeset
|
1058 |
|
24439
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1059 |
def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None): |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1060 |
"""Copy a directory tree using hardlinks if possible.""" |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1061 |
num = 0 |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1062 |
|
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1063 |
if hardlink is None: |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1064 |
hardlink = (os.stat(src).st_dev == |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1065 |
os.stat(os.path.dirname(dst)).st_dev) |
24439
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1066 |
if hardlink: |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1067 |
topic = _('linking') |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1068 |
else: |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1069 |
topic = _('copying') |
698
df78d8ccac4c
Use python function instead of external 'cp' command when cloning repos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
667
diff
changeset
|
1070 |
|
1207 | 1071 |
if os.path.isdir(src): |
1072 |
os.mkdir(dst) |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5360
diff
changeset
|
1073 |
for name, kind in osutil.listdir(src): |
1207 | 1074 |
srcname = os.path.join(src, name) |
1075 |
dstname = os.path.join(dst, name) |
|
24439
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1076 |
def nprog(t, pos): |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1077 |
if pos is not None: |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1078 |
return progress(t, pos + num) |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1079 |
hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog) |
11251
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
1080 |
num += n |
1207 | 1081 |
else: |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1082 |
if hardlink: |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1083 |
try: |
14235
b9e1b041744f
rename util.os_link to oslink
Adrian Buehlmann <adrian@cadifra.com>
parents:
14234
diff
changeset
|
1084 |
oslink(src, dst) |
2050
e49d0fa38176
util.copyfiles: only switch to copy if hardlink raises IOError or OSError.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2026
diff
changeset
|
1085 |
except (IOError, OSError): |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1086 |
hardlink = False |
1591
5a3229cf1492
do not copy atime and mtime in util.copyfiles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1585
diff
changeset
|
1087 |
shutil.copy(src, dst) |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
1088 |
else: |
1591
5a3229cf1492
do not copy atime and mtime in util.copyfiles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1585
diff
changeset
|
1089 |
shutil.copy(src, dst) |
11251
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
1090 |
num += 1 |
24439
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1091 |
progress(topic, num) |
2ddfac2f163e
util: add progress callback support to copyfiles
Augie Fackler <augie@google.com>
parents:
24236
diff
changeset
|
1092 |
progress(topic, None) |
698
df78d8ccac4c
Use python function instead of external 'cp' command when cloning repos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
667
diff
changeset
|
1093 |
|
11251
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
1094 |
return hardlink, num |
11254
640d419725d0
util.copyfiles: don't try os_link() again if it failed before
Adrian Buehlmann <adrian@cadifra.com>
parents:
11010
diff
changeset
|
1095 |
|
14262
23cd7eeff678
util: rename _windows_reserved_filenames and _windows_reserved_chars
Adrian Buehlmann <adrian@cadifra.com>
parents:
14250
diff
changeset
|
1096 |
_winreservednames = '''con prn aux nul |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1097 |
com1 com2 com3 com4 com5 com6 com7 com8 com9 |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1098 |
lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() |
14262
23cd7eeff678
util: rename _windows_reserved_filenames and _windows_reserved_chars
Adrian Buehlmann <adrian@cadifra.com>
parents:
14250
diff
changeset
|
1099 |
_winreservedchars = ':*?"<>|' |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1100 |
def checkwinfilename(path): |
20000
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1101 |
r'''Check that the base-relative path is a valid filename on Windows. |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1102 |
Returns None if the path is ok, or a UI string describing the problem. |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1103 |
|
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1104 |
>>> checkwinfilename("just/a/normal/path") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1105 |
>>> checkwinfilename("foo/bar/con.xml") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1106 |
"filename contains 'con', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1107 |
>>> checkwinfilename("foo/con.xml/bar") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1108 |
"filename contains 'con', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1109 |
>>> checkwinfilename("foo/bar/xml.con") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1110 |
>>> checkwinfilename("foo/bar/AUX/bla.txt") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1111 |
"filename contains 'AUX', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1112 |
>>> checkwinfilename("foo/bar/bla:.txt") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1113 |
"filename contains ':', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1114 |
>>> checkwinfilename("foo/bar/b\07la.txt") |
20000
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1115 |
"filename contains '\\x07', which is invalid on Windows" |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1116 |
>>> checkwinfilename("foo/bar/bla ") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1117 |
"filename ends with ' ', which is not allowed on Windows" |
15358
a347b3614bae
util: don't complain about '..' in path components not working on Windows
Matt Mackall <mpm@selenic.com>
parents:
15159
diff
changeset
|
1118 |
>>> checkwinfilename("../bar") |
20000
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1119 |
>>> checkwinfilename("foo\\") |
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1120 |
"filename ends with '\\', which is invalid on Windows" |
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1121 |
>>> checkwinfilename("foo\\/bar") |
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1122 |
"directory name ends with '\\', which is invalid on Windows" |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1123 |
''' |
20000
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1124 |
if path.endswith('\\'): |
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1125 |
return _("filename ends with '\\', which is invalid on Windows") |
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1126 |
if '\\/' in path: |
0849d280663e
util: warn when adding paths ending with \
Mads Kiilerich <madski@unity3d.com>
parents:
19951
diff
changeset
|
1127 |
return _("directory name ends with '\\', which is invalid on Windows") |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1128 |
for n in path.replace('\\', '/').split('/'): |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1129 |
if not n: |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1130 |
continue |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1131 |
for c in n: |
14262
23cd7eeff678
util: rename _windows_reserved_filenames and _windows_reserved_chars
Adrian Buehlmann <adrian@cadifra.com>
parents:
14250
diff
changeset
|
1132 |
if c in _winreservedchars: |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1133 |
return _("filename contains '%s', which is reserved " |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1134 |
"on Windows") % c |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1135 |
if ord(c) <= 31: |
13947
d2d1ef6a5238
checkwinfilename: use %r in format string
Adrian Buehlmann <adrian@cadifra.com>
parents:
13944
diff
changeset
|
1136 |
return _("filename contains %r, which is invalid " |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1137 |
"on Windows") % c |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1138 |
base = n.split('.')[0] |
14262
23cd7eeff678
util: rename _windows_reserved_filenames and _windows_reserved_chars
Adrian Buehlmann <adrian@cadifra.com>
parents:
14250
diff
changeset
|
1139 |
if base and base.lower() in _winreservednames: |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1140 |
return _("filename contains '%s', which is reserved " |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1141 |
"on Windows") % base |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1142 |
t = n[-1] |
15358
a347b3614bae
util: don't complain about '..' in path components not working on Windows
Matt Mackall <mpm@selenic.com>
parents:
15159
diff
changeset
|
1143 |
if t in '. ' and n not in '..': |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1144 |
return _("filename ends with '%s', which is not allowed " |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1145 |
"on Windows") % t |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1146 |
|
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1147 |
if os.name == 'nt': |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
1148 |
checkosfilename = checkwinfilename |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1149 |
else: |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
1150 |
checkosfilename = platform.checkosfilename |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1151 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1152 |
def makelock(info, pathname): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1153 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1154 |
return os.symlink(info, pathname) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25420
diff
changeset
|
1155 |
except OSError as why: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1156 |
if why.errno == errno.EEXIST: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1157 |
raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1158 |
except AttributeError: # no symlink in os |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1159 |
pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1160 |
|
704
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
1161 |
ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) |
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
1162 |
os.write(ld, info) |
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
1163 |
os.close(ld) |
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
1164 |
|
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1165 |
def readlock(pathname): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1166 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1167 |
return os.readlink(pathname) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25420
diff
changeset
|
1168 |
except OSError as why: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1169 |
if why.errno not in (errno.EINVAL, errno.ENOSYS): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1170 |
raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1171 |
except AttributeError: # no symlink in os |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
1172 |
pass |
13400
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
1173 |
fp = posixfile(pathname) |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
1174 |
r = fp.read() |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
1175 |
fp.close() |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
1176 |
return r |
704
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
1177 |
|
2176
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1178 |
def fstat(fp): |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1179 |
'''stat file object that may not have fileno method.''' |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1180 |
try: |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1181 |
return os.fstat(fp.fileno()) |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1182 |
except AttributeError: |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1183 |
return os.stat(fp.name) |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1184 |
|
3784 | 1185 |
# File system features |
1186 |
||
6746
1dca460e7d1e
rename checkfolding to checkcase
Matt Mackall <mpm@selenic.com>
parents:
6743
diff
changeset
|
1187 |
def checkcase(path): |
3784 | 1188 |
""" |
18911
451eb1c27c1b
util: improve doc for checkcase
Mads Kiilerich <mads@kiilerich.com>
parents:
18868
diff
changeset
|
1189 |
Return true if the given path is on a case-sensitive filesystem |
3784 | 1190 |
|
1191 |
Requires a path (like /foo/.hg) ending with a foldable final |
|
1192 |
directory component. |
|
1193 |
""" |
|
24902
986a5c23b1c1
util.checkcase: don't abort on broken symlinks
Siddharth Agarwal <sid0@fb.com>
parents:
24692
diff
changeset
|
1194 |
s1 = os.lstat(path) |
3784 | 1195 |
d, b = os.path.split(path) |
15667
eacfd851cb9e
icasefs: consider as case sensitive if there is no counterevidence, for safety
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15609
diff
changeset
|
1196 |
b2 = b.upper() |
eacfd851cb9e
icasefs: consider as case sensitive if there is no counterevidence, for safety
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15609
diff
changeset
|
1197 |
if b == b2: |
eacfd851cb9e
icasefs: consider as case sensitive if there is no counterevidence, for safety
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15609
diff
changeset
|
1198 |
b2 = b.lower() |
eacfd851cb9e
icasefs: consider as case sensitive if there is no counterevidence, for safety
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15609
diff
changeset
|
1199 |
if b == b2: |
eacfd851cb9e
icasefs: consider as case sensitive if there is no counterevidence, for safety
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15609
diff
changeset
|
1200 |
return True # no evidence against case sensitivity |
eacfd851cb9e
icasefs: consider as case sensitive if there is no counterevidence, for safety
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15609
diff
changeset
|
1201 |
p2 = os.path.join(d, b2) |
3784 | 1202 |
try: |
24902
986a5c23b1c1
util.checkcase: don't abort on broken symlinks
Siddharth Agarwal <sid0@fb.com>
parents:
24692
diff
changeset
|
1203 |
s2 = os.lstat(p2) |
3784 | 1204 |
if s2 == s1: |
1205 |
return False |
|
1206 |
return True |
|
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13985
diff
changeset
|
1207 |
except OSError: |
3784 | 1208 |
return True |
1209 |
||
16943
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1210 |
try: |
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1211 |
import re2 |
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1212 |
_re2 = None |
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1213 |
except ImportError: |
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1214 |
_re2 = False |
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1215 |
|
21908
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1216 |
class _re(object): |
21913
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1217 |
def _checkre2(self): |
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1218 |
global _re2 |
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1219 |
try: |
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1220 |
# check if match works, see issue3964 |
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1221 |
_re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]')) |
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1222 |
except ImportError: |
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1223 |
_re2 = False |
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1224 |
|
21908
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1225 |
def compile(self, pat, flags=0): |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1226 |
'''Compile a regular expression, using re2 if possible |
16943
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1227 |
|
21908
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1228 |
For best performance, use only re2-compatible regexp features. The |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1229 |
only flags from the re module that are re2-compatible are |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1230 |
IGNORECASE and MULTILINE.''' |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1231 |
if _re2 is None: |
21913
50aad4609224
util.re: move check for re2 into a separate method
Siddharth Agarwal <sid0@fb.com>
parents:
21912
diff
changeset
|
1232 |
self._checkre2() |
21908
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1233 |
if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1234 |
if flags & remod.IGNORECASE: |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1235 |
pat = '(?i)' + pat |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1236 |
if flags & remod.MULTILINE: |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1237 |
pat = '(?m)' + pat |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1238 |
try: |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1239 |
return re2.compile(pat) |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1240 |
except re2.error: |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1241 |
pass |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1242 |
return remod.compile(pat, flags) |
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1243 |
|
21914
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1244 |
@propertycache |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1245 |
def escape(self): |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1246 |
'''Return the version of escape corresponding to self.compile. |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1247 |
|
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1248 |
This is imperfect because whether re2 or re is used for a particular |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1249 |
function depends on the flags, etc, but it's the best we can do. |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1250 |
''' |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1251 |
global _re2 |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1252 |
if _re2 is None: |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1253 |
self._checkre2() |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1254 |
if _re2: |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1255 |
return re2.escape |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1256 |
else: |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1257 |
return remod.escape |
10e99839a7a4
util.re: add an escape method
Siddharth Agarwal <sid0@fb.com>
parents:
21913
diff
changeset
|
1258 |
|
21908
cad9dadc9d26
util: move compilere to a class
Siddharth Agarwal <sid0@fb.com>
parents:
21907
diff
changeset
|
1259 |
re = _re() |
16943
8d08a28aa63e
matcher: use re2 bindings if available
Bryan O'Sullivan <bryano@fb.com>
parents:
16873
diff
changeset
|
1260 |
|
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1261 |
_fspathcache = {} |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1262 |
def fspath(name, root): |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1263 |
'''Get name in the case stored in the filesystem |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1264 |
|
15710
f63e40047372
icasefs: avoid path-absoluteness/existance check in util.fspath() for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15709
diff
changeset
|
1265 |
The name should be relative to root, and be normcase-ed for efficiency. |
f63e40047372
icasefs: avoid path-absoluteness/existance check in util.fspath() for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15709
diff
changeset
|
1266 |
|
f63e40047372
icasefs: avoid path-absoluteness/existance check in util.fspath() for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15709
diff
changeset
|
1267 |
Note that this function is unnecessary, and should not be |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1268 |
called, for case-sensitive filesystems (simply because it's expensive). |
15670
d6c19cfa03ce
icasefs: avoid normcase()-ing in util.fspath() for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15669
diff
changeset
|
1269 |
|
15710
f63e40047372
icasefs: avoid path-absoluteness/existance check in util.fspath() for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15709
diff
changeset
|
1270 |
The root should be normcase-ed, too. |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1271 |
''' |
23097
30124c40d11f
util.fspath: use a dict rather than a linear scan for lookups
Siddharth Agarwal <sid0@fb.com>
parents:
23076
diff
changeset
|
1272 |
def _makefspathcacheentry(dir): |
30124c40d11f
util.fspath: use a dict rather than a linear scan for lookups
Siddharth Agarwal <sid0@fb.com>
parents:
23076
diff
changeset
|
1273 |
return dict((normcase(n), n) for n in os.listdir(dir)) |
15709
a1f4bd47d18e
icasefs: retry directory scan once for already invalidated cache
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15674
diff
changeset
|
1274 |
|
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1275 |
seps = os.sep |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1276 |
if os.altsep: |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1277 |
seps = seps + os.altsep |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1278 |
# Protect backslashes. This gets silly very quickly. |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1279 |
seps.replace('\\','\\\\') |
21907
7e5dfa00e3c2
util: rename 're' to 'remod'
Siddharth Agarwal <sid0@fb.com>
parents:
21857
diff
changeset
|
1280 |
pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps)) |
15669
390bcd01775a
icasefs: use util.normcase() instead of lower() or os.path.normcase in fspath
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15667
diff
changeset
|
1281 |
dir = os.path.normpath(root) |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1282 |
result = [] |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1283 |
for part, sep in pattern.findall(name): |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1284 |
if sep: |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1285 |
result.append(sep) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1286 |
continue |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1287 |
|
15719
1dd60426b061
icasefs: follow standard cache look up pattern
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15718
diff
changeset
|
1288 |
if dir not in _fspathcache: |
23097
30124c40d11f
util.fspath: use a dict rather than a linear scan for lookups
Siddharth Agarwal <sid0@fb.com>
parents:
23076
diff
changeset
|
1289 |
_fspathcache[dir] = _makefspathcacheentry(dir) |
15719
1dd60426b061
icasefs: follow standard cache look up pattern
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15718
diff
changeset
|
1290 |
contents = _fspathcache[dir] |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1291 |
|
23097
30124c40d11f
util.fspath: use a dict rather than a linear scan for lookups
Siddharth Agarwal <sid0@fb.com>
parents:
23076
diff
changeset
|
1292 |
found = contents.get(part) |
15709
a1f4bd47d18e
icasefs: retry directory scan once for already invalidated cache
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15674
diff
changeset
|
1293 |
if not found: |
15720
3bcfea777efc
icasefs: rewrite comment to explain situtation precisely
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15719
diff
changeset
|
1294 |
# retry "once per directory" per "dirstate.walk" which |
3bcfea777efc
icasefs: rewrite comment to explain situtation precisely
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15719
diff
changeset
|
1295 |
# may take place for each patches of "hg qpush", for example |
23097
30124c40d11f
util.fspath: use a dict rather than a linear scan for lookups
Siddharth Agarwal <sid0@fb.com>
parents:
23076
diff
changeset
|
1296 |
_fspathcache[dir] = contents = _makefspathcacheentry(dir) |
30124c40d11f
util.fspath: use a dict rather than a linear scan for lookups
Siddharth Agarwal <sid0@fb.com>
parents:
23076
diff
changeset
|
1297 |
found = contents.get(part) |
15709
a1f4bd47d18e
icasefs: retry directory scan once for already invalidated cache
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15674
diff
changeset
|
1298 |
|
a1f4bd47d18e
icasefs: retry directory scan once for already invalidated cache
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15674
diff
changeset
|
1299 |
result.append(found or part) |
15669
390bcd01775a
icasefs: use util.normcase() instead of lower() or os.path.normcase in fspath
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15667
diff
changeset
|
1300 |
dir = os.path.join(dir, part) |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1301 |
|
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1302 |
return ''.join(result) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
1303 |
|
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1304 |
def checknlink(testfile): |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1305 |
'''check whether hardlink count reporting works properly''' |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1306 |
|
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1307 |
# testfile may be open, so we need a separate file for checking to |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1308 |
# work around issue2543 (or testfile may get lost on Samba shares) |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1309 |
f1 = testfile + ".hgtmp1" |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1310 |
if os.path.lexists(f1): |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1311 |
return False |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1312 |
try: |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1313 |
posixfile(f1, 'w').close() |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1314 |
except IOError: |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1315 |
return False |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1316 |
|
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1317 |
f2 = testfile + ".hgtmp2" |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1318 |
fd = None |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1319 |
try: |
25088
754df8e932d3
util: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents:
24902
diff
changeset
|
1320 |
oslink(f1, f2) |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1321 |
# nlinks() may behave differently for files on Windows shares if |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1322 |
# the file is open. |
13342
2dc7a2a96cfd
opener: use posixfile to hold file open when calling nlinks()
Adrian Buehlmann <adrian@cadifra.com>
parents:
13316
diff
changeset
|
1323 |
fd = posixfile(f2) |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1324 |
return nlinks(f2) > 1 |
25088
754df8e932d3
util: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents:
24902
diff
changeset
|
1325 |
except OSError: |
754df8e932d3
util: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents:
24902
diff
changeset
|
1326 |
return False |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1327 |
finally: |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1328 |
if fd is not None: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1329 |
fd.close() |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1330 |
for f in (f1, f2): |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1331 |
try: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1332 |
os.unlink(f) |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1333 |
except OSError: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
1334 |
pass |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
1335 |
|
5843
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
1336 |
def endswithsep(path): |
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
1337 |
'''Check path ends with os.sep or os.altsep.''' |
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
1338 |
return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep) |
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
1339 |
|
5844
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1340 |
def splitpath(path): |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1341 |
'''Split path by os.sep. |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1342 |
Note that this function does not use os.altsep because this is |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1343 |
an alternative of simple "xxx.split(os.sep)". |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1344 |
It is recommended to use os.path.normpath() before using this |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1345 |
function if need.''' |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1346 |
return path.split(os.sep) |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
1347 |
|
6007
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
1348 |
def gui(): |
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
1349 |
'''Are we running in a GUI?''' |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1350 |
if sys.platform == 'darwin': |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1351 |
if 'SSH_CONNECTION' in os.environ: |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1352 |
# handle SSH access to a box where the user is logged in |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1353 |
return False |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1354 |
elif getattr(osutil, 'isgui', None): |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1355 |
# check if a CoreGraphics session is available |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1356 |
return osutil.isgui() |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1357 |
else: |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1358 |
# pure build; use a safe default |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1359 |
return True |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1360 |
else: |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
1361 |
return os.name == "nt" or os.environ.get("DISPLAY") |
6007
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
1362 |
|
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
1363 |
def mktempcopy(name, emptyok=False, createmode=None): |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1364 |
"""Create a temporary file with the same contents from name |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1365 |
|
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1366 |
The permission bits are copied from the original file. |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1367 |
|
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1368 |
If the temporary file is going to be truncated immediately, you |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1369 |
can use emptyok=True as an optimization. |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1370 |
|
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1371 |
Returns the name of the temporary file. |
2176
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1372 |
""" |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1373 |
d, fn = os.path.split(name) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1374 |
fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1375 |
os.close(fd) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1376 |
# Temporary files are created with mode 0600, which is usually not |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1377 |
# what we want. If the original file already exists, just copy |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1378 |
# its mode. Otherwise, manually obey umask. |
15010
c3114acd8ea2
util: factor new function copymode out of mktempcopy
Adrian Buehlmann <adrian@cadifra.com>
parents:
14999
diff
changeset
|
1379 |
copymode(name, temp, createmode) |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1380 |
if emptyok: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1381 |
return temp |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1382 |
try: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1383 |
try: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1384 |
ifp = posixfile(name, "rb") |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25420
diff
changeset
|
1385 |
except IOError as inst: |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1386 |
if inst.errno == errno.ENOENT: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1387 |
return temp |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1388 |
if not getattr(inst, 'filename', None): |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1389 |
inst.filename = name |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1390 |
raise |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1391 |
ofp = posixfile(temp, "wb") |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1392 |
for chunk in filechunkiter(ifp): |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1393 |
ofp.write(chunk) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1394 |
ifp.close() |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1395 |
ofp.close() |
16705
c2d9ef43ff6c
check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents:
16703
diff
changeset
|
1396 |
except: # re-raises |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1397 |
try: os.unlink(temp) |
16703
7292a4618f46
cleanup: replace more naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents:
16688
diff
changeset
|
1398 |
except OSError: pass |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1399 |
raise |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1400 |
return temp |
2176
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
1401 |
|
29200
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1402 |
class filestat(object): |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1403 |
"""help to exactly detect change of a file |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1404 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1405 |
'stat' attribute is result of 'os.stat()' if specified 'path' |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1406 |
exists. Otherwise, it is None. This can avoid preparative |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1407 |
'exists()' examination on client side of this class. |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1408 |
""" |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1409 |
def __init__(self, path): |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1410 |
try: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1411 |
self.stat = os.stat(path) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1412 |
except OSError as err: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1413 |
if err.errno != errno.ENOENT: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1414 |
raise |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1415 |
self.stat = None |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1416 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1417 |
__hash__ = object.__hash__ |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1418 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1419 |
def __eq__(self, old): |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1420 |
try: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1421 |
# if ambiguity between stat of new and old file is |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1422 |
# avoided, comparision of size, ctime and mtime is enough |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1423 |
# to exactly detect change of a file regardless of platform |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1424 |
return (self.stat.st_size == old.stat.st_size and |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1425 |
self.stat.st_ctime == old.stat.st_ctime and |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1426 |
self.stat.st_mtime == old.stat.st_mtime) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1427 |
except AttributeError: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1428 |
return False |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1429 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1430 |
def isambig(self, old): |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1431 |
"""Examine whether new (= self) stat is ambiguous against old one |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1432 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1433 |
"S[N]" below means stat of a file at N-th change: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1434 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1435 |
- S[n-1].ctime < S[n].ctime: can detect change of a file |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1436 |
- S[n-1].ctime == S[n].ctime |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1437 |
- S[n-1].ctime < S[n].mtime: means natural advancing (*1) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1438 |
- S[n-1].ctime == S[n].mtime: is ambiguous (*2) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1439 |
- S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1440 |
- S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1441 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1442 |
Case (*2) above means that a file was changed twice or more at |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1443 |
same time in sec (= S[n-1].ctime), and comparison of timestamp |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1444 |
is ambiguous. |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1445 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1446 |
Base idea to avoid such ambiguity is "advance mtime 1 sec, if |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1447 |
timestamp is ambiguous". |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1448 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1449 |
But advancing mtime only in case (*2) doesn't work as |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1450 |
expected, because naturally advanced S[n].mtime in case (*1) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1451 |
might be equal to manually advanced S[n-1 or earlier].mtime. |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1452 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1453 |
Therefore, all "S[n-1].ctime == S[n].ctime" cases should be |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1454 |
treated as ambiguous regardless of mtime, to avoid overlooking |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1455 |
by confliction between such mtime. |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1456 |
|
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1457 |
Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1458 |
S[n].mtime", even if size of a file isn't changed. |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1459 |
""" |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1460 |
try: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1461 |
return (self.stat.st_ctime == old.stat.st_ctime) |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1462 |
except AttributeError: |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1463 |
return False |
ca4065028e00
util: add filestat class to detect ambiguity of file stat
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29017
diff
changeset
|
1464 |
|
29298
82f6193ff2bc
util: add __ne__ to filestat class for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29261
diff
changeset
|
1465 |
def __ne__(self, other): |
82f6193ff2bc
util: add __ne__ to filestat class for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29261
diff
changeset
|
1466 |
return not self == other |
82f6193ff2bc
util: add __ne__ to filestat class for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29261
diff
changeset
|
1467 |
|
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8631
diff
changeset
|
1468 |
class atomictempfile(object): |
17424
e7cfe3587ea4
fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents:
17391
diff
changeset
|
1469 |
'''writable file object that atomically updates a file |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1470 |
|
14008
da65edcac72a
atomictempfile: rewrite docstring to clarify rename() vs. close().
Greg Ward <greg@gerg.ca>
parents:
14007
diff
changeset
|
1471 |
All writes will go to a temporary copy of the original file. Call |
15057
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
15050
diff
changeset
|
1472 |
close() when you are done writing, and atomictempfile will rename |
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
15050
diff
changeset
|
1473 |
the temporary copy to the original name, making the changes |
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
15050
diff
changeset
|
1474 |
visible. If the object is destroyed without being closed, all your |
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
15050
diff
changeset
|
1475 |
writes are discarded. |
29367
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1476 |
|
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1477 |
checkambig argument of constructor is used with filestat, and is |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1478 |
useful only if target file is guarded by any lock (e.g. repo.lock |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29342
diff
changeset
|
1479 |
or repo.wlock). |
14008
da65edcac72a
atomictempfile: rewrite docstring to clarify rename() vs. close().
Greg Ward <greg@gerg.ca>
parents:
14007
diff
changeset
|
1480 |
''' |
29201
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1481 |
def __init__(self, name, mode='w+b', createmode=None, checkambig=False): |
14007
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1482 |
self.__name = name # permanent name |
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1483 |
self._tempname = mktempcopy(name, emptyok=('w' in mode), |
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1484 |
createmode=createmode) |
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1485 |
self._fp = posixfile(self._tempname, mode) |
29201
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1486 |
self._checkambig = checkambig |
8327
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
1487 |
|
14007
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1488 |
# delegated methods |
29393
50269a4dce61
atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents:
29367
diff
changeset
|
1489 |
self.read = self._fp.read |
14007
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1490 |
self.write = self._fp.write |
17237
e73128535105
util: delegate seek and tell methods of atomictempfile
Bryan O'Sullivan <bryano@fb.com>
parents:
17203
diff
changeset
|
1491 |
self.seek = self._fp.seek |
e73128535105
util: delegate seek and tell methods of atomictempfile
Bryan O'Sullivan <bryano@fb.com>
parents:
17203
diff
changeset
|
1492 |
self.tell = self._fp.tell |
14007
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1493 |
self.fileno = self._fp.fileno |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1494 |
|
15057
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
15050
diff
changeset
|
1495 |
def close(self): |
8785
7a9151bc5b37
atomictempfile: fix exception in __del__ if mktempcopy fails (self._fp is None)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8778
diff
changeset
|
1496 |
if not self._fp.closed: |
8327
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
1497 |
self._fp.close() |
29201
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1498 |
filename = localpath(self.__name) |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1499 |
oldstat = self._checkambig and filestat(filename) |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1500 |
if oldstat and oldstat.stat: |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1501 |
rename(self._tempname, filename) |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1502 |
newstat = filestat(filename) |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1503 |
if newstat.isambig(oldstat): |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1504 |
# stat of changed file is ambiguous to original one |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1505 |
advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1506 |
os.utime(filename, (advanced, advanced)) |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1507 |
else: |
a109bf7e0dc2
util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29200
diff
changeset
|
1508 |
rename(self._tempname, filename) |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1509 |
|
15057
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
15050
diff
changeset
|
1510 |
def discard(self): |
8785
7a9151bc5b37
atomictempfile: fix exception in __del__ if mktempcopy fails (self._fp is None)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8778
diff
changeset
|
1511 |
if not self._fp.closed: |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1512 |
try: |
14007
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1513 |
os.unlink(self._tempname) |
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1514 |
except OSError: |
d764463b433e
atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
14004
diff
changeset
|
1515 |
pass |
8327
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
1516 |
self._fp.close() |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
1517 |
|
13098
f7d6750dcd01
util: make atomicfiles closable
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13072
diff
changeset
|
1518 |
def __del__(self): |
14968
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
1519 |
if safehasattr(self, '_fp'): # constructor actually did something |
15057
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
15050
diff
changeset
|
1520 |
self.discard() |
13098
f7d6750dcd01
util: make atomicfiles closable
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13072
diff
changeset
|
1521 |
|
29394
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1522 |
def __enter__(self): |
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1523 |
return self |
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1524 |
|
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1525 |
def __exit__(self, exctype, excvalue, traceback): |
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1526 |
if exctype is not None: |
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1527 |
self.discard() |
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1528 |
else: |
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1529 |
self.close() |
6d96658a22b0
atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents:
29393
diff
changeset
|
1530 |
|
18938
e22107cff6bf
util: add notindexed optional parameter to makedirs function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18930
diff
changeset
|
1531 |
def makedirs(name, mode=None, notindexed=False): |
29017
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1532 |
"""recursive directory creation with parent mode inheritance |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1533 |
|
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1534 |
Newly created directories are marked as "not to be indexed by |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1535 |
the content indexing service", if ``notindexed`` is specified |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1536 |
for "write" mode access. |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1537 |
""" |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
1538 |
try: |
18938
e22107cff6bf
util: add notindexed optional parameter to makedirs function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18930
diff
changeset
|
1539 |
makedir(name, notindexed) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25420
diff
changeset
|
1540 |
except OSError as err: |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
1541 |
if err.errno == errno.EEXIST: |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
1542 |
return |
15058
81f33be0ea79
util: postpone and reorder parent calculation in makedirs
Adrian Buehlmann <adrian@cadifra.com>
parents:
15057
diff
changeset
|
1543 |
if err.errno != errno.ENOENT or not name: |
81f33be0ea79
util: postpone and reorder parent calculation in makedirs
Adrian Buehlmann <adrian@cadifra.com>
parents:
15057
diff
changeset
|
1544 |
raise |
81f33be0ea79
util: postpone and reorder parent calculation in makedirs
Adrian Buehlmann <adrian@cadifra.com>
parents:
15057
diff
changeset
|
1545 |
parent = os.path.dirname(os.path.abspath(name)) |
81f33be0ea79
util: postpone and reorder parent calculation in makedirs
Adrian Buehlmann <adrian@cadifra.com>
parents:
15057
diff
changeset
|
1546 |
if parent == name: |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
1547 |
raise |
18938
e22107cff6bf
util: add notindexed optional parameter to makedirs function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18930
diff
changeset
|
1548 |
makedirs(parent, mode, notindexed) |
29017
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1549 |
try: |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1550 |
makedir(name, notindexed) |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1551 |
except OSError as err: |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1552 |
# Catch EEXIST to handle races |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1553 |
if err.errno == errno.EEXIST: |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1554 |
return |
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28883
diff
changeset
|
1555 |
raise |
18678
423eee0b0b14
util: make ensuredirs safer against races
Bryan O'Sullivan <bryano@fb.com>
parents:
18668
diff
changeset
|
1556 |
if mode is not None: |
423eee0b0b14
util: make ensuredirs safer against races
Bryan O'Sullivan <bryano@fb.com>
parents:
18668
diff
changeset
|
1557 |
os.chmod(name, mode) |
18668
4034b8d551b1
scmutil: create directories in a race-safe way during update
Bryan O'Sullivan <bryano@fb.com>
parents:
18614
diff
changeset
|
1558 |
|
14099
0824a0a3cefc
util: add readfile() & writefile() helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14077
diff
changeset
|
1559 |
def readfile(path): |
27778
4d10600c3f08
util: simplify file I/O functions using context managers
Bryan O'Sullivan <bryano@fb.com>
parents:
27768
diff
changeset
|
1560 |
with open(path, 'rb') as fp: |
14100
3e9e02a41dfb
util: really drop size from readfile
Matt Mackall <mpm@selenic.com>
parents:
14099
diff
changeset
|
1561 |
return fp.read() |
14099
0824a0a3cefc
util: add readfile() & writefile() helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14077
diff
changeset
|
1562 |
|
14167
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14147
diff
changeset
|
1563 |
def writefile(path, text): |
27778
4d10600c3f08
util: simplify file I/O functions using context managers
Bryan O'Sullivan <bryano@fb.com>
parents:
27768
diff
changeset
|
1564 |
with open(path, 'wb') as fp: |
14167
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14147
diff
changeset
|
1565 |
fp.write(text) |
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14147
diff
changeset
|
1566 |
|
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14147
diff
changeset
|
1567 |
def appendfile(path, text): |
27778
4d10600c3f08
util: simplify file I/O functions using context managers
Bryan O'Sullivan <bryano@fb.com>
parents:
27768
diff
changeset
|
1568 |
with open(path, 'ab') as fp: |
14099
0824a0a3cefc
util: add readfile() & writefile() helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14077
diff
changeset
|
1569 |
fp.write(text) |
0824a0a3cefc
util: add readfile() & writefile() helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14077
diff
changeset
|
1570 |
|
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
1571 |
class chunkbuffer(object): |
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
1572 |
"""Allow arbitrary sized chunks of data to be efficiently read from an |
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
1573 |
iterator over chunks of arbitrary size.""" |
1200 | 1574 |
|
5446
fa836e050c50
chunkbuffer: removed unused method and arg
Matt Mackall <mpm@selenic.com>
parents:
5420
diff
changeset
|
1575 |
def __init__(self, in_iter): |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
1576 |
"""in_iter is the iterator that's iterating over the input chunks. |
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
1577 |
targetsize is how big a buffer to try to maintain.""" |
11670
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1578 |
def splitbig(chunks): |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1579 |
for chunk in chunks: |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1580 |
if len(chunk) > 2**20: |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1581 |
pos = 0 |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1582 |
while pos < len(chunk): |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1583 |
end = pos + 2 ** 18 |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1584 |
yield chunk[pos:end] |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1585 |
pos = end |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1586 |
else: |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1587 |
yield chunk |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
1588 |
self.iter = splitbig(in_iter) |
25113
0ca8410ea345
util: drop alias for collections.deque
Martin von Zweigbergk <martinvonz@google.com>
parents:
25112
diff
changeset
|
1589 |
self._queue = collections.deque() |
26480
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1590 |
self._chunkoffset = 0 |
1200 | 1591 |
|
21018
c848bfd02366
util: support None size in chunkbuffer.read()
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
20835
diff
changeset
|
1592 |
def read(self, l=None): |
1200 | 1593 |
"""Read L bytes of data from the iterator of chunks of data. |
21018
c848bfd02366
util: support None size in chunkbuffer.read()
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
20835
diff
changeset
|
1594 |
Returns less than L bytes if the iterator runs dry. |
c848bfd02366
util: support None size in chunkbuffer.read()
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
20835
diff
changeset
|
1595 |
|
23139
e53f6b72a0e4
spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents:
23097
diff
changeset
|
1596 |
If size parameter is omitted, read everything""" |
26478
a3f7e5461dbd
util.chunkbuffer: special case reading everything
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26450
diff
changeset
|
1597 |
if l is None: |
a3f7e5461dbd
util.chunkbuffer: special case reading everything
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26450
diff
changeset
|
1598 |
return ''.join(self.iter) |
a3f7e5461dbd
util.chunkbuffer: special case reading everything
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26450
diff
changeset
|
1599 |
|
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1600 |
left = l |
17962
4c29668ca316
util: make chunkbuffer non-quadratic on Windows
Matt Mackall <mpm@selenic.com>
parents:
17560
diff
changeset
|
1601 |
buf = [] |
16873
37e081609828
util: simplify queue management in chunkbuffer
Bryan O'Sullivan <bryano@fb.com>
parents:
16834
diff
changeset
|
1602 |
queue = self._queue |
26478
a3f7e5461dbd
util.chunkbuffer: special case reading everything
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26450
diff
changeset
|
1603 |
while left > 0: |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1604 |
# refill the queue |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1605 |
if not queue: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1606 |
target = 2**18 |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1607 |
for chunk in self.iter: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1608 |
queue.append(chunk) |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1609 |
target -= len(chunk) |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1610 |
if target <= 0: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1611 |
break |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1612 |
if not queue: |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
1613 |
break |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1614 |
|
26480
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1615 |
# The easy way to do this would be to queue.popleft(), modify the |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1616 |
# chunk (if necessary), then queue.appendleft(). However, for cases |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1617 |
# where we read partial chunk content, this incurs 2 dequeue |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1618 |
# mutations and creates a new str for the remaining chunk in the |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1619 |
# queue. Our code below avoids this overhead. |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1620 |
|
26479
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1621 |
chunk = queue[0] |
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1622 |
chunkl = len(chunk) |
26480
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1623 |
offset = self._chunkoffset |
26479
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1624 |
|
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1625 |
# Use full chunk. |
26480
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1626 |
if offset == 0 and left >= chunkl: |
26479
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1627 |
left -= chunkl |
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1628 |
queue.popleft() |
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1629 |
buf.append(chunk) |
26480
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1630 |
# self._chunkoffset remains at 0. |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1631 |
continue |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1632 |
|
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1633 |
chunkremaining = chunkl - offset |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1634 |
|
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1635 |
# Use all of unconsumed part of chunk. |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1636 |
if left >= chunkremaining: |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1637 |
left -= chunkremaining |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1638 |
queue.popleft() |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1639 |
# offset == 0 is enabled by block above, so this won't merely |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1640 |
# copy via ``chunk[0:]``. |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1641 |
buf.append(chunk[offset:]) |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1642 |
self._chunkoffset = 0 |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1643 |
|
26479
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1644 |
# Partial chunk needed. |
46143f31290e
util.chunkbuffer: refactor chunk handling logic
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26478
diff
changeset
|
1645 |
else: |
26480
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1646 |
buf.append(chunk[offset:offset + left]) |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1647 |
self._chunkoffset += left |
6ae14d1ca3aa
util.chunkbuffer: avoid extra mutations when reading partial chunks
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26479
diff
changeset
|
1648 |
left -= chunkremaining |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1649 |
|
17962
4c29668ca316
util: make chunkbuffer non-quadratic on Windows
Matt Mackall <mpm@selenic.com>
parents:
17560
diff
changeset
|
1650 |
return ''.join(buf) |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
1651 |
|
2462
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1652 |
def filechunkiter(f, size=65536, limit=None): |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1653 |
"""Create a generator that produces the data in the file size |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1654 |
(default 65536) bytes at a time, up to optional limit (default is |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1655 |
to read all data). Chunks may be less than size bytes if the |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1656 |
chunk is the last chunk in the file, or the file is a socket or |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1657 |
some other type of file that sometimes reads less data than is |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1658 |
requested.""" |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1659 |
assert size >= 0 |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1660 |
assert limit is None or limit >= 0 |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1661 |
while True: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1662 |
if limit is None: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1663 |
nbytes = size |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1664 |
else: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1665 |
nbytes = min(limit, size) |
2462
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
1666 |
s = nbytes and f.read(nbytes) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1667 |
if not s: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1668 |
break |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1669 |
if limit: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1670 |
limit -= len(s) |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
1671 |
yield s |
1320
5f277e73778f
Fix up representation of dates in hgweb.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1312
diff
changeset
|
1672 |
|
19287
8b04e1344111
util: add an optional timestamp parameter to makedate
Bryan O'Sullivan <bryano@fb.com>
parents:
19286
diff
changeset
|
1673 |
def makedate(timestamp=None): |
8b04e1344111
util: add an optional timestamp parameter to makedate
Bryan O'Sullivan <bryano@fb.com>
parents:
19286
diff
changeset
|
1674 |
'''Return a unix timestamp (or the current time) as a (unixtime, |
8b04e1344111
util: add an optional timestamp parameter to makedate
Bryan O'Sullivan <bryano@fb.com>
parents:
19286
diff
changeset
|
1675 |
offset) tuple based off the local timezone.''' |
8b04e1344111
util: add an optional timestamp parameter to makedate
Bryan O'Sullivan <bryano@fb.com>
parents:
19286
diff
changeset
|
1676 |
if timestamp is None: |
8b04e1344111
util: add an optional timestamp parameter to makedate
Bryan O'Sullivan <bryano@fb.com>
parents:
19286
diff
changeset
|
1677 |
timestamp = time.time() |
19286
78501209488a
util: rename ct variable in makedate to timestamp
Bryan O'Sullivan <bryano@fb.com>
parents:
19211
diff
changeset
|
1678 |
if timestamp < 0: |
13063
e98581d44f0b
makedate: abort on negative timestamps (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13062
diff
changeset
|
1679 |
hint = _("check your clock") |
19286
78501209488a
util: rename ct variable in makedate to timestamp
Bryan O'Sullivan <bryano@fb.com>
parents:
19211
diff
changeset
|
1680 |
raise Abort(_("negative timestamp: %d") % timestamp, hint=hint) |
78501209488a
util: rename ct variable in makedate to timestamp
Bryan O'Sullivan <bryano@fb.com>
parents:
19211
diff
changeset
|
1681 |
delta = (datetime.datetime.utcfromtimestamp(timestamp) - |
78501209488a
util: rename ct variable in makedate to timestamp
Bryan O'Sullivan <bryano@fb.com>
parents:
19211
diff
changeset
|
1682 |
datetime.datetime.fromtimestamp(timestamp)) |
15505
ae04af1ce78d
makedate: wrong timezone offset if DST rules changed this year (issue2511)
Dmitry Panov <dop@itoolabs.com>
parents:
15496
diff
changeset
|
1683 |
tz = delta.days * 86400 + delta.seconds |
19286
78501209488a
util: rename ct variable in makedate to timestamp
Bryan O'Sullivan <bryano@fb.com>
parents:
19211
diff
changeset
|
1684 |
return timestamp, tz |
1329
8f06817bf266
Allow files to be opened in text mode, even on Windows.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1321
diff
changeset
|
1685 |
|
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
1686 |
def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): |
1321
b47f96a178a3
Clean up date and timezone handling.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1320
diff
changeset
|
1687 |
"""represent a (unixtime, offset) tuple as a localized time. |
b47f96a178a3
Clean up date and timezone handling.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1320
diff
changeset
|
1688 |
unixtime is seconds since the epoch, and offset is the time zone's |
28865
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1689 |
number of seconds away from UTC. |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1690 |
|
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1691 |
>>> datestr((0, 0)) |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1692 |
'Thu Jan 01 00:00:00 1970 +0000' |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1693 |
>>> datestr((42, 0)) |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1694 |
'Thu Jan 01 00:00:42 1970 +0000' |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1695 |
>>> datestr((-42, 0)) |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1696 |
'Wed Dec 31 23:59:18 1969 +0000' |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1697 |
>>> datestr((0x7fffffff, 0)) |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1698 |
'Tue Jan 19 03:14:07 2038 +0000' |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1699 |
>>> datestr((-0x80000000, 0)) |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1700 |
'Fri Dec 13 20:45:52 1901 +0000' |
16255662446d
util: add doctest to datestr()
Adrian Buehlmann <adrian@cadifra.com>
parents:
28864
diff
changeset
|
1701 |
""" |
1321
b47f96a178a3
Clean up date and timezone handling.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1320
diff
changeset
|
1702 |
t, tz = date or makedate() |
19989
c38c3fdc8b93
date: allow %z in format (issue4040)
Matt Mackall <mpm@selenic.com>
parents:
19951
diff
changeset
|
1703 |
if "%1" in format or "%2" in format or "%z" in format: |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
1704 |
sign = (tz > 0) and "-" or "+" |
9029
0001e49f1c11
compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents:
8938
diff
changeset
|
1705 |
minutes = abs(tz) // 60 |
27066
6f1f8e88f036
util.datestr: use divmod()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27016
diff
changeset
|
1706 |
q, r = divmod(minutes, 60) |
19989
c38c3fdc8b93
date: allow %z in format (issue4040)
Matt Mackall <mpm@selenic.com>
parents:
19951
diff
changeset
|
1707 |
format = format.replace("%z", "%1%2") |
27066
6f1f8e88f036
util.datestr: use divmod()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27016
diff
changeset
|
1708 |
format = format.replace("%1", "%c%02d" % (sign, q)) |
6f1f8e88f036
util.datestr: use divmod()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27016
diff
changeset
|
1709 |
format = format.replace("%2", "%02d" % r) |
28825
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1710 |
d = t - tz |
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1711 |
if d > 0x7fffffff: |
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1712 |
d = 0x7fffffff |
28864
b0811a9fe67c
date: fix boundary check of negative integer
Florent Gallaire <fgallaire@gmail.com>
parents:
28835
diff
changeset
|
1713 |
elif d < -0x80000000: |
b0811a9fe67c
date: fix boundary check of negative integer
Florent Gallaire <fgallaire@gmail.com>
parents:
28835
diff
changeset
|
1714 |
d = -0x80000000 |
28825
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1715 |
# Never use time.gmtime() and datetime.datetime.fromtimestamp() |
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1716 |
# because they use the gmtime() system call which is buggy on Windows |
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1717 |
# for negative values. |
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1718 |
t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d) |
87c6ad2251d8
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)
Florent Gallaire <fgallaire@gmail.com>
parents:
28818
diff
changeset
|
1719 |
s = t.strftime(format) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1976
diff
changeset
|
1720 |
return s |
1829
b0f6af327fd4
hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1721 |
|
6134
7b937b26adf7
Make annotae/grep print short dates with -q/--quiet.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6111
diff
changeset
|
1722 |
def shortdate(date=None): |
7b937b26adf7
Make annotae/grep print short dates with -q/--quiet.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6111
diff
changeset
|
1723 |
"""turn (timestamp, tzoff) tuple into iso 8631 date.""" |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
1724 |
return datestr(date, format='%Y-%m-%d') |
6134
7b937b26adf7
Make annotae/grep print short dates with -q/--quiet.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6111
diff
changeset
|
1725 |
|
26126
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1726 |
def parsetimezone(tz): |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1727 |
"""parse a timezone string and return an offset integer""" |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1728 |
if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1729 |
sign = (tz[0] == "+") and 1 or -1 |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1730 |
hours = int(tz[1:3]) |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1731 |
minutes = int(tz[3:5]) |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1732 |
return -sign * (hours * 60 + minutes) * 60 |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1733 |
if tz == "GMT" or tz == "UTC": |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1734 |
return 0 |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1735 |
return None |
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1736 |
|
5357
c6adf2be6069
util: add default argument to strdate
Bryan O'Sullivan <bos@serpentine.com>
parents:
5293
diff
changeset
|
1737 |
def strdate(string, format, defaults=[]): |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
1738 |
"""parse a localized time string and return a (unixtime, offset) tuple. |
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
1739 |
if the string cannot be parsed, ValueError is raised.""" |
3255
e96d2956eb4a
util.strdate: compute timestamp using UTC, not local timezone
Jose M. Prieto <jmprieto@gmx.net>
parents:
3176
diff
changeset
|
1740 |
# NOTE: unixtime = localunixtime + offset |
26126
7b625baed995
util: extract function that parses timezone string
Yuya Nishihara <yuya@tcha.org>
parents:
26098
diff
changeset
|
1741 |
offset, date = parsetimezone(string.split()[-1]), string |
13031
3da456d0c885
code style: prefer 'is' and 'is not' tests with singletons
Martin Geisler <mg@aragost.com>
parents:
12957
diff
changeset
|
1742 |
if offset is not None: |
3809
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
1743 |
date = " ".join(string.split()[:-1]) |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
1744 |
|
3812 | 1745 |
# add missing elements from defaults |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1746 |
usenow = False # default to using biased defaults |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1747 |
for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity |
3812 | 1748 |
found = [True for p in part if ("%"+p) in format] |
1749 |
if not found: |
|
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1750 |
date += "@" + defaults[part][usenow] |
3812 | 1751 |
format += "@%" + part[0] |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1752 |
else: |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1753 |
# We've found a specific time element, less specific time |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1754 |
# elements are relative to today |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1755 |
usenow = True |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
1756 |
|
3256
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1757 |
timetuple = time.strptime(date, format) |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1758 |
localunixtime = int(calendar.timegm(timetuple)) |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1759 |
if offset is None: |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1760 |
# local timezone |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1761 |
unixtime = int(time.mktime(timetuple)) |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1762 |
offset = unixtime - localunixtime |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1763 |
else: |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
1764 |
unixtime = localunixtime + offset |
3255
e96d2956eb4a
util.strdate: compute timestamp using UTC, not local timezone
Jose M. Prieto <jmprieto@gmx.net>
parents:
3176
diff
changeset
|
1765 |
return unixtime, offset |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
1766 |
|
26311
60dd8e3977f0
util: avoid mutable default arguments
Siddharth Agarwal <sid0@fb.com>
parents:
26267
diff
changeset
|
1767 |
def parsedate(date, formats=None, bias=None): |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1768 |
"""parse a localized date/time and return a (unixtime, offset) tuple. |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1769 |
|
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
1770 |
The date may be a "unixtime offset" string or in one of the specified |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1771 |
formats. If the date already is a (unixtime, offset) tuple, it is returned. |
18537
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1772 |
|
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1773 |
>>> parsedate(' today ') == parsedate(\ |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1774 |
datetime.date.today().strftime('%b %d')) |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1775 |
True |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1776 |
>>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1777 |
datetime.timedelta(days=1)\ |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1778 |
).strftime('%b %d')) |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1779 |
True |
18614
b2586e2cc67a
parsedate: understand "now" as a shortcut for the current time
Augie Fackler <raf@durin42.com>
parents:
18603
diff
changeset
|
1780 |
>>> now, tz = makedate() |
b2586e2cc67a
parsedate: understand "now" as a shortcut for the current time
Augie Fackler <raf@durin42.com>
parents:
18603
diff
changeset
|
1781 |
>>> strnow, strtz = parsedate('now') |
b2586e2cc67a
parsedate: understand "now" as a shortcut for the current time
Augie Fackler <raf@durin42.com>
parents:
18603
diff
changeset
|
1782 |
>>> (strnow - now) < 1 |
b2586e2cc67a
parsedate: understand "now" as a shortcut for the current time
Augie Fackler <raf@durin42.com>
parents:
18603
diff
changeset
|
1783 |
True |
b2586e2cc67a
parsedate: understand "now" as a shortcut for the current time
Augie Fackler <raf@durin42.com>
parents:
18603
diff
changeset
|
1784 |
>>> tz == strtz |
b2586e2cc67a
parsedate: understand "now" as a shortcut for the current time
Augie Fackler <raf@durin42.com>
parents:
18603
diff
changeset
|
1785 |
True |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1786 |
""" |
26311
60dd8e3977f0
util: avoid mutable default arguments
Siddharth Agarwal <sid0@fb.com>
parents:
26267
diff
changeset
|
1787 |
if bias is None: |
60dd8e3977f0
util: avoid mutable default arguments
Siddharth Agarwal <sid0@fb.com>
parents:
26267
diff
changeset
|
1788 |
bias = {} |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1789 |
if not date: |
3807
e43b48f0f718
parsedate: allow '' for epoch
Matt Mackall <mpm@selenic.com>
parents:
3806
diff
changeset
|
1790 |
return 0, 0 |
6230
c7253d1a774e
dates: Fix bare times to be relative to "today"
Matt Mackall <mpm@selenic.com>
parents:
6229
diff
changeset
|
1791 |
if isinstance(date, tuple) and len(date) == 2: |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1792 |
return date |
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
1793 |
if not formats: |
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
1794 |
formats = defaultdateformats |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1795 |
date = date.strip() |
18537
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1796 |
|
24188
5a7920c4d2ea
util: accept "now, today, yesterday" for dates even the locale is not english
André Klitzing <aklitzing@gmail.com>
parents:
24164
diff
changeset
|
1797 |
if date == 'now' or date == _('now'): |
18614
b2586e2cc67a
parsedate: understand "now" as a shortcut for the current time
Augie Fackler <raf@durin42.com>
parents:
18603
diff
changeset
|
1798 |
return makedate() |
24188
5a7920c4d2ea
util: accept "now, today, yesterday" for dates even the locale is not english
André Klitzing <aklitzing@gmail.com>
parents:
24164
diff
changeset
|
1799 |
if date == 'today' or date == _('today'): |
18537
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1800 |
date = datetime.date.today().strftime('%b %d') |
24188
5a7920c4d2ea
util: accept "now, today, yesterday" for dates even the locale is not english
André Klitzing <aklitzing@gmail.com>
parents:
24164
diff
changeset
|
1801 |
elif date == 'yesterday' or date == _('yesterday'): |
18537
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1802 |
date = (datetime.date.today() - |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1803 |
datetime.timedelta(days=1)).strftime('%b %d') |
ae60735e37d2
dates: support 'today' and 'yesterday' in parsedate (issue3764)
Paul Cavallaro <ptc@fb.com>
parents:
18326
diff
changeset
|
1804 |
|
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
1805 |
try: |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1806 |
when, offset = map(int, date.split(' ')) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1807 |
except ValueError: |
3812 | 1808 |
# fill out defaults |
1809 |
now = makedate() |
|
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1810 |
defaults = {} |
13200
6f011cf52f9a
avoid .split() in for loops and use tuples instead
David Soria Parra <dsp@php.net>
parents:
13197
diff
changeset
|
1811 |
for part in ("d", "mb", "yY", "HI", "M", "S"): |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1812 |
# this piece is for rounding the specific end of unknowns |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1813 |
b = bias.get(part) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1814 |
if b is None: |
3812 | 1815 |
if part[0] in "HMS": |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1816 |
b = "00" |
3812 | 1817 |
else: |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1818 |
b = "0" |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1819 |
|
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1820 |
# this piece is for matching the generic end to today's date |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1821 |
n = datestr(now, "%" + part[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1822 |
|
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1823 |
defaults[part] = (b, n) |
3812 | 1824 |
|
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1825 |
for format in formats: |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1826 |
try: |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
1827 |
when, offset = strdate(date, format, defaults) |
6087
12856a1742dc
better handle errors with date parsing (issue983)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5917
diff
changeset
|
1828 |
except (ValueError, OverflowError): |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1829 |
pass |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1830 |
else: |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1831 |
break |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1832 |
else: |
12105
6f58430dfdd0
util: get rid of extra trailing whitespace in parsedate abort message
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
12088
diff
changeset
|
1833 |
raise Abort(_('invalid date: %r') % date) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1834 |
# validate explicit (probably user-specified) date and |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1835 |
# time zone offset. values must fit in signed 32 bits for |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1836 |
# current 32-bit linux runtimes. timezones go from UTC-12 |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1837 |
# to UTC+14 |
28864
b0811a9fe67c
date: fix boundary check of negative integer
Florent Gallaire <fgallaire@gmail.com>
parents:
28835
diff
changeset
|
1838 |
if when < -0x80000000 or when > 0x7fffffff: |
3806
92a3532a01d9
parsedate: use Abort rather than ValueError
Matt Mackall <mpm@selenic.com>
parents:
3784
diff
changeset
|
1839 |
raise Abort(_('date exceeds 32 bits: %d') % when) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1840 |
if offset < -50400 or offset > 43200: |
3806
92a3532a01d9
parsedate: use Abort rather than ValueError
Matt Mackall <mpm@selenic.com>
parents:
3784
diff
changeset
|
1841 |
raise Abort(_('impossible time zone offset: %d') % offset) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
1842 |
return when, offset |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
1843 |
|
3812 | 1844 |
def matchdate(date): |
1845 |
"""Return a function that matches a given date match specifier |
|
1846 |
||
1847 |
Formats include: |
|
1848 |
||
1849 |
'{date}' match a given date to the accuracy provided |
|
1850 |
||
1851 |
'<{date}' on or before a given date |
|
1852 |
||
1853 |
'>{date}' on or after a given date |
|
1854 |
||
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1855 |
>>> p1 = parsedate("10:29:59") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1856 |
>>> p2 = parsedate("10:30:00") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1857 |
>>> p3 = parsedate("10:30:59") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1858 |
>>> p4 = parsedate("10:31:00") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1859 |
>>> p5 = parsedate("Sep 15 10:30:00 1999") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1860 |
>>> f = matchdate("10:30") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1861 |
>>> f(p1[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1862 |
False |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1863 |
>>> f(p2[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1864 |
True |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1865 |
>>> f(p3[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1866 |
True |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1867 |
>>> f(p4[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1868 |
False |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1869 |
>>> f(p5[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
1870 |
False |
3812 | 1871 |
""" |
1872 |
||
1873 |
def lower(date): |
|
20679
0916f829eb8d
util: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents:
20542
diff
changeset
|
1874 |
d = {'mb': "1", 'd': "1"} |
6230
c7253d1a774e
dates: Fix bare times to be relative to "today"
Matt Mackall <mpm@selenic.com>
parents:
6229
diff
changeset
|
1875 |
return parsedate(date, extendeddateformats, d)[0] |
3812 | 1876 |
|
1877 |
def upper(date): |
|
20679
0916f829eb8d
util: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents:
20542
diff
changeset
|
1878 |
d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"} |
13200
6f011cf52f9a
avoid .split() in for loops and use tuples instead
David Soria Parra <dsp@php.net>
parents:
13197
diff
changeset
|
1879 |
for days in ("31", "30", "29"): |
3812 | 1880 |
try: |
1881 |
d["d"] = days |
|
1882 |
return parsedate(date, extendeddateformats, d)[0] |
|
16688
cfb6682961b8
cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents:
16397
diff
changeset
|
1883 |
except Abort: |
3812 | 1884 |
pass |
1885 |
d["d"] = "28" |
|
1886 |
return parsedate(date, extendeddateformats, d)[0] |
|
1887 |
||
7953
8c6f823efcc9
Correct a bug on date formats with '>' or '<' accompanied by space characters.
Justin Peng <justin.peng.sw@gmail.com>
parents:
7948
diff
changeset
|
1888 |
date = date.strip() |
13780
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1889 |
|
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1890 |
if not date: |
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1891 |
raise Abort(_("dates cannot consist entirely of whitespace")) |
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1892 |
elif date[0] == "<": |
13869
b470894c33f8
date: fixup breakage from ">" fix
Matt Mackall <mpm@selenic.com>
parents:
13867
diff
changeset
|
1893 |
if not date[1:]: |
13886
fe48c57390f2
help/dates: use DATE as place-holder in help and abort texts
Martin Geisler <mg@aragost.com>
parents:
13879
diff
changeset
|
1894 |
raise Abort(_("invalid day spec, use '<DATE'")) |
3812 | 1895 |
when = upper(date[1:]) |
1896 |
return lambda x: x <= when |
|
1897 |
elif date[0] == ">": |
|
13869
b470894c33f8
date: fixup breakage from ">" fix
Matt Mackall <mpm@selenic.com>
parents:
13867
diff
changeset
|
1898 |
if not date[1:]: |
13886
fe48c57390f2
help/dates: use DATE as place-holder in help and abort texts
Martin Geisler <mg@aragost.com>
parents:
13879
diff
changeset
|
1899 |
raise Abort(_("invalid day spec, use '>DATE'")) |
3812 | 1900 |
when = lower(date[1:]) |
1901 |
return lambda x: x >= when |
|
1902 |
elif date[0] == "-": |
|
1903 |
try: |
|
1904 |
days = int(date[1:]) |
|
1905 |
except ValueError: |
|
1906 |
raise Abort(_("invalid day spec: %s") % date[1:]) |
|
13889
9a96efc4af8a
util: make 'hg log -d --2' abort (issue2734)
Yun Lee <yunlee.bj@gmail.com>
parents:
13886
diff
changeset
|
1907 |
if days < 0: |
23917
3cbb5bf4035d
messages: quote "hg help" hints consistently
Wagner Bruna <wbruna@yahoo.com>
parents:
23899
diff
changeset
|
1908 |
raise Abort(_('%s must be nonnegative (see "hg help dates")') |
13889
9a96efc4af8a
util: make 'hg log -d --2' abort (issue2734)
Yun Lee <yunlee.bj@gmail.com>
parents:
13886
diff
changeset
|
1909 |
% date[1:]) |
3812 | 1910 |
when = makedate()[0] - days * 3600 * 24 |
3813 | 1911 |
return lambda x: x >= when |
3812 | 1912 |
elif " to " in date: |
1913 |
a, b = date.split(" to ") |
|
1914 |
start, stop = lower(a), upper(b) |
|
1915 |
return lambda x: x >= start and x <= stop |
|
1916 |
else: |
|
1917 |
start, stop = lower(date), upper(date) |
|
1918 |
return lambda x: x >= start and x <= stop |
|
1919 |
||
26481
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1920 |
def stringmatcher(pattern): |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1921 |
""" |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1922 |
accepts a string, possibly starting with 're:' or 'literal:' prefix. |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1923 |
returns the matcher name, pattern, and matcher function. |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1924 |
missing or unknown prefixes are treated as literal matches. |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1925 |
|
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1926 |
helper for tests: |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1927 |
>>> def test(pattern, *tests): |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1928 |
... kind, pattern, matcher = stringmatcher(pattern) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1929 |
... return (kind, pattern, [bool(matcher(t)) for t in tests]) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1930 |
|
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1931 |
exact matching (no prefix): |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1932 |
>>> test('abcdefg', 'abc', 'def', 'abcdefg') |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1933 |
('literal', 'abcdefg', [False, False, True]) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1934 |
|
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1935 |
regex matching ('re:' prefix) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1936 |
>>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar') |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1937 |
('re', 'a.+b', [False, False, True]) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1938 |
|
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1939 |
force exact matches ('literal:' prefix) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1940 |
>>> test('literal:re:foobar', 'foobar', 're:foobar') |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1941 |
('literal', 're:foobar', [False, True]) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1942 |
|
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1943 |
unknown prefixes are ignored and treated as literals |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1944 |
>>> test('foo:bar', 'foo', 'bar', 'foo:bar') |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1945 |
('literal', 'foo:bar', [False, False, True]) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1946 |
""" |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1947 |
if pattern.startswith('re:'): |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1948 |
pattern = pattern[3:] |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1949 |
try: |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1950 |
regex = remod.compile(pattern) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1951 |
except remod.error as e: |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1952 |
raise error.ParseError(_('invalid regular expression: %s') |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1953 |
% e) |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1954 |
return 're', pattern, regex.search |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1955 |
elif pattern.startswith('literal:'): |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1956 |
pattern = pattern[8:] |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1957 |
return 'literal', pattern, pattern.__eq__ |
7d132557e44a
util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents:
26480
diff
changeset
|
1958 |
|
1903
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1959 |
def shortuser(user): |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1960 |
"""Return a short representation of a user name or email address.""" |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1961 |
f = user.find('@') |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1962 |
if f >= 0: |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1963 |
user = user[:f] |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1964 |
f = user.find('<') |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1965 |
if f >= 0: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1966 |
user = user[f + 1:] |
3176
7492b33bdd9f
shortuser should stop before the first space character.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3147
diff
changeset
|
1967 |
f = user.find(' ') |
7492b33bdd9f
shortuser should stop before the first space character.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3147
diff
changeset
|
1968 |
if f >= 0: |
7492b33bdd9f
shortuser should stop before the first space character.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3147
diff
changeset
|
1969 |
user = user[:f] |
3533
bb44489b901f
shortname: truncate at '.' too
Matt Mackall <mpm@selenic.com>
parents:
3466
diff
changeset
|
1970 |
f = user.find('.') |
bb44489b901f
shortname: truncate at '.' too
Matt Mackall <mpm@selenic.com>
parents:
3466
diff
changeset
|
1971 |
if f >= 0: |
bb44489b901f
shortname: truncate at '.' too
Matt Mackall <mpm@selenic.com>
parents:
3466
diff
changeset
|
1972 |
user = user[:f] |
1903
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1973 |
return user |
1920 | 1974 |
|
16360
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1975 |
def emailuser(user): |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1976 |
"""Return the user portion of an email address.""" |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1977 |
f = user.find('@') |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1978 |
if f >= 0: |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1979 |
user = user[:f] |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1980 |
f = user.find('<') |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1981 |
if f >= 0: |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1982 |
user = user[f + 1:] |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1983 |
return user |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
15720
diff
changeset
|
1984 |
|
5975
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1985 |
def email(author): |
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1986 |
'''get email of author.''' |
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1987 |
r = author.find('>') |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1988 |
if r == -1: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1989 |
r = None |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1990 |
return author[author.find('<') + 1:r] |
5975
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1991 |
|
3767
1861fa38a6a7
Move ellipsis code to util.ellipsis() and improve maxlength handling.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3721
diff
changeset
|
1992 |
def ellipsis(text, maxlength=400): |
21857
86c2d792a4b7
util: replace 'ellipsis' implementation by 'encoding.trim'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
21813
diff
changeset
|
1993 |
"""Trim string to at most maxlength (default: 400) columns in display.""" |
86c2d792a4b7
util: replace 'ellipsis' implementation by 'encoding.trim'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
21813
diff
changeset
|
1994 |
return encoding.trim(text, maxlength, ellipsis='...') |
3767
1861fa38a6a7
Move ellipsis code to util.ellipsis() and improve maxlength handling.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3721
diff
changeset
|
1995 |
|
18735
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
1996 |
def unitcountfn(*unittable): |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
1997 |
'''return a function that renders a readable count of some quantity''' |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
1998 |
|
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
1999 |
def go(count): |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2000 |
for multiplier, divisor, format in unittable: |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2001 |
if count >= divisor * multiplier: |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2002 |
return format % (count / float(divisor)) |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2003 |
return unittable[-1][2] % count |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2004 |
|
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2005 |
return go |
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2006 |
|
716cad930691
util: generalize bytecount to unitcountfn
Bryan O'Sullivan <bryano@fb.com>
parents:
18678
diff
changeset
|
2007 |
bytecount = unitcountfn( |
16397
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2008 |
(100, 1 << 30, _('%.0f GB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2009 |
(10, 1 << 30, _('%.1f GB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2010 |
(1, 1 << 30, _('%.2f GB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2011 |
(100, 1 << 20, _('%.0f MB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2012 |
(10, 1 << 20, _('%.1f MB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2013 |
(1, 1 << 20, _('%.2f MB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2014 |
(100, 1 << 10, _('%.0f KB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2015 |
(10, 1 << 10, _('%.1f KB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2016 |
(1, 1 << 10, _('%.2f KB')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2017 |
(1, 1, _('%.0f bytes')), |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2018 |
) |
f0f7f3fab315
util: create bytecount array just once
Matt Mackall <mpm@selenic.com>
parents:
16383
diff
changeset
|
2019 |
|
5291
23651848d638
extdiff: avoid repr() doubling paths backslashes under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
5213
diff
changeset
|
2020 |
def uirepr(s): |
23651848d638
extdiff: avoid repr() doubling paths backslashes under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
5213
diff
changeset
|
2021 |
# Avoid double backslash in Windows path repr() |
23651848d638
extdiff: avoid repr() doubling paths backslashes under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
5213
diff
changeset
|
2022 |
return repr(s).replace('\\\\', '\\') |
7547
4949729ee9ee
python implementation of diffstat
Alexander Solovyov <piranha@piranha.org.ua>
parents:
7537
diff
changeset
|
2023 |
|
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2024 |
# delay import of textwrap |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2025 |
def MBTextWrapper(**kwargs): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2026 |
class tw(textwrap.TextWrapper): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2027 |
""" |
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2028 |
Extend TextWrapper for width-awareness. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2029 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2030 |
Neither number of 'bytes' in any encoding nor 'characters' is |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2031 |
appropriate to calculate terminal columns for specified string. |
12957
9f2ac318b92e
util: clarify purpose of MBTextWrapper class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
12938
diff
changeset
|
2032 |
|
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2033 |
Original TextWrapper implementation uses built-in 'len()' directly, |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2034 |
so overriding is needed to use width information of each characters. |
12957
9f2ac318b92e
util: clarify purpose of MBTextWrapper class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
12938
diff
changeset
|
2035 |
|
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2036 |
In addition, characters classified into 'ambiguous' width are |
17424
e7cfe3587ea4
fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents:
17391
diff
changeset
|
2037 |
treated as wide in East Asian area, but as narrow in other. |
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2038 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2039 |
This requires use decision to determine width of such characters. |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2040 |
""" |
15065
24a6c3f903bb
util: wrap lines with multi-byte characters correctly (issue2943)
Mads Kiilerich <mads@kiilerich.com>
parents:
15024
diff
changeset
|
2041 |
def _cutdown(self, ucstr, space_left): |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2042 |
l = 0 |
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2043 |
colwidth = encoding.ucolwidth |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2044 |
for i in xrange(len(ucstr)): |
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2045 |
l += colwidth(ucstr[i]) |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2046 |
if space_left < l: |
15065
24a6c3f903bb
util: wrap lines with multi-byte characters correctly (issue2943)
Mads Kiilerich <mads@kiilerich.com>
parents:
15024
diff
changeset
|
2047 |
return (ucstr[:i], ucstr[i:]) |
24a6c3f903bb
util: wrap lines with multi-byte characters correctly (issue2943)
Mads Kiilerich <mads@kiilerich.com>
parents:
15024
diff
changeset
|
2048 |
return ucstr, '' |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2049 |
|
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2050 |
# overriding of base class |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2051 |
def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2052 |
space_left = max(width - cur_len, 1) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2053 |
|
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2054 |
if self.break_long_words: |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2055 |
cut, res = self._cutdown(reversed_chunks[-1], space_left) |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2056 |
cur_line.append(cut) |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2057 |
reversed_chunks[-1] = res |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2058 |
elif not cur_line: |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2059 |
cur_line.append(reversed_chunks.pop()) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2060 |
|
26201
c5b2074ae8c0
util: capitalize Python in MBTextWrapper._wrap_chunks comment
timeless@mozdev.org
parents:
26126
diff
changeset
|
2061 |
# this overriding code is imported from TextWrapper of Python 2.6 |
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2062 |
# to calculate columns of string by 'encoding.ucolwidth()' |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2063 |
def _wrap_chunks(self, chunks): |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2064 |
colwidth = encoding.ucolwidth |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2065 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2066 |
lines = [] |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2067 |
if self.width <= 0: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2068 |
raise ValueError("invalid width %r (must be > 0)" % self.width) |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2069 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2070 |
# Arrange in reverse order so items can be efficiently popped |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2071 |
# from a stack of chucks. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2072 |
chunks.reverse() |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2073 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2074 |
while chunks: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2075 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2076 |
# Start the list of chunks that will make up the current line. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2077 |
# cur_len is just the length of all the chunks in cur_line. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2078 |
cur_line = [] |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2079 |
cur_len = 0 |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2080 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2081 |
# Figure out which static string will prefix this line. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2082 |
if lines: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2083 |
indent = self.subsequent_indent |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2084 |
else: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2085 |
indent = self.initial_indent |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2086 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2087 |
# Maximum width for this line. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2088 |
width = self.width - len(indent) |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2089 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2090 |
# First chunk on line is whitespace -- drop it, unless this |
17424
e7cfe3587ea4
fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents:
17391
diff
changeset
|
2091 |
# is the very beginning of the text (i.e. no lines started yet). |
15066
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2092 |
if self.drop_whitespace and chunks[-1].strip() == '' and lines: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2093 |
del chunks[-1] |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2094 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2095 |
while chunks: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2096 |
l = colwidth(chunks[-1]) |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2097 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2098 |
# Can at least squeeze this chunk onto the current line. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2099 |
if cur_len + l <= width: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2100 |
cur_line.append(chunks.pop()) |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2101 |
cur_len += l |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2102 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2103 |
# Nope, this line is full. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2104 |
else: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2105 |
break |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2106 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2107 |
# The current line is full, and the next chunk is too big to |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2108 |
# fit on *any* line (not just this one). |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2109 |
if chunks and colwidth(chunks[-1]) > width: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2110 |
self._handle_long_word(chunks, cur_line, cur_len, width) |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2111 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2112 |
# If the last chunk on this line is all whitespace, drop it. |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2113 |
if (self.drop_whitespace and |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2114 |
cur_line and cur_line[-1].strip() == ''): |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2115 |
del cur_line[-1] |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2116 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2117 |
# Convert current line back to a string and store it in list |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2118 |
# of all lines (return value). |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2119 |
if cur_line: |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2120 |
lines.append(indent + ''.join(cur_line)) |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2121 |
|
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2122 |
return lines |
24efa83d81cb
i18n: calculate terminal columns by width information of each characters
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15065
diff
changeset
|
2123 |
|
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2124 |
global MBTextWrapper |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2125 |
MBTextWrapper = tw |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
2126 |
return tw(**kwargs) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2127 |
|
12698
7aef77e74cf3
util: make wrap() require a width argument
Matt Mackall <mpm@selenic.com>
parents:
12689
diff
changeset
|
2128 |
def wrap(line, width, initindent='', hangindent=''): |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2129 |
maxindent = max(len(hangindent), len(initindent)) |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2130 |
if width <= maxindent: |
9417
4c3fb45123e5
util, minirst: do not crash with COLUMNS=0
Martin Geisler <mg@lazybytes.net>
parents:
9397
diff
changeset
|
2131 |
# adjust for weird terminal size |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2132 |
width = max(78, maxindent + 1) |
15065
24a6c3f903bb
util: wrap lines with multi-byte characters correctly (issue2943)
Mads Kiilerich <mads@kiilerich.com>
parents:
15024
diff
changeset
|
2133 |
line = line.decode(encoding.encoding, encoding.encodingmode) |
24a6c3f903bb
util: wrap lines with multi-byte characters correctly (issue2943)
Mads Kiilerich <mads@kiilerich.com>
parents:
15024
diff
changeset
|
2134 |
initindent = initindent.decode(encoding.encoding, encoding.encodingmode) |
24a6c3f903bb
util: wrap lines with multi-byte characters correctly (issue2943)
Mads Kiilerich <mads@kiilerich.com>
parents:
15024
diff
changeset
|
2135 |
hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2136 |
wrapper = MBTextWrapper(width=width, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2137 |
initial_indent=initindent, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
2138 |
subsequent_indent=hangindent) |
15065
24a6c3f903bb
util: wrap lines with multi-byte characters correctly (issue2943)
Mads Kiilerich <mads@kiilerich.com>
parents:
15024
diff
changeset
|
2139 |
return wrapper.fill(line).encode(encoding.encoding) |
8938
9b8c9266c59d
commands: wrap short descriptions in 'hg help'
Martin Geisler <mg@lazybytes.net>
parents:
8785
diff
changeset
|
2140 |
|
7879
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
2141 |
def iterlines(iterator): |
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
2142 |
for chunk in iterator: |
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
2143 |
for line in chunk.splitlines(): |
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
2144 |
yield line |
9610
d78fe60f6bda
make path expanding more consistent
Alexander Solovyov <piranha@piranha.org.ua>
parents:
9569
diff
changeset
|
2145 |
|
d78fe60f6bda
make path expanding more consistent
Alexander Solovyov <piranha@piranha.org.ua>
parents:
9569
diff
changeset
|
2146 |
def expandpath(path): |
d78fe60f6bda
make path expanding more consistent
Alexander Solovyov <piranha@piranha.org.ua>
parents:
9569
diff
changeset
|
2147 |
return os.path.expanduser(os.path.expandvars(path)) |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2148 |
|
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2149 |
def hgcmd(): |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2150 |
"""Return the command used to execute current hg |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2151 |
|
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2152 |
This is different from hgexecutable() because on Windows we want |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2153 |
to avoid things opening new shell windows like batch files, so we |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2154 |
get either the python call or current executable. |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2155 |
""" |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
2156 |
if mainfrozen(): |
27766
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
2157 |
if getattr(sys, 'frozen', None) == 'macosx_app': |
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
2158 |
# Env variable set by py2app |
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
2159 |
return [os.environ['EXECUTABLEPATH']] |
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
2160 |
else: |
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
2161 |
return [sys.executable] |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
2162 |
return gethgcmd() |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2163 |
|
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2164 |
def rundetached(args, condfn): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2165 |
"""Execute the argument list in a detached process. |
10422
600142e7a028
util: fix trailing whitespace found by check-code
Augie Fackler <durin42@gmail.com>
parents:
10344
diff
changeset
|
2166 |
|
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2167 |
condfn is a callable which is called repeatedly and should return |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2168 |
True once the child process is known to have started successfully. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2169 |
At this point, the child process PID is returned. If the child |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2170 |
process fails to start or finishes before condfn() evaluates to |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2171 |
True, return -1. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2172 |
""" |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2173 |
# Windows case is easier because the child process is either |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2174 |
# successfully starting and validating the condition or exiting |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2175 |
# on failure. We just poll on its PID. On Unix, if the child |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2176 |
# process fails to start, it will be left in a zombie state until |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2177 |
# the parent wait on it, which we cannot do since we expect a long |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2178 |
# running process on success. Instead we listen for SIGCHLD telling |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2179 |
# us our child process terminated. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2180 |
terminated = set() |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2181 |
def handler(signum, frame): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2182 |
terminated.add(os.wait()) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2183 |
prevhandler = None |
14968
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
2184 |
SIGCHLD = getattr(signal, 'SIGCHLD', None) |
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
2185 |
if SIGCHLD is not None: |
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
2186 |
prevhandler = signal.signal(SIGCHLD, handler) |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2187 |
try: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2188 |
pid = spawndetached(args) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2189 |
while not condfn(): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2190 |
if ((pid in terminated or not testpid(pid)) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2191 |
and not condfn()): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2192 |
return -1 |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2193 |
time.sleep(0.1) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2194 |
return pid |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2195 |
finally: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2196 |
if prevhandler is not None: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
2197 |
signal.signal(signal.SIGCHLD, prevhandler) |
10438
e6dc44147234
util: add any() and all() functions for Python 2.4 compatibility
Steve Losh <steve@stevelosh.com>
parents:
10422
diff
changeset
|
2198 |
|
13392
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2199 |
def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
11988
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2200 |
"""Return the result of interpolating items in the mapping into string s. |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2201 |
|
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2202 |
prefix is a single character string, or a two character string with |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2203 |
a backslash as the first character if the prefix needs to be escaped in |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2204 |
a regular expression. |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2205 |
|
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2206 |
fn is an optional function that will be applied to the replacement text |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2207 |
just before replacement. |
13392
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2208 |
|
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2209 |
escape_prefix is an optional flag that allows using doubled prefix for |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2210 |
its escaping. |
11988
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2211 |
""" |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2212 |
fn = fn or (lambda s: s) |
13392
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2213 |
patterns = '|'.join(mapping.keys()) |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2214 |
if escape_prefix: |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2215 |
patterns += '|' + prefix |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2216 |
if len(prefix) > 1: |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2217 |
prefix_char = prefix[1:] |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2218 |
else: |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2219 |
prefix_char = prefix |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
2220 |
mapping[prefix_char] = prefix_char |
21907
7e5dfa00e3c2
util: rename 're' to 'remod'
Siddharth Agarwal <sid0@fb.com>
parents:
21857
diff
changeset
|
2221 |
r = remod.compile(r'%s(%s)' % (prefix, patterns)) |
11988
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2222 |
return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
2223 |
|
12076
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2224 |
def getport(port): |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2225 |
"""Return the port for a given network service. |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2226 |
|
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2227 |
If port is an integer, it's returned as is. If it's a string, it's |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2228 |
looked up using socket.getservbyname(). If there's no matching |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26493
diff
changeset
|
2229 |
service, error.Abort is raised. |
12076
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2230 |
""" |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2231 |
try: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2232 |
return int(port) |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2233 |
except ValueError: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2234 |
pass |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2235 |
|
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2236 |
try: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2237 |
return socket.getservbyname(port) |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2238 |
except socket.error: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
2239 |
raise Abort(_("no port number associated with service '%s'") % port) |
12087
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2240 |
|
12088
1f71dffabc53
parsebool: accept always as true and never as false
Augie Fackler <durin42@gmail.com>
parents:
12087
diff
changeset
|
2241 |
_booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True, |
1f71dffabc53
parsebool: accept always as true and never as false
Augie Fackler <durin42@gmail.com>
parents:
12087
diff
changeset
|
2242 |
'0': False, 'no': False, 'false': False, 'off': False, |
1f71dffabc53
parsebool: accept always as true and never as false
Augie Fackler <durin42@gmail.com>
parents:
12087
diff
changeset
|
2243 |
'never': False} |
12087
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2244 |
|
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2245 |
def parsebool(s): |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2246 |
"""Parse s into a boolean. |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2247 |
|
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2248 |
If s is not a valid boolean, returns None. |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2249 |
""" |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
2250 |
return _booleans.get(s.lower(), None) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2251 |
|
14077
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2252 |
_hexdig = '0123456789ABCDEFabcdef' |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2253 |
_hextochr = dict((a + b, chr(int(a + b, 16))) |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2254 |
for a in _hexdig for b in _hexdig) |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2255 |
|
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2256 |
def _urlunquote(s): |
17425
e95ec38f86b0
fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents:
17424
diff
changeset
|
2257 |
"""Decode HTTP/HTML % encoding. |
e95ec38f86b0
fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents:
17424
diff
changeset
|
2258 |
|
e95ec38f86b0
fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents:
17424
diff
changeset
|
2259 |
>>> _urlunquote('abc%20def') |
e95ec38f86b0
fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents:
17424
diff
changeset
|
2260 |
'abc def' |
e95ec38f86b0
fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents:
17424
diff
changeset
|
2261 |
""" |
14077
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2262 |
res = s.split('%') |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2263 |
# fastpath |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2264 |
if len(res) == 1: |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2265 |
return s |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2266 |
s = res[0] |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2267 |
for item in res[1:]: |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2268 |
try: |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2269 |
s += _hextochr[item[:2]] + item[2:] |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2270 |
except KeyError: |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2271 |
s += '%' + item |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2272 |
except UnicodeDecodeError: |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2273 |
s += unichr(int(item[:2], 16)) + item[2:] |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2274 |
return s |
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2275 |
|
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2276 |
class url(object): |
14146
1618c4f6f15b
tests: use raw string for url tests of '\' handling
Mads Kiilerich <mads@kiilerich.com>
parents:
14100
diff
changeset
|
2277 |
r"""Reliable URL parser. |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2278 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2279 |
This parses URLs and provides attributes for the following |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2280 |
components: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2281 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2282 |
<scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2283 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2284 |
Missing components are set to None. The only exception is |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2285 |
fragment, which is set to '' if present but empty. |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2286 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2287 |
If parsefragment is False, fragment is included in query. If |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2288 |
parsequery is False, query is included in path. If both are |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2289 |
False, both fragment and query are included in path. |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2290 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2291 |
See http://www.ietf.org/rfc/rfc2396.txt for more information. |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2292 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2293 |
Note that for backward compatibility reasons, bundle URLs do not |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2294 |
take host names. That means 'bundle://../' has a path of '../'. |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2295 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2296 |
Examples: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2297 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2298 |
>>> url('http://www.ietf.org/rfc/rfc2396.txt') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2299 |
<url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2300 |
>>> url('ssh://[::1]:2200//home/joe/repo') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2301 |
<url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2302 |
>>> url('file:///home/joe/repo') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2303 |
<url scheme: 'file', path: '/home/joe/repo'> |
14915
28edd65000d9
url: handle urls of the form file:///c:/foo/bar/ correctly
Matt Mackall <mpm@selenic.com>
parents:
14825
diff
changeset
|
2304 |
>>> url('file:///c:/temp/foo/') |
28edd65000d9
url: handle urls of the form file:///c:/foo/bar/ correctly
Matt Mackall <mpm@selenic.com>
parents:
14825
diff
changeset
|
2305 |
<url scheme: 'file', path: 'c:/temp/foo/'> |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2306 |
>>> url('bundle:foo') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2307 |
<url scheme: 'bundle', path: 'foo'> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2308 |
>>> url('bundle://../foo') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2309 |
<url scheme: 'bundle', path: '../foo'> |
14146
1618c4f6f15b
tests: use raw string for url tests of '\' handling
Mads Kiilerich <mads@kiilerich.com>
parents:
14100
diff
changeset
|
2310 |
>>> url(r'c:\foo\bar') |
1618c4f6f15b
tests: use raw string for url tests of '\' handling
Mads Kiilerich <mads@kiilerich.com>
parents:
14100
diff
changeset
|
2311 |
<url path: 'c:\\foo\\bar'> |
14699
388af80c058b
url: catch UNC paths as yet another Windows special case (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14640
diff
changeset
|
2312 |
>>> url(r'\\blah\blah\blah') |
388af80c058b
url: catch UNC paths as yet another Windows special case (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14640
diff
changeset
|
2313 |
<url path: '\\\\blah\\blah\\blah'> |
15074
64fbd0de9773
url: parse fragments first (issue2997)
Matt Mackall <mpm@selenic.com>
parents:
15066
diff
changeset
|
2314 |
>>> url(r'\\blah\blah\blah#baz') |
64fbd0de9773
url: parse fragments first (issue2997)
Matt Mackall <mpm@selenic.com>
parents:
15066
diff
changeset
|
2315 |
<url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
20106
c33d9217e99d
util: url keeps backslash in paths
Simon Heimberg <simohe@besonet.ch>
parents:
20000
diff
changeset
|
2316 |
>>> url(r'file:///C:\users\me') |
c33d9217e99d
util: url keeps backslash in paths
Simon Heimberg <simohe@besonet.ch>
parents:
20000
diff
changeset
|
2317 |
<url scheme: 'file', path: 'C:\\users\\me'> |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2318 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2319 |
Authentication credentials: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2320 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2321 |
>>> url('ssh://joe:xyz@x/repo') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2322 |
<url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2323 |
>>> url('ssh://joe@x/repo') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2324 |
<url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2325 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2326 |
Query strings and fragments: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2327 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2328 |
>>> url('http://host/a?b#c') |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2329 |
<url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2330 |
>>> url('http://host/a?b#c', parsequery=False, parsefragment=False) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2331 |
<url scheme: 'http', host: 'host', path: 'a?b#c'> |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2332 |
""" |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2333 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2334 |
_safechars = "!~*'()+" |
20106
c33d9217e99d
util: url keeps backslash in paths
Simon Heimberg <simohe@besonet.ch>
parents:
20000
diff
changeset
|
2335 |
_safepchars = "/!~*'()+:\\" |
21907
7e5dfa00e3c2
util: rename 're' to 'remod'
Siddharth Agarwal <sid0@fb.com>
parents:
21857
diff
changeset
|
2336 |
_matchscheme = remod.compile(r'^[a-zA-Z0-9+.\-]+:').match |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2337 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2338 |
def __init__(self, path, parsequery=True, parsefragment=True): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2339 |
# We slowly chomp away at path until we have only the path left |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2340 |
self.scheme = self.user = self.passwd = self.host = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2341 |
self.port = self.path = self.query = self.fragment = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2342 |
self._localpath = True |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2343 |
self._hostport = '' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2344 |
self._origpath = path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2345 |
|
15074
64fbd0de9773
url: parse fragments first (issue2997)
Matt Mackall <mpm@selenic.com>
parents:
15066
diff
changeset
|
2346 |
if parsefragment and '#' in path: |
64fbd0de9773
url: parse fragments first (issue2997)
Matt Mackall <mpm@selenic.com>
parents:
15066
diff
changeset
|
2347 |
path, self.fragment = path.split('#', 1) |
64fbd0de9773
url: parse fragments first (issue2997)
Matt Mackall <mpm@selenic.com>
parents:
15066
diff
changeset
|
2348 |
if not path: |
64fbd0de9773
url: parse fragments first (issue2997)
Matt Mackall <mpm@selenic.com>
parents:
15066
diff
changeset
|
2349 |
path = None |
64fbd0de9773
url: parse fragments first (issue2997)
Matt Mackall <mpm@selenic.com>
parents:
15066
diff
changeset
|
2350 |
|
14699
388af80c058b
url: catch UNC paths as yet another Windows special case (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14640
diff
changeset
|
2351 |
# special case for Windows drive letters and UNC paths |
388af80c058b
url: catch UNC paths as yet another Windows special case (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14640
diff
changeset
|
2352 |
if hasdriveletter(path) or path.startswith(r'\\'): |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2353 |
self.path = path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2354 |
return |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2355 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2356 |
# For compatibility reasons, we can't handle bundle paths as |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2357 |
# normal URLS |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2358 |
if path.startswith('bundle:'): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2359 |
self.scheme = 'bundle' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2360 |
path = path[7:] |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2361 |
if path.startswith('//'): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2362 |
path = path[2:] |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2363 |
self.path = path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2364 |
return |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2365 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2366 |
if self._matchscheme(path): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2367 |
parts = path.split(':', 1) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2368 |
if parts[0]: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2369 |
self.scheme, path = parts |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2370 |
self._localpath = False |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2371 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2372 |
if not path: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2373 |
path = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2374 |
if self._localpath: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2375 |
self.path = '' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2376 |
return |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2377 |
else: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2378 |
if self._localpath: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2379 |
self.path = path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2380 |
return |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2381 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2382 |
if parsequery and '?' in path: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2383 |
path, self.query = path.split('?', 1) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2384 |
if not path: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2385 |
path = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2386 |
if not self.query: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2387 |
self.query = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2388 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2389 |
# // is required to specify a host/authority |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2390 |
if path and path.startswith('//'): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2391 |
parts = path[2:].split('/', 1) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2392 |
if len(parts) > 1: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2393 |
self.host, path = parts |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2394 |
else: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2395 |
self.host = parts[0] |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2396 |
path = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2397 |
if not self.host: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2398 |
self.host = None |
15018
e89f62dcd723
url: really handle urls of the form file:///c:/foo/bar/ correctly
Mads Kiilerich <mads@kiilerich.com>
parents:
14988
diff
changeset
|
2399 |
# path of file:///d is /d |
e89f62dcd723
url: really handle urls of the form file:///c:/foo/bar/ correctly
Mads Kiilerich <mads@kiilerich.com>
parents:
14988
diff
changeset
|
2400 |
# path of file:///d:/ is d:/, not /d:/ |
14915
28edd65000d9
url: handle urls of the form file:///c:/foo/bar/ correctly
Matt Mackall <mpm@selenic.com>
parents:
14825
diff
changeset
|
2401 |
if path and not hasdriveletter(path): |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2402 |
path = '/' + path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2403 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2404 |
if self.host and '@' in self.host: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2405 |
self.user, self.host = self.host.rsplit('@', 1) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2406 |
if ':' in self.user: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2407 |
self.user, self.passwd = self.user.split(':', 1) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2408 |
if not self.host: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2409 |
self.host = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2410 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2411 |
# Don't split on colons in IPv6 addresses without ports |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2412 |
if (self.host and ':' in self.host and |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2413 |
not (self.host.startswith('[') and self.host.endswith(']'))): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2414 |
self._hostport = self.host |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2415 |
self.host, self.port = self.host.rsplit(':', 1) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2416 |
if not self.host: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2417 |
self.host = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2418 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2419 |
if (self.host and self.scheme == 'file' and |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2420 |
self.host not in ('localhost', '127.0.0.1', '[::1]')): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2421 |
raise Abort(_('file:// URLs can only refer to localhost')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2422 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2423 |
self.path = path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2424 |
|
14988
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2425 |
# leave the query string escaped |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2426 |
for a in ('user', 'passwd', 'host', 'port', |
14988
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2427 |
'path', 'fragment'): |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2428 |
v = getattr(self, a) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2429 |
if v is not None: |
14077
c285bdb0572a
util.url: copy urllib.unquote() into util to improve startup times
Brodie Rao <brodie@bitheap.org>
parents:
14076
diff
changeset
|
2430 |
setattr(self, a, _urlunquote(v)) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2431 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2432 |
def __repr__(self): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2433 |
attrs = [] |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2434 |
for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path', |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2435 |
'query', 'fragment'): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2436 |
v = getattr(self, a) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2437 |
if v is not None: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2438 |
attrs.append('%s: %r' % (a, v)) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2439 |
return '<url %s>' % ', '.join(attrs) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2440 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2441 |
def __str__(self): |
14147
617483af1cc0
test: test that backslash is preserved by the url class
Mads Kiilerich <mads@kiilerich.com>
parents:
14146
diff
changeset
|
2442 |
r"""Join the URL's components back into a URL string. |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2443 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2444 |
Examples: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2445 |
|
15452
de7e2fba4326
util: don't encode ':' in url paths
Mads Kiilerich <mads@kiilerich.com>
parents:
15392
diff
changeset
|
2446 |
>>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
de7e2fba4326
util: don't encode ':' in url paths
Mads Kiilerich <mads@kiilerich.com>
parents:
15392
diff
changeset
|
2447 |
'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
14988
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2448 |
>>> str(url('http://user:pw@host:80/?foo=bar&baz=42')) |
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2449 |
'http://user:pw@host:80/?foo=bar&baz=42' |
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2450 |
>>> str(url('http://user:pw@host:80/?foo=bar%3dbaz')) |
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2451 |
'http://user:pw@host:80/?foo=bar%3dbaz' |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2452 |
>>> str(url('ssh://user:pw@[::1]:2200//home/joe#')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2453 |
'ssh://user:pw@[::1]:2200//home/joe#' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2454 |
>>> str(url('http://localhost:80//')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2455 |
'http://localhost:80//' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2456 |
>>> str(url('http://localhost:80/')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2457 |
'http://localhost:80/' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2458 |
>>> str(url('http://localhost:80')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2459 |
'http://localhost:80/' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2460 |
>>> str(url('bundle:foo')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2461 |
'bundle:foo' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2462 |
>>> str(url('bundle://../foo')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2463 |
'bundle:../foo' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2464 |
>>> str(url('path')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2465 |
'path' |
14313
a389dd285282
util: make str(url) return file:/// for abs paths again
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
14271
diff
changeset
|
2466 |
>>> str(url('file:///tmp/foo/bar')) |
a389dd285282
util: make str(url) return file:/// for abs paths again
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
14271
diff
changeset
|
2467 |
'file:///tmp/foo/bar' |
15609
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
15505
diff
changeset
|
2468 |
>>> str(url('file:///c:/tmp/foo/bar')) |
15611 | 2469 |
'file:///c:/tmp/foo/bar' |
14147
617483af1cc0
test: test that backslash is preserved by the url class
Mads Kiilerich <mads@kiilerich.com>
parents:
14146
diff
changeset
|
2470 |
>>> print url(r'bundle:foo\bar') |
617483af1cc0
test: test that backslash is preserved by the url class
Mads Kiilerich <mads@kiilerich.com>
parents:
14146
diff
changeset
|
2471 |
bundle:foo\bar |
20106
c33d9217e99d
util: url keeps backslash in paths
Simon Heimberg <simohe@besonet.ch>
parents:
20000
diff
changeset
|
2472 |
>>> print url(r'file:///D:\data\hg') |
c33d9217e99d
util: url keeps backslash in paths
Simon Heimberg <simohe@besonet.ch>
parents:
20000
diff
changeset
|
2473 |
file:///D:\data\hg |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2474 |
""" |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2475 |
if self._localpath: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2476 |
s = self.path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2477 |
if self.scheme == 'bundle': |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2478 |
s = 'bundle:' + s |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2479 |
if self.fragment: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2480 |
s += '#' + self.fragment |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2481 |
return s |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2482 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2483 |
s = self.scheme + ':' |
14313
a389dd285282
util: make str(url) return file:/// for abs paths again
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
14271
diff
changeset
|
2484 |
if self.user or self.passwd or self.host: |
a389dd285282
util: make str(url) return file:/// for abs paths again
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
14271
diff
changeset
|
2485 |
s += '//' |
15609
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
15505
diff
changeset
|
2486 |
elif self.scheme and (not self.path or self.path.startswith('/') |
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
15505
diff
changeset
|
2487 |
or hasdriveletter(self.path)): |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2488 |
s += '//' |
15609
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
15505
diff
changeset
|
2489 |
if hasdriveletter(self.path): |
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
15505
diff
changeset
|
2490 |
s += '/' |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2491 |
if self.user: |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
2492 |
s += urlreq.quote(self.user, safe=self._safechars) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2493 |
if self.passwd: |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
2494 |
s += ':' + urlreq.quote(self.passwd, safe=self._safechars) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2495 |
if self.user or self.passwd: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2496 |
s += '@' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2497 |
if self.host: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2498 |
if not (self.host.startswith('[') and self.host.endswith(']')): |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
2499 |
s += urlreq.quote(self.host) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2500 |
else: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2501 |
s += self.host |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2502 |
if self.port: |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
2503 |
s += ':' + urlreq.quote(self.port) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2504 |
if self.host: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2505 |
s += '/' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2506 |
if self.path: |
14988
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2507 |
# TODO: similar to the query string, we should not unescape the |
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2508 |
# path when we store it, the path might contain '%2f' = '/', |
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2509 |
# which we should *not* escape. |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
2510 |
s += urlreq.quote(self.path, safe=self._safepchars) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2511 |
if self.query: |
14988
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2512 |
# we store the query in escaped form. |
e6730f9e13bc
url: store and assume the query part of an url is in escaped form (issue2921)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
14915
diff
changeset
|
2513 |
s += '?' + self.query |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2514 |
if self.fragment is not None: |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28882
diff
changeset
|
2515 |
s += '#' + urlreq.quote(self.fragment, safe=self._safepchars) |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2516 |
return s |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2517 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2518 |
def authinfo(self): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2519 |
user, passwd = self.user, self.passwd |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2520 |
try: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2521 |
self.user, self.passwd = None, None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2522 |
s = str(self) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2523 |
finally: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2524 |
self.user, self.passwd = user, passwd |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2525 |
if not self.user: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2526 |
return (s, None) |
15028
eb97a3e38656
http: explain why the host is passed to urllib2 password manager
Patrick Mezard <pmezard@gmail.com>
parents:
15027
diff
changeset
|
2527 |
# authinfo[1] is passed to urllib2 password manager, and its |
eb97a3e38656
http: explain why the host is passed to urllib2 password manager
Patrick Mezard <pmezard@gmail.com>
parents:
15027
diff
changeset
|
2528 |
# URIs must not contain credentials. The host is passed in the |
eb97a3e38656
http: explain why the host is passed to urllib2 password manager
Patrick Mezard <pmezard@gmail.com>
parents:
15027
diff
changeset
|
2529 |
# URIs list because Python < 2.4.3 uses only that to search for |
eb97a3e38656
http: explain why the host is passed to urllib2 password manager
Patrick Mezard <pmezard@gmail.com>
parents:
15027
diff
changeset
|
2530 |
# a password. |
15024
0f1311e829c9
http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents:
15018
diff
changeset
|
2531 |
return (s, (None, (s, self.host), |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2532 |
self.user, self.passwd or '')) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2533 |
|
14766
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2534 |
def isabs(self): |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2535 |
if self.scheme and self.scheme != 'file': |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2536 |
return True # remote URL |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2537 |
if hasdriveletter(self.path): |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2538 |
return True # absolute for our purposes - can't be joined() |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2539 |
if self.path.startswith(r'\\'): |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2540 |
return True # Windows UNC path |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2541 |
if self.path.startswith('/'): |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2542 |
return True # POSIX-style |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2543 |
return False |
4f56b7530eab
subrepos: be smarter about what's an absolute path (issue2808)
Matt Mackall <mpm@selenic.com>
parents:
14699
diff
changeset
|
2544 |
|
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2545 |
def localpath(self): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2546 |
if self.scheme == 'file' or self.scheme == 'bundle': |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2547 |
path = self.path or '/' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2548 |
# For Windows, we need to promote hosts containing drive |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2549 |
# letters to paths with drive letters. |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2550 |
if hasdriveletter(self._hostport): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2551 |
path = self._hostport + '/' + self.path |
15496
396e83d635a6
url: handle file://localhost/c:/foo "correctly"
Mads Kiilerich <mads@kiilerich.com>
parents:
15488
diff
changeset
|
2552 |
elif (self.host is not None and self.path |
396e83d635a6
url: handle file://localhost/c:/foo "correctly"
Mads Kiilerich <mads@kiilerich.com>
parents:
15488
diff
changeset
|
2553 |
and not hasdriveletter(path)): |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2554 |
path = '/' + path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2555 |
return path |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2556 |
return self._origpath |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2557 |
|
20353
0889585b44f1
util.url: add an 'islocal' method
Siddharth Agarwal <sid0@fb.com>
parents:
20244
diff
changeset
|
2558 |
def islocal(self): |
0889585b44f1
util.url: add an 'islocal' method
Siddharth Agarwal <sid0@fb.com>
parents:
20244
diff
changeset
|
2559 |
'''whether localpath will return something that posixfile can open''' |
0889585b44f1
util.url: add an 'islocal' method
Siddharth Agarwal <sid0@fb.com>
parents:
20244
diff
changeset
|
2560 |
return (not self.scheme or self.scheme == 'file' |
0889585b44f1
util.url: add an 'islocal' method
Siddharth Agarwal <sid0@fb.com>
parents:
20244
diff
changeset
|
2561 |
or self.scheme == 'bundle') |
0889585b44f1
util.url: add an 'islocal' method
Siddharth Agarwal <sid0@fb.com>
parents:
20244
diff
changeset
|
2562 |
|
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2563 |
def hasscheme(path): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2564 |
return bool(url(path).scheme) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2565 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2566 |
def hasdriveletter(path): |
15609
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
15505
diff
changeset
|
2567 |
return path and path[1:2] == ':' and path[0:1].isalpha() |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2568 |
|
14825
de9eb6b1da4f
util: rename the util.localpath that uses url to urllocalpath (issue2875)
Mads Kiilerich <mads@kiilerich.com>
parents:
14766
diff
changeset
|
2569 |
def urllocalpath(path): |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2570 |
return url(path, parsequery=False, parsefragment=False).localpath() |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2571 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2572 |
def hidepassword(u): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2573 |
'''hide user credential in a url string''' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2574 |
u = url(u) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2575 |
if u.passwd: |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2576 |
u.passwd = '***' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2577 |
return str(u) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2578 |
|
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2579 |
def removeauth(u): |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2580 |
'''remove all authentication information from a url string''' |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2581 |
u = url(u) |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2582 |
u.user = u.passwd = None |
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
14064
diff
changeset
|
2583 |
return str(u) |
14515
76f295eaed86
util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents:
14313
diff
changeset
|
2584 |
|
27363
c7ab2087d436
util: rename argument of isatty()
Yuya Nishihara <yuya@tcha.org>
parents:
27358
diff
changeset
|
2585 |
def isatty(fp): |
14515
76f295eaed86
util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents:
14313
diff
changeset
|
2586 |
try: |
27363
c7ab2087d436
util: rename argument of isatty()
Yuya Nishihara <yuya@tcha.org>
parents:
27358
diff
changeset
|
2587 |
return fp.isatty() |
14515
76f295eaed86
util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents:
14313
diff
changeset
|
2588 |
except AttributeError: |
76f295eaed86
util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents:
14313
diff
changeset
|
2589 |
return False |
18736
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2590 |
|
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2591 |
timecount = unitcountfn( |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2592 |
(1, 1e3, _('%.0f s')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2593 |
(100, 1, _('%.1f s')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2594 |
(10, 1, _('%.2f s')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2595 |
(1, 1, _('%.3f s')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2596 |
(100, 0.001, _('%.1f ms')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2597 |
(10, 0.001, _('%.2f ms')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2598 |
(1, 0.001, _('%.3f ms')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2599 |
(100, 0.000001, _('%.1f us')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2600 |
(10, 0.000001, _('%.2f us')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2601 |
(1, 0.000001, _('%.3f us')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2602 |
(100, 0.000000001, _('%.1f ns')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2603 |
(10, 0.000000001, _('%.2f ns')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2604 |
(1, 0.000000001, _('%.3f ns')), |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2605 |
) |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2606 |
|
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2607 |
_timenesting = [0] |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2608 |
|
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2609 |
def timed(func): |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2610 |
'''Report the execution time of a function call to stderr. |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2611 |
|
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2612 |
During development, use as a decorator when you need to measure |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2613 |
the cost of a function, e.g. as follows: |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2614 |
|
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2615 |
@util.timed |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2616 |
def foo(a, b, c): |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2617 |
pass |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2618 |
''' |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2619 |
|
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2620 |
def wrapper(*args, **kwargs): |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2621 |
start = time.time() |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2622 |
indent = 2 |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2623 |
_timenesting[0] += indent |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2624 |
try: |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2625 |
return func(*args, **kwargs) |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2626 |
finally: |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2627 |
elapsed = time.time() - start |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2628 |
_timenesting[0] -= indent |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2629 |
sys.stderr.write('%s%s: %s\n' % |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2630 |
(' ' * _timenesting[0], func.__name__, |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2631 |
timecount(elapsed))) |
af9ddea2cb99
util: add a timed function for use during development
Bryan O'Sullivan <bryano@fb.com>
parents:
18735
diff
changeset
|
2632 |
return wrapper |
19194
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2633 |
|
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2634 |
_sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2635 |
('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2636 |
|
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2637 |
def sizetoint(s): |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2638 |
'''Convert a space specifier to a byte count. |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2639 |
|
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2640 |
>>> sizetoint('30') |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2641 |
30 |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2642 |
>>> sizetoint('2.2kb') |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2643 |
2252 |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2644 |
>>> sizetoint('6M') |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2645 |
6291456 |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2646 |
''' |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2647 |
t = s.strip().lower() |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2648 |
try: |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2649 |
for k, u in _sizeunits: |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2650 |
if t.endswith(k): |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2651 |
return int(float(t[:-len(k)]) * u) |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2652 |
return int(t) |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2653 |
except ValueError: |
1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
Bryan O'Sullivan <bryano@fb.com>
parents:
18938
diff
changeset
|
2654 |
raise error.ParseError(_("couldn't parse size: %s") % s) |
19211
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2655 |
|
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2656 |
class hooks(object): |
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2657 |
'''A collection of hook functions that can be used to extend a |
26098 | 2658 |
function's behavior. Hooks are called in lexicographic order, |
19211
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2659 |
based on the names of their sources.''' |
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2660 |
|
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2661 |
def __init__(self): |
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2662 |
self._hooks = [] |
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2663 |
|
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2664 |
def add(self, source, hook): |
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2665 |
self._hooks.append((source, hook)) |
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2666 |
|
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2667 |
def __call__(self, *args): |
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2668 |
self._hooks.sort(key=lambda x: x[0]) |
21046
cc13addbd62b
util: enable "hooks" to return list of the values returned from each hooks
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
21018
diff
changeset
|
2669 |
results = [] |
19211
3bfd7f1e7485
summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents:
19194
diff
changeset
|
2670 |
for source, hook in self._hooks: |
21046
cc13addbd62b
util: enable "hooks" to return list of the values returned from each hooks
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
21018
diff
changeset
|
2671 |
results.append(hook(*args)) |
cc13addbd62b
util: enable "hooks" to return list of the values returned from each hooks
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
21018
diff
changeset
|
2672 |
return results |
20244
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2673 |
|
28497
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2674 |
def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'): |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2675 |
'''Yields lines for a nicely formatted stacktrace. |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2676 |
Skips the 'skip' last entries. |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2677 |
Each file+linenumber is formatted according to fileline. |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2678 |
Each line is formatted according to line. |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2679 |
If line is None, it yields: |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2680 |
length of longest filepath+line number, |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2681 |
filepath+linenumber, |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2682 |
function |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2683 |
|
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2684 |
Not be used in production code but very convenient while developing. |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2685 |
''' |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2686 |
entries = [(fileline % (fn, ln), func) |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2687 |
for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]] |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2688 |
if entries: |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2689 |
fnmax = max(len(entry[0]) for entry in entries) |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2690 |
for fnln, func in entries: |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2691 |
if line is None: |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2692 |
yield (fnmax, fnln, func) |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2693 |
else: |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2694 |
yield line % (fnmax, fnln, func) |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2695 |
|
20542
be27652675ce
util: debugstacktrace, flush before and after writing
Mads Kiilerich <madski@unity3d.com>
parents:
20353
diff
changeset
|
2696 |
def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout): |
20244
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2697 |
'''Writes a message to f (stderr) with a nicely formatted stacktrace. |
20542
be27652675ce
util: debugstacktrace, flush before and after writing
Mads Kiilerich <madski@unity3d.com>
parents:
20353
diff
changeset
|
2698 |
Skips the 'skip' last entries. By default it will flush stdout first. |
28496
b592564a803c
util: reword debugstacktrace comment
timeless <timeless@mozdev.org>
parents:
28027
diff
changeset
|
2699 |
It can be used everywhere and intentionally does not require an ui object. |
20244
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2700 |
Not be used in production code but very convenient while developing. |
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2701 |
''' |
20542
be27652675ce
util: debugstacktrace, flush before and after writing
Mads Kiilerich <madski@unity3d.com>
parents:
20353
diff
changeset
|
2702 |
if otherf: |
be27652675ce
util: debugstacktrace, flush before and after writing
Mads Kiilerich <madski@unity3d.com>
parents:
20353
diff
changeset
|
2703 |
otherf.flush() |
20244
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2704 |
f.write('%s at:\n' % msg) |
28497
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2705 |
for line in getstackframes(skip + 1): |
906fece80cfa
util: refactor getstackframes
timeless <timeless@mozdev.org>
parents:
28496
diff
changeset
|
2706 |
f.write(line) |
20542
be27652675ce
util: debugstacktrace, flush before and after writing
Mads Kiilerich <madski@unity3d.com>
parents:
20353
diff
changeset
|
2707 |
f.flush() |
20244
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2708 |
|
24635
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2709 |
class dirs(object): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2710 |
'''a multiset of directory names from a dirstate or manifest''' |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2711 |
|
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2712 |
def __init__(self, map, skip=None): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2713 |
self._dirs = {} |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2714 |
addpath = self.addpath |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2715 |
if safehasattr(map, 'iteritems') and skip is not None: |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2716 |
for f, s in map.iteritems(): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2717 |
if s[0] != skip: |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2718 |
addpath(f) |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2719 |
else: |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2720 |
for f in map: |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2721 |
addpath(f) |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2722 |
|
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2723 |
def addpath(self, path): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2724 |
dirs = self._dirs |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2725 |
for base in finddirs(path): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2726 |
if base in dirs: |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2727 |
dirs[base] += 1 |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2728 |
return |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2729 |
dirs[base] = 1 |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2730 |
|
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2731 |
def delpath(self, path): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2732 |
dirs = self._dirs |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2733 |
for base in finddirs(path): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2734 |
if dirs[base] > 1: |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2735 |
dirs[base] -= 1 |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2736 |
return |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2737 |
del dirs[base] |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2738 |
|
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2739 |
def __iter__(self): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2740 |
return self._dirs.iterkeys() |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2741 |
|
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2742 |
def __contains__(self, d): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2743 |
return d in self._dirs |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2744 |
|
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2745 |
if safehasattr(parsers, 'dirs'): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2746 |
dirs = parsers.dirs |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2747 |
|
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2748 |
def finddirs(path): |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2749 |
pos = path.rfind('/') |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2750 |
while pos != -1: |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2751 |
yield path[:pos] |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2752 |
pos = path.rfind('/', 0, pos) |
21e1ece30f8c
util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents:
24605
diff
changeset
|
2753 |
|
26266
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2754 |
# compression utility |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2755 |
|
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2756 |
class nocompress(object): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2757 |
def compress(self, x): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2758 |
return x |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2759 |
def flush(self): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2760 |
return "" |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2761 |
|
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2762 |
compressors = { |
26267
eca468b8fae4
compression: use 'None' for no-compression
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26266
diff
changeset
|
2763 |
None: nocompress, |
26266
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2764 |
# lambda to prevent early import |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2765 |
'BZ': lambda: bz2.BZ2Compressor(), |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2766 |
'GZ': lambda: zlib.compressobj(), |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2767 |
} |
26267
eca468b8fae4
compression: use 'None' for no-compression
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26266
diff
changeset
|
2768 |
# also support the old form by courtesies |
eca468b8fae4
compression: use 'None' for no-compression
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26266
diff
changeset
|
2769 |
compressors['UN'] = compressors[None] |
26266
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2770 |
|
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2771 |
def _makedecompressor(decompcls): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2772 |
def generator(f): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2773 |
d = decompcls() |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2774 |
for chunk in filechunkiter(f): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2775 |
yield d.decompress(chunk) |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2776 |
def func(fh): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2777 |
return chunkbuffer(generator(fh)) |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2778 |
return func |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2779 |
|
27703
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2780 |
class ctxmanager(object): |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2781 |
'''A context manager for use in 'with' blocks to allow multiple |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2782 |
contexts to be entered at once. This is both safer and more |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2783 |
flexible than contextlib.nested. |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2784 |
|
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2785 |
Once Mercurial supports Python 2.7+, this will become mostly |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2786 |
unnecessary. |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2787 |
''' |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2788 |
|
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2789 |
def __init__(self, *args): |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2790 |
'''Accepts a list of no-argument functions that return context |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2791 |
managers. These will be invoked at __call__ time.''' |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2792 |
self._pending = args |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2793 |
self._atexit = [] |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2794 |
|
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2795 |
def __enter__(self): |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2796 |
return self |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2797 |
|
27785
ba427b51f1d8
util: rename ctxmanager's __call__ method to enter
Bryan O'Sullivan <bryano@fb.com>
parents:
27778
diff
changeset
|
2798 |
def enter(self): |
27703
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2799 |
'''Create and enter context managers in the order in which they were |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2800 |
passed to the constructor.''' |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2801 |
values = [] |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2802 |
for func in self._pending: |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2803 |
obj = func() |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2804 |
values.append(obj.__enter__()) |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2805 |
self._atexit.append(obj.__exit__) |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2806 |
del self._pending |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2807 |
return values |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2808 |
|
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2809 |
def atexit(self, func, *args, **kwargs): |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2810 |
'''Add a function to call when this context manager exits. The |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2811 |
ordering of multiple atexit calls is unspecified, save that |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2812 |
they will happen before any __exit__ functions.''' |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2813 |
def wrapper(exc_type, exc_val, exc_tb): |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2814 |
func(*args, **kwargs) |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2815 |
self._atexit.append(wrapper) |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2816 |
return func |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2817 |
|
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2818 |
def __exit__(self, exc_type, exc_val, exc_tb): |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2819 |
'''Context managers are exited in the reverse order from which |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2820 |
they were created.''' |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2821 |
received = exc_type is not None |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2822 |
suppressed = False |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2823 |
pending = None |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2824 |
self._atexit.reverse() |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2825 |
for exitfunc in self._atexit: |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2826 |
try: |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2827 |
if exitfunc(exc_type, exc_val, exc_tb): |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2828 |
suppressed = True |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2829 |
exc_type = None |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2830 |
exc_val = None |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2831 |
exc_tb = None |
27755
50c5192e4a5e
util: don't capture exception with a name since we don't use it
Augie Fackler <augie@google.com>
parents:
27703
diff
changeset
|
2832 |
except BaseException: |
27703
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2833 |
pending = sys.exc_info() |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2834 |
exc_type, exc_val, exc_tb = pending = sys.exc_info() |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2835 |
del self._atexit |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2836 |
if pending: |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2837 |
raise exc_val |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2838 |
return received and suppressed |
4e27c0a70574
util: introduce ctxmanager, to avoid nested try/finally blocks
Bryan O'Sullivan <bos@serpentine.com>
parents:
27667
diff
changeset
|
2839 |
|
26266
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2840 |
def _bz2(): |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2841 |
d = bz2.BZ2Decompressor() |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2842 |
# Bzip2 stream start with BZ, but we stripped it. |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2843 |
# we put it back for good measure. |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2844 |
d.decompress('BZ') |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2845 |
return d |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2846 |
|
26267
eca468b8fae4
compression: use 'None' for no-compression
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26266
diff
changeset
|
2847 |
decompressors = {None: lambda fh: fh, |
26392
127b59787fd5
changegroup: use a different compression key for BZ in HG10
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26311
diff
changeset
|
2848 |
'_truncatedBZ': _makedecompressor(_bz2), |
127b59787fd5
changegroup: use a different compression key for BZ in HG10
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26311
diff
changeset
|
2849 |
'BZ': _makedecompressor(lambda: bz2.BZ2Decompressor()), |
26266
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2850 |
'GZ': _makedecompressor(lambda: zlib.decompressobj()), |
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2851 |
} |
26267
eca468b8fae4
compression: use 'None' for no-compression
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26266
diff
changeset
|
2852 |
# also support the old form by courtesies |
eca468b8fae4
compression: use 'None' for no-compression
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26266
diff
changeset
|
2853 |
decompressors['UN'] = decompressors[None] |
26266
1e042e31bd0c
changegroup: move all compressions utilities in util
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26201
diff
changeset
|
2854 |
|
20244
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2855 |
# convenient shortcut |
47d0843647d1
util: introduce util.debugstacktrace for showing a stack trace without crashing
Mads Kiilerich <madski@unity3d.com>
parents:
20202
diff
changeset
|
2856 |
dst = debugstacktrace |