Show More
@@ -1,2231 +1,2233 | |||||
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 | import i18n |
|
16 | import i18n | |
17 | _ = i18n._ |
|
17 | _ = i18n._ | |
18 | import error, osutil, encoding |
|
18 | import error, osutil, encoding | |
19 | import errno, shutil, sys, tempfile, traceback |
|
19 | import errno, shutil, sys, tempfile, traceback | |
20 | import re as remod |
|
20 | import re as remod | |
21 | import os, time, datetime, calendar, textwrap, signal, collections |
|
21 | import os, time, datetime, calendar, textwrap, signal, collections | |
22 | import imp, socket, urllib, struct |
|
22 | import imp, socket, urllib, struct | |
23 | import gc |
|
23 | import gc | |
24 |
|
24 | |||
25 | if os.name == 'nt': |
|
25 | if os.name == 'nt': | |
26 | import windows as platform |
|
26 | import windows as platform | |
27 | else: |
|
27 | else: | |
28 | import posix as platform |
|
28 | import posix as platform | |
29 |
|
29 | |||
30 | cachestat = platform.cachestat |
|
30 | cachestat = platform.cachestat | |
31 | checkexec = platform.checkexec |
|
31 | checkexec = platform.checkexec | |
32 | checklink = platform.checklink |
|
32 | checklink = platform.checklink | |
33 | copymode = platform.copymode |
|
33 | copymode = platform.copymode | |
34 | executablepath = platform.executablepath |
|
34 | executablepath = platform.executablepath | |
35 | expandglobs = platform.expandglobs |
|
35 | expandglobs = platform.expandglobs | |
36 | explainexit = platform.explainexit |
|
36 | explainexit = platform.explainexit | |
37 | findexe = platform.findexe |
|
37 | findexe = platform.findexe | |
38 | gethgcmd = platform.gethgcmd |
|
38 | gethgcmd = platform.gethgcmd | |
39 | getuser = platform.getuser |
|
39 | getuser = platform.getuser | |
40 | groupmembers = platform.groupmembers |
|
40 | groupmembers = platform.groupmembers | |
41 | groupname = platform.groupname |
|
41 | groupname = platform.groupname | |
42 | hidewindow = platform.hidewindow |
|
42 | hidewindow = platform.hidewindow | |
43 | isexec = platform.isexec |
|
43 | isexec = platform.isexec | |
44 | isowner = platform.isowner |
|
44 | isowner = platform.isowner | |
45 | localpath = platform.localpath |
|
45 | localpath = platform.localpath | |
46 | lookupreg = platform.lookupreg |
|
46 | lookupreg = platform.lookupreg | |
47 | makedir = platform.makedir |
|
47 | makedir = platform.makedir | |
48 | nlinks = platform.nlinks |
|
48 | nlinks = platform.nlinks | |
49 | normpath = platform.normpath |
|
49 | normpath = platform.normpath | |
50 | normcase = platform.normcase |
|
50 | normcase = platform.normcase | |
51 | openhardlinks = platform.openhardlinks |
|
51 | openhardlinks = platform.openhardlinks | |
52 | oslink = platform.oslink |
|
52 | oslink = platform.oslink | |
53 | parsepatchoutput = platform.parsepatchoutput |
|
53 | parsepatchoutput = platform.parsepatchoutput | |
54 | pconvert = platform.pconvert |
|
54 | pconvert = platform.pconvert | |
55 | popen = platform.popen |
|
55 | popen = platform.popen | |
56 | posixfile = platform.posixfile |
|
56 | posixfile = platform.posixfile | |
57 | quotecommand = platform.quotecommand |
|
57 | quotecommand = platform.quotecommand | |
58 | readpipe = platform.readpipe |
|
58 | readpipe = platform.readpipe | |
59 | rename = platform.rename |
|
59 | rename = platform.rename | |
60 | samedevice = platform.samedevice |
|
60 | samedevice = platform.samedevice | |
61 | samefile = platform.samefile |
|
61 | samefile = platform.samefile | |
62 | samestat = platform.samestat |
|
62 | samestat = platform.samestat | |
63 | setbinary = platform.setbinary |
|
63 | setbinary = platform.setbinary | |
64 | setflags = platform.setflags |
|
64 | setflags = platform.setflags | |
65 | setsignalhandler = platform.setsignalhandler |
|
65 | setsignalhandler = platform.setsignalhandler | |
66 | shellquote = platform.shellquote |
|
66 | shellquote = platform.shellquote | |
67 | spawndetached = platform.spawndetached |
|
67 | spawndetached = platform.spawndetached | |
68 | split = platform.split |
|
68 | split = platform.split | |
69 | sshargs = platform.sshargs |
|
69 | sshargs = platform.sshargs | |
70 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) |
|
70 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) | |
71 | statisexec = platform.statisexec |
|
71 | statisexec = platform.statisexec | |
72 | statislink = platform.statislink |
|
72 | statislink = platform.statislink | |
73 | termwidth = platform.termwidth |
|
73 | termwidth = platform.termwidth | |
74 | testpid = platform.testpid |
|
74 | testpid = platform.testpid | |
75 | umask = platform.umask |
|
75 | umask = platform.umask | |
76 | unlink = platform.unlink |
|
76 | unlink = platform.unlink | |
77 | unlinkpath = platform.unlinkpath |
|
77 | unlinkpath = platform.unlinkpath | |
78 | username = platform.username |
|
78 | username = platform.username | |
79 |
|
79 | |||
80 | # Python compatibility |
|
80 | # Python compatibility | |
81 |
|
81 | |||
82 | _notset = object() |
|
82 | _notset = object() | |
83 |
|
83 | |||
84 | def safehasattr(thing, attr): |
|
84 | def safehasattr(thing, attr): | |
85 | return getattr(thing, attr, _notset) is not _notset |
|
85 | return getattr(thing, attr, _notset) is not _notset | |
86 |
|
86 | |||
87 | def sha1(s=''): |
|
87 | def sha1(s=''): | |
88 | ''' |
|
88 | ''' | |
89 | Low-overhead wrapper around Python's SHA support |
|
89 | Low-overhead wrapper around Python's SHA support | |
90 |
|
90 | |||
91 | >>> f = _fastsha1 |
|
91 | >>> f = _fastsha1 | |
92 | >>> a = sha1() |
|
92 | >>> a = sha1() | |
93 | >>> a = f() |
|
93 | >>> a = f() | |
94 | >>> a.hexdigest() |
|
94 | >>> a.hexdigest() | |
95 | 'da39a3ee5e6b4b0d3255bfef95601890afd80709' |
|
95 | 'da39a3ee5e6b4b0d3255bfef95601890afd80709' | |
96 | ''' |
|
96 | ''' | |
97 |
|
97 | |||
98 | return _fastsha1(s) |
|
98 | return _fastsha1(s) | |
99 |
|
99 | |||
100 | def _fastsha1(s=''): |
|
100 | def _fastsha1(s=''): | |
101 | # This function will import sha1 from hashlib or sha (whichever is |
|
101 | # This function will import sha1 from hashlib or sha (whichever is | |
102 | # available) and overwrite itself with it on the first call. |
|
102 | # available) and overwrite itself with it on the first call. | |
103 | # Subsequent calls will go directly to the imported function. |
|
103 | # Subsequent calls will go directly to the imported function. | |
104 | if sys.version_info >= (2, 5): |
|
104 | if sys.version_info >= (2, 5): | |
105 | from hashlib import sha1 as _sha1 |
|
105 | from hashlib import sha1 as _sha1 | |
106 | else: |
|
106 | else: | |
107 | from sha import sha as _sha1 |
|
107 | from sha import sha as _sha1 | |
108 | global _fastsha1, sha1 |
|
108 | global _fastsha1, sha1 | |
109 | _fastsha1 = sha1 = _sha1 |
|
109 | _fastsha1 = sha1 = _sha1 | |
110 | return _sha1(s) |
|
110 | return _sha1(s) | |
111 |
|
111 | |||
112 | def md5(s=''): |
|
112 | def md5(s=''): | |
113 | try: |
|
113 | try: | |
114 | from hashlib import md5 as _md5 |
|
114 | from hashlib import md5 as _md5 | |
115 | except ImportError: |
|
115 | except ImportError: | |
116 | from md5 import md5 as _md5 |
|
116 | from md5 import md5 as _md5 | |
117 | global md5 |
|
117 | global md5 | |
118 | md5 = _md5 |
|
118 | md5 = _md5 | |
119 | return _md5(s) |
|
119 | return _md5(s) | |
120 |
|
120 | |||
121 | DIGESTS = { |
|
121 | DIGESTS = { | |
122 | 'md5': md5, |
|
122 | 'md5': md5, | |
123 | 'sha1': sha1, |
|
123 | 'sha1': sha1, | |
124 | } |
|
124 | } | |
125 | # List of digest types from strongest to weakest |
|
125 | # List of digest types from strongest to weakest | |
126 | DIGESTS_BY_STRENGTH = ['sha1', 'md5'] |
|
126 | DIGESTS_BY_STRENGTH = ['sha1', 'md5'] | |
127 |
|
127 | |||
128 | try: |
|
128 | try: | |
129 | import hashlib |
|
129 | import hashlib | |
130 | DIGESTS.update({ |
|
130 | DIGESTS.update({ | |
131 | 'sha512': hashlib.sha512, |
|
131 | 'sha512': hashlib.sha512, | |
132 | }) |
|
132 | }) | |
133 | DIGESTS_BY_STRENGTH.insert(0, 'sha512') |
|
133 | DIGESTS_BY_STRENGTH.insert(0, 'sha512') | |
134 | except ImportError: |
|
134 | except ImportError: | |
135 | pass |
|
135 | pass | |
136 |
|
136 | |||
137 | for k in DIGESTS_BY_STRENGTH: |
|
137 | for k in DIGESTS_BY_STRENGTH: | |
138 | assert k in DIGESTS |
|
138 | assert k in DIGESTS | |
139 |
|
139 | |||
140 | class digester(object): |
|
140 | class digester(object): | |
141 | """helper to compute digests. |
|
141 | """helper to compute digests. | |
142 |
|
142 | |||
143 | This helper can be used to compute one or more digests given their name. |
|
143 | This helper can be used to compute one or more digests given their name. | |
144 |
|
144 | |||
145 | >>> d = digester(['md5', 'sha1']) |
|
145 | >>> d = digester(['md5', 'sha1']) | |
146 | >>> d.update('foo') |
|
146 | >>> d.update('foo') | |
147 | >>> [k for k in sorted(d)] |
|
147 | >>> [k for k in sorted(d)] | |
148 | ['md5', 'sha1'] |
|
148 | ['md5', 'sha1'] | |
149 | >>> d['md5'] |
|
149 | >>> d['md5'] | |
150 | 'acbd18db4cc2f85cedef654fccc4a4d8' |
|
150 | 'acbd18db4cc2f85cedef654fccc4a4d8' | |
151 | >>> d['sha1'] |
|
151 | >>> d['sha1'] | |
152 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' |
|
152 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' | |
153 | >>> digester.preferred(['md5', 'sha1']) |
|
153 | >>> digester.preferred(['md5', 'sha1']) | |
154 | 'sha1' |
|
154 | 'sha1' | |
155 | """ |
|
155 | """ | |
156 |
|
156 | |||
157 | def __init__(self, digests, s=''): |
|
157 | def __init__(self, digests, s=''): | |
158 | self._hashes = {} |
|
158 | self._hashes = {} | |
159 | for k in digests: |
|
159 | for k in digests: | |
160 | if k not in DIGESTS: |
|
160 | if k not in DIGESTS: | |
161 | raise Abort(_('unknown digest type: %s') % k) |
|
161 | raise Abort(_('unknown digest type: %s') % k) | |
162 | self._hashes[k] = DIGESTS[k]() |
|
162 | self._hashes[k] = DIGESTS[k]() | |
163 | if s: |
|
163 | if s: | |
164 | self.update(s) |
|
164 | self.update(s) | |
165 |
|
165 | |||
166 | def update(self, data): |
|
166 | def update(self, data): | |
167 | for h in self._hashes.values(): |
|
167 | for h in self._hashes.values(): | |
168 | h.update(data) |
|
168 | h.update(data) | |
169 |
|
169 | |||
170 | def __getitem__(self, key): |
|
170 | def __getitem__(self, key): | |
171 | if key not in DIGESTS: |
|
171 | if key not in DIGESTS: | |
172 | raise Abort(_('unknown digest type: %s') % k) |
|
172 | raise Abort(_('unknown digest type: %s') % k) | |
173 | return self._hashes[key].hexdigest() |
|
173 | return self._hashes[key].hexdigest() | |
174 |
|
174 | |||
175 | def __iter__(self): |
|
175 | def __iter__(self): | |
176 | return iter(self._hashes) |
|
176 | return iter(self._hashes) | |
177 |
|
177 | |||
178 | @staticmethod |
|
178 | @staticmethod | |
179 | def preferred(supported): |
|
179 | def preferred(supported): | |
180 | """returns the strongest digest type in both supported and DIGESTS.""" |
|
180 | """returns the strongest digest type in both supported and DIGESTS.""" | |
181 |
|
181 | |||
182 | for k in DIGESTS_BY_STRENGTH: |
|
182 | for k in DIGESTS_BY_STRENGTH: | |
183 | if k in supported: |
|
183 | if k in supported: | |
184 | return k |
|
184 | return k | |
185 | return None |
|
185 | return None | |
186 |
|
186 | |||
187 | class digestchecker(object): |
|
187 | class digestchecker(object): | |
188 | """file handle wrapper that additionally checks content against a given |
|
188 | """file handle wrapper that additionally checks content against a given | |
189 | size and digests. |
|
189 | size and digests. | |
190 |
|
190 | |||
191 | d = digestchecker(fh, size, {'md5': '...'}) |
|
191 | d = digestchecker(fh, size, {'md5': '...'}) | |
192 |
|
192 | |||
193 | When multiple digests are given, all of them are validated. |
|
193 | When multiple digests are given, all of them are validated. | |
194 | """ |
|
194 | """ | |
195 |
|
195 | |||
196 | def __init__(self, fh, size, digests): |
|
196 | def __init__(self, fh, size, digests): | |
197 | self._fh = fh |
|
197 | self._fh = fh | |
198 | self._size = size |
|
198 | self._size = size | |
199 | self._got = 0 |
|
199 | self._got = 0 | |
200 | self._digests = dict(digests) |
|
200 | self._digests = dict(digests) | |
201 | self._digester = digester(self._digests.keys()) |
|
201 | self._digester = digester(self._digests.keys()) | |
202 |
|
202 | |||
203 | def read(self, length=-1): |
|
203 | def read(self, length=-1): | |
204 | content = self._fh.read(length) |
|
204 | content = self._fh.read(length) | |
205 | self._digester.update(content) |
|
205 | self._digester.update(content) | |
206 | self._got += len(content) |
|
206 | self._got += len(content) | |
207 | return content |
|
207 | return content | |
208 |
|
208 | |||
209 | def validate(self): |
|
209 | def validate(self): | |
210 | if self._size != self._got: |
|
210 | if self._size != self._got: | |
211 | raise Abort(_('size mismatch: expected %d, got %d') % |
|
211 | raise Abort(_('size mismatch: expected %d, got %d') % | |
212 | (self._size, self._got)) |
|
212 | (self._size, self._got)) | |
213 | for k, v in self._digests.items(): |
|
213 | for k, v in self._digests.items(): | |
214 | if v != self._digester[k]: |
|
214 | if v != self._digester[k]: | |
215 | # i18n: first parameter is a digest name |
|
215 | # i18n: first parameter is a digest name | |
216 | raise Abort(_('%s mismatch: expected %s, got %s') % |
|
216 | raise Abort(_('%s mismatch: expected %s, got %s') % | |
217 | (k, v, self._digester[k])) |
|
217 | (k, v, self._digester[k])) | |
218 |
|
218 | |||
219 | try: |
|
219 | try: | |
220 | buffer = buffer |
|
220 | buffer = buffer | |
221 | except NameError: |
|
221 | except NameError: | |
222 | if sys.version_info[0] < 3: |
|
222 | if sys.version_info[0] < 3: | |
223 | def buffer(sliceable, offset=0): |
|
223 | def buffer(sliceable, offset=0): | |
224 | return sliceable[offset:] |
|
224 | return sliceable[offset:] | |
225 | else: |
|
225 | else: | |
226 | def buffer(sliceable, offset=0): |
|
226 | def buffer(sliceable, offset=0): | |
227 | return memoryview(sliceable)[offset:] |
|
227 | return memoryview(sliceable)[offset:] | |
228 |
|
228 | |||
229 | import subprocess |
|
229 | import subprocess | |
230 | closefds = os.name == 'posix' |
|
230 | closefds = os.name == 'posix' | |
231 |
|
231 | |||
232 | def unpacker(fmt): |
|
232 | def unpacker(fmt): | |
233 | """create a struct unpacker for the specified format""" |
|
233 | """create a struct unpacker for the specified format""" | |
234 | try: |
|
234 | try: | |
235 | # 2.5+ |
|
235 | # 2.5+ | |
236 | return struct.Struct(fmt).unpack |
|
236 | return struct.Struct(fmt).unpack | |
237 | except AttributeError: |
|
237 | except AttributeError: | |
238 | # 2.4 |
|
238 | # 2.4 | |
239 | return lambda buf: struct.unpack(fmt, buf) |
|
239 | return lambda buf: struct.unpack(fmt, buf) | |
240 |
|
240 | |||
241 | def popen2(cmd, env=None, newlines=False): |
|
241 | def popen2(cmd, env=None, newlines=False): | |
242 | # Setting bufsize to -1 lets the system decide the buffer size. |
|
242 | # Setting bufsize to -1 lets the system decide the buffer size. | |
243 | # The default for bufsize is 0, meaning unbuffered. This leads to |
|
243 | # The default for bufsize is 0, meaning unbuffered. This leads to | |
244 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
|
244 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 | |
245 | p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
|
245 | p = subprocess.Popen(cmd, shell=True, bufsize=-1, | |
246 | close_fds=closefds, |
|
246 | close_fds=closefds, | |
247 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
|
247 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
248 | universal_newlines=newlines, |
|
248 | universal_newlines=newlines, | |
249 | env=env) |
|
249 | env=env) | |
250 | return p.stdin, p.stdout |
|
250 | return p.stdin, p.stdout | |
251 |
|
251 | |||
252 | def popen3(cmd, env=None, newlines=False): |
|
252 | def popen3(cmd, env=None, newlines=False): | |
253 | stdin, stdout, stderr, p = popen4(cmd, env, newlines) |
|
253 | stdin, stdout, stderr, p = popen4(cmd, env, newlines) | |
254 | return stdin, stdout, stderr |
|
254 | return stdin, stdout, stderr | |
255 |
|
255 | |||
256 | def popen4(cmd, env=None, newlines=False): |
|
256 | def popen4(cmd, env=None, newlines=False): | |
257 | p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
|
257 | p = subprocess.Popen(cmd, shell=True, bufsize=-1, | |
258 | close_fds=closefds, |
|
258 | close_fds=closefds, | |
259 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
|
259 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
260 | stderr=subprocess.PIPE, |
|
260 | stderr=subprocess.PIPE, | |
261 | universal_newlines=newlines, |
|
261 | universal_newlines=newlines, | |
262 | env=env) |
|
262 | env=env) | |
263 | return p.stdin, p.stdout, p.stderr, p |
|
263 | return p.stdin, p.stdout, p.stderr, p | |
264 |
|
264 | |||
265 | def version(): |
|
265 | def version(): | |
266 | """Return version information if available.""" |
|
266 | """Return version information if available.""" | |
267 | try: |
|
267 | try: | |
268 | import __version__ |
|
268 | import __version__ | |
269 | return __version__.version |
|
269 | return __version__.version | |
270 | except ImportError: |
|
270 | except ImportError: | |
271 | return 'unknown' |
|
271 | return 'unknown' | |
272 |
|
272 | |||
273 | # used by parsedate |
|
273 | # used by parsedate | |
274 | defaultdateformats = ( |
|
274 | defaultdateformats = ( | |
275 | '%Y-%m-%d %H:%M:%S', |
|
275 | '%Y-%m-%d %H:%M:%S', | |
276 | '%Y-%m-%d %I:%M:%S%p', |
|
276 | '%Y-%m-%d %I:%M:%S%p', | |
277 | '%Y-%m-%d %H:%M', |
|
277 | '%Y-%m-%d %H:%M', | |
278 | '%Y-%m-%d %I:%M%p', |
|
278 | '%Y-%m-%d %I:%M%p', | |
279 | '%Y-%m-%d', |
|
279 | '%Y-%m-%d', | |
280 | '%m-%d', |
|
280 | '%m-%d', | |
281 | '%m/%d', |
|
281 | '%m/%d', | |
282 | '%m/%d/%y', |
|
282 | '%m/%d/%y', | |
283 | '%m/%d/%Y', |
|
283 | '%m/%d/%Y', | |
284 | '%a %b %d %H:%M:%S %Y', |
|
284 | '%a %b %d %H:%M:%S %Y', | |
285 | '%a %b %d %I:%M:%S%p %Y', |
|
285 | '%a %b %d %I:%M:%S%p %Y', | |
286 | '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" |
|
286 | '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" | |
287 | '%b %d %H:%M:%S %Y', |
|
287 | '%b %d %H:%M:%S %Y', | |
288 | '%b %d %I:%M:%S%p %Y', |
|
288 | '%b %d %I:%M:%S%p %Y', | |
289 | '%b %d %H:%M:%S', |
|
289 | '%b %d %H:%M:%S', | |
290 | '%b %d %I:%M:%S%p', |
|
290 | '%b %d %I:%M:%S%p', | |
291 | '%b %d %H:%M', |
|
291 | '%b %d %H:%M', | |
292 | '%b %d %I:%M%p', |
|
292 | '%b %d %I:%M%p', | |
293 | '%b %d %Y', |
|
293 | '%b %d %Y', | |
294 | '%b %d', |
|
294 | '%b %d', | |
295 | '%H:%M:%S', |
|
295 | '%H:%M:%S', | |
296 | '%I:%M:%S%p', |
|
296 | '%I:%M:%S%p', | |
297 | '%H:%M', |
|
297 | '%H:%M', | |
298 | '%I:%M%p', |
|
298 | '%I:%M%p', | |
299 | ) |
|
299 | ) | |
300 |
|
300 | |||
301 | extendeddateformats = defaultdateformats + ( |
|
301 | extendeddateformats = defaultdateformats + ( | |
302 | "%Y", |
|
302 | "%Y", | |
303 | "%Y-%m", |
|
303 | "%Y-%m", | |
304 | "%b", |
|
304 | "%b", | |
305 | "%b %Y", |
|
305 | "%b %Y", | |
306 | ) |
|
306 | ) | |
307 |
|
307 | |||
308 | def cachefunc(func): |
|
308 | def cachefunc(func): | |
309 | '''cache the result of function calls''' |
|
309 | '''cache the result of function calls''' | |
310 | # XXX doesn't handle keywords args |
|
310 | # XXX doesn't handle keywords args | |
311 | if func.func_code.co_argcount == 0: |
|
311 | if func.func_code.co_argcount == 0: | |
312 | cache = [] |
|
312 | cache = [] | |
313 | def f(): |
|
313 | def f(): | |
314 | if len(cache) == 0: |
|
314 | if len(cache) == 0: | |
315 | cache.append(func()) |
|
315 | cache.append(func()) | |
316 | return cache[0] |
|
316 | return cache[0] | |
317 | return f |
|
317 | return f | |
318 | cache = {} |
|
318 | cache = {} | |
319 | if func.func_code.co_argcount == 1: |
|
319 | if func.func_code.co_argcount == 1: | |
320 | # we gain a small amount of time because |
|
320 | # we gain a small amount of time because | |
321 | # we don't need to pack/unpack the list |
|
321 | # we don't need to pack/unpack the list | |
322 | def f(arg): |
|
322 | def f(arg): | |
323 | if arg not in cache: |
|
323 | if arg not in cache: | |
324 | cache[arg] = func(arg) |
|
324 | cache[arg] = func(arg) | |
325 | return cache[arg] |
|
325 | return cache[arg] | |
326 | else: |
|
326 | else: | |
327 | def f(*args): |
|
327 | def f(*args): | |
328 | if args not in cache: |
|
328 | if args not in cache: | |
329 | cache[args] = func(*args) |
|
329 | cache[args] = func(*args) | |
330 | return cache[args] |
|
330 | return cache[args] | |
331 |
|
331 | |||
332 | return f |
|
332 | return f | |
333 |
|
333 | |||
334 | try: |
|
334 | try: | |
335 | collections.deque.remove |
|
335 | collections.deque.remove | |
336 | deque = collections.deque |
|
336 | deque = collections.deque | |
337 | except AttributeError: |
|
337 | except AttributeError: | |
338 | # python 2.4 lacks deque.remove |
|
338 | # python 2.4 lacks deque.remove | |
339 | class deque(collections.deque): |
|
339 | class deque(collections.deque): | |
340 | def remove(self, val): |
|
340 | def remove(self, val): | |
341 | for i, v in enumerate(self): |
|
341 | for i, v in enumerate(self): | |
342 | if v == val: |
|
342 | if v == val: | |
343 | del self[i] |
|
343 | del self[i] | |
344 | break |
|
344 | break | |
345 |
|
345 | |||
346 | class sortdict(dict): |
|
346 | class sortdict(dict): | |
347 | '''a simple sorted dictionary''' |
|
347 | '''a simple sorted dictionary''' | |
348 | def __init__(self, data=None): |
|
348 | def __init__(self, data=None): | |
349 | self._list = [] |
|
349 | self._list = [] | |
350 | if data: |
|
350 | if data: | |
351 | self.update(data) |
|
351 | self.update(data) | |
352 | def copy(self): |
|
352 | def copy(self): | |
353 | return sortdict(self) |
|
353 | return sortdict(self) | |
354 | def __setitem__(self, key, val): |
|
354 | def __setitem__(self, key, val): | |
355 | if key in self: |
|
355 | if key in self: | |
356 | self._list.remove(key) |
|
356 | self._list.remove(key) | |
357 | self._list.append(key) |
|
357 | self._list.append(key) | |
358 | dict.__setitem__(self, key, val) |
|
358 | dict.__setitem__(self, key, val) | |
359 | def __iter__(self): |
|
359 | def __iter__(self): | |
360 | return self._list.__iter__() |
|
360 | return self._list.__iter__() | |
361 | def update(self, src): |
|
361 | def update(self, src): | |
362 | for k in src: |
|
362 | if isinstance(src, dict): | |
363 |
s |
|
363 | src = src.iteritems() | |
|
364 | for k, v in src: | |||
|
365 | self[k] = v | |||
364 | def clear(self): |
|
366 | def clear(self): | |
365 | dict.clear(self) |
|
367 | dict.clear(self) | |
366 | self._list = [] |
|
368 | self._list = [] | |
367 | def items(self): |
|
369 | def items(self): | |
368 | return [(k, self[k]) for k in self._list] |
|
370 | return [(k, self[k]) for k in self._list] | |
369 | def __delitem__(self, key): |
|
371 | def __delitem__(self, key): | |
370 | dict.__delitem__(self, key) |
|
372 | dict.__delitem__(self, key) | |
371 | self._list.remove(key) |
|
373 | self._list.remove(key) | |
372 | def pop(self, key, *args, **kwargs): |
|
374 | def pop(self, key, *args, **kwargs): | |
373 | dict.pop(self, key, *args, **kwargs) |
|
375 | dict.pop(self, key, *args, **kwargs) | |
374 | try: |
|
376 | try: | |
375 | self._list.remove(key) |
|
377 | self._list.remove(key) | |
376 | except ValueError: |
|
378 | except ValueError: | |
377 | pass |
|
379 | pass | |
378 | def keys(self): |
|
380 | def keys(self): | |
379 | return self._list |
|
381 | return self._list | |
380 | def iterkeys(self): |
|
382 | def iterkeys(self): | |
381 | return self._list.__iter__() |
|
383 | return self._list.__iter__() | |
382 | def iteritems(self): |
|
384 | def iteritems(self): | |
383 | for k in self._list: |
|
385 | for k in self._list: | |
384 | yield k, self[k] |
|
386 | yield k, self[k] | |
385 | def insert(self, index, key, val): |
|
387 | def insert(self, index, key, val): | |
386 | self._list.insert(index, key) |
|
388 | self._list.insert(index, key) | |
387 | dict.__setitem__(self, key, val) |
|
389 | dict.__setitem__(self, key, val) | |
388 |
|
390 | |||
389 | class lrucachedict(object): |
|
391 | class lrucachedict(object): | |
390 | '''cache most recent gets from or sets to this dictionary''' |
|
392 | '''cache most recent gets from or sets to this dictionary''' | |
391 | def __init__(self, maxsize): |
|
393 | def __init__(self, maxsize): | |
392 | self._cache = {} |
|
394 | self._cache = {} | |
393 | self._maxsize = maxsize |
|
395 | self._maxsize = maxsize | |
394 | self._order = deque() |
|
396 | self._order = deque() | |
395 |
|
397 | |||
396 | def __getitem__(self, key): |
|
398 | def __getitem__(self, key): | |
397 | value = self._cache[key] |
|
399 | value = self._cache[key] | |
398 | self._order.remove(key) |
|
400 | self._order.remove(key) | |
399 | self._order.append(key) |
|
401 | self._order.append(key) | |
400 | return value |
|
402 | return value | |
401 |
|
403 | |||
402 | def __setitem__(self, key, value): |
|
404 | def __setitem__(self, key, value): | |
403 | if key not in self._cache: |
|
405 | if key not in self._cache: | |
404 | if len(self._cache) >= self._maxsize: |
|
406 | if len(self._cache) >= self._maxsize: | |
405 | del self._cache[self._order.popleft()] |
|
407 | del self._cache[self._order.popleft()] | |
406 | else: |
|
408 | else: | |
407 | self._order.remove(key) |
|
409 | self._order.remove(key) | |
408 | self._cache[key] = value |
|
410 | self._cache[key] = value | |
409 | self._order.append(key) |
|
411 | self._order.append(key) | |
410 |
|
412 | |||
411 | def __contains__(self, key): |
|
413 | def __contains__(self, key): | |
412 | return key in self._cache |
|
414 | return key in self._cache | |
413 |
|
415 | |||
414 | def clear(self): |
|
416 | def clear(self): | |
415 | self._cache.clear() |
|
417 | self._cache.clear() | |
416 | self._order = deque() |
|
418 | self._order = deque() | |
417 |
|
419 | |||
418 | def lrucachefunc(func): |
|
420 | def lrucachefunc(func): | |
419 | '''cache most recent results of function calls''' |
|
421 | '''cache most recent results of function calls''' | |
420 | cache = {} |
|
422 | cache = {} | |
421 | order = deque() |
|
423 | order = deque() | |
422 | if func.func_code.co_argcount == 1: |
|
424 | if func.func_code.co_argcount == 1: | |
423 | def f(arg): |
|
425 | def f(arg): | |
424 | if arg not in cache: |
|
426 | if arg not in cache: | |
425 | if len(cache) > 20: |
|
427 | if len(cache) > 20: | |
426 | del cache[order.popleft()] |
|
428 | del cache[order.popleft()] | |
427 | cache[arg] = func(arg) |
|
429 | cache[arg] = func(arg) | |
428 | else: |
|
430 | else: | |
429 | order.remove(arg) |
|
431 | order.remove(arg) | |
430 | order.append(arg) |
|
432 | order.append(arg) | |
431 | return cache[arg] |
|
433 | return cache[arg] | |
432 | else: |
|
434 | else: | |
433 | def f(*args): |
|
435 | def f(*args): | |
434 | if args not in cache: |
|
436 | if args not in cache: | |
435 | if len(cache) > 20: |
|
437 | if len(cache) > 20: | |
436 | del cache[order.popleft()] |
|
438 | del cache[order.popleft()] | |
437 | cache[args] = func(*args) |
|
439 | cache[args] = func(*args) | |
438 | else: |
|
440 | else: | |
439 | order.remove(args) |
|
441 | order.remove(args) | |
440 | order.append(args) |
|
442 | order.append(args) | |
441 | return cache[args] |
|
443 | return cache[args] | |
442 |
|
444 | |||
443 | return f |
|
445 | return f | |
444 |
|
446 | |||
445 | class propertycache(object): |
|
447 | class propertycache(object): | |
446 | def __init__(self, func): |
|
448 | def __init__(self, func): | |
447 | self.func = func |
|
449 | self.func = func | |
448 | self.name = func.__name__ |
|
450 | self.name = func.__name__ | |
449 | def __get__(self, obj, type=None): |
|
451 | def __get__(self, obj, type=None): | |
450 | result = self.func(obj) |
|
452 | result = self.func(obj) | |
451 | self.cachevalue(obj, result) |
|
453 | self.cachevalue(obj, result) | |
452 | return result |
|
454 | return result | |
453 |
|
455 | |||
454 | def cachevalue(self, obj, value): |
|
456 | def cachevalue(self, obj, value): | |
455 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) |
|
457 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) | |
456 | obj.__dict__[self.name] = value |
|
458 | obj.__dict__[self.name] = value | |
457 |
|
459 | |||
458 | def pipefilter(s, cmd): |
|
460 | def pipefilter(s, cmd): | |
459 | '''filter string S through command CMD, returning its output''' |
|
461 | '''filter string S through command CMD, returning its output''' | |
460 | p = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
|
462 | p = subprocess.Popen(cmd, shell=True, close_fds=closefds, | |
461 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
463 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
462 | pout, perr = p.communicate(s) |
|
464 | pout, perr = p.communicate(s) | |
463 | return pout |
|
465 | return pout | |
464 |
|
466 | |||
465 | def tempfilter(s, cmd): |
|
467 | def tempfilter(s, cmd): | |
466 | '''filter string S through a pair of temporary files with CMD. |
|
468 | '''filter string S through a pair of temporary files with CMD. | |
467 | CMD is used as a template to create the real command to be run, |
|
469 | CMD is used as a template to create the real command to be run, | |
468 | with the strings INFILE and OUTFILE replaced by the real names of |
|
470 | with the strings INFILE and OUTFILE replaced by the real names of | |
469 | the temporary files generated.''' |
|
471 | the temporary files generated.''' | |
470 | inname, outname = None, None |
|
472 | inname, outname = None, None | |
471 | try: |
|
473 | try: | |
472 | infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') |
|
474 | infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') | |
473 | fp = os.fdopen(infd, 'wb') |
|
475 | fp = os.fdopen(infd, 'wb') | |
474 | fp.write(s) |
|
476 | fp.write(s) | |
475 | fp.close() |
|
477 | fp.close() | |
476 | outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') |
|
478 | outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') | |
477 | os.close(outfd) |
|
479 | os.close(outfd) | |
478 | cmd = cmd.replace('INFILE', inname) |
|
480 | cmd = cmd.replace('INFILE', inname) | |
479 | cmd = cmd.replace('OUTFILE', outname) |
|
481 | cmd = cmd.replace('OUTFILE', outname) | |
480 | code = os.system(cmd) |
|
482 | code = os.system(cmd) | |
481 | if sys.platform == 'OpenVMS' and code & 1: |
|
483 | if sys.platform == 'OpenVMS' and code & 1: | |
482 | code = 0 |
|
484 | code = 0 | |
483 | if code: |
|
485 | if code: | |
484 | raise Abort(_("command '%s' failed: %s") % |
|
486 | raise Abort(_("command '%s' failed: %s") % | |
485 | (cmd, explainexit(code))) |
|
487 | (cmd, explainexit(code))) | |
486 | fp = open(outname, 'rb') |
|
488 | fp = open(outname, 'rb') | |
487 | r = fp.read() |
|
489 | r = fp.read() | |
488 | fp.close() |
|
490 | fp.close() | |
489 | return r |
|
491 | return r | |
490 | finally: |
|
492 | finally: | |
491 | try: |
|
493 | try: | |
492 | if inname: |
|
494 | if inname: | |
493 | os.unlink(inname) |
|
495 | os.unlink(inname) | |
494 | except OSError: |
|
496 | except OSError: | |
495 | pass |
|
497 | pass | |
496 | try: |
|
498 | try: | |
497 | if outname: |
|
499 | if outname: | |
498 | os.unlink(outname) |
|
500 | os.unlink(outname) | |
499 | except OSError: |
|
501 | except OSError: | |
500 | pass |
|
502 | pass | |
501 |
|
503 | |||
502 | filtertable = { |
|
504 | filtertable = { | |
503 | 'tempfile:': tempfilter, |
|
505 | 'tempfile:': tempfilter, | |
504 | 'pipe:': pipefilter, |
|
506 | 'pipe:': pipefilter, | |
505 | } |
|
507 | } | |
506 |
|
508 | |||
507 | def filter(s, cmd): |
|
509 | def filter(s, cmd): | |
508 | "filter a string through a command that transforms its input to its output" |
|
510 | "filter a string through a command that transforms its input to its output" | |
509 | for name, fn in filtertable.iteritems(): |
|
511 | for name, fn in filtertable.iteritems(): | |
510 | if cmd.startswith(name): |
|
512 | if cmd.startswith(name): | |
511 | return fn(s, cmd[len(name):].lstrip()) |
|
513 | return fn(s, cmd[len(name):].lstrip()) | |
512 | return pipefilter(s, cmd) |
|
514 | return pipefilter(s, cmd) | |
513 |
|
515 | |||
514 | def binary(s): |
|
516 | def binary(s): | |
515 | """return true if a string is binary data""" |
|
517 | """return true if a string is binary data""" | |
516 | return bool(s and '\0' in s) |
|
518 | return bool(s and '\0' in s) | |
517 |
|
519 | |||
518 | def increasingchunks(source, min=1024, max=65536): |
|
520 | def increasingchunks(source, min=1024, max=65536): | |
519 | '''return no less than min bytes per chunk while data remains, |
|
521 | '''return no less than min bytes per chunk while data remains, | |
520 | doubling min after each chunk until it reaches max''' |
|
522 | doubling min after each chunk until it reaches max''' | |
521 | def log2(x): |
|
523 | def log2(x): | |
522 | if not x: |
|
524 | if not x: | |
523 | return 0 |
|
525 | return 0 | |
524 | i = 0 |
|
526 | i = 0 | |
525 | while x: |
|
527 | while x: | |
526 | x >>= 1 |
|
528 | x >>= 1 | |
527 | i += 1 |
|
529 | i += 1 | |
528 | return i - 1 |
|
530 | return i - 1 | |
529 |
|
531 | |||
530 | buf = [] |
|
532 | buf = [] | |
531 | blen = 0 |
|
533 | blen = 0 | |
532 | for chunk in source: |
|
534 | for chunk in source: | |
533 | buf.append(chunk) |
|
535 | buf.append(chunk) | |
534 | blen += len(chunk) |
|
536 | blen += len(chunk) | |
535 | if blen >= min: |
|
537 | if blen >= min: | |
536 | if min < max: |
|
538 | if min < max: | |
537 | min = min << 1 |
|
539 | min = min << 1 | |
538 | nmin = 1 << log2(blen) |
|
540 | nmin = 1 << log2(blen) | |
539 | if nmin > min: |
|
541 | if nmin > min: | |
540 | min = nmin |
|
542 | min = nmin | |
541 | if min > max: |
|
543 | if min > max: | |
542 | min = max |
|
544 | min = max | |
543 | yield ''.join(buf) |
|
545 | yield ''.join(buf) | |
544 | blen = 0 |
|
546 | blen = 0 | |
545 | buf = [] |
|
547 | buf = [] | |
546 | if buf: |
|
548 | if buf: | |
547 | yield ''.join(buf) |
|
549 | yield ''.join(buf) | |
548 |
|
550 | |||
549 | Abort = error.Abort |
|
551 | Abort = error.Abort | |
550 |
|
552 | |||
551 | def always(fn): |
|
553 | def always(fn): | |
552 | return True |
|
554 | return True | |
553 |
|
555 | |||
554 | def never(fn): |
|
556 | def never(fn): | |
555 | return False |
|
557 | return False | |
556 |
|
558 | |||
557 | def nogc(func): |
|
559 | def nogc(func): | |
558 | """disable garbage collector |
|
560 | """disable garbage collector | |
559 |
|
561 | |||
560 | Python's garbage collector triggers a GC each time a certain number of |
|
562 | Python's garbage collector triggers a GC each time a certain number of | |
561 | container objects (the number being defined by gc.get_threshold()) are |
|
563 | container objects (the number being defined by gc.get_threshold()) are | |
562 | allocated even when marked not to be tracked by the collector. Tracking has |
|
564 | allocated even when marked not to be tracked by the collector. Tracking has | |
563 | no effect on when GCs are triggered, only on what objects the GC looks |
|
565 | no effect on when GCs are triggered, only on what objects the GC looks | |
564 | into. As a workaround, disable GC while building complex (huge) |
|
566 | into. As a workaround, disable GC while building complex (huge) | |
565 | containers. |
|
567 | containers. | |
566 |
|
568 | |||
567 | This garbage collector issue have been fixed in 2.7. |
|
569 | This garbage collector issue have been fixed in 2.7. | |
568 | """ |
|
570 | """ | |
569 | def wrapper(*args, **kwargs): |
|
571 | def wrapper(*args, **kwargs): | |
570 | gcenabled = gc.isenabled() |
|
572 | gcenabled = gc.isenabled() | |
571 | gc.disable() |
|
573 | gc.disable() | |
572 | try: |
|
574 | try: | |
573 | return func(*args, **kwargs) |
|
575 | return func(*args, **kwargs) | |
574 | finally: |
|
576 | finally: | |
575 | if gcenabled: |
|
577 | if gcenabled: | |
576 | gc.enable() |
|
578 | gc.enable() | |
577 | return wrapper |
|
579 | return wrapper | |
578 |
|
580 | |||
579 | def pathto(root, n1, n2): |
|
581 | def pathto(root, n1, n2): | |
580 | '''return the relative path from one place to another. |
|
582 | '''return the relative path from one place to another. | |
581 | root should use os.sep to separate directories |
|
583 | root should use os.sep to separate directories | |
582 | n1 should use os.sep to separate directories |
|
584 | n1 should use os.sep to separate directories | |
583 | n2 should use "/" to separate directories |
|
585 | n2 should use "/" to separate directories | |
584 | returns an os.sep-separated path. |
|
586 | returns an os.sep-separated path. | |
585 |
|
587 | |||
586 | If n1 is a relative path, it's assumed it's |
|
588 | If n1 is a relative path, it's assumed it's | |
587 | relative to root. |
|
589 | relative to root. | |
588 | n2 should always be relative to root. |
|
590 | n2 should always be relative to root. | |
589 | ''' |
|
591 | ''' | |
590 | if not n1: |
|
592 | if not n1: | |
591 | return localpath(n2) |
|
593 | return localpath(n2) | |
592 | if os.path.isabs(n1): |
|
594 | if os.path.isabs(n1): | |
593 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
|
595 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: | |
594 | return os.path.join(root, localpath(n2)) |
|
596 | return os.path.join(root, localpath(n2)) | |
595 | n2 = '/'.join((pconvert(root), n2)) |
|
597 | n2 = '/'.join((pconvert(root), n2)) | |
596 | a, b = splitpath(n1), n2.split('/') |
|
598 | a, b = splitpath(n1), n2.split('/') | |
597 | a.reverse() |
|
599 | a.reverse() | |
598 | b.reverse() |
|
600 | b.reverse() | |
599 | while a and b and a[-1] == b[-1]: |
|
601 | while a and b and a[-1] == b[-1]: | |
600 | a.pop() |
|
602 | a.pop() | |
601 | b.pop() |
|
603 | b.pop() | |
602 | b.reverse() |
|
604 | b.reverse() | |
603 | return os.sep.join((['..'] * len(a)) + b) or '.' |
|
605 | return os.sep.join((['..'] * len(a)) + b) or '.' | |
604 |
|
606 | |||
605 | def mainfrozen(): |
|
607 | def mainfrozen(): | |
606 | """return True if we are a frozen executable. |
|
608 | """return True if we are a frozen executable. | |
607 |
|
609 | |||
608 | The code supports py2exe (most common, Windows only) and tools/freeze |
|
610 | The code supports py2exe (most common, Windows only) and tools/freeze | |
609 | (portable, not much used). |
|
611 | (portable, not much used). | |
610 | """ |
|
612 | """ | |
611 | return (safehasattr(sys, "frozen") or # new py2exe |
|
613 | return (safehasattr(sys, "frozen") or # new py2exe | |
612 | safehasattr(sys, "importers") or # old py2exe |
|
614 | safehasattr(sys, "importers") or # old py2exe | |
613 | imp.is_frozen("__main__")) # tools/freeze |
|
615 | imp.is_frozen("__main__")) # tools/freeze | |
614 |
|
616 | |||
615 | # the location of data files matching the source code |
|
617 | # the location of data files matching the source code | |
616 | if mainfrozen(): |
|
618 | if mainfrozen(): | |
617 | # executable version (py2exe) doesn't support __file__ |
|
619 | # executable version (py2exe) doesn't support __file__ | |
618 | datapath = os.path.dirname(sys.executable) |
|
620 | datapath = os.path.dirname(sys.executable) | |
619 | else: |
|
621 | else: | |
620 | datapath = os.path.dirname(__file__) |
|
622 | datapath = os.path.dirname(__file__) | |
621 |
|
623 | |||
622 | i18n.setdatapath(datapath) |
|
624 | i18n.setdatapath(datapath) | |
623 |
|
625 | |||
624 | _hgexecutable = None |
|
626 | _hgexecutable = None | |
625 |
|
627 | |||
626 | def hgexecutable(): |
|
628 | def hgexecutable(): | |
627 | """return location of the 'hg' executable. |
|
629 | """return location of the 'hg' executable. | |
628 |
|
630 | |||
629 | Defaults to $HG or 'hg' in the search path. |
|
631 | Defaults to $HG or 'hg' in the search path. | |
630 | """ |
|
632 | """ | |
631 | if _hgexecutable is None: |
|
633 | if _hgexecutable is None: | |
632 | hg = os.environ.get('HG') |
|
634 | hg = os.environ.get('HG') | |
633 | mainmod = sys.modules['__main__'] |
|
635 | mainmod = sys.modules['__main__'] | |
634 | if hg: |
|
636 | if hg: | |
635 | _sethgexecutable(hg) |
|
637 | _sethgexecutable(hg) | |
636 | elif mainfrozen(): |
|
638 | elif mainfrozen(): | |
637 | _sethgexecutable(sys.executable) |
|
639 | _sethgexecutable(sys.executable) | |
638 | elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg': |
|
640 | elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg': | |
639 | _sethgexecutable(mainmod.__file__) |
|
641 | _sethgexecutable(mainmod.__file__) | |
640 | else: |
|
642 | else: | |
641 | exe = findexe('hg') or os.path.basename(sys.argv[0]) |
|
643 | exe = findexe('hg') or os.path.basename(sys.argv[0]) | |
642 | _sethgexecutable(exe) |
|
644 | _sethgexecutable(exe) | |
643 | return _hgexecutable |
|
645 | return _hgexecutable | |
644 |
|
646 | |||
645 | def _sethgexecutable(path): |
|
647 | def _sethgexecutable(path): | |
646 | """set location of the 'hg' executable""" |
|
648 | """set location of the 'hg' executable""" | |
647 | global _hgexecutable |
|
649 | global _hgexecutable | |
648 | _hgexecutable = path |
|
650 | _hgexecutable = path | |
649 |
|
651 | |||
650 | def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None, out=None): |
|
652 | def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None, out=None): | |
651 | '''enhanced shell command execution. |
|
653 | '''enhanced shell command execution. | |
652 | run with environment maybe modified, maybe in different dir. |
|
654 | run with environment maybe modified, maybe in different dir. | |
653 |
|
655 | |||
654 | if command fails and onerr is None, return status, else raise onerr |
|
656 | if command fails and onerr is None, return status, else raise onerr | |
655 | object as exception. |
|
657 | object as exception. | |
656 |
|
658 | |||
657 | if out is specified, it is assumed to be a file-like object that has a |
|
659 | if out is specified, it is assumed to be a file-like object that has a | |
658 | write() method. stdout and stderr will be redirected to out.''' |
|
660 | write() method. stdout and stderr will be redirected to out.''' | |
659 | try: |
|
661 | try: | |
660 | sys.stdout.flush() |
|
662 | sys.stdout.flush() | |
661 | except Exception: |
|
663 | except Exception: | |
662 | pass |
|
664 | pass | |
663 | def py2shell(val): |
|
665 | def py2shell(val): | |
664 | 'convert python object into string that is useful to shell' |
|
666 | 'convert python object into string that is useful to shell' | |
665 | if val is None or val is False: |
|
667 | if val is None or val is False: | |
666 | return '0' |
|
668 | return '0' | |
667 | if val is True: |
|
669 | if val is True: | |
668 | return '1' |
|
670 | return '1' | |
669 | return str(val) |
|
671 | return str(val) | |
670 | origcmd = cmd |
|
672 | origcmd = cmd | |
671 | cmd = quotecommand(cmd) |
|
673 | cmd = quotecommand(cmd) | |
672 | if sys.platform == 'plan9' and (sys.version_info[0] == 2 |
|
674 | if sys.platform == 'plan9' and (sys.version_info[0] == 2 | |
673 | and sys.version_info[1] < 7): |
|
675 | and sys.version_info[1] < 7): | |
674 | # subprocess kludge to work around issues in half-baked Python |
|
676 | # subprocess kludge to work around issues in half-baked Python | |
675 | # ports, notably bichued/python: |
|
677 | # ports, notably bichued/python: | |
676 | if not cwd is None: |
|
678 | if not cwd is None: | |
677 | os.chdir(cwd) |
|
679 | os.chdir(cwd) | |
678 | rc = os.system(cmd) |
|
680 | rc = os.system(cmd) | |
679 | else: |
|
681 | else: | |
680 | env = dict(os.environ) |
|
682 | env = dict(os.environ) | |
681 | env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
|
683 | env.update((k, py2shell(v)) for k, v in environ.iteritems()) | |
682 | env['HG'] = hgexecutable() |
|
684 | env['HG'] = hgexecutable() | |
683 | if out is None or out == sys.__stdout__: |
|
685 | if out is None or out == sys.__stdout__: | |
684 | rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
|
686 | rc = subprocess.call(cmd, shell=True, close_fds=closefds, | |
685 | env=env, cwd=cwd) |
|
687 | env=env, cwd=cwd) | |
686 | else: |
|
688 | else: | |
687 | proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
|
689 | proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, | |
688 | env=env, cwd=cwd, stdout=subprocess.PIPE, |
|
690 | env=env, cwd=cwd, stdout=subprocess.PIPE, | |
689 | stderr=subprocess.STDOUT) |
|
691 | stderr=subprocess.STDOUT) | |
690 | while True: |
|
692 | while True: | |
691 | line = proc.stdout.readline() |
|
693 | line = proc.stdout.readline() | |
692 | if not line: |
|
694 | if not line: | |
693 | break |
|
695 | break | |
694 | out.write(line) |
|
696 | out.write(line) | |
695 | proc.wait() |
|
697 | proc.wait() | |
696 | rc = proc.returncode |
|
698 | rc = proc.returncode | |
697 | if sys.platform == 'OpenVMS' and rc & 1: |
|
699 | if sys.platform == 'OpenVMS' and rc & 1: | |
698 | rc = 0 |
|
700 | rc = 0 | |
699 | if rc and onerr: |
|
701 | if rc and onerr: | |
700 | errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), |
|
702 | errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), | |
701 | explainexit(rc)[0]) |
|
703 | explainexit(rc)[0]) | |
702 | if errprefix: |
|
704 | if errprefix: | |
703 | errmsg = '%s: %s' % (errprefix, errmsg) |
|
705 | errmsg = '%s: %s' % (errprefix, errmsg) | |
704 | raise onerr(errmsg) |
|
706 | raise onerr(errmsg) | |
705 | return rc |
|
707 | return rc | |
706 |
|
708 | |||
707 | def checksignature(func): |
|
709 | def checksignature(func): | |
708 | '''wrap a function with code to check for calling errors''' |
|
710 | '''wrap a function with code to check for calling errors''' | |
709 | def check(*args, **kwargs): |
|
711 | def check(*args, **kwargs): | |
710 | try: |
|
712 | try: | |
711 | return func(*args, **kwargs) |
|
713 | return func(*args, **kwargs) | |
712 | except TypeError: |
|
714 | except TypeError: | |
713 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
|
715 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: | |
714 | raise error.SignatureError |
|
716 | raise error.SignatureError | |
715 | raise |
|
717 | raise | |
716 |
|
718 | |||
717 | return check |
|
719 | return check | |
718 |
|
720 | |||
719 | def copyfile(src, dest, hardlink=False): |
|
721 | def copyfile(src, dest, hardlink=False): | |
720 | "copy a file, preserving mode and atime/mtime" |
|
722 | "copy a file, preserving mode and atime/mtime" | |
721 | if os.path.lexists(dest): |
|
723 | if os.path.lexists(dest): | |
722 | unlink(dest) |
|
724 | unlink(dest) | |
723 | # hardlinks are problematic on CIFS, quietly ignore this flag |
|
725 | # hardlinks are problematic on CIFS, quietly ignore this flag | |
724 | # until we find a way to work around it cleanly (issue4546) |
|
726 | # until we find a way to work around it cleanly (issue4546) | |
725 | if False and hardlink: |
|
727 | if False and hardlink: | |
726 | try: |
|
728 | try: | |
727 | oslink(src, dest) |
|
729 | oslink(src, dest) | |
728 | return |
|
730 | return | |
729 | except (IOError, OSError): |
|
731 | except (IOError, OSError): | |
730 | pass # fall back to normal copy |
|
732 | pass # fall back to normal copy | |
731 | if os.path.islink(src): |
|
733 | if os.path.islink(src): | |
732 | os.symlink(os.readlink(src), dest) |
|
734 | os.symlink(os.readlink(src), dest) | |
733 | else: |
|
735 | else: | |
734 | try: |
|
736 | try: | |
735 | shutil.copyfile(src, dest) |
|
737 | shutil.copyfile(src, dest) | |
736 | shutil.copymode(src, dest) |
|
738 | shutil.copymode(src, dest) | |
737 | except shutil.Error, inst: |
|
739 | except shutil.Error, inst: | |
738 | raise Abort(str(inst)) |
|
740 | raise Abort(str(inst)) | |
739 |
|
741 | |||
740 | def copyfiles(src, dst, hardlink=None): |
|
742 | def copyfiles(src, dst, hardlink=None): | |
741 | """Copy a directory tree using hardlinks if possible""" |
|
743 | """Copy a directory tree using hardlinks if possible""" | |
742 |
|
744 | |||
743 | if hardlink is None: |
|
745 | if hardlink is None: | |
744 | hardlink = (os.stat(src).st_dev == |
|
746 | hardlink = (os.stat(src).st_dev == | |
745 | os.stat(os.path.dirname(dst)).st_dev) |
|
747 | os.stat(os.path.dirname(dst)).st_dev) | |
746 |
|
748 | |||
747 | num = 0 |
|
749 | num = 0 | |
748 | if os.path.isdir(src): |
|
750 | if os.path.isdir(src): | |
749 | os.mkdir(dst) |
|
751 | os.mkdir(dst) | |
750 | for name, kind in osutil.listdir(src): |
|
752 | for name, kind in osutil.listdir(src): | |
751 | srcname = os.path.join(src, name) |
|
753 | srcname = os.path.join(src, name) | |
752 | dstname = os.path.join(dst, name) |
|
754 | dstname = os.path.join(dst, name) | |
753 | hardlink, n = copyfiles(srcname, dstname, hardlink) |
|
755 | hardlink, n = copyfiles(srcname, dstname, hardlink) | |
754 | num += n |
|
756 | num += n | |
755 | else: |
|
757 | else: | |
756 | if hardlink: |
|
758 | if hardlink: | |
757 | try: |
|
759 | try: | |
758 | oslink(src, dst) |
|
760 | oslink(src, dst) | |
759 | except (IOError, OSError): |
|
761 | except (IOError, OSError): | |
760 | hardlink = False |
|
762 | hardlink = False | |
761 | shutil.copy(src, dst) |
|
763 | shutil.copy(src, dst) | |
762 | else: |
|
764 | else: | |
763 | shutil.copy(src, dst) |
|
765 | shutil.copy(src, dst) | |
764 | num += 1 |
|
766 | num += 1 | |
765 |
|
767 | |||
766 | return hardlink, num |
|
768 | return hardlink, num | |
767 |
|
769 | |||
768 | _winreservednames = '''con prn aux nul |
|
770 | _winreservednames = '''con prn aux nul | |
769 | com1 com2 com3 com4 com5 com6 com7 com8 com9 |
|
771 | com1 com2 com3 com4 com5 com6 com7 com8 com9 | |
770 | lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() |
|
772 | lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() | |
771 | _winreservedchars = ':*?"<>|' |
|
773 | _winreservedchars = ':*?"<>|' | |
772 | def checkwinfilename(path): |
|
774 | def checkwinfilename(path): | |
773 | r'''Check that the base-relative path is a valid filename on Windows. |
|
775 | r'''Check that the base-relative path is a valid filename on Windows. | |
774 | Returns None if the path is ok, or a UI string describing the problem. |
|
776 | Returns None if the path is ok, or a UI string describing the problem. | |
775 |
|
777 | |||
776 | >>> checkwinfilename("just/a/normal/path") |
|
778 | >>> checkwinfilename("just/a/normal/path") | |
777 | >>> checkwinfilename("foo/bar/con.xml") |
|
779 | >>> checkwinfilename("foo/bar/con.xml") | |
778 | "filename contains 'con', which is reserved on Windows" |
|
780 | "filename contains 'con', which is reserved on Windows" | |
779 | >>> checkwinfilename("foo/con.xml/bar") |
|
781 | >>> checkwinfilename("foo/con.xml/bar") | |
780 | "filename contains 'con', which is reserved on Windows" |
|
782 | "filename contains 'con', which is reserved on Windows" | |
781 | >>> checkwinfilename("foo/bar/xml.con") |
|
783 | >>> checkwinfilename("foo/bar/xml.con") | |
782 | >>> checkwinfilename("foo/bar/AUX/bla.txt") |
|
784 | >>> checkwinfilename("foo/bar/AUX/bla.txt") | |
783 | "filename contains 'AUX', which is reserved on Windows" |
|
785 | "filename contains 'AUX', which is reserved on Windows" | |
784 | >>> checkwinfilename("foo/bar/bla:.txt") |
|
786 | >>> checkwinfilename("foo/bar/bla:.txt") | |
785 | "filename contains ':', which is reserved on Windows" |
|
787 | "filename contains ':', which is reserved on Windows" | |
786 | >>> checkwinfilename("foo/bar/b\07la.txt") |
|
788 | >>> checkwinfilename("foo/bar/b\07la.txt") | |
787 | "filename contains '\\x07', which is invalid on Windows" |
|
789 | "filename contains '\\x07', which is invalid on Windows" | |
788 | >>> checkwinfilename("foo/bar/bla ") |
|
790 | >>> checkwinfilename("foo/bar/bla ") | |
789 | "filename ends with ' ', which is not allowed on Windows" |
|
791 | "filename ends with ' ', which is not allowed on Windows" | |
790 | >>> checkwinfilename("../bar") |
|
792 | >>> checkwinfilename("../bar") | |
791 | >>> checkwinfilename("foo\\") |
|
793 | >>> checkwinfilename("foo\\") | |
792 | "filename ends with '\\', which is invalid on Windows" |
|
794 | "filename ends with '\\', which is invalid on Windows" | |
793 | >>> checkwinfilename("foo\\/bar") |
|
795 | >>> checkwinfilename("foo\\/bar") | |
794 | "directory name ends with '\\', which is invalid on Windows" |
|
796 | "directory name ends with '\\', which is invalid on Windows" | |
795 | ''' |
|
797 | ''' | |
796 | if path.endswith('\\'): |
|
798 | if path.endswith('\\'): | |
797 | return _("filename ends with '\\', which is invalid on Windows") |
|
799 | return _("filename ends with '\\', which is invalid on Windows") | |
798 | if '\\/' in path: |
|
800 | if '\\/' in path: | |
799 | return _("directory name ends with '\\', which is invalid on Windows") |
|
801 | return _("directory name ends with '\\', which is invalid on Windows") | |
800 | for n in path.replace('\\', '/').split('/'): |
|
802 | for n in path.replace('\\', '/').split('/'): | |
801 | if not n: |
|
803 | if not n: | |
802 | continue |
|
804 | continue | |
803 | for c in n: |
|
805 | for c in n: | |
804 | if c in _winreservedchars: |
|
806 | if c in _winreservedchars: | |
805 | return _("filename contains '%s', which is reserved " |
|
807 | return _("filename contains '%s', which is reserved " | |
806 | "on Windows") % c |
|
808 | "on Windows") % c | |
807 | if ord(c) <= 31: |
|
809 | if ord(c) <= 31: | |
808 | return _("filename contains %r, which is invalid " |
|
810 | return _("filename contains %r, which is invalid " | |
809 | "on Windows") % c |
|
811 | "on Windows") % c | |
810 | base = n.split('.')[0] |
|
812 | base = n.split('.')[0] | |
811 | if base and base.lower() in _winreservednames: |
|
813 | if base and base.lower() in _winreservednames: | |
812 | return _("filename contains '%s', which is reserved " |
|
814 | return _("filename contains '%s', which is reserved " | |
813 | "on Windows") % base |
|
815 | "on Windows") % base | |
814 | t = n[-1] |
|
816 | t = n[-1] | |
815 | if t in '. ' and n not in '..': |
|
817 | if t in '. ' and n not in '..': | |
816 | return _("filename ends with '%s', which is not allowed " |
|
818 | return _("filename ends with '%s', which is not allowed " | |
817 | "on Windows") % t |
|
819 | "on Windows") % t | |
818 |
|
820 | |||
819 | if os.name == 'nt': |
|
821 | if os.name == 'nt': | |
820 | checkosfilename = checkwinfilename |
|
822 | checkosfilename = checkwinfilename | |
821 | else: |
|
823 | else: | |
822 | checkosfilename = platform.checkosfilename |
|
824 | checkosfilename = platform.checkosfilename | |
823 |
|
825 | |||
824 | def makelock(info, pathname): |
|
826 | def makelock(info, pathname): | |
825 | try: |
|
827 | try: | |
826 | return os.symlink(info, pathname) |
|
828 | return os.symlink(info, pathname) | |
827 | except OSError, why: |
|
829 | except OSError, why: | |
828 | if why.errno == errno.EEXIST: |
|
830 | if why.errno == errno.EEXIST: | |
829 | raise |
|
831 | raise | |
830 | except AttributeError: # no symlink in os |
|
832 | except AttributeError: # no symlink in os | |
831 | pass |
|
833 | pass | |
832 |
|
834 | |||
833 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) |
|
835 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) | |
834 | os.write(ld, info) |
|
836 | os.write(ld, info) | |
835 | os.close(ld) |
|
837 | os.close(ld) | |
836 |
|
838 | |||
837 | def readlock(pathname): |
|
839 | def readlock(pathname): | |
838 | try: |
|
840 | try: | |
839 | return os.readlink(pathname) |
|
841 | return os.readlink(pathname) | |
840 | except OSError, why: |
|
842 | except OSError, why: | |
841 | if why.errno not in (errno.EINVAL, errno.ENOSYS): |
|
843 | if why.errno not in (errno.EINVAL, errno.ENOSYS): | |
842 | raise |
|
844 | raise | |
843 | except AttributeError: # no symlink in os |
|
845 | except AttributeError: # no symlink in os | |
844 | pass |
|
846 | pass | |
845 | fp = posixfile(pathname) |
|
847 | fp = posixfile(pathname) | |
846 | r = fp.read() |
|
848 | r = fp.read() | |
847 | fp.close() |
|
849 | fp.close() | |
848 | return r |
|
850 | return r | |
849 |
|
851 | |||
850 | def fstat(fp): |
|
852 | def fstat(fp): | |
851 | '''stat file object that may not have fileno method.''' |
|
853 | '''stat file object that may not have fileno method.''' | |
852 | try: |
|
854 | try: | |
853 | return os.fstat(fp.fileno()) |
|
855 | return os.fstat(fp.fileno()) | |
854 | except AttributeError: |
|
856 | except AttributeError: | |
855 | return os.stat(fp.name) |
|
857 | return os.stat(fp.name) | |
856 |
|
858 | |||
857 | # File system features |
|
859 | # File system features | |
858 |
|
860 | |||
859 | def checkcase(path): |
|
861 | def checkcase(path): | |
860 | """ |
|
862 | """ | |
861 | Return true if the given path is on a case-sensitive filesystem |
|
863 | Return true if the given path is on a case-sensitive filesystem | |
862 |
|
864 | |||
863 | Requires a path (like /foo/.hg) ending with a foldable final |
|
865 | Requires a path (like /foo/.hg) ending with a foldable final | |
864 | directory component. |
|
866 | directory component. | |
865 | """ |
|
867 | """ | |
866 | s1 = os.stat(path) |
|
868 | s1 = os.stat(path) | |
867 | d, b = os.path.split(path) |
|
869 | d, b = os.path.split(path) | |
868 | b2 = b.upper() |
|
870 | b2 = b.upper() | |
869 | if b == b2: |
|
871 | if b == b2: | |
870 | b2 = b.lower() |
|
872 | b2 = b.lower() | |
871 | if b == b2: |
|
873 | if b == b2: | |
872 | return True # no evidence against case sensitivity |
|
874 | return True # no evidence against case sensitivity | |
873 | p2 = os.path.join(d, b2) |
|
875 | p2 = os.path.join(d, b2) | |
874 | try: |
|
876 | try: | |
875 | s2 = os.stat(p2) |
|
877 | s2 = os.stat(p2) | |
876 | if s2 == s1: |
|
878 | if s2 == s1: | |
877 | return False |
|
879 | return False | |
878 | return True |
|
880 | return True | |
879 | except OSError: |
|
881 | except OSError: | |
880 | return True |
|
882 | return True | |
881 |
|
883 | |||
882 | try: |
|
884 | try: | |
883 | import re2 |
|
885 | import re2 | |
884 | _re2 = None |
|
886 | _re2 = None | |
885 | except ImportError: |
|
887 | except ImportError: | |
886 | _re2 = False |
|
888 | _re2 = False | |
887 |
|
889 | |||
888 | class _re(object): |
|
890 | class _re(object): | |
889 | def _checkre2(self): |
|
891 | def _checkre2(self): | |
890 | global _re2 |
|
892 | global _re2 | |
891 | try: |
|
893 | try: | |
892 | # check if match works, see issue3964 |
|
894 | # check if match works, see issue3964 | |
893 | _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]')) |
|
895 | _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]')) | |
894 | except ImportError: |
|
896 | except ImportError: | |
895 | _re2 = False |
|
897 | _re2 = False | |
896 |
|
898 | |||
897 | def compile(self, pat, flags=0): |
|
899 | def compile(self, pat, flags=0): | |
898 | '''Compile a regular expression, using re2 if possible |
|
900 | '''Compile a regular expression, using re2 if possible | |
899 |
|
901 | |||
900 | For best performance, use only re2-compatible regexp features. The |
|
902 | For best performance, use only re2-compatible regexp features. The | |
901 | only flags from the re module that are re2-compatible are |
|
903 | only flags from the re module that are re2-compatible are | |
902 | IGNORECASE and MULTILINE.''' |
|
904 | IGNORECASE and MULTILINE.''' | |
903 | if _re2 is None: |
|
905 | if _re2 is None: | |
904 | self._checkre2() |
|
906 | self._checkre2() | |
905 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: |
|
907 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: | |
906 | if flags & remod.IGNORECASE: |
|
908 | if flags & remod.IGNORECASE: | |
907 | pat = '(?i)' + pat |
|
909 | pat = '(?i)' + pat | |
908 | if flags & remod.MULTILINE: |
|
910 | if flags & remod.MULTILINE: | |
909 | pat = '(?m)' + pat |
|
911 | pat = '(?m)' + pat | |
910 | try: |
|
912 | try: | |
911 | return re2.compile(pat) |
|
913 | return re2.compile(pat) | |
912 | except re2.error: |
|
914 | except re2.error: | |
913 | pass |
|
915 | pass | |
914 | return remod.compile(pat, flags) |
|
916 | return remod.compile(pat, flags) | |
915 |
|
917 | |||
916 | @propertycache |
|
918 | @propertycache | |
917 | def escape(self): |
|
919 | def escape(self): | |
918 | '''Return the version of escape corresponding to self.compile. |
|
920 | '''Return the version of escape corresponding to self.compile. | |
919 |
|
921 | |||
920 | This is imperfect because whether re2 or re is used for a particular |
|
922 | This is imperfect because whether re2 or re is used for a particular | |
921 | function depends on the flags, etc, but it's the best we can do. |
|
923 | function depends on the flags, etc, but it's the best we can do. | |
922 | ''' |
|
924 | ''' | |
923 | global _re2 |
|
925 | global _re2 | |
924 | if _re2 is None: |
|
926 | if _re2 is None: | |
925 | self._checkre2() |
|
927 | self._checkre2() | |
926 | if _re2: |
|
928 | if _re2: | |
927 | return re2.escape |
|
929 | return re2.escape | |
928 | else: |
|
930 | else: | |
929 | return remod.escape |
|
931 | return remod.escape | |
930 |
|
932 | |||
931 | re = _re() |
|
933 | re = _re() | |
932 |
|
934 | |||
933 | _fspathcache = {} |
|
935 | _fspathcache = {} | |
934 | def fspath(name, root): |
|
936 | def fspath(name, root): | |
935 | '''Get name in the case stored in the filesystem |
|
937 | '''Get name in the case stored in the filesystem | |
936 |
|
938 | |||
937 | The name should be relative to root, and be normcase-ed for efficiency. |
|
939 | The name should be relative to root, and be normcase-ed for efficiency. | |
938 |
|
940 | |||
939 | Note that this function is unnecessary, and should not be |
|
941 | Note that this function is unnecessary, and should not be | |
940 | called, for case-sensitive filesystems (simply because it's expensive). |
|
942 | called, for case-sensitive filesystems (simply because it's expensive). | |
941 |
|
943 | |||
942 | The root should be normcase-ed, too. |
|
944 | The root should be normcase-ed, too. | |
943 | ''' |
|
945 | ''' | |
944 | def _makefspathcacheentry(dir): |
|
946 | def _makefspathcacheentry(dir): | |
945 | return dict((normcase(n), n) for n in os.listdir(dir)) |
|
947 | return dict((normcase(n), n) for n in os.listdir(dir)) | |
946 |
|
948 | |||
947 | seps = os.sep |
|
949 | seps = os.sep | |
948 | if os.altsep: |
|
950 | if os.altsep: | |
949 | seps = seps + os.altsep |
|
951 | seps = seps + os.altsep | |
950 | # Protect backslashes. This gets silly very quickly. |
|
952 | # Protect backslashes. This gets silly very quickly. | |
951 | seps.replace('\\','\\\\') |
|
953 | seps.replace('\\','\\\\') | |
952 | pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps)) |
|
954 | pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps)) | |
953 | dir = os.path.normpath(root) |
|
955 | dir = os.path.normpath(root) | |
954 | result = [] |
|
956 | result = [] | |
955 | for part, sep in pattern.findall(name): |
|
957 | for part, sep in pattern.findall(name): | |
956 | if sep: |
|
958 | if sep: | |
957 | result.append(sep) |
|
959 | result.append(sep) | |
958 | continue |
|
960 | continue | |
959 |
|
961 | |||
960 | if dir not in _fspathcache: |
|
962 | if dir not in _fspathcache: | |
961 | _fspathcache[dir] = _makefspathcacheentry(dir) |
|
963 | _fspathcache[dir] = _makefspathcacheentry(dir) | |
962 | contents = _fspathcache[dir] |
|
964 | contents = _fspathcache[dir] | |
963 |
|
965 | |||
964 | found = contents.get(part) |
|
966 | found = contents.get(part) | |
965 | if not found: |
|
967 | if not found: | |
966 | # retry "once per directory" per "dirstate.walk" which |
|
968 | # retry "once per directory" per "dirstate.walk" which | |
967 | # may take place for each patches of "hg qpush", for example |
|
969 | # may take place for each patches of "hg qpush", for example | |
968 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) |
|
970 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) | |
969 | found = contents.get(part) |
|
971 | found = contents.get(part) | |
970 |
|
972 | |||
971 | result.append(found or part) |
|
973 | result.append(found or part) | |
972 | dir = os.path.join(dir, part) |
|
974 | dir = os.path.join(dir, part) | |
973 |
|
975 | |||
974 | return ''.join(result) |
|
976 | return ''.join(result) | |
975 |
|
977 | |||
976 | def checknlink(testfile): |
|
978 | def checknlink(testfile): | |
977 | '''check whether hardlink count reporting works properly''' |
|
979 | '''check whether hardlink count reporting works properly''' | |
978 |
|
980 | |||
979 | # testfile may be open, so we need a separate file for checking to |
|
981 | # testfile may be open, so we need a separate file for checking to | |
980 | # work around issue2543 (or testfile may get lost on Samba shares) |
|
982 | # work around issue2543 (or testfile may get lost on Samba shares) | |
981 | f1 = testfile + ".hgtmp1" |
|
983 | f1 = testfile + ".hgtmp1" | |
982 | if os.path.lexists(f1): |
|
984 | if os.path.lexists(f1): | |
983 | return False |
|
985 | return False | |
984 | try: |
|
986 | try: | |
985 | posixfile(f1, 'w').close() |
|
987 | posixfile(f1, 'w').close() | |
986 | except IOError: |
|
988 | except IOError: | |
987 | return False |
|
989 | return False | |
988 |
|
990 | |||
989 | f2 = testfile + ".hgtmp2" |
|
991 | f2 = testfile + ".hgtmp2" | |
990 | fd = None |
|
992 | fd = None | |
991 | try: |
|
993 | try: | |
992 | try: |
|
994 | try: | |
993 | oslink(f1, f2) |
|
995 | oslink(f1, f2) | |
994 | except OSError: |
|
996 | except OSError: | |
995 | return False |
|
997 | return False | |
996 |
|
998 | |||
997 | # nlinks() may behave differently for files on Windows shares if |
|
999 | # nlinks() may behave differently for files on Windows shares if | |
998 | # the file is open. |
|
1000 | # the file is open. | |
999 | fd = posixfile(f2) |
|
1001 | fd = posixfile(f2) | |
1000 | return nlinks(f2) > 1 |
|
1002 | return nlinks(f2) > 1 | |
1001 | finally: |
|
1003 | finally: | |
1002 | if fd is not None: |
|
1004 | if fd is not None: | |
1003 | fd.close() |
|
1005 | fd.close() | |
1004 | for f in (f1, f2): |
|
1006 | for f in (f1, f2): | |
1005 | try: |
|
1007 | try: | |
1006 | os.unlink(f) |
|
1008 | os.unlink(f) | |
1007 | except OSError: |
|
1009 | except OSError: | |
1008 | pass |
|
1010 | pass | |
1009 |
|
1011 | |||
1010 | def endswithsep(path): |
|
1012 | def endswithsep(path): | |
1011 | '''Check path ends with os.sep or os.altsep.''' |
|
1013 | '''Check path ends with os.sep or os.altsep.''' | |
1012 | return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep) |
|
1014 | return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep) | |
1013 |
|
1015 | |||
1014 | def splitpath(path): |
|
1016 | def splitpath(path): | |
1015 | '''Split path by os.sep. |
|
1017 | '''Split path by os.sep. | |
1016 | Note that this function does not use os.altsep because this is |
|
1018 | Note that this function does not use os.altsep because this is | |
1017 | an alternative of simple "xxx.split(os.sep)". |
|
1019 | an alternative of simple "xxx.split(os.sep)". | |
1018 | It is recommended to use os.path.normpath() before using this |
|
1020 | It is recommended to use os.path.normpath() before using this | |
1019 | function if need.''' |
|
1021 | function if need.''' | |
1020 | return path.split(os.sep) |
|
1022 | return path.split(os.sep) | |
1021 |
|
1023 | |||
1022 | def gui(): |
|
1024 | def gui(): | |
1023 | '''Are we running in a GUI?''' |
|
1025 | '''Are we running in a GUI?''' | |
1024 | if sys.platform == 'darwin': |
|
1026 | if sys.platform == 'darwin': | |
1025 | if 'SSH_CONNECTION' in os.environ: |
|
1027 | if 'SSH_CONNECTION' in os.environ: | |
1026 | # handle SSH access to a box where the user is logged in |
|
1028 | # handle SSH access to a box where the user is logged in | |
1027 | return False |
|
1029 | return False | |
1028 | elif getattr(osutil, 'isgui', None): |
|
1030 | elif getattr(osutil, 'isgui', None): | |
1029 | # check if a CoreGraphics session is available |
|
1031 | # check if a CoreGraphics session is available | |
1030 | return osutil.isgui() |
|
1032 | return osutil.isgui() | |
1031 | else: |
|
1033 | else: | |
1032 | # pure build; use a safe default |
|
1034 | # pure build; use a safe default | |
1033 | return True |
|
1035 | return True | |
1034 | else: |
|
1036 | else: | |
1035 | return os.name == "nt" or os.environ.get("DISPLAY") |
|
1037 | return os.name == "nt" or os.environ.get("DISPLAY") | |
1036 |
|
1038 | |||
1037 | def mktempcopy(name, emptyok=False, createmode=None): |
|
1039 | def mktempcopy(name, emptyok=False, createmode=None): | |
1038 | """Create a temporary file with the same contents from name |
|
1040 | """Create a temporary file with the same contents from name | |
1039 |
|
1041 | |||
1040 | The permission bits are copied from the original file. |
|
1042 | The permission bits are copied from the original file. | |
1041 |
|
1043 | |||
1042 | If the temporary file is going to be truncated immediately, you |
|
1044 | If the temporary file is going to be truncated immediately, you | |
1043 | can use emptyok=True as an optimization. |
|
1045 | can use emptyok=True as an optimization. | |
1044 |
|
1046 | |||
1045 | Returns the name of the temporary file. |
|
1047 | Returns the name of the temporary file. | |
1046 | """ |
|
1048 | """ | |
1047 | d, fn = os.path.split(name) |
|
1049 | d, fn = os.path.split(name) | |
1048 | fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) |
|
1050 | fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) | |
1049 | os.close(fd) |
|
1051 | os.close(fd) | |
1050 | # Temporary files are created with mode 0600, which is usually not |
|
1052 | # Temporary files are created with mode 0600, which is usually not | |
1051 | # what we want. If the original file already exists, just copy |
|
1053 | # what we want. If the original file already exists, just copy | |
1052 | # its mode. Otherwise, manually obey umask. |
|
1054 | # its mode. Otherwise, manually obey umask. | |
1053 | copymode(name, temp, createmode) |
|
1055 | copymode(name, temp, createmode) | |
1054 | if emptyok: |
|
1056 | if emptyok: | |
1055 | return temp |
|
1057 | return temp | |
1056 | try: |
|
1058 | try: | |
1057 | try: |
|
1059 | try: | |
1058 | ifp = posixfile(name, "rb") |
|
1060 | ifp = posixfile(name, "rb") | |
1059 | except IOError, inst: |
|
1061 | except IOError, inst: | |
1060 | if inst.errno == errno.ENOENT: |
|
1062 | if inst.errno == errno.ENOENT: | |
1061 | return temp |
|
1063 | return temp | |
1062 | if not getattr(inst, 'filename', None): |
|
1064 | if not getattr(inst, 'filename', None): | |
1063 | inst.filename = name |
|
1065 | inst.filename = name | |
1064 | raise |
|
1066 | raise | |
1065 | ofp = posixfile(temp, "wb") |
|
1067 | ofp = posixfile(temp, "wb") | |
1066 | for chunk in filechunkiter(ifp): |
|
1068 | for chunk in filechunkiter(ifp): | |
1067 | ofp.write(chunk) |
|
1069 | ofp.write(chunk) | |
1068 | ifp.close() |
|
1070 | ifp.close() | |
1069 | ofp.close() |
|
1071 | ofp.close() | |
1070 | except: # re-raises |
|
1072 | except: # re-raises | |
1071 | try: os.unlink(temp) |
|
1073 | try: os.unlink(temp) | |
1072 | except OSError: pass |
|
1074 | except OSError: pass | |
1073 | raise |
|
1075 | raise | |
1074 | return temp |
|
1076 | return temp | |
1075 |
|
1077 | |||
1076 | class atomictempfile(object): |
|
1078 | class atomictempfile(object): | |
1077 | '''writable file object that atomically updates a file |
|
1079 | '''writable file object that atomically updates a file | |
1078 |
|
1080 | |||
1079 | All writes will go to a temporary copy of the original file. Call |
|
1081 | All writes will go to a temporary copy of the original file. Call | |
1080 | close() when you are done writing, and atomictempfile will rename |
|
1082 | close() when you are done writing, and atomictempfile will rename | |
1081 | the temporary copy to the original name, making the changes |
|
1083 | the temporary copy to the original name, making the changes | |
1082 | visible. If the object is destroyed without being closed, all your |
|
1084 | visible. If the object is destroyed without being closed, all your | |
1083 | writes are discarded. |
|
1085 | writes are discarded. | |
1084 | ''' |
|
1086 | ''' | |
1085 | def __init__(self, name, mode='w+b', createmode=None): |
|
1087 | def __init__(self, name, mode='w+b', createmode=None): | |
1086 | self.__name = name # permanent name |
|
1088 | self.__name = name # permanent name | |
1087 | self._tempname = mktempcopy(name, emptyok=('w' in mode), |
|
1089 | self._tempname = mktempcopy(name, emptyok=('w' in mode), | |
1088 | createmode=createmode) |
|
1090 | createmode=createmode) | |
1089 | self._fp = posixfile(self._tempname, mode) |
|
1091 | self._fp = posixfile(self._tempname, mode) | |
1090 |
|
1092 | |||
1091 | # delegated methods |
|
1093 | # delegated methods | |
1092 | self.write = self._fp.write |
|
1094 | self.write = self._fp.write | |
1093 | self.seek = self._fp.seek |
|
1095 | self.seek = self._fp.seek | |
1094 | self.tell = self._fp.tell |
|
1096 | self.tell = self._fp.tell | |
1095 | self.fileno = self._fp.fileno |
|
1097 | self.fileno = self._fp.fileno | |
1096 |
|
1098 | |||
1097 | def close(self): |
|
1099 | def close(self): | |
1098 | if not self._fp.closed: |
|
1100 | if not self._fp.closed: | |
1099 | self._fp.close() |
|
1101 | self._fp.close() | |
1100 | rename(self._tempname, localpath(self.__name)) |
|
1102 | rename(self._tempname, localpath(self.__name)) | |
1101 |
|
1103 | |||
1102 | def discard(self): |
|
1104 | def discard(self): | |
1103 | if not self._fp.closed: |
|
1105 | if not self._fp.closed: | |
1104 | try: |
|
1106 | try: | |
1105 | os.unlink(self._tempname) |
|
1107 | os.unlink(self._tempname) | |
1106 | except OSError: |
|
1108 | except OSError: | |
1107 | pass |
|
1109 | pass | |
1108 | self._fp.close() |
|
1110 | self._fp.close() | |
1109 |
|
1111 | |||
1110 | def __del__(self): |
|
1112 | def __del__(self): | |
1111 | if safehasattr(self, '_fp'): # constructor actually did something |
|
1113 | if safehasattr(self, '_fp'): # constructor actually did something | |
1112 | self.discard() |
|
1114 | self.discard() | |
1113 |
|
1115 | |||
1114 | def makedirs(name, mode=None, notindexed=False): |
|
1116 | def makedirs(name, mode=None, notindexed=False): | |
1115 | """recursive directory creation with parent mode inheritance""" |
|
1117 | """recursive directory creation with parent mode inheritance""" | |
1116 | try: |
|
1118 | try: | |
1117 | makedir(name, notindexed) |
|
1119 | makedir(name, notindexed) | |
1118 | except OSError, err: |
|
1120 | except OSError, err: | |
1119 | if err.errno == errno.EEXIST: |
|
1121 | if err.errno == errno.EEXIST: | |
1120 | return |
|
1122 | return | |
1121 | if err.errno != errno.ENOENT or not name: |
|
1123 | if err.errno != errno.ENOENT or not name: | |
1122 | raise |
|
1124 | raise | |
1123 | parent = os.path.dirname(os.path.abspath(name)) |
|
1125 | parent = os.path.dirname(os.path.abspath(name)) | |
1124 | if parent == name: |
|
1126 | if parent == name: | |
1125 | raise |
|
1127 | raise | |
1126 | makedirs(parent, mode, notindexed) |
|
1128 | makedirs(parent, mode, notindexed) | |
1127 | makedir(name, notindexed) |
|
1129 | makedir(name, notindexed) | |
1128 | if mode is not None: |
|
1130 | if mode is not None: | |
1129 | os.chmod(name, mode) |
|
1131 | os.chmod(name, mode) | |
1130 |
|
1132 | |||
1131 | def ensuredirs(name, mode=None, notindexed=False): |
|
1133 | def ensuredirs(name, mode=None, notindexed=False): | |
1132 | """race-safe recursive directory creation |
|
1134 | """race-safe recursive directory creation | |
1133 |
|
1135 | |||
1134 | Newly created directories are marked as "not to be indexed by |
|
1136 | Newly created directories are marked as "not to be indexed by | |
1135 | the content indexing service", if ``notindexed`` is specified |
|
1137 | the content indexing service", if ``notindexed`` is specified | |
1136 | for "write" mode access. |
|
1138 | for "write" mode access. | |
1137 | """ |
|
1139 | """ | |
1138 | if os.path.isdir(name): |
|
1140 | if os.path.isdir(name): | |
1139 | return |
|
1141 | return | |
1140 | parent = os.path.dirname(os.path.abspath(name)) |
|
1142 | parent = os.path.dirname(os.path.abspath(name)) | |
1141 | if parent != name: |
|
1143 | if parent != name: | |
1142 | ensuredirs(parent, mode, notindexed) |
|
1144 | ensuredirs(parent, mode, notindexed) | |
1143 | try: |
|
1145 | try: | |
1144 | makedir(name, notindexed) |
|
1146 | makedir(name, notindexed) | |
1145 | except OSError, err: |
|
1147 | except OSError, err: | |
1146 | if err.errno == errno.EEXIST and os.path.isdir(name): |
|
1148 | if err.errno == errno.EEXIST and os.path.isdir(name): | |
1147 | # someone else seems to have won a directory creation race |
|
1149 | # someone else seems to have won a directory creation race | |
1148 | return |
|
1150 | return | |
1149 | raise |
|
1151 | raise | |
1150 | if mode is not None: |
|
1152 | if mode is not None: | |
1151 | os.chmod(name, mode) |
|
1153 | os.chmod(name, mode) | |
1152 |
|
1154 | |||
1153 | def readfile(path): |
|
1155 | def readfile(path): | |
1154 | fp = open(path, 'rb') |
|
1156 | fp = open(path, 'rb') | |
1155 | try: |
|
1157 | try: | |
1156 | return fp.read() |
|
1158 | return fp.read() | |
1157 | finally: |
|
1159 | finally: | |
1158 | fp.close() |
|
1160 | fp.close() | |
1159 |
|
1161 | |||
1160 | def writefile(path, text): |
|
1162 | def writefile(path, text): | |
1161 | fp = open(path, 'wb') |
|
1163 | fp = open(path, 'wb') | |
1162 | try: |
|
1164 | try: | |
1163 | fp.write(text) |
|
1165 | fp.write(text) | |
1164 | finally: |
|
1166 | finally: | |
1165 | fp.close() |
|
1167 | fp.close() | |
1166 |
|
1168 | |||
1167 | def appendfile(path, text): |
|
1169 | def appendfile(path, text): | |
1168 | fp = open(path, 'ab') |
|
1170 | fp = open(path, 'ab') | |
1169 | try: |
|
1171 | try: | |
1170 | fp.write(text) |
|
1172 | fp.write(text) | |
1171 | finally: |
|
1173 | finally: | |
1172 | fp.close() |
|
1174 | fp.close() | |
1173 |
|
1175 | |||
1174 | class chunkbuffer(object): |
|
1176 | class chunkbuffer(object): | |
1175 | """Allow arbitrary sized chunks of data to be efficiently read from an |
|
1177 | """Allow arbitrary sized chunks of data to be efficiently read from an | |
1176 | iterator over chunks of arbitrary size.""" |
|
1178 | iterator over chunks of arbitrary size.""" | |
1177 |
|
1179 | |||
1178 | def __init__(self, in_iter): |
|
1180 | def __init__(self, in_iter): | |
1179 | """in_iter is the iterator that's iterating over the input chunks. |
|
1181 | """in_iter is the iterator that's iterating over the input chunks. | |
1180 | targetsize is how big a buffer to try to maintain.""" |
|
1182 | targetsize is how big a buffer to try to maintain.""" | |
1181 | def splitbig(chunks): |
|
1183 | def splitbig(chunks): | |
1182 | for chunk in chunks: |
|
1184 | for chunk in chunks: | |
1183 | if len(chunk) > 2**20: |
|
1185 | if len(chunk) > 2**20: | |
1184 | pos = 0 |
|
1186 | pos = 0 | |
1185 | while pos < len(chunk): |
|
1187 | while pos < len(chunk): | |
1186 | end = pos + 2 ** 18 |
|
1188 | end = pos + 2 ** 18 | |
1187 | yield chunk[pos:end] |
|
1189 | yield chunk[pos:end] | |
1188 | pos = end |
|
1190 | pos = end | |
1189 | else: |
|
1191 | else: | |
1190 | yield chunk |
|
1192 | yield chunk | |
1191 | self.iter = splitbig(in_iter) |
|
1193 | self.iter = splitbig(in_iter) | |
1192 | self._queue = deque() |
|
1194 | self._queue = deque() | |
1193 |
|
1195 | |||
1194 | def read(self, l=None): |
|
1196 | def read(self, l=None): | |
1195 | """Read L bytes of data from the iterator of chunks of data. |
|
1197 | """Read L bytes of data from the iterator of chunks of data. | |
1196 | Returns less than L bytes if the iterator runs dry. |
|
1198 | Returns less than L bytes if the iterator runs dry. | |
1197 |
|
1199 | |||
1198 | If size parameter is omitted, read everything""" |
|
1200 | If size parameter is omitted, read everything""" | |
1199 | left = l |
|
1201 | left = l | |
1200 | buf = [] |
|
1202 | buf = [] | |
1201 | queue = self._queue |
|
1203 | queue = self._queue | |
1202 | while left is None or left > 0: |
|
1204 | while left is None or left > 0: | |
1203 | # refill the queue |
|
1205 | # refill the queue | |
1204 | if not queue: |
|
1206 | if not queue: | |
1205 | target = 2**18 |
|
1207 | target = 2**18 | |
1206 | for chunk in self.iter: |
|
1208 | for chunk in self.iter: | |
1207 | queue.append(chunk) |
|
1209 | queue.append(chunk) | |
1208 | target -= len(chunk) |
|
1210 | target -= len(chunk) | |
1209 | if target <= 0: |
|
1211 | if target <= 0: | |
1210 | break |
|
1212 | break | |
1211 | if not queue: |
|
1213 | if not queue: | |
1212 | break |
|
1214 | break | |
1213 |
|
1215 | |||
1214 | chunk = queue.popleft() |
|
1216 | chunk = queue.popleft() | |
1215 | if left is not None: |
|
1217 | if left is not None: | |
1216 | left -= len(chunk) |
|
1218 | left -= len(chunk) | |
1217 | if left is not None and left < 0: |
|
1219 | if left is not None and left < 0: | |
1218 | queue.appendleft(chunk[left:]) |
|
1220 | queue.appendleft(chunk[left:]) | |
1219 | buf.append(chunk[:left]) |
|
1221 | buf.append(chunk[:left]) | |
1220 | else: |
|
1222 | else: | |
1221 | buf.append(chunk) |
|
1223 | buf.append(chunk) | |
1222 |
|
1224 | |||
1223 | return ''.join(buf) |
|
1225 | return ''.join(buf) | |
1224 |
|
1226 | |||
1225 | def filechunkiter(f, size=65536, limit=None): |
|
1227 | def filechunkiter(f, size=65536, limit=None): | |
1226 | """Create a generator that produces the data in the file size |
|
1228 | """Create a generator that produces the data in the file size | |
1227 | (default 65536) bytes at a time, up to optional limit (default is |
|
1229 | (default 65536) bytes at a time, up to optional limit (default is | |
1228 | to read all data). Chunks may be less than size bytes if the |
|
1230 | to read all data). Chunks may be less than size bytes if the | |
1229 | chunk is the last chunk in the file, or the file is a socket or |
|
1231 | chunk is the last chunk in the file, or the file is a socket or | |
1230 | some other type of file that sometimes reads less data than is |
|
1232 | some other type of file that sometimes reads less data than is | |
1231 | requested.""" |
|
1233 | requested.""" | |
1232 | assert size >= 0 |
|
1234 | assert size >= 0 | |
1233 | assert limit is None or limit >= 0 |
|
1235 | assert limit is None or limit >= 0 | |
1234 | while True: |
|
1236 | while True: | |
1235 | if limit is None: |
|
1237 | if limit is None: | |
1236 | nbytes = size |
|
1238 | nbytes = size | |
1237 | else: |
|
1239 | else: | |
1238 | nbytes = min(limit, size) |
|
1240 | nbytes = min(limit, size) | |
1239 | s = nbytes and f.read(nbytes) |
|
1241 | s = nbytes and f.read(nbytes) | |
1240 | if not s: |
|
1242 | if not s: | |
1241 | break |
|
1243 | break | |
1242 | if limit: |
|
1244 | if limit: | |
1243 | limit -= len(s) |
|
1245 | limit -= len(s) | |
1244 | yield s |
|
1246 | yield s | |
1245 |
|
1247 | |||
1246 | def makedate(timestamp=None): |
|
1248 | def makedate(timestamp=None): | |
1247 | '''Return a unix timestamp (or the current time) as a (unixtime, |
|
1249 | '''Return a unix timestamp (or the current time) as a (unixtime, | |
1248 | offset) tuple based off the local timezone.''' |
|
1250 | offset) tuple based off the local timezone.''' | |
1249 | if timestamp is None: |
|
1251 | if timestamp is None: | |
1250 | timestamp = time.time() |
|
1252 | timestamp = time.time() | |
1251 | if timestamp < 0: |
|
1253 | if timestamp < 0: | |
1252 | hint = _("check your clock") |
|
1254 | hint = _("check your clock") | |
1253 | raise Abort(_("negative timestamp: %d") % timestamp, hint=hint) |
|
1255 | raise Abort(_("negative timestamp: %d") % timestamp, hint=hint) | |
1254 | delta = (datetime.datetime.utcfromtimestamp(timestamp) - |
|
1256 | delta = (datetime.datetime.utcfromtimestamp(timestamp) - | |
1255 | datetime.datetime.fromtimestamp(timestamp)) |
|
1257 | datetime.datetime.fromtimestamp(timestamp)) | |
1256 | tz = delta.days * 86400 + delta.seconds |
|
1258 | tz = delta.days * 86400 + delta.seconds | |
1257 | return timestamp, tz |
|
1259 | return timestamp, tz | |
1258 |
|
1260 | |||
1259 | def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): |
|
1261 | def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): | |
1260 | """represent a (unixtime, offset) tuple as a localized time. |
|
1262 | """represent a (unixtime, offset) tuple as a localized time. | |
1261 | unixtime is seconds since the epoch, and offset is the time zone's |
|
1263 | unixtime is seconds since the epoch, and offset is the time zone's | |
1262 | number of seconds away from UTC. if timezone is false, do not |
|
1264 | number of seconds away from UTC. if timezone is false, do not | |
1263 | append time zone to string.""" |
|
1265 | append time zone to string.""" | |
1264 | t, tz = date or makedate() |
|
1266 | t, tz = date or makedate() | |
1265 | if t < 0: |
|
1267 | if t < 0: | |
1266 | t = 0 # time.gmtime(lt) fails on Windows for lt < -43200 |
|
1268 | t = 0 # time.gmtime(lt) fails on Windows for lt < -43200 | |
1267 | tz = 0 |
|
1269 | tz = 0 | |
1268 | if "%1" in format or "%2" in format or "%z" in format: |
|
1270 | if "%1" in format or "%2" in format or "%z" in format: | |
1269 | sign = (tz > 0) and "-" or "+" |
|
1271 | sign = (tz > 0) and "-" or "+" | |
1270 | minutes = abs(tz) // 60 |
|
1272 | minutes = abs(tz) // 60 | |
1271 | format = format.replace("%z", "%1%2") |
|
1273 | format = format.replace("%z", "%1%2") | |
1272 | format = format.replace("%1", "%c%02d" % (sign, minutes // 60)) |
|
1274 | format = format.replace("%1", "%c%02d" % (sign, minutes // 60)) | |
1273 | format = format.replace("%2", "%02d" % (minutes % 60)) |
|
1275 | format = format.replace("%2", "%02d" % (minutes % 60)) | |
1274 | try: |
|
1276 | try: | |
1275 | t = time.gmtime(float(t) - tz) |
|
1277 | t = time.gmtime(float(t) - tz) | |
1276 | except ValueError: |
|
1278 | except ValueError: | |
1277 | # time was out of range |
|
1279 | # time was out of range | |
1278 | t = time.gmtime(sys.maxint) |
|
1280 | t = time.gmtime(sys.maxint) | |
1279 | s = time.strftime(format, t) |
|
1281 | s = time.strftime(format, t) | |
1280 | return s |
|
1282 | return s | |
1281 |
|
1283 | |||
1282 | def shortdate(date=None): |
|
1284 | def shortdate(date=None): | |
1283 | """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
|
1285 | """turn (timestamp, tzoff) tuple into iso 8631 date.""" | |
1284 | return datestr(date, format='%Y-%m-%d') |
|
1286 | return datestr(date, format='%Y-%m-%d') | |
1285 |
|
1287 | |||
1286 | def strdate(string, format, defaults=[]): |
|
1288 | def strdate(string, format, defaults=[]): | |
1287 | """parse a localized time string and return a (unixtime, offset) tuple. |
|
1289 | """parse a localized time string and return a (unixtime, offset) tuple. | |
1288 | if the string cannot be parsed, ValueError is raised.""" |
|
1290 | if the string cannot be parsed, ValueError is raised.""" | |
1289 | def timezone(string): |
|
1291 | def timezone(string): | |
1290 | tz = string.split()[-1] |
|
1292 | tz = string.split()[-1] | |
1291 | if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): |
|
1293 | if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): | |
1292 | sign = (tz[0] == "+") and 1 or -1 |
|
1294 | sign = (tz[0] == "+") and 1 or -1 | |
1293 | hours = int(tz[1:3]) |
|
1295 | hours = int(tz[1:3]) | |
1294 | minutes = int(tz[3:5]) |
|
1296 | minutes = int(tz[3:5]) | |
1295 | return -sign * (hours * 60 + minutes) * 60 |
|
1297 | return -sign * (hours * 60 + minutes) * 60 | |
1296 | if tz == "GMT" or tz == "UTC": |
|
1298 | if tz == "GMT" or tz == "UTC": | |
1297 | return 0 |
|
1299 | return 0 | |
1298 | return None |
|
1300 | return None | |
1299 |
|
1301 | |||
1300 | # NOTE: unixtime = localunixtime + offset |
|
1302 | # NOTE: unixtime = localunixtime + offset | |
1301 | offset, date = timezone(string), string |
|
1303 | offset, date = timezone(string), string | |
1302 | if offset is not None: |
|
1304 | if offset is not None: | |
1303 | date = " ".join(string.split()[:-1]) |
|
1305 | date = " ".join(string.split()[:-1]) | |
1304 |
|
1306 | |||
1305 | # add missing elements from defaults |
|
1307 | # add missing elements from defaults | |
1306 | usenow = False # default to using biased defaults |
|
1308 | usenow = False # default to using biased defaults | |
1307 | for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity |
|
1309 | for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity | |
1308 | found = [True for p in part if ("%"+p) in format] |
|
1310 | found = [True for p in part if ("%"+p) in format] | |
1309 | if not found: |
|
1311 | if not found: | |
1310 | date += "@" + defaults[part][usenow] |
|
1312 | date += "@" + defaults[part][usenow] | |
1311 | format += "@%" + part[0] |
|
1313 | format += "@%" + part[0] | |
1312 | else: |
|
1314 | else: | |
1313 | # We've found a specific time element, less specific time |
|
1315 | # We've found a specific time element, less specific time | |
1314 | # elements are relative to today |
|
1316 | # elements are relative to today | |
1315 | usenow = True |
|
1317 | usenow = True | |
1316 |
|
1318 | |||
1317 | timetuple = time.strptime(date, format) |
|
1319 | timetuple = time.strptime(date, format) | |
1318 | localunixtime = int(calendar.timegm(timetuple)) |
|
1320 | localunixtime = int(calendar.timegm(timetuple)) | |
1319 | if offset is None: |
|
1321 | if offset is None: | |
1320 | # local timezone |
|
1322 | # local timezone | |
1321 | unixtime = int(time.mktime(timetuple)) |
|
1323 | unixtime = int(time.mktime(timetuple)) | |
1322 | offset = unixtime - localunixtime |
|
1324 | offset = unixtime - localunixtime | |
1323 | else: |
|
1325 | else: | |
1324 | unixtime = localunixtime + offset |
|
1326 | unixtime = localunixtime + offset | |
1325 | return unixtime, offset |
|
1327 | return unixtime, offset | |
1326 |
|
1328 | |||
1327 | def parsedate(date, formats=None, bias={}): |
|
1329 | def parsedate(date, formats=None, bias={}): | |
1328 | """parse a localized date/time and return a (unixtime, offset) tuple. |
|
1330 | """parse a localized date/time and return a (unixtime, offset) tuple. | |
1329 |
|
1331 | |||
1330 | The date may be a "unixtime offset" string or in one of the specified |
|
1332 | The date may be a "unixtime offset" string or in one of the specified | |
1331 | formats. If the date already is a (unixtime, offset) tuple, it is returned. |
|
1333 | formats. If the date already is a (unixtime, offset) tuple, it is returned. | |
1332 |
|
1334 | |||
1333 | >>> parsedate(' today ') == parsedate(\ |
|
1335 | >>> parsedate(' today ') == parsedate(\ | |
1334 | datetime.date.today().strftime('%b %d')) |
|
1336 | datetime.date.today().strftime('%b %d')) | |
1335 | True |
|
1337 | True | |
1336 | >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ |
|
1338 | >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ | |
1337 | datetime.timedelta(days=1)\ |
|
1339 | datetime.timedelta(days=1)\ | |
1338 | ).strftime('%b %d')) |
|
1340 | ).strftime('%b %d')) | |
1339 | True |
|
1341 | True | |
1340 | >>> now, tz = makedate() |
|
1342 | >>> now, tz = makedate() | |
1341 | >>> strnow, strtz = parsedate('now') |
|
1343 | >>> strnow, strtz = parsedate('now') | |
1342 | >>> (strnow - now) < 1 |
|
1344 | >>> (strnow - now) < 1 | |
1343 | True |
|
1345 | True | |
1344 | >>> tz == strtz |
|
1346 | >>> tz == strtz | |
1345 | True |
|
1347 | True | |
1346 | """ |
|
1348 | """ | |
1347 | if not date: |
|
1349 | if not date: | |
1348 | return 0, 0 |
|
1350 | return 0, 0 | |
1349 | if isinstance(date, tuple) and len(date) == 2: |
|
1351 | if isinstance(date, tuple) and len(date) == 2: | |
1350 | return date |
|
1352 | return date | |
1351 | if not formats: |
|
1353 | if not formats: | |
1352 | formats = defaultdateformats |
|
1354 | formats = defaultdateformats | |
1353 | date = date.strip() |
|
1355 | date = date.strip() | |
1354 |
|
1356 | |||
1355 | if date == 'now' or date == _('now'): |
|
1357 | if date == 'now' or date == _('now'): | |
1356 | return makedate() |
|
1358 | return makedate() | |
1357 | if date == 'today' or date == _('today'): |
|
1359 | if date == 'today' or date == _('today'): | |
1358 | date = datetime.date.today().strftime('%b %d') |
|
1360 | date = datetime.date.today().strftime('%b %d') | |
1359 | elif date == 'yesterday' or date == _('yesterday'): |
|
1361 | elif date == 'yesterday' or date == _('yesterday'): | |
1360 | date = (datetime.date.today() - |
|
1362 | date = (datetime.date.today() - | |
1361 | datetime.timedelta(days=1)).strftime('%b %d') |
|
1363 | datetime.timedelta(days=1)).strftime('%b %d') | |
1362 |
|
1364 | |||
1363 | try: |
|
1365 | try: | |
1364 | when, offset = map(int, date.split(' ')) |
|
1366 | when, offset = map(int, date.split(' ')) | |
1365 | except ValueError: |
|
1367 | except ValueError: | |
1366 | # fill out defaults |
|
1368 | # fill out defaults | |
1367 | now = makedate() |
|
1369 | now = makedate() | |
1368 | defaults = {} |
|
1370 | defaults = {} | |
1369 | for part in ("d", "mb", "yY", "HI", "M", "S"): |
|
1371 | for part in ("d", "mb", "yY", "HI", "M", "S"): | |
1370 | # this piece is for rounding the specific end of unknowns |
|
1372 | # this piece is for rounding the specific end of unknowns | |
1371 | b = bias.get(part) |
|
1373 | b = bias.get(part) | |
1372 | if b is None: |
|
1374 | if b is None: | |
1373 | if part[0] in "HMS": |
|
1375 | if part[0] in "HMS": | |
1374 | b = "00" |
|
1376 | b = "00" | |
1375 | else: |
|
1377 | else: | |
1376 | b = "0" |
|
1378 | b = "0" | |
1377 |
|
1379 | |||
1378 | # this piece is for matching the generic end to today's date |
|
1380 | # this piece is for matching the generic end to today's date | |
1379 | n = datestr(now, "%" + part[0]) |
|
1381 | n = datestr(now, "%" + part[0]) | |
1380 |
|
1382 | |||
1381 | defaults[part] = (b, n) |
|
1383 | defaults[part] = (b, n) | |
1382 |
|
1384 | |||
1383 | for format in formats: |
|
1385 | for format in formats: | |
1384 | try: |
|
1386 | try: | |
1385 | when, offset = strdate(date, format, defaults) |
|
1387 | when, offset = strdate(date, format, defaults) | |
1386 | except (ValueError, OverflowError): |
|
1388 | except (ValueError, OverflowError): | |
1387 | pass |
|
1389 | pass | |
1388 | else: |
|
1390 | else: | |
1389 | break |
|
1391 | break | |
1390 | else: |
|
1392 | else: | |
1391 | raise Abort(_('invalid date: %r') % date) |
|
1393 | raise Abort(_('invalid date: %r') % date) | |
1392 | # validate explicit (probably user-specified) date and |
|
1394 | # validate explicit (probably user-specified) date and | |
1393 | # time zone offset. values must fit in signed 32 bits for |
|
1395 | # time zone offset. values must fit in signed 32 bits for | |
1394 | # current 32-bit linux runtimes. timezones go from UTC-12 |
|
1396 | # current 32-bit linux runtimes. timezones go from UTC-12 | |
1395 | # to UTC+14 |
|
1397 | # to UTC+14 | |
1396 | if abs(when) > 0x7fffffff: |
|
1398 | if abs(when) > 0x7fffffff: | |
1397 | raise Abort(_('date exceeds 32 bits: %d') % when) |
|
1399 | raise Abort(_('date exceeds 32 bits: %d') % when) | |
1398 | if when < 0: |
|
1400 | if when < 0: | |
1399 | raise Abort(_('negative date value: %d') % when) |
|
1401 | raise Abort(_('negative date value: %d') % when) | |
1400 | if offset < -50400 or offset > 43200: |
|
1402 | if offset < -50400 or offset > 43200: | |
1401 | raise Abort(_('impossible time zone offset: %d') % offset) |
|
1403 | raise Abort(_('impossible time zone offset: %d') % offset) | |
1402 | return when, offset |
|
1404 | return when, offset | |
1403 |
|
1405 | |||
1404 | def matchdate(date): |
|
1406 | def matchdate(date): | |
1405 | """Return a function that matches a given date match specifier |
|
1407 | """Return a function that matches a given date match specifier | |
1406 |
|
1408 | |||
1407 | Formats include: |
|
1409 | Formats include: | |
1408 |
|
1410 | |||
1409 | '{date}' match a given date to the accuracy provided |
|
1411 | '{date}' match a given date to the accuracy provided | |
1410 |
|
1412 | |||
1411 | '<{date}' on or before a given date |
|
1413 | '<{date}' on or before a given date | |
1412 |
|
1414 | |||
1413 | '>{date}' on or after a given date |
|
1415 | '>{date}' on or after a given date | |
1414 |
|
1416 | |||
1415 | >>> p1 = parsedate("10:29:59") |
|
1417 | >>> p1 = parsedate("10:29:59") | |
1416 | >>> p2 = parsedate("10:30:00") |
|
1418 | >>> p2 = parsedate("10:30:00") | |
1417 | >>> p3 = parsedate("10:30:59") |
|
1419 | >>> p3 = parsedate("10:30:59") | |
1418 | >>> p4 = parsedate("10:31:00") |
|
1420 | >>> p4 = parsedate("10:31:00") | |
1419 | >>> p5 = parsedate("Sep 15 10:30:00 1999") |
|
1421 | >>> p5 = parsedate("Sep 15 10:30:00 1999") | |
1420 | >>> f = matchdate("10:30") |
|
1422 | >>> f = matchdate("10:30") | |
1421 | >>> f(p1[0]) |
|
1423 | >>> f(p1[0]) | |
1422 | False |
|
1424 | False | |
1423 | >>> f(p2[0]) |
|
1425 | >>> f(p2[0]) | |
1424 | True |
|
1426 | True | |
1425 | >>> f(p3[0]) |
|
1427 | >>> f(p3[0]) | |
1426 | True |
|
1428 | True | |
1427 | >>> f(p4[0]) |
|
1429 | >>> f(p4[0]) | |
1428 | False |
|
1430 | False | |
1429 | >>> f(p5[0]) |
|
1431 | >>> f(p5[0]) | |
1430 | False |
|
1432 | False | |
1431 | """ |
|
1433 | """ | |
1432 |
|
1434 | |||
1433 | def lower(date): |
|
1435 | def lower(date): | |
1434 | d = {'mb': "1", 'd': "1"} |
|
1436 | d = {'mb': "1", 'd': "1"} | |
1435 | return parsedate(date, extendeddateformats, d)[0] |
|
1437 | return parsedate(date, extendeddateformats, d)[0] | |
1436 |
|
1438 | |||
1437 | def upper(date): |
|
1439 | def upper(date): | |
1438 | d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"} |
|
1440 | d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"} | |
1439 | for days in ("31", "30", "29"): |
|
1441 | for days in ("31", "30", "29"): | |
1440 | try: |
|
1442 | try: | |
1441 | d["d"] = days |
|
1443 | d["d"] = days | |
1442 | return parsedate(date, extendeddateformats, d)[0] |
|
1444 | return parsedate(date, extendeddateformats, d)[0] | |
1443 | except Abort: |
|
1445 | except Abort: | |
1444 | pass |
|
1446 | pass | |
1445 | d["d"] = "28" |
|
1447 | d["d"] = "28" | |
1446 | return parsedate(date, extendeddateformats, d)[0] |
|
1448 | return parsedate(date, extendeddateformats, d)[0] | |
1447 |
|
1449 | |||
1448 | date = date.strip() |
|
1450 | date = date.strip() | |
1449 |
|
1451 | |||
1450 | if not date: |
|
1452 | if not date: | |
1451 | raise Abort(_("dates cannot consist entirely of whitespace")) |
|
1453 | raise Abort(_("dates cannot consist entirely of whitespace")) | |
1452 | elif date[0] == "<": |
|
1454 | elif date[0] == "<": | |
1453 | if not date[1:]: |
|
1455 | if not date[1:]: | |
1454 | raise Abort(_("invalid day spec, use '<DATE'")) |
|
1456 | raise Abort(_("invalid day spec, use '<DATE'")) | |
1455 | when = upper(date[1:]) |
|
1457 | when = upper(date[1:]) | |
1456 | return lambda x: x <= when |
|
1458 | return lambda x: x <= when | |
1457 | elif date[0] == ">": |
|
1459 | elif date[0] == ">": | |
1458 | if not date[1:]: |
|
1460 | if not date[1:]: | |
1459 | raise Abort(_("invalid day spec, use '>DATE'")) |
|
1461 | raise Abort(_("invalid day spec, use '>DATE'")) | |
1460 | when = lower(date[1:]) |
|
1462 | when = lower(date[1:]) | |
1461 | return lambda x: x >= when |
|
1463 | return lambda x: x >= when | |
1462 | elif date[0] == "-": |
|
1464 | elif date[0] == "-": | |
1463 | try: |
|
1465 | try: | |
1464 | days = int(date[1:]) |
|
1466 | days = int(date[1:]) | |
1465 | except ValueError: |
|
1467 | except ValueError: | |
1466 | raise Abort(_("invalid day spec: %s") % date[1:]) |
|
1468 | raise Abort(_("invalid day spec: %s") % date[1:]) | |
1467 | if days < 0: |
|
1469 | if days < 0: | |
1468 | raise Abort(_('%s must be nonnegative (see "hg help dates")') |
|
1470 | raise Abort(_('%s must be nonnegative (see "hg help dates")') | |
1469 | % date[1:]) |
|
1471 | % date[1:]) | |
1470 | when = makedate()[0] - days * 3600 * 24 |
|
1472 | when = makedate()[0] - days * 3600 * 24 | |
1471 | return lambda x: x >= when |
|
1473 | return lambda x: x >= when | |
1472 | elif " to " in date: |
|
1474 | elif " to " in date: | |
1473 | a, b = date.split(" to ") |
|
1475 | a, b = date.split(" to ") | |
1474 | start, stop = lower(a), upper(b) |
|
1476 | start, stop = lower(a), upper(b) | |
1475 | return lambda x: x >= start and x <= stop |
|
1477 | return lambda x: x >= start and x <= stop | |
1476 | else: |
|
1478 | else: | |
1477 | start, stop = lower(date), upper(date) |
|
1479 | start, stop = lower(date), upper(date) | |
1478 | return lambda x: x >= start and x <= stop |
|
1480 | return lambda x: x >= start and x <= stop | |
1479 |
|
1481 | |||
1480 | def shortuser(user): |
|
1482 | def shortuser(user): | |
1481 | """Return a short representation of a user name or email address.""" |
|
1483 | """Return a short representation of a user name or email address.""" | |
1482 | f = user.find('@') |
|
1484 | f = user.find('@') | |
1483 | if f >= 0: |
|
1485 | if f >= 0: | |
1484 | user = user[:f] |
|
1486 | user = user[:f] | |
1485 | f = user.find('<') |
|
1487 | f = user.find('<') | |
1486 | if f >= 0: |
|
1488 | if f >= 0: | |
1487 | user = user[f + 1:] |
|
1489 | user = user[f + 1:] | |
1488 | f = user.find(' ') |
|
1490 | f = user.find(' ') | |
1489 | if f >= 0: |
|
1491 | if f >= 0: | |
1490 | user = user[:f] |
|
1492 | user = user[:f] | |
1491 | f = user.find('.') |
|
1493 | f = user.find('.') | |
1492 | if f >= 0: |
|
1494 | if f >= 0: | |
1493 | user = user[:f] |
|
1495 | user = user[:f] | |
1494 | return user |
|
1496 | return user | |
1495 |
|
1497 | |||
1496 | def emailuser(user): |
|
1498 | def emailuser(user): | |
1497 | """Return the user portion of an email address.""" |
|
1499 | """Return the user portion of an email address.""" | |
1498 | f = user.find('@') |
|
1500 | f = user.find('@') | |
1499 | if f >= 0: |
|
1501 | if f >= 0: | |
1500 | user = user[:f] |
|
1502 | user = user[:f] | |
1501 | f = user.find('<') |
|
1503 | f = user.find('<') | |
1502 | if f >= 0: |
|
1504 | if f >= 0: | |
1503 | user = user[f + 1:] |
|
1505 | user = user[f + 1:] | |
1504 | return user |
|
1506 | return user | |
1505 |
|
1507 | |||
1506 | def email(author): |
|
1508 | def email(author): | |
1507 | '''get email of author.''' |
|
1509 | '''get email of author.''' | |
1508 | r = author.find('>') |
|
1510 | r = author.find('>') | |
1509 | if r == -1: |
|
1511 | if r == -1: | |
1510 | r = None |
|
1512 | r = None | |
1511 | return author[author.find('<') + 1:r] |
|
1513 | return author[author.find('<') + 1:r] | |
1512 |
|
1514 | |||
1513 | def ellipsis(text, maxlength=400): |
|
1515 | def ellipsis(text, maxlength=400): | |
1514 | """Trim string to at most maxlength (default: 400) columns in display.""" |
|
1516 | """Trim string to at most maxlength (default: 400) columns in display.""" | |
1515 | return encoding.trim(text, maxlength, ellipsis='...') |
|
1517 | return encoding.trim(text, maxlength, ellipsis='...') | |
1516 |
|
1518 | |||
1517 | def unitcountfn(*unittable): |
|
1519 | def unitcountfn(*unittable): | |
1518 | '''return a function that renders a readable count of some quantity''' |
|
1520 | '''return a function that renders a readable count of some quantity''' | |
1519 |
|
1521 | |||
1520 | def go(count): |
|
1522 | def go(count): | |
1521 | for multiplier, divisor, format in unittable: |
|
1523 | for multiplier, divisor, format in unittable: | |
1522 | if count >= divisor * multiplier: |
|
1524 | if count >= divisor * multiplier: | |
1523 | return format % (count / float(divisor)) |
|
1525 | return format % (count / float(divisor)) | |
1524 | return unittable[-1][2] % count |
|
1526 | return unittable[-1][2] % count | |
1525 |
|
1527 | |||
1526 | return go |
|
1528 | return go | |
1527 |
|
1529 | |||
1528 | bytecount = unitcountfn( |
|
1530 | bytecount = unitcountfn( | |
1529 | (100, 1 << 30, _('%.0f GB')), |
|
1531 | (100, 1 << 30, _('%.0f GB')), | |
1530 | (10, 1 << 30, _('%.1f GB')), |
|
1532 | (10, 1 << 30, _('%.1f GB')), | |
1531 | (1, 1 << 30, _('%.2f GB')), |
|
1533 | (1, 1 << 30, _('%.2f GB')), | |
1532 | (100, 1 << 20, _('%.0f MB')), |
|
1534 | (100, 1 << 20, _('%.0f MB')), | |
1533 | (10, 1 << 20, _('%.1f MB')), |
|
1535 | (10, 1 << 20, _('%.1f MB')), | |
1534 | (1, 1 << 20, _('%.2f MB')), |
|
1536 | (1, 1 << 20, _('%.2f MB')), | |
1535 | (100, 1 << 10, _('%.0f KB')), |
|
1537 | (100, 1 << 10, _('%.0f KB')), | |
1536 | (10, 1 << 10, _('%.1f KB')), |
|
1538 | (10, 1 << 10, _('%.1f KB')), | |
1537 | (1, 1 << 10, _('%.2f KB')), |
|
1539 | (1, 1 << 10, _('%.2f KB')), | |
1538 | (1, 1, _('%.0f bytes')), |
|
1540 | (1, 1, _('%.0f bytes')), | |
1539 | ) |
|
1541 | ) | |
1540 |
|
1542 | |||
1541 | def uirepr(s): |
|
1543 | def uirepr(s): | |
1542 | # Avoid double backslash in Windows path repr() |
|
1544 | # Avoid double backslash in Windows path repr() | |
1543 | return repr(s).replace('\\\\', '\\') |
|
1545 | return repr(s).replace('\\\\', '\\') | |
1544 |
|
1546 | |||
1545 | # delay import of textwrap |
|
1547 | # delay import of textwrap | |
1546 | def MBTextWrapper(**kwargs): |
|
1548 | def MBTextWrapper(**kwargs): | |
1547 | class tw(textwrap.TextWrapper): |
|
1549 | class tw(textwrap.TextWrapper): | |
1548 | """ |
|
1550 | """ | |
1549 | Extend TextWrapper for width-awareness. |
|
1551 | Extend TextWrapper for width-awareness. | |
1550 |
|
1552 | |||
1551 | Neither number of 'bytes' in any encoding nor 'characters' is |
|
1553 | Neither number of 'bytes' in any encoding nor 'characters' is | |
1552 | appropriate to calculate terminal columns for specified string. |
|
1554 | appropriate to calculate terminal columns for specified string. | |
1553 |
|
1555 | |||
1554 | Original TextWrapper implementation uses built-in 'len()' directly, |
|
1556 | Original TextWrapper implementation uses built-in 'len()' directly, | |
1555 | so overriding is needed to use width information of each characters. |
|
1557 | so overriding is needed to use width information of each characters. | |
1556 |
|
1558 | |||
1557 | In addition, characters classified into 'ambiguous' width are |
|
1559 | In addition, characters classified into 'ambiguous' width are | |
1558 | treated as wide in East Asian area, but as narrow in other. |
|
1560 | treated as wide in East Asian area, but as narrow in other. | |
1559 |
|
1561 | |||
1560 | This requires use decision to determine width of such characters. |
|
1562 | This requires use decision to determine width of such characters. | |
1561 | """ |
|
1563 | """ | |
1562 | def __init__(self, **kwargs): |
|
1564 | def __init__(self, **kwargs): | |
1563 | textwrap.TextWrapper.__init__(self, **kwargs) |
|
1565 | textwrap.TextWrapper.__init__(self, **kwargs) | |
1564 |
|
1566 | |||
1565 | # for compatibility between 2.4 and 2.6 |
|
1567 | # for compatibility between 2.4 and 2.6 | |
1566 | if getattr(self, 'drop_whitespace', None) is None: |
|
1568 | if getattr(self, 'drop_whitespace', None) is None: | |
1567 | self.drop_whitespace = kwargs.get('drop_whitespace', True) |
|
1569 | self.drop_whitespace = kwargs.get('drop_whitespace', True) | |
1568 |
|
1570 | |||
1569 | def _cutdown(self, ucstr, space_left): |
|
1571 | def _cutdown(self, ucstr, space_left): | |
1570 | l = 0 |
|
1572 | l = 0 | |
1571 | colwidth = encoding.ucolwidth |
|
1573 | colwidth = encoding.ucolwidth | |
1572 | for i in xrange(len(ucstr)): |
|
1574 | for i in xrange(len(ucstr)): | |
1573 | l += colwidth(ucstr[i]) |
|
1575 | l += colwidth(ucstr[i]) | |
1574 | if space_left < l: |
|
1576 | if space_left < l: | |
1575 | return (ucstr[:i], ucstr[i:]) |
|
1577 | return (ucstr[:i], ucstr[i:]) | |
1576 | return ucstr, '' |
|
1578 | return ucstr, '' | |
1577 |
|
1579 | |||
1578 | # overriding of base class |
|
1580 | # overriding of base class | |
1579 | def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
|
1581 | def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): | |
1580 | space_left = max(width - cur_len, 1) |
|
1582 | space_left = max(width - cur_len, 1) | |
1581 |
|
1583 | |||
1582 | if self.break_long_words: |
|
1584 | if self.break_long_words: | |
1583 | cut, res = self._cutdown(reversed_chunks[-1], space_left) |
|
1585 | cut, res = self._cutdown(reversed_chunks[-1], space_left) | |
1584 | cur_line.append(cut) |
|
1586 | cur_line.append(cut) | |
1585 | reversed_chunks[-1] = res |
|
1587 | reversed_chunks[-1] = res | |
1586 | elif not cur_line: |
|
1588 | elif not cur_line: | |
1587 | cur_line.append(reversed_chunks.pop()) |
|
1589 | cur_line.append(reversed_chunks.pop()) | |
1588 |
|
1590 | |||
1589 | # this overriding code is imported from TextWrapper of python 2.6 |
|
1591 | # this overriding code is imported from TextWrapper of python 2.6 | |
1590 | # to calculate columns of string by 'encoding.ucolwidth()' |
|
1592 | # to calculate columns of string by 'encoding.ucolwidth()' | |
1591 | def _wrap_chunks(self, chunks): |
|
1593 | def _wrap_chunks(self, chunks): | |
1592 | colwidth = encoding.ucolwidth |
|
1594 | colwidth = encoding.ucolwidth | |
1593 |
|
1595 | |||
1594 | lines = [] |
|
1596 | lines = [] | |
1595 | if self.width <= 0: |
|
1597 | if self.width <= 0: | |
1596 | raise ValueError("invalid width %r (must be > 0)" % self.width) |
|
1598 | raise ValueError("invalid width %r (must be > 0)" % self.width) | |
1597 |
|
1599 | |||
1598 | # Arrange in reverse order so items can be efficiently popped |
|
1600 | # Arrange in reverse order so items can be efficiently popped | |
1599 | # from a stack of chucks. |
|
1601 | # from a stack of chucks. | |
1600 | chunks.reverse() |
|
1602 | chunks.reverse() | |
1601 |
|
1603 | |||
1602 | while chunks: |
|
1604 | while chunks: | |
1603 |
|
1605 | |||
1604 | # Start the list of chunks that will make up the current line. |
|
1606 | # Start the list of chunks that will make up the current line. | |
1605 | # cur_len is just the length of all the chunks in cur_line. |
|
1607 | # cur_len is just the length of all the chunks in cur_line. | |
1606 | cur_line = [] |
|
1608 | cur_line = [] | |
1607 | cur_len = 0 |
|
1609 | cur_len = 0 | |
1608 |
|
1610 | |||
1609 | # Figure out which static string will prefix this line. |
|
1611 | # Figure out which static string will prefix this line. | |
1610 | if lines: |
|
1612 | if lines: | |
1611 | indent = self.subsequent_indent |
|
1613 | indent = self.subsequent_indent | |
1612 | else: |
|
1614 | else: | |
1613 | indent = self.initial_indent |
|
1615 | indent = self.initial_indent | |
1614 |
|
1616 | |||
1615 | # Maximum width for this line. |
|
1617 | # Maximum width for this line. | |
1616 | width = self.width - len(indent) |
|
1618 | width = self.width - len(indent) | |
1617 |
|
1619 | |||
1618 | # First chunk on line is whitespace -- drop it, unless this |
|
1620 | # First chunk on line is whitespace -- drop it, unless this | |
1619 | # is the very beginning of the text (i.e. no lines started yet). |
|
1621 | # is the very beginning of the text (i.e. no lines started yet). | |
1620 | if self.drop_whitespace and chunks[-1].strip() == '' and lines: |
|
1622 | if self.drop_whitespace and chunks[-1].strip() == '' and lines: | |
1621 | del chunks[-1] |
|
1623 | del chunks[-1] | |
1622 |
|
1624 | |||
1623 | while chunks: |
|
1625 | while chunks: | |
1624 | l = colwidth(chunks[-1]) |
|
1626 | l = colwidth(chunks[-1]) | |
1625 |
|
1627 | |||
1626 | # Can at least squeeze this chunk onto the current line. |
|
1628 | # Can at least squeeze this chunk onto the current line. | |
1627 | if cur_len + l <= width: |
|
1629 | if cur_len + l <= width: | |
1628 | cur_line.append(chunks.pop()) |
|
1630 | cur_line.append(chunks.pop()) | |
1629 | cur_len += l |
|
1631 | cur_len += l | |
1630 |
|
1632 | |||
1631 | # Nope, this line is full. |
|
1633 | # Nope, this line is full. | |
1632 | else: |
|
1634 | else: | |
1633 | break |
|
1635 | break | |
1634 |
|
1636 | |||
1635 | # The current line is full, and the next chunk is too big to |
|
1637 | # The current line is full, and the next chunk is too big to | |
1636 | # fit on *any* line (not just this one). |
|
1638 | # fit on *any* line (not just this one). | |
1637 | if chunks and colwidth(chunks[-1]) > width: |
|
1639 | if chunks and colwidth(chunks[-1]) > width: | |
1638 | self._handle_long_word(chunks, cur_line, cur_len, width) |
|
1640 | self._handle_long_word(chunks, cur_line, cur_len, width) | |
1639 |
|
1641 | |||
1640 | # If the last chunk on this line is all whitespace, drop it. |
|
1642 | # If the last chunk on this line is all whitespace, drop it. | |
1641 | if (self.drop_whitespace and |
|
1643 | if (self.drop_whitespace and | |
1642 | cur_line and cur_line[-1].strip() == ''): |
|
1644 | cur_line and cur_line[-1].strip() == ''): | |
1643 | del cur_line[-1] |
|
1645 | del cur_line[-1] | |
1644 |
|
1646 | |||
1645 | # Convert current line back to a string and store it in list |
|
1647 | # Convert current line back to a string and store it in list | |
1646 | # of all lines (return value). |
|
1648 | # of all lines (return value). | |
1647 | if cur_line: |
|
1649 | if cur_line: | |
1648 | lines.append(indent + ''.join(cur_line)) |
|
1650 | lines.append(indent + ''.join(cur_line)) | |
1649 |
|
1651 | |||
1650 | return lines |
|
1652 | return lines | |
1651 |
|
1653 | |||
1652 | global MBTextWrapper |
|
1654 | global MBTextWrapper | |
1653 | MBTextWrapper = tw |
|
1655 | MBTextWrapper = tw | |
1654 | return tw(**kwargs) |
|
1656 | return tw(**kwargs) | |
1655 |
|
1657 | |||
1656 | def wrap(line, width, initindent='', hangindent=''): |
|
1658 | def wrap(line, width, initindent='', hangindent=''): | |
1657 | maxindent = max(len(hangindent), len(initindent)) |
|
1659 | maxindent = max(len(hangindent), len(initindent)) | |
1658 | if width <= maxindent: |
|
1660 | if width <= maxindent: | |
1659 | # adjust for weird terminal size |
|
1661 | # adjust for weird terminal size | |
1660 | width = max(78, maxindent + 1) |
|
1662 | width = max(78, maxindent + 1) | |
1661 | line = line.decode(encoding.encoding, encoding.encodingmode) |
|
1663 | line = line.decode(encoding.encoding, encoding.encodingmode) | |
1662 | initindent = initindent.decode(encoding.encoding, encoding.encodingmode) |
|
1664 | initindent = initindent.decode(encoding.encoding, encoding.encodingmode) | |
1663 | hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode) |
|
1665 | hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode) | |
1664 | wrapper = MBTextWrapper(width=width, |
|
1666 | wrapper = MBTextWrapper(width=width, | |
1665 | initial_indent=initindent, |
|
1667 | initial_indent=initindent, | |
1666 | subsequent_indent=hangindent) |
|
1668 | subsequent_indent=hangindent) | |
1667 | return wrapper.fill(line).encode(encoding.encoding) |
|
1669 | return wrapper.fill(line).encode(encoding.encoding) | |
1668 |
|
1670 | |||
1669 | def iterlines(iterator): |
|
1671 | def iterlines(iterator): | |
1670 | for chunk in iterator: |
|
1672 | for chunk in iterator: | |
1671 | for line in chunk.splitlines(): |
|
1673 | for line in chunk.splitlines(): | |
1672 | yield line |
|
1674 | yield line | |
1673 |
|
1675 | |||
1674 | def expandpath(path): |
|
1676 | def expandpath(path): | |
1675 | return os.path.expanduser(os.path.expandvars(path)) |
|
1677 | return os.path.expanduser(os.path.expandvars(path)) | |
1676 |
|
1678 | |||
1677 | def hgcmd(): |
|
1679 | def hgcmd(): | |
1678 | """Return the command used to execute current hg |
|
1680 | """Return the command used to execute current hg | |
1679 |
|
1681 | |||
1680 | This is different from hgexecutable() because on Windows we want |
|
1682 | This is different from hgexecutable() because on Windows we want | |
1681 | to avoid things opening new shell windows like batch files, so we |
|
1683 | to avoid things opening new shell windows like batch files, so we | |
1682 | get either the python call or current executable. |
|
1684 | get either the python call or current executable. | |
1683 | """ |
|
1685 | """ | |
1684 | if mainfrozen(): |
|
1686 | if mainfrozen(): | |
1685 | return [sys.executable] |
|
1687 | return [sys.executable] | |
1686 | return gethgcmd() |
|
1688 | return gethgcmd() | |
1687 |
|
1689 | |||
1688 | def rundetached(args, condfn): |
|
1690 | def rundetached(args, condfn): | |
1689 | """Execute the argument list in a detached process. |
|
1691 | """Execute the argument list in a detached process. | |
1690 |
|
1692 | |||
1691 | condfn is a callable which is called repeatedly and should return |
|
1693 | condfn is a callable which is called repeatedly and should return | |
1692 | True once the child process is known to have started successfully. |
|
1694 | True once the child process is known to have started successfully. | |
1693 | At this point, the child process PID is returned. If the child |
|
1695 | At this point, the child process PID is returned. If the child | |
1694 | process fails to start or finishes before condfn() evaluates to |
|
1696 | process fails to start or finishes before condfn() evaluates to | |
1695 | True, return -1. |
|
1697 | True, return -1. | |
1696 | """ |
|
1698 | """ | |
1697 | # Windows case is easier because the child process is either |
|
1699 | # Windows case is easier because the child process is either | |
1698 | # successfully starting and validating the condition or exiting |
|
1700 | # successfully starting and validating the condition or exiting | |
1699 | # on failure. We just poll on its PID. On Unix, if the child |
|
1701 | # on failure. We just poll on its PID. On Unix, if the child | |
1700 | # process fails to start, it will be left in a zombie state until |
|
1702 | # process fails to start, it will be left in a zombie state until | |
1701 | # the parent wait on it, which we cannot do since we expect a long |
|
1703 | # the parent wait on it, which we cannot do since we expect a long | |
1702 | # running process on success. Instead we listen for SIGCHLD telling |
|
1704 | # running process on success. Instead we listen for SIGCHLD telling | |
1703 | # us our child process terminated. |
|
1705 | # us our child process terminated. | |
1704 | terminated = set() |
|
1706 | terminated = set() | |
1705 | def handler(signum, frame): |
|
1707 | def handler(signum, frame): | |
1706 | terminated.add(os.wait()) |
|
1708 | terminated.add(os.wait()) | |
1707 | prevhandler = None |
|
1709 | prevhandler = None | |
1708 | SIGCHLD = getattr(signal, 'SIGCHLD', None) |
|
1710 | SIGCHLD = getattr(signal, 'SIGCHLD', None) | |
1709 | if SIGCHLD is not None: |
|
1711 | if SIGCHLD is not None: | |
1710 | prevhandler = signal.signal(SIGCHLD, handler) |
|
1712 | prevhandler = signal.signal(SIGCHLD, handler) | |
1711 | try: |
|
1713 | try: | |
1712 | pid = spawndetached(args) |
|
1714 | pid = spawndetached(args) | |
1713 | while not condfn(): |
|
1715 | while not condfn(): | |
1714 | if ((pid in terminated or not testpid(pid)) |
|
1716 | if ((pid in terminated or not testpid(pid)) | |
1715 | and not condfn()): |
|
1717 | and not condfn()): | |
1716 | return -1 |
|
1718 | return -1 | |
1717 | time.sleep(0.1) |
|
1719 | time.sleep(0.1) | |
1718 | return pid |
|
1720 | return pid | |
1719 | finally: |
|
1721 | finally: | |
1720 | if prevhandler is not None: |
|
1722 | if prevhandler is not None: | |
1721 | signal.signal(signal.SIGCHLD, prevhandler) |
|
1723 | signal.signal(signal.SIGCHLD, prevhandler) | |
1722 |
|
1724 | |||
1723 | try: |
|
1725 | try: | |
1724 | any, all = any, all |
|
1726 | any, all = any, all | |
1725 | except NameError: |
|
1727 | except NameError: | |
1726 | def any(iterable): |
|
1728 | def any(iterable): | |
1727 | for i in iterable: |
|
1729 | for i in iterable: | |
1728 | if i: |
|
1730 | if i: | |
1729 | return True |
|
1731 | return True | |
1730 | return False |
|
1732 | return False | |
1731 |
|
1733 | |||
1732 | def all(iterable): |
|
1734 | def all(iterable): | |
1733 | for i in iterable: |
|
1735 | for i in iterable: | |
1734 | if not i: |
|
1736 | if not i: | |
1735 | return False |
|
1737 | return False | |
1736 | return True |
|
1738 | return True | |
1737 |
|
1739 | |||
1738 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
|
1740 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): | |
1739 | """Return the result of interpolating items in the mapping into string s. |
|
1741 | """Return the result of interpolating items in the mapping into string s. | |
1740 |
|
1742 | |||
1741 | prefix is a single character string, or a two character string with |
|
1743 | prefix is a single character string, or a two character string with | |
1742 | a backslash as the first character if the prefix needs to be escaped in |
|
1744 | a backslash as the first character if the prefix needs to be escaped in | |
1743 | a regular expression. |
|
1745 | a regular expression. | |
1744 |
|
1746 | |||
1745 | fn is an optional function that will be applied to the replacement text |
|
1747 | fn is an optional function that will be applied to the replacement text | |
1746 | just before replacement. |
|
1748 | just before replacement. | |
1747 |
|
1749 | |||
1748 | escape_prefix is an optional flag that allows using doubled prefix for |
|
1750 | escape_prefix is an optional flag that allows using doubled prefix for | |
1749 | its escaping. |
|
1751 | its escaping. | |
1750 | """ |
|
1752 | """ | |
1751 | fn = fn or (lambda s: s) |
|
1753 | fn = fn or (lambda s: s) | |
1752 | patterns = '|'.join(mapping.keys()) |
|
1754 | patterns = '|'.join(mapping.keys()) | |
1753 | if escape_prefix: |
|
1755 | if escape_prefix: | |
1754 | patterns += '|' + prefix |
|
1756 | patterns += '|' + prefix | |
1755 | if len(prefix) > 1: |
|
1757 | if len(prefix) > 1: | |
1756 | prefix_char = prefix[1:] |
|
1758 | prefix_char = prefix[1:] | |
1757 | else: |
|
1759 | else: | |
1758 | prefix_char = prefix |
|
1760 | prefix_char = prefix | |
1759 | mapping[prefix_char] = prefix_char |
|
1761 | mapping[prefix_char] = prefix_char | |
1760 | r = remod.compile(r'%s(%s)' % (prefix, patterns)) |
|
1762 | r = remod.compile(r'%s(%s)' % (prefix, patterns)) | |
1761 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
|
1763 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) | |
1762 |
|
1764 | |||
1763 | def getport(port): |
|
1765 | def getport(port): | |
1764 | """Return the port for a given network service. |
|
1766 | """Return the port for a given network service. | |
1765 |
|
1767 | |||
1766 | If port is an integer, it's returned as is. If it's a string, it's |
|
1768 | If port is an integer, it's returned as is. If it's a string, it's | |
1767 | looked up using socket.getservbyname(). If there's no matching |
|
1769 | looked up using socket.getservbyname(). If there's no matching | |
1768 | service, util.Abort is raised. |
|
1770 | service, util.Abort is raised. | |
1769 | """ |
|
1771 | """ | |
1770 | try: |
|
1772 | try: | |
1771 | return int(port) |
|
1773 | return int(port) | |
1772 | except ValueError: |
|
1774 | except ValueError: | |
1773 | pass |
|
1775 | pass | |
1774 |
|
1776 | |||
1775 | try: |
|
1777 | try: | |
1776 | return socket.getservbyname(port) |
|
1778 | return socket.getservbyname(port) | |
1777 | except socket.error: |
|
1779 | except socket.error: | |
1778 | raise Abort(_("no port number associated with service '%s'") % port) |
|
1780 | raise Abort(_("no port number associated with service '%s'") % port) | |
1779 |
|
1781 | |||
1780 | _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True, |
|
1782 | _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True, | |
1781 | '0': False, 'no': False, 'false': False, 'off': False, |
|
1783 | '0': False, 'no': False, 'false': False, 'off': False, | |
1782 | 'never': False} |
|
1784 | 'never': False} | |
1783 |
|
1785 | |||
1784 | def parsebool(s): |
|
1786 | def parsebool(s): | |
1785 | """Parse s into a boolean. |
|
1787 | """Parse s into a boolean. | |
1786 |
|
1788 | |||
1787 | If s is not a valid boolean, returns None. |
|
1789 | If s is not a valid boolean, returns None. | |
1788 | """ |
|
1790 | """ | |
1789 | return _booleans.get(s.lower(), None) |
|
1791 | return _booleans.get(s.lower(), None) | |
1790 |
|
1792 | |||
1791 | _hexdig = '0123456789ABCDEFabcdef' |
|
1793 | _hexdig = '0123456789ABCDEFabcdef' | |
1792 | _hextochr = dict((a + b, chr(int(a + b, 16))) |
|
1794 | _hextochr = dict((a + b, chr(int(a + b, 16))) | |
1793 | for a in _hexdig for b in _hexdig) |
|
1795 | for a in _hexdig for b in _hexdig) | |
1794 |
|
1796 | |||
1795 | def _urlunquote(s): |
|
1797 | def _urlunquote(s): | |
1796 | """Decode HTTP/HTML % encoding. |
|
1798 | """Decode HTTP/HTML % encoding. | |
1797 |
|
1799 | |||
1798 | >>> _urlunquote('abc%20def') |
|
1800 | >>> _urlunquote('abc%20def') | |
1799 | 'abc def' |
|
1801 | 'abc def' | |
1800 | """ |
|
1802 | """ | |
1801 | res = s.split('%') |
|
1803 | res = s.split('%') | |
1802 | # fastpath |
|
1804 | # fastpath | |
1803 | if len(res) == 1: |
|
1805 | if len(res) == 1: | |
1804 | return s |
|
1806 | return s | |
1805 | s = res[0] |
|
1807 | s = res[0] | |
1806 | for item in res[1:]: |
|
1808 | for item in res[1:]: | |
1807 | try: |
|
1809 | try: | |
1808 | s += _hextochr[item[:2]] + item[2:] |
|
1810 | s += _hextochr[item[:2]] + item[2:] | |
1809 | except KeyError: |
|
1811 | except KeyError: | |
1810 | s += '%' + item |
|
1812 | s += '%' + item | |
1811 | except UnicodeDecodeError: |
|
1813 | except UnicodeDecodeError: | |
1812 | s += unichr(int(item[:2], 16)) + item[2:] |
|
1814 | s += unichr(int(item[:2], 16)) + item[2:] | |
1813 | return s |
|
1815 | return s | |
1814 |
|
1816 | |||
1815 | class url(object): |
|
1817 | class url(object): | |
1816 | r"""Reliable URL parser. |
|
1818 | r"""Reliable URL parser. | |
1817 |
|
1819 | |||
1818 | This parses URLs and provides attributes for the following |
|
1820 | This parses URLs and provides attributes for the following | |
1819 | components: |
|
1821 | components: | |
1820 |
|
1822 | |||
1821 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
|
1823 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> | |
1822 |
|
1824 | |||
1823 | Missing components are set to None. The only exception is |
|
1825 | Missing components are set to None. The only exception is | |
1824 | fragment, which is set to '' if present but empty. |
|
1826 | fragment, which is set to '' if present but empty. | |
1825 |
|
1827 | |||
1826 | If parsefragment is False, fragment is included in query. If |
|
1828 | If parsefragment is False, fragment is included in query. If | |
1827 | parsequery is False, query is included in path. If both are |
|
1829 | parsequery is False, query is included in path. If both are | |
1828 | False, both fragment and query are included in path. |
|
1830 | False, both fragment and query are included in path. | |
1829 |
|
1831 | |||
1830 | See http://www.ietf.org/rfc/rfc2396.txt for more information. |
|
1832 | See http://www.ietf.org/rfc/rfc2396.txt for more information. | |
1831 |
|
1833 | |||
1832 | Note that for backward compatibility reasons, bundle URLs do not |
|
1834 | Note that for backward compatibility reasons, bundle URLs do not | |
1833 | take host names. That means 'bundle://../' has a path of '../'. |
|
1835 | take host names. That means 'bundle://../' has a path of '../'. | |
1834 |
|
1836 | |||
1835 | Examples: |
|
1837 | Examples: | |
1836 |
|
1838 | |||
1837 | >>> url('http://www.ietf.org/rfc/rfc2396.txt') |
|
1839 | >>> url('http://www.ietf.org/rfc/rfc2396.txt') | |
1838 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
|
1840 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> | |
1839 | >>> url('ssh://[::1]:2200//home/joe/repo') |
|
1841 | >>> url('ssh://[::1]:2200//home/joe/repo') | |
1840 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
|
1842 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> | |
1841 | >>> url('file:///home/joe/repo') |
|
1843 | >>> url('file:///home/joe/repo') | |
1842 | <url scheme: 'file', path: '/home/joe/repo'> |
|
1844 | <url scheme: 'file', path: '/home/joe/repo'> | |
1843 | >>> url('file:///c:/temp/foo/') |
|
1845 | >>> url('file:///c:/temp/foo/') | |
1844 | <url scheme: 'file', path: 'c:/temp/foo/'> |
|
1846 | <url scheme: 'file', path: 'c:/temp/foo/'> | |
1845 | >>> url('bundle:foo') |
|
1847 | >>> url('bundle:foo') | |
1846 | <url scheme: 'bundle', path: 'foo'> |
|
1848 | <url scheme: 'bundle', path: 'foo'> | |
1847 | >>> url('bundle://../foo') |
|
1849 | >>> url('bundle://../foo') | |
1848 | <url scheme: 'bundle', path: '../foo'> |
|
1850 | <url scheme: 'bundle', path: '../foo'> | |
1849 | >>> url(r'c:\foo\bar') |
|
1851 | >>> url(r'c:\foo\bar') | |
1850 | <url path: 'c:\\foo\\bar'> |
|
1852 | <url path: 'c:\\foo\\bar'> | |
1851 | >>> url(r'\\blah\blah\blah') |
|
1853 | >>> url(r'\\blah\blah\blah') | |
1852 | <url path: '\\\\blah\\blah\\blah'> |
|
1854 | <url path: '\\\\blah\\blah\\blah'> | |
1853 | >>> url(r'\\blah\blah\blah#baz') |
|
1855 | >>> url(r'\\blah\blah\blah#baz') | |
1854 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
|
1856 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> | |
1855 | >>> url(r'file:///C:\users\me') |
|
1857 | >>> url(r'file:///C:\users\me') | |
1856 | <url scheme: 'file', path: 'C:\\users\\me'> |
|
1858 | <url scheme: 'file', path: 'C:\\users\\me'> | |
1857 |
|
1859 | |||
1858 | Authentication credentials: |
|
1860 | Authentication credentials: | |
1859 |
|
1861 | |||
1860 | >>> url('ssh://joe:xyz@x/repo') |
|
1862 | >>> url('ssh://joe:xyz@x/repo') | |
1861 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
|
1863 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> | |
1862 | >>> url('ssh://joe@x/repo') |
|
1864 | >>> url('ssh://joe@x/repo') | |
1863 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
|
1865 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> | |
1864 |
|
1866 | |||
1865 | Query strings and fragments: |
|
1867 | Query strings and fragments: | |
1866 |
|
1868 | |||
1867 | >>> url('http://host/a?b#c') |
|
1869 | >>> url('http://host/a?b#c') | |
1868 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
1870 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
1869 | >>> url('http://host/a?b#c', parsequery=False, parsefragment=False) |
|
1871 | >>> url('http://host/a?b#c', parsequery=False, parsefragment=False) | |
1870 | <url scheme: 'http', host: 'host', path: 'a?b#c'> |
|
1872 | <url scheme: 'http', host: 'host', path: 'a?b#c'> | |
1871 | """ |
|
1873 | """ | |
1872 |
|
1874 | |||
1873 | _safechars = "!~*'()+" |
|
1875 | _safechars = "!~*'()+" | |
1874 | _safepchars = "/!~*'()+:\\" |
|
1876 | _safepchars = "/!~*'()+:\\" | |
1875 | _matchscheme = remod.compile(r'^[a-zA-Z0-9+.\-]+:').match |
|
1877 | _matchscheme = remod.compile(r'^[a-zA-Z0-9+.\-]+:').match | |
1876 |
|
1878 | |||
1877 | def __init__(self, path, parsequery=True, parsefragment=True): |
|
1879 | def __init__(self, path, parsequery=True, parsefragment=True): | |
1878 | # We slowly chomp away at path until we have only the path left |
|
1880 | # We slowly chomp away at path until we have only the path left | |
1879 | self.scheme = self.user = self.passwd = self.host = None |
|
1881 | self.scheme = self.user = self.passwd = self.host = None | |
1880 | self.port = self.path = self.query = self.fragment = None |
|
1882 | self.port = self.path = self.query = self.fragment = None | |
1881 | self._localpath = True |
|
1883 | self._localpath = True | |
1882 | self._hostport = '' |
|
1884 | self._hostport = '' | |
1883 | self._origpath = path |
|
1885 | self._origpath = path | |
1884 |
|
1886 | |||
1885 | if parsefragment and '#' in path: |
|
1887 | if parsefragment and '#' in path: | |
1886 | path, self.fragment = path.split('#', 1) |
|
1888 | path, self.fragment = path.split('#', 1) | |
1887 | if not path: |
|
1889 | if not path: | |
1888 | path = None |
|
1890 | path = None | |
1889 |
|
1891 | |||
1890 | # special case for Windows drive letters and UNC paths |
|
1892 | # special case for Windows drive letters and UNC paths | |
1891 | if hasdriveletter(path) or path.startswith(r'\\'): |
|
1893 | if hasdriveletter(path) or path.startswith(r'\\'): | |
1892 | self.path = path |
|
1894 | self.path = path | |
1893 | return |
|
1895 | return | |
1894 |
|
1896 | |||
1895 | # For compatibility reasons, we can't handle bundle paths as |
|
1897 | # For compatibility reasons, we can't handle bundle paths as | |
1896 | # normal URLS |
|
1898 | # normal URLS | |
1897 | if path.startswith('bundle:'): |
|
1899 | if path.startswith('bundle:'): | |
1898 | self.scheme = 'bundle' |
|
1900 | self.scheme = 'bundle' | |
1899 | path = path[7:] |
|
1901 | path = path[7:] | |
1900 | if path.startswith('//'): |
|
1902 | if path.startswith('//'): | |
1901 | path = path[2:] |
|
1903 | path = path[2:] | |
1902 | self.path = path |
|
1904 | self.path = path | |
1903 | return |
|
1905 | return | |
1904 |
|
1906 | |||
1905 | if self._matchscheme(path): |
|
1907 | if self._matchscheme(path): | |
1906 | parts = path.split(':', 1) |
|
1908 | parts = path.split(':', 1) | |
1907 | if parts[0]: |
|
1909 | if parts[0]: | |
1908 | self.scheme, path = parts |
|
1910 | self.scheme, path = parts | |
1909 | self._localpath = False |
|
1911 | self._localpath = False | |
1910 |
|
1912 | |||
1911 | if not path: |
|
1913 | if not path: | |
1912 | path = None |
|
1914 | path = None | |
1913 | if self._localpath: |
|
1915 | if self._localpath: | |
1914 | self.path = '' |
|
1916 | self.path = '' | |
1915 | return |
|
1917 | return | |
1916 | else: |
|
1918 | else: | |
1917 | if self._localpath: |
|
1919 | if self._localpath: | |
1918 | self.path = path |
|
1920 | self.path = path | |
1919 | return |
|
1921 | return | |
1920 |
|
1922 | |||
1921 | if parsequery and '?' in path: |
|
1923 | if parsequery and '?' in path: | |
1922 | path, self.query = path.split('?', 1) |
|
1924 | path, self.query = path.split('?', 1) | |
1923 | if not path: |
|
1925 | if not path: | |
1924 | path = None |
|
1926 | path = None | |
1925 | if not self.query: |
|
1927 | if not self.query: | |
1926 | self.query = None |
|
1928 | self.query = None | |
1927 |
|
1929 | |||
1928 | # // is required to specify a host/authority |
|
1930 | # // is required to specify a host/authority | |
1929 | if path and path.startswith('//'): |
|
1931 | if path and path.startswith('//'): | |
1930 | parts = path[2:].split('/', 1) |
|
1932 | parts = path[2:].split('/', 1) | |
1931 | if len(parts) > 1: |
|
1933 | if len(parts) > 1: | |
1932 | self.host, path = parts |
|
1934 | self.host, path = parts | |
1933 | else: |
|
1935 | else: | |
1934 | self.host = parts[0] |
|
1936 | self.host = parts[0] | |
1935 | path = None |
|
1937 | path = None | |
1936 | if not self.host: |
|
1938 | if not self.host: | |
1937 | self.host = None |
|
1939 | self.host = None | |
1938 | # path of file:///d is /d |
|
1940 | # path of file:///d is /d | |
1939 | # path of file:///d:/ is d:/, not /d:/ |
|
1941 | # path of file:///d:/ is d:/, not /d:/ | |
1940 | if path and not hasdriveletter(path): |
|
1942 | if path and not hasdriveletter(path): | |
1941 | path = '/' + path |
|
1943 | path = '/' + path | |
1942 |
|
1944 | |||
1943 | if self.host and '@' in self.host: |
|
1945 | if self.host and '@' in self.host: | |
1944 | self.user, self.host = self.host.rsplit('@', 1) |
|
1946 | self.user, self.host = self.host.rsplit('@', 1) | |
1945 | if ':' in self.user: |
|
1947 | if ':' in self.user: | |
1946 | self.user, self.passwd = self.user.split(':', 1) |
|
1948 | self.user, self.passwd = self.user.split(':', 1) | |
1947 | if not self.host: |
|
1949 | if not self.host: | |
1948 | self.host = None |
|
1950 | self.host = None | |
1949 |
|
1951 | |||
1950 | # Don't split on colons in IPv6 addresses without ports |
|
1952 | # Don't split on colons in IPv6 addresses without ports | |
1951 | if (self.host and ':' in self.host and |
|
1953 | if (self.host and ':' in self.host and | |
1952 | not (self.host.startswith('[') and self.host.endswith(']'))): |
|
1954 | not (self.host.startswith('[') and self.host.endswith(']'))): | |
1953 | self._hostport = self.host |
|
1955 | self._hostport = self.host | |
1954 | self.host, self.port = self.host.rsplit(':', 1) |
|
1956 | self.host, self.port = self.host.rsplit(':', 1) | |
1955 | if not self.host: |
|
1957 | if not self.host: | |
1956 | self.host = None |
|
1958 | self.host = None | |
1957 |
|
1959 | |||
1958 | if (self.host and self.scheme == 'file' and |
|
1960 | if (self.host and self.scheme == 'file' and | |
1959 | self.host not in ('localhost', '127.0.0.1', '[::1]')): |
|
1961 | self.host not in ('localhost', '127.0.0.1', '[::1]')): | |
1960 | raise Abort(_('file:// URLs can only refer to localhost')) |
|
1962 | raise Abort(_('file:// URLs can only refer to localhost')) | |
1961 |
|
1963 | |||
1962 | self.path = path |
|
1964 | self.path = path | |
1963 |
|
1965 | |||
1964 | # leave the query string escaped |
|
1966 | # leave the query string escaped | |
1965 | for a in ('user', 'passwd', 'host', 'port', |
|
1967 | for a in ('user', 'passwd', 'host', 'port', | |
1966 | 'path', 'fragment'): |
|
1968 | 'path', 'fragment'): | |
1967 | v = getattr(self, a) |
|
1969 | v = getattr(self, a) | |
1968 | if v is not None: |
|
1970 | if v is not None: | |
1969 | setattr(self, a, _urlunquote(v)) |
|
1971 | setattr(self, a, _urlunquote(v)) | |
1970 |
|
1972 | |||
1971 | def __repr__(self): |
|
1973 | def __repr__(self): | |
1972 | attrs = [] |
|
1974 | attrs = [] | |
1973 | for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path', |
|
1975 | for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path', | |
1974 | 'query', 'fragment'): |
|
1976 | 'query', 'fragment'): | |
1975 | v = getattr(self, a) |
|
1977 | v = getattr(self, a) | |
1976 | if v is not None: |
|
1978 | if v is not None: | |
1977 | attrs.append('%s: %r' % (a, v)) |
|
1979 | attrs.append('%s: %r' % (a, v)) | |
1978 | return '<url %s>' % ', '.join(attrs) |
|
1980 | return '<url %s>' % ', '.join(attrs) | |
1979 |
|
1981 | |||
1980 | def __str__(self): |
|
1982 | def __str__(self): | |
1981 | r"""Join the URL's components back into a URL string. |
|
1983 | r"""Join the URL's components back into a URL string. | |
1982 |
|
1984 | |||
1983 | Examples: |
|
1985 | Examples: | |
1984 |
|
1986 | |||
1985 | >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
|
1987 | >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) | |
1986 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
|
1988 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' | |
1987 | >>> str(url('http://user:pw@host:80/?foo=bar&baz=42')) |
|
1989 | >>> str(url('http://user:pw@host:80/?foo=bar&baz=42')) | |
1988 | 'http://user:pw@host:80/?foo=bar&baz=42' |
|
1990 | 'http://user:pw@host:80/?foo=bar&baz=42' | |
1989 | >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz')) |
|
1991 | >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz')) | |
1990 | 'http://user:pw@host:80/?foo=bar%3dbaz' |
|
1992 | 'http://user:pw@host:80/?foo=bar%3dbaz' | |
1991 | >>> str(url('ssh://user:pw@[::1]:2200//home/joe#')) |
|
1993 | >>> str(url('ssh://user:pw@[::1]:2200//home/joe#')) | |
1992 | 'ssh://user:pw@[::1]:2200//home/joe#' |
|
1994 | 'ssh://user:pw@[::1]:2200//home/joe#' | |
1993 | >>> str(url('http://localhost:80//')) |
|
1995 | >>> str(url('http://localhost:80//')) | |
1994 | 'http://localhost:80//' |
|
1996 | 'http://localhost:80//' | |
1995 | >>> str(url('http://localhost:80/')) |
|
1997 | >>> str(url('http://localhost:80/')) | |
1996 | 'http://localhost:80/' |
|
1998 | 'http://localhost:80/' | |
1997 | >>> str(url('http://localhost:80')) |
|
1999 | >>> str(url('http://localhost:80')) | |
1998 | 'http://localhost:80/' |
|
2000 | 'http://localhost:80/' | |
1999 | >>> str(url('bundle:foo')) |
|
2001 | >>> str(url('bundle:foo')) | |
2000 | 'bundle:foo' |
|
2002 | 'bundle:foo' | |
2001 | >>> str(url('bundle://../foo')) |
|
2003 | >>> str(url('bundle://../foo')) | |
2002 | 'bundle:../foo' |
|
2004 | 'bundle:../foo' | |
2003 | >>> str(url('path')) |
|
2005 | >>> str(url('path')) | |
2004 | 'path' |
|
2006 | 'path' | |
2005 | >>> str(url('file:///tmp/foo/bar')) |
|
2007 | >>> str(url('file:///tmp/foo/bar')) | |
2006 | 'file:///tmp/foo/bar' |
|
2008 | 'file:///tmp/foo/bar' | |
2007 | >>> str(url('file:///c:/tmp/foo/bar')) |
|
2009 | >>> str(url('file:///c:/tmp/foo/bar')) | |
2008 | 'file:///c:/tmp/foo/bar' |
|
2010 | 'file:///c:/tmp/foo/bar' | |
2009 | >>> print url(r'bundle:foo\bar') |
|
2011 | >>> print url(r'bundle:foo\bar') | |
2010 | bundle:foo\bar |
|
2012 | bundle:foo\bar | |
2011 | >>> print url(r'file:///D:\data\hg') |
|
2013 | >>> print url(r'file:///D:\data\hg') | |
2012 | file:///D:\data\hg |
|
2014 | file:///D:\data\hg | |
2013 | """ |
|
2015 | """ | |
2014 | if self._localpath: |
|
2016 | if self._localpath: | |
2015 | s = self.path |
|
2017 | s = self.path | |
2016 | if self.scheme == 'bundle': |
|
2018 | if self.scheme == 'bundle': | |
2017 | s = 'bundle:' + s |
|
2019 | s = 'bundle:' + s | |
2018 | if self.fragment: |
|
2020 | if self.fragment: | |
2019 | s += '#' + self.fragment |
|
2021 | s += '#' + self.fragment | |
2020 | return s |
|
2022 | return s | |
2021 |
|
2023 | |||
2022 | s = self.scheme + ':' |
|
2024 | s = self.scheme + ':' | |
2023 | if self.user or self.passwd or self.host: |
|
2025 | if self.user or self.passwd or self.host: | |
2024 | s += '//' |
|
2026 | s += '//' | |
2025 | elif self.scheme and (not self.path or self.path.startswith('/') |
|
2027 | elif self.scheme and (not self.path or self.path.startswith('/') | |
2026 | or hasdriveletter(self.path)): |
|
2028 | or hasdriveletter(self.path)): | |
2027 | s += '//' |
|
2029 | s += '//' | |
2028 | if hasdriveletter(self.path): |
|
2030 | if hasdriveletter(self.path): | |
2029 | s += '/' |
|
2031 | s += '/' | |
2030 | if self.user: |
|
2032 | if self.user: | |
2031 | s += urllib.quote(self.user, safe=self._safechars) |
|
2033 | s += urllib.quote(self.user, safe=self._safechars) | |
2032 | if self.passwd: |
|
2034 | if self.passwd: | |
2033 | s += ':' + urllib.quote(self.passwd, safe=self._safechars) |
|
2035 | s += ':' + urllib.quote(self.passwd, safe=self._safechars) | |
2034 | if self.user or self.passwd: |
|
2036 | if self.user or self.passwd: | |
2035 | s += '@' |
|
2037 | s += '@' | |
2036 | if self.host: |
|
2038 | if self.host: | |
2037 | if not (self.host.startswith('[') and self.host.endswith(']')): |
|
2039 | if not (self.host.startswith('[') and self.host.endswith(']')): | |
2038 | s += urllib.quote(self.host) |
|
2040 | s += urllib.quote(self.host) | |
2039 | else: |
|
2041 | else: | |
2040 | s += self.host |
|
2042 | s += self.host | |
2041 | if self.port: |
|
2043 | if self.port: | |
2042 | s += ':' + urllib.quote(self.port) |
|
2044 | s += ':' + urllib.quote(self.port) | |
2043 | if self.host: |
|
2045 | if self.host: | |
2044 | s += '/' |
|
2046 | s += '/' | |
2045 | if self.path: |
|
2047 | if self.path: | |
2046 | # TODO: similar to the query string, we should not unescape the |
|
2048 | # TODO: similar to the query string, we should not unescape the | |
2047 | # path when we store it, the path might contain '%2f' = '/', |
|
2049 | # path when we store it, the path might contain '%2f' = '/', | |
2048 | # which we should *not* escape. |
|
2050 | # which we should *not* escape. | |
2049 | s += urllib.quote(self.path, safe=self._safepchars) |
|
2051 | s += urllib.quote(self.path, safe=self._safepchars) | |
2050 | if self.query: |
|
2052 | if self.query: | |
2051 | # we store the query in escaped form. |
|
2053 | # we store the query in escaped form. | |
2052 | s += '?' + self.query |
|
2054 | s += '?' + self.query | |
2053 | if self.fragment is not None: |
|
2055 | if self.fragment is not None: | |
2054 | s += '#' + urllib.quote(self.fragment, safe=self._safepchars) |
|
2056 | s += '#' + urllib.quote(self.fragment, safe=self._safepchars) | |
2055 | return s |
|
2057 | return s | |
2056 |
|
2058 | |||
2057 | def authinfo(self): |
|
2059 | def authinfo(self): | |
2058 | user, passwd = self.user, self.passwd |
|
2060 | user, passwd = self.user, self.passwd | |
2059 | try: |
|
2061 | try: | |
2060 | self.user, self.passwd = None, None |
|
2062 | self.user, self.passwd = None, None | |
2061 | s = str(self) |
|
2063 | s = str(self) | |
2062 | finally: |
|
2064 | finally: | |
2063 | self.user, self.passwd = user, passwd |
|
2065 | self.user, self.passwd = user, passwd | |
2064 | if not self.user: |
|
2066 | if not self.user: | |
2065 | return (s, None) |
|
2067 | return (s, None) | |
2066 | # authinfo[1] is passed to urllib2 password manager, and its |
|
2068 | # authinfo[1] is passed to urllib2 password manager, and its | |
2067 | # URIs must not contain credentials. The host is passed in the |
|
2069 | # URIs must not contain credentials. The host is passed in the | |
2068 | # URIs list because Python < 2.4.3 uses only that to search for |
|
2070 | # URIs list because Python < 2.4.3 uses only that to search for | |
2069 | # a password. |
|
2071 | # a password. | |
2070 | return (s, (None, (s, self.host), |
|
2072 | return (s, (None, (s, self.host), | |
2071 | self.user, self.passwd or '')) |
|
2073 | self.user, self.passwd or '')) | |
2072 |
|
2074 | |||
2073 | def isabs(self): |
|
2075 | def isabs(self): | |
2074 | if self.scheme and self.scheme != 'file': |
|
2076 | if self.scheme and self.scheme != 'file': | |
2075 | return True # remote URL |
|
2077 | return True # remote URL | |
2076 | if hasdriveletter(self.path): |
|
2078 | if hasdriveletter(self.path): | |
2077 | return True # absolute for our purposes - can't be joined() |
|
2079 | return True # absolute for our purposes - can't be joined() | |
2078 | if self.path.startswith(r'\\'): |
|
2080 | if self.path.startswith(r'\\'): | |
2079 | return True # Windows UNC path |
|
2081 | return True # Windows UNC path | |
2080 | if self.path.startswith('/'): |
|
2082 | if self.path.startswith('/'): | |
2081 | return True # POSIX-style |
|
2083 | return True # POSIX-style | |
2082 | return False |
|
2084 | return False | |
2083 |
|
2085 | |||
2084 | def localpath(self): |
|
2086 | def localpath(self): | |
2085 | if self.scheme == 'file' or self.scheme == 'bundle': |
|
2087 | if self.scheme == 'file' or self.scheme == 'bundle': | |
2086 | path = self.path or '/' |
|
2088 | path = self.path or '/' | |
2087 | # For Windows, we need to promote hosts containing drive |
|
2089 | # For Windows, we need to promote hosts containing drive | |
2088 | # letters to paths with drive letters. |
|
2090 | # letters to paths with drive letters. | |
2089 | if hasdriveletter(self._hostport): |
|
2091 | if hasdriveletter(self._hostport): | |
2090 | path = self._hostport + '/' + self.path |
|
2092 | path = self._hostport + '/' + self.path | |
2091 | elif (self.host is not None and self.path |
|
2093 | elif (self.host is not None and self.path | |
2092 | and not hasdriveletter(path)): |
|
2094 | and not hasdriveletter(path)): | |
2093 | path = '/' + path |
|
2095 | path = '/' + path | |
2094 | return path |
|
2096 | return path | |
2095 | return self._origpath |
|
2097 | return self._origpath | |
2096 |
|
2098 | |||
2097 | def islocal(self): |
|
2099 | def islocal(self): | |
2098 | '''whether localpath will return something that posixfile can open''' |
|
2100 | '''whether localpath will return something that posixfile can open''' | |
2099 | return (not self.scheme or self.scheme == 'file' |
|
2101 | return (not self.scheme or self.scheme == 'file' | |
2100 | or self.scheme == 'bundle') |
|
2102 | or self.scheme == 'bundle') | |
2101 |
|
2103 | |||
2102 | def hasscheme(path): |
|
2104 | def hasscheme(path): | |
2103 | return bool(url(path).scheme) |
|
2105 | return bool(url(path).scheme) | |
2104 |
|
2106 | |||
2105 | def hasdriveletter(path): |
|
2107 | def hasdriveletter(path): | |
2106 | return path and path[1:2] == ':' and path[0:1].isalpha() |
|
2108 | return path and path[1:2] == ':' and path[0:1].isalpha() | |
2107 |
|
2109 | |||
2108 | def urllocalpath(path): |
|
2110 | def urllocalpath(path): | |
2109 | return url(path, parsequery=False, parsefragment=False).localpath() |
|
2111 | return url(path, parsequery=False, parsefragment=False).localpath() | |
2110 |
|
2112 | |||
2111 | def hidepassword(u): |
|
2113 | def hidepassword(u): | |
2112 | '''hide user credential in a url string''' |
|
2114 | '''hide user credential in a url string''' | |
2113 | u = url(u) |
|
2115 | u = url(u) | |
2114 | if u.passwd: |
|
2116 | if u.passwd: | |
2115 | u.passwd = '***' |
|
2117 | u.passwd = '***' | |
2116 | return str(u) |
|
2118 | return str(u) | |
2117 |
|
2119 | |||
2118 | def removeauth(u): |
|
2120 | def removeauth(u): | |
2119 | '''remove all authentication information from a url string''' |
|
2121 | '''remove all authentication information from a url string''' | |
2120 | u = url(u) |
|
2122 | u = url(u) | |
2121 | u.user = u.passwd = None |
|
2123 | u.user = u.passwd = None | |
2122 | return str(u) |
|
2124 | return str(u) | |
2123 |
|
2125 | |||
2124 | def isatty(fd): |
|
2126 | def isatty(fd): | |
2125 | try: |
|
2127 | try: | |
2126 | return fd.isatty() |
|
2128 | return fd.isatty() | |
2127 | except AttributeError: |
|
2129 | except AttributeError: | |
2128 | return False |
|
2130 | return False | |
2129 |
|
2131 | |||
2130 | timecount = unitcountfn( |
|
2132 | timecount = unitcountfn( | |
2131 | (1, 1e3, _('%.0f s')), |
|
2133 | (1, 1e3, _('%.0f s')), | |
2132 | (100, 1, _('%.1f s')), |
|
2134 | (100, 1, _('%.1f s')), | |
2133 | (10, 1, _('%.2f s')), |
|
2135 | (10, 1, _('%.2f s')), | |
2134 | (1, 1, _('%.3f s')), |
|
2136 | (1, 1, _('%.3f s')), | |
2135 | (100, 0.001, _('%.1f ms')), |
|
2137 | (100, 0.001, _('%.1f ms')), | |
2136 | (10, 0.001, _('%.2f ms')), |
|
2138 | (10, 0.001, _('%.2f ms')), | |
2137 | (1, 0.001, _('%.3f ms')), |
|
2139 | (1, 0.001, _('%.3f ms')), | |
2138 | (100, 0.000001, _('%.1f us')), |
|
2140 | (100, 0.000001, _('%.1f us')), | |
2139 | (10, 0.000001, _('%.2f us')), |
|
2141 | (10, 0.000001, _('%.2f us')), | |
2140 | (1, 0.000001, _('%.3f us')), |
|
2142 | (1, 0.000001, _('%.3f us')), | |
2141 | (100, 0.000000001, _('%.1f ns')), |
|
2143 | (100, 0.000000001, _('%.1f ns')), | |
2142 | (10, 0.000000001, _('%.2f ns')), |
|
2144 | (10, 0.000000001, _('%.2f ns')), | |
2143 | (1, 0.000000001, _('%.3f ns')), |
|
2145 | (1, 0.000000001, _('%.3f ns')), | |
2144 | ) |
|
2146 | ) | |
2145 |
|
2147 | |||
2146 | _timenesting = [0] |
|
2148 | _timenesting = [0] | |
2147 |
|
2149 | |||
2148 | def timed(func): |
|
2150 | def timed(func): | |
2149 | '''Report the execution time of a function call to stderr. |
|
2151 | '''Report the execution time of a function call to stderr. | |
2150 |
|
2152 | |||
2151 | During development, use as a decorator when you need to measure |
|
2153 | During development, use as a decorator when you need to measure | |
2152 | the cost of a function, e.g. as follows: |
|
2154 | the cost of a function, e.g. as follows: | |
2153 |
|
2155 | |||
2154 | @util.timed |
|
2156 | @util.timed | |
2155 | def foo(a, b, c): |
|
2157 | def foo(a, b, c): | |
2156 | pass |
|
2158 | pass | |
2157 | ''' |
|
2159 | ''' | |
2158 |
|
2160 | |||
2159 | def wrapper(*args, **kwargs): |
|
2161 | def wrapper(*args, **kwargs): | |
2160 | start = time.time() |
|
2162 | start = time.time() | |
2161 | indent = 2 |
|
2163 | indent = 2 | |
2162 | _timenesting[0] += indent |
|
2164 | _timenesting[0] += indent | |
2163 | try: |
|
2165 | try: | |
2164 | return func(*args, **kwargs) |
|
2166 | return func(*args, **kwargs) | |
2165 | finally: |
|
2167 | finally: | |
2166 | elapsed = time.time() - start |
|
2168 | elapsed = time.time() - start | |
2167 | _timenesting[0] -= indent |
|
2169 | _timenesting[0] -= indent | |
2168 | sys.stderr.write('%s%s: %s\n' % |
|
2170 | sys.stderr.write('%s%s: %s\n' % | |
2169 | (' ' * _timenesting[0], func.__name__, |
|
2171 | (' ' * _timenesting[0], func.__name__, | |
2170 | timecount(elapsed))) |
|
2172 | timecount(elapsed))) | |
2171 | return wrapper |
|
2173 | return wrapper | |
2172 |
|
2174 | |||
2173 | _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), |
|
2175 | _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), | |
2174 | ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) |
|
2176 | ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) | |
2175 |
|
2177 | |||
2176 | def sizetoint(s): |
|
2178 | def sizetoint(s): | |
2177 | '''Convert a space specifier to a byte count. |
|
2179 | '''Convert a space specifier to a byte count. | |
2178 |
|
2180 | |||
2179 | >>> sizetoint('30') |
|
2181 | >>> sizetoint('30') | |
2180 | 30 |
|
2182 | 30 | |
2181 | >>> sizetoint('2.2kb') |
|
2183 | >>> sizetoint('2.2kb') | |
2182 | 2252 |
|
2184 | 2252 | |
2183 | >>> sizetoint('6M') |
|
2185 | >>> sizetoint('6M') | |
2184 | 6291456 |
|
2186 | 6291456 | |
2185 | ''' |
|
2187 | ''' | |
2186 | t = s.strip().lower() |
|
2188 | t = s.strip().lower() | |
2187 | try: |
|
2189 | try: | |
2188 | for k, u in _sizeunits: |
|
2190 | for k, u in _sizeunits: | |
2189 | if t.endswith(k): |
|
2191 | if t.endswith(k): | |
2190 | return int(float(t[:-len(k)]) * u) |
|
2192 | return int(float(t[:-len(k)]) * u) | |
2191 | return int(t) |
|
2193 | return int(t) | |
2192 | except ValueError: |
|
2194 | except ValueError: | |
2193 | raise error.ParseError(_("couldn't parse size: %s") % s) |
|
2195 | raise error.ParseError(_("couldn't parse size: %s") % s) | |
2194 |
|
2196 | |||
2195 | class hooks(object): |
|
2197 | class hooks(object): | |
2196 | '''A collection of hook functions that can be used to extend a |
|
2198 | '''A collection of hook functions that can be used to extend a | |
2197 | function's behaviour. Hooks are called in lexicographic order, |
|
2199 | function's behaviour. Hooks are called in lexicographic order, | |
2198 | based on the names of their sources.''' |
|
2200 | based on the names of their sources.''' | |
2199 |
|
2201 | |||
2200 | def __init__(self): |
|
2202 | def __init__(self): | |
2201 | self._hooks = [] |
|
2203 | self._hooks = [] | |
2202 |
|
2204 | |||
2203 | def add(self, source, hook): |
|
2205 | def add(self, source, hook): | |
2204 | self._hooks.append((source, hook)) |
|
2206 | self._hooks.append((source, hook)) | |
2205 |
|
2207 | |||
2206 | def __call__(self, *args): |
|
2208 | def __call__(self, *args): | |
2207 | self._hooks.sort(key=lambda x: x[0]) |
|
2209 | self._hooks.sort(key=lambda x: x[0]) | |
2208 | results = [] |
|
2210 | results = [] | |
2209 | for source, hook in self._hooks: |
|
2211 | for source, hook in self._hooks: | |
2210 | results.append(hook(*args)) |
|
2212 | results.append(hook(*args)) | |
2211 | return results |
|
2213 | return results | |
2212 |
|
2214 | |||
2213 | def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout): |
|
2215 | def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout): | |
2214 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. |
|
2216 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. | |
2215 | Skips the 'skip' last entries. By default it will flush stdout first. |
|
2217 | Skips the 'skip' last entries. By default it will flush stdout first. | |
2216 | It can be used everywhere and do intentionally not require an ui object. |
|
2218 | It can be used everywhere and do intentionally not require an ui object. | |
2217 | Not be used in production code but very convenient while developing. |
|
2219 | Not be used in production code but very convenient while developing. | |
2218 | ''' |
|
2220 | ''' | |
2219 | if otherf: |
|
2221 | if otherf: | |
2220 | otherf.flush() |
|
2222 | otherf.flush() | |
2221 | f.write('%s at:\n' % msg) |
|
2223 | f.write('%s at:\n' % msg) | |
2222 | entries = [('%s:%s' % (fn, ln), func) |
|
2224 | entries = [('%s:%s' % (fn, ln), func) | |
2223 | for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]] |
|
2225 | for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]] | |
2224 | if entries: |
|
2226 | if entries: | |
2225 | fnmax = max(len(entry[0]) for entry in entries) |
|
2227 | fnmax = max(len(entry[0]) for entry in entries) | |
2226 | for fnln, func in entries: |
|
2228 | for fnln, func in entries: | |
2227 | f.write(' %-*s in %s\n' % (fnmax, fnln, func)) |
|
2229 | f.write(' %-*s in %s\n' % (fnmax, fnln, func)) | |
2228 | f.flush() |
|
2230 | f.flush() | |
2229 |
|
2231 | |||
2230 | # convenient shortcut |
|
2232 | # convenient shortcut | |
2231 | dst = debugstacktrace |
|
2233 | dst = debugstacktrace |
General Comments 0
You need to be logged in to leave comments.
Login now