Show More
@@ -1,386 +1,388 | |||||
1 | # ui.py - user interface bits for mercurial |
|
1 | # ui.py - user interface bits for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2, incorporated herein by reference. |
|
6 | # GNU General Public License version 2, incorporated herein by reference. | |
7 |
|
7 | |||
8 | from i18n import _ |
|
8 | from i18n import _ | |
9 | import errno, getpass, os, socket, sys, tempfile, traceback |
|
9 | import errno, getpass, os, socket, sys, tempfile, traceback | |
10 | import config, util, error |
|
10 | import config, util, error | |
11 |
|
11 | |||
12 | _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, |
|
12 | _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, | |
13 | '0': False, 'no': False, 'false': False, 'off': False} |
|
13 | '0': False, 'no': False, 'false': False, 'off': False} | |
14 |
|
14 | |||
15 | class ui(object): |
|
15 | class ui(object): | |
16 | def __init__(self, src=None): |
|
16 | def __init__(self, src=None): | |
17 | self._buffers = [] |
|
17 | self._buffers = [] | |
18 | self.quiet = self.verbose = self.debugflag = self.tracebackflag = False |
|
18 | self.quiet = self.verbose = self.debugflag = self.tracebackflag = False | |
19 | self._reportuntrusted = True |
|
19 | self._reportuntrusted = True | |
20 | self._ocfg = config.config() # overlay |
|
20 | self._ocfg = config.config() # overlay | |
21 | self._tcfg = config.config() # trusted |
|
21 | self._tcfg = config.config() # trusted | |
22 | self._ucfg = config.config() # untrusted |
|
22 | self._ucfg = config.config() # untrusted | |
23 | self._trustusers = set() |
|
23 | self._trustusers = set() | |
24 | self._trustgroups = set() |
|
24 | self._trustgroups = set() | |
25 |
|
25 | |||
26 | if src: |
|
26 | if src: | |
27 | self._tcfg = src._tcfg.copy() |
|
27 | self._tcfg = src._tcfg.copy() | |
28 | self._ucfg = src._ucfg.copy() |
|
28 | self._ucfg = src._ucfg.copy() | |
29 | self._ocfg = src._ocfg.copy() |
|
29 | self._ocfg = src._ocfg.copy() | |
30 | self._trustusers = src._trustusers.copy() |
|
30 | self._trustusers = src._trustusers.copy() | |
31 | self._trustgroups = src._trustgroups.copy() |
|
31 | self._trustgroups = src._trustgroups.copy() | |
32 | self.fixconfig() |
|
32 | self.fixconfig() | |
33 | else: |
|
33 | else: | |
34 | # we always trust global config files |
|
34 | # we always trust global config files | |
35 | for f in util.rcpath(): |
|
35 | for f in util.rcpath(): | |
36 | self.readconfig(f, trust=True) |
|
36 | self.readconfig(f, trust=True) | |
37 |
|
37 | |||
38 | def copy(self): |
|
38 | def copy(self): | |
39 | return self.__class__(self) |
|
39 | return self.__class__(self) | |
40 |
|
40 | |||
41 | def _is_trusted(self, fp, f): |
|
41 | def _is_trusted(self, fp, f): | |
42 | st = util.fstat(fp) |
|
42 | st = util.fstat(fp) | |
43 | if util.isowner(st): |
|
43 | if util.isowner(st): | |
44 | return True |
|
44 | return True | |
45 |
|
45 | |||
46 | tusers, tgroups = self._trustusers, self._trustgroups |
|
46 | tusers, tgroups = self._trustusers, self._trustgroups | |
47 | if '*' in tusers or '*' in tgroups: |
|
47 | if '*' in tusers or '*' in tgroups: | |
48 | return True |
|
48 | return True | |
49 |
|
49 | |||
50 | user = util.username(st.st_uid) |
|
50 | user = util.username(st.st_uid) | |
51 | group = util.groupname(st.st_gid) |
|
51 | group = util.groupname(st.st_gid) | |
52 | if user in tusers or group in tgroups or user == util.username(): |
|
52 | if user in tusers or group in tgroups or user == util.username(): | |
53 | return True |
|
53 | return True | |
54 |
|
54 | |||
55 | if self._reportuntrusted: |
|
55 | if self._reportuntrusted: | |
56 | self.warn(_('Not trusting file %s from untrusted ' |
|
56 | self.warn(_('Not trusting file %s from untrusted ' | |
57 | 'user %s, group %s\n') % (f, user, group)) |
|
57 | 'user %s, group %s\n') % (f, user, group)) | |
58 | return False |
|
58 | return False | |
59 |
|
59 | |||
60 | def readconfig(self, filename, root=None, trust=False, |
|
60 | def readconfig(self, filename, root=None, trust=False, | |
61 | sections=None, remap=None): |
|
61 | sections=None, remap=None): | |
62 | try: |
|
62 | try: | |
63 | fp = open(filename) |
|
63 | fp = open(filename) | |
64 | except IOError: |
|
64 | except IOError: | |
65 | if not sections: # ignore unless we were looking for something |
|
65 | if not sections: # ignore unless we were looking for something | |
66 | return |
|
66 | return | |
67 | raise |
|
67 | raise | |
68 |
|
68 | |||
69 | cfg = config.config() |
|
69 | cfg = config.config() | |
70 | trusted = sections or trust or self._is_trusted(fp, filename) |
|
70 | trusted = sections or trust or self._is_trusted(fp, filename) | |
71 |
|
71 | |||
72 | try: |
|
72 | try: | |
73 | cfg.read(filename, fp, sections=sections, remap=remap) |
|
73 | cfg.read(filename, fp, sections=sections, remap=remap) | |
74 | except error.ConfigError, inst: |
|
74 | except error.ConfigError, inst: | |
75 | if trusted: |
|
75 | if trusted: | |
76 | raise |
|
76 | raise | |
77 | self.warn(_("Ignored: %s\n") % str(inst)) |
|
77 | self.warn(_("Ignored: %s\n") % str(inst)) | |
78 |
|
78 | |||
79 | if trusted: |
|
79 | if trusted: | |
80 | self._tcfg.update(cfg) |
|
80 | self._tcfg.update(cfg) | |
81 | self._tcfg.update(self._ocfg) |
|
81 | self._tcfg.update(self._ocfg) | |
82 | self._ucfg.update(cfg) |
|
82 | self._ucfg.update(cfg) | |
83 | self._ucfg.update(self._ocfg) |
|
83 | self._ucfg.update(self._ocfg) | |
84 |
|
84 | |||
85 | if root is None: |
|
85 | if root is None: | |
86 | root = os.path.expanduser('~') |
|
86 | root = os.path.expanduser('~') | |
87 | self.fixconfig(root=root) |
|
87 | self.fixconfig(root=root) | |
88 |
|
88 | |||
89 | def fixconfig(self, root=None): |
|
89 | def fixconfig(self, root=None): | |
90 | # translate paths relative to root (or home) into absolute paths |
|
90 | # translate paths relative to root (or home) into absolute paths | |
91 | root = root or os.getcwd() |
|
91 | root = root or os.getcwd() | |
92 | for c in self._tcfg, self._ucfg, self._ocfg: |
|
92 | for c in self._tcfg, self._ucfg, self._ocfg: | |
93 | for n, p in c.items('paths'): |
|
93 | for n, p in c.items('paths'): | |
94 | if p and "://" not in p and not os.path.isabs(p): |
|
94 | if p and "://" not in p and not os.path.isabs(p): | |
95 | c.set("paths", n, os.path.normpath(os.path.join(root, p))) |
|
95 | c.set("paths", n, os.path.normpath(os.path.join(root, p))) | |
96 |
|
96 | |||
97 | # update ui options |
|
97 | # update ui options | |
98 | self.debugflag = self.configbool('ui', 'debug') |
|
98 | self.debugflag = self.configbool('ui', 'debug') | |
99 | self.verbose = self.debugflag or self.configbool('ui', 'verbose') |
|
99 | self.verbose = self.debugflag or self.configbool('ui', 'verbose') | |
100 | self.quiet = not self.debugflag and self.configbool('ui', 'quiet') |
|
100 | self.quiet = not self.debugflag and self.configbool('ui', 'quiet') | |
101 | if self.verbose and self.quiet: |
|
101 | if self.verbose and self.quiet: | |
102 | self.quiet = self.verbose = False |
|
102 | self.quiet = self.verbose = False | |
103 | self._reportuntrusted = self.configbool("ui", "report_untrusted", True) |
|
103 | self._reportuntrusted = self.configbool("ui", "report_untrusted", True) | |
104 | self.tracebackflag = self.configbool('ui', 'traceback', False) |
|
104 | self.tracebackflag = self.configbool('ui', 'traceback', False) | |
105 |
|
105 | |||
106 | # update trust information |
|
106 | # update trust information | |
107 | self._trustusers.update(self.configlist('trusted', 'users')) |
|
107 | self._trustusers.update(self.configlist('trusted', 'users')) | |
108 | self._trustgroups.update(self.configlist('trusted', 'groups')) |
|
108 | self._trustgroups.update(self.configlist('trusted', 'groups')) | |
109 |
|
109 | |||
110 | def setconfig(self, section, name, value): |
|
110 | def setconfig(self, section, name, value): | |
111 | for cfg in (self._ocfg, self._tcfg, self._ucfg): |
|
111 | for cfg in (self._ocfg, self._tcfg, self._ucfg): | |
112 | cfg.set(section, name, value) |
|
112 | cfg.set(section, name, value) | |
113 | self.fixconfig() |
|
113 | self.fixconfig() | |
114 |
|
114 | |||
115 | def _data(self, untrusted): |
|
115 | def _data(self, untrusted): | |
116 | return untrusted and self._ucfg or self._tcfg |
|
116 | return untrusted and self._ucfg or self._tcfg | |
117 |
|
117 | |||
118 | def configsource(self, section, name, untrusted=False): |
|
118 | def configsource(self, section, name, untrusted=False): | |
119 | return self._data(untrusted).source(section, name) or 'none' |
|
119 | return self._data(untrusted).source(section, name) or 'none' | |
120 |
|
120 | |||
121 | def config(self, section, name, default=None, untrusted=False): |
|
121 | def config(self, section, name, default=None, untrusted=False): | |
122 | value = self._data(untrusted).get(section, name, default) |
|
122 | value = self._data(untrusted).get(section, name, default) | |
123 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
123 | if self.debugflag and not untrusted and self._reportuntrusted: | |
124 | uvalue = self._ucfg.get(section, name) |
|
124 | uvalue = self._ucfg.get(section, name) | |
125 | if uvalue is not None and uvalue != value: |
|
125 | if uvalue is not None and uvalue != value: | |
126 | self.debug(_("ignoring untrusted configuration option " |
|
126 | self.debug(_("ignoring untrusted configuration option " | |
127 | "%s.%s = %s\n") % (section, name, uvalue)) |
|
127 | "%s.%s = %s\n") % (section, name, uvalue)) | |
128 | return value |
|
128 | return value | |
129 |
|
129 | |||
130 | def configbool(self, section, name, default=False, untrusted=False): |
|
130 | def configbool(self, section, name, default=False, untrusted=False): | |
131 | v = self.config(section, name, None, untrusted) |
|
131 | v = self.config(section, name, None, untrusted) | |
132 | if v is None: |
|
132 | if v is None: | |
133 | return default |
|
133 | return default | |
|
134 | if isinstance(v, bool): | |||
|
135 | return v | |||
134 | if v.lower() not in _booleans: |
|
136 | if v.lower() not in _booleans: | |
135 | raise error.ConfigError(_("%s.%s not a boolean ('%s')") |
|
137 | raise error.ConfigError(_("%s.%s not a boolean ('%s')") | |
136 | % (section, name, v)) |
|
138 | % (section, name, v)) | |
137 | return _booleans[v.lower()] |
|
139 | return _booleans[v.lower()] | |
138 |
|
140 | |||
139 | def configlist(self, section, name, default=None, untrusted=False): |
|
141 | def configlist(self, section, name, default=None, untrusted=False): | |
140 | """Return a list of comma/space separated strings""" |
|
142 | """Return a list of comma/space separated strings""" | |
141 | result = self.config(section, name, untrusted=untrusted) |
|
143 | result = self.config(section, name, untrusted=untrusted) | |
142 | if result is None: |
|
144 | if result is None: | |
143 | result = default or [] |
|
145 | result = default or [] | |
144 | if isinstance(result, basestring): |
|
146 | if isinstance(result, basestring): | |
145 | result = result.replace(",", " ").split() |
|
147 | result = result.replace(",", " ").split() | |
146 | return result |
|
148 | return result | |
147 |
|
149 | |||
148 | def has_section(self, section, untrusted=False): |
|
150 | def has_section(self, section, untrusted=False): | |
149 | '''tell whether section exists in config.''' |
|
151 | '''tell whether section exists in config.''' | |
150 | return section in self._data(untrusted) |
|
152 | return section in self._data(untrusted) | |
151 |
|
153 | |||
152 | def configitems(self, section, untrusted=False): |
|
154 | def configitems(self, section, untrusted=False): | |
153 | items = self._data(untrusted).items(section) |
|
155 | items = self._data(untrusted).items(section) | |
154 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
156 | if self.debugflag and not untrusted and self._reportuntrusted: | |
155 | for k, v in self._ucfg.items(section): |
|
157 | for k, v in self._ucfg.items(section): | |
156 | if self._tcfg.get(section, k) != v: |
|
158 | if self._tcfg.get(section, k) != v: | |
157 | self.debug(_("ignoring untrusted configuration option " |
|
159 | self.debug(_("ignoring untrusted configuration option " | |
158 | "%s.%s = %s\n") % (section, k, v)) |
|
160 | "%s.%s = %s\n") % (section, k, v)) | |
159 | return items |
|
161 | return items | |
160 |
|
162 | |||
161 | def walkconfig(self, untrusted=False): |
|
163 | def walkconfig(self, untrusted=False): | |
162 | cfg = self._data(untrusted) |
|
164 | cfg = self._data(untrusted) | |
163 | for section in cfg.sections(): |
|
165 | for section in cfg.sections(): | |
164 | for name, value in self.configitems(section, untrusted): |
|
166 | for name, value in self.configitems(section, untrusted): | |
165 | yield section, name, str(value).replace('\n', '\\n') |
|
167 | yield section, name, str(value).replace('\n', '\\n') | |
166 |
|
168 | |||
167 | def username(self): |
|
169 | def username(self): | |
168 | """Return default username to be used in commits. |
|
170 | """Return default username to be used in commits. | |
169 |
|
171 | |||
170 | Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL |
|
172 | Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL | |
171 | and stop searching if one of these is set. |
|
173 | and stop searching if one of these is set. | |
172 | If not found and ui.askusername is True, ask the user, else use |
|
174 | If not found and ui.askusername is True, ask the user, else use | |
173 | ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". |
|
175 | ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". | |
174 | """ |
|
176 | """ | |
175 | user = os.environ.get("HGUSER") |
|
177 | user = os.environ.get("HGUSER") | |
176 | if user is None: |
|
178 | if user is None: | |
177 | user = self.config("ui", "username") |
|
179 | user = self.config("ui", "username") | |
178 | if user is None: |
|
180 | if user is None: | |
179 | user = os.environ.get("EMAIL") |
|
181 | user = os.environ.get("EMAIL") | |
180 | if user is None and self.configbool("ui", "askusername"): |
|
182 | if user is None and self.configbool("ui", "askusername"): | |
181 | user = self.prompt(_("enter a commit username:"), default=None) |
|
183 | user = self.prompt(_("enter a commit username:"), default=None) | |
182 | if user is None and not self.interactive(): |
|
184 | if user is None and not self.interactive(): | |
183 | try: |
|
185 | try: | |
184 | user = '%s@%s' % (util.getuser(), socket.getfqdn()) |
|
186 | user = '%s@%s' % (util.getuser(), socket.getfqdn()) | |
185 | self.warn(_("No username found, using '%s' instead\n") % user) |
|
187 | self.warn(_("No username found, using '%s' instead\n") % user) | |
186 | except KeyError: |
|
188 | except KeyError: | |
187 | pass |
|
189 | pass | |
188 | if not user: |
|
190 | if not user: | |
189 | raise util.Abort(_('no username supplied (see "hg help config")')) |
|
191 | raise util.Abort(_('no username supplied (see "hg help config")')) | |
190 | if "\n" in user: |
|
192 | if "\n" in user: | |
191 | raise util.Abort(_("username %s contains a newline\n") % repr(user)) |
|
193 | raise util.Abort(_("username %s contains a newline\n") % repr(user)) | |
192 | return user |
|
194 | return user | |
193 |
|
195 | |||
194 | def shortuser(self, user): |
|
196 | def shortuser(self, user): | |
195 | """Return a short representation of a user name or email address.""" |
|
197 | """Return a short representation of a user name or email address.""" | |
196 | if not self.verbose: user = util.shortuser(user) |
|
198 | if not self.verbose: user = util.shortuser(user) | |
197 | return user |
|
199 | return user | |
198 |
|
200 | |||
199 | def _path(self, loc): |
|
201 | def _path(self, loc): | |
200 | p = self.config('paths', loc) |
|
202 | p = self.config('paths', loc) | |
201 | if p: |
|
203 | if p: | |
202 | if '%%' in p: |
|
204 | if '%%' in p: | |
203 | self.warn("(deprecated '%%' in path %s=%s from %s)\n" % |
|
205 | self.warn("(deprecated '%%' in path %s=%s from %s)\n" % | |
204 | (loc, p, self.configsource('paths', loc))) |
|
206 | (loc, p, self.configsource('paths', loc))) | |
205 | p = p.replace('%%', '%') |
|
207 | p = p.replace('%%', '%') | |
206 | p = util.expandpath(p) |
|
208 | p = util.expandpath(p) | |
207 | return p |
|
209 | return p | |
208 |
|
210 | |||
209 | def expandpath(self, loc, default=None): |
|
211 | def expandpath(self, loc, default=None): | |
210 | """Return repository location relative to cwd or from [paths]""" |
|
212 | """Return repository location relative to cwd or from [paths]""" | |
211 | if "://" in loc or os.path.isdir(os.path.join(loc, '.hg')): |
|
213 | if "://" in loc or os.path.isdir(os.path.join(loc, '.hg')): | |
212 | return loc |
|
214 | return loc | |
213 |
|
215 | |||
214 | path = self._path(loc) |
|
216 | path = self._path(loc) | |
215 | if not path and default is not None: |
|
217 | if not path and default is not None: | |
216 | path = self._path(default) |
|
218 | path = self._path(default) | |
217 | return path or loc |
|
219 | return path or loc | |
218 |
|
220 | |||
219 | def pushbuffer(self): |
|
221 | def pushbuffer(self): | |
220 | self._buffers.append([]) |
|
222 | self._buffers.append([]) | |
221 |
|
223 | |||
222 | def popbuffer(self): |
|
224 | def popbuffer(self): | |
223 | return "".join(self._buffers.pop()) |
|
225 | return "".join(self._buffers.pop()) | |
224 |
|
226 | |||
225 | def write(self, *args): |
|
227 | def write(self, *args): | |
226 | if self._buffers: |
|
228 | if self._buffers: | |
227 | self._buffers[-1].extend([str(a) for a in args]) |
|
229 | self._buffers[-1].extend([str(a) for a in args]) | |
228 | else: |
|
230 | else: | |
229 | for a in args: |
|
231 | for a in args: | |
230 | sys.stdout.write(str(a)) |
|
232 | sys.stdout.write(str(a)) | |
231 |
|
233 | |||
232 | def write_err(self, *args): |
|
234 | def write_err(self, *args): | |
233 | try: |
|
235 | try: | |
234 | if not sys.stdout.closed: sys.stdout.flush() |
|
236 | if not sys.stdout.closed: sys.stdout.flush() | |
235 | for a in args: |
|
237 | for a in args: | |
236 | sys.stderr.write(str(a)) |
|
238 | sys.stderr.write(str(a)) | |
237 | # stderr may be buffered under win32 when redirected to files, |
|
239 | # stderr may be buffered under win32 when redirected to files, | |
238 | # including stdout. |
|
240 | # including stdout. | |
239 | if not sys.stderr.closed: sys.stderr.flush() |
|
241 | if not sys.stderr.closed: sys.stderr.flush() | |
240 | except IOError, inst: |
|
242 | except IOError, inst: | |
241 | if inst.errno != errno.EPIPE: |
|
243 | if inst.errno != errno.EPIPE: | |
242 | raise |
|
244 | raise | |
243 |
|
245 | |||
244 | def flush(self): |
|
246 | def flush(self): | |
245 | try: sys.stdout.flush() |
|
247 | try: sys.stdout.flush() | |
246 | except: pass |
|
248 | except: pass | |
247 | try: sys.stderr.flush() |
|
249 | try: sys.stderr.flush() | |
248 | except: pass |
|
250 | except: pass | |
249 |
|
251 | |||
250 | def interactive(self): |
|
252 | def interactive(self): | |
251 | i = self.configbool("ui", "interactive", None) |
|
253 | i = self.configbool("ui", "interactive", None) | |
252 | if i is None: |
|
254 | if i is None: | |
253 | return sys.stdin.isatty() |
|
255 | return sys.stdin.isatty() | |
254 | return i |
|
256 | return i | |
255 |
|
257 | |||
256 | def _readline(self, prompt=''): |
|
258 | def _readline(self, prompt=''): | |
257 | if sys.stdin.isatty(): |
|
259 | if sys.stdin.isatty(): | |
258 | try: |
|
260 | try: | |
259 | # magically add command line editing support, where |
|
261 | # magically add command line editing support, where | |
260 | # available |
|
262 | # available | |
261 | import readline |
|
263 | import readline | |
262 | # force demandimport to really load the module |
|
264 | # force demandimport to really load the module | |
263 | readline.read_history_file |
|
265 | readline.read_history_file | |
264 | # windows sometimes raises something other than ImportError |
|
266 | # windows sometimes raises something other than ImportError | |
265 | except Exception: |
|
267 | except Exception: | |
266 | pass |
|
268 | pass | |
267 | line = raw_input(prompt) |
|
269 | line = raw_input(prompt) | |
268 | # When stdin is in binary mode on Windows, it can cause |
|
270 | # When stdin is in binary mode on Windows, it can cause | |
269 | # raw_input() to emit an extra trailing carriage return |
|
271 | # raw_input() to emit an extra trailing carriage return | |
270 | if os.linesep == '\r\n' and line and line[-1] == '\r': |
|
272 | if os.linesep == '\r\n' and line and line[-1] == '\r': | |
271 | line = line[:-1] |
|
273 | line = line[:-1] | |
272 | return line |
|
274 | return line | |
273 |
|
275 | |||
274 | def prompt(self, msg, default="y"): |
|
276 | def prompt(self, msg, default="y"): | |
275 | """Prompt user with msg, read response. |
|
277 | """Prompt user with msg, read response. | |
276 | If ui is not interactive, the default is returned. |
|
278 | If ui is not interactive, the default is returned. | |
277 | """ |
|
279 | """ | |
278 | if not self.interactive(): |
|
280 | if not self.interactive(): | |
279 | self.write(msg, ' ', default, "\n") |
|
281 | self.write(msg, ' ', default, "\n") | |
280 | return default |
|
282 | return default | |
281 | try: |
|
283 | try: | |
282 | r = self._readline(msg + ' ') |
|
284 | r = self._readline(msg + ' ') | |
283 | if not r: |
|
285 | if not r: | |
284 | return default |
|
286 | return default | |
285 | return r |
|
287 | return r | |
286 | except EOFError: |
|
288 | except EOFError: | |
287 | raise util.Abort(_('response expected')) |
|
289 | raise util.Abort(_('response expected')) | |
288 |
|
290 | |||
289 | def promptchoice(self, msg, choices, default=0): |
|
291 | def promptchoice(self, msg, choices, default=0): | |
290 | """Prompt user with msg, read response, and ensure it matches |
|
292 | """Prompt user with msg, read response, and ensure it matches | |
291 | one of the provided choices. The index of the choice is returned. |
|
293 | one of the provided choices. The index of the choice is returned. | |
292 | choices is a sequence of acceptable responses with the format: |
|
294 | choices is a sequence of acceptable responses with the format: | |
293 | ('&None', 'E&xec', 'Sym&link') Responses are case insensitive. |
|
295 | ('&None', 'E&xec', 'Sym&link') Responses are case insensitive. | |
294 | If ui is not interactive, the default is returned. |
|
296 | If ui is not interactive, the default is returned. | |
295 | """ |
|
297 | """ | |
296 | resps = [s[s.index('&')+1].lower() for s in choices] |
|
298 | resps = [s[s.index('&')+1].lower() for s in choices] | |
297 | while True: |
|
299 | while True: | |
298 | r = self.prompt(msg, resps[default]) |
|
300 | r = self.prompt(msg, resps[default]) | |
299 | if r.lower() in resps: |
|
301 | if r.lower() in resps: | |
300 | return resps.index(r.lower()) |
|
302 | return resps.index(r.lower()) | |
301 | self.write(_("unrecognized response\n")) |
|
303 | self.write(_("unrecognized response\n")) | |
302 |
|
304 | |||
303 |
|
305 | |||
304 | def getpass(self, prompt=None, default=None): |
|
306 | def getpass(self, prompt=None, default=None): | |
305 | if not self.interactive(): return default |
|
307 | if not self.interactive(): return default | |
306 | try: |
|
308 | try: | |
307 | return getpass.getpass(prompt or _('password: ')) |
|
309 | return getpass.getpass(prompt or _('password: ')) | |
308 | except EOFError: |
|
310 | except EOFError: | |
309 | raise util.Abort(_('response expected')) |
|
311 | raise util.Abort(_('response expected')) | |
310 | def status(self, *msg): |
|
312 | def status(self, *msg): | |
311 | if not self.quiet: self.write(*msg) |
|
313 | if not self.quiet: self.write(*msg) | |
312 | def warn(self, *msg): |
|
314 | def warn(self, *msg): | |
313 | self.write_err(*msg) |
|
315 | self.write_err(*msg) | |
314 | def note(self, *msg): |
|
316 | def note(self, *msg): | |
315 | if self.verbose: self.write(*msg) |
|
317 | if self.verbose: self.write(*msg) | |
316 | def debug(self, *msg): |
|
318 | def debug(self, *msg): | |
317 | if self.debugflag: self.write(*msg) |
|
319 | if self.debugflag: self.write(*msg) | |
318 | def edit(self, text, user): |
|
320 | def edit(self, text, user): | |
319 | (fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt", |
|
321 | (fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt", | |
320 | text=True) |
|
322 | text=True) | |
321 | try: |
|
323 | try: | |
322 | f = os.fdopen(fd, "w") |
|
324 | f = os.fdopen(fd, "w") | |
323 | f.write(text) |
|
325 | f.write(text) | |
324 | f.close() |
|
326 | f.close() | |
325 |
|
327 | |||
326 | editor = self.geteditor() |
|
328 | editor = self.geteditor() | |
327 |
|
329 | |||
328 | util.system("%s \"%s\"" % (editor, name), |
|
330 | util.system("%s \"%s\"" % (editor, name), | |
329 | environ={'HGUSER': user}, |
|
331 | environ={'HGUSER': user}, | |
330 | onerr=util.Abort, errprefix=_("edit failed")) |
|
332 | onerr=util.Abort, errprefix=_("edit failed")) | |
331 |
|
333 | |||
332 | f = open(name) |
|
334 | f = open(name) | |
333 | t = f.read() |
|
335 | t = f.read() | |
334 | f.close() |
|
336 | f.close() | |
335 | finally: |
|
337 | finally: | |
336 | os.unlink(name) |
|
338 | os.unlink(name) | |
337 |
|
339 | |||
338 | return t |
|
340 | return t | |
339 |
|
341 | |||
340 | def traceback(self, exc=None): |
|
342 | def traceback(self, exc=None): | |
341 | '''print exception traceback if traceback printing enabled. |
|
343 | '''print exception traceback if traceback printing enabled. | |
342 | only to call in exception handler. returns true if traceback |
|
344 | only to call in exception handler. returns true if traceback | |
343 | printed.''' |
|
345 | printed.''' | |
344 | if self.tracebackflag: |
|
346 | if self.tracebackflag: | |
345 | if exc: |
|
347 | if exc: | |
346 | traceback.print_exception(exc[0], exc[1], exc[2]) |
|
348 | traceback.print_exception(exc[0], exc[1], exc[2]) | |
347 | else: |
|
349 | else: | |
348 | traceback.print_exc() |
|
350 | traceback.print_exc() | |
349 | return self.tracebackflag |
|
351 | return self.tracebackflag | |
350 |
|
352 | |||
351 | def geteditor(self): |
|
353 | def geteditor(self): | |
352 | '''return editor to use''' |
|
354 | '''return editor to use''' | |
353 | return (os.environ.get("HGEDITOR") or |
|
355 | return (os.environ.get("HGEDITOR") or | |
354 | self.config("ui", "editor") or |
|
356 | self.config("ui", "editor") or | |
355 | os.environ.get("VISUAL") or |
|
357 | os.environ.get("VISUAL") or | |
356 | os.environ.get("EDITOR", "vi")) |
|
358 | os.environ.get("EDITOR", "vi")) | |
357 |
|
359 | |||
358 | def progress(self, topic, pos, item="", unit="", total=None): |
|
360 | def progress(self, topic, pos, item="", unit="", total=None): | |
359 | '''show a progress message |
|
361 | '''show a progress message | |
360 |
|
362 | |||
361 | With stock hg, this is simply a debug message that is hidden |
|
363 | With stock hg, this is simply a debug message that is hidden | |
362 | by default, but with extensions or GUI tools it may be |
|
364 | by default, but with extensions or GUI tools it may be | |
363 | visible. 'topic' is the current operation, 'item' is a |
|
365 | visible. 'topic' is the current operation, 'item' is a | |
364 | non-numeric marker of the current position (ie the currently |
|
366 | non-numeric marker of the current position (ie the currently | |
365 | in-process file), 'pos' is the current numeric position (ie |
|
367 | in-process file), 'pos' is the current numeric position (ie | |
366 | revision, bytes, etc.), unit is a corresponding unit label, |
|
368 | revision, bytes, etc.), unit is a corresponding unit label, | |
367 | and total is the highest expected pos. |
|
369 | and total is the highest expected pos. | |
368 |
|
370 | |||
369 | Multiple nested topics may be active at a time. All topics |
|
371 | Multiple nested topics may be active at a time. All topics | |
370 | should be marked closed by setting pos to None at termination. |
|
372 | should be marked closed by setting pos to None at termination. | |
371 | ''' |
|
373 | ''' | |
372 |
|
374 | |||
373 | if pos == None or not self.debugflag: |
|
375 | if pos == None or not self.debugflag: | |
374 | return |
|
376 | return | |
375 |
|
377 | |||
376 | if unit: |
|
378 | if unit: | |
377 | unit = ' ' + unit |
|
379 | unit = ' ' + unit | |
378 | if item: |
|
380 | if item: | |
379 | item = ' ' + item |
|
381 | item = ' ' + item | |
380 |
|
382 | |||
381 | if total: |
|
383 | if total: | |
382 | pct = 100.0 * pos / total |
|
384 | pct = 100.0 * pos / total | |
383 | self.debug('%s:%s %s/%s%s (%4.2f%%)\n' |
|
385 | self.debug('%s:%s %s/%s%s (%4.2f%%)\n' | |
384 | % (topic, item, pos, total, unit, pct)) |
|
386 | % (topic, item, pos, total, unit, pct)) | |
385 | else: |
|
387 | else: | |
386 | self.debug('%s:%s %s%s\n' % (topic, item, pos, unit)) |
|
388 | self.debug('%s:%s %s%s\n' % (topic, item, pos, unit)) |
General Comments 0
You need to be logged in to leave comments.
Login now