##// END OF EJS Templates
Fix wrong module reference in copyfile exception
Andrei Vermel -
r4067:c620376b default
parent child Browse files
Show More
@@ -1,1343 +1,1343
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 rel.reverse()
366 rel.reverse()
367 name = os.path.join(*rel)
367 name = os.path.join(*rel)
368 audit_path(name)
368 audit_path(name)
369 return pconvert(name)
369 return pconvert(name)
370 dirname, basename = os.path.split(name)
370 dirname, basename = os.path.split(name)
371 rel.append(basename)
371 rel.append(basename)
372 if dirname == name:
372 if dirname == name:
373 break
373 break
374 name = dirname
374 name = dirname
375
375
376 raise Abort('%s not under root' % myname)
376 raise Abort('%s not under root' % myname)
377
377
378 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
378 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
379 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src)
379 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src)
380
380
381 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='',
381 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='',
382 src=None, globbed=False):
382 src=None, globbed=False):
383 if not globbed:
383 if not globbed:
384 names = expand_glob(names)
384 names = expand_glob(names)
385 return _matcher(canonroot, cwd, names, inc, exc, head, 'relpath', src)
385 return _matcher(canonroot, cwd, names, inc, exc, head, 'relpath', src)
386
386
387 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src):
387 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src):
388 """build a function to match a set of file patterns
388 """build a function to match a set of file patterns
389
389
390 arguments:
390 arguments:
391 canonroot - the canonical root of the tree you're matching against
391 canonroot - the canonical root of the tree you're matching against
392 cwd - the current working directory, if relevant
392 cwd - the current working directory, if relevant
393 names - patterns to find
393 names - patterns to find
394 inc - patterns to include
394 inc - patterns to include
395 exc - patterns to exclude
395 exc - patterns to exclude
396 head - a regex to prepend to patterns to control whether a match is rooted
396 head - a regex to prepend to patterns to control whether a match is rooted
397
397
398 a pattern is one of:
398 a pattern is one of:
399 'glob:<rooted glob>'
399 'glob:<rooted glob>'
400 're:<rooted regexp>'
400 're:<rooted regexp>'
401 'path:<rooted path>'
401 'path:<rooted path>'
402 'relglob:<relative glob>'
402 'relglob:<relative glob>'
403 'relpath:<relative path>'
403 'relpath:<relative path>'
404 'relre:<relative regexp>'
404 'relre:<relative regexp>'
405 '<rooted path or regexp>'
405 '<rooted path or regexp>'
406
406
407 returns:
407 returns:
408 a 3-tuple containing
408 a 3-tuple containing
409 - list of explicit non-pattern names passed in
409 - list of explicit non-pattern names passed in
410 - a bool match(filename) function
410 - a bool match(filename) function
411 - a bool indicating if any patterns were passed in
411 - a bool indicating if any patterns were passed in
412
412
413 todo:
413 todo:
414 make head regex a rooted bool
414 make head regex a rooted bool
415 """
415 """
416
416
417 def contains_glob(name):
417 def contains_glob(name):
418 for c in name:
418 for c in name:
419 if c in _globchars: return True
419 if c in _globchars: return True
420 return False
420 return False
421
421
422 def regex(kind, name, tail):
422 def regex(kind, name, tail):
423 '''convert a pattern into a regular expression'''
423 '''convert a pattern into a regular expression'''
424 if kind == 're':
424 if kind == 're':
425 return name
425 return name
426 elif kind == 'path':
426 elif kind == 'path':
427 return '^' + re.escape(name) + '(?:/|$)'
427 return '^' + re.escape(name) + '(?:/|$)'
428 elif kind == 'relglob':
428 elif kind == 'relglob':
429 return head + globre(name, '(?:|.*/)', tail)
429 return head + globre(name, '(?:|.*/)', tail)
430 elif kind == 'relpath':
430 elif kind == 'relpath':
431 return head + re.escape(name) + tail
431 return head + re.escape(name) + tail
432 elif kind == 'relre':
432 elif kind == 'relre':
433 if name.startswith('^'):
433 if name.startswith('^'):
434 return name
434 return name
435 return '.*' + name
435 return '.*' + name
436 return head + globre(name, '', tail)
436 return head + globre(name, '', tail)
437
437
438 def matchfn(pats, tail):
438 def matchfn(pats, tail):
439 """build a matching function from a set of patterns"""
439 """build a matching function from a set of patterns"""
440 if not pats:
440 if not pats:
441 return
441 return
442 matches = []
442 matches = []
443 for k, p in pats:
443 for k, p in pats:
444 try:
444 try:
445 pat = '(?:%s)' % regex(k, p, tail)
445 pat = '(?:%s)' % regex(k, p, tail)
446 matches.append(re.compile(pat).match)
446 matches.append(re.compile(pat).match)
447 except re.error:
447 except re.error:
448 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p))
448 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p))
449 else: raise Abort("invalid pattern (%s): %s" % (k, p))
449 else: raise Abort("invalid pattern (%s): %s" % (k, p))
450
450
451 def buildfn(text):
451 def buildfn(text):
452 for m in matches:
452 for m in matches:
453 r = m(text)
453 r = m(text)
454 if r:
454 if r:
455 return r
455 return r
456
456
457 return buildfn
457 return buildfn
458
458
459 def globprefix(pat):
459 def globprefix(pat):
460 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
460 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
461 root = []
461 root = []
462 for p in pat.split(os.sep):
462 for p in pat.split(os.sep):
463 if contains_glob(p): break
463 if contains_glob(p): break
464 root.append(p)
464 root.append(p)
465 return '/'.join(root)
465 return '/'.join(root)
466
466
467 pats = []
467 pats = []
468 files = []
468 files = []
469 roots = []
469 roots = []
470 for kind, name in [patkind(p, dflt_pat) for p in names]:
470 for kind, name in [patkind(p, dflt_pat) for p in names]:
471 if kind in ('glob', 'relpath'):
471 if kind in ('glob', 'relpath'):
472 name = canonpath(canonroot, cwd, name)
472 name = canonpath(canonroot, cwd, name)
473 if name == '':
473 if name == '':
474 kind, name = 'glob', '**'
474 kind, name = 'glob', '**'
475 if kind in ('glob', 'path', 're'):
475 if kind in ('glob', 'path', 're'):
476 pats.append((kind, name))
476 pats.append((kind, name))
477 if kind == 'glob':
477 if kind == 'glob':
478 root = globprefix(name)
478 root = globprefix(name)
479 if root: roots.append(root)
479 if root: roots.append(root)
480 elif kind == 'relpath':
480 elif kind == 'relpath':
481 files.append((kind, name))
481 files.append((kind, name))
482 roots.append(name)
482 roots.append(name)
483
483
484 patmatch = matchfn(pats, '$') or always
484 patmatch = matchfn(pats, '$') or always
485 filematch = matchfn(files, '(?:/|$)') or always
485 filematch = matchfn(files, '(?:/|$)') or always
486 incmatch = always
486 incmatch = always
487 if inc:
487 if inc:
488 inckinds = [patkind(canonpath(canonroot, cwd, i)) for i in inc]
488 inckinds = [patkind(canonpath(canonroot, cwd, i)) for i in inc]
489 incmatch = matchfn(inckinds, '(?:/|$)')
489 incmatch = matchfn(inckinds, '(?:/|$)')
490 excmatch = lambda fn: False
490 excmatch = lambda fn: False
491 if exc:
491 if exc:
492 exckinds = [patkind(canonpath(canonroot, cwd, x)) for x in exc]
492 exckinds = [patkind(canonpath(canonroot, cwd, x)) for x in exc]
493 excmatch = matchfn(exckinds, '(?:/|$)')
493 excmatch = matchfn(exckinds, '(?:/|$)')
494
494
495 return (roots,
495 return (roots,
496 lambda fn: (incmatch(fn) and not excmatch(fn) and
496 lambda fn: (incmatch(fn) and not excmatch(fn) and
497 (fn.endswith('/') or
497 (fn.endswith('/') or
498 (not pats and not files) or
498 (not pats and not files) or
499 (pats and patmatch(fn)) or
499 (pats and patmatch(fn)) or
500 (files and filematch(fn)))),
500 (files and filematch(fn)))),
501 (inc or exc or (pats and pats != [('glob', '**')])) and True)
501 (inc or exc or (pats and pats != [('glob', '**')])) and True)
502
502
503 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
503 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
504 '''enhanced shell command execution.
504 '''enhanced shell command execution.
505 run with environment maybe modified, maybe in different dir.
505 run with environment maybe modified, maybe in different dir.
506
506
507 if command fails and onerr is None, return status. if ui object,
507 if command fails and onerr is None, return status. if ui object,
508 print error message and return status, else raise onerr object as
508 print error message and return status, else raise onerr object as
509 exception.'''
509 exception.'''
510 def py2shell(val):
510 def py2shell(val):
511 'convert python object into string that is useful to shell'
511 'convert python object into string that is useful to shell'
512 if val in (None, False):
512 if val in (None, False):
513 return '0'
513 return '0'
514 if val == True:
514 if val == True:
515 return '1'
515 return '1'
516 return str(val)
516 return str(val)
517 oldenv = {}
517 oldenv = {}
518 for k in environ:
518 for k in environ:
519 oldenv[k] = os.environ.get(k)
519 oldenv[k] = os.environ.get(k)
520 if cwd is not None:
520 if cwd is not None:
521 oldcwd = os.getcwd()
521 oldcwd = os.getcwd()
522 origcmd = cmd
522 origcmd = cmd
523 if os.name == 'nt':
523 if os.name == 'nt':
524 cmd = '"%s"' % cmd
524 cmd = '"%s"' % cmd
525 try:
525 try:
526 for k, v in environ.iteritems():
526 for k, v in environ.iteritems():
527 os.environ[k] = py2shell(v)
527 os.environ[k] = py2shell(v)
528 if cwd is not None and oldcwd != cwd:
528 if cwd is not None and oldcwd != cwd:
529 os.chdir(cwd)
529 os.chdir(cwd)
530 rc = os.system(cmd)
530 rc = os.system(cmd)
531 if rc and onerr:
531 if rc and onerr:
532 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
532 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
533 explain_exit(rc)[0])
533 explain_exit(rc)[0])
534 if errprefix:
534 if errprefix:
535 errmsg = '%s: %s' % (errprefix, errmsg)
535 errmsg = '%s: %s' % (errprefix, errmsg)
536 try:
536 try:
537 onerr.warn(errmsg + '\n')
537 onerr.warn(errmsg + '\n')
538 except AttributeError:
538 except AttributeError:
539 raise onerr(errmsg)
539 raise onerr(errmsg)
540 return rc
540 return rc
541 finally:
541 finally:
542 for k, v in oldenv.iteritems():
542 for k, v in oldenv.iteritems():
543 if v is None:
543 if v is None:
544 del os.environ[k]
544 del os.environ[k]
545 else:
545 else:
546 os.environ[k] = v
546 os.environ[k] = v
547 if cwd is not None and oldcwd != cwd:
547 if cwd is not None and oldcwd != cwd:
548 os.chdir(oldcwd)
548 os.chdir(oldcwd)
549
549
550 def rename(src, dst):
550 def rename(src, dst):
551 """forcibly rename a file"""
551 """forcibly rename a file"""
552 try:
552 try:
553 os.rename(src, dst)
553 os.rename(src, dst)
554 except OSError, err:
554 except OSError, err:
555 # on windows, rename to existing file is not allowed, so we
555 # on windows, rename to existing file is not allowed, so we
556 # must delete destination first. but if file is open, unlink
556 # must delete destination first. but if file is open, unlink
557 # schedules it for delete but does not delete it. rename
557 # schedules it for delete but does not delete it. rename
558 # happens immediately even for open files, so we create
558 # happens immediately even for open files, so we create
559 # temporary file, delete it, rename destination to that name,
559 # temporary file, delete it, rename destination to that name,
560 # then delete that. then rename is safe to do.
560 # then delete that. then rename is safe to do.
561 fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.')
561 fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.')
562 os.close(fd)
562 os.close(fd)
563 os.unlink(temp)
563 os.unlink(temp)
564 os.rename(dst, temp)
564 os.rename(dst, temp)
565 os.unlink(temp)
565 os.unlink(temp)
566 os.rename(src, dst)
566 os.rename(src, dst)
567
567
568 def unlink(f):
568 def unlink(f):
569 """unlink and remove the directory if it is empty"""
569 """unlink and remove the directory if it is empty"""
570 os.unlink(f)
570 os.unlink(f)
571 # try removing directories that might now be empty
571 # try removing directories that might now be empty
572 try:
572 try:
573 os.removedirs(os.path.dirname(f))
573 os.removedirs(os.path.dirname(f))
574 except OSError:
574 except OSError:
575 pass
575 pass
576
576
577 def copyfile(src, dest):
577 def copyfile(src, dest):
578 "copy a file, preserving mode"
578 "copy a file, preserving mode"
579 try:
579 try:
580 shutil.copyfile(src, dest)
580 shutil.copyfile(src, dest)
581 shutil.copymode(src, dest)
581 shutil.copymode(src, dest)
582 except shutil.Error, inst:
582 except shutil.Error, inst:
583 raise util.Abort(str(inst))
583 raise Abort(str(inst))
584
584
585 def copyfiles(src, dst, hardlink=None):
585 def copyfiles(src, dst, hardlink=None):
586 """Copy a directory tree using hardlinks if possible"""
586 """Copy a directory tree using hardlinks if possible"""
587
587
588 if hardlink is None:
588 if hardlink is None:
589 hardlink = (os.stat(src).st_dev ==
589 hardlink = (os.stat(src).st_dev ==
590 os.stat(os.path.dirname(dst)).st_dev)
590 os.stat(os.path.dirname(dst)).st_dev)
591
591
592 if os.path.isdir(src):
592 if os.path.isdir(src):
593 os.mkdir(dst)
593 os.mkdir(dst)
594 for name in os.listdir(src):
594 for name in os.listdir(src):
595 srcname = os.path.join(src, name)
595 srcname = os.path.join(src, name)
596 dstname = os.path.join(dst, name)
596 dstname = os.path.join(dst, name)
597 copyfiles(srcname, dstname, hardlink)
597 copyfiles(srcname, dstname, hardlink)
598 else:
598 else:
599 if hardlink:
599 if hardlink:
600 try:
600 try:
601 os_link(src, dst)
601 os_link(src, dst)
602 except (IOError, OSError):
602 except (IOError, OSError):
603 hardlink = False
603 hardlink = False
604 shutil.copy(src, dst)
604 shutil.copy(src, dst)
605 else:
605 else:
606 shutil.copy(src, dst)
606 shutil.copy(src, dst)
607
607
608 def audit_path(path):
608 def audit_path(path):
609 """Abort if path contains dangerous components"""
609 """Abort if path contains dangerous components"""
610 parts = os.path.normcase(path).split(os.sep)
610 parts = os.path.normcase(path).split(os.sep)
611 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '')
611 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '')
612 or os.pardir in parts):
612 or os.pardir in parts):
613 raise Abort(_("path contains illegal component: %s\n") % path)
613 raise Abort(_("path contains illegal component: %s\n") % path)
614
614
615 def _makelock_file(info, pathname):
615 def _makelock_file(info, pathname):
616 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
616 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
617 os.write(ld, info)
617 os.write(ld, info)
618 os.close(ld)
618 os.close(ld)
619
619
620 def _readlock_file(pathname):
620 def _readlock_file(pathname):
621 return posixfile(pathname).read()
621 return posixfile(pathname).read()
622
622
623 def nlinks(pathname):
623 def nlinks(pathname):
624 """Return number of hardlinks for the given file."""
624 """Return number of hardlinks for the given file."""
625 return os.lstat(pathname).st_nlink
625 return os.lstat(pathname).st_nlink
626
626
627 if hasattr(os, 'link'):
627 if hasattr(os, 'link'):
628 os_link = os.link
628 os_link = os.link
629 else:
629 else:
630 def os_link(src, dst):
630 def os_link(src, dst):
631 raise OSError(0, _("Hardlinks not supported"))
631 raise OSError(0, _("Hardlinks not supported"))
632
632
633 def fstat(fp):
633 def fstat(fp):
634 '''stat file object that may not have fileno method.'''
634 '''stat file object that may not have fileno method.'''
635 try:
635 try:
636 return os.fstat(fp.fileno())
636 return os.fstat(fp.fileno())
637 except AttributeError:
637 except AttributeError:
638 return os.stat(fp.name)
638 return os.stat(fp.name)
639
639
640 posixfile = file
640 posixfile = file
641
641
642 def is_win_9x():
642 def is_win_9x():
643 '''return true if run on windows 95, 98 or me.'''
643 '''return true if run on windows 95, 98 or me.'''
644 try:
644 try:
645 return sys.getwindowsversion()[3] == 1
645 return sys.getwindowsversion()[3] == 1
646 except AttributeError:
646 except AttributeError:
647 return os.name == 'nt' and 'command' in os.environ.get('comspec', '')
647 return os.name == 'nt' and 'command' in os.environ.get('comspec', '')
648
648
649 getuser_fallback = None
649 getuser_fallback = None
650
650
651 def getuser():
651 def getuser():
652 '''return name of current user'''
652 '''return name of current user'''
653 try:
653 try:
654 return getpass.getuser()
654 return getpass.getuser()
655 except ImportError:
655 except ImportError:
656 # import of pwd will fail on windows - try fallback
656 # import of pwd will fail on windows - try fallback
657 if getuser_fallback:
657 if getuser_fallback:
658 return getuser_fallback()
658 return getuser_fallback()
659 # raised if win32api not available
659 # raised if win32api not available
660 raise Abort(_('user name not available - set USERNAME '
660 raise Abort(_('user name not available - set USERNAME '
661 'environment variable'))
661 'environment variable'))
662
662
663 def username(uid=None):
663 def username(uid=None):
664 """Return the name of the user with the given uid.
664 """Return the name of the user with the given uid.
665
665
666 If uid is None, return the name of the current user."""
666 If uid is None, return the name of the current user."""
667 try:
667 try:
668 import pwd
668 import pwd
669 if uid is None:
669 if uid is None:
670 uid = os.getuid()
670 uid = os.getuid()
671 try:
671 try:
672 return pwd.getpwuid(uid)[0]
672 return pwd.getpwuid(uid)[0]
673 except KeyError:
673 except KeyError:
674 return str(uid)
674 return str(uid)
675 except ImportError:
675 except ImportError:
676 return None
676 return None
677
677
678 def groupname(gid=None):
678 def groupname(gid=None):
679 """Return the name of the group with the given gid.
679 """Return the name of the group with the given gid.
680
680
681 If gid is None, return the name of the current group."""
681 If gid is None, return the name of the current group."""
682 try:
682 try:
683 import grp
683 import grp
684 if gid is None:
684 if gid is None:
685 gid = os.getgid()
685 gid = os.getgid()
686 try:
686 try:
687 return grp.getgrgid(gid)[0]
687 return grp.getgrgid(gid)[0]
688 except KeyError:
688 except KeyError:
689 return str(gid)
689 return str(gid)
690 except ImportError:
690 except ImportError:
691 return None
691 return None
692
692
693 # File system features
693 # File system features
694
694
695 def checkfolding(path):
695 def checkfolding(path):
696 """
696 """
697 Check whether the given path is on a case-sensitive filesystem
697 Check whether the given path is on a case-sensitive filesystem
698
698
699 Requires a path (like /foo/.hg) ending with a foldable final
699 Requires a path (like /foo/.hg) ending with a foldable final
700 directory component.
700 directory component.
701 """
701 """
702 s1 = os.stat(path)
702 s1 = os.stat(path)
703 d, b = os.path.split(path)
703 d, b = os.path.split(path)
704 p2 = os.path.join(d, b.upper())
704 p2 = os.path.join(d, b.upper())
705 if path == p2:
705 if path == p2:
706 p2 = os.path.join(d, b.lower())
706 p2 = os.path.join(d, b.lower())
707 try:
707 try:
708 s2 = os.stat(p2)
708 s2 = os.stat(p2)
709 if s2 == s1:
709 if s2 == s1:
710 return False
710 return False
711 return True
711 return True
712 except:
712 except:
713 return True
713 return True
714
714
715 # Platform specific variants
715 # Platform specific variants
716 if os.name == 'nt':
716 if os.name == 'nt':
717 demandload(globals(), "msvcrt")
717 demandload(globals(), "msvcrt")
718 nulldev = 'NUL:'
718 nulldev = 'NUL:'
719
719
720 class winstdout:
720 class winstdout:
721 '''stdout on windows misbehaves if sent through a pipe'''
721 '''stdout on windows misbehaves if sent through a pipe'''
722
722
723 def __init__(self, fp):
723 def __init__(self, fp):
724 self.fp = fp
724 self.fp = fp
725
725
726 def __getattr__(self, key):
726 def __getattr__(self, key):
727 return getattr(self.fp, key)
727 return getattr(self.fp, key)
728
728
729 def close(self):
729 def close(self):
730 try:
730 try:
731 self.fp.close()
731 self.fp.close()
732 except: pass
732 except: pass
733
733
734 def write(self, s):
734 def write(self, s):
735 try:
735 try:
736 return self.fp.write(s)
736 return self.fp.write(s)
737 except IOError, inst:
737 except IOError, inst:
738 if inst.errno != 0: raise
738 if inst.errno != 0: raise
739 self.close()
739 self.close()
740 raise IOError(errno.EPIPE, 'Broken pipe')
740 raise IOError(errno.EPIPE, 'Broken pipe')
741
741
742 sys.stdout = winstdout(sys.stdout)
742 sys.stdout = winstdout(sys.stdout)
743
743
744 def system_rcpath():
744 def system_rcpath():
745 try:
745 try:
746 return system_rcpath_win32()
746 return system_rcpath_win32()
747 except:
747 except:
748 return [r'c:\mercurial\mercurial.ini']
748 return [r'c:\mercurial\mercurial.ini']
749
749
750 def os_rcpath():
750 def os_rcpath():
751 '''return default os-specific hgrc search path'''
751 '''return default os-specific hgrc search path'''
752 path = system_rcpath()
752 path = system_rcpath()
753 path.append(user_rcpath())
753 path.append(user_rcpath())
754 userprofile = os.environ.get('USERPROFILE')
754 userprofile = os.environ.get('USERPROFILE')
755 if userprofile:
755 if userprofile:
756 path.append(os.path.join(userprofile, 'mercurial.ini'))
756 path.append(os.path.join(userprofile, 'mercurial.ini'))
757 return path
757 return path
758
758
759 def user_rcpath():
759 def user_rcpath():
760 '''return os-specific hgrc search path to the user dir'''
760 '''return os-specific hgrc search path to the user dir'''
761 return os.path.join(os.path.expanduser('~'), 'mercurial.ini')
761 return os.path.join(os.path.expanduser('~'), 'mercurial.ini')
762
762
763 def parse_patch_output(output_line):
763 def parse_patch_output(output_line):
764 """parses the output produced by patch and returns the file name"""
764 """parses the output produced by patch and returns the file name"""
765 pf = output_line[14:]
765 pf = output_line[14:]
766 if pf[0] == '`':
766 if pf[0] == '`':
767 pf = pf[1:-1] # Remove the quotes
767 pf = pf[1:-1] # Remove the quotes
768 return pf
768 return pf
769
769
770 def testpid(pid):
770 def testpid(pid):
771 '''return False if pid dead, True if running or not known'''
771 '''return False if pid dead, True if running or not known'''
772 return True
772 return True
773
773
774 def is_exec(f, last):
774 def is_exec(f, last):
775 return last
775 return last
776
776
777 def set_exec(f, mode):
777 def set_exec(f, mode):
778 pass
778 pass
779
779
780 def set_binary(fd):
780 def set_binary(fd):
781 msvcrt.setmode(fd.fileno(), os.O_BINARY)
781 msvcrt.setmode(fd.fileno(), os.O_BINARY)
782
782
783 def pconvert(path):
783 def pconvert(path):
784 return path.replace("\\", "/")
784 return path.replace("\\", "/")
785
785
786 def localpath(path):
786 def localpath(path):
787 return path.replace('/', '\\')
787 return path.replace('/', '\\')
788
788
789 def normpath(path):
789 def normpath(path):
790 return pconvert(os.path.normpath(path))
790 return pconvert(os.path.normpath(path))
791
791
792 makelock = _makelock_file
792 makelock = _makelock_file
793 readlock = _readlock_file
793 readlock = _readlock_file
794
794
795 def samestat(s1, s2):
795 def samestat(s1, s2):
796 return False
796 return False
797
797
798 def shellquote(s):
798 def shellquote(s):
799 return '"%s"' % s.replace('"', '\\"')
799 return '"%s"' % s.replace('"', '\\"')
800
800
801 def explain_exit(code):
801 def explain_exit(code):
802 return _("exited with status %d") % code, code
802 return _("exited with status %d") % code, code
803
803
804 # if you change this stub into a real check, please try to implement the
804 # if you change this stub into a real check, please try to implement the
805 # username and groupname functions above, too.
805 # username and groupname functions above, too.
806 def isowner(fp, st=None):
806 def isowner(fp, st=None):
807 return True
807 return True
808
808
809 try:
809 try:
810 # override functions with win32 versions if possible
810 # override functions with win32 versions if possible
811 from util_win32 import *
811 from util_win32 import *
812 if not is_win_9x():
812 if not is_win_9x():
813 posixfile = posixfile_nt
813 posixfile = posixfile_nt
814 except ImportError:
814 except ImportError:
815 pass
815 pass
816
816
817 else:
817 else:
818 nulldev = '/dev/null'
818 nulldev = '/dev/null'
819
819
820 def rcfiles(path):
820 def rcfiles(path):
821 rcs = [os.path.join(path, 'hgrc')]
821 rcs = [os.path.join(path, 'hgrc')]
822 rcdir = os.path.join(path, 'hgrc.d')
822 rcdir = os.path.join(path, 'hgrc.d')
823 try:
823 try:
824 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir)
824 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir)
825 if f.endswith(".rc")])
825 if f.endswith(".rc")])
826 except OSError:
826 except OSError:
827 pass
827 pass
828 return rcs
828 return rcs
829
829
830 def os_rcpath():
830 def os_rcpath():
831 '''return default os-specific hgrc search path'''
831 '''return default os-specific hgrc search path'''
832 path = []
832 path = []
833 # old mod_python does not set sys.argv
833 # old mod_python does not set sys.argv
834 if len(getattr(sys, 'argv', [])) > 0:
834 if len(getattr(sys, 'argv', [])) > 0:
835 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
835 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
836 '/../etc/mercurial'))
836 '/../etc/mercurial'))
837 path.extend(rcfiles('/etc/mercurial'))
837 path.extend(rcfiles('/etc/mercurial'))
838 path.append(os.path.expanduser('~/.hgrc'))
838 path.append(os.path.expanduser('~/.hgrc'))
839 path = [os.path.normpath(f) for f in path]
839 path = [os.path.normpath(f) for f in path]
840 return path
840 return path
841
841
842 def parse_patch_output(output_line):
842 def parse_patch_output(output_line):
843 """parses the output produced by patch and returns the file name"""
843 """parses the output produced by patch and returns the file name"""
844 pf = output_line[14:]
844 pf = output_line[14:]
845 if pf.startswith("'") and pf.endswith("'") and " " in pf:
845 if pf.startswith("'") and pf.endswith("'") and " " in pf:
846 pf = pf[1:-1] # Remove the quotes
846 pf = pf[1:-1] # Remove the quotes
847 return pf
847 return pf
848
848
849 def is_exec(f, last):
849 def is_exec(f, last):
850 """check whether a file is executable"""
850 """check whether a file is executable"""
851 return (os.lstat(f).st_mode & 0100 != 0)
851 return (os.lstat(f).st_mode & 0100 != 0)
852
852
853 def set_exec(f, mode):
853 def set_exec(f, mode):
854 s = os.lstat(f).st_mode
854 s = os.lstat(f).st_mode
855 if (s & 0100 != 0) == mode:
855 if (s & 0100 != 0) == mode:
856 return
856 return
857 if mode:
857 if mode:
858 # Turn on +x for every +r bit when making a file executable
858 # Turn on +x for every +r bit when making a file executable
859 # and obey umask.
859 # and obey umask.
860 umask = os.umask(0)
860 umask = os.umask(0)
861 os.umask(umask)
861 os.umask(umask)
862 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
862 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
863 else:
863 else:
864 os.chmod(f, s & 0666)
864 os.chmod(f, s & 0666)
865
865
866 def set_binary(fd):
866 def set_binary(fd):
867 pass
867 pass
868
868
869 def pconvert(path):
869 def pconvert(path):
870 return path
870 return path
871
871
872 def localpath(path):
872 def localpath(path):
873 return path
873 return path
874
874
875 normpath = os.path.normpath
875 normpath = os.path.normpath
876 samestat = os.path.samestat
876 samestat = os.path.samestat
877
877
878 def makelock(info, pathname):
878 def makelock(info, pathname):
879 try:
879 try:
880 os.symlink(info, pathname)
880 os.symlink(info, pathname)
881 except OSError, why:
881 except OSError, why:
882 if why.errno == errno.EEXIST:
882 if why.errno == errno.EEXIST:
883 raise
883 raise
884 else:
884 else:
885 _makelock_file(info, pathname)
885 _makelock_file(info, pathname)
886
886
887 def readlock(pathname):
887 def readlock(pathname):
888 try:
888 try:
889 return os.readlink(pathname)
889 return os.readlink(pathname)
890 except OSError, why:
890 except OSError, why:
891 if why.errno == errno.EINVAL:
891 if why.errno == errno.EINVAL:
892 return _readlock_file(pathname)
892 return _readlock_file(pathname)
893 else:
893 else:
894 raise
894 raise
895
895
896 def shellquote(s):
896 def shellquote(s):
897 return "'%s'" % s.replace("'", "'\\''")
897 return "'%s'" % s.replace("'", "'\\''")
898
898
899 def testpid(pid):
899 def testpid(pid):
900 '''return False if pid dead, True if running or not sure'''
900 '''return False if pid dead, True if running or not sure'''
901 try:
901 try:
902 os.kill(pid, 0)
902 os.kill(pid, 0)
903 return True
903 return True
904 except OSError, inst:
904 except OSError, inst:
905 return inst.errno != errno.ESRCH
905 return inst.errno != errno.ESRCH
906
906
907 def explain_exit(code):
907 def explain_exit(code):
908 """return a 2-tuple (desc, code) describing a process's status"""
908 """return a 2-tuple (desc, code) describing a process's status"""
909 if os.WIFEXITED(code):
909 if os.WIFEXITED(code):
910 val = os.WEXITSTATUS(code)
910 val = os.WEXITSTATUS(code)
911 return _("exited with status %d") % val, val
911 return _("exited with status %d") % val, val
912 elif os.WIFSIGNALED(code):
912 elif os.WIFSIGNALED(code):
913 val = os.WTERMSIG(code)
913 val = os.WTERMSIG(code)
914 return _("killed by signal %d") % val, val
914 return _("killed by signal %d") % val, val
915 elif os.WIFSTOPPED(code):
915 elif os.WIFSTOPPED(code):
916 val = os.WSTOPSIG(code)
916 val = os.WSTOPSIG(code)
917 return _("stopped by signal %d") % val, val
917 return _("stopped by signal %d") % val, val
918 raise ValueError(_("invalid exit code"))
918 raise ValueError(_("invalid exit code"))
919
919
920 def isowner(fp, st=None):
920 def isowner(fp, st=None):
921 """Return True if the file object f belongs to the current user.
921 """Return True if the file object f belongs to the current user.
922
922
923 The return value of a util.fstat(f) may be passed as the st argument.
923 The return value of a util.fstat(f) may be passed as the st argument.
924 """
924 """
925 if st is None:
925 if st is None:
926 st = fstat(fp)
926 st = fstat(fp)
927 return st.st_uid == os.getuid()
927 return st.st_uid == os.getuid()
928
928
929 def _buildencodefun():
929 def _buildencodefun():
930 e = '_'
930 e = '_'
931 win_reserved = [ord(x) for x in '\\:*?"<>|']
931 win_reserved = [ord(x) for x in '\\:*?"<>|']
932 cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
932 cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
933 for x in (range(32) + range(126, 256) + win_reserved):
933 for x in (range(32) + range(126, 256) + win_reserved):
934 cmap[chr(x)] = "~%02x" % x
934 cmap[chr(x)] = "~%02x" % x
935 for x in range(ord("A"), ord("Z")+1) + [ord(e)]:
935 for x in range(ord("A"), ord("Z")+1) + [ord(e)]:
936 cmap[chr(x)] = e + chr(x).lower()
936 cmap[chr(x)] = e + chr(x).lower()
937 dmap = {}
937 dmap = {}
938 for k, v in cmap.iteritems():
938 for k, v in cmap.iteritems():
939 dmap[v] = k
939 dmap[v] = k
940 def decode(s):
940 def decode(s):
941 i = 0
941 i = 0
942 while i < len(s):
942 while i < len(s):
943 for l in xrange(1, 4):
943 for l in xrange(1, 4):
944 try:
944 try:
945 yield dmap[s[i:i+l]]
945 yield dmap[s[i:i+l]]
946 i += l
946 i += l
947 break
947 break
948 except KeyError:
948 except KeyError:
949 pass
949 pass
950 else:
950 else:
951 raise KeyError
951 raise KeyError
952 return (lambda s: "".join([cmap[c] for c in s]),
952 return (lambda s: "".join([cmap[c] for c in s]),
953 lambda s: "".join(list(decode(s))))
953 lambda s: "".join(list(decode(s))))
954
954
955 encodefilename, decodefilename = _buildencodefun()
955 encodefilename, decodefilename = _buildencodefun()
956
956
957 def encodedopener(openerfn, fn):
957 def encodedopener(openerfn, fn):
958 def o(path, *args, **kw):
958 def o(path, *args, **kw):
959 return openerfn(fn(path), *args, **kw)
959 return openerfn(fn(path), *args, **kw)
960 return o
960 return o
961
961
962 def opener(base, audit=True):
962 def opener(base, audit=True):
963 """
963 """
964 return a function that opens files relative to base
964 return a function that opens files relative to base
965
965
966 this function is used to hide the details of COW semantics and
966 this function is used to hide the details of COW semantics and
967 remote file access from higher level code.
967 remote file access from higher level code.
968 """
968 """
969 p = base
969 p = base
970 audit_p = audit
970 audit_p = audit
971
971
972 def mktempcopy(name):
972 def mktempcopy(name):
973 d, fn = os.path.split(name)
973 d, fn = os.path.split(name)
974 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
974 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
975 os.close(fd)
975 os.close(fd)
976 ofp = posixfile(temp, "wb")
976 ofp = posixfile(temp, "wb")
977 try:
977 try:
978 try:
978 try:
979 ifp = posixfile(name, "rb")
979 ifp = posixfile(name, "rb")
980 except IOError, inst:
980 except IOError, inst:
981 if not getattr(inst, 'filename', None):
981 if not getattr(inst, 'filename', None):
982 inst.filename = name
982 inst.filename = name
983 raise
983 raise
984 for chunk in filechunkiter(ifp):
984 for chunk in filechunkiter(ifp):
985 ofp.write(chunk)
985 ofp.write(chunk)
986 ifp.close()
986 ifp.close()
987 ofp.close()
987 ofp.close()
988 except:
988 except:
989 try: os.unlink(temp)
989 try: os.unlink(temp)
990 except: pass
990 except: pass
991 raise
991 raise
992 st = os.lstat(name)
992 st = os.lstat(name)
993 os.chmod(temp, st.st_mode)
993 os.chmod(temp, st.st_mode)
994 return temp
994 return temp
995
995
996 class atomictempfile(posixfile):
996 class atomictempfile(posixfile):
997 """the file will only be copied when rename is called"""
997 """the file will only be copied when rename is called"""
998 def __init__(self, name, mode):
998 def __init__(self, name, mode):
999 self.__name = name
999 self.__name = name
1000 self.temp = mktempcopy(name)
1000 self.temp = mktempcopy(name)
1001 posixfile.__init__(self, self.temp, mode)
1001 posixfile.__init__(self, self.temp, mode)
1002 def rename(self):
1002 def rename(self):
1003 if not self.closed:
1003 if not self.closed:
1004 posixfile.close(self)
1004 posixfile.close(self)
1005 rename(self.temp, localpath(self.__name))
1005 rename(self.temp, localpath(self.__name))
1006 def __del__(self):
1006 def __del__(self):
1007 if not self.closed:
1007 if not self.closed:
1008 try:
1008 try:
1009 os.unlink(self.temp)
1009 os.unlink(self.temp)
1010 except: pass
1010 except: pass
1011 posixfile.close(self)
1011 posixfile.close(self)
1012
1012
1013 class atomicfile(atomictempfile):
1013 class atomicfile(atomictempfile):
1014 """the file will only be copied on close"""
1014 """the file will only be copied on close"""
1015 def __init__(self, name, mode):
1015 def __init__(self, name, mode):
1016 atomictempfile.__init__(self, name, mode)
1016 atomictempfile.__init__(self, name, mode)
1017 def close(self):
1017 def close(self):
1018 self.rename()
1018 self.rename()
1019 def __del__(self):
1019 def __del__(self):
1020 self.rename()
1020 self.rename()
1021
1021
1022 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
1022 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
1023 if audit_p:
1023 if audit_p:
1024 audit_path(path)
1024 audit_path(path)
1025 f = os.path.join(p, path)
1025 f = os.path.join(p, path)
1026
1026
1027 if not text:
1027 if not text:
1028 mode += "b" # for that other OS
1028 mode += "b" # for that other OS
1029
1029
1030 if mode[0] != "r":
1030 if mode[0] != "r":
1031 try:
1031 try:
1032 nlink = nlinks(f)
1032 nlink = nlinks(f)
1033 except OSError:
1033 except OSError:
1034 d = os.path.dirname(f)
1034 d = os.path.dirname(f)
1035 if not os.path.isdir(d):
1035 if not os.path.isdir(d):
1036 os.makedirs(d)
1036 os.makedirs(d)
1037 else:
1037 else:
1038 if atomic:
1038 if atomic:
1039 return atomicfile(f, mode)
1039 return atomicfile(f, mode)
1040 elif atomictemp:
1040 elif atomictemp:
1041 return atomictempfile(f, mode)
1041 return atomictempfile(f, mode)
1042 if nlink > 1:
1042 if nlink > 1:
1043 rename(mktempcopy(f), f)
1043 rename(mktempcopy(f), f)
1044 return posixfile(f, mode)
1044 return posixfile(f, mode)
1045
1045
1046 return o
1046 return o
1047
1047
1048 class chunkbuffer(object):
1048 class chunkbuffer(object):
1049 """Allow arbitrary sized chunks of data to be efficiently read from an
1049 """Allow arbitrary sized chunks of data to be efficiently read from an
1050 iterator over chunks of arbitrary size."""
1050 iterator over chunks of arbitrary size."""
1051
1051
1052 def __init__(self, in_iter, targetsize = 2**16):
1052 def __init__(self, in_iter, targetsize = 2**16):
1053 """in_iter is the iterator that's iterating over the input chunks.
1053 """in_iter is the iterator that's iterating over the input chunks.
1054 targetsize is how big a buffer to try to maintain."""
1054 targetsize is how big a buffer to try to maintain."""
1055 self.in_iter = iter(in_iter)
1055 self.in_iter = iter(in_iter)
1056 self.buf = ''
1056 self.buf = ''
1057 self.targetsize = int(targetsize)
1057 self.targetsize = int(targetsize)
1058 if self.targetsize <= 0:
1058 if self.targetsize <= 0:
1059 raise ValueError(_("targetsize must be greater than 0, was %d") %
1059 raise ValueError(_("targetsize must be greater than 0, was %d") %
1060 targetsize)
1060 targetsize)
1061 self.iterempty = False
1061 self.iterempty = False
1062
1062
1063 def fillbuf(self):
1063 def fillbuf(self):
1064 """Ignore target size; read every chunk from iterator until empty."""
1064 """Ignore target size; read every chunk from iterator until empty."""
1065 if not self.iterempty:
1065 if not self.iterempty:
1066 collector = cStringIO.StringIO()
1066 collector = cStringIO.StringIO()
1067 collector.write(self.buf)
1067 collector.write(self.buf)
1068 for ch in self.in_iter:
1068 for ch in self.in_iter:
1069 collector.write(ch)
1069 collector.write(ch)
1070 self.buf = collector.getvalue()
1070 self.buf = collector.getvalue()
1071 self.iterempty = True
1071 self.iterempty = True
1072
1072
1073 def read(self, l):
1073 def read(self, l):
1074 """Read L bytes of data from the iterator of chunks of data.
1074 """Read L bytes of data from the iterator of chunks of data.
1075 Returns less than L bytes if the iterator runs dry."""
1075 Returns less than L bytes if the iterator runs dry."""
1076 if l > len(self.buf) and not self.iterempty:
1076 if l > len(self.buf) and not self.iterempty:
1077 # Clamp to a multiple of self.targetsize
1077 # Clamp to a multiple of self.targetsize
1078 targetsize = self.targetsize * ((l // self.targetsize) + 1)
1078 targetsize = self.targetsize * ((l // self.targetsize) + 1)
1079 collector = cStringIO.StringIO()
1079 collector = cStringIO.StringIO()
1080 collector.write(self.buf)
1080 collector.write(self.buf)
1081 collected = len(self.buf)
1081 collected = len(self.buf)
1082 for chunk in self.in_iter:
1082 for chunk in self.in_iter:
1083 collector.write(chunk)
1083 collector.write(chunk)
1084 collected += len(chunk)
1084 collected += len(chunk)
1085 if collected >= targetsize:
1085 if collected >= targetsize:
1086 break
1086 break
1087 if collected < targetsize:
1087 if collected < targetsize:
1088 self.iterempty = True
1088 self.iterempty = True
1089 self.buf = collector.getvalue()
1089 self.buf = collector.getvalue()
1090 s, self.buf = self.buf[:l], buffer(self.buf, l)
1090 s, self.buf = self.buf[:l], buffer(self.buf, l)
1091 return s
1091 return s
1092
1092
1093 def filechunkiter(f, size=65536, limit=None):
1093 def filechunkiter(f, size=65536, limit=None):
1094 """Create a generator that produces the data in the file size
1094 """Create a generator that produces the data in the file size
1095 (default 65536) bytes at a time, up to optional limit (default is
1095 (default 65536) bytes at a time, up to optional limit (default is
1096 to read all data). Chunks may be less than size bytes if the
1096 to read all data). Chunks may be less than size bytes if the
1097 chunk is the last chunk in the file, or the file is a socket or
1097 chunk is the last chunk in the file, or the file is a socket or
1098 some other type of file that sometimes reads less data than is
1098 some other type of file that sometimes reads less data than is
1099 requested."""
1099 requested."""
1100 assert size >= 0
1100 assert size >= 0
1101 assert limit is None or limit >= 0
1101 assert limit is None or limit >= 0
1102 while True:
1102 while True:
1103 if limit is None: nbytes = size
1103 if limit is None: nbytes = size
1104 else: nbytes = min(limit, size)
1104 else: nbytes = min(limit, size)
1105 s = nbytes and f.read(nbytes)
1105 s = nbytes and f.read(nbytes)
1106 if not s: break
1106 if not s: break
1107 if limit: limit -= len(s)
1107 if limit: limit -= len(s)
1108 yield s
1108 yield s
1109
1109
1110 def makedate():
1110 def makedate():
1111 lt = time.localtime()
1111 lt = time.localtime()
1112 if lt[8] == 1 and time.daylight:
1112 if lt[8] == 1 and time.daylight:
1113 tz = time.altzone
1113 tz = time.altzone
1114 else:
1114 else:
1115 tz = time.timezone
1115 tz = time.timezone
1116 return time.mktime(lt), tz
1116 return time.mktime(lt), tz
1117
1117
1118 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True):
1118 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True):
1119 """represent a (unixtime, offset) tuple as a localized time.
1119 """represent a (unixtime, offset) tuple as a localized time.
1120 unixtime is seconds since the epoch, and offset is the time zone's
1120 unixtime is seconds since the epoch, and offset is the time zone's
1121 number of seconds away from UTC. if timezone is false, do not
1121 number of seconds away from UTC. if timezone is false, do not
1122 append time zone to string."""
1122 append time zone to string."""
1123 t, tz = date or makedate()
1123 t, tz = date or makedate()
1124 s = time.strftime(format, time.gmtime(float(t) - tz))
1124 s = time.strftime(format, time.gmtime(float(t) - tz))
1125 if timezone:
1125 if timezone:
1126 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
1126 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
1127 return s
1127 return s
1128
1128
1129 def strdate(string, format, defaults):
1129 def strdate(string, format, defaults):
1130 """parse a localized time string and return a (unixtime, offset) tuple.
1130 """parse a localized time string and return a (unixtime, offset) tuple.
1131 if the string cannot be parsed, ValueError is raised."""
1131 if the string cannot be parsed, ValueError is raised."""
1132 def timezone(string):
1132 def timezone(string):
1133 tz = string.split()[-1]
1133 tz = string.split()[-1]
1134 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1134 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1135 tz = int(tz)
1135 tz = int(tz)
1136 offset = - 3600 * (tz / 100) - 60 * (tz % 100)
1136 offset = - 3600 * (tz / 100) - 60 * (tz % 100)
1137 return offset
1137 return offset
1138 if tz == "GMT" or tz == "UTC":
1138 if tz == "GMT" or tz == "UTC":
1139 return 0
1139 return 0
1140 return None
1140 return None
1141
1141
1142 # NOTE: unixtime = localunixtime + offset
1142 # NOTE: unixtime = localunixtime + offset
1143 offset, date = timezone(string), string
1143 offset, date = timezone(string), string
1144 if offset != None:
1144 if offset != None:
1145 date = " ".join(string.split()[:-1])
1145 date = " ".join(string.split()[:-1])
1146
1146
1147 # add missing elements from defaults
1147 # add missing elements from defaults
1148 for part in defaults:
1148 for part in defaults:
1149 found = [True for p in part if ("%"+p) in format]
1149 found = [True for p in part if ("%"+p) in format]
1150 if not found:
1150 if not found:
1151 date += "@" + defaults[part]
1151 date += "@" + defaults[part]
1152 format += "@%" + part[0]
1152 format += "@%" + part[0]
1153
1153
1154 timetuple = time.strptime(date, format)
1154 timetuple = time.strptime(date, format)
1155 localunixtime = int(calendar.timegm(timetuple))
1155 localunixtime = int(calendar.timegm(timetuple))
1156 if offset is None:
1156 if offset is None:
1157 # local timezone
1157 # local timezone
1158 unixtime = int(time.mktime(timetuple))
1158 unixtime = int(time.mktime(timetuple))
1159 offset = unixtime - localunixtime
1159 offset = unixtime - localunixtime
1160 else:
1160 else:
1161 unixtime = localunixtime + offset
1161 unixtime = localunixtime + offset
1162 return unixtime, offset
1162 return unixtime, offset
1163
1163
1164 def parsedate(string, formats=None, defaults=None):
1164 def parsedate(string, formats=None, defaults=None):
1165 """parse a localized time string and return a (unixtime, offset) tuple.
1165 """parse a localized time string and return a (unixtime, offset) tuple.
1166 The date may be a "unixtime offset" string or in one of the specified
1166 The date may be a "unixtime offset" string or in one of the specified
1167 formats."""
1167 formats."""
1168 if not string:
1168 if not string:
1169 return 0, 0
1169 return 0, 0
1170 if not formats:
1170 if not formats:
1171 formats = defaultdateformats
1171 formats = defaultdateformats
1172 string = string.strip()
1172 string = string.strip()
1173 try:
1173 try:
1174 when, offset = map(int, string.split(' '))
1174 when, offset = map(int, string.split(' '))
1175 except ValueError:
1175 except ValueError:
1176 # fill out defaults
1176 # fill out defaults
1177 if not defaults:
1177 if not defaults:
1178 defaults = {}
1178 defaults = {}
1179 now = makedate()
1179 now = makedate()
1180 for part in "d mb yY HI M S".split():
1180 for part in "d mb yY HI M S".split():
1181 if part not in defaults:
1181 if part not in defaults:
1182 if part[0] in "HMS":
1182 if part[0] in "HMS":
1183 defaults[part] = "00"
1183 defaults[part] = "00"
1184 elif part[0] in "dm":
1184 elif part[0] in "dm":
1185 defaults[part] = "1"
1185 defaults[part] = "1"
1186 else:
1186 else:
1187 defaults[part] = datestr(now, "%" + part[0], False)
1187 defaults[part] = datestr(now, "%" + part[0], False)
1188
1188
1189 for format in formats:
1189 for format in formats:
1190 try:
1190 try:
1191 when, offset = strdate(string, format, defaults)
1191 when, offset = strdate(string, format, defaults)
1192 except ValueError:
1192 except ValueError:
1193 pass
1193 pass
1194 else:
1194 else:
1195 break
1195 break
1196 else:
1196 else:
1197 raise Abort(_('invalid date: %r ') % string)
1197 raise Abort(_('invalid date: %r ') % string)
1198 # validate explicit (probably user-specified) date and
1198 # validate explicit (probably user-specified) date and
1199 # time zone offset. values must fit in signed 32 bits for
1199 # time zone offset. values must fit in signed 32 bits for
1200 # current 32-bit linux runtimes. timezones go from UTC-12
1200 # current 32-bit linux runtimes. timezones go from UTC-12
1201 # to UTC+14
1201 # to UTC+14
1202 if abs(when) > 0x7fffffff:
1202 if abs(when) > 0x7fffffff:
1203 raise Abort(_('date exceeds 32 bits: %d') % when)
1203 raise Abort(_('date exceeds 32 bits: %d') % when)
1204 if offset < -50400 or offset > 43200:
1204 if offset < -50400 or offset > 43200:
1205 raise Abort(_('impossible time zone offset: %d') % offset)
1205 raise Abort(_('impossible time zone offset: %d') % offset)
1206 return when, offset
1206 return when, offset
1207
1207
1208 def matchdate(date):
1208 def matchdate(date):
1209 """Return a function that matches a given date match specifier
1209 """Return a function that matches a given date match specifier
1210
1210
1211 Formats include:
1211 Formats include:
1212
1212
1213 '{date}' match a given date to the accuracy provided
1213 '{date}' match a given date to the accuracy provided
1214
1214
1215 '<{date}' on or before a given date
1215 '<{date}' on or before a given date
1216
1216
1217 '>{date}' on or after a given date
1217 '>{date}' on or after a given date
1218
1218
1219 """
1219 """
1220
1220
1221 def lower(date):
1221 def lower(date):
1222 return parsedate(date, extendeddateformats)[0]
1222 return parsedate(date, extendeddateformats)[0]
1223
1223
1224 def upper(date):
1224 def upper(date):
1225 d = dict(mb="12", HI="23", M="59", S="59")
1225 d = dict(mb="12", HI="23", M="59", S="59")
1226 for days in "31 30 29".split():
1226 for days in "31 30 29".split():
1227 try:
1227 try:
1228 d["d"] = days
1228 d["d"] = days
1229 return parsedate(date, extendeddateformats, d)[0]
1229 return parsedate(date, extendeddateformats, d)[0]
1230 except:
1230 except:
1231 pass
1231 pass
1232 d["d"] = "28"
1232 d["d"] = "28"
1233 return parsedate(date, extendeddateformats, d)[0]
1233 return parsedate(date, extendeddateformats, d)[0]
1234
1234
1235 if date[0] == "<":
1235 if date[0] == "<":
1236 when = upper(date[1:])
1236 when = upper(date[1:])
1237 return lambda x: x <= when
1237 return lambda x: x <= when
1238 elif date[0] == ">":
1238 elif date[0] == ">":
1239 when = lower(date[1:])
1239 when = lower(date[1:])
1240 return lambda x: x >= when
1240 return lambda x: x >= when
1241 elif date[0] == "-":
1241 elif date[0] == "-":
1242 try:
1242 try:
1243 days = int(date[1:])
1243 days = int(date[1:])
1244 except ValueError:
1244 except ValueError:
1245 raise Abort(_("invalid day spec: %s") % date[1:])
1245 raise Abort(_("invalid day spec: %s") % date[1:])
1246 when = makedate()[0] - days * 3600 * 24
1246 when = makedate()[0] - days * 3600 * 24
1247 return lambda x: x >= when
1247 return lambda x: x >= when
1248 elif " to " in date:
1248 elif " to " in date:
1249 a, b = date.split(" to ")
1249 a, b = date.split(" to ")
1250 start, stop = lower(a), upper(b)
1250 start, stop = lower(a), upper(b)
1251 return lambda x: x >= start and x <= stop
1251 return lambda x: x >= start and x <= stop
1252 else:
1252 else:
1253 start, stop = lower(date), upper(date)
1253 start, stop = lower(date), upper(date)
1254 return lambda x: x >= start and x <= stop
1254 return lambda x: x >= start and x <= stop
1255
1255
1256 def shortuser(user):
1256 def shortuser(user):
1257 """Return a short representation of a user name or email address."""
1257 """Return a short representation of a user name or email address."""
1258 f = user.find('@')
1258 f = user.find('@')
1259 if f >= 0:
1259 if f >= 0:
1260 user = user[:f]
1260 user = user[:f]
1261 f = user.find('<')
1261 f = user.find('<')
1262 if f >= 0:
1262 if f >= 0:
1263 user = user[f+1:]
1263 user = user[f+1:]
1264 f = user.find(' ')
1264 f = user.find(' ')
1265 if f >= 0:
1265 if f >= 0:
1266 user = user[:f]
1266 user = user[:f]
1267 f = user.find('.')
1267 f = user.find('.')
1268 if f >= 0:
1268 if f >= 0:
1269 user = user[:f]
1269 user = user[:f]
1270 return user
1270 return user
1271
1271
1272 def ellipsis(text, maxlength=400):
1272 def ellipsis(text, maxlength=400):
1273 """Trim string to at most maxlength (default: 400) characters."""
1273 """Trim string to at most maxlength (default: 400) characters."""
1274 if len(text) <= maxlength:
1274 if len(text) <= maxlength:
1275 return text
1275 return text
1276 else:
1276 else:
1277 return "%s..." % (text[:maxlength-3])
1277 return "%s..." % (text[:maxlength-3])
1278
1278
1279 def walkrepos(path):
1279 def walkrepos(path):
1280 '''yield every hg repository under path, recursively.'''
1280 '''yield every hg repository under path, recursively.'''
1281 def errhandler(err):
1281 def errhandler(err):
1282 if err.filename == path:
1282 if err.filename == path:
1283 raise err
1283 raise err
1284
1284
1285 for root, dirs, files in os.walk(path, onerror=errhandler):
1285 for root, dirs, files in os.walk(path, onerror=errhandler):
1286 for d in dirs:
1286 for d in dirs:
1287 if d == '.hg':
1287 if d == '.hg':
1288 yield root
1288 yield root
1289 dirs[:] = []
1289 dirs[:] = []
1290 break
1290 break
1291
1291
1292 _rcpath = None
1292 _rcpath = None
1293
1293
1294 def rcpath():
1294 def rcpath():
1295 '''return hgrc search path. if env var HGRCPATH is set, use it.
1295 '''return hgrc search path. if env var HGRCPATH is set, use it.
1296 for each item in path, if directory, use files ending in .rc,
1296 for each item in path, if directory, use files ending in .rc,
1297 else use item.
1297 else use item.
1298 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1298 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1299 if no HGRCPATH, use default os-specific path.'''
1299 if no HGRCPATH, use default os-specific path.'''
1300 global _rcpath
1300 global _rcpath
1301 if _rcpath is None:
1301 if _rcpath is None:
1302 if 'HGRCPATH' in os.environ:
1302 if 'HGRCPATH' in os.environ:
1303 _rcpath = []
1303 _rcpath = []
1304 for p in os.environ['HGRCPATH'].split(os.pathsep):
1304 for p in os.environ['HGRCPATH'].split(os.pathsep):
1305 if not p: continue
1305 if not p: continue
1306 if os.path.isdir(p):
1306 if os.path.isdir(p):
1307 for f in os.listdir(p):
1307 for f in os.listdir(p):
1308 if f.endswith('.rc'):
1308 if f.endswith('.rc'):
1309 _rcpath.append(os.path.join(p, f))
1309 _rcpath.append(os.path.join(p, f))
1310 else:
1310 else:
1311 _rcpath.append(p)
1311 _rcpath.append(p)
1312 else:
1312 else:
1313 _rcpath = os_rcpath()
1313 _rcpath = os_rcpath()
1314 return _rcpath
1314 return _rcpath
1315
1315
1316 def bytecount(nbytes):
1316 def bytecount(nbytes):
1317 '''return byte count formatted as readable string, with units'''
1317 '''return byte count formatted as readable string, with units'''
1318
1318
1319 units = (
1319 units = (
1320 (100, 1<<30, _('%.0f GB')),
1320 (100, 1<<30, _('%.0f GB')),
1321 (10, 1<<30, _('%.1f GB')),
1321 (10, 1<<30, _('%.1f GB')),
1322 (1, 1<<30, _('%.2f GB')),
1322 (1, 1<<30, _('%.2f GB')),
1323 (100, 1<<20, _('%.0f MB')),
1323 (100, 1<<20, _('%.0f MB')),
1324 (10, 1<<20, _('%.1f MB')),
1324 (10, 1<<20, _('%.1f MB')),
1325 (1, 1<<20, _('%.2f MB')),
1325 (1, 1<<20, _('%.2f MB')),
1326 (100, 1<<10, _('%.0f KB')),
1326 (100, 1<<10, _('%.0f KB')),
1327 (10, 1<<10, _('%.1f KB')),
1327 (10, 1<<10, _('%.1f KB')),
1328 (1, 1<<10, _('%.2f KB')),
1328 (1, 1<<10, _('%.2f KB')),
1329 (1, 1, _('%.0f bytes')),
1329 (1, 1, _('%.0f bytes')),
1330 )
1330 )
1331
1331
1332 for multiplier, divisor, format in units:
1332 for multiplier, divisor, format in units:
1333 if nbytes >= divisor * multiplier:
1333 if nbytes >= divisor * multiplier:
1334 return format % (nbytes / float(divisor))
1334 return format % (nbytes / float(divisor))
1335 return units[-1][2] % nbytes
1335 return units[-1][2] % nbytes
1336
1336
1337 def drop_scheme(scheme, path):
1337 def drop_scheme(scheme, path):
1338 sc = scheme + ':'
1338 sc = scheme + ':'
1339 if path.startswith(sc):
1339 if path.startswith(sc):
1340 path = path[len(sc):]
1340 path = path[len(sc):]
1341 if path.startswith('//'):
1341 if path.startswith('//'):
1342 path = path[2:]
1342 path = path[2:]
1343 return path
1343 return path
General Comments 0
You need to be logged in to leave comments. Login now