Show More
@@ -1,1779 +1,1779 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-2007 Matt Mackall <mpm@selenic.com> |
|
5 | Copyright 2005-2007 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, strutil |
|
16 | import cStringIO, errno, getpass, popen2, re, shutil, sys, tempfile, strutil | |
17 | import os, stat, threading, time, calendar, ConfigParser, locale, glob, osutil |
|
17 | import os, stat, threading, time, calendar, ConfigParser, locale, glob, osutil | |
18 | import urlparse |
|
18 | import urlparse | |
19 |
|
19 | |||
20 | try: |
|
20 | try: | |
21 | set = set |
|
21 | set = set | |
22 | frozenset = frozenset |
|
22 | frozenset = frozenset | |
23 | except NameError: |
|
23 | except NameError: | |
24 | from sets import Set as set, ImmutableSet as frozenset |
|
24 | from sets import Set as set, ImmutableSet as frozenset | |
25 |
|
25 | |||
26 | try: |
|
26 | try: | |
27 | _encoding = os.environ.get("HGENCODING") |
|
27 | _encoding = os.environ.get("HGENCODING") | |
28 | if sys.platform == 'darwin' and not _encoding: |
|
28 | if sys.platform == 'darwin' and not _encoding: | |
29 | # On darwin, getpreferredencoding ignores the locale environment and |
|
29 | # On darwin, getpreferredencoding ignores the locale environment and | |
30 | # always returns mac-roman. We override this if the environment is |
|
30 | # always returns mac-roman. We override this if the environment is | |
31 | # not C (has been customized by the user). |
|
31 | # not C (has been customized by the user). | |
32 | locale.setlocale(locale.LC_CTYPE, '') |
|
32 | locale.setlocale(locale.LC_CTYPE, '') | |
33 | _encoding = locale.getlocale()[1] |
|
33 | _encoding = locale.getlocale()[1] | |
34 | if not _encoding: |
|
34 | if not _encoding: | |
35 | _encoding = locale.getpreferredencoding() or 'ascii' |
|
35 | _encoding = locale.getpreferredencoding() or 'ascii' | |
36 | except locale.Error: |
|
36 | except locale.Error: | |
37 | _encoding = 'ascii' |
|
37 | _encoding = 'ascii' | |
38 | _encodingmode = os.environ.get("HGENCODINGMODE", "strict") |
|
38 | _encodingmode = os.environ.get("HGENCODINGMODE", "strict") | |
39 | _fallbackencoding = 'ISO-8859-1' |
|
39 | _fallbackencoding = 'ISO-8859-1' | |
40 |
|
40 | |||
41 | def tolocal(s): |
|
41 | def tolocal(s): | |
42 | """ |
|
42 | """ | |
43 | Convert a string from internal UTF-8 to local encoding |
|
43 | Convert a string from internal UTF-8 to local encoding | |
44 |
|
44 | |||
45 | All internal strings should be UTF-8 but some repos before the |
|
45 | All internal strings should be UTF-8 but some repos before the | |
46 | implementation of locale support may contain latin1 or possibly |
|
46 | implementation of locale support may contain latin1 or possibly | |
47 | other character sets. We attempt to decode everything strictly |
|
47 | other character sets. We attempt to decode everything strictly | |
48 | using UTF-8, then Latin-1, and failing that, we use UTF-8 and |
|
48 | using UTF-8, then Latin-1, and failing that, we use UTF-8 and | |
49 | replace unknown characters. |
|
49 | replace unknown characters. | |
50 | """ |
|
50 | """ | |
51 | for e in ('UTF-8', _fallbackencoding): |
|
51 | for e in ('UTF-8', _fallbackencoding): | |
52 | try: |
|
52 | try: | |
53 | u = s.decode(e) # attempt strict decoding |
|
53 | u = s.decode(e) # attempt strict decoding | |
54 | return u.encode(_encoding, "replace") |
|
54 | return u.encode(_encoding, "replace") | |
55 | except LookupError, k: |
|
55 | except LookupError, k: | |
56 | raise Abort(_("%s, please check your locale settings") % k) |
|
56 | raise Abort(_("%s, please check your locale settings") % k) | |
57 | except UnicodeDecodeError: |
|
57 | except UnicodeDecodeError: | |
58 | pass |
|
58 | pass | |
59 | u = s.decode("utf-8", "replace") # last ditch |
|
59 | u = s.decode("utf-8", "replace") # last ditch | |
60 | return u.encode(_encoding, "replace") |
|
60 | return u.encode(_encoding, "replace") | |
61 |
|
61 | |||
62 | def fromlocal(s): |
|
62 | def fromlocal(s): | |
63 | """ |
|
63 | """ | |
64 | Convert a string from the local character encoding to UTF-8 |
|
64 | Convert a string from the local character encoding to UTF-8 | |
65 |
|
65 | |||
66 | We attempt to decode strings using the encoding mode set by |
|
66 | We attempt to decode strings using the encoding mode set by | |
67 | HGENCODINGMODE, which defaults to 'strict'. In this mode, unknown |
|
67 | HGENCODINGMODE, which defaults to 'strict'. In this mode, unknown | |
68 | characters will cause an error message. Other modes include |
|
68 | characters will cause an error message. Other modes include | |
69 | 'replace', which replaces unknown characters with a special |
|
69 | 'replace', which replaces unknown characters with a special | |
70 | Unicode character, and 'ignore', which drops the character. |
|
70 | Unicode character, and 'ignore', which drops the character. | |
71 | """ |
|
71 | """ | |
72 | try: |
|
72 | try: | |
73 | return s.decode(_encoding, _encodingmode).encode("utf-8") |
|
73 | return s.decode(_encoding, _encodingmode).encode("utf-8") | |
74 | except UnicodeDecodeError, inst: |
|
74 | except UnicodeDecodeError, inst: | |
75 | sub = s[max(0, inst.start-10):inst.start+10] |
|
75 | sub = s[max(0, inst.start-10):inst.start+10] | |
76 | raise Abort("decoding near '%s': %s!" % (sub, inst)) |
|
76 | raise Abort("decoding near '%s': %s!" % (sub, inst)) | |
77 | except LookupError, k: |
|
77 | except LookupError, k: | |
78 | raise Abort(_("%s, please check your locale settings") % k) |
|
78 | raise Abort(_("%s, please check your locale settings") % k) | |
79 |
|
79 | |||
80 | def locallen(s): |
|
80 | def locallen(s): | |
81 | """Find the length in characters of a local string""" |
|
81 | """Find the length in characters of a local string""" | |
82 | return len(s.decode(_encoding, "replace")) |
|
82 | return len(s.decode(_encoding, "replace")) | |
83 |
|
83 | |||
84 | # used by parsedate |
|
84 | # used by parsedate | |
85 | defaultdateformats = ( |
|
85 | defaultdateformats = ( | |
86 | '%Y-%m-%d %H:%M:%S', |
|
86 | '%Y-%m-%d %H:%M:%S', | |
87 | '%Y-%m-%d %I:%M:%S%p', |
|
87 | '%Y-%m-%d %I:%M:%S%p', | |
88 | '%Y-%m-%d %H:%M', |
|
88 | '%Y-%m-%d %H:%M', | |
89 | '%Y-%m-%d %I:%M%p', |
|
89 | '%Y-%m-%d %I:%M%p', | |
90 | '%Y-%m-%d', |
|
90 | '%Y-%m-%d', | |
91 | '%m-%d', |
|
91 | '%m-%d', | |
92 | '%m/%d', |
|
92 | '%m/%d', | |
93 | '%m/%d/%y', |
|
93 | '%m/%d/%y', | |
94 | '%m/%d/%Y', |
|
94 | '%m/%d/%Y', | |
95 | '%a %b %d %H:%M:%S %Y', |
|
95 | '%a %b %d %H:%M:%S %Y', | |
96 | '%a %b %d %I:%M:%S%p %Y', |
|
96 | '%a %b %d %I:%M:%S%p %Y', | |
97 | '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" |
|
97 | '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" | |
98 | '%b %d %H:%M:%S %Y', |
|
98 | '%b %d %H:%M:%S %Y', | |
99 | '%b %d %I:%M:%S%p %Y', |
|
99 | '%b %d %I:%M:%S%p %Y', | |
100 | '%b %d %H:%M:%S', |
|
100 | '%b %d %H:%M:%S', | |
101 | '%b %d %I:%M:%S%p', |
|
101 | '%b %d %I:%M:%S%p', | |
102 | '%b %d %H:%M', |
|
102 | '%b %d %H:%M', | |
103 | '%b %d %I:%M%p', |
|
103 | '%b %d %I:%M%p', | |
104 | '%b %d %Y', |
|
104 | '%b %d %Y', | |
105 | '%b %d', |
|
105 | '%b %d', | |
106 | '%H:%M:%S', |
|
106 | '%H:%M:%S', | |
107 | '%I:%M:%SP', |
|
107 | '%I:%M:%SP', | |
108 | '%H:%M', |
|
108 | '%H:%M', | |
109 | '%I:%M%p', |
|
109 | '%I:%M%p', | |
110 | ) |
|
110 | ) | |
111 |
|
111 | |||
112 | extendeddateformats = defaultdateformats + ( |
|
112 | extendeddateformats = defaultdateformats + ( | |
113 | "%Y", |
|
113 | "%Y", | |
114 | "%Y-%m", |
|
114 | "%Y-%m", | |
115 | "%b", |
|
115 | "%b", | |
116 | "%b %Y", |
|
116 | "%b %Y", | |
117 | ) |
|
117 | ) | |
118 |
|
118 | |||
119 | class SignalInterrupt(Exception): |
|
119 | class SignalInterrupt(Exception): | |
120 | """Exception raised on SIGTERM and SIGHUP.""" |
|
120 | """Exception raised on SIGTERM and SIGHUP.""" | |
121 |
|
121 | |||
122 | # differences from SafeConfigParser: |
|
122 | # differences from SafeConfigParser: | |
123 | # - case-sensitive keys |
|
123 | # - case-sensitive keys | |
124 | # - allows values that are not strings (this means that you may not |
|
124 | # - allows values that are not strings (this means that you may not | |
125 | # be able to save the configuration to a file) |
|
125 | # be able to save the configuration to a file) | |
126 | class configparser(ConfigParser.SafeConfigParser): |
|
126 | class configparser(ConfigParser.SafeConfigParser): | |
127 | def optionxform(self, optionstr): |
|
127 | def optionxform(self, optionstr): | |
128 | return optionstr |
|
128 | return optionstr | |
129 |
|
129 | |||
130 | def set(self, section, option, value): |
|
130 | def set(self, section, option, value): | |
131 | return ConfigParser.ConfigParser.set(self, section, option, value) |
|
131 | return ConfigParser.ConfigParser.set(self, section, option, value) | |
132 |
|
132 | |||
133 | def _interpolate(self, section, option, rawval, vars): |
|
133 | def _interpolate(self, section, option, rawval, vars): | |
134 | if not isinstance(rawval, basestring): |
|
134 | if not isinstance(rawval, basestring): | |
135 | return rawval |
|
135 | return rawval | |
136 | return ConfigParser.SafeConfigParser._interpolate(self, section, |
|
136 | return ConfigParser.SafeConfigParser._interpolate(self, section, | |
137 | option, rawval, vars) |
|
137 | option, rawval, vars) | |
138 |
|
138 | |||
139 | def cachefunc(func): |
|
139 | def cachefunc(func): | |
140 | '''cache the result of function calls''' |
|
140 | '''cache the result of function calls''' | |
141 | # XXX doesn't handle keywords args |
|
141 | # XXX doesn't handle keywords args | |
142 | cache = {} |
|
142 | cache = {} | |
143 | if func.func_code.co_argcount == 1: |
|
143 | if func.func_code.co_argcount == 1: | |
144 | # we gain a small amount of time because |
|
144 | # we gain a small amount of time because | |
145 | # we don't need to pack/unpack the list |
|
145 | # we don't need to pack/unpack the list | |
146 | def f(arg): |
|
146 | def f(arg): | |
147 | if arg not in cache: |
|
147 | if arg not in cache: | |
148 | cache[arg] = func(arg) |
|
148 | cache[arg] = func(arg) | |
149 | return cache[arg] |
|
149 | return cache[arg] | |
150 | else: |
|
150 | else: | |
151 | def f(*args): |
|
151 | def f(*args): | |
152 | if args not in cache: |
|
152 | if args not in cache: | |
153 | cache[args] = func(*args) |
|
153 | cache[args] = func(*args) | |
154 | return cache[args] |
|
154 | return cache[args] | |
155 |
|
155 | |||
156 | return f |
|
156 | return f | |
157 |
|
157 | |||
158 | def pipefilter(s, cmd): |
|
158 | def pipefilter(s, cmd): | |
159 | '''filter string S through command CMD, returning its output''' |
|
159 | '''filter string S through command CMD, returning its output''' | |
160 | (pin, pout) = os.popen2(cmd, 'b') |
|
160 | (pin, pout) = os.popen2(cmd, 'b') | |
161 | def writer(): |
|
161 | def writer(): | |
162 | try: |
|
162 | try: | |
163 | pin.write(s) |
|
163 | pin.write(s) | |
164 | pin.close() |
|
164 | pin.close() | |
165 | except IOError, inst: |
|
165 | except IOError, inst: | |
166 | if inst.errno != errno.EPIPE: |
|
166 | if inst.errno != errno.EPIPE: | |
167 | raise |
|
167 | raise | |
168 |
|
168 | |||
169 | # we should use select instead on UNIX, but this will work on most |
|
169 | # we should use select instead on UNIX, but this will work on most | |
170 | # systems, including Windows |
|
170 | # systems, including Windows | |
171 | w = threading.Thread(target=writer) |
|
171 | w = threading.Thread(target=writer) | |
172 | w.start() |
|
172 | w.start() | |
173 | f = pout.read() |
|
173 | f = pout.read() | |
174 | pout.close() |
|
174 | pout.close() | |
175 | w.join() |
|
175 | w.join() | |
176 | return f |
|
176 | return f | |
177 |
|
177 | |||
178 | def tempfilter(s, cmd): |
|
178 | def tempfilter(s, cmd): | |
179 | '''filter string S through a pair of temporary files with CMD. |
|
179 | '''filter string S through a pair of temporary files with CMD. | |
180 | CMD is used as a template to create the real command to be run, |
|
180 | CMD is used as a template to create the real command to be run, | |
181 | with the strings INFILE and OUTFILE replaced by the real names of |
|
181 | with the strings INFILE and OUTFILE replaced by the real names of | |
182 | the temporary files generated.''' |
|
182 | the temporary files generated.''' | |
183 | inname, outname = None, None |
|
183 | inname, outname = None, None | |
184 | try: |
|
184 | try: | |
185 | infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') |
|
185 | infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') | |
186 | fp = os.fdopen(infd, 'wb') |
|
186 | fp = os.fdopen(infd, 'wb') | |
187 | fp.write(s) |
|
187 | fp.write(s) | |
188 | fp.close() |
|
188 | fp.close() | |
189 | outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') |
|
189 | outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') | |
190 | os.close(outfd) |
|
190 | os.close(outfd) | |
191 | cmd = cmd.replace('INFILE', inname) |
|
191 | cmd = cmd.replace('INFILE', inname) | |
192 | cmd = cmd.replace('OUTFILE', outname) |
|
192 | cmd = cmd.replace('OUTFILE', outname) | |
193 | code = os.system(cmd) |
|
193 | code = os.system(cmd) | |
194 | if sys.platform == 'OpenVMS' and code & 1: |
|
194 | if sys.platform == 'OpenVMS' and code & 1: | |
195 | code = 0 |
|
195 | code = 0 | |
196 | if code: raise Abort(_("command '%s' failed: %s") % |
|
196 | if code: raise Abort(_("command '%s' failed: %s") % | |
197 | (cmd, explain_exit(code))) |
|
197 | (cmd, explain_exit(code))) | |
198 | return open(outname, 'rb').read() |
|
198 | return open(outname, 'rb').read() | |
199 | finally: |
|
199 | finally: | |
200 | try: |
|
200 | try: | |
201 | if inname: os.unlink(inname) |
|
201 | if inname: os.unlink(inname) | |
202 | except: pass |
|
202 | except: pass | |
203 | try: |
|
203 | try: | |
204 | if outname: os.unlink(outname) |
|
204 | if outname: os.unlink(outname) | |
205 | except: pass |
|
205 | except: pass | |
206 |
|
206 | |||
207 | filtertable = { |
|
207 | filtertable = { | |
208 | 'tempfile:': tempfilter, |
|
208 | 'tempfile:': tempfilter, | |
209 | 'pipe:': pipefilter, |
|
209 | 'pipe:': pipefilter, | |
210 | } |
|
210 | } | |
211 |
|
211 | |||
212 | def filter(s, cmd): |
|
212 | def filter(s, cmd): | |
213 | "filter a string through a command that transforms its input to its output" |
|
213 | "filter a string through a command that transforms its input to its output" | |
214 | for name, fn in filtertable.iteritems(): |
|
214 | for name, fn in filtertable.iteritems(): | |
215 | if cmd.startswith(name): |
|
215 | if cmd.startswith(name): | |
216 | return fn(s, cmd[len(name):].lstrip()) |
|
216 | return fn(s, cmd[len(name):].lstrip()) | |
217 | return pipefilter(s, cmd) |
|
217 | return pipefilter(s, cmd) | |
218 |
|
218 | |||
219 | def binary(s): |
|
219 | def binary(s): | |
220 | """return true if a string is binary data using diff's heuristic""" |
|
220 | """return true if a string is binary data using diff's heuristic""" | |
221 | if s and '\0' in s[:4096]: |
|
221 | if s and '\0' in s[:4096]: | |
222 | return True |
|
222 | return True | |
223 | return False |
|
223 | return False | |
224 |
|
224 | |||
225 | def unique(g): |
|
225 | def unique(g): | |
226 | """return the uniq elements of iterable g""" |
|
226 | """return the uniq elements of iterable g""" | |
227 | return dict.fromkeys(g).keys() |
|
227 | return dict.fromkeys(g).keys() | |
228 |
|
228 | |||
229 | class Abort(Exception): |
|
229 | class Abort(Exception): | |
230 | """Raised if a command needs to print an error and exit.""" |
|
230 | """Raised if a command needs to print an error and exit.""" | |
231 |
|
231 | |||
232 | class UnexpectedOutput(Abort): |
|
232 | class UnexpectedOutput(Abort): | |
233 | """Raised to print an error with part of output and exit.""" |
|
233 | """Raised to print an error with part of output and exit.""" | |
234 |
|
234 | |||
235 | def always(fn): return True |
|
235 | def always(fn): return True | |
236 | def never(fn): return False |
|
236 | def never(fn): return False | |
237 |
|
237 | |||
238 | def expand_glob(pats): |
|
238 | def expand_glob(pats): | |
239 | '''On Windows, expand the implicit globs in a list of patterns''' |
|
239 | '''On Windows, expand the implicit globs in a list of patterns''' | |
240 | if os.name != 'nt': |
|
240 | if os.name != 'nt': | |
241 | return list(pats) |
|
241 | return list(pats) | |
242 | ret = [] |
|
242 | ret = [] | |
243 | for p in pats: |
|
243 | for p in pats: | |
244 | kind, name = patkind(p, None) |
|
244 | kind, name = patkind(p, None) | |
245 | if kind is None: |
|
245 | if kind is None: | |
246 | globbed = glob.glob(name) |
|
246 | globbed = glob.glob(name) | |
247 | if globbed: |
|
247 | if globbed: | |
248 | ret.extend(globbed) |
|
248 | ret.extend(globbed) | |
249 | continue |
|
249 | continue | |
250 | # if we couldn't expand the glob, just keep it around |
|
250 | # if we couldn't expand the glob, just keep it around | |
251 | ret.append(p) |
|
251 | ret.append(p) | |
252 | return ret |
|
252 | return ret | |
253 |
|
253 | |||
254 | def patkind(name, dflt_pat='glob'): |
|
254 | def patkind(name, dflt_pat='glob'): | |
255 | """Split a string into an optional pattern kind prefix and the |
|
255 | """Split a string into an optional pattern kind prefix and the | |
256 | actual pattern.""" |
|
256 | actual pattern.""" | |
257 | for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre': |
|
257 | for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre': | |
258 | if name.startswith(prefix + ':'): return name.split(':', 1) |
|
258 | if name.startswith(prefix + ':'): return name.split(':', 1) | |
259 | return dflt_pat, name |
|
259 | return dflt_pat, name | |
260 |
|
260 | |||
261 | def globre(pat, head='^', tail='$'): |
|
261 | def globre(pat, head='^', tail='$'): | |
262 | "convert a glob pattern into a regexp" |
|
262 | "convert a glob pattern into a regexp" | |
263 | i, n = 0, len(pat) |
|
263 | i, n = 0, len(pat) | |
264 | res = '' |
|
264 | res = '' | |
265 | group = 0 |
|
265 | group = 0 | |
266 | def peek(): return i < n and pat[i] |
|
266 | def peek(): return i < n and pat[i] | |
267 | while i < n: |
|
267 | while i < n: | |
268 | c = pat[i] |
|
268 | c = pat[i] | |
269 | i = i+1 |
|
269 | i = i+1 | |
270 | if c == '*': |
|
270 | if c == '*': | |
271 | if peek() == '*': |
|
271 | if peek() == '*': | |
272 | i += 1 |
|
272 | i += 1 | |
273 | res += '.*' |
|
273 | res += '.*' | |
274 | else: |
|
274 | else: | |
275 | res += '[^/]*' |
|
275 | res += '[^/]*' | |
276 | elif c == '?': |
|
276 | elif c == '?': | |
277 | res += '.' |
|
277 | res += '.' | |
278 | elif c == '[': |
|
278 | elif c == '[': | |
279 | j = i |
|
279 | j = i | |
280 | if j < n and pat[j] in '!]': |
|
280 | if j < n and pat[j] in '!]': | |
281 | j += 1 |
|
281 | j += 1 | |
282 | while j < n and pat[j] != ']': |
|
282 | while j < n and pat[j] != ']': | |
283 | j += 1 |
|
283 | j += 1 | |
284 | if j >= n: |
|
284 | if j >= n: | |
285 | res += '\\[' |
|
285 | res += '\\[' | |
286 | else: |
|
286 | else: | |
287 | stuff = pat[i:j].replace('\\','\\\\') |
|
287 | stuff = pat[i:j].replace('\\','\\\\') | |
288 | i = j + 1 |
|
288 | i = j + 1 | |
289 | if stuff[0] == '!': |
|
289 | if stuff[0] == '!': | |
290 | stuff = '^' + stuff[1:] |
|
290 | stuff = '^' + stuff[1:] | |
291 | elif stuff[0] == '^': |
|
291 | elif stuff[0] == '^': | |
292 | stuff = '\\' + stuff |
|
292 | stuff = '\\' + stuff | |
293 | res = '%s[%s]' % (res, stuff) |
|
293 | res = '%s[%s]' % (res, stuff) | |
294 | elif c == '{': |
|
294 | elif c == '{': | |
295 | group += 1 |
|
295 | group += 1 | |
296 | res += '(?:' |
|
296 | res += '(?:' | |
297 | elif c == '}' and group: |
|
297 | elif c == '}' and group: | |
298 | res += ')' |
|
298 | res += ')' | |
299 | group -= 1 |
|
299 | group -= 1 | |
300 | elif c == ',' and group: |
|
300 | elif c == ',' and group: | |
301 | res += '|' |
|
301 | res += '|' | |
302 | elif c == '\\': |
|
302 | elif c == '\\': | |
303 | p = peek() |
|
303 | p = peek() | |
304 | if p: |
|
304 | if p: | |
305 | i += 1 |
|
305 | i += 1 | |
306 | res += re.escape(p) |
|
306 | res += re.escape(p) | |
307 | else: |
|
307 | else: | |
308 | res += re.escape(c) |
|
308 | res += re.escape(c) | |
309 | else: |
|
309 | else: | |
310 | res += re.escape(c) |
|
310 | res += re.escape(c) | |
311 | return head + res + tail |
|
311 | return head + res + tail | |
312 |
|
312 | |||
313 | _globchars = {'[': 1, '{': 1, '*': 1, '?': 1} |
|
313 | _globchars = {'[': 1, '{': 1, '*': 1, '?': 1} | |
314 |
|
314 | |||
315 | def pathto(root, n1, n2): |
|
315 | def pathto(root, n1, n2): | |
316 | '''return the relative path from one place to another. |
|
316 | '''return the relative path from one place to another. | |
317 | root should use os.sep to separate directories |
|
317 | root should use os.sep to separate directories | |
318 | n1 should use os.sep to separate directories |
|
318 | n1 should use os.sep to separate directories | |
319 | n2 should use "/" to separate directories |
|
319 | n2 should use "/" to separate directories | |
320 | returns an os.sep-separated path. |
|
320 | returns an os.sep-separated path. | |
321 |
|
321 | |||
322 | If n1 is a relative path, it's assumed it's |
|
322 | If n1 is a relative path, it's assumed it's | |
323 | relative to root. |
|
323 | relative to root. | |
324 | n2 should always be relative to root. |
|
324 | n2 should always be relative to root. | |
325 | ''' |
|
325 | ''' | |
326 | if not n1: return localpath(n2) |
|
326 | if not n1: return localpath(n2) | |
327 | if os.path.isabs(n1): |
|
327 | if os.path.isabs(n1): | |
328 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
|
328 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: | |
329 | return os.path.join(root, localpath(n2)) |
|
329 | return os.path.join(root, localpath(n2)) | |
330 | n2 = '/'.join((pconvert(root), n2)) |
|
330 | n2 = '/'.join((pconvert(root), n2)) | |
331 | a, b = splitpath(n1), n2.split('/') |
|
331 | a, b = splitpath(n1), n2.split('/') | |
332 | a.reverse() |
|
332 | a.reverse() | |
333 | b.reverse() |
|
333 | b.reverse() | |
334 | while a and b and a[-1] == b[-1]: |
|
334 | while a and b and a[-1] == b[-1]: | |
335 | a.pop() |
|
335 | a.pop() | |
336 | b.pop() |
|
336 | b.pop() | |
337 | b.reverse() |
|
337 | b.reverse() | |
338 | return os.sep.join((['..'] * len(a)) + b) |
|
338 | return os.sep.join((['..'] * len(a)) + b) or '.' | |
339 |
|
339 | |||
340 | def canonpath(root, cwd, myname): |
|
340 | def canonpath(root, cwd, myname): | |
341 | """return the canonical path of myname, given cwd and root""" |
|
341 | """return the canonical path of myname, given cwd and root""" | |
342 | if root == os.sep: |
|
342 | if root == os.sep: | |
343 | rootsep = os.sep |
|
343 | rootsep = os.sep | |
344 | elif endswithsep(root): |
|
344 | elif endswithsep(root): | |
345 | rootsep = root |
|
345 | rootsep = root | |
346 | else: |
|
346 | else: | |
347 | rootsep = root + os.sep |
|
347 | rootsep = root + os.sep | |
348 | name = myname |
|
348 | name = myname | |
349 | if not os.path.isabs(name): |
|
349 | if not os.path.isabs(name): | |
350 | name = os.path.join(root, cwd, name) |
|
350 | name = os.path.join(root, cwd, name) | |
351 | name = os.path.normpath(name) |
|
351 | name = os.path.normpath(name) | |
352 | audit_path = path_auditor(root) |
|
352 | audit_path = path_auditor(root) | |
353 | if name != rootsep and name.startswith(rootsep): |
|
353 | if name != rootsep and name.startswith(rootsep): | |
354 | name = name[len(rootsep):] |
|
354 | name = name[len(rootsep):] | |
355 | audit_path(name) |
|
355 | audit_path(name) | |
356 | return pconvert(name) |
|
356 | return pconvert(name) | |
357 | elif name == root: |
|
357 | elif name == root: | |
358 | return '' |
|
358 | return '' | |
359 | else: |
|
359 | else: | |
360 | # Determine whether `name' is in the hierarchy at or beneath `root', |
|
360 | # Determine whether `name' is in the hierarchy at or beneath `root', | |
361 | # by iterating name=dirname(name) until that causes no change (can't |
|
361 | # by iterating name=dirname(name) until that causes no change (can't | |
362 | # check name == '/', because that doesn't work on windows). For each |
|
362 | # check name == '/', because that doesn't work on windows). For each | |
363 | # `name', compare dev/inode numbers. If they match, the list `rel' |
|
363 | # `name', compare dev/inode numbers. If they match, the list `rel' | |
364 | # holds the reversed list of components making up the relative file |
|
364 | # holds the reversed list of components making up the relative file | |
365 | # name we want. |
|
365 | # name we want. | |
366 | root_st = os.stat(root) |
|
366 | root_st = os.stat(root) | |
367 | rel = [] |
|
367 | rel = [] | |
368 | while True: |
|
368 | while True: | |
369 | try: |
|
369 | try: | |
370 | name_st = os.stat(name) |
|
370 | name_st = os.stat(name) | |
371 | except OSError: |
|
371 | except OSError: | |
372 | break |
|
372 | break | |
373 | if samestat(name_st, root_st): |
|
373 | if samestat(name_st, root_st): | |
374 | if not rel: |
|
374 | if not rel: | |
375 | # name was actually the same as root (maybe a symlink) |
|
375 | # name was actually the same as root (maybe a symlink) | |
376 | return '' |
|
376 | return '' | |
377 | rel.reverse() |
|
377 | rel.reverse() | |
378 | name = os.path.join(*rel) |
|
378 | name = os.path.join(*rel) | |
379 | audit_path(name) |
|
379 | audit_path(name) | |
380 | return pconvert(name) |
|
380 | return pconvert(name) | |
381 | dirname, basename = os.path.split(name) |
|
381 | dirname, basename = os.path.split(name) | |
382 | rel.append(basename) |
|
382 | rel.append(basename) | |
383 | if dirname == name: |
|
383 | if dirname == name: | |
384 | break |
|
384 | break | |
385 | name = dirname |
|
385 | name = dirname | |
386 |
|
386 | |||
387 | raise Abort('%s not under root' % myname) |
|
387 | raise Abort('%s not under root' % myname) | |
388 |
|
388 | |||
389 | def matcher(canonroot, cwd='', names=[], inc=[], exc=[], src=None): |
|
389 | def matcher(canonroot, cwd='', names=[], inc=[], exc=[], src=None): | |
390 | return _matcher(canonroot, cwd, names, inc, exc, 'glob', src) |
|
390 | return _matcher(canonroot, cwd, names, inc, exc, 'glob', src) | |
391 |
|
391 | |||
392 | def cmdmatcher(canonroot, cwd='', names=[], inc=[], exc=[], src=None, |
|
392 | def cmdmatcher(canonroot, cwd='', names=[], inc=[], exc=[], src=None, | |
393 | globbed=False, default=None): |
|
393 | globbed=False, default=None): | |
394 | default = default or 'relpath' |
|
394 | default = default or 'relpath' | |
395 | if default == 'relpath' and not globbed: |
|
395 | if default == 'relpath' and not globbed: | |
396 | names = expand_glob(names) |
|
396 | names = expand_glob(names) | |
397 | return _matcher(canonroot, cwd, names, inc, exc, default, src) |
|
397 | return _matcher(canonroot, cwd, names, inc, exc, default, src) | |
398 |
|
398 | |||
399 | def _matcher(canonroot, cwd, names, inc, exc, dflt_pat, src): |
|
399 | def _matcher(canonroot, cwd, names, inc, exc, dflt_pat, src): | |
400 | """build a function to match a set of file patterns |
|
400 | """build a function to match a set of file patterns | |
401 |
|
401 | |||
402 | arguments: |
|
402 | arguments: | |
403 | canonroot - the canonical root of the tree you're matching against |
|
403 | canonroot - the canonical root of the tree you're matching against | |
404 | cwd - the current working directory, if relevant |
|
404 | cwd - the current working directory, if relevant | |
405 | names - patterns to find |
|
405 | names - patterns to find | |
406 | inc - patterns to include |
|
406 | inc - patterns to include | |
407 | exc - patterns to exclude |
|
407 | exc - patterns to exclude | |
408 | dflt_pat - if a pattern in names has no explicit type, assume this one |
|
408 | dflt_pat - if a pattern in names has no explicit type, assume this one | |
409 | src - where these patterns came from (e.g. .hgignore) |
|
409 | src - where these patterns came from (e.g. .hgignore) | |
410 |
|
410 | |||
411 | a pattern is one of: |
|
411 | a pattern is one of: | |
412 | 'glob:<glob>' - a glob relative to cwd |
|
412 | 'glob:<glob>' - a glob relative to cwd | |
413 | 're:<regexp>' - a regular expression |
|
413 | 're:<regexp>' - a regular expression | |
414 | 'path:<path>' - a path relative to canonroot |
|
414 | 'path:<path>' - a path relative to canonroot | |
415 | 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs) |
|
415 | 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs) | |
416 | 'relpath:<path>' - a path relative to cwd |
|
416 | 'relpath:<path>' - a path relative to cwd | |
417 | 'relre:<regexp>' - a regexp that doesn't have to match the start of a name |
|
417 | 'relre:<regexp>' - a regexp that doesn't have to match the start of a name | |
418 | '<something>' - one of the cases above, selected by the dflt_pat argument |
|
418 | '<something>' - one of the cases above, selected by the dflt_pat argument | |
419 |
|
419 | |||
420 | returns: |
|
420 | returns: | |
421 | a 3-tuple containing |
|
421 | a 3-tuple containing | |
422 | - list of roots (places where one should start a recursive walk of the fs); |
|
422 | - list of roots (places where one should start a recursive walk of the fs); | |
423 | this often matches the explicit non-pattern names passed in, but also |
|
423 | this often matches the explicit non-pattern names passed in, but also | |
424 | includes the initial part of glob: patterns that has no glob characters |
|
424 | includes the initial part of glob: patterns that has no glob characters | |
425 | - a bool match(filename) function |
|
425 | - a bool match(filename) function | |
426 | - a bool indicating if any patterns were passed in |
|
426 | - a bool indicating if any patterns were passed in | |
427 | """ |
|
427 | """ | |
428 |
|
428 | |||
429 | # a common case: no patterns at all |
|
429 | # a common case: no patterns at all | |
430 | if not names and not inc and not exc: |
|
430 | if not names and not inc and not exc: | |
431 | return [], always, False |
|
431 | return [], always, False | |
432 |
|
432 | |||
433 | def contains_glob(name): |
|
433 | def contains_glob(name): | |
434 | for c in name: |
|
434 | for c in name: | |
435 | if c in _globchars: return True |
|
435 | if c in _globchars: return True | |
436 | return False |
|
436 | return False | |
437 |
|
437 | |||
438 | def regex(kind, name, tail): |
|
438 | def regex(kind, name, tail): | |
439 | '''convert a pattern into a regular expression''' |
|
439 | '''convert a pattern into a regular expression''' | |
440 | if not name: |
|
440 | if not name: | |
441 | return '' |
|
441 | return '' | |
442 | if kind == 're': |
|
442 | if kind == 're': | |
443 | return name |
|
443 | return name | |
444 | elif kind == 'path': |
|
444 | elif kind == 'path': | |
445 | return '^' + re.escape(name) + '(?:/|$)' |
|
445 | return '^' + re.escape(name) + '(?:/|$)' | |
446 | elif kind == 'relglob': |
|
446 | elif kind == 'relglob': | |
447 | return globre(name, '(?:|.*/)', tail) |
|
447 | return globre(name, '(?:|.*/)', tail) | |
448 | elif kind == 'relpath': |
|
448 | elif kind == 'relpath': | |
449 | return re.escape(name) + '(?:/|$)' |
|
449 | return re.escape(name) + '(?:/|$)' | |
450 | elif kind == 'relre': |
|
450 | elif kind == 'relre': | |
451 | if name.startswith('^'): |
|
451 | if name.startswith('^'): | |
452 | return name |
|
452 | return name | |
453 | return '.*' + name |
|
453 | return '.*' + name | |
454 | return globre(name, '', tail) |
|
454 | return globre(name, '', tail) | |
455 |
|
455 | |||
456 | def matchfn(pats, tail): |
|
456 | def matchfn(pats, tail): | |
457 | """build a matching function from a set of patterns""" |
|
457 | """build a matching function from a set of patterns""" | |
458 | if not pats: |
|
458 | if not pats: | |
459 | return |
|
459 | return | |
460 | try: |
|
460 | try: | |
461 | pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats]) |
|
461 | pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats]) | |
462 | if len(pat) > 20000: |
|
462 | if len(pat) > 20000: | |
463 | raise OverflowError() |
|
463 | raise OverflowError() | |
464 | return re.compile(pat).match |
|
464 | return re.compile(pat).match | |
465 | except OverflowError: |
|
465 | except OverflowError: | |
466 | # We're using a Python with a tiny regex engine and we |
|
466 | # We're using a Python with a tiny regex engine and we | |
467 | # made it explode, so we'll divide the pattern list in two |
|
467 | # made it explode, so we'll divide the pattern list in two | |
468 | # until it works |
|
468 | # until it works | |
469 | l = len(pats) |
|
469 | l = len(pats) | |
470 | if l < 2: |
|
470 | if l < 2: | |
471 | raise |
|
471 | raise | |
472 | a, b = matchfn(pats[:l//2], tail), matchfn(pats[l//2:], tail) |
|
472 | a, b = matchfn(pats[:l//2], tail), matchfn(pats[l//2:], tail) | |
473 | return lambda s: a(s) or b(s) |
|
473 | return lambda s: a(s) or b(s) | |
474 | except re.error: |
|
474 | except re.error: | |
475 | for k, p in pats: |
|
475 | for k, p in pats: | |
476 | try: |
|
476 | try: | |
477 | re.compile('(?:%s)' % regex(k, p, tail)) |
|
477 | re.compile('(?:%s)' % regex(k, p, tail)) | |
478 | except re.error: |
|
478 | except re.error: | |
479 | if src: |
|
479 | if src: | |
480 | raise Abort("%s: invalid pattern (%s): %s" % |
|
480 | raise Abort("%s: invalid pattern (%s): %s" % | |
481 | (src, k, p)) |
|
481 | (src, k, p)) | |
482 | else: |
|
482 | else: | |
483 | raise Abort("invalid pattern (%s): %s" % (k, p)) |
|
483 | raise Abort("invalid pattern (%s): %s" % (k, p)) | |
484 | raise Abort("invalid pattern") |
|
484 | raise Abort("invalid pattern") | |
485 |
|
485 | |||
486 | def globprefix(pat): |
|
486 | def globprefix(pat): | |
487 | '''return the non-glob prefix of a path, e.g. foo/* -> foo''' |
|
487 | '''return the non-glob prefix of a path, e.g. foo/* -> foo''' | |
488 | root = [] |
|
488 | root = [] | |
489 | for p in pat.split('/'): |
|
489 | for p in pat.split('/'): | |
490 | if contains_glob(p): break |
|
490 | if contains_glob(p): break | |
491 | root.append(p) |
|
491 | root.append(p) | |
492 | return '/'.join(root) or '.' |
|
492 | return '/'.join(root) or '.' | |
493 |
|
493 | |||
494 | def normalizepats(names, default): |
|
494 | def normalizepats(names, default): | |
495 | pats = [] |
|
495 | pats = [] | |
496 | roots = [] |
|
496 | roots = [] | |
497 | anypats = False |
|
497 | anypats = False | |
498 | for kind, name in [patkind(p, default) for p in names]: |
|
498 | for kind, name in [patkind(p, default) for p in names]: | |
499 | if kind in ('glob', 'relpath'): |
|
499 | if kind in ('glob', 'relpath'): | |
500 | name = canonpath(canonroot, cwd, name) |
|
500 | name = canonpath(canonroot, cwd, name) | |
501 | elif kind in ('relglob', 'path'): |
|
501 | elif kind in ('relglob', 'path'): | |
502 | name = normpath(name) |
|
502 | name = normpath(name) | |
503 |
|
503 | |||
504 | pats.append((kind, name)) |
|
504 | pats.append((kind, name)) | |
505 |
|
505 | |||
506 | if kind in ('glob', 're', 'relglob', 'relre'): |
|
506 | if kind in ('glob', 're', 'relglob', 'relre'): | |
507 | anypats = True |
|
507 | anypats = True | |
508 |
|
508 | |||
509 | if kind == 'glob': |
|
509 | if kind == 'glob': | |
510 | root = globprefix(name) |
|
510 | root = globprefix(name) | |
511 | roots.append(root) |
|
511 | roots.append(root) | |
512 | elif kind in ('relpath', 'path'): |
|
512 | elif kind in ('relpath', 'path'): | |
513 | roots.append(name or '.') |
|
513 | roots.append(name or '.') | |
514 | elif kind == 'relglob': |
|
514 | elif kind == 'relglob': | |
515 | roots.append('.') |
|
515 | roots.append('.') | |
516 | return roots, pats, anypats |
|
516 | return roots, pats, anypats | |
517 |
|
517 | |||
518 | roots, pats, anypats = normalizepats(names, dflt_pat) |
|
518 | roots, pats, anypats = normalizepats(names, dflt_pat) | |
519 |
|
519 | |||
520 | patmatch = matchfn(pats, '$') or always |
|
520 | patmatch = matchfn(pats, '$') or always | |
521 | incmatch = always |
|
521 | incmatch = always | |
522 | if inc: |
|
522 | if inc: | |
523 | dummy, inckinds, dummy = normalizepats(inc, 'glob') |
|
523 | dummy, inckinds, dummy = normalizepats(inc, 'glob') | |
524 | incmatch = matchfn(inckinds, '(?:/|$)') |
|
524 | incmatch = matchfn(inckinds, '(?:/|$)') | |
525 | excmatch = lambda fn: False |
|
525 | excmatch = lambda fn: False | |
526 | if exc: |
|
526 | if exc: | |
527 | dummy, exckinds, dummy = normalizepats(exc, 'glob') |
|
527 | dummy, exckinds, dummy = normalizepats(exc, 'glob') | |
528 | excmatch = matchfn(exckinds, '(?:/|$)') |
|
528 | excmatch = matchfn(exckinds, '(?:/|$)') | |
529 |
|
529 | |||
530 | if not names and inc and not exc: |
|
530 | if not names and inc and not exc: | |
531 | # common case: hgignore patterns |
|
531 | # common case: hgignore patterns | |
532 | match = incmatch |
|
532 | match = incmatch | |
533 | else: |
|
533 | else: | |
534 | match = lambda fn: incmatch(fn) and not excmatch(fn) and patmatch(fn) |
|
534 | match = lambda fn: incmatch(fn) and not excmatch(fn) and patmatch(fn) | |
535 |
|
535 | |||
536 | return (roots, match, (inc or exc or anypats) and True) |
|
536 | return (roots, match, (inc or exc or anypats) and True) | |
537 |
|
537 | |||
538 | _hgexecutable = None |
|
538 | _hgexecutable = None | |
539 |
|
539 | |||
540 | def hgexecutable(): |
|
540 | def hgexecutable(): | |
541 | """return location of the 'hg' executable. |
|
541 | """return location of the 'hg' executable. | |
542 |
|
542 | |||
543 | Defaults to $HG or 'hg' in the search path. |
|
543 | Defaults to $HG or 'hg' in the search path. | |
544 | """ |
|
544 | """ | |
545 | if _hgexecutable is None: |
|
545 | if _hgexecutable is None: | |
546 | set_hgexecutable(os.environ.get('HG') or find_exe('hg', 'hg')) |
|
546 | set_hgexecutable(os.environ.get('HG') or find_exe('hg', 'hg')) | |
547 | return _hgexecutable |
|
547 | return _hgexecutable | |
548 |
|
548 | |||
549 | def set_hgexecutable(path): |
|
549 | def set_hgexecutable(path): | |
550 | """set location of the 'hg' executable""" |
|
550 | """set location of the 'hg' executable""" | |
551 | global _hgexecutable |
|
551 | global _hgexecutable | |
552 | _hgexecutable = path |
|
552 | _hgexecutable = path | |
553 |
|
553 | |||
554 | def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None): |
|
554 | def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None): | |
555 | '''enhanced shell command execution. |
|
555 | '''enhanced shell command execution. | |
556 | run with environment maybe modified, maybe in different dir. |
|
556 | run with environment maybe modified, maybe in different dir. | |
557 |
|
557 | |||
558 | if command fails and onerr is None, return status. if ui object, |
|
558 | if command fails and onerr is None, return status. if ui object, | |
559 | print error message and return status, else raise onerr object as |
|
559 | print error message and return status, else raise onerr object as | |
560 | exception.''' |
|
560 | exception.''' | |
561 | def py2shell(val): |
|
561 | def py2shell(val): | |
562 | 'convert python object into string that is useful to shell' |
|
562 | 'convert python object into string that is useful to shell' | |
563 | if val in (None, False): |
|
563 | if val in (None, False): | |
564 | return '0' |
|
564 | return '0' | |
565 | if val == True: |
|
565 | if val == True: | |
566 | return '1' |
|
566 | return '1' | |
567 | return str(val) |
|
567 | return str(val) | |
568 | oldenv = {} |
|
568 | oldenv = {} | |
569 | for k in environ: |
|
569 | for k in environ: | |
570 | oldenv[k] = os.environ.get(k) |
|
570 | oldenv[k] = os.environ.get(k) | |
571 | if cwd is not None: |
|
571 | if cwd is not None: | |
572 | oldcwd = os.getcwd() |
|
572 | oldcwd = os.getcwd() | |
573 | origcmd = cmd |
|
573 | origcmd = cmd | |
574 | if os.name == 'nt': |
|
574 | if os.name == 'nt': | |
575 | cmd = '"%s"' % cmd |
|
575 | cmd = '"%s"' % cmd | |
576 | try: |
|
576 | try: | |
577 | for k, v in environ.iteritems(): |
|
577 | for k, v in environ.iteritems(): | |
578 | os.environ[k] = py2shell(v) |
|
578 | os.environ[k] = py2shell(v) | |
579 | os.environ['HG'] = hgexecutable() |
|
579 | os.environ['HG'] = hgexecutable() | |
580 | if cwd is not None and oldcwd != cwd: |
|
580 | if cwd is not None and oldcwd != cwd: | |
581 | os.chdir(cwd) |
|
581 | os.chdir(cwd) | |
582 | rc = os.system(cmd) |
|
582 | rc = os.system(cmd) | |
583 | if sys.platform == 'OpenVMS' and rc & 1: |
|
583 | if sys.platform == 'OpenVMS' and rc & 1: | |
584 | rc = 0 |
|
584 | rc = 0 | |
585 | if rc and onerr: |
|
585 | if rc and onerr: | |
586 | errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), |
|
586 | errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), | |
587 | explain_exit(rc)[0]) |
|
587 | explain_exit(rc)[0]) | |
588 | if errprefix: |
|
588 | if errprefix: | |
589 | errmsg = '%s: %s' % (errprefix, errmsg) |
|
589 | errmsg = '%s: %s' % (errprefix, errmsg) | |
590 | try: |
|
590 | try: | |
591 | onerr.warn(errmsg + '\n') |
|
591 | onerr.warn(errmsg + '\n') | |
592 | except AttributeError: |
|
592 | except AttributeError: | |
593 | raise onerr(errmsg) |
|
593 | raise onerr(errmsg) | |
594 | return rc |
|
594 | return rc | |
595 | finally: |
|
595 | finally: | |
596 | for k, v in oldenv.iteritems(): |
|
596 | for k, v in oldenv.iteritems(): | |
597 | if v is None: |
|
597 | if v is None: | |
598 | del os.environ[k] |
|
598 | del os.environ[k] | |
599 | else: |
|
599 | else: | |
600 | os.environ[k] = v |
|
600 | os.environ[k] = v | |
601 | if cwd is not None and oldcwd != cwd: |
|
601 | if cwd is not None and oldcwd != cwd: | |
602 | os.chdir(oldcwd) |
|
602 | os.chdir(oldcwd) | |
603 |
|
603 | |||
604 | # os.path.lexists is not available on python2.3 |
|
604 | # os.path.lexists is not available on python2.3 | |
605 | def lexists(filename): |
|
605 | def lexists(filename): | |
606 | "test whether a file with this name exists. does not follow symlinks" |
|
606 | "test whether a file with this name exists. does not follow symlinks" | |
607 | try: |
|
607 | try: | |
608 | os.lstat(filename) |
|
608 | os.lstat(filename) | |
609 | except: |
|
609 | except: | |
610 | return False |
|
610 | return False | |
611 | return True |
|
611 | return True | |
612 |
|
612 | |||
613 | def rename(src, dst): |
|
613 | def rename(src, dst): | |
614 | """forcibly rename a file""" |
|
614 | """forcibly rename a file""" | |
615 | try: |
|
615 | try: | |
616 | os.rename(src, dst) |
|
616 | os.rename(src, dst) | |
617 | except OSError, err: # FIXME: check err (EEXIST ?) |
|
617 | except OSError, err: # FIXME: check err (EEXIST ?) | |
618 | # on windows, rename to existing file is not allowed, so we |
|
618 | # on windows, rename to existing file is not allowed, so we | |
619 | # must delete destination first. but if file is open, unlink |
|
619 | # must delete destination first. but if file is open, unlink | |
620 | # schedules it for delete but does not delete it. rename |
|
620 | # schedules it for delete but does not delete it. rename | |
621 | # happens immediately even for open files, so we create |
|
621 | # happens immediately even for open files, so we create | |
622 | # temporary file, delete it, rename destination to that name, |
|
622 | # temporary file, delete it, rename destination to that name, | |
623 | # then delete that. then rename is safe to do. |
|
623 | # then delete that. then rename is safe to do. | |
624 | fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.') |
|
624 | fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.') | |
625 | os.close(fd) |
|
625 | os.close(fd) | |
626 | os.unlink(temp) |
|
626 | os.unlink(temp) | |
627 | os.rename(dst, temp) |
|
627 | os.rename(dst, temp) | |
628 | os.unlink(temp) |
|
628 | os.unlink(temp) | |
629 | os.rename(src, dst) |
|
629 | os.rename(src, dst) | |
630 |
|
630 | |||
631 | def unlink(f): |
|
631 | def unlink(f): | |
632 | """unlink and remove the directory if it is empty""" |
|
632 | """unlink and remove the directory if it is empty""" | |
633 | os.unlink(f) |
|
633 | os.unlink(f) | |
634 | # try removing directories that might now be empty |
|
634 | # try removing directories that might now be empty | |
635 | try: |
|
635 | try: | |
636 | os.removedirs(os.path.dirname(f)) |
|
636 | os.removedirs(os.path.dirname(f)) | |
637 | except OSError: |
|
637 | except OSError: | |
638 | pass |
|
638 | pass | |
639 |
|
639 | |||
640 | def copyfile(src, dest): |
|
640 | def copyfile(src, dest): | |
641 | "copy a file, preserving mode" |
|
641 | "copy a file, preserving mode" | |
642 | if os.path.islink(src): |
|
642 | if os.path.islink(src): | |
643 | try: |
|
643 | try: | |
644 | os.unlink(dest) |
|
644 | os.unlink(dest) | |
645 | except: |
|
645 | except: | |
646 | pass |
|
646 | pass | |
647 | os.symlink(os.readlink(src), dest) |
|
647 | os.symlink(os.readlink(src), dest) | |
648 | else: |
|
648 | else: | |
649 | try: |
|
649 | try: | |
650 | shutil.copyfile(src, dest) |
|
650 | shutil.copyfile(src, dest) | |
651 | shutil.copymode(src, dest) |
|
651 | shutil.copymode(src, dest) | |
652 | except shutil.Error, inst: |
|
652 | except shutil.Error, inst: | |
653 | raise Abort(str(inst)) |
|
653 | raise Abort(str(inst)) | |
654 |
|
654 | |||
655 | def copyfiles(src, dst, hardlink=None): |
|
655 | def copyfiles(src, dst, hardlink=None): | |
656 | """Copy a directory tree using hardlinks if possible""" |
|
656 | """Copy a directory tree using hardlinks if possible""" | |
657 |
|
657 | |||
658 | if hardlink is None: |
|
658 | if hardlink is None: | |
659 | hardlink = (os.stat(src).st_dev == |
|
659 | hardlink = (os.stat(src).st_dev == | |
660 | os.stat(os.path.dirname(dst)).st_dev) |
|
660 | os.stat(os.path.dirname(dst)).st_dev) | |
661 |
|
661 | |||
662 | if os.path.isdir(src): |
|
662 | if os.path.isdir(src): | |
663 | os.mkdir(dst) |
|
663 | os.mkdir(dst) | |
664 | for name, kind in osutil.listdir(src): |
|
664 | for name, kind in osutil.listdir(src): | |
665 | srcname = os.path.join(src, name) |
|
665 | srcname = os.path.join(src, name) | |
666 | dstname = os.path.join(dst, name) |
|
666 | dstname = os.path.join(dst, name) | |
667 | copyfiles(srcname, dstname, hardlink) |
|
667 | copyfiles(srcname, dstname, hardlink) | |
668 | else: |
|
668 | else: | |
669 | if hardlink: |
|
669 | if hardlink: | |
670 | try: |
|
670 | try: | |
671 | os_link(src, dst) |
|
671 | os_link(src, dst) | |
672 | except (IOError, OSError): |
|
672 | except (IOError, OSError): | |
673 | hardlink = False |
|
673 | hardlink = False | |
674 | shutil.copy(src, dst) |
|
674 | shutil.copy(src, dst) | |
675 | else: |
|
675 | else: | |
676 | shutil.copy(src, dst) |
|
676 | shutil.copy(src, dst) | |
677 |
|
677 | |||
678 | class path_auditor(object): |
|
678 | class path_auditor(object): | |
679 | '''ensure that a filesystem path contains no banned components. |
|
679 | '''ensure that a filesystem path contains no banned components. | |
680 | the following properties of a path are checked: |
|
680 | the following properties of a path are checked: | |
681 |
|
681 | |||
682 | - under top-level .hg |
|
682 | - under top-level .hg | |
683 | - starts at the root of a windows drive |
|
683 | - starts at the root of a windows drive | |
684 | - contains ".." |
|
684 | - contains ".." | |
685 | - traverses a symlink (e.g. a/symlink_here/b) |
|
685 | - traverses a symlink (e.g. a/symlink_here/b) | |
686 | - inside a nested repository''' |
|
686 | - inside a nested repository''' | |
687 |
|
687 | |||
688 | def __init__(self, root): |
|
688 | def __init__(self, root): | |
689 | self.audited = set() |
|
689 | self.audited = set() | |
690 | self.auditeddir = set() |
|
690 | self.auditeddir = set() | |
691 | self.root = root |
|
691 | self.root = root | |
692 |
|
692 | |||
693 | def __call__(self, path): |
|
693 | def __call__(self, path): | |
694 | if path in self.audited: |
|
694 | if path in self.audited: | |
695 | return |
|
695 | return | |
696 | normpath = os.path.normcase(path) |
|
696 | normpath = os.path.normcase(path) | |
697 | parts = splitpath(normpath) |
|
697 | parts = splitpath(normpath) | |
698 | if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '') |
|
698 | if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '') | |
699 | or os.pardir in parts): |
|
699 | or os.pardir in parts): | |
700 | raise Abort(_("path contains illegal component: %s") % path) |
|
700 | raise Abort(_("path contains illegal component: %s") % path) | |
701 | def check(prefix): |
|
701 | def check(prefix): | |
702 | curpath = os.path.join(self.root, prefix) |
|
702 | curpath = os.path.join(self.root, prefix) | |
703 | try: |
|
703 | try: | |
704 | st = os.lstat(curpath) |
|
704 | st = os.lstat(curpath) | |
705 | except OSError, err: |
|
705 | except OSError, err: | |
706 | # EINVAL can be raised as invalid path syntax under win32. |
|
706 | # EINVAL can be raised as invalid path syntax under win32. | |
707 | # They must be ignored for patterns can be checked too. |
|
707 | # They must be ignored for patterns can be checked too. | |
708 | if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL): |
|
708 | if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL): | |
709 | raise |
|
709 | raise | |
710 | else: |
|
710 | else: | |
711 | if stat.S_ISLNK(st.st_mode): |
|
711 | if stat.S_ISLNK(st.st_mode): | |
712 | raise Abort(_('path %r traverses symbolic link %r') % |
|
712 | raise Abort(_('path %r traverses symbolic link %r') % | |
713 | (path, prefix)) |
|
713 | (path, prefix)) | |
714 | elif (stat.S_ISDIR(st.st_mode) and |
|
714 | elif (stat.S_ISDIR(st.st_mode) and | |
715 | os.path.isdir(os.path.join(curpath, '.hg'))): |
|
715 | os.path.isdir(os.path.join(curpath, '.hg'))): | |
716 | raise Abort(_('path %r is inside repo %r') % |
|
716 | raise Abort(_('path %r is inside repo %r') % | |
717 | (path, prefix)) |
|
717 | (path, prefix)) | |
718 | parts.pop() |
|
718 | parts.pop() | |
719 | prefixes = [] |
|
719 | prefixes = [] | |
720 | for n in range(len(parts)): |
|
720 | for n in range(len(parts)): | |
721 | prefix = os.sep.join(parts) |
|
721 | prefix = os.sep.join(parts) | |
722 | if prefix in self.auditeddir: |
|
722 | if prefix in self.auditeddir: | |
723 | break |
|
723 | break | |
724 | check(prefix) |
|
724 | check(prefix) | |
725 | prefixes.append(prefix) |
|
725 | prefixes.append(prefix) | |
726 | parts.pop() |
|
726 | parts.pop() | |
727 |
|
727 | |||
728 | self.audited.add(path) |
|
728 | self.audited.add(path) | |
729 | # only add prefixes to the cache after checking everything: we don't |
|
729 | # only add prefixes to the cache after checking everything: we don't | |
730 | # want to add "foo/bar/baz" before checking if there's a "foo/.hg" |
|
730 | # want to add "foo/bar/baz" before checking if there's a "foo/.hg" | |
731 | self.auditeddir.update(prefixes) |
|
731 | self.auditeddir.update(prefixes) | |
732 |
|
732 | |||
733 | def _makelock_file(info, pathname): |
|
733 | def _makelock_file(info, pathname): | |
734 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) |
|
734 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) | |
735 | os.write(ld, info) |
|
735 | os.write(ld, info) | |
736 | os.close(ld) |
|
736 | os.close(ld) | |
737 |
|
737 | |||
738 | def _readlock_file(pathname): |
|
738 | def _readlock_file(pathname): | |
739 | return posixfile(pathname).read() |
|
739 | return posixfile(pathname).read() | |
740 |
|
740 | |||
741 | def nlinks(pathname): |
|
741 | def nlinks(pathname): | |
742 | """Return number of hardlinks for the given file.""" |
|
742 | """Return number of hardlinks for the given file.""" | |
743 | return os.lstat(pathname).st_nlink |
|
743 | return os.lstat(pathname).st_nlink | |
744 |
|
744 | |||
745 | if hasattr(os, 'link'): |
|
745 | if hasattr(os, 'link'): | |
746 | os_link = os.link |
|
746 | os_link = os.link | |
747 | else: |
|
747 | else: | |
748 | def os_link(src, dst): |
|
748 | def os_link(src, dst): | |
749 | raise OSError(0, _("Hardlinks not supported")) |
|
749 | raise OSError(0, _("Hardlinks not supported")) | |
750 |
|
750 | |||
751 | def fstat(fp): |
|
751 | def fstat(fp): | |
752 | '''stat file object that may not have fileno method.''' |
|
752 | '''stat file object that may not have fileno method.''' | |
753 | try: |
|
753 | try: | |
754 | return os.fstat(fp.fileno()) |
|
754 | return os.fstat(fp.fileno()) | |
755 | except AttributeError: |
|
755 | except AttributeError: | |
756 | return os.stat(fp.name) |
|
756 | return os.stat(fp.name) | |
757 |
|
757 | |||
758 | posixfile = file |
|
758 | posixfile = file | |
759 |
|
759 | |||
760 | def openhardlinks(): |
|
760 | def openhardlinks(): | |
761 | '''return true if it is safe to hold open file handles to hardlinks''' |
|
761 | '''return true if it is safe to hold open file handles to hardlinks''' | |
762 | return True |
|
762 | return True | |
763 |
|
763 | |||
764 | getuser_fallback = None |
|
764 | getuser_fallback = None | |
765 |
|
765 | |||
766 | def getuser(): |
|
766 | def getuser(): | |
767 | '''return name of current user''' |
|
767 | '''return name of current user''' | |
768 | try: |
|
768 | try: | |
769 | return getpass.getuser() |
|
769 | return getpass.getuser() | |
770 | except ImportError: |
|
770 | except ImportError: | |
771 | # import of pwd will fail on windows - try fallback |
|
771 | # import of pwd will fail on windows - try fallback | |
772 | if getuser_fallback: |
|
772 | if getuser_fallback: | |
773 | return getuser_fallback() |
|
773 | return getuser_fallback() | |
774 | # raised if win32api not available |
|
774 | # raised if win32api not available | |
775 | raise Abort(_('user name not available - set USERNAME ' |
|
775 | raise Abort(_('user name not available - set USERNAME ' | |
776 | 'environment variable')) |
|
776 | 'environment variable')) | |
777 |
|
777 | |||
778 | def username(uid=None): |
|
778 | def username(uid=None): | |
779 | """Return the name of the user with the given uid. |
|
779 | """Return the name of the user with the given uid. | |
780 |
|
780 | |||
781 | If uid is None, return the name of the current user.""" |
|
781 | If uid is None, return the name of the current user.""" | |
782 | try: |
|
782 | try: | |
783 | import pwd |
|
783 | import pwd | |
784 | if uid is None: |
|
784 | if uid is None: | |
785 | uid = os.getuid() |
|
785 | uid = os.getuid() | |
786 | try: |
|
786 | try: | |
787 | return pwd.getpwuid(uid)[0] |
|
787 | return pwd.getpwuid(uid)[0] | |
788 | except KeyError: |
|
788 | except KeyError: | |
789 | return str(uid) |
|
789 | return str(uid) | |
790 | except ImportError: |
|
790 | except ImportError: | |
791 | return None |
|
791 | return None | |
792 |
|
792 | |||
793 | def groupname(gid=None): |
|
793 | def groupname(gid=None): | |
794 | """Return the name of the group with the given gid. |
|
794 | """Return the name of the group with the given gid. | |
795 |
|
795 | |||
796 | If gid is None, return the name of the current group.""" |
|
796 | If gid is None, return the name of the current group.""" | |
797 | try: |
|
797 | try: | |
798 | import grp |
|
798 | import grp | |
799 | if gid is None: |
|
799 | if gid is None: | |
800 | gid = os.getgid() |
|
800 | gid = os.getgid() | |
801 | try: |
|
801 | try: | |
802 | return grp.getgrgid(gid)[0] |
|
802 | return grp.getgrgid(gid)[0] | |
803 | except KeyError: |
|
803 | except KeyError: | |
804 | return str(gid) |
|
804 | return str(gid) | |
805 | except ImportError: |
|
805 | except ImportError: | |
806 | return None |
|
806 | return None | |
807 |
|
807 | |||
808 | # File system features |
|
808 | # File system features | |
809 |
|
809 | |||
810 | def checkfolding(path): |
|
810 | def checkfolding(path): | |
811 | """ |
|
811 | """ | |
812 | Check whether the given path is on a case-sensitive filesystem |
|
812 | Check whether the given path is on a case-sensitive filesystem | |
813 |
|
813 | |||
814 | Requires a path (like /foo/.hg) ending with a foldable final |
|
814 | Requires a path (like /foo/.hg) ending with a foldable final | |
815 | directory component. |
|
815 | directory component. | |
816 | """ |
|
816 | """ | |
817 | s1 = os.stat(path) |
|
817 | s1 = os.stat(path) | |
818 | d, b = os.path.split(path) |
|
818 | d, b = os.path.split(path) | |
819 | p2 = os.path.join(d, b.upper()) |
|
819 | p2 = os.path.join(d, b.upper()) | |
820 | if path == p2: |
|
820 | if path == p2: | |
821 | p2 = os.path.join(d, b.lower()) |
|
821 | p2 = os.path.join(d, b.lower()) | |
822 | try: |
|
822 | try: | |
823 | s2 = os.stat(p2) |
|
823 | s2 = os.stat(p2) | |
824 | if s2 == s1: |
|
824 | if s2 == s1: | |
825 | return False |
|
825 | return False | |
826 | return True |
|
826 | return True | |
827 | except: |
|
827 | except: | |
828 | return True |
|
828 | return True | |
829 |
|
829 | |||
830 | def checkexec(path): |
|
830 | def checkexec(path): | |
831 | """ |
|
831 | """ | |
832 | Check whether the given path is on a filesystem with UNIX-like exec flags |
|
832 | Check whether the given path is on a filesystem with UNIX-like exec flags | |
833 |
|
833 | |||
834 | Requires a directory (like /foo/.hg) |
|
834 | Requires a directory (like /foo/.hg) | |
835 | """ |
|
835 | """ | |
836 |
|
836 | |||
837 | # VFAT on some Linux versions can flip mode but it doesn't persist |
|
837 | # VFAT on some Linux versions can flip mode but it doesn't persist | |
838 | # a FS remount. Frequently we can detect it if files are created |
|
838 | # a FS remount. Frequently we can detect it if files are created | |
839 | # with exec bit on. |
|
839 | # with exec bit on. | |
840 |
|
840 | |||
841 | try: |
|
841 | try: | |
842 | EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
|
842 | EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | |
843 | fh, fn = tempfile.mkstemp("", "", path) |
|
843 | fh, fn = tempfile.mkstemp("", "", path) | |
844 | try: |
|
844 | try: | |
845 | os.close(fh) |
|
845 | os.close(fh) | |
846 | m = os.stat(fn).st_mode & 0777 |
|
846 | m = os.stat(fn).st_mode & 0777 | |
847 | new_file_has_exec = m & EXECFLAGS |
|
847 | new_file_has_exec = m & EXECFLAGS | |
848 | os.chmod(fn, m ^ EXECFLAGS) |
|
848 | os.chmod(fn, m ^ EXECFLAGS) | |
849 | exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m) |
|
849 | exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m) | |
850 | finally: |
|
850 | finally: | |
851 | os.unlink(fn) |
|
851 | os.unlink(fn) | |
852 | except (IOError, OSError): |
|
852 | except (IOError, OSError): | |
853 | # we don't care, the user probably won't be able to commit anyway |
|
853 | # we don't care, the user probably won't be able to commit anyway | |
854 | return False |
|
854 | return False | |
855 | return not (new_file_has_exec or exec_flags_cannot_flip) |
|
855 | return not (new_file_has_exec or exec_flags_cannot_flip) | |
856 |
|
856 | |||
857 | def execfunc(path, fallback): |
|
857 | def execfunc(path, fallback): | |
858 | '''return an is_exec() function with default to fallback''' |
|
858 | '''return an is_exec() function with default to fallback''' | |
859 | if checkexec(path): |
|
859 | if checkexec(path): | |
860 | return lambda x: is_exec(os.path.join(path, x)) |
|
860 | return lambda x: is_exec(os.path.join(path, x)) | |
861 | return fallback |
|
861 | return fallback | |
862 |
|
862 | |||
863 | def checklink(path): |
|
863 | def checklink(path): | |
864 | """check whether the given path is on a symlink-capable filesystem""" |
|
864 | """check whether the given path is on a symlink-capable filesystem""" | |
865 | # mktemp is not racy because symlink creation will fail if the |
|
865 | # mktemp is not racy because symlink creation will fail if the | |
866 | # file already exists |
|
866 | # file already exists | |
867 | name = tempfile.mktemp(dir=path) |
|
867 | name = tempfile.mktemp(dir=path) | |
868 | try: |
|
868 | try: | |
869 | os.symlink(".", name) |
|
869 | os.symlink(".", name) | |
870 | os.unlink(name) |
|
870 | os.unlink(name) | |
871 | return True |
|
871 | return True | |
872 | except (OSError, AttributeError): |
|
872 | except (OSError, AttributeError): | |
873 | return False |
|
873 | return False | |
874 |
|
874 | |||
875 | def linkfunc(path, fallback): |
|
875 | def linkfunc(path, fallback): | |
876 | '''return an is_link() function with default to fallback''' |
|
876 | '''return an is_link() function with default to fallback''' | |
877 | if checklink(path): |
|
877 | if checklink(path): | |
878 | return lambda x: os.path.islink(os.path.join(path, x)) |
|
878 | return lambda x: os.path.islink(os.path.join(path, x)) | |
879 | return fallback |
|
879 | return fallback | |
880 |
|
880 | |||
881 | _umask = os.umask(0) |
|
881 | _umask = os.umask(0) | |
882 | os.umask(_umask) |
|
882 | os.umask(_umask) | |
883 |
|
883 | |||
884 | def needbinarypatch(): |
|
884 | def needbinarypatch(): | |
885 | """return True if patches should be applied in binary mode by default.""" |
|
885 | """return True if patches should be applied in binary mode by default.""" | |
886 | return os.name == 'nt' |
|
886 | return os.name == 'nt' | |
887 |
|
887 | |||
888 | def endswithsep(path): |
|
888 | def endswithsep(path): | |
889 | '''Check path ends with os.sep or os.altsep.''' |
|
889 | '''Check path ends with os.sep or os.altsep.''' | |
890 | return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep) |
|
890 | return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep) | |
891 |
|
891 | |||
892 | def splitpath(path): |
|
892 | def splitpath(path): | |
893 | '''Split path by os.sep. |
|
893 | '''Split path by os.sep. | |
894 | Note that this function does not use os.altsep because this is |
|
894 | Note that this function does not use os.altsep because this is | |
895 | an alternative of simple "xxx.split(os.sep)". |
|
895 | an alternative of simple "xxx.split(os.sep)". | |
896 | It is recommended to use os.path.normpath() before using this |
|
896 | It is recommended to use os.path.normpath() before using this | |
897 | function if need.''' |
|
897 | function if need.''' | |
898 | return path.split(os.sep) |
|
898 | return path.split(os.sep) | |
899 |
|
899 | |||
900 | def gui(): |
|
900 | def gui(): | |
901 | '''Are we running in a GUI?''' |
|
901 | '''Are we running in a GUI?''' | |
902 | return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY") |
|
902 | return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY") | |
903 |
|
903 | |||
904 | # Platform specific variants |
|
904 | # Platform specific variants | |
905 | if os.name == 'nt': |
|
905 | if os.name == 'nt': | |
906 | import msvcrt |
|
906 | import msvcrt | |
907 | nulldev = 'NUL:' |
|
907 | nulldev = 'NUL:' | |
908 |
|
908 | |||
909 | class winstdout: |
|
909 | class winstdout: | |
910 | '''stdout on windows misbehaves if sent through a pipe''' |
|
910 | '''stdout on windows misbehaves if sent through a pipe''' | |
911 |
|
911 | |||
912 | def __init__(self, fp): |
|
912 | def __init__(self, fp): | |
913 | self.fp = fp |
|
913 | self.fp = fp | |
914 |
|
914 | |||
915 | def __getattr__(self, key): |
|
915 | def __getattr__(self, key): | |
916 | return getattr(self.fp, key) |
|
916 | return getattr(self.fp, key) | |
917 |
|
917 | |||
918 | def close(self): |
|
918 | def close(self): | |
919 | try: |
|
919 | try: | |
920 | self.fp.close() |
|
920 | self.fp.close() | |
921 | except: pass |
|
921 | except: pass | |
922 |
|
922 | |||
923 | def write(self, s): |
|
923 | def write(self, s): | |
924 | try: |
|
924 | try: | |
925 | # This is workaround for "Not enough space" error on |
|
925 | # This is workaround for "Not enough space" error on | |
926 | # writing large size of data to console. |
|
926 | # writing large size of data to console. | |
927 | limit = 16000 |
|
927 | limit = 16000 | |
928 | l = len(s) |
|
928 | l = len(s) | |
929 | start = 0 |
|
929 | start = 0 | |
930 | while start < l: |
|
930 | while start < l: | |
931 | end = start + limit |
|
931 | end = start + limit | |
932 | self.fp.write(s[start:end]) |
|
932 | self.fp.write(s[start:end]) | |
933 | start = end |
|
933 | start = end | |
934 | except IOError, inst: |
|
934 | except IOError, inst: | |
935 | if inst.errno != 0: raise |
|
935 | if inst.errno != 0: raise | |
936 | self.close() |
|
936 | self.close() | |
937 | raise IOError(errno.EPIPE, 'Broken pipe') |
|
937 | raise IOError(errno.EPIPE, 'Broken pipe') | |
938 |
|
938 | |||
939 | def flush(self): |
|
939 | def flush(self): | |
940 | try: |
|
940 | try: | |
941 | return self.fp.flush() |
|
941 | return self.fp.flush() | |
942 | except IOError, inst: |
|
942 | except IOError, inst: | |
943 | if inst.errno != errno.EINVAL: raise |
|
943 | if inst.errno != errno.EINVAL: raise | |
944 | self.close() |
|
944 | self.close() | |
945 | raise IOError(errno.EPIPE, 'Broken pipe') |
|
945 | raise IOError(errno.EPIPE, 'Broken pipe') | |
946 |
|
946 | |||
947 | sys.stdout = winstdout(sys.stdout) |
|
947 | sys.stdout = winstdout(sys.stdout) | |
948 |
|
948 | |||
949 | def _is_win_9x(): |
|
949 | def _is_win_9x(): | |
950 | '''return true if run on windows 95, 98 or me.''' |
|
950 | '''return true if run on windows 95, 98 or me.''' | |
951 | try: |
|
951 | try: | |
952 | return sys.getwindowsversion()[3] == 1 |
|
952 | return sys.getwindowsversion()[3] == 1 | |
953 | except AttributeError: |
|
953 | except AttributeError: | |
954 | return 'command' in os.environ.get('comspec', '') |
|
954 | return 'command' in os.environ.get('comspec', '') | |
955 |
|
955 | |||
956 | def openhardlinks(): |
|
956 | def openhardlinks(): | |
957 | return not _is_win_9x and "win32api" in locals() |
|
957 | return not _is_win_9x and "win32api" in locals() | |
958 |
|
958 | |||
959 | def system_rcpath(): |
|
959 | def system_rcpath(): | |
960 | try: |
|
960 | try: | |
961 | return system_rcpath_win32() |
|
961 | return system_rcpath_win32() | |
962 | except: |
|
962 | except: | |
963 | return [r'c:\mercurial\mercurial.ini'] |
|
963 | return [r'c:\mercurial\mercurial.ini'] | |
964 |
|
964 | |||
965 | def user_rcpath(): |
|
965 | def user_rcpath(): | |
966 | '''return os-specific hgrc search path to the user dir''' |
|
966 | '''return os-specific hgrc search path to the user dir''' | |
967 | try: |
|
967 | try: | |
968 | userrc = user_rcpath_win32() |
|
968 | userrc = user_rcpath_win32() | |
969 | except: |
|
969 | except: | |
970 | userrc = os.path.join(os.path.expanduser('~'), 'mercurial.ini') |
|
970 | userrc = os.path.join(os.path.expanduser('~'), 'mercurial.ini') | |
971 | path = [userrc] |
|
971 | path = [userrc] | |
972 | userprofile = os.environ.get('USERPROFILE') |
|
972 | userprofile = os.environ.get('USERPROFILE') | |
973 | if userprofile: |
|
973 | if userprofile: | |
974 | path.append(os.path.join(userprofile, 'mercurial.ini')) |
|
974 | path.append(os.path.join(userprofile, 'mercurial.ini')) | |
975 | return path |
|
975 | return path | |
976 |
|
976 | |||
977 | def parse_patch_output(output_line): |
|
977 | def parse_patch_output(output_line): | |
978 | """parses the output produced by patch and returns the file name""" |
|
978 | """parses the output produced by patch and returns the file name""" | |
979 | pf = output_line[14:] |
|
979 | pf = output_line[14:] | |
980 | if pf[0] == '`': |
|
980 | if pf[0] == '`': | |
981 | pf = pf[1:-1] # Remove the quotes |
|
981 | pf = pf[1:-1] # Remove the quotes | |
982 | return pf |
|
982 | return pf | |
983 |
|
983 | |||
984 | def sshargs(sshcmd, host, user, port): |
|
984 | def sshargs(sshcmd, host, user, port): | |
985 | '''Build argument list for ssh or Plink''' |
|
985 | '''Build argument list for ssh or Plink''' | |
986 | pflag = 'plink' in sshcmd.lower() and '-P' or '-p' |
|
986 | pflag = 'plink' in sshcmd.lower() and '-P' or '-p' | |
987 | args = user and ("%s@%s" % (user, host)) or host |
|
987 | args = user and ("%s@%s" % (user, host)) or host | |
988 | return port and ("%s %s %s" % (args, pflag, port)) or args |
|
988 | return port and ("%s %s %s" % (args, pflag, port)) or args | |
989 |
|
989 | |||
990 | def testpid(pid): |
|
990 | def testpid(pid): | |
991 | '''return False if pid dead, True if running or not known''' |
|
991 | '''return False if pid dead, True if running or not known''' | |
992 | return True |
|
992 | return True | |
993 |
|
993 | |||
994 | def set_flags(f, flags): |
|
994 | def set_flags(f, flags): | |
995 | pass |
|
995 | pass | |
996 |
|
996 | |||
997 | def set_binary(fd): |
|
997 | def set_binary(fd): | |
998 | msvcrt.setmode(fd.fileno(), os.O_BINARY) |
|
998 | msvcrt.setmode(fd.fileno(), os.O_BINARY) | |
999 |
|
999 | |||
1000 | def pconvert(path): |
|
1000 | def pconvert(path): | |
1001 | return '/'.join(splitpath(path)) |
|
1001 | return '/'.join(splitpath(path)) | |
1002 |
|
1002 | |||
1003 | def localpath(path): |
|
1003 | def localpath(path): | |
1004 | return path.replace('/', '\\') |
|
1004 | return path.replace('/', '\\') | |
1005 |
|
1005 | |||
1006 | def normpath(path): |
|
1006 | def normpath(path): | |
1007 | return pconvert(os.path.normpath(path)) |
|
1007 | return pconvert(os.path.normpath(path)) | |
1008 |
|
1008 | |||
1009 | makelock = _makelock_file |
|
1009 | makelock = _makelock_file | |
1010 | readlock = _readlock_file |
|
1010 | readlock = _readlock_file | |
1011 |
|
1011 | |||
1012 | def samestat(s1, s2): |
|
1012 | def samestat(s1, s2): | |
1013 | return False |
|
1013 | return False | |
1014 |
|
1014 | |||
1015 | # A sequence of backslashes is special iff it precedes a double quote: |
|
1015 | # A sequence of backslashes is special iff it precedes a double quote: | |
1016 | # - if there's an even number of backslashes, the double quote is not |
|
1016 | # - if there's an even number of backslashes, the double quote is not | |
1017 | # quoted (i.e. it ends the quoted region) |
|
1017 | # quoted (i.e. it ends the quoted region) | |
1018 | # - if there's an odd number of backslashes, the double quote is quoted |
|
1018 | # - if there's an odd number of backslashes, the double quote is quoted | |
1019 | # - in both cases, every pair of backslashes is unquoted into a single |
|
1019 | # - in both cases, every pair of backslashes is unquoted into a single | |
1020 | # backslash |
|
1020 | # backslash | |
1021 | # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx ) |
|
1021 | # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx ) | |
1022 | # So, to quote a string, we must surround it in double quotes, double |
|
1022 | # So, to quote a string, we must surround it in double quotes, double | |
1023 | # the number of backslashes that preceed double quotes and add another |
|
1023 | # the number of backslashes that preceed double quotes and add another | |
1024 | # backslash before every double quote (being careful with the double |
|
1024 | # backslash before every double quote (being careful with the double | |
1025 | # quote we've appended to the end) |
|
1025 | # quote we've appended to the end) | |
1026 | _quotere = None |
|
1026 | _quotere = None | |
1027 | def shellquote(s): |
|
1027 | def shellquote(s): | |
1028 | global _quotere |
|
1028 | global _quotere | |
1029 | if _quotere is None: |
|
1029 | if _quotere is None: | |
1030 | _quotere = re.compile(r'(\\*)("|\\$)') |
|
1030 | _quotere = re.compile(r'(\\*)("|\\$)') | |
1031 | return '"%s"' % _quotere.sub(r'\1\1\\\2', s) |
|
1031 | return '"%s"' % _quotere.sub(r'\1\1\\\2', s) | |
1032 |
|
1032 | |||
1033 | def quotecommand(cmd): |
|
1033 | def quotecommand(cmd): | |
1034 | """Build a command string suitable for os.popen* calls.""" |
|
1034 | """Build a command string suitable for os.popen* calls.""" | |
1035 | # The extra quotes are needed because popen* runs the command |
|
1035 | # The extra quotes are needed because popen* runs the command | |
1036 | # through the current COMSPEC. cmd.exe suppress enclosing quotes. |
|
1036 | # through the current COMSPEC. cmd.exe suppress enclosing quotes. | |
1037 | return '"' + cmd + '"' |
|
1037 | return '"' + cmd + '"' | |
1038 |
|
1038 | |||
1039 | def popen(command): |
|
1039 | def popen(command): | |
1040 | # Work around "popen spawned process may not write to stdout |
|
1040 | # Work around "popen spawned process may not write to stdout | |
1041 | # under windows" |
|
1041 | # under windows" | |
1042 | # http://bugs.python.org/issue1366 |
|
1042 | # http://bugs.python.org/issue1366 | |
1043 | command += " 2> %s" % nulldev |
|
1043 | command += " 2> %s" % nulldev | |
1044 | return os.popen(quotecommand(command)) |
|
1044 | return os.popen(quotecommand(command)) | |
1045 |
|
1045 | |||
1046 | def explain_exit(code): |
|
1046 | def explain_exit(code): | |
1047 | return _("exited with status %d") % code, code |
|
1047 | return _("exited with status %d") % code, code | |
1048 |
|
1048 | |||
1049 | # if you change this stub into a real check, please try to implement the |
|
1049 | # if you change this stub into a real check, please try to implement the | |
1050 | # username and groupname functions above, too. |
|
1050 | # username and groupname functions above, too. | |
1051 | def isowner(fp, st=None): |
|
1051 | def isowner(fp, st=None): | |
1052 | return True |
|
1052 | return True | |
1053 |
|
1053 | |||
1054 | def find_in_path(name, path, default=None): |
|
1054 | def find_in_path(name, path, default=None): | |
1055 | '''find name in search path. path can be string (will be split |
|
1055 | '''find name in search path. path can be string (will be split | |
1056 | with os.pathsep), or iterable thing that returns strings. if name |
|
1056 | with os.pathsep), or iterable thing that returns strings. if name | |
1057 | found, return path to name. else return default. name is looked up |
|
1057 | found, return path to name. else return default. name is looked up | |
1058 | using cmd.exe rules, using PATHEXT.''' |
|
1058 | using cmd.exe rules, using PATHEXT.''' | |
1059 | if isinstance(path, str): |
|
1059 | if isinstance(path, str): | |
1060 | path = path.split(os.pathsep) |
|
1060 | path = path.split(os.pathsep) | |
1061 |
|
1061 | |||
1062 | pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD') |
|
1062 | pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD') | |
1063 | pathext = pathext.lower().split(os.pathsep) |
|
1063 | pathext = pathext.lower().split(os.pathsep) | |
1064 | isexec = os.path.splitext(name)[1].lower() in pathext |
|
1064 | isexec = os.path.splitext(name)[1].lower() in pathext | |
1065 |
|
1065 | |||
1066 | for p in path: |
|
1066 | for p in path: | |
1067 | p_name = os.path.join(p, name) |
|
1067 | p_name = os.path.join(p, name) | |
1068 |
|
1068 | |||
1069 | if isexec and os.path.exists(p_name): |
|
1069 | if isexec and os.path.exists(p_name): | |
1070 | return p_name |
|
1070 | return p_name | |
1071 |
|
1071 | |||
1072 | for ext in pathext: |
|
1072 | for ext in pathext: | |
1073 | p_name_ext = p_name + ext |
|
1073 | p_name_ext = p_name + ext | |
1074 | if os.path.exists(p_name_ext): |
|
1074 | if os.path.exists(p_name_ext): | |
1075 | return p_name_ext |
|
1075 | return p_name_ext | |
1076 | return default |
|
1076 | return default | |
1077 |
|
1077 | |||
1078 | def set_signal_handler(): |
|
1078 | def set_signal_handler(): | |
1079 | try: |
|
1079 | try: | |
1080 | set_signal_handler_win32() |
|
1080 | set_signal_handler_win32() | |
1081 | except NameError: |
|
1081 | except NameError: | |
1082 | pass |
|
1082 | pass | |
1083 |
|
1083 | |||
1084 | try: |
|
1084 | try: | |
1085 | # override functions with win32 versions if possible |
|
1085 | # override functions with win32 versions if possible | |
1086 | from util_win32 import * |
|
1086 | from util_win32 import * | |
1087 | if not _is_win_9x(): |
|
1087 | if not _is_win_9x(): | |
1088 | posixfile = posixfile_nt |
|
1088 | posixfile = posixfile_nt | |
1089 | except ImportError: |
|
1089 | except ImportError: | |
1090 | pass |
|
1090 | pass | |
1091 |
|
1091 | |||
1092 | else: |
|
1092 | else: | |
1093 | nulldev = '/dev/null' |
|
1093 | nulldev = '/dev/null' | |
1094 |
|
1094 | |||
1095 | def lookup_reg(key, name=None, scope=None): |
|
1095 | def lookup_reg(key, name=None, scope=None): | |
1096 | return None |
|
1096 | return None | |
1097 |
|
1097 | |||
1098 | def rcfiles(path): |
|
1098 | def rcfiles(path): | |
1099 | rcs = [os.path.join(path, 'hgrc')] |
|
1099 | rcs = [os.path.join(path, 'hgrc')] | |
1100 | rcdir = os.path.join(path, 'hgrc.d') |
|
1100 | rcdir = os.path.join(path, 'hgrc.d') | |
1101 | try: |
|
1101 | try: | |
1102 | rcs.extend([os.path.join(rcdir, f) |
|
1102 | rcs.extend([os.path.join(rcdir, f) | |
1103 | for f, kind in osutil.listdir(rcdir) |
|
1103 | for f, kind in osutil.listdir(rcdir) | |
1104 | if f.endswith(".rc")]) |
|
1104 | if f.endswith(".rc")]) | |
1105 | except OSError: |
|
1105 | except OSError: | |
1106 | pass |
|
1106 | pass | |
1107 | return rcs |
|
1107 | return rcs | |
1108 |
|
1108 | |||
1109 | def system_rcpath(): |
|
1109 | def system_rcpath(): | |
1110 | path = [] |
|
1110 | path = [] | |
1111 | # old mod_python does not set sys.argv |
|
1111 | # old mod_python does not set sys.argv | |
1112 | if len(getattr(sys, 'argv', [])) > 0: |
|
1112 | if len(getattr(sys, 'argv', [])) > 0: | |
1113 | path.extend(rcfiles(os.path.dirname(sys.argv[0]) + |
|
1113 | path.extend(rcfiles(os.path.dirname(sys.argv[0]) + | |
1114 | '/../etc/mercurial')) |
|
1114 | '/../etc/mercurial')) | |
1115 | path.extend(rcfiles('/etc/mercurial')) |
|
1115 | path.extend(rcfiles('/etc/mercurial')) | |
1116 | return path |
|
1116 | return path | |
1117 |
|
1117 | |||
1118 | def user_rcpath(): |
|
1118 | def user_rcpath(): | |
1119 | return [os.path.expanduser('~/.hgrc')] |
|
1119 | return [os.path.expanduser('~/.hgrc')] | |
1120 |
|
1120 | |||
1121 | def parse_patch_output(output_line): |
|
1121 | def parse_patch_output(output_line): | |
1122 | """parses the output produced by patch and returns the file name""" |
|
1122 | """parses the output produced by patch and returns the file name""" | |
1123 | pf = output_line[14:] |
|
1123 | pf = output_line[14:] | |
1124 | if os.sys.platform == 'OpenVMS': |
|
1124 | if os.sys.platform == 'OpenVMS': | |
1125 | if pf[0] == '`': |
|
1125 | if pf[0] == '`': | |
1126 | pf = pf[1:-1] # Remove the quotes |
|
1126 | pf = pf[1:-1] # Remove the quotes | |
1127 | else: |
|
1127 | else: | |
1128 | if pf.startswith("'") and pf.endswith("'") and " " in pf: |
|
1128 | if pf.startswith("'") and pf.endswith("'") and " " in pf: | |
1129 | pf = pf[1:-1] # Remove the quotes |
|
1129 | pf = pf[1:-1] # Remove the quotes | |
1130 | return pf |
|
1130 | return pf | |
1131 |
|
1131 | |||
1132 | def sshargs(sshcmd, host, user, port): |
|
1132 | def sshargs(sshcmd, host, user, port): | |
1133 | '''Build argument list for ssh''' |
|
1133 | '''Build argument list for ssh''' | |
1134 | args = user and ("%s@%s" % (user, host)) or host |
|
1134 | args = user and ("%s@%s" % (user, host)) or host | |
1135 | return port and ("%s -p %s" % (args, port)) or args |
|
1135 | return port and ("%s -p %s" % (args, port)) or args | |
1136 |
|
1136 | |||
1137 | def is_exec(f): |
|
1137 | def is_exec(f): | |
1138 | """check whether a file is executable""" |
|
1138 | """check whether a file is executable""" | |
1139 | return (os.lstat(f).st_mode & 0100 != 0) |
|
1139 | return (os.lstat(f).st_mode & 0100 != 0) | |
1140 |
|
1140 | |||
1141 | def set_flags(f, flags): |
|
1141 | def set_flags(f, flags): | |
1142 | s = os.lstat(f).st_mode |
|
1142 | s = os.lstat(f).st_mode | |
1143 | x = "x" in flags |
|
1143 | x = "x" in flags | |
1144 | l = "l" in flags |
|
1144 | l = "l" in flags | |
1145 | if l: |
|
1145 | if l: | |
1146 | if not stat.S_ISLNK(s): |
|
1146 | if not stat.S_ISLNK(s): | |
1147 | # switch file to link |
|
1147 | # switch file to link | |
1148 | data = file(f).read() |
|
1148 | data = file(f).read() | |
1149 | os.unlink(f) |
|
1149 | os.unlink(f) | |
1150 | os.symlink(data, f) |
|
1150 | os.symlink(data, f) | |
1151 | # no chmod needed at this point |
|
1151 | # no chmod needed at this point | |
1152 | return |
|
1152 | return | |
1153 | if stat.S_ISLNK(s): |
|
1153 | if stat.S_ISLNK(s): | |
1154 | # switch link to file |
|
1154 | # switch link to file | |
1155 | data = os.readlink(f) |
|
1155 | data = os.readlink(f) | |
1156 | os.unlink(f) |
|
1156 | os.unlink(f) | |
1157 | file(f, "w").write(data) |
|
1157 | file(f, "w").write(data) | |
1158 | s = 0666 & ~_umask # avoid restatting for chmod |
|
1158 | s = 0666 & ~_umask # avoid restatting for chmod | |
1159 |
|
1159 | |||
1160 | sx = s & 0100 |
|
1160 | sx = s & 0100 | |
1161 | if x and not sx: |
|
1161 | if x and not sx: | |
1162 | # Turn on +x for every +r bit when making a file executable |
|
1162 | # Turn on +x for every +r bit when making a file executable | |
1163 | # and obey umask. |
|
1163 | # and obey umask. | |
1164 | os.chmod(f, s | (s & 0444) >> 2 & ~_umask) |
|
1164 | os.chmod(f, s | (s & 0444) >> 2 & ~_umask) | |
1165 | elif not x and sx: |
|
1165 | elif not x and sx: | |
1166 | # Turn off all +x bits |
|
1166 | # Turn off all +x bits | |
1167 | os.chmod(f, s & 0666) |
|
1167 | os.chmod(f, s & 0666) | |
1168 |
|
1168 | |||
1169 | def set_binary(fd): |
|
1169 | def set_binary(fd): | |
1170 | pass |
|
1170 | pass | |
1171 |
|
1171 | |||
1172 | def pconvert(path): |
|
1172 | def pconvert(path): | |
1173 | return path |
|
1173 | return path | |
1174 |
|
1174 | |||
1175 | def localpath(path): |
|
1175 | def localpath(path): | |
1176 | return path |
|
1176 | return path | |
1177 |
|
1177 | |||
1178 | normpath = os.path.normpath |
|
1178 | normpath = os.path.normpath | |
1179 | samestat = os.path.samestat |
|
1179 | samestat = os.path.samestat | |
1180 |
|
1180 | |||
1181 | def makelock(info, pathname): |
|
1181 | def makelock(info, pathname): | |
1182 | try: |
|
1182 | try: | |
1183 | os.symlink(info, pathname) |
|
1183 | os.symlink(info, pathname) | |
1184 | except OSError, why: |
|
1184 | except OSError, why: | |
1185 | if why.errno == errno.EEXIST: |
|
1185 | if why.errno == errno.EEXIST: | |
1186 | raise |
|
1186 | raise | |
1187 | else: |
|
1187 | else: | |
1188 | _makelock_file(info, pathname) |
|
1188 | _makelock_file(info, pathname) | |
1189 |
|
1189 | |||
1190 | def readlock(pathname): |
|
1190 | def readlock(pathname): | |
1191 | try: |
|
1191 | try: | |
1192 | return os.readlink(pathname) |
|
1192 | return os.readlink(pathname) | |
1193 | except OSError, why: |
|
1193 | except OSError, why: | |
1194 | if why.errno in (errno.EINVAL, errno.ENOSYS): |
|
1194 | if why.errno in (errno.EINVAL, errno.ENOSYS): | |
1195 | return _readlock_file(pathname) |
|
1195 | return _readlock_file(pathname) | |
1196 | else: |
|
1196 | else: | |
1197 | raise |
|
1197 | raise | |
1198 |
|
1198 | |||
1199 | def shellquote(s): |
|
1199 | def shellquote(s): | |
1200 | if os.sys.platform == 'OpenVMS': |
|
1200 | if os.sys.platform == 'OpenVMS': | |
1201 | return '"%s"' % s |
|
1201 | return '"%s"' % s | |
1202 | else: |
|
1202 | else: | |
1203 | return "'%s'" % s.replace("'", "'\\''") |
|
1203 | return "'%s'" % s.replace("'", "'\\''") | |
1204 |
|
1204 | |||
1205 | def quotecommand(cmd): |
|
1205 | def quotecommand(cmd): | |
1206 | return cmd |
|
1206 | return cmd | |
1207 |
|
1207 | |||
1208 | def popen(command): |
|
1208 | def popen(command): | |
1209 | return os.popen(command) |
|
1209 | return os.popen(command) | |
1210 |
|
1210 | |||
1211 | def testpid(pid): |
|
1211 | def testpid(pid): | |
1212 | '''return False if pid dead, True if running or not sure''' |
|
1212 | '''return False if pid dead, True if running or not sure''' | |
1213 | if os.sys.platform == 'OpenVMS': |
|
1213 | if os.sys.platform == 'OpenVMS': | |
1214 | return True |
|
1214 | return True | |
1215 | try: |
|
1215 | try: | |
1216 | os.kill(pid, 0) |
|
1216 | os.kill(pid, 0) | |
1217 | return True |
|
1217 | return True | |
1218 | except OSError, inst: |
|
1218 | except OSError, inst: | |
1219 | return inst.errno != errno.ESRCH |
|
1219 | return inst.errno != errno.ESRCH | |
1220 |
|
1220 | |||
1221 | def explain_exit(code): |
|
1221 | def explain_exit(code): | |
1222 | """return a 2-tuple (desc, code) describing a process's status""" |
|
1222 | """return a 2-tuple (desc, code) describing a process's status""" | |
1223 | if os.WIFEXITED(code): |
|
1223 | if os.WIFEXITED(code): | |
1224 | val = os.WEXITSTATUS(code) |
|
1224 | val = os.WEXITSTATUS(code) | |
1225 | return _("exited with status %d") % val, val |
|
1225 | return _("exited with status %d") % val, val | |
1226 | elif os.WIFSIGNALED(code): |
|
1226 | elif os.WIFSIGNALED(code): | |
1227 | val = os.WTERMSIG(code) |
|
1227 | val = os.WTERMSIG(code) | |
1228 | return _("killed by signal %d") % val, val |
|
1228 | return _("killed by signal %d") % val, val | |
1229 | elif os.WIFSTOPPED(code): |
|
1229 | elif os.WIFSTOPPED(code): | |
1230 | val = os.WSTOPSIG(code) |
|
1230 | val = os.WSTOPSIG(code) | |
1231 | return _("stopped by signal %d") % val, val |
|
1231 | return _("stopped by signal %d") % val, val | |
1232 | raise ValueError(_("invalid exit code")) |
|
1232 | raise ValueError(_("invalid exit code")) | |
1233 |
|
1233 | |||
1234 | def isowner(fp, st=None): |
|
1234 | def isowner(fp, st=None): | |
1235 | """Return True if the file object f belongs to the current user. |
|
1235 | """Return True if the file object f belongs to the current user. | |
1236 |
|
1236 | |||
1237 | The return value of a util.fstat(f) may be passed as the st argument. |
|
1237 | The return value of a util.fstat(f) may be passed as the st argument. | |
1238 | """ |
|
1238 | """ | |
1239 | if st is None: |
|
1239 | if st is None: | |
1240 | st = fstat(fp) |
|
1240 | st = fstat(fp) | |
1241 | return st.st_uid == os.getuid() |
|
1241 | return st.st_uid == os.getuid() | |
1242 |
|
1242 | |||
1243 | def find_in_path(name, path, default=None): |
|
1243 | def find_in_path(name, path, default=None): | |
1244 | '''find name in search path. path can be string (will be split |
|
1244 | '''find name in search path. path can be string (will be split | |
1245 | with os.pathsep), or iterable thing that returns strings. if name |
|
1245 | with os.pathsep), or iterable thing that returns strings. if name | |
1246 | found, return path to name. else return default.''' |
|
1246 | found, return path to name. else return default.''' | |
1247 | if isinstance(path, str): |
|
1247 | if isinstance(path, str): | |
1248 | path = path.split(os.pathsep) |
|
1248 | path = path.split(os.pathsep) | |
1249 | for p in path: |
|
1249 | for p in path: | |
1250 | p_name = os.path.join(p, name) |
|
1250 | p_name = os.path.join(p, name) | |
1251 | if os.path.exists(p_name): |
|
1251 | if os.path.exists(p_name): | |
1252 | return p_name |
|
1252 | return p_name | |
1253 | return default |
|
1253 | return default | |
1254 |
|
1254 | |||
1255 | def set_signal_handler(): |
|
1255 | def set_signal_handler(): | |
1256 | pass |
|
1256 | pass | |
1257 |
|
1257 | |||
1258 | def find_exe(name, default=None): |
|
1258 | def find_exe(name, default=None): | |
1259 | '''find path of an executable. |
|
1259 | '''find path of an executable. | |
1260 | if name contains a path component, return it as is. otherwise, |
|
1260 | if name contains a path component, return it as is. otherwise, | |
1261 | use normal executable search path.''' |
|
1261 | use normal executable search path.''' | |
1262 |
|
1262 | |||
1263 | if os.sep in name or sys.platform == 'OpenVMS': |
|
1263 | if os.sep in name or sys.platform == 'OpenVMS': | |
1264 | # don't check the executable bit. if the file isn't |
|
1264 | # don't check the executable bit. if the file isn't | |
1265 | # executable, whoever tries to actually run it will give a |
|
1265 | # executable, whoever tries to actually run it will give a | |
1266 | # much more useful error message. |
|
1266 | # much more useful error message. | |
1267 | return name |
|
1267 | return name | |
1268 | return find_in_path(name, os.environ.get('PATH', ''), default=default) |
|
1268 | return find_in_path(name, os.environ.get('PATH', ''), default=default) | |
1269 |
|
1269 | |||
1270 | def _buildencodefun(): |
|
1270 | def _buildencodefun(): | |
1271 | e = '_' |
|
1271 | e = '_' | |
1272 | win_reserved = [ord(x) for x in '\\:*?"<>|'] |
|
1272 | win_reserved = [ord(x) for x in '\\:*?"<>|'] | |
1273 | cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ]) |
|
1273 | cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ]) | |
1274 | for x in (range(32) + range(126, 256) + win_reserved): |
|
1274 | for x in (range(32) + range(126, 256) + win_reserved): | |
1275 | cmap[chr(x)] = "~%02x" % x |
|
1275 | cmap[chr(x)] = "~%02x" % x | |
1276 | for x in range(ord("A"), ord("Z")+1) + [ord(e)]: |
|
1276 | for x in range(ord("A"), ord("Z")+1) + [ord(e)]: | |
1277 | cmap[chr(x)] = e + chr(x).lower() |
|
1277 | cmap[chr(x)] = e + chr(x).lower() | |
1278 | dmap = {} |
|
1278 | dmap = {} | |
1279 | for k, v in cmap.iteritems(): |
|
1279 | for k, v in cmap.iteritems(): | |
1280 | dmap[v] = k |
|
1280 | dmap[v] = k | |
1281 | def decode(s): |
|
1281 | def decode(s): | |
1282 | i = 0 |
|
1282 | i = 0 | |
1283 | while i < len(s): |
|
1283 | while i < len(s): | |
1284 | for l in xrange(1, 4): |
|
1284 | for l in xrange(1, 4): | |
1285 | try: |
|
1285 | try: | |
1286 | yield dmap[s[i:i+l]] |
|
1286 | yield dmap[s[i:i+l]] | |
1287 | i += l |
|
1287 | i += l | |
1288 | break |
|
1288 | break | |
1289 | except KeyError: |
|
1289 | except KeyError: | |
1290 | pass |
|
1290 | pass | |
1291 | else: |
|
1291 | else: | |
1292 | raise KeyError |
|
1292 | raise KeyError | |
1293 | return (lambda s: "".join([cmap[c] for c in s]), |
|
1293 | return (lambda s: "".join([cmap[c] for c in s]), | |
1294 | lambda s: "".join(list(decode(s)))) |
|
1294 | lambda s: "".join(list(decode(s)))) | |
1295 |
|
1295 | |||
1296 | encodefilename, decodefilename = _buildencodefun() |
|
1296 | encodefilename, decodefilename = _buildencodefun() | |
1297 |
|
1297 | |||
1298 | def encodedopener(openerfn, fn): |
|
1298 | def encodedopener(openerfn, fn): | |
1299 | def o(path, *args, **kw): |
|
1299 | def o(path, *args, **kw): | |
1300 | return openerfn(fn(path), *args, **kw) |
|
1300 | return openerfn(fn(path), *args, **kw) | |
1301 | return o |
|
1301 | return o | |
1302 |
|
1302 | |||
1303 | def mktempcopy(name, emptyok=False, createmode=None): |
|
1303 | def mktempcopy(name, emptyok=False, createmode=None): | |
1304 | """Create a temporary file with the same contents from name |
|
1304 | """Create a temporary file with the same contents from name | |
1305 |
|
1305 | |||
1306 | The permission bits are copied from the original file. |
|
1306 | The permission bits are copied from the original file. | |
1307 |
|
1307 | |||
1308 | If the temporary file is going to be truncated immediately, you |
|
1308 | If the temporary file is going to be truncated immediately, you | |
1309 | can use emptyok=True as an optimization. |
|
1309 | can use emptyok=True as an optimization. | |
1310 |
|
1310 | |||
1311 | Returns the name of the temporary file. |
|
1311 | Returns the name of the temporary file. | |
1312 | """ |
|
1312 | """ | |
1313 | d, fn = os.path.split(name) |
|
1313 | d, fn = os.path.split(name) | |
1314 | fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) |
|
1314 | fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) | |
1315 | os.close(fd) |
|
1315 | os.close(fd) | |
1316 | # Temporary files are created with mode 0600, which is usually not |
|
1316 | # Temporary files are created with mode 0600, which is usually not | |
1317 | # what we want. If the original file already exists, just copy |
|
1317 | # what we want. If the original file already exists, just copy | |
1318 | # its mode. Otherwise, manually obey umask. |
|
1318 | # its mode. Otherwise, manually obey umask. | |
1319 | try: |
|
1319 | try: | |
1320 | st_mode = os.lstat(name).st_mode & 0777 |
|
1320 | st_mode = os.lstat(name).st_mode & 0777 | |
1321 | except OSError, inst: |
|
1321 | except OSError, inst: | |
1322 | if inst.errno != errno.ENOENT: |
|
1322 | if inst.errno != errno.ENOENT: | |
1323 | raise |
|
1323 | raise | |
1324 | st_mode = createmode |
|
1324 | st_mode = createmode | |
1325 | if st_mode is None: |
|
1325 | if st_mode is None: | |
1326 | st_mode = ~_umask |
|
1326 | st_mode = ~_umask | |
1327 | st_mode &= 0666 |
|
1327 | st_mode &= 0666 | |
1328 | os.chmod(temp, st_mode) |
|
1328 | os.chmod(temp, st_mode) | |
1329 | if emptyok: |
|
1329 | if emptyok: | |
1330 | return temp |
|
1330 | return temp | |
1331 | try: |
|
1331 | try: | |
1332 | try: |
|
1332 | try: | |
1333 | ifp = posixfile(name, "rb") |
|
1333 | ifp = posixfile(name, "rb") | |
1334 | except IOError, inst: |
|
1334 | except IOError, inst: | |
1335 | if inst.errno == errno.ENOENT: |
|
1335 | if inst.errno == errno.ENOENT: | |
1336 | return temp |
|
1336 | return temp | |
1337 | if not getattr(inst, 'filename', None): |
|
1337 | if not getattr(inst, 'filename', None): | |
1338 | inst.filename = name |
|
1338 | inst.filename = name | |
1339 | raise |
|
1339 | raise | |
1340 | ofp = posixfile(temp, "wb") |
|
1340 | ofp = posixfile(temp, "wb") | |
1341 | for chunk in filechunkiter(ifp): |
|
1341 | for chunk in filechunkiter(ifp): | |
1342 | ofp.write(chunk) |
|
1342 | ofp.write(chunk) | |
1343 | ifp.close() |
|
1343 | ifp.close() | |
1344 | ofp.close() |
|
1344 | ofp.close() | |
1345 | except: |
|
1345 | except: | |
1346 | try: os.unlink(temp) |
|
1346 | try: os.unlink(temp) | |
1347 | except: pass |
|
1347 | except: pass | |
1348 | raise |
|
1348 | raise | |
1349 | return temp |
|
1349 | return temp | |
1350 |
|
1350 | |||
1351 | class atomictempfile(posixfile): |
|
1351 | class atomictempfile(posixfile): | |
1352 | """file-like object that atomically updates a file |
|
1352 | """file-like object that atomically updates a file | |
1353 |
|
1353 | |||
1354 | All writes will be redirected to a temporary copy of the original |
|
1354 | All writes will be redirected to a temporary copy of the original | |
1355 | file. When rename is called, the copy is renamed to the original |
|
1355 | file. When rename is called, the copy is renamed to the original | |
1356 | name, making the changes visible. |
|
1356 | name, making the changes visible. | |
1357 | """ |
|
1357 | """ | |
1358 | def __init__(self, name, mode, createmode): |
|
1358 | def __init__(self, name, mode, createmode): | |
1359 | self.__name = name |
|
1359 | self.__name = name | |
1360 | self.temp = mktempcopy(name, emptyok=('w' in mode), |
|
1360 | self.temp = mktempcopy(name, emptyok=('w' in mode), | |
1361 | createmode=createmode) |
|
1361 | createmode=createmode) | |
1362 | posixfile.__init__(self, self.temp, mode) |
|
1362 | posixfile.__init__(self, self.temp, mode) | |
1363 |
|
1363 | |||
1364 | def rename(self): |
|
1364 | def rename(self): | |
1365 | if not self.closed: |
|
1365 | if not self.closed: | |
1366 | posixfile.close(self) |
|
1366 | posixfile.close(self) | |
1367 | rename(self.temp, localpath(self.__name)) |
|
1367 | rename(self.temp, localpath(self.__name)) | |
1368 |
|
1368 | |||
1369 | def __del__(self): |
|
1369 | def __del__(self): | |
1370 | if not self.closed: |
|
1370 | if not self.closed: | |
1371 | try: |
|
1371 | try: | |
1372 | os.unlink(self.temp) |
|
1372 | os.unlink(self.temp) | |
1373 | except: pass |
|
1373 | except: pass | |
1374 | posixfile.close(self) |
|
1374 | posixfile.close(self) | |
1375 |
|
1375 | |||
1376 | def makedirs(name, mode=None): |
|
1376 | def makedirs(name, mode=None): | |
1377 | """recursive directory creation with parent mode inheritance""" |
|
1377 | """recursive directory creation with parent mode inheritance""" | |
1378 | try: |
|
1378 | try: | |
1379 | os.mkdir(name) |
|
1379 | os.mkdir(name) | |
1380 | if mode is not None: |
|
1380 | if mode is not None: | |
1381 | os.chmod(name, mode) |
|
1381 | os.chmod(name, mode) | |
1382 | return |
|
1382 | return | |
1383 | except OSError, err: |
|
1383 | except OSError, err: | |
1384 | if err.errno == errno.EEXIST: |
|
1384 | if err.errno == errno.EEXIST: | |
1385 | return |
|
1385 | return | |
1386 | if err.errno != errno.ENOENT: |
|
1386 | if err.errno != errno.ENOENT: | |
1387 | raise |
|
1387 | raise | |
1388 | parent = os.path.abspath(os.path.dirname(name)) |
|
1388 | parent = os.path.abspath(os.path.dirname(name)) | |
1389 | makedirs(parent, mode) |
|
1389 | makedirs(parent, mode) | |
1390 | makedirs(name, mode) |
|
1390 | makedirs(name, mode) | |
1391 |
|
1391 | |||
1392 | class opener(object): |
|
1392 | class opener(object): | |
1393 | """Open files relative to a base directory |
|
1393 | """Open files relative to a base directory | |
1394 |
|
1394 | |||
1395 | This class is used to hide the details of COW semantics and |
|
1395 | This class is used to hide the details of COW semantics and | |
1396 | remote file access from higher level code. |
|
1396 | remote file access from higher level code. | |
1397 | """ |
|
1397 | """ | |
1398 | def __init__(self, base, audit=True): |
|
1398 | def __init__(self, base, audit=True): | |
1399 | self.base = base |
|
1399 | self.base = base | |
1400 | if audit: |
|
1400 | if audit: | |
1401 | self.audit_path = path_auditor(base) |
|
1401 | self.audit_path = path_auditor(base) | |
1402 | else: |
|
1402 | else: | |
1403 | self.audit_path = always |
|
1403 | self.audit_path = always | |
1404 | self.createmode = None |
|
1404 | self.createmode = None | |
1405 |
|
1405 | |||
1406 | def __getattr__(self, name): |
|
1406 | def __getattr__(self, name): | |
1407 | if name == '_can_symlink': |
|
1407 | if name == '_can_symlink': | |
1408 | self._can_symlink = checklink(self.base) |
|
1408 | self._can_symlink = checklink(self.base) | |
1409 | return self._can_symlink |
|
1409 | return self._can_symlink | |
1410 | raise AttributeError(name) |
|
1410 | raise AttributeError(name) | |
1411 |
|
1411 | |||
1412 | def _fixfilemode(self, name): |
|
1412 | def _fixfilemode(self, name): | |
1413 | if self.createmode is None: |
|
1413 | if self.createmode is None: | |
1414 | return |
|
1414 | return | |
1415 | os.chmod(name, self.createmode & 0666) |
|
1415 | os.chmod(name, self.createmode & 0666) | |
1416 |
|
1416 | |||
1417 | def __call__(self, path, mode="r", text=False, atomictemp=False): |
|
1417 | def __call__(self, path, mode="r", text=False, atomictemp=False): | |
1418 | self.audit_path(path) |
|
1418 | self.audit_path(path) | |
1419 | f = os.path.join(self.base, path) |
|
1419 | f = os.path.join(self.base, path) | |
1420 |
|
1420 | |||
1421 | if not text and "b" not in mode: |
|
1421 | if not text and "b" not in mode: | |
1422 | mode += "b" # for that other OS |
|
1422 | mode += "b" # for that other OS | |
1423 |
|
1423 | |||
1424 | nlink = -1 |
|
1424 | nlink = -1 | |
1425 | if mode[0] != "r": |
|
1425 | if mode[0] != "r": | |
1426 | try: |
|
1426 | try: | |
1427 | nlink = nlinks(f) |
|
1427 | nlink = nlinks(f) | |
1428 | except OSError: |
|
1428 | except OSError: | |
1429 | nlink = 0 |
|
1429 | nlink = 0 | |
1430 | d = os.path.dirname(f) |
|
1430 | d = os.path.dirname(f) | |
1431 | if not os.path.isdir(d): |
|
1431 | if not os.path.isdir(d): | |
1432 | makedirs(d, self.createmode) |
|
1432 | makedirs(d, self.createmode) | |
1433 | if atomictemp: |
|
1433 | if atomictemp: | |
1434 | return atomictempfile(f, mode, self.createmode) |
|
1434 | return atomictempfile(f, mode, self.createmode) | |
1435 | if nlink > 1: |
|
1435 | if nlink > 1: | |
1436 | rename(mktempcopy(f), f) |
|
1436 | rename(mktempcopy(f), f) | |
1437 | fp = posixfile(f, mode) |
|
1437 | fp = posixfile(f, mode) | |
1438 | if nlink == 0: |
|
1438 | if nlink == 0: | |
1439 | self._fixfilemode(f) |
|
1439 | self._fixfilemode(f) | |
1440 | return fp |
|
1440 | return fp | |
1441 |
|
1441 | |||
1442 | def symlink(self, src, dst): |
|
1442 | def symlink(self, src, dst): | |
1443 | self.audit_path(dst) |
|
1443 | self.audit_path(dst) | |
1444 | linkname = os.path.join(self.base, dst) |
|
1444 | linkname = os.path.join(self.base, dst) | |
1445 | try: |
|
1445 | try: | |
1446 | os.unlink(linkname) |
|
1446 | os.unlink(linkname) | |
1447 | except OSError: |
|
1447 | except OSError: | |
1448 | pass |
|
1448 | pass | |
1449 |
|
1449 | |||
1450 | dirname = os.path.dirname(linkname) |
|
1450 | dirname = os.path.dirname(linkname) | |
1451 | if not os.path.exists(dirname): |
|
1451 | if not os.path.exists(dirname): | |
1452 | makedirs(dirname, self.createmode) |
|
1452 | makedirs(dirname, self.createmode) | |
1453 |
|
1453 | |||
1454 | if self._can_symlink: |
|
1454 | if self._can_symlink: | |
1455 | try: |
|
1455 | try: | |
1456 | os.symlink(src, linkname) |
|
1456 | os.symlink(src, linkname) | |
1457 | except OSError, err: |
|
1457 | except OSError, err: | |
1458 | raise OSError(err.errno, _('could not symlink to %r: %s') % |
|
1458 | raise OSError(err.errno, _('could not symlink to %r: %s') % | |
1459 | (src, err.strerror), linkname) |
|
1459 | (src, err.strerror), linkname) | |
1460 | else: |
|
1460 | else: | |
1461 | f = self(dst, "w") |
|
1461 | f = self(dst, "w") | |
1462 | f.write(src) |
|
1462 | f.write(src) | |
1463 | f.close() |
|
1463 | f.close() | |
1464 | self._fixfilemode(dst) |
|
1464 | self._fixfilemode(dst) | |
1465 |
|
1465 | |||
1466 | class chunkbuffer(object): |
|
1466 | class chunkbuffer(object): | |
1467 | """Allow arbitrary sized chunks of data to be efficiently read from an |
|
1467 | """Allow arbitrary sized chunks of data to be efficiently read from an | |
1468 | iterator over chunks of arbitrary size.""" |
|
1468 | iterator over chunks of arbitrary size.""" | |
1469 |
|
1469 | |||
1470 | def __init__(self, in_iter): |
|
1470 | def __init__(self, in_iter): | |
1471 | """in_iter is the iterator that's iterating over the input chunks. |
|
1471 | """in_iter is the iterator that's iterating over the input chunks. | |
1472 | targetsize is how big a buffer to try to maintain.""" |
|
1472 | targetsize is how big a buffer to try to maintain.""" | |
1473 | self.iter = iter(in_iter) |
|
1473 | self.iter = iter(in_iter) | |
1474 | self.buf = '' |
|
1474 | self.buf = '' | |
1475 | self.targetsize = 2**16 |
|
1475 | self.targetsize = 2**16 | |
1476 |
|
1476 | |||
1477 | def read(self, l): |
|
1477 | def read(self, l): | |
1478 | """Read L bytes of data from the iterator of chunks of data. |
|
1478 | """Read L bytes of data from the iterator of chunks of data. | |
1479 | Returns less than L bytes if the iterator runs dry.""" |
|
1479 | Returns less than L bytes if the iterator runs dry.""" | |
1480 | if l > len(self.buf) and self.iter: |
|
1480 | if l > len(self.buf) and self.iter: | |
1481 | # Clamp to a multiple of self.targetsize |
|
1481 | # Clamp to a multiple of self.targetsize | |
1482 | targetsize = max(l, self.targetsize) |
|
1482 | targetsize = max(l, self.targetsize) | |
1483 | collector = cStringIO.StringIO() |
|
1483 | collector = cStringIO.StringIO() | |
1484 | collector.write(self.buf) |
|
1484 | collector.write(self.buf) | |
1485 | collected = len(self.buf) |
|
1485 | collected = len(self.buf) | |
1486 | for chunk in self.iter: |
|
1486 | for chunk in self.iter: | |
1487 | collector.write(chunk) |
|
1487 | collector.write(chunk) | |
1488 | collected += len(chunk) |
|
1488 | collected += len(chunk) | |
1489 | if collected >= targetsize: |
|
1489 | if collected >= targetsize: | |
1490 | break |
|
1490 | break | |
1491 | if collected < targetsize: |
|
1491 | if collected < targetsize: | |
1492 | self.iter = False |
|
1492 | self.iter = False | |
1493 | self.buf = collector.getvalue() |
|
1493 | self.buf = collector.getvalue() | |
1494 | if len(self.buf) == l: |
|
1494 | if len(self.buf) == l: | |
1495 | s, self.buf = str(self.buf), '' |
|
1495 | s, self.buf = str(self.buf), '' | |
1496 | else: |
|
1496 | else: | |
1497 | s, self.buf = self.buf[:l], buffer(self.buf, l) |
|
1497 | s, self.buf = self.buf[:l], buffer(self.buf, l) | |
1498 | return s |
|
1498 | return s | |
1499 |
|
1499 | |||
1500 | def filechunkiter(f, size=65536, limit=None): |
|
1500 | def filechunkiter(f, size=65536, limit=None): | |
1501 | """Create a generator that produces the data in the file size |
|
1501 | """Create a generator that produces the data in the file size | |
1502 | (default 65536) bytes at a time, up to optional limit (default is |
|
1502 | (default 65536) bytes at a time, up to optional limit (default is | |
1503 | to read all data). Chunks may be less than size bytes if the |
|
1503 | to read all data). Chunks may be less than size bytes if the | |
1504 | chunk is the last chunk in the file, or the file is a socket or |
|
1504 | chunk is the last chunk in the file, or the file is a socket or | |
1505 | some other type of file that sometimes reads less data than is |
|
1505 | some other type of file that sometimes reads less data than is | |
1506 | requested.""" |
|
1506 | requested.""" | |
1507 | assert size >= 0 |
|
1507 | assert size >= 0 | |
1508 | assert limit is None or limit >= 0 |
|
1508 | assert limit is None or limit >= 0 | |
1509 | while True: |
|
1509 | while True: | |
1510 | if limit is None: nbytes = size |
|
1510 | if limit is None: nbytes = size | |
1511 | else: nbytes = min(limit, size) |
|
1511 | else: nbytes = min(limit, size) | |
1512 | s = nbytes and f.read(nbytes) |
|
1512 | s = nbytes and f.read(nbytes) | |
1513 | if not s: break |
|
1513 | if not s: break | |
1514 | if limit: limit -= len(s) |
|
1514 | if limit: limit -= len(s) | |
1515 | yield s |
|
1515 | yield s | |
1516 |
|
1516 | |||
1517 | def makedate(): |
|
1517 | def makedate(): | |
1518 | lt = time.localtime() |
|
1518 | lt = time.localtime() | |
1519 | if lt[8] == 1 and time.daylight: |
|
1519 | if lt[8] == 1 and time.daylight: | |
1520 | tz = time.altzone |
|
1520 | tz = time.altzone | |
1521 | else: |
|
1521 | else: | |
1522 | tz = time.timezone |
|
1522 | tz = time.timezone | |
1523 | return time.mktime(lt), tz |
|
1523 | return time.mktime(lt), tz | |
1524 |
|
1524 | |||
1525 | def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True, timezone_format=" %+03d%02d"): |
|
1525 | def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True, timezone_format=" %+03d%02d"): | |
1526 | """represent a (unixtime, offset) tuple as a localized time. |
|
1526 | """represent a (unixtime, offset) tuple as a localized time. | |
1527 | unixtime is seconds since the epoch, and offset is the time zone's |
|
1527 | unixtime is seconds since the epoch, and offset is the time zone's | |
1528 | number of seconds away from UTC. if timezone is false, do not |
|
1528 | number of seconds away from UTC. if timezone is false, do not | |
1529 | append time zone to string.""" |
|
1529 | append time zone to string.""" | |
1530 | t, tz = date or makedate() |
|
1530 | t, tz = date or makedate() | |
1531 | s = time.strftime(format, time.gmtime(float(t) - tz)) |
|
1531 | s = time.strftime(format, time.gmtime(float(t) - tz)) | |
1532 | if timezone: |
|
1532 | if timezone: | |
1533 | s += timezone_format % (-tz / 3600, ((-tz % 3600) / 60)) |
|
1533 | s += timezone_format % (-tz / 3600, ((-tz % 3600) / 60)) | |
1534 | return s |
|
1534 | return s | |
1535 |
|
1535 | |||
1536 | def strdate(string, format, defaults=[]): |
|
1536 | def strdate(string, format, defaults=[]): | |
1537 | """parse a localized time string and return a (unixtime, offset) tuple. |
|
1537 | """parse a localized time string and return a (unixtime, offset) tuple. | |
1538 | if the string cannot be parsed, ValueError is raised.""" |
|
1538 | if the string cannot be parsed, ValueError is raised.""" | |
1539 | def timezone(string): |
|
1539 | def timezone(string): | |
1540 | tz = string.split()[-1] |
|
1540 | tz = string.split()[-1] | |
1541 | if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): |
|
1541 | if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): | |
1542 | tz = int(tz) |
|
1542 | tz = int(tz) | |
1543 | offset = - 3600 * (tz / 100) - 60 * (tz % 100) |
|
1543 | offset = - 3600 * (tz / 100) - 60 * (tz % 100) | |
1544 | return offset |
|
1544 | return offset | |
1545 | if tz == "GMT" or tz == "UTC": |
|
1545 | if tz == "GMT" or tz == "UTC": | |
1546 | return 0 |
|
1546 | return 0 | |
1547 | return None |
|
1547 | return None | |
1548 |
|
1548 | |||
1549 | # NOTE: unixtime = localunixtime + offset |
|
1549 | # NOTE: unixtime = localunixtime + offset | |
1550 | offset, date = timezone(string), string |
|
1550 | offset, date = timezone(string), string | |
1551 | if offset != None: |
|
1551 | if offset != None: | |
1552 | date = " ".join(string.split()[:-1]) |
|
1552 | date = " ".join(string.split()[:-1]) | |
1553 |
|
1553 | |||
1554 | # add missing elements from defaults |
|
1554 | # add missing elements from defaults | |
1555 | for part in defaults: |
|
1555 | for part in defaults: | |
1556 | found = [True for p in part if ("%"+p) in format] |
|
1556 | found = [True for p in part if ("%"+p) in format] | |
1557 | if not found: |
|
1557 | if not found: | |
1558 | date += "@" + defaults[part] |
|
1558 | date += "@" + defaults[part] | |
1559 | format += "@%" + part[0] |
|
1559 | format += "@%" + part[0] | |
1560 |
|
1560 | |||
1561 | timetuple = time.strptime(date, format) |
|
1561 | timetuple = time.strptime(date, format) | |
1562 | localunixtime = int(calendar.timegm(timetuple)) |
|
1562 | localunixtime = int(calendar.timegm(timetuple)) | |
1563 | if offset is None: |
|
1563 | if offset is None: | |
1564 | # local timezone |
|
1564 | # local timezone | |
1565 | unixtime = int(time.mktime(timetuple)) |
|
1565 | unixtime = int(time.mktime(timetuple)) | |
1566 | offset = unixtime - localunixtime |
|
1566 | offset = unixtime - localunixtime | |
1567 | else: |
|
1567 | else: | |
1568 | unixtime = localunixtime + offset |
|
1568 | unixtime = localunixtime + offset | |
1569 | return unixtime, offset |
|
1569 | return unixtime, offset | |
1570 |
|
1570 | |||
1571 | def parsedate(string, formats=None, defaults=None): |
|
1571 | def parsedate(string, formats=None, defaults=None): | |
1572 | """parse a localized time string and return a (unixtime, offset) tuple. |
|
1572 | """parse a localized time string and return a (unixtime, offset) tuple. | |
1573 | The date may be a "unixtime offset" string or in one of the specified |
|
1573 | The date may be a "unixtime offset" string or in one of the specified | |
1574 | formats.""" |
|
1574 | formats.""" | |
1575 | if not string: |
|
1575 | if not string: | |
1576 | return 0, 0 |
|
1576 | return 0, 0 | |
1577 | if not formats: |
|
1577 | if not formats: | |
1578 | formats = defaultdateformats |
|
1578 | formats = defaultdateformats | |
1579 | string = string.strip() |
|
1579 | string = string.strip() | |
1580 | try: |
|
1580 | try: | |
1581 | when, offset = map(int, string.split(' ')) |
|
1581 | when, offset = map(int, string.split(' ')) | |
1582 | except ValueError: |
|
1582 | except ValueError: | |
1583 | # fill out defaults |
|
1583 | # fill out defaults | |
1584 | if not defaults: |
|
1584 | if not defaults: | |
1585 | defaults = {} |
|
1585 | defaults = {} | |
1586 | now = makedate() |
|
1586 | now = makedate() | |
1587 | for part in "d mb yY HI M S".split(): |
|
1587 | for part in "d mb yY HI M S".split(): | |
1588 | if part not in defaults: |
|
1588 | if part not in defaults: | |
1589 | if part[0] in "HMS": |
|
1589 | if part[0] in "HMS": | |
1590 | defaults[part] = "00" |
|
1590 | defaults[part] = "00" | |
1591 | elif part[0] in "dm": |
|
1591 | elif part[0] in "dm": | |
1592 | defaults[part] = "1" |
|
1592 | defaults[part] = "1" | |
1593 | else: |
|
1593 | else: | |
1594 | defaults[part] = datestr(now, "%" + part[0], False) |
|
1594 | defaults[part] = datestr(now, "%" + part[0], False) | |
1595 |
|
1595 | |||
1596 | for format in formats: |
|
1596 | for format in formats: | |
1597 | try: |
|
1597 | try: | |
1598 | when, offset = strdate(string, format, defaults) |
|
1598 | when, offset = strdate(string, format, defaults) | |
1599 | except (ValueError, OverflowError): |
|
1599 | except (ValueError, OverflowError): | |
1600 | pass |
|
1600 | pass | |
1601 | else: |
|
1601 | else: | |
1602 | break |
|
1602 | break | |
1603 | else: |
|
1603 | else: | |
1604 | raise Abort(_('invalid date: %r ') % string) |
|
1604 | raise Abort(_('invalid date: %r ') % string) | |
1605 | # validate explicit (probably user-specified) date and |
|
1605 | # validate explicit (probably user-specified) date and | |
1606 | # time zone offset. values must fit in signed 32 bits for |
|
1606 | # time zone offset. values must fit in signed 32 bits for | |
1607 | # current 32-bit linux runtimes. timezones go from UTC-12 |
|
1607 | # current 32-bit linux runtimes. timezones go from UTC-12 | |
1608 | # to UTC+14 |
|
1608 | # to UTC+14 | |
1609 | if abs(when) > 0x7fffffff: |
|
1609 | if abs(when) > 0x7fffffff: | |
1610 | raise Abort(_('date exceeds 32 bits: %d') % when) |
|
1610 | raise Abort(_('date exceeds 32 bits: %d') % when) | |
1611 | if offset < -50400 or offset > 43200: |
|
1611 | if offset < -50400 or offset > 43200: | |
1612 | raise Abort(_('impossible time zone offset: %d') % offset) |
|
1612 | raise Abort(_('impossible time zone offset: %d') % offset) | |
1613 | return when, offset |
|
1613 | return when, offset | |
1614 |
|
1614 | |||
1615 | def matchdate(date): |
|
1615 | def matchdate(date): | |
1616 | """Return a function that matches a given date match specifier |
|
1616 | """Return a function that matches a given date match specifier | |
1617 |
|
1617 | |||
1618 | Formats include: |
|
1618 | Formats include: | |
1619 |
|
1619 | |||
1620 | '{date}' match a given date to the accuracy provided |
|
1620 | '{date}' match a given date to the accuracy provided | |
1621 |
|
1621 | |||
1622 | '<{date}' on or before a given date |
|
1622 | '<{date}' on or before a given date | |
1623 |
|
1623 | |||
1624 | '>{date}' on or after a given date |
|
1624 | '>{date}' on or after a given date | |
1625 |
|
1625 | |||
1626 | """ |
|
1626 | """ | |
1627 |
|
1627 | |||
1628 | def lower(date): |
|
1628 | def lower(date): | |
1629 | return parsedate(date, extendeddateformats)[0] |
|
1629 | return parsedate(date, extendeddateformats)[0] | |
1630 |
|
1630 | |||
1631 | def upper(date): |
|
1631 | def upper(date): | |
1632 | d = dict(mb="12", HI="23", M="59", S="59") |
|
1632 | d = dict(mb="12", HI="23", M="59", S="59") | |
1633 | for days in "31 30 29".split(): |
|
1633 | for days in "31 30 29".split(): | |
1634 | try: |
|
1634 | try: | |
1635 | d["d"] = days |
|
1635 | d["d"] = days | |
1636 | return parsedate(date, extendeddateformats, d)[0] |
|
1636 | return parsedate(date, extendeddateformats, d)[0] | |
1637 | except: |
|
1637 | except: | |
1638 | pass |
|
1638 | pass | |
1639 | d["d"] = "28" |
|
1639 | d["d"] = "28" | |
1640 | return parsedate(date, extendeddateformats, d)[0] |
|
1640 | return parsedate(date, extendeddateformats, d)[0] | |
1641 |
|
1641 | |||
1642 | if date[0] == "<": |
|
1642 | if date[0] == "<": | |
1643 | when = upper(date[1:]) |
|
1643 | when = upper(date[1:]) | |
1644 | return lambda x: x <= when |
|
1644 | return lambda x: x <= when | |
1645 | elif date[0] == ">": |
|
1645 | elif date[0] == ">": | |
1646 | when = lower(date[1:]) |
|
1646 | when = lower(date[1:]) | |
1647 | return lambda x: x >= when |
|
1647 | return lambda x: x >= when | |
1648 | elif date[0] == "-": |
|
1648 | elif date[0] == "-": | |
1649 | try: |
|
1649 | try: | |
1650 | days = int(date[1:]) |
|
1650 | days = int(date[1:]) | |
1651 | except ValueError: |
|
1651 | except ValueError: | |
1652 | raise Abort(_("invalid day spec: %s") % date[1:]) |
|
1652 | raise Abort(_("invalid day spec: %s") % date[1:]) | |
1653 | when = makedate()[0] - days * 3600 * 24 |
|
1653 | when = makedate()[0] - days * 3600 * 24 | |
1654 | return lambda x: x >= when |
|
1654 | return lambda x: x >= when | |
1655 | elif " to " in date: |
|
1655 | elif " to " in date: | |
1656 | a, b = date.split(" to ") |
|
1656 | a, b = date.split(" to ") | |
1657 | start, stop = lower(a), upper(b) |
|
1657 | start, stop = lower(a), upper(b) | |
1658 | return lambda x: x >= start and x <= stop |
|
1658 | return lambda x: x >= start and x <= stop | |
1659 | else: |
|
1659 | else: | |
1660 | start, stop = lower(date), upper(date) |
|
1660 | start, stop = lower(date), upper(date) | |
1661 | return lambda x: x >= start and x <= stop |
|
1661 | return lambda x: x >= start and x <= stop | |
1662 |
|
1662 | |||
1663 | def shortuser(user): |
|
1663 | def shortuser(user): | |
1664 | """Return a short representation of a user name or email address.""" |
|
1664 | """Return a short representation of a user name or email address.""" | |
1665 | f = user.find('@') |
|
1665 | f = user.find('@') | |
1666 | if f >= 0: |
|
1666 | if f >= 0: | |
1667 | user = user[:f] |
|
1667 | user = user[:f] | |
1668 | f = user.find('<') |
|
1668 | f = user.find('<') | |
1669 | if f >= 0: |
|
1669 | if f >= 0: | |
1670 | user = user[f+1:] |
|
1670 | user = user[f+1:] | |
1671 | f = user.find(' ') |
|
1671 | f = user.find(' ') | |
1672 | if f >= 0: |
|
1672 | if f >= 0: | |
1673 | user = user[:f] |
|
1673 | user = user[:f] | |
1674 | f = user.find('.') |
|
1674 | f = user.find('.') | |
1675 | if f >= 0: |
|
1675 | if f >= 0: | |
1676 | user = user[:f] |
|
1676 | user = user[:f] | |
1677 | return user |
|
1677 | return user | |
1678 |
|
1678 | |||
1679 | def email(author): |
|
1679 | def email(author): | |
1680 | '''get email of author.''' |
|
1680 | '''get email of author.''' | |
1681 | r = author.find('>') |
|
1681 | r = author.find('>') | |
1682 | if r == -1: r = None |
|
1682 | if r == -1: r = None | |
1683 | return author[author.find('<')+1:r] |
|
1683 | return author[author.find('<')+1:r] | |
1684 |
|
1684 | |||
1685 | def ellipsis(text, maxlength=400): |
|
1685 | def ellipsis(text, maxlength=400): | |
1686 | """Trim string to at most maxlength (default: 400) characters.""" |
|
1686 | """Trim string to at most maxlength (default: 400) characters.""" | |
1687 | if len(text) <= maxlength: |
|
1687 | if len(text) <= maxlength: | |
1688 | return text |
|
1688 | return text | |
1689 | else: |
|
1689 | else: | |
1690 | return "%s..." % (text[:maxlength-3]) |
|
1690 | return "%s..." % (text[:maxlength-3]) | |
1691 |
|
1691 | |||
1692 | def walkrepos(path): |
|
1692 | def walkrepos(path): | |
1693 | '''yield every hg repository under path, recursively.''' |
|
1693 | '''yield every hg repository under path, recursively.''' | |
1694 | def errhandler(err): |
|
1694 | def errhandler(err): | |
1695 | if err.filename == path: |
|
1695 | if err.filename == path: | |
1696 | raise err |
|
1696 | raise err | |
1697 |
|
1697 | |||
1698 | for root, dirs, files in os.walk(path, onerror=errhandler): |
|
1698 | for root, dirs, files in os.walk(path, onerror=errhandler): | |
1699 | for d in dirs: |
|
1699 | for d in dirs: | |
1700 | if d == '.hg': |
|
1700 | if d == '.hg': | |
1701 | yield root |
|
1701 | yield root | |
1702 | dirs[:] = [] |
|
1702 | dirs[:] = [] | |
1703 | break |
|
1703 | break | |
1704 |
|
1704 | |||
1705 | _rcpath = None |
|
1705 | _rcpath = None | |
1706 |
|
1706 | |||
1707 | def os_rcpath(): |
|
1707 | def os_rcpath(): | |
1708 | '''return default os-specific hgrc search path''' |
|
1708 | '''return default os-specific hgrc search path''' | |
1709 | path = system_rcpath() |
|
1709 | path = system_rcpath() | |
1710 | path.extend(user_rcpath()) |
|
1710 | path.extend(user_rcpath()) | |
1711 | path = [os.path.normpath(f) for f in path] |
|
1711 | path = [os.path.normpath(f) for f in path] | |
1712 | return path |
|
1712 | return path | |
1713 |
|
1713 | |||
1714 | def rcpath(): |
|
1714 | def rcpath(): | |
1715 | '''return hgrc search path. if env var HGRCPATH is set, use it. |
|
1715 | '''return hgrc search path. if env var HGRCPATH is set, use it. | |
1716 | for each item in path, if directory, use files ending in .rc, |
|
1716 | for each item in path, if directory, use files ending in .rc, | |
1717 | else use item. |
|
1717 | else use item. | |
1718 | make HGRCPATH empty to only look in .hg/hgrc of current repo. |
|
1718 | make HGRCPATH empty to only look in .hg/hgrc of current repo. | |
1719 | if no HGRCPATH, use default os-specific path.''' |
|
1719 | if no HGRCPATH, use default os-specific path.''' | |
1720 | global _rcpath |
|
1720 | global _rcpath | |
1721 | if _rcpath is None: |
|
1721 | if _rcpath is None: | |
1722 | if 'HGRCPATH' in os.environ: |
|
1722 | if 'HGRCPATH' in os.environ: | |
1723 | _rcpath = [] |
|
1723 | _rcpath = [] | |
1724 | for p in os.environ['HGRCPATH'].split(os.pathsep): |
|
1724 | for p in os.environ['HGRCPATH'].split(os.pathsep): | |
1725 | if not p: continue |
|
1725 | if not p: continue | |
1726 | if os.path.isdir(p): |
|
1726 | if os.path.isdir(p): | |
1727 | for f, kind in osutil.listdir(p): |
|
1727 | for f, kind in osutil.listdir(p): | |
1728 | if f.endswith('.rc'): |
|
1728 | if f.endswith('.rc'): | |
1729 | _rcpath.append(os.path.join(p, f)) |
|
1729 | _rcpath.append(os.path.join(p, f)) | |
1730 | else: |
|
1730 | else: | |
1731 | _rcpath.append(p) |
|
1731 | _rcpath.append(p) | |
1732 | else: |
|
1732 | else: | |
1733 | _rcpath = os_rcpath() |
|
1733 | _rcpath = os_rcpath() | |
1734 | return _rcpath |
|
1734 | return _rcpath | |
1735 |
|
1735 | |||
1736 | def bytecount(nbytes): |
|
1736 | def bytecount(nbytes): | |
1737 | '''return byte count formatted as readable string, with units''' |
|
1737 | '''return byte count formatted as readable string, with units''' | |
1738 |
|
1738 | |||
1739 | units = ( |
|
1739 | units = ( | |
1740 | (100, 1<<30, _('%.0f GB')), |
|
1740 | (100, 1<<30, _('%.0f GB')), | |
1741 | (10, 1<<30, _('%.1f GB')), |
|
1741 | (10, 1<<30, _('%.1f GB')), | |
1742 | (1, 1<<30, _('%.2f GB')), |
|
1742 | (1, 1<<30, _('%.2f GB')), | |
1743 | (100, 1<<20, _('%.0f MB')), |
|
1743 | (100, 1<<20, _('%.0f MB')), | |
1744 | (10, 1<<20, _('%.1f MB')), |
|
1744 | (10, 1<<20, _('%.1f MB')), | |
1745 | (1, 1<<20, _('%.2f MB')), |
|
1745 | (1, 1<<20, _('%.2f MB')), | |
1746 | (100, 1<<10, _('%.0f KB')), |
|
1746 | (100, 1<<10, _('%.0f KB')), | |
1747 | (10, 1<<10, _('%.1f KB')), |
|
1747 | (10, 1<<10, _('%.1f KB')), | |
1748 | (1, 1<<10, _('%.2f KB')), |
|
1748 | (1, 1<<10, _('%.2f KB')), | |
1749 | (1, 1, _('%.0f bytes')), |
|
1749 | (1, 1, _('%.0f bytes')), | |
1750 | ) |
|
1750 | ) | |
1751 |
|
1751 | |||
1752 | for multiplier, divisor, format in units: |
|
1752 | for multiplier, divisor, format in units: | |
1753 | if nbytes >= divisor * multiplier: |
|
1753 | if nbytes >= divisor * multiplier: | |
1754 | return format % (nbytes / float(divisor)) |
|
1754 | return format % (nbytes / float(divisor)) | |
1755 | return units[-1][2] % nbytes |
|
1755 | return units[-1][2] % nbytes | |
1756 |
|
1756 | |||
1757 | def drop_scheme(scheme, path): |
|
1757 | def drop_scheme(scheme, path): | |
1758 | sc = scheme + ':' |
|
1758 | sc = scheme + ':' | |
1759 | if path.startswith(sc): |
|
1759 | if path.startswith(sc): | |
1760 | path = path[len(sc):] |
|
1760 | path = path[len(sc):] | |
1761 | if path.startswith('//'): |
|
1761 | if path.startswith('//'): | |
1762 | path = path[2:] |
|
1762 | path = path[2:] | |
1763 | return path |
|
1763 | return path | |
1764 |
|
1764 | |||
1765 | def uirepr(s): |
|
1765 | def uirepr(s): | |
1766 | # Avoid double backslash in Windows path repr() |
|
1766 | # Avoid double backslash in Windows path repr() | |
1767 | return repr(s).replace('\\\\', '\\') |
|
1767 | return repr(s).replace('\\\\', '\\') | |
1768 |
|
1768 | |||
1769 | def hidepassword(url): |
|
1769 | def hidepassword(url): | |
1770 | '''hide user credential in a url string''' |
|
1770 | '''hide user credential in a url string''' | |
1771 | scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
|
1771 | scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) | |
1772 | netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc) |
|
1772 | netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc) | |
1773 | return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) |
|
1773 | return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) | |
1774 |
|
1774 | |||
1775 | def removeauth(url): |
|
1775 | def removeauth(url): | |
1776 | '''remove all authentication information from a url string''' |
|
1776 | '''remove all authentication information from a url string''' | |
1777 | scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
|
1777 | scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) | |
1778 | netloc = netloc[netloc.find('@')+1:] |
|
1778 | netloc = netloc[netloc.find('@')+1:] | |
1779 | return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) |
|
1779 | return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) |
General Comments 0
You need to be logged in to leave comments.
Login now