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