2005-06-01 10:57:53 +04:00
|
|
|
# ui.py - user interface bits for mercurial
|
|
|
|
#
|
2007-06-19 10:51:34 +04:00
|
|
|
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
|
2005-06-01 10:57:53 +04:00
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms
|
|
|
|
# of the GNU General Public License, incorporated herein by reference.
|
|
|
|
|
2006-12-15 05:25:19 +03:00
|
|
|
from i18n import _
|
2006-12-13 22:27:09 +03:00
|
|
|
import errno, getpass, os, re, socket, sys, tempfile
|
2009-04-24 00:40:10 +04:00
|
|
|
import config, traceback, util, error
|
|
|
|
|
|
|
|
_booleans = {'1':True, 'yes':True, 'true':True, 'on':True,
|
|
|
|
'0':False, 'no':False, 'false':False, 'off':False}
|
2006-10-11 01:43:20 +04:00
|
|
|
|
2005-11-19 09:48:47 +03:00
|
|
|
class ui(object):
|
2009-04-27 01:50:43 +04:00
|
|
|
def __init__(self, src=None):
|
2006-12-01 10:28:18 +03:00
|
|
|
self.buffers = []
|
2009-04-24 00:40:10 +04:00
|
|
|
self.quiet = self.verbose = self.debugflag = self.traceback = False
|
|
|
|
self.interactive = self.report_untrusted = True
|
2009-04-24 00:40:10 +04:00
|
|
|
self.overlay = config.config()
|
|
|
|
self.cdata = config.config()
|
|
|
|
self.ucdata = config.config()
|
2009-04-24 00:40:10 +04:00
|
|
|
self.trusted_users = {}
|
|
|
|
self.trusted_groups = {}
|
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
if src:
|
|
|
|
self.cdata = src.cdata.copy()
|
|
|
|
self.ucdata = src.ucdata.copy()
|
|
|
|
self.overlay = src.overlay.copy()
|
|
|
|
self.trusted_users = src.trusted_users.copy()
|
|
|
|
self.trusted_groups = src.trusted_groups.copy()
|
2009-04-24 00:40:10 +04:00
|
|
|
self.fixconfig()
|
2009-04-24 00:40:10 +04:00
|
|
|
else:
|
|
|
|
# we always trust global config files
|
|
|
|
for f in util.rcpath():
|
2009-04-27 01:50:44 +04:00
|
|
|
self.readconfig(f, trust=True)
|
2009-04-27 01:50:43 +04:00
|
|
|
def copy(self):
|
|
|
|
return ui(self)
|
2005-06-09 02:45:06 +04:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
_isatty = None
|
2007-08-01 03:28:05 +04:00
|
|
|
def isatty(self):
|
|
|
|
if ui._isatty is None:
|
2009-04-24 00:40:10 +04:00
|
|
|
try:
|
|
|
|
ui._isatty = sys.stdin.isatty()
|
|
|
|
except AttributeError: # not a real file object
|
|
|
|
ui._isatty = False
|
2009-04-27 00:36:08 +04:00
|
|
|
except IOError:
|
|
|
|
# access to stdin is unsafe in a WSGI environment
|
|
|
|
ui._isatty = False
|
2007-08-01 03:28:05 +04:00
|
|
|
return ui._isatty
|
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
def _is_trusted(self, fp, f):
|
2006-11-19 04:51:14 +03:00
|
|
|
st = util.fstat(fp)
|
|
|
|
if util.isowner(fp, st):
|
|
|
|
return True
|
2009-04-24 00:40:10 +04:00
|
|
|
|
2006-10-26 21:25:44 +04:00
|
|
|
tusers = self.trusted_users
|
|
|
|
tgroups = self.trusted_groups
|
2009-04-24 00:40:10 +04:00
|
|
|
if '*' in tusers or '*' in tgroups:
|
|
|
|
return True
|
|
|
|
|
|
|
|
user = util.username(st.st_uid)
|
|
|
|
group = util.groupname(st.st_gid)
|
|
|
|
if user in tusers or group in tgroups or user == util.username():
|
|
|
|
return True
|
|
|
|
|
|
|
|
if self.report_untrusted:
|
|
|
|
self.warn(_('Not trusting file %s from untrusted '
|
|
|
|
'user %s, group %s\n') % (f, user, group))
|
|
|
|
return False
|
2006-10-26 21:25:44 +04:00
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
def readconfig(self, filename, root=None, trust=False,
|
2009-04-24 00:40:10 +04:00
|
|
|
sections = None):
|
|
|
|
try:
|
|
|
|
fp = open(filename)
|
|
|
|
except IOError:
|
|
|
|
if not sections: # ignore unless we were looking for something
|
|
|
|
return
|
|
|
|
raise
|
2006-10-26 21:25:45 +04:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
cdata = config.config()
|
2009-04-27 01:50:44 +04:00
|
|
|
trusted = sections or trust or self._is_trusted(fp, filename)
|
2006-10-26 21:25:45 +04:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
try:
|
2009-04-27 01:50:43 +04:00
|
|
|
cdata.read(filename, fp, sections=sections)
|
2009-04-24 00:40:10 +04:00
|
|
|
except error.ConfigError, inst:
|
2006-10-26 21:25:45 +04:00
|
|
|
if trusted:
|
2009-04-24 00:40:10 +04:00
|
|
|
raise
|
|
|
|
self.warn(_("Ignored: %s\n") % str(inst))
|
2009-04-24 00:40:10 +04:00
|
|
|
|
|
|
|
if trusted:
|
2009-04-27 01:50:43 +04:00
|
|
|
self.cdata.update(cdata)
|
|
|
|
self.cdata.update(self.overlay)
|
|
|
|
self.ucdata.update(cdata)
|
|
|
|
self.ucdata.update(self.overlay)
|
2009-04-24 00:40:10 +04:00
|
|
|
|
2006-10-11 01:43:20 +04:00
|
|
|
if root is None:
|
|
|
|
root = os.path.expanduser('~')
|
|
|
|
self.fixconfig(root=root)
|
2006-08-23 05:49:30 +04:00
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
def fixconfig(self, root=None):
|
2006-10-11 01:43:20 +04:00
|
|
|
# translate paths relative to root (or home) into absolute paths
|
2009-04-27 01:50:43 +04:00
|
|
|
root = root or os.getcwd()
|
|
|
|
for c in self.cdata, self.ucdata, self.overlay:
|
|
|
|
for n, p in c.items('paths'):
|
|
|
|
if p and "://" not in p and not os.path.isabs(p):
|
|
|
|
c.set("paths", n, os.path.normpath(os.path.join(root, p)))
|
2006-10-11 01:43:20 +04:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
# update ui options
|
2009-04-27 01:50:43 +04:00
|
|
|
self.debugflag = self.configbool('ui', 'debug')
|
|
|
|
self.verbose = self.debugflag or self.configbool('ui', 'verbose')
|
|
|
|
self.quiet = not self.debugflag and self.configbool('ui', 'quiet')
|
|
|
|
if self.verbose and self.quiet:
|
|
|
|
self.quiet = self.verbose = False
|
|
|
|
self.report_untrusted = self.configbool("ui", "report_untrusted", True)
|
|
|
|
self.interactive = self.configbool("ui", "interactive", self.isatty())
|
|
|
|
self.traceback = self.configbool('ui', 'traceback', False)
|
2006-10-11 01:43:20 +04:00
|
|
|
|
2006-10-26 21:25:44 +04:00
|
|
|
# update trust information
|
2009-04-27 01:50:43 +04:00
|
|
|
for user in self.configlist('trusted', 'users'):
|
|
|
|
self.trusted_users[user] = 1
|
|
|
|
for group in self.configlist('trusted', 'groups'):
|
|
|
|
self.trusted_groups[group] = 1
|
2006-10-26 21:25:44 +04:00
|
|
|
|
2006-10-11 01:43:20 +04:00
|
|
|
def setconfig(self, section, name, value):
|
2006-10-26 21:25:45 +04:00
|
|
|
for cdata in (self.overlay, self.cdata, self.ucdata):
|
2006-10-11 01:43:20 +04:00
|
|
|
cdata.set(section, name, value)
|
2009-04-27 01:50:43 +04:00
|
|
|
self.fixconfig()
|
2005-08-20 09:25:25 +04:00
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
def _data(self, untrusted):
|
|
|
|
return untrusted and self.ucdata or self.cdata
|
2006-10-26 21:25:45 +04:00
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
def configsource(self, section, name, untrusted=False):
|
2009-04-27 01:50:44 +04:00
|
|
|
return self._data(untrusted).source(section, name) or 'none'
|
2009-04-27 01:50:43 +04:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
def config(self, section, name, default=None, untrusted=False):
|
2009-04-27 01:50:44 +04:00
|
|
|
value = self._data(untrusted).get(section, name, default)
|
2009-04-24 00:40:10 +04:00
|
|
|
if self.debugflag and not untrusted:
|
2009-04-24 00:40:10 +04:00
|
|
|
uvalue = self.ucdata.get(section, name)
|
2006-10-26 21:25:45 +04:00
|
|
|
if uvalue is not None and uvalue != value:
|
|
|
|
self.warn(_("Ignoring untrusted configuration option "
|
|
|
|
"%s.%s = %s\n") % (section, name, uvalue))
|
|
|
|
return value
|
|
|
|
|
|
|
|
def configbool(self, section, name, default=False, untrusted=False):
|
2009-04-24 00:40:10 +04:00
|
|
|
v = self.config(section, name, None, untrusted)
|
|
|
|
if v == None:
|
|
|
|
return default
|
|
|
|
if v.lower() not in _booleans:
|
|
|
|
raise error.ConfigError(_("%s.%s not a boolean ('%s')")
|
|
|
|
% (section, name, v))
|
|
|
|
return _booleans[v.lower()]
|
2006-10-26 21:25:45 +04:00
|
|
|
|
|
|
|
def configlist(self, section, name, default=None, untrusted=False):
|
2006-06-26 16:52:26 +04:00
|
|
|
"""Return a list of comma/space separated strings"""
|
2006-10-26 21:25:45 +04:00
|
|
|
result = self.config(section, name, untrusted=untrusted)
|
2006-06-26 16:52:26 +04:00
|
|
|
if result is None:
|
2006-06-27 00:44:48 +04:00
|
|
|
result = default or []
|
|
|
|
if isinstance(result, basestring):
|
|
|
|
result = result.replace(",", " ").split()
|
|
|
|
return result
|
2006-06-26 16:52:26 +04:00
|
|
|
|
2007-05-28 00:50:59 +04:00
|
|
|
def has_section(self, section, untrusted=False):
|
2006-05-24 01:57:45 +04:00
|
|
|
'''tell whether section exists in config.'''
|
2009-04-27 01:50:44 +04:00
|
|
|
return section in self._data(untrusted)
|
2006-10-26 21:25:45 +04:00
|
|
|
|
|
|
|
def configitems(self, section, untrusted=False):
|
2009-04-27 01:50:44 +04:00
|
|
|
items = self._data(untrusted).items(section)
|
2009-04-24 00:40:10 +04:00
|
|
|
if self.debugflag and not untrusted:
|
2009-04-24 00:40:10 +04:00
|
|
|
for k,v in self.ucdata.items(section):
|
|
|
|
if self.cdata.get(section, k) != v:
|
2006-10-26 21:25:45 +04:00
|
|
|
self.warn(_("Ignoring untrusted configuration option "
|
2009-04-24 00:40:10 +04:00
|
|
|
"%s.%s = %s\n") % (section, k, v))
|
|
|
|
return items
|
2006-03-06 19:47:41 +03:00
|
|
|
|
2006-10-26 21:25:45 +04:00
|
|
|
def walkconfig(self, untrusted=False):
|
2009-04-27 01:50:44 +04:00
|
|
|
cdata = self._data(untrusted)
|
2009-04-24 00:40:10 +04:00
|
|
|
for section in cdata.sections():
|
2006-10-26 21:25:45 +04:00
|
|
|
for name, value in self.configitems(section, untrusted):
|
2007-02-15 15:15:08 +03:00
|
|
|
yield section, name, str(value).replace('\n', '\\n')
|
2005-08-24 08:30:12 +04:00
|
|
|
|
2005-07-04 09:51:09 +04:00
|
|
|
def username(self):
|
2006-03-21 17:33:29 +03:00
|
|
|
"""Return default username to be used in commits.
|
|
|
|
|
|
|
|
Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
|
|
|
|
and stop searching if one of these is set.
|
2008-08-06 17:10:05 +04:00
|
|
|
If not found and ui.askusername is True, ask the user, else use
|
|
|
|
($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname".
|
2006-03-21 17:33:29 +03:00
|
|
|
"""
|
|
|
|
user = os.environ.get("HGUSER")
|
|
|
|
if user is None:
|
|
|
|
user = self.config("ui", "username")
|
|
|
|
if user is None:
|
|
|
|
user = os.environ.get("EMAIL")
|
2008-08-06 17:10:05 +04:00
|
|
|
if user is None and self.configbool("ui", "askusername"):
|
2009-01-03 19:15:21 +03:00
|
|
|
user = self.prompt(_("enter a commit username:"), default=None)
|
2007-01-25 01:04:51 +03:00
|
|
|
if user is None:
|
2006-11-28 23:16:05 +03:00
|
|
|
try:
|
|
|
|
user = '%s@%s' % (util.getuser(), socket.getfqdn())
|
2007-01-25 01:04:51 +03:00
|
|
|
self.warn(_("No username found, using '%s' instead\n") % user)
|
2006-11-28 23:16:05 +03:00
|
|
|
except KeyError:
|
2007-01-25 01:04:51 +03:00
|
|
|
pass
|
|
|
|
if not user:
|
|
|
|
raise util.Abort(_("Please specify a username."))
|
2008-03-22 21:50:59 +03:00
|
|
|
if "\n" in user:
|
2008-12-06 16:27:31 +03:00
|
|
|
raise util.Abort(_("username %s contains a newline\n") % repr(user))
|
2006-03-21 17:33:29 +03:00
|
|
|
return user
|
2005-07-04 09:51:09 +04:00
|
|
|
|
2005-08-28 19:29:28 +04:00
|
|
|
def shortuser(self, user):
|
|
|
|
"""Return a short representation of a user name or email address."""
|
2006-02-27 22:32:10 +03:00
|
|
|
if not self.verbose: user = util.shortuser(user)
|
2005-08-28 19:29:28 +04:00
|
|
|
return user
|
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
def _path(self, loc):
|
|
|
|
p = self.config('paths', loc)
|
|
|
|
if p and '%%' in p:
|
|
|
|
ui.warn('(deprecated \'\%\%\' in path %s=%s from %s)\n' %
|
|
|
|
(loc, p, self.configsource('paths', loc)))
|
2009-04-27 01:50:43 +04:00
|
|
|
p = p.replace('%%', '%')
|
2009-04-27 01:50:43 +04:00
|
|
|
return p
|
|
|
|
|
2006-06-24 05:09:44 +04:00
|
|
|
def expandpath(self, loc, default=None):
|
2006-03-12 20:05:13 +03:00
|
|
|
"""Return repository location relative to cwd or from [paths]"""
|
2007-03-15 00:38:40 +03:00
|
|
|
if "://" in loc or os.path.isdir(os.path.join(loc, '.hg')):
|
2006-03-12 20:05:13 +03:00
|
|
|
return loc
|
2005-06-28 14:30:36 +04:00
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
path = self._path(loc)
|
2006-06-24 05:23:32 +04:00
|
|
|
if not path and default is not None:
|
2009-04-27 01:50:43 +04:00
|
|
|
path = self._path(default)
|
2006-06-26 16:49:18 +04:00
|
|
|
return path or loc
|
2005-06-28 14:30:36 +04:00
|
|
|
|
2006-12-01 10:28:18 +03:00
|
|
|
def pushbuffer(self):
|
|
|
|
self.buffers.append([])
|
|
|
|
|
|
|
|
def popbuffer(self):
|
|
|
|
return "".join(self.buffers.pop())
|
|
|
|
|
2005-06-01 10:57:53 +04:00
|
|
|
def write(self, *args):
|
2006-12-01 10:28:18 +03:00
|
|
|
if self.buffers:
|
|
|
|
self.buffers[-1].extend([str(a) for a in args])
|
|
|
|
else:
|
|
|
|
for a in args:
|
|
|
|
sys.stdout.write(str(a))
|
2005-07-02 02:18:02 +04:00
|
|
|
|
|
|
|
def write_err(self, *args):
|
2006-03-24 01:16:41 +03:00
|
|
|
try:
|
|
|
|
if not sys.stdout.closed: sys.stdout.flush()
|
|
|
|
for a in args:
|
|
|
|
sys.stderr.write(str(a))
|
2007-01-10 22:35:27 +03:00
|
|
|
# stderr may be buffered under win32 when redirected to files,
|
|
|
|
# including stdout.
|
|
|
|
if not sys.stderr.closed: sys.stderr.flush()
|
2006-03-24 01:16:41 +03:00
|
|
|
except IOError, inst:
|
|
|
|
if inst.errno != errno.EPIPE:
|
|
|
|
raise
|
2005-07-02 02:18:02 +04:00
|
|
|
|
2006-03-06 19:35:15 +03:00
|
|
|
def flush(self):
|
2006-03-28 21:24:29 +04:00
|
|
|
try: sys.stdout.flush()
|
|
|
|
except: pass
|
|
|
|
try: sys.stderr.flush()
|
|
|
|
except: pass
|
2006-03-06 19:35:15 +03:00
|
|
|
|
2007-09-25 21:05:34 +04:00
|
|
|
def _readline(self, prompt=''):
|
2007-08-01 03:28:05 +04:00
|
|
|
if self.isatty():
|
|
|
|
try:
|
|
|
|
# magically add command line editing support, where
|
|
|
|
# available
|
|
|
|
import readline
|
|
|
|
# force demandimport to really load the module
|
|
|
|
readline.read_history_file
|
2008-12-10 04:01:14 +03:00
|
|
|
# windows sometimes raises something other than ImportError
|
|
|
|
except Exception:
|
2007-08-01 03:28:05 +04:00
|
|
|
pass
|
2007-12-04 02:28:26 +03:00
|
|
|
line = raw_input(prompt)
|
|
|
|
# When stdin is in binary mode on Windows, it can cause
|
|
|
|
# raw_input() to emit an extra trailing carriage return
|
|
|
|
if os.linesep == '\r\n' and line and line[-1] == '\r':
|
|
|
|
line = line[:-1]
|
|
|
|
return line
|
2007-08-01 03:28:05 +04:00
|
|
|
|
2007-12-28 09:03:55 +03:00
|
|
|
def prompt(self, msg, pat=None, default="y"):
|
|
|
|
"""Prompt user with msg, read response, and ensure it matches pat
|
|
|
|
|
|
|
|
If not interactive -- the default is returned
|
|
|
|
"""
|
2008-11-07 12:27:39 +03:00
|
|
|
if not self.interactive:
|
|
|
|
self.note(msg, ' ', default, "\n")
|
|
|
|
return default
|
2007-12-27 12:32:33 +03:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
r = self._readline(msg + ' ')
|
2007-12-28 07:49:01 +03:00
|
|
|
if not r:
|
|
|
|
return default
|
2007-12-28 09:03:55 +03:00
|
|
|
if not pat or re.match(pat, r):
|
2007-12-27 12:32:33 +03:00
|
|
|
return r
|
|
|
|
else:
|
|
|
|
self.write(_("unrecognized response\n"))
|
|
|
|
except EOFError:
|
|
|
|
raise util.Abort(_('response expected'))
|
2007-08-01 03:28:05 +04:00
|
|
|
|
2006-05-15 04:37:17 +04:00
|
|
|
def getpass(self, prompt=None, default=None):
|
|
|
|
if not self.interactive: return default
|
2009-02-22 02:46:06 +03:00
|
|
|
try:
|
|
|
|
return getpass.getpass(prompt or _('password: '))
|
|
|
|
except EOFError:
|
|
|
|
raise util.Abort(_('response expected'))
|
2005-06-01 10:57:53 +04:00
|
|
|
def status(self, *msg):
|
|
|
|
if not self.quiet: self.write(*msg)
|
2005-06-02 10:06:29 +04:00
|
|
|
def warn(self, *msg):
|
2005-07-02 02:18:02 +04:00
|
|
|
self.write_err(*msg)
|
2005-06-01 10:57:53 +04:00
|
|
|
def note(self, *msg):
|
|
|
|
if self.verbose: self.write(*msg)
|
|
|
|
def debug(self, *msg):
|
|
|
|
if self.debugflag: self.write(*msg)
|
2006-03-21 14:45:27 +03:00
|
|
|
def edit(self, text, user):
|
2006-05-05 02:42:14 +04:00
|
|
|
(fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt",
|
|
|
|
text=True)
|
2006-03-21 14:54:32 +03:00
|
|
|
try:
|
|
|
|
f = os.fdopen(fd, "w")
|
|
|
|
f.write(text)
|
|
|
|
f.close()
|
|
|
|
|
2007-12-05 14:40:01 +03:00
|
|
|
editor = self.geteditor()
|
2006-03-21 14:54:32 +03:00
|
|
|
|
|
|
|
util.system("%s \"%s\"" % (editor, name),
|
|
|
|
environ={'HGUSER': user},
|
|
|
|
onerr=util.Abort, errprefix=_("edit failed"))
|
|
|
|
|
|
|
|
f = open(name)
|
|
|
|
t = f.read()
|
|
|
|
f.close()
|
|
|
|
t = re.sub("(?m)^HG:.*\n", "", t)
|
|
|
|
finally:
|
|
|
|
os.unlink(name)
|
2005-07-11 04:00:17 +04:00
|
|
|
|
2005-06-01 10:57:53 +04:00
|
|
|
return t
|
2006-05-04 23:23:01 +04:00
|
|
|
|
2006-05-22 19:47:53 +04:00
|
|
|
def print_exc(self):
|
|
|
|
'''print exception traceback if traceback printing enabled.
|
|
|
|
only to call in exception handler. returns true if traceback
|
|
|
|
printed.'''
|
|
|
|
if self.traceback:
|
|
|
|
traceback.print_exc()
|
|
|
|
return self.traceback
|
2007-12-05 14:40:01 +03:00
|
|
|
|
|
|
|
def geteditor(self):
|
|
|
|
'''return editor to use'''
|
|
|
|
return (os.environ.get("HGEDITOR") or
|
|
|
|
self.config("ui", "editor") or
|
|
|
|
os.environ.get("VISUAL") or
|
|
|
|
os.environ.get("EDITOR", "vi"))
|