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