##// END OF EJS Templates
Fix util.shellquote on windows.
Alexis S. L. Carvalho -
r4087:587c6c65 default
parent child Browse files
Show More
@@ -1,1346 +1,1361 b''
1 """
1 """
2 util.py - Mercurial utility functions and platform specfic implementations
2 util.py - Mercurial utility functions and platform specfic implementations
3
3
4 Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 Copyright 2005 K. Thananchayan <thananck@yahoo.com>
5 Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
5 Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
6 Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
7
7
8 This software may be used and distributed according to the terms
8 This software may be used and distributed according to the terms
9 of the GNU General Public License, incorporated herein by reference.
9 of the GNU General Public License, incorporated herein by reference.
10
10
11 This contains helper routines that are independent of the SCM core and hide
11 This contains helper routines that are independent of the SCM core and hide
12 platform-specific details from the core.
12 platform-specific details from the core.
13 """
13 """
14
14
15 from i18n import gettext as _
15 from i18n import gettext as _
16 from demandload import *
16 from demandload import *
17 demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile")
17 demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile")
18 demandload(globals(), "os threading time calendar ConfigParser locale glob")
18 demandload(globals(), "os threading time calendar ConfigParser locale glob")
19
19
20 try:
20 try:
21 _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() \
21 _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() \
22 or "ascii"
22 or "ascii"
23 except locale.Error:
23 except locale.Error:
24 _encoding = 'ascii'
24 _encoding = 'ascii'
25 _encodingmode = os.environ.get("HGENCODINGMODE", "strict")
25 _encodingmode = os.environ.get("HGENCODINGMODE", "strict")
26 _fallbackencoding = 'ISO-8859-1'
26 _fallbackencoding = 'ISO-8859-1'
27
27
28 def tolocal(s):
28 def tolocal(s):
29 """
29 """
30 Convert a string from internal UTF-8 to local encoding
30 Convert a string from internal UTF-8 to local encoding
31
31
32 All internal strings should be UTF-8 but some repos before the
32 All internal strings should be UTF-8 but some repos before the
33 implementation of locale support may contain latin1 or possibly
33 implementation of locale support may contain latin1 or possibly
34 other character sets. We attempt to decode everything strictly
34 other character sets. We attempt to decode everything strictly
35 using UTF-8, then Latin-1, and failing that, we use UTF-8 and
35 using UTF-8, then Latin-1, and failing that, we use UTF-8 and
36 replace unknown characters.
36 replace unknown characters.
37 """
37 """
38 for e in ('UTF-8', _fallbackencoding):
38 for e in ('UTF-8', _fallbackencoding):
39 try:
39 try:
40 u = s.decode(e) # attempt strict decoding
40 u = s.decode(e) # attempt strict decoding
41 return u.encode(_encoding, "replace")
41 return u.encode(_encoding, "replace")
42 except LookupError, k:
42 except LookupError, k:
43 raise Abort(_("%s, please check your locale settings") % k)
43 raise Abort(_("%s, please check your locale settings") % k)
44 except UnicodeDecodeError:
44 except UnicodeDecodeError:
45 pass
45 pass
46 u = s.decode("utf-8", "replace") # last ditch
46 u = s.decode("utf-8", "replace") # last ditch
47 return u.encode(_encoding, "replace")
47 return u.encode(_encoding, "replace")
48
48
49 def fromlocal(s):
49 def fromlocal(s):
50 """
50 """
51 Convert a string from the local character encoding to UTF-8
51 Convert a string from the local character encoding to UTF-8
52
52
53 We attempt to decode strings using the encoding mode set by
53 We attempt to decode strings using the encoding mode set by
54 HG_ENCODINGMODE, which defaults to 'strict'. In this mode, unknown
54 HG_ENCODINGMODE, which defaults to 'strict'. In this mode, unknown
55 characters will cause an error message. Other modes include
55 characters will cause an error message. Other modes include
56 'replace', which replaces unknown characters with a special
56 'replace', which replaces unknown characters with a special
57 Unicode character, and 'ignore', which drops the character.
57 Unicode character, and 'ignore', which drops the character.
58 """
58 """
59 try:
59 try:
60 return s.decode(_encoding, _encodingmode).encode("utf-8")
60 return s.decode(_encoding, _encodingmode).encode("utf-8")
61 except UnicodeDecodeError, inst:
61 except UnicodeDecodeError, inst:
62 sub = s[max(0, inst.start-10):inst.start+10]
62 sub = s[max(0, inst.start-10):inst.start+10]
63 raise Abort("decoding near '%s': %s!" % (sub, inst))
63 raise Abort("decoding near '%s': %s!" % (sub, inst))
64 except LookupError, k:
64 except LookupError, k:
65 raise Abort(_("%s, please check your locale settings") % k)
65 raise Abort(_("%s, please check your locale settings") % k)
66
66
67 def locallen(s):
67 def locallen(s):
68 """Find the length in characters of a local string"""
68 """Find the length in characters of a local string"""
69 return len(s.decode(_encoding, "replace"))
69 return len(s.decode(_encoding, "replace"))
70
70
71 def localsub(s, a, b=None):
71 def localsub(s, a, b=None):
72 try:
72 try:
73 u = s.decode(_encoding, _encodingmode)
73 u = s.decode(_encoding, _encodingmode)
74 if b is not None:
74 if b is not None:
75 u = u[a:b]
75 u = u[a:b]
76 else:
76 else:
77 u = u[:a]
77 u = u[:a]
78 return u.encode(_encoding, _encodingmode)
78 return u.encode(_encoding, _encodingmode)
79 except UnicodeDecodeError, inst:
79 except UnicodeDecodeError, inst:
80 sub = s[max(0, inst.start-10), inst.start+10]
80 sub = s[max(0, inst.start-10), inst.start+10]
81 raise Abort(_("decoding near '%s': %s!\n") % (sub, inst))
81 raise Abort(_("decoding near '%s': %s!\n") % (sub, inst))
82
82
83 # used by parsedate
83 # used by parsedate
84 defaultdateformats = (
84 defaultdateformats = (
85 '%Y-%m-%d %H:%M:%S',
85 '%Y-%m-%d %H:%M:%S',
86 '%Y-%m-%d %I:%M:%S%p',
86 '%Y-%m-%d %I:%M:%S%p',
87 '%Y-%m-%d %H:%M',
87 '%Y-%m-%d %H:%M',
88 '%Y-%m-%d %I:%M%p',
88 '%Y-%m-%d %I:%M%p',
89 '%Y-%m-%d',
89 '%Y-%m-%d',
90 '%m-%d',
90 '%m-%d',
91 '%m/%d',
91 '%m/%d',
92 '%m/%d/%y',
92 '%m/%d/%y',
93 '%m/%d/%Y',
93 '%m/%d/%Y',
94 '%a %b %d %H:%M:%S %Y',
94 '%a %b %d %H:%M:%S %Y',
95 '%a %b %d %I:%M:%S%p %Y',
95 '%a %b %d %I:%M:%S%p %Y',
96 '%b %d %H:%M:%S %Y',
96 '%b %d %H:%M:%S %Y',
97 '%b %d %I:%M:%S%p %Y',
97 '%b %d %I:%M:%S%p %Y',
98 '%b %d %H:%M:%S',
98 '%b %d %H:%M:%S',
99 '%b %d %I:%M:%S%p',
99 '%b %d %I:%M:%S%p',
100 '%b %d %H:%M',
100 '%b %d %H:%M',
101 '%b %d %I:%M%p',
101 '%b %d %I:%M%p',
102 '%b %d %Y',
102 '%b %d %Y',
103 '%b %d',
103 '%b %d',
104 '%H:%M:%S',
104 '%H:%M:%S',
105 '%I:%M:%SP',
105 '%I:%M:%SP',
106 '%H:%M',
106 '%H:%M',
107 '%I:%M%p',
107 '%I:%M%p',
108 )
108 )
109
109
110 extendeddateformats = defaultdateformats + (
110 extendeddateformats = defaultdateformats + (
111 "%Y",
111 "%Y",
112 "%Y-%m",
112 "%Y-%m",
113 "%b",
113 "%b",
114 "%b %Y",
114 "%b %Y",
115 )
115 )
116
116
117 class SignalInterrupt(Exception):
117 class SignalInterrupt(Exception):
118 """Exception raised on SIGTERM and SIGHUP."""
118 """Exception raised on SIGTERM and SIGHUP."""
119
119
120 # like SafeConfigParser but with case-sensitive keys
120 # like SafeConfigParser but with case-sensitive keys
121 class configparser(ConfigParser.SafeConfigParser):
121 class configparser(ConfigParser.SafeConfigParser):
122 def optionxform(self, optionstr):
122 def optionxform(self, optionstr):
123 return optionstr
123 return optionstr
124
124
125 def cachefunc(func):
125 def cachefunc(func):
126 '''cache the result of function calls'''
126 '''cache the result of function calls'''
127 # XXX doesn't handle keywords args
127 # XXX doesn't handle keywords args
128 cache = {}
128 cache = {}
129 if func.func_code.co_argcount == 1:
129 if func.func_code.co_argcount == 1:
130 # we gain a small amount of time because
130 # we gain a small amount of time because
131 # we don't need to pack/unpack the list
131 # we don't need to pack/unpack the list
132 def f(arg):
132 def f(arg):
133 if arg not in cache:
133 if arg not in cache:
134 cache[arg] = func(arg)
134 cache[arg] = func(arg)
135 return cache[arg]
135 return cache[arg]
136 else:
136 else:
137 def f(*args):
137 def f(*args):
138 if args not in cache:
138 if args not in cache:
139 cache[args] = func(*args)
139 cache[args] = func(*args)
140 return cache[args]
140 return cache[args]
141
141
142 return f
142 return f
143
143
144 def pipefilter(s, cmd):
144 def pipefilter(s, cmd):
145 '''filter string S through command CMD, returning its output'''
145 '''filter string S through command CMD, returning its output'''
146 (pout, pin) = popen2.popen2(cmd, -1, 'b')
146 (pout, pin) = popen2.popen2(cmd, -1, 'b')
147 def writer():
147 def writer():
148 try:
148 try:
149 pin.write(s)
149 pin.write(s)
150 pin.close()
150 pin.close()
151 except IOError, inst:
151 except IOError, inst:
152 if inst.errno != errno.EPIPE:
152 if inst.errno != errno.EPIPE:
153 raise
153 raise
154
154
155 # we should use select instead on UNIX, but this will work on most
155 # we should use select instead on UNIX, but this will work on most
156 # systems, including Windows
156 # systems, including Windows
157 w = threading.Thread(target=writer)
157 w = threading.Thread(target=writer)
158 w.start()
158 w.start()
159 f = pout.read()
159 f = pout.read()
160 pout.close()
160 pout.close()
161 w.join()
161 w.join()
162 return f
162 return f
163
163
164 def tempfilter(s, cmd):
164 def tempfilter(s, cmd):
165 '''filter string S through a pair of temporary files with CMD.
165 '''filter string S through a pair of temporary files with CMD.
166 CMD is used as a template to create the real command to be run,
166 CMD is used as a template to create the real command to be run,
167 with the strings INFILE and OUTFILE replaced by the real names of
167 with the strings INFILE and OUTFILE replaced by the real names of
168 the temporary files generated.'''
168 the temporary files generated.'''
169 inname, outname = None, None
169 inname, outname = None, None
170 try:
170 try:
171 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
171 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
172 fp = os.fdopen(infd, 'wb')
172 fp = os.fdopen(infd, 'wb')
173 fp.write(s)
173 fp.write(s)
174 fp.close()
174 fp.close()
175 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
175 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
176 os.close(outfd)
176 os.close(outfd)
177 cmd = cmd.replace('INFILE', inname)
177 cmd = cmd.replace('INFILE', inname)
178 cmd = cmd.replace('OUTFILE', outname)
178 cmd = cmd.replace('OUTFILE', outname)
179 code = os.system(cmd)
179 code = os.system(cmd)
180 if code: raise Abort(_("command '%s' failed: %s") %
180 if code: raise Abort(_("command '%s' failed: %s") %
181 (cmd, explain_exit(code)))
181 (cmd, explain_exit(code)))
182 return open(outname, 'rb').read()
182 return open(outname, 'rb').read()
183 finally:
183 finally:
184 try:
184 try:
185 if inname: os.unlink(inname)
185 if inname: os.unlink(inname)
186 except: pass
186 except: pass
187 try:
187 try:
188 if outname: os.unlink(outname)
188 if outname: os.unlink(outname)
189 except: pass
189 except: pass
190
190
191 filtertable = {
191 filtertable = {
192 'tempfile:': tempfilter,
192 'tempfile:': tempfilter,
193 'pipe:': pipefilter,
193 'pipe:': pipefilter,
194 }
194 }
195
195
196 def filter(s, cmd):
196 def filter(s, cmd):
197 "filter a string through a command that transforms its input to its output"
197 "filter a string through a command that transforms its input to its output"
198 for name, fn in filtertable.iteritems():
198 for name, fn in filtertable.iteritems():
199 if cmd.startswith(name):
199 if cmd.startswith(name):
200 return fn(s, cmd[len(name):].lstrip())
200 return fn(s, cmd[len(name):].lstrip())
201 return pipefilter(s, cmd)
201 return pipefilter(s, cmd)
202
202
203 def find_in_path(name, path, default=None):
203 def find_in_path(name, path, default=None):
204 '''find name in search path. path can be string (will be split
204 '''find name in search path. path can be string (will be split
205 with os.pathsep), or iterable thing that returns strings. if name
205 with os.pathsep), or iterable thing that returns strings. if name
206 found, return path to name. else return default.'''
206 found, return path to name. else return default.'''
207 if isinstance(path, str):
207 if isinstance(path, str):
208 path = path.split(os.pathsep)
208 path = path.split(os.pathsep)
209 for p in path:
209 for p in path:
210 p_name = os.path.join(p, name)
210 p_name = os.path.join(p, name)
211 if os.path.exists(p_name):
211 if os.path.exists(p_name):
212 return p_name
212 return p_name
213 return default
213 return default
214
214
215 def binary(s):
215 def binary(s):
216 """return true if a string is binary data using diff's heuristic"""
216 """return true if a string is binary data using diff's heuristic"""
217 if s and '\0' in s[:4096]:
217 if s and '\0' in s[:4096]:
218 return True
218 return True
219 return False
219 return False
220
220
221 def unique(g):
221 def unique(g):
222 """return the uniq elements of iterable g"""
222 """return the uniq elements of iterable g"""
223 seen = {}
223 seen = {}
224 l = []
224 l = []
225 for f in g:
225 for f in g:
226 if f not in seen:
226 if f not in seen:
227 seen[f] = 1
227 seen[f] = 1
228 l.append(f)
228 l.append(f)
229 return l
229 return l
230
230
231 class Abort(Exception):
231 class Abort(Exception):
232 """Raised if a command needs to print an error and exit."""
232 """Raised if a command needs to print an error and exit."""
233
233
234 class UnexpectedOutput(Abort):
234 class UnexpectedOutput(Abort):
235 """Raised to print an error with part of output and exit."""
235 """Raised to print an error with part of output and exit."""
236
236
237 def always(fn): return True
237 def always(fn): return True
238 def never(fn): return False
238 def never(fn): return False
239
239
240 def expand_glob(pats):
240 def expand_glob(pats):
241 '''On Windows, expand the implicit globs in a list of patterns'''
241 '''On Windows, expand the implicit globs in a list of patterns'''
242 if os.name != 'nt':
242 if os.name != 'nt':
243 return list(pats)
243 return list(pats)
244 ret = []
244 ret = []
245 for p in pats:
245 for p in pats:
246 kind, name = patkind(p, None)
246 kind, name = patkind(p, None)
247 if kind is None:
247 if kind is None:
248 globbed = glob.glob(name)
248 globbed = glob.glob(name)
249 if globbed:
249 if globbed:
250 ret.extend(globbed)
250 ret.extend(globbed)
251 continue
251 continue
252 # if we couldn't expand the glob, just keep it around
252 # if we couldn't expand the glob, just keep it around
253 ret.append(p)
253 ret.append(p)
254 return ret
254 return ret
255
255
256 def patkind(name, dflt_pat='glob'):
256 def patkind(name, dflt_pat='glob'):
257 """Split a string into an optional pattern kind prefix and the
257 """Split a string into an optional pattern kind prefix and the
258 actual pattern."""
258 actual pattern."""
259 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
259 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
260 if name.startswith(prefix + ':'): return name.split(':', 1)
260 if name.startswith(prefix + ':'): return name.split(':', 1)
261 return dflt_pat, name
261 return dflt_pat, name
262
262
263 def globre(pat, head='^', tail='$'):
263 def globre(pat, head='^', tail='$'):
264 "convert a glob pattern into a regexp"
264 "convert a glob pattern into a regexp"
265 i, n = 0, len(pat)
265 i, n = 0, len(pat)
266 res = ''
266 res = ''
267 group = False
267 group = False
268 def peek(): return i < n and pat[i]
268 def peek(): return i < n and pat[i]
269 while i < n:
269 while i < n:
270 c = pat[i]
270 c = pat[i]
271 i = i+1
271 i = i+1
272 if c == '*':
272 if c == '*':
273 if peek() == '*':
273 if peek() == '*':
274 i += 1
274 i += 1
275 res += '.*'
275 res += '.*'
276 else:
276 else:
277 res += '[^/]*'
277 res += '[^/]*'
278 elif c == '?':
278 elif c == '?':
279 res += '.'
279 res += '.'
280 elif c == '[':
280 elif c == '[':
281 j = i
281 j = i
282 if j < n and pat[j] in '!]':
282 if j < n and pat[j] in '!]':
283 j += 1
283 j += 1
284 while j < n and pat[j] != ']':
284 while j < n and pat[j] != ']':
285 j += 1
285 j += 1
286 if j >= n:
286 if j >= n:
287 res += '\\['
287 res += '\\['
288 else:
288 else:
289 stuff = pat[i:j].replace('\\','\\\\')
289 stuff = pat[i:j].replace('\\','\\\\')
290 i = j + 1
290 i = j + 1
291 if stuff[0] == '!':
291 if stuff[0] == '!':
292 stuff = '^' + stuff[1:]
292 stuff = '^' + stuff[1:]
293 elif stuff[0] == '^':
293 elif stuff[0] == '^':
294 stuff = '\\' + stuff
294 stuff = '\\' + stuff
295 res = '%s[%s]' % (res, stuff)
295 res = '%s[%s]' % (res, stuff)
296 elif c == '{':
296 elif c == '{':
297 group = True
297 group = True
298 res += '(?:'
298 res += '(?:'
299 elif c == '}' and group:
299 elif c == '}' and group:
300 res += ')'
300 res += ')'
301 group = False
301 group = False
302 elif c == ',' and group:
302 elif c == ',' and group:
303 res += '|'
303 res += '|'
304 elif c == '\\':
304 elif c == '\\':
305 p = peek()
305 p = peek()
306 if p:
306 if p:
307 i += 1
307 i += 1
308 res += re.escape(p)
308 res += re.escape(p)
309 else:
309 else:
310 res += re.escape(c)
310 res += re.escape(c)
311 else:
311 else:
312 res += re.escape(c)
312 res += re.escape(c)
313 return head + res + tail
313 return head + res + tail
314
314
315 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
315 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
316
316
317 def pathto(n1, n2):
317 def pathto(n1, n2):
318 '''return the relative path from one place to another.
318 '''return the relative path from one place to another.
319 n1 should use os.sep to separate directories
319 n1 should use os.sep to separate directories
320 n2 should use "/" to separate directories
320 n2 should use "/" to separate directories
321 returns an os.sep-separated path.
321 returns an os.sep-separated path.
322 '''
322 '''
323 if not n1: return localpath(n2)
323 if not n1: return localpath(n2)
324 a, b = n1.split(os.sep), n2.split('/')
324 a, b = n1.split(os.sep), n2.split('/')
325 a.reverse()
325 a.reverse()
326 b.reverse()
326 b.reverse()
327 while a and b and a[-1] == b[-1]:
327 while a and b and a[-1] == b[-1]:
328 a.pop()
328 a.pop()
329 b.pop()
329 b.pop()
330 b.reverse()
330 b.reverse()
331 return os.sep.join((['..'] * len(a)) + b)
331 return os.sep.join((['..'] * len(a)) + b)
332
332
333 def canonpath(root, cwd, myname):
333 def canonpath(root, cwd, myname):
334 """return the canonical path of myname, given cwd and root"""
334 """return the canonical path of myname, given cwd and root"""
335 if root == os.sep:
335 if root == os.sep:
336 rootsep = os.sep
336 rootsep = os.sep
337 elif root.endswith(os.sep):
337 elif root.endswith(os.sep):
338 rootsep = root
338 rootsep = root
339 else:
339 else:
340 rootsep = root + os.sep
340 rootsep = root + os.sep
341 name = myname
341 name = myname
342 if not os.path.isabs(name):
342 if not os.path.isabs(name):
343 name = os.path.join(root, cwd, name)
343 name = os.path.join(root, cwd, name)
344 name = os.path.normpath(name)
344 name = os.path.normpath(name)
345 if name != rootsep and name.startswith(rootsep):
345 if name != rootsep and name.startswith(rootsep):
346 name = name[len(rootsep):]
346 name = name[len(rootsep):]
347 audit_path(name)
347 audit_path(name)
348 return pconvert(name)
348 return pconvert(name)
349 elif name == root:
349 elif name == root:
350 return ''
350 return ''
351 else:
351 else:
352 # Determine whether `name' is in the hierarchy at or beneath `root',
352 # Determine whether `name' is in the hierarchy at or beneath `root',
353 # by iterating name=dirname(name) until that causes no change (can't
353 # by iterating name=dirname(name) until that causes no change (can't
354 # check name == '/', because that doesn't work on windows). For each
354 # check name == '/', because that doesn't work on windows). For each
355 # `name', compare dev/inode numbers. If they match, the list `rel'
355 # `name', compare dev/inode numbers. If they match, the list `rel'
356 # holds the reversed list of components making up the relative file
356 # holds the reversed list of components making up the relative file
357 # name we want.
357 # name we want.
358 root_st = os.stat(root)
358 root_st = os.stat(root)
359 rel = []
359 rel = []
360 while True:
360 while True:
361 try:
361 try:
362 name_st = os.stat(name)
362 name_st = os.stat(name)
363 except OSError:
363 except OSError:
364 break
364 break
365 if samestat(name_st, root_st):
365 if samestat(name_st, root_st):
366 if not rel:
366 if not rel:
367 # name was actually the same as root (maybe a symlink)
367 # name was actually the same as root (maybe a symlink)
368 return ''
368 return ''
369 rel.reverse()
369 rel.reverse()
370 name = os.path.join(*rel)
370 name = os.path.join(*rel)
371 audit_path(name)
371 audit_path(name)
372 return pconvert(name)
372 return pconvert(name)
373 dirname, basename = os.path.split(name)
373 dirname, basename = os.path.split(name)
374 rel.append(basename)
374 rel.append(basename)
375 if dirname == name:
375 if dirname == name:
376 break
376 break
377 name = dirname
377 name = dirname
378
378
379 raise Abort('%s not under root' % myname)
379 raise Abort('%s not under root' % myname)
380
380
381 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
381 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
382 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src)
382 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src)
383
383
384 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='',
384 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='',
385 src=None, globbed=False):
385 src=None, globbed=False):
386 if not globbed:
386 if not globbed:
387 names = expand_glob(names)
387 names = expand_glob(names)
388 return _matcher(canonroot, cwd, names, inc, exc, head, 'relpath', src)
388 return _matcher(canonroot, cwd, names, inc, exc, head, 'relpath', src)
389
389
390 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src):
390 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src):
391 """build a function to match a set of file patterns
391 """build a function to match a set of file patterns
392
392
393 arguments:
393 arguments:
394 canonroot - the canonical root of the tree you're matching against
394 canonroot - the canonical root of the tree you're matching against
395 cwd - the current working directory, if relevant
395 cwd - the current working directory, if relevant
396 names - patterns to find
396 names - patterns to find
397 inc - patterns to include
397 inc - patterns to include
398 exc - patterns to exclude
398 exc - patterns to exclude
399 head - a regex to prepend to patterns to control whether a match is rooted
399 head - a regex to prepend to patterns to control whether a match is rooted
400
400
401 a pattern is one of:
401 a pattern is one of:
402 'glob:<rooted glob>'
402 'glob:<rooted glob>'
403 're:<rooted regexp>'
403 're:<rooted regexp>'
404 'path:<rooted path>'
404 'path:<rooted path>'
405 'relglob:<relative glob>'
405 'relglob:<relative glob>'
406 'relpath:<relative path>'
406 'relpath:<relative path>'
407 'relre:<relative regexp>'
407 'relre:<relative regexp>'
408 '<rooted path or regexp>'
408 '<rooted path or regexp>'
409
409
410 returns:
410 returns:
411 a 3-tuple containing
411 a 3-tuple containing
412 - list of explicit non-pattern names passed in
412 - list of explicit non-pattern names passed in
413 - a bool match(filename) function
413 - a bool match(filename) function
414 - a bool indicating if any patterns were passed in
414 - a bool indicating if any patterns were passed in
415
415
416 todo:
416 todo:
417 make head regex a rooted bool
417 make head regex a rooted bool
418 """
418 """
419
419
420 def contains_glob(name):
420 def contains_glob(name):
421 for c in name:
421 for c in name:
422 if c in _globchars: return True
422 if c in _globchars: return True
423 return False
423 return False
424
424
425 def regex(kind, name, tail):
425 def regex(kind, name, tail):
426 '''convert a pattern into a regular expression'''
426 '''convert a pattern into a regular expression'''
427 if kind == 're':
427 if kind == 're':
428 return name
428 return name
429 elif kind == 'path':
429 elif kind == 'path':
430 return '^' + re.escape(name) + '(?:/|$)'
430 return '^' + re.escape(name) + '(?:/|$)'
431 elif kind == 'relglob':
431 elif kind == 'relglob':
432 return head + globre(name, '(?:|.*/)', tail)
432 return head + globre(name, '(?:|.*/)', tail)
433 elif kind == 'relpath':
433 elif kind == 'relpath':
434 return head + re.escape(name) + tail
434 return head + re.escape(name) + tail
435 elif kind == 'relre':
435 elif kind == 'relre':
436 if name.startswith('^'):
436 if name.startswith('^'):
437 return name
437 return name
438 return '.*' + name
438 return '.*' + name
439 return head + globre(name, '', tail)
439 return head + globre(name, '', tail)
440
440
441 def matchfn(pats, tail):
441 def matchfn(pats, tail):
442 """build a matching function from a set of patterns"""
442 """build a matching function from a set of patterns"""
443 if not pats:
443 if not pats:
444 return
444 return
445 matches = []
445 matches = []
446 for k, p in pats:
446 for k, p in pats:
447 try:
447 try:
448 pat = '(?:%s)' % regex(k, p, tail)
448 pat = '(?:%s)' % regex(k, p, tail)
449 matches.append(re.compile(pat).match)
449 matches.append(re.compile(pat).match)
450 except re.error:
450 except re.error:
451 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p))
451 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p))
452 else: raise Abort("invalid pattern (%s): %s" % (k, p))
452 else: raise Abort("invalid pattern (%s): %s" % (k, p))
453
453
454 def buildfn(text):
454 def buildfn(text):
455 for m in matches:
455 for m in matches:
456 r = m(text)
456 r = m(text)
457 if r:
457 if r:
458 return r
458 return r
459
459
460 return buildfn
460 return buildfn
461
461
462 def globprefix(pat):
462 def globprefix(pat):
463 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
463 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
464 root = []
464 root = []
465 for p in pat.split(os.sep):
465 for p in pat.split(os.sep):
466 if contains_glob(p): break
466 if contains_glob(p): break
467 root.append(p)
467 root.append(p)
468 return '/'.join(root)
468 return '/'.join(root)
469
469
470 pats = []
470 pats = []
471 files = []
471 files = []
472 roots = []
472 roots = []
473 for kind, name in [patkind(p, dflt_pat) for p in names]:
473 for kind, name in [patkind(p, dflt_pat) for p in names]:
474 if kind in ('glob', 'relpath'):
474 if kind in ('glob', 'relpath'):
475 name = canonpath(canonroot, cwd, name)
475 name = canonpath(canonroot, cwd, name)
476 if name == '':
476 if name == '':
477 kind, name = 'glob', '**'
477 kind, name = 'glob', '**'
478 if kind in ('glob', 'path', 're'):
478 if kind in ('glob', 'path', 're'):
479 pats.append((kind, name))
479 pats.append((kind, name))
480 if kind == 'glob':
480 if kind == 'glob':
481 root = globprefix(name)
481 root = globprefix(name)
482 if root: roots.append(root)
482 if root: roots.append(root)
483 elif kind == 'relpath':
483 elif kind == 'relpath':
484 files.append((kind, name))
484 files.append((kind, name))
485 roots.append(name)
485 roots.append(name)
486
486
487 patmatch = matchfn(pats, '$') or always
487 patmatch = matchfn(pats, '$') or always
488 filematch = matchfn(files, '(?:/|$)') or always
488 filematch = matchfn(files, '(?:/|$)') or always
489 incmatch = always
489 incmatch = always
490 if inc:
490 if inc:
491 inckinds = [patkind(canonpath(canonroot, cwd, i)) for i in inc]
491 inckinds = [patkind(canonpath(canonroot, cwd, i)) for i in inc]
492 incmatch = matchfn(inckinds, '(?:/|$)')
492 incmatch = matchfn(inckinds, '(?:/|$)')
493 excmatch = lambda fn: False
493 excmatch = lambda fn: False
494 if exc:
494 if exc:
495 exckinds = [patkind(canonpath(canonroot, cwd, x)) for x in exc]
495 exckinds = [patkind(canonpath(canonroot, cwd, x)) for x in exc]
496 excmatch = matchfn(exckinds, '(?:/|$)')
496 excmatch = matchfn(exckinds, '(?:/|$)')
497
497
498 return (roots,
498 return (roots,
499 lambda fn: (incmatch(fn) and not excmatch(fn) and
499 lambda fn: (incmatch(fn) and not excmatch(fn) and
500 (fn.endswith('/') or
500 (fn.endswith('/') or
501 (not pats and not files) or
501 (not pats and not files) or
502 (pats and patmatch(fn)) or
502 (pats and patmatch(fn)) or
503 (files and filematch(fn)))),
503 (files and filematch(fn)))),
504 (inc or exc or (pats and pats != [('glob', '**')])) and True)
504 (inc or exc or (pats and pats != [('glob', '**')])) and True)
505
505
506 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
506 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
507 '''enhanced shell command execution.
507 '''enhanced shell command execution.
508 run with environment maybe modified, maybe in different dir.
508 run with environment maybe modified, maybe in different dir.
509
509
510 if command fails and onerr is None, return status. if ui object,
510 if command fails and onerr is None, return status. if ui object,
511 print error message and return status, else raise onerr object as
511 print error message and return status, else raise onerr object as
512 exception.'''
512 exception.'''
513 def py2shell(val):
513 def py2shell(val):
514 'convert python object into string that is useful to shell'
514 'convert python object into string that is useful to shell'
515 if val in (None, False):
515 if val in (None, False):
516 return '0'
516 return '0'
517 if val == True:
517 if val == True:
518 return '1'
518 return '1'
519 return str(val)
519 return str(val)
520 oldenv = {}
520 oldenv = {}
521 for k in environ:
521 for k in environ:
522 oldenv[k] = os.environ.get(k)
522 oldenv[k] = os.environ.get(k)
523 if cwd is not None:
523 if cwd is not None:
524 oldcwd = os.getcwd()
524 oldcwd = os.getcwd()
525 origcmd = cmd
525 origcmd = cmd
526 if os.name == 'nt':
526 if os.name == 'nt':
527 cmd = '"%s"' % cmd
527 cmd = '"%s"' % cmd
528 try:
528 try:
529 for k, v in environ.iteritems():
529 for k, v in environ.iteritems():
530 os.environ[k] = py2shell(v)
530 os.environ[k] = py2shell(v)
531 if cwd is not None and oldcwd != cwd:
531 if cwd is not None and oldcwd != cwd:
532 os.chdir(cwd)
532 os.chdir(cwd)
533 rc = os.system(cmd)
533 rc = os.system(cmd)
534 if rc and onerr:
534 if rc and onerr:
535 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
535 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
536 explain_exit(rc)[0])
536 explain_exit(rc)[0])
537 if errprefix:
537 if errprefix:
538 errmsg = '%s: %s' % (errprefix, errmsg)
538 errmsg = '%s: %s' % (errprefix, errmsg)
539 try:
539 try:
540 onerr.warn(errmsg + '\n')
540 onerr.warn(errmsg + '\n')
541 except AttributeError:
541 except AttributeError:
542 raise onerr(errmsg)
542 raise onerr(errmsg)
543 return rc
543 return rc
544 finally:
544 finally:
545 for k, v in oldenv.iteritems():
545 for k, v in oldenv.iteritems():
546 if v is None:
546 if v is None:
547 del os.environ[k]
547 del os.environ[k]
548 else:
548 else:
549 os.environ[k] = v
549 os.environ[k] = v
550 if cwd is not None and oldcwd != cwd:
550 if cwd is not None and oldcwd != cwd:
551 os.chdir(oldcwd)
551 os.chdir(oldcwd)
552
552
553 def rename(src, dst):
553 def rename(src, dst):
554 """forcibly rename a file"""
554 """forcibly rename a file"""
555 try:
555 try:
556 os.rename(src, dst)
556 os.rename(src, dst)
557 except OSError, err:
557 except OSError, err:
558 # on windows, rename to existing file is not allowed, so we
558 # on windows, rename to existing file is not allowed, so we
559 # must delete destination first. but if file is open, unlink
559 # must delete destination first. but if file is open, unlink
560 # schedules it for delete but does not delete it. rename
560 # schedules it for delete but does not delete it. rename
561 # happens immediately even for open files, so we create
561 # happens immediately even for open files, so we create
562 # temporary file, delete it, rename destination to that name,
562 # temporary file, delete it, rename destination to that name,
563 # then delete that. then rename is safe to do.
563 # then delete that. then rename is safe to do.
564 fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.')
564 fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.')
565 os.close(fd)
565 os.close(fd)
566 os.unlink(temp)
566 os.unlink(temp)
567 os.rename(dst, temp)
567 os.rename(dst, temp)
568 os.unlink(temp)
568 os.unlink(temp)
569 os.rename(src, dst)
569 os.rename(src, dst)
570
570
571 def unlink(f):
571 def unlink(f):
572 """unlink and remove the directory if it is empty"""
572 """unlink and remove the directory if it is empty"""
573 os.unlink(f)
573 os.unlink(f)
574 # try removing directories that might now be empty
574 # try removing directories that might now be empty
575 try:
575 try:
576 os.removedirs(os.path.dirname(f))
576 os.removedirs(os.path.dirname(f))
577 except OSError:
577 except OSError:
578 pass
578 pass
579
579
580 def copyfile(src, dest):
580 def copyfile(src, dest):
581 "copy a file, preserving mode"
581 "copy a file, preserving mode"
582 try:
582 try:
583 shutil.copyfile(src, dest)
583 shutil.copyfile(src, dest)
584 shutil.copymode(src, dest)
584 shutil.copymode(src, dest)
585 except shutil.Error, inst:
585 except shutil.Error, inst:
586 raise Abort(str(inst))
586 raise Abort(str(inst))
587
587
588 def copyfiles(src, dst, hardlink=None):
588 def copyfiles(src, dst, hardlink=None):
589 """Copy a directory tree using hardlinks if possible"""
589 """Copy a directory tree using hardlinks if possible"""
590
590
591 if hardlink is None:
591 if hardlink is None:
592 hardlink = (os.stat(src).st_dev ==
592 hardlink = (os.stat(src).st_dev ==
593 os.stat(os.path.dirname(dst)).st_dev)
593 os.stat(os.path.dirname(dst)).st_dev)
594
594
595 if os.path.isdir(src):
595 if os.path.isdir(src):
596 os.mkdir(dst)
596 os.mkdir(dst)
597 for name in os.listdir(src):
597 for name in os.listdir(src):
598 srcname = os.path.join(src, name)
598 srcname = os.path.join(src, name)
599 dstname = os.path.join(dst, name)
599 dstname = os.path.join(dst, name)
600 copyfiles(srcname, dstname, hardlink)
600 copyfiles(srcname, dstname, hardlink)
601 else:
601 else:
602 if hardlink:
602 if hardlink:
603 try:
603 try:
604 os_link(src, dst)
604 os_link(src, dst)
605 except (IOError, OSError):
605 except (IOError, OSError):
606 hardlink = False
606 hardlink = False
607 shutil.copy(src, dst)
607 shutil.copy(src, dst)
608 else:
608 else:
609 shutil.copy(src, dst)
609 shutil.copy(src, dst)
610
610
611 def audit_path(path):
611 def audit_path(path):
612 """Abort if path contains dangerous components"""
612 """Abort if path contains dangerous components"""
613 parts = os.path.normcase(path).split(os.sep)
613 parts = os.path.normcase(path).split(os.sep)
614 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '')
614 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '')
615 or os.pardir in parts):
615 or os.pardir in parts):
616 raise Abort(_("path contains illegal component: %s\n") % path)
616 raise Abort(_("path contains illegal component: %s\n") % path)
617
617
618 def _makelock_file(info, pathname):
618 def _makelock_file(info, pathname):
619 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
619 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
620 os.write(ld, info)
620 os.write(ld, info)
621 os.close(ld)
621 os.close(ld)
622
622
623 def _readlock_file(pathname):
623 def _readlock_file(pathname):
624 return posixfile(pathname).read()
624 return posixfile(pathname).read()
625
625
626 def nlinks(pathname):
626 def nlinks(pathname):
627 """Return number of hardlinks for the given file."""
627 """Return number of hardlinks for the given file."""
628 return os.lstat(pathname).st_nlink
628 return os.lstat(pathname).st_nlink
629
629
630 if hasattr(os, 'link'):
630 if hasattr(os, 'link'):
631 os_link = os.link
631 os_link = os.link
632 else:
632 else:
633 def os_link(src, dst):
633 def os_link(src, dst):
634 raise OSError(0, _("Hardlinks not supported"))
634 raise OSError(0, _("Hardlinks not supported"))
635
635
636 def fstat(fp):
636 def fstat(fp):
637 '''stat file object that may not have fileno method.'''
637 '''stat file object that may not have fileno method.'''
638 try:
638 try:
639 return os.fstat(fp.fileno())
639 return os.fstat(fp.fileno())
640 except AttributeError:
640 except AttributeError:
641 return os.stat(fp.name)
641 return os.stat(fp.name)
642
642
643 posixfile = file
643 posixfile = file
644
644
645 def is_win_9x():
645 def is_win_9x():
646 '''return true if run on windows 95, 98 or me.'''
646 '''return true if run on windows 95, 98 or me.'''
647 try:
647 try:
648 return sys.getwindowsversion()[3] == 1
648 return sys.getwindowsversion()[3] == 1
649 except AttributeError:
649 except AttributeError:
650 return os.name == 'nt' and 'command' in os.environ.get('comspec', '')
650 return os.name == 'nt' and 'command' in os.environ.get('comspec', '')
651
651
652 getuser_fallback = None
652 getuser_fallback = None
653
653
654 def getuser():
654 def getuser():
655 '''return name of current user'''
655 '''return name of current user'''
656 try:
656 try:
657 return getpass.getuser()
657 return getpass.getuser()
658 except ImportError:
658 except ImportError:
659 # import of pwd will fail on windows - try fallback
659 # import of pwd will fail on windows - try fallback
660 if getuser_fallback:
660 if getuser_fallback:
661 return getuser_fallback()
661 return getuser_fallback()
662 # raised if win32api not available
662 # raised if win32api not available
663 raise Abort(_('user name not available - set USERNAME '
663 raise Abort(_('user name not available - set USERNAME '
664 'environment variable'))
664 'environment variable'))
665
665
666 def username(uid=None):
666 def username(uid=None):
667 """Return the name of the user with the given uid.
667 """Return the name of the user with the given uid.
668
668
669 If uid is None, return the name of the current user."""
669 If uid is None, return the name of the current user."""
670 try:
670 try:
671 import pwd
671 import pwd
672 if uid is None:
672 if uid is None:
673 uid = os.getuid()
673 uid = os.getuid()
674 try:
674 try:
675 return pwd.getpwuid(uid)[0]
675 return pwd.getpwuid(uid)[0]
676 except KeyError:
676 except KeyError:
677 return str(uid)
677 return str(uid)
678 except ImportError:
678 except ImportError:
679 return None
679 return None
680
680
681 def groupname(gid=None):
681 def groupname(gid=None):
682 """Return the name of the group with the given gid.
682 """Return the name of the group with the given gid.
683
683
684 If gid is None, return the name of the current group."""
684 If gid is None, return the name of the current group."""
685 try:
685 try:
686 import grp
686 import grp
687 if gid is None:
687 if gid is None:
688 gid = os.getgid()
688 gid = os.getgid()
689 try:
689 try:
690 return grp.getgrgid(gid)[0]
690 return grp.getgrgid(gid)[0]
691 except KeyError:
691 except KeyError:
692 return str(gid)
692 return str(gid)
693 except ImportError:
693 except ImportError:
694 return None
694 return None
695
695
696 # File system features
696 # File system features
697
697
698 def checkfolding(path):
698 def checkfolding(path):
699 """
699 """
700 Check whether the given path is on a case-sensitive filesystem
700 Check whether the given path is on a case-sensitive filesystem
701
701
702 Requires a path (like /foo/.hg) ending with a foldable final
702 Requires a path (like /foo/.hg) ending with a foldable final
703 directory component.
703 directory component.
704 """
704 """
705 s1 = os.stat(path)
705 s1 = os.stat(path)
706 d, b = os.path.split(path)
706 d, b = os.path.split(path)
707 p2 = os.path.join(d, b.upper())
707 p2 = os.path.join(d, b.upper())
708 if path == p2:
708 if path == p2:
709 p2 = os.path.join(d, b.lower())
709 p2 = os.path.join(d, b.lower())
710 try:
710 try:
711 s2 = os.stat(p2)
711 s2 = os.stat(p2)
712 if s2 == s1:
712 if s2 == s1:
713 return False
713 return False
714 return True
714 return True
715 except:
715 except:
716 return True
716 return True
717
717
718 # Platform specific variants
718 # Platform specific variants
719 if os.name == 'nt':
719 if os.name == 'nt':
720 demandload(globals(), "msvcrt")
720 demandload(globals(), "msvcrt")
721 nulldev = 'NUL:'
721 nulldev = 'NUL:'
722
722
723 class winstdout:
723 class winstdout:
724 '''stdout on windows misbehaves if sent through a pipe'''
724 '''stdout on windows misbehaves if sent through a pipe'''
725
725
726 def __init__(self, fp):
726 def __init__(self, fp):
727 self.fp = fp
727 self.fp = fp
728
728
729 def __getattr__(self, key):
729 def __getattr__(self, key):
730 return getattr(self.fp, key)
730 return getattr(self.fp, key)
731
731
732 def close(self):
732 def close(self):
733 try:
733 try:
734 self.fp.close()
734 self.fp.close()
735 except: pass
735 except: pass
736
736
737 def write(self, s):
737 def write(self, s):
738 try:
738 try:
739 return self.fp.write(s)
739 return self.fp.write(s)
740 except IOError, inst:
740 except IOError, inst:
741 if inst.errno != 0: raise
741 if inst.errno != 0: raise
742 self.close()
742 self.close()
743 raise IOError(errno.EPIPE, 'Broken pipe')
743 raise IOError(errno.EPIPE, 'Broken pipe')
744
744
745 sys.stdout = winstdout(sys.stdout)
745 sys.stdout = winstdout(sys.stdout)
746
746
747 def system_rcpath():
747 def system_rcpath():
748 try:
748 try:
749 return system_rcpath_win32()
749 return system_rcpath_win32()
750 except:
750 except:
751 return [r'c:\mercurial\mercurial.ini']
751 return [r'c:\mercurial\mercurial.ini']
752
752
753 def os_rcpath():
753 def os_rcpath():
754 '''return default os-specific hgrc search path'''
754 '''return default os-specific hgrc search path'''
755 path = system_rcpath()
755 path = system_rcpath()
756 path.append(user_rcpath())
756 path.append(user_rcpath())
757 userprofile = os.environ.get('USERPROFILE')
757 userprofile = os.environ.get('USERPROFILE')
758 if userprofile:
758 if userprofile:
759 path.append(os.path.join(userprofile, 'mercurial.ini'))
759 path.append(os.path.join(userprofile, 'mercurial.ini'))
760 return path
760 return path
761
761
762 def user_rcpath():
762 def user_rcpath():
763 '''return os-specific hgrc search path to the user dir'''
763 '''return os-specific hgrc search path to the user dir'''
764 return os.path.join(os.path.expanduser('~'), 'mercurial.ini')
764 return os.path.join(os.path.expanduser('~'), 'mercurial.ini')
765
765
766 def parse_patch_output(output_line):
766 def parse_patch_output(output_line):
767 """parses the output produced by patch and returns the file name"""
767 """parses the output produced by patch and returns the file name"""
768 pf = output_line[14:]
768 pf = output_line[14:]
769 if pf[0] == '`':
769 if pf[0] == '`':
770 pf = pf[1:-1] # Remove the quotes
770 pf = pf[1:-1] # Remove the quotes
771 return pf
771 return pf
772
772
773 def testpid(pid):
773 def testpid(pid):
774 '''return False if pid dead, True if running or not known'''
774 '''return False if pid dead, True if running or not known'''
775 return True
775 return True
776
776
777 def is_exec(f, last):
777 def is_exec(f, last):
778 return last
778 return last
779
779
780 def set_exec(f, mode):
780 def set_exec(f, mode):
781 pass
781 pass
782
782
783 def set_binary(fd):
783 def set_binary(fd):
784 msvcrt.setmode(fd.fileno(), os.O_BINARY)
784 msvcrt.setmode(fd.fileno(), os.O_BINARY)
785
785
786 def pconvert(path):
786 def pconvert(path):
787 return path.replace("\\", "/")
787 return path.replace("\\", "/")
788
788
789 def localpath(path):
789 def localpath(path):
790 return path.replace('/', '\\')
790 return path.replace('/', '\\')
791
791
792 def normpath(path):
792 def normpath(path):
793 return pconvert(os.path.normpath(path))
793 return pconvert(os.path.normpath(path))
794
794
795 makelock = _makelock_file
795 makelock = _makelock_file
796 readlock = _readlock_file
796 readlock = _readlock_file
797
797
798 def samestat(s1, s2):
798 def samestat(s1, s2):
799 return False
799 return False
800
800
801 # A sequence of backslashes is special iff it precedes a double quote:
802 # - if there's an even number of backslashes, the double quote is not
803 # quoted (i.e. it ends the quoted region)
804 # - if there's an odd number of backslashes, the double quote is quoted
805 # - in both cases, every pair of backslashes is unquoted into a single
806 # backslash
807 # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx )
808 # So, to quote a string, we must surround it in double quotes, double
809 # the number of backslashes that preceed double quotes and add another
810 # backslash before every double quote (being careful with the double
811 # quote we've appended to the end)
812 _quotere = None
801 def shellquote(s):
813 def shellquote(s):
802 return '"%s"' % s.replace('"', '\\"')
814 global _quotere
815 if _quotere is None:
816 _quotere = re.compile(r'(\\*)("|\\$)')
817 return '"%s"' % _quotere.sub(r'\1\1\\\2', s)
803
818
804 def explain_exit(code):
819 def explain_exit(code):
805 return _("exited with status %d") % code, code
820 return _("exited with status %d") % code, code
806
821
807 # if you change this stub into a real check, please try to implement the
822 # if you change this stub into a real check, please try to implement the
808 # username and groupname functions above, too.
823 # username and groupname functions above, too.
809 def isowner(fp, st=None):
824 def isowner(fp, st=None):
810 return True
825 return True
811
826
812 try:
827 try:
813 # override functions with win32 versions if possible
828 # override functions with win32 versions if possible
814 from util_win32 import *
829 from util_win32 import *
815 if not is_win_9x():
830 if not is_win_9x():
816 posixfile = posixfile_nt
831 posixfile = posixfile_nt
817 except ImportError:
832 except ImportError:
818 pass
833 pass
819
834
820 else:
835 else:
821 nulldev = '/dev/null'
836 nulldev = '/dev/null'
822
837
823 def rcfiles(path):
838 def rcfiles(path):
824 rcs = [os.path.join(path, 'hgrc')]
839 rcs = [os.path.join(path, 'hgrc')]
825 rcdir = os.path.join(path, 'hgrc.d')
840 rcdir = os.path.join(path, 'hgrc.d')
826 try:
841 try:
827 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir)
842 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir)
828 if f.endswith(".rc")])
843 if f.endswith(".rc")])
829 except OSError:
844 except OSError:
830 pass
845 pass
831 return rcs
846 return rcs
832
847
833 def os_rcpath():
848 def os_rcpath():
834 '''return default os-specific hgrc search path'''
849 '''return default os-specific hgrc search path'''
835 path = []
850 path = []
836 # old mod_python does not set sys.argv
851 # old mod_python does not set sys.argv
837 if len(getattr(sys, 'argv', [])) > 0:
852 if len(getattr(sys, 'argv', [])) > 0:
838 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
853 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
839 '/../etc/mercurial'))
854 '/../etc/mercurial'))
840 path.extend(rcfiles('/etc/mercurial'))
855 path.extend(rcfiles('/etc/mercurial'))
841 path.append(os.path.expanduser('~/.hgrc'))
856 path.append(os.path.expanduser('~/.hgrc'))
842 path = [os.path.normpath(f) for f in path]
857 path = [os.path.normpath(f) for f in path]
843 return path
858 return path
844
859
845 def parse_patch_output(output_line):
860 def parse_patch_output(output_line):
846 """parses the output produced by patch and returns the file name"""
861 """parses the output produced by patch and returns the file name"""
847 pf = output_line[14:]
862 pf = output_line[14:]
848 if pf.startswith("'") and pf.endswith("'") and " " in pf:
863 if pf.startswith("'") and pf.endswith("'") and " " in pf:
849 pf = pf[1:-1] # Remove the quotes
864 pf = pf[1:-1] # Remove the quotes
850 return pf
865 return pf
851
866
852 def is_exec(f, last):
867 def is_exec(f, last):
853 """check whether a file is executable"""
868 """check whether a file is executable"""
854 return (os.lstat(f).st_mode & 0100 != 0)
869 return (os.lstat(f).st_mode & 0100 != 0)
855
870
856 def set_exec(f, mode):
871 def set_exec(f, mode):
857 s = os.lstat(f).st_mode
872 s = os.lstat(f).st_mode
858 if (s & 0100 != 0) == mode:
873 if (s & 0100 != 0) == mode:
859 return
874 return
860 if mode:
875 if mode:
861 # Turn on +x for every +r bit when making a file executable
876 # Turn on +x for every +r bit when making a file executable
862 # and obey umask.
877 # and obey umask.
863 umask = os.umask(0)
878 umask = os.umask(0)
864 os.umask(umask)
879 os.umask(umask)
865 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
880 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
866 else:
881 else:
867 os.chmod(f, s & 0666)
882 os.chmod(f, s & 0666)
868
883
869 def set_binary(fd):
884 def set_binary(fd):
870 pass
885 pass
871
886
872 def pconvert(path):
887 def pconvert(path):
873 return path
888 return path
874
889
875 def localpath(path):
890 def localpath(path):
876 return path
891 return path
877
892
878 normpath = os.path.normpath
893 normpath = os.path.normpath
879 samestat = os.path.samestat
894 samestat = os.path.samestat
880
895
881 def makelock(info, pathname):
896 def makelock(info, pathname):
882 try:
897 try:
883 os.symlink(info, pathname)
898 os.symlink(info, pathname)
884 except OSError, why:
899 except OSError, why:
885 if why.errno == errno.EEXIST:
900 if why.errno == errno.EEXIST:
886 raise
901 raise
887 else:
902 else:
888 _makelock_file(info, pathname)
903 _makelock_file(info, pathname)
889
904
890 def readlock(pathname):
905 def readlock(pathname):
891 try:
906 try:
892 return os.readlink(pathname)
907 return os.readlink(pathname)
893 except OSError, why:
908 except OSError, why:
894 if why.errno == errno.EINVAL:
909 if why.errno == errno.EINVAL:
895 return _readlock_file(pathname)
910 return _readlock_file(pathname)
896 else:
911 else:
897 raise
912 raise
898
913
899 def shellquote(s):
914 def shellquote(s):
900 return "'%s'" % s.replace("'", "'\\''")
915 return "'%s'" % s.replace("'", "'\\''")
901
916
902 def testpid(pid):
917 def testpid(pid):
903 '''return False if pid dead, True if running or not sure'''
918 '''return False if pid dead, True if running or not sure'''
904 try:
919 try:
905 os.kill(pid, 0)
920 os.kill(pid, 0)
906 return True
921 return True
907 except OSError, inst:
922 except OSError, inst:
908 return inst.errno != errno.ESRCH
923 return inst.errno != errno.ESRCH
909
924
910 def explain_exit(code):
925 def explain_exit(code):
911 """return a 2-tuple (desc, code) describing a process's status"""
926 """return a 2-tuple (desc, code) describing a process's status"""
912 if os.WIFEXITED(code):
927 if os.WIFEXITED(code):
913 val = os.WEXITSTATUS(code)
928 val = os.WEXITSTATUS(code)
914 return _("exited with status %d") % val, val
929 return _("exited with status %d") % val, val
915 elif os.WIFSIGNALED(code):
930 elif os.WIFSIGNALED(code):
916 val = os.WTERMSIG(code)
931 val = os.WTERMSIG(code)
917 return _("killed by signal %d") % val, val
932 return _("killed by signal %d") % val, val
918 elif os.WIFSTOPPED(code):
933 elif os.WIFSTOPPED(code):
919 val = os.WSTOPSIG(code)
934 val = os.WSTOPSIG(code)
920 return _("stopped by signal %d") % val, val
935 return _("stopped by signal %d") % val, val
921 raise ValueError(_("invalid exit code"))
936 raise ValueError(_("invalid exit code"))
922
937
923 def isowner(fp, st=None):
938 def isowner(fp, st=None):
924 """Return True if the file object f belongs to the current user.
939 """Return True if the file object f belongs to the current user.
925
940
926 The return value of a util.fstat(f) may be passed as the st argument.
941 The return value of a util.fstat(f) may be passed as the st argument.
927 """
942 """
928 if st is None:
943 if st is None:
929 st = fstat(fp)
944 st = fstat(fp)
930 return st.st_uid == os.getuid()
945 return st.st_uid == os.getuid()
931
946
932 def _buildencodefun():
947 def _buildencodefun():
933 e = '_'
948 e = '_'
934 win_reserved = [ord(x) for x in '\\:*?"<>|']
949 win_reserved = [ord(x) for x in '\\:*?"<>|']
935 cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
950 cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
936 for x in (range(32) + range(126, 256) + win_reserved):
951 for x in (range(32) + range(126, 256) + win_reserved):
937 cmap[chr(x)] = "~%02x" % x
952 cmap[chr(x)] = "~%02x" % x
938 for x in range(ord("A"), ord("Z")+1) + [ord(e)]:
953 for x in range(ord("A"), ord("Z")+1) + [ord(e)]:
939 cmap[chr(x)] = e + chr(x).lower()
954 cmap[chr(x)] = e + chr(x).lower()
940 dmap = {}
955 dmap = {}
941 for k, v in cmap.iteritems():
956 for k, v in cmap.iteritems():
942 dmap[v] = k
957 dmap[v] = k
943 def decode(s):
958 def decode(s):
944 i = 0
959 i = 0
945 while i < len(s):
960 while i < len(s):
946 for l in xrange(1, 4):
961 for l in xrange(1, 4):
947 try:
962 try:
948 yield dmap[s[i:i+l]]
963 yield dmap[s[i:i+l]]
949 i += l
964 i += l
950 break
965 break
951 except KeyError:
966 except KeyError:
952 pass
967 pass
953 else:
968 else:
954 raise KeyError
969 raise KeyError
955 return (lambda s: "".join([cmap[c] for c in s]),
970 return (lambda s: "".join([cmap[c] for c in s]),
956 lambda s: "".join(list(decode(s))))
971 lambda s: "".join(list(decode(s))))
957
972
958 encodefilename, decodefilename = _buildencodefun()
973 encodefilename, decodefilename = _buildencodefun()
959
974
960 def encodedopener(openerfn, fn):
975 def encodedopener(openerfn, fn):
961 def o(path, *args, **kw):
976 def o(path, *args, **kw):
962 return openerfn(fn(path), *args, **kw)
977 return openerfn(fn(path), *args, **kw)
963 return o
978 return o
964
979
965 def opener(base, audit=True):
980 def opener(base, audit=True):
966 """
981 """
967 return a function that opens files relative to base
982 return a function that opens files relative to base
968
983
969 this function is used to hide the details of COW semantics and
984 this function is used to hide the details of COW semantics and
970 remote file access from higher level code.
985 remote file access from higher level code.
971 """
986 """
972 p = base
987 p = base
973 audit_p = audit
988 audit_p = audit
974
989
975 def mktempcopy(name):
990 def mktempcopy(name):
976 d, fn = os.path.split(name)
991 d, fn = os.path.split(name)
977 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
992 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
978 os.close(fd)
993 os.close(fd)
979 ofp = posixfile(temp, "wb")
994 ofp = posixfile(temp, "wb")
980 try:
995 try:
981 try:
996 try:
982 ifp = posixfile(name, "rb")
997 ifp = posixfile(name, "rb")
983 except IOError, inst:
998 except IOError, inst:
984 if not getattr(inst, 'filename', None):
999 if not getattr(inst, 'filename', None):
985 inst.filename = name
1000 inst.filename = name
986 raise
1001 raise
987 for chunk in filechunkiter(ifp):
1002 for chunk in filechunkiter(ifp):
988 ofp.write(chunk)
1003 ofp.write(chunk)
989 ifp.close()
1004 ifp.close()
990 ofp.close()
1005 ofp.close()
991 except:
1006 except:
992 try: os.unlink(temp)
1007 try: os.unlink(temp)
993 except: pass
1008 except: pass
994 raise
1009 raise
995 st = os.lstat(name)
1010 st = os.lstat(name)
996 os.chmod(temp, st.st_mode)
1011 os.chmod(temp, st.st_mode)
997 return temp
1012 return temp
998
1013
999 class atomictempfile(posixfile):
1014 class atomictempfile(posixfile):
1000 """the file will only be copied when rename is called"""
1015 """the file will only be copied when rename is called"""
1001 def __init__(self, name, mode):
1016 def __init__(self, name, mode):
1002 self.__name = name
1017 self.__name = name
1003 self.temp = mktempcopy(name)
1018 self.temp = mktempcopy(name)
1004 posixfile.__init__(self, self.temp, mode)
1019 posixfile.__init__(self, self.temp, mode)
1005 def rename(self):
1020 def rename(self):
1006 if not self.closed:
1021 if not self.closed:
1007 posixfile.close(self)
1022 posixfile.close(self)
1008 rename(self.temp, localpath(self.__name))
1023 rename(self.temp, localpath(self.__name))
1009 def __del__(self):
1024 def __del__(self):
1010 if not self.closed:
1025 if not self.closed:
1011 try:
1026 try:
1012 os.unlink(self.temp)
1027 os.unlink(self.temp)
1013 except: pass
1028 except: pass
1014 posixfile.close(self)
1029 posixfile.close(self)
1015
1030
1016 class atomicfile(atomictempfile):
1031 class atomicfile(atomictempfile):
1017 """the file will only be copied on close"""
1032 """the file will only be copied on close"""
1018 def __init__(self, name, mode):
1033 def __init__(self, name, mode):
1019 atomictempfile.__init__(self, name, mode)
1034 atomictempfile.__init__(self, name, mode)
1020 def close(self):
1035 def close(self):
1021 self.rename()
1036 self.rename()
1022 def __del__(self):
1037 def __del__(self):
1023 self.rename()
1038 self.rename()
1024
1039
1025 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
1040 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
1026 if audit_p:
1041 if audit_p:
1027 audit_path(path)
1042 audit_path(path)
1028 f = os.path.join(p, path)
1043 f = os.path.join(p, path)
1029
1044
1030 if not text:
1045 if not text:
1031 mode += "b" # for that other OS
1046 mode += "b" # for that other OS
1032
1047
1033 if mode[0] != "r":
1048 if mode[0] != "r":
1034 try:
1049 try:
1035 nlink = nlinks(f)
1050 nlink = nlinks(f)
1036 except OSError:
1051 except OSError:
1037 d = os.path.dirname(f)
1052 d = os.path.dirname(f)
1038 if not os.path.isdir(d):
1053 if not os.path.isdir(d):
1039 os.makedirs(d)
1054 os.makedirs(d)
1040 else:
1055 else:
1041 if atomic:
1056 if atomic:
1042 return atomicfile(f, mode)
1057 return atomicfile(f, mode)
1043 elif atomictemp:
1058 elif atomictemp:
1044 return atomictempfile(f, mode)
1059 return atomictempfile(f, mode)
1045 if nlink > 1:
1060 if nlink > 1:
1046 rename(mktempcopy(f), f)
1061 rename(mktempcopy(f), f)
1047 return posixfile(f, mode)
1062 return posixfile(f, mode)
1048
1063
1049 return o
1064 return o
1050
1065
1051 class chunkbuffer(object):
1066 class chunkbuffer(object):
1052 """Allow arbitrary sized chunks of data to be efficiently read from an
1067 """Allow arbitrary sized chunks of data to be efficiently read from an
1053 iterator over chunks of arbitrary size."""
1068 iterator over chunks of arbitrary size."""
1054
1069
1055 def __init__(self, in_iter, targetsize = 2**16):
1070 def __init__(self, in_iter, targetsize = 2**16):
1056 """in_iter is the iterator that's iterating over the input chunks.
1071 """in_iter is the iterator that's iterating over the input chunks.
1057 targetsize is how big a buffer to try to maintain."""
1072 targetsize is how big a buffer to try to maintain."""
1058 self.in_iter = iter(in_iter)
1073 self.in_iter = iter(in_iter)
1059 self.buf = ''
1074 self.buf = ''
1060 self.targetsize = int(targetsize)
1075 self.targetsize = int(targetsize)
1061 if self.targetsize <= 0:
1076 if self.targetsize <= 0:
1062 raise ValueError(_("targetsize must be greater than 0, was %d") %
1077 raise ValueError(_("targetsize must be greater than 0, was %d") %
1063 targetsize)
1078 targetsize)
1064 self.iterempty = False
1079 self.iterempty = False
1065
1080
1066 def fillbuf(self):
1081 def fillbuf(self):
1067 """Ignore target size; read every chunk from iterator until empty."""
1082 """Ignore target size; read every chunk from iterator until empty."""
1068 if not self.iterempty:
1083 if not self.iterempty:
1069 collector = cStringIO.StringIO()
1084 collector = cStringIO.StringIO()
1070 collector.write(self.buf)
1085 collector.write(self.buf)
1071 for ch in self.in_iter:
1086 for ch in self.in_iter:
1072 collector.write(ch)
1087 collector.write(ch)
1073 self.buf = collector.getvalue()
1088 self.buf = collector.getvalue()
1074 self.iterempty = True
1089 self.iterempty = True
1075
1090
1076 def read(self, l):
1091 def read(self, l):
1077 """Read L bytes of data from the iterator of chunks of data.
1092 """Read L bytes of data from the iterator of chunks of data.
1078 Returns less than L bytes if the iterator runs dry."""
1093 Returns less than L bytes if the iterator runs dry."""
1079 if l > len(self.buf) and not self.iterempty:
1094 if l > len(self.buf) and not self.iterempty:
1080 # Clamp to a multiple of self.targetsize
1095 # Clamp to a multiple of self.targetsize
1081 targetsize = self.targetsize * ((l // self.targetsize) + 1)
1096 targetsize = self.targetsize * ((l // self.targetsize) + 1)
1082 collector = cStringIO.StringIO()
1097 collector = cStringIO.StringIO()
1083 collector.write(self.buf)
1098 collector.write(self.buf)
1084 collected = len(self.buf)
1099 collected = len(self.buf)
1085 for chunk in self.in_iter:
1100 for chunk in self.in_iter:
1086 collector.write(chunk)
1101 collector.write(chunk)
1087 collected += len(chunk)
1102 collected += len(chunk)
1088 if collected >= targetsize:
1103 if collected >= targetsize:
1089 break
1104 break
1090 if collected < targetsize:
1105 if collected < targetsize:
1091 self.iterempty = True
1106 self.iterempty = True
1092 self.buf = collector.getvalue()
1107 self.buf = collector.getvalue()
1093 s, self.buf = self.buf[:l], buffer(self.buf, l)
1108 s, self.buf = self.buf[:l], buffer(self.buf, l)
1094 return s
1109 return s
1095
1110
1096 def filechunkiter(f, size=65536, limit=None):
1111 def filechunkiter(f, size=65536, limit=None):
1097 """Create a generator that produces the data in the file size
1112 """Create a generator that produces the data in the file size
1098 (default 65536) bytes at a time, up to optional limit (default is
1113 (default 65536) bytes at a time, up to optional limit (default is
1099 to read all data). Chunks may be less than size bytes if the
1114 to read all data). Chunks may be less than size bytes if the
1100 chunk is the last chunk in the file, or the file is a socket or
1115 chunk is the last chunk in the file, or the file is a socket or
1101 some other type of file that sometimes reads less data than is
1116 some other type of file that sometimes reads less data than is
1102 requested."""
1117 requested."""
1103 assert size >= 0
1118 assert size >= 0
1104 assert limit is None or limit >= 0
1119 assert limit is None or limit >= 0
1105 while True:
1120 while True:
1106 if limit is None: nbytes = size
1121 if limit is None: nbytes = size
1107 else: nbytes = min(limit, size)
1122 else: nbytes = min(limit, size)
1108 s = nbytes and f.read(nbytes)
1123 s = nbytes and f.read(nbytes)
1109 if not s: break
1124 if not s: break
1110 if limit: limit -= len(s)
1125 if limit: limit -= len(s)
1111 yield s
1126 yield s
1112
1127
1113 def makedate():
1128 def makedate():
1114 lt = time.localtime()
1129 lt = time.localtime()
1115 if lt[8] == 1 and time.daylight:
1130 if lt[8] == 1 and time.daylight:
1116 tz = time.altzone
1131 tz = time.altzone
1117 else:
1132 else:
1118 tz = time.timezone
1133 tz = time.timezone
1119 return time.mktime(lt), tz
1134 return time.mktime(lt), tz
1120
1135
1121 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True):
1136 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True):
1122 """represent a (unixtime, offset) tuple as a localized time.
1137 """represent a (unixtime, offset) tuple as a localized time.
1123 unixtime is seconds since the epoch, and offset is the time zone's
1138 unixtime is seconds since the epoch, and offset is the time zone's
1124 number of seconds away from UTC. if timezone is false, do not
1139 number of seconds away from UTC. if timezone is false, do not
1125 append time zone to string."""
1140 append time zone to string."""
1126 t, tz = date or makedate()
1141 t, tz = date or makedate()
1127 s = time.strftime(format, time.gmtime(float(t) - tz))
1142 s = time.strftime(format, time.gmtime(float(t) - tz))
1128 if timezone:
1143 if timezone:
1129 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
1144 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
1130 return s
1145 return s
1131
1146
1132 def strdate(string, format, defaults):
1147 def strdate(string, format, defaults):
1133 """parse a localized time string and return a (unixtime, offset) tuple.
1148 """parse a localized time string and return a (unixtime, offset) tuple.
1134 if the string cannot be parsed, ValueError is raised."""
1149 if the string cannot be parsed, ValueError is raised."""
1135 def timezone(string):
1150 def timezone(string):
1136 tz = string.split()[-1]
1151 tz = string.split()[-1]
1137 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1152 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1138 tz = int(tz)
1153 tz = int(tz)
1139 offset = - 3600 * (tz / 100) - 60 * (tz % 100)
1154 offset = - 3600 * (tz / 100) - 60 * (tz % 100)
1140 return offset
1155 return offset
1141 if tz == "GMT" or tz == "UTC":
1156 if tz == "GMT" or tz == "UTC":
1142 return 0
1157 return 0
1143 return None
1158 return None
1144
1159
1145 # NOTE: unixtime = localunixtime + offset
1160 # NOTE: unixtime = localunixtime + offset
1146 offset, date = timezone(string), string
1161 offset, date = timezone(string), string
1147 if offset != None:
1162 if offset != None:
1148 date = " ".join(string.split()[:-1])
1163 date = " ".join(string.split()[:-1])
1149
1164
1150 # add missing elements from defaults
1165 # add missing elements from defaults
1151 for part in defaults:
1166 for part in defaults:
1152 found = [True for p in part if ("%"+p) in format]
1167 found = [True for p in part if ("%"+p) in format]
1153 if not found:
1168 if not found:
1154 date += "@" + defaults[part]
1169 date += "@" + defaults[part]
1155 format += "@%" + part[0]
1170 format += "@%" + part[0]
1156
1171
1157 timetuple = time.strptime(date, format)
1172 timetuple = time.strptime(date, format)
1158 localunixtime = int(calendar.timegm(timetuple))
1173 localunixtime = int(calendar.timegm(timetuple))
1159 if offset is None:
1174 if offset is None:
1160 # local timezone
1175 # local timezone
1161 unixtime = int(time.mktime(timetuple))
1176 unixtime = int(time.mktime(timetuple))
1162 offset = unixtime - localunixtime
1177 offset = unixtime - localunixtime
1163 else:
1178 else:
1164 unixtime = localunixtime + offset
1179 unixtime = localunixtime + offset
1165 return unixtime, offset
1180 return unixtime, offset
1166
1181
1167 def parsedate(string, formats=None, defaults=None):
1182 def parsedate(string, formats=None, defaults=None):
1168 """parse a localized time string and return a (unixtime, offset) tuple.
1183 """parse a localized time string and return a (unixtime, offset) tuple.
1169 The date may be a "unixtime offset" string or in one of the specified
1184 The date may be a "unixtime offset" string or in one of the specified
1170 formats."""
1185 formats."""
1171 if not string:
1186 if not string:
1172 return 0, 0
1187 return 0, 0
1173 if not formats:
1188 if not formats:
1174 formats = defaultdateformats
1189 formats = defaultdateformats
1175 string = string.strip()
1190 string = string.strip()
1176 try:
1191 try:
1177 when, offset = map(int, string.split(' '))
1192 when, offset = map(int, string.split(' '))
1178 except ValueError:
1193 except ValueError:
1179 # fill out defaults
1194 # fill out defaults
1180 if not defaults:
1195 if not defaults:
1181 defaults = {}
1196 defaults = {}
1182 now = makedate()
1197 now = makedate()
1183 for part in "d mb yY HI M S".split():
1198 for part in "d mb yY HI M S".split():
1184 if part not in defaults:
1199 if part not in defaults:
1185 if part[0] in "HMS":
1200 if part[0] in "HMS":
1186 defaults[part] = "00"
1201 defaults[part] = "00"
1187 elif part[0] in "dm":
1202 elif part[0] in "dm":
1188 defaults[part] = "1"
1203 defaults[part] = "1"
1189 else:
1204 else:
1190 defaults[part] = datestr(now, "%" + part[0], False)
1205 defaults[part] = datestr(now, "%" + part[0], False)
1191
1206
1192 for format in formats:
1207 for format in formats:
1193 try:
1208 try:
1194 when, offset = strdate(string, format, defaults)
1209 when, offset = strdate(string, format, defaults)
1195 except ValueError:
1210 except ValueError:
1196 pass
1211 pass
1197 else:
1212 else:
1198 break
1213 break
1199 else:
1214 else:
1200 raise Abort(_('invalid date: %r ') % string)
1215 raise Abort(_('invalid date: %r ') % string)
1201 # validate explicit (probably user-specified) date and
1216 # validate explicit (probably user-specified) date and
1202 # time zone offset. values must fit in signed 32 bits for
1217 # time zone offset. values must fit in signed 32 bits for
1203 # current 32-bit linux runtimes. timezones go from UTC-12
1218 # current 32-bit linux runtimes. timezones go from UTC-12
1204 # to UTC+14
1219 # to UTC+14
1205 if abs(when) > 0x7fffffff:
1220 if abs(when) > 0x7fffffff:
1206 raise Abort(_('date exceeds 32 bits: %d') % when)
1221 raise Abort(_('date exceeds 32 bits: %d') % when)
1207 if offset < -50400 or offset > 43200:
1222 if offset < -50400 or offset > 43200:
1208 raise Abort(_('impossible time zone offset: %d') % offset)
1223 raise Abort(_('impossible time zone offset: %d') % offset)
1209 return when, offset
1224 return when, offset
1210
1225
1211 def matchdate(date):
1226 def matchdate(date):
1212 """Return a function that matches a given date match specifier
1227 """Return a function that matches a given date match specifier
1213
1228
1214 Formats include:
1229 Formats include:
1215
1230
1216 '{date}' match a given date to the accuracy provided
1231 '{date}' match a given date to the accuracy provided
1217
1232
1218 '<{date}' on or before a given date
1233 '<{date}' on or before a given date
1219
1234
1220 '>{date}' on or after a given date
1235 '>{date}' on or after a given date
1221
1236
1222 """
1237 """
1223
1238
1224 def lower(date):
1239 def lower(date):
1225 return parsedate(date, extendeddateformats)[0]
1240 return parsedate(date, extendeddateformats)[0]
1226
1241
1227 def upper(date):
1242 def upper(date):
1228 d = dict(mb="12", HI="23", M="59", S="59")
1243 d = dict(mb="12", HI="23", M="59", S="59")
1229 for days in "31 30 29".split():
1244 for days in "31 30 29".split():
1230 try:
1245 try:
1231 d["d"] = days
1246 d["d"] = days
1232 return parsedate(date, extendeddateformats, d)[0]
1247 return parsedate(date, extendeddateformats, d)[0]
1233 except:
1248 except:
1234 pass
1249 pass
1235 d["d"] = "28"
1250 d["d"] = "28"
1236 return parsedate(date, extendeddateformats, d)[0]
1251 return parsedate(date, extendeddateformats, d)[0]
1237
1252
1238 if date[0] == "<":
1253 if date[0] == "<":
1239 when = upper(date[1:])
1254 when = upper(date[1:])
1240 return lambda x: x <= when
1255 return lambda x: x <= when
1241 elif date[0] == ">":
1256 elif date[0] == ">":
1242 when = lower(date[1:])
1257 when = lower(date[1:])
1243 return lambda x: x >= when
1258 return lambda x: x >= when
1244 elif date[0] == "-":
1259 elif date[0] == "-":
1245 try:
1260 try:
1246 days = int(date[1:])
1261 days = int(date[1:])
1247 except ValueError:
1262 except ValueError:
1248 raise Abort(_("invalid day spec: %s") % date[1:])
1263 raise Abort(_("invalid day spec: %s") % date[1:])
1249 when = makedate()[0] - days * 3600 * 24
1264 when = makedate()[0] - days * 3600 * 24
1250 return lambda x: x >= when
1265 return lambda x: x >= when
1251 elif " to " in date:
1266 elif " to " in date:
1252 a, b = date.split(" to ")
1267 a, b = date.split(" to ")
1253 start, stop = lower(a), upper(b)
1268 start, stop = lower(a), upper(b)
1254 return lambda x: x >= start and x <= stop
1269 return lambda x: x >= start and x <= stop
1255 else:
1270 else:
1256 start, stop = lower(date), upper(date)
1271 start, stop = lower(date), upper(date)
1257 return lambda x: x >= start and x <= stop
1272 return lambda x: x >= start and x <= stop
1258
1273
1259 def shortuser(user):
1274 def shortuser(user):
1260 """Return a short representation of a user name or email address."""
1275 """Return a short representation of a user name or email address."""
1261 f = user.find('@')
1276 f = user.find('@')
1262 if f >= 0:
1277 if f >= 0:
1263 user = user[:f]
1278 user = user[:f]
1264 f = user.find('<')
1279 f = user.find('<')
1265 if f >= 0:
1280 if f >= 0:
1266 user = user[f+1:]
1281 user = user[f+1:]
1267 f = user.find(' ')
1282 f = user.find(' ')
1268 if f >= 0:
1283 if f >= 0:
1269 user = user[:f]
1284 user = user[:f]
1270 f = user.find('.')
1285 f = user.find('.')
1271 if f >= 0:
1286 if f >= 0:
1272 user = user[:f]
1287 user = user[:f]
1273 return user
1288 return user
1274
1289
1275 def ellipsis(text, maxlength=400):
1290 def ellipsis(text, maxlength=400):
1276 """Trim string to at most maxlength (default: 400) characters."""
1291 """Trim string to at most maxlength (default: 400) characters."""
1277 if len(text) <= maxlength:
1292 if len(text) <= maxlength:
1278 return text
1293 return text
1279 else:
1294 else:
1280 return "%s..." % (text[:maxlength-3])
1295 return "%s..." % (text[:maxlength-3])
1281
1296
1282 def walkrepos(path):
1297 def walkrepos(path):
1283 '''yield every hg repository under path, recursively.'''
1298 '''yield every hg repository under path, recursively.'''
1284 def errhandler(err):
1299 def errhandler(err):
1285 if err.filename == path:
1300 if err.filename == path:
1286 raise err
1301 raise err
1287
1302
1288 for root, dirs, files in os.walk(path, onerror=errhandler):
1303 for root, dirs, files in os.walk(path, onerror=errhandler):
1289 for d in dirs:
1304 for d in dirs:
1290 if d == '.hg':
1305 if d == '.hg':
1291 yield root
1306 yield root
1292 dirs[:] = []
1307 dirs[:] = []
1293 break
1308 break
1294
1309
1295 _rcpath = None
1310 _rcpath = None
1296
1311
1297 def rcpath():
1312 def rcpath():
1298 '''return hgrc search path. if env var HGRCPATH is set, use it.
1313 '''return hgrc search path. if env var HGRCPATH is set, use it.
1299 for each item in path, if directory, use files ending in .rc,
1314 for each item in path, if directory, use files ending in .rc,
1300 else use item.
1315 else use item.
1301 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1316 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1302 if no HGRCPATH, use default os-specific path.'''
1317 if no HGRCPATH, use default os-specific path.'''
1303 global _rcpath
1318 global _rcpath
1304 if _rcpath is None:
1319 if _rcpath is None:
1305 if 'HGRCPATH' in os.environ:
1320 if 'HGRCPATH' in os.environ:
1306 _rcpath = []
1321 _rcpath = []
1307 for p in os.environ['HGRCPATH'].split(os.pathsep):
1322 for p in os.environ['HGRCPATH'].split(os.pathsep):
1308 if not p: continue
1323 if not p: continue
1309 if os.path.isdir(p):
1324 if os.path.isdir(p):
1310 for f in os.listdir(p):
1325 for f in os.listdir(p):
1311 if f.endswith('.rc'):
1326 if f.endswith('.rc'):
1312 _rcpath.append(os.path.join(p, f))
1327 _rcpath.append(os.path.join(p, f))
1313 else:
1328 else:
1314 _rcpath.append(p)
1329 _rcpath.append(p)
1315 else:
1330 else:
1316 _rcpath = os_rcpath()
1331 _rcpath = os_rcpath()
1317 return _rcpath
1332 return _rcpath
1318
1333
1319 def bytecount(nbytes):
1334 def bytecount(nbytes):
1320 '''return byte count formatted as readable string, with units'''
1335 '''return byte count formatted as readable string, with units'''
1321
1336
1322 units = (
1337 units = (
1323 (100, 1<<30, _('%.0f GB')),
1338 (100, 1<<30, _('%.0f GB')),
1324 (10, 1<<30, _('%.1f GB')),
1339 (10, 1<<30, _('%.1f GB')),
1325 (1, 1<<30, _('%.2f GB')),
1340 (1, 1<<30, _('%.2f GB')),
1326 (100, 1<<20, _('%.0f MB')),
1341 (100, 1<<20, _('%.0f MB')),
1327 (10, 1<<20, _('%.1f MB')),
1342 (10, 1<<20, _('%.1f MB')),
1328 (1, 1<<20, _('%.2f MB')),
1343 (1, 1<<20, _('%.2f MB')),
1329 (100, 1<<10, _('%.0f KB')),
1344 (100, 1<<10, _('%.0f KB')),
1330 (10, 1<<10, _('%.1f KB')),
1345 (10, 1<<10, _('%.1f KB')),
1331 (1, 1<<10, _('%.2f KB')),
1346 (1, 1<<10, _('%.2f KB')),
1332 (1, 1, _('%.0f bytes')),
1347 (1, 1, _('%.0f bytes')),
1333 )
1348 )
1334
1349
1335 for multiplier, divisor, format in units:
1350 for multiplier, divisor, format in units:
1336 if nbytes >= divisor * multiplier:
1351 if nbytes >= divisor * multiplier:
1337 return format % (nbytes / float(divisor))
1352 return format % (nbytes / float(divisor))
1338 return units[-1][2] % nbytes
1353 return units[-1][2] % nbytes
1339
1354
1340 def drop_scheme(scheme, path):
1355 def drop_scheme(scheme, path):
1341 sc = scheme + ':'
1356 sc = scheme + ':'
1342 if path.startswith(sc):
1357 if path.startswith(sc):
1343 path = path[len(sc):]
1358 path = path[len(sc):]
1344 if path.startswith('//'):
1359 if path.startswith('//'):
1345 path = path[2:]
1360 path = path[2:]
1346 return path
1361 return path
General Comments 0
You need to be logged in to leave comments. Login now