# HG changeset patch # User Vadim Gelfer # Date 2006-03-11 06:42:59 # Node ID c0320567931f610c4a94032f54407cbdbe3fd163 # Parent 05c7d75be9256c1575d20b9a7f886422877fdb9a merge util.esystem and util.system. diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -55,7 +55,7 @@ class localrepository(object): def runhook(name, cmd): self.ui.note(_("running hook %s: %s\n") % (name, cmd)) env = dict([('HG_' + k.upper(), v) for k, v in args.iteritems()]) - r = util.esystem(cmd, environ=env, cwd=self.root) + r = util.system(cmd, environ=env, cwd=self.root) if r: desc, r = util.explain_exit(r) if throw: diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -209,7 +209,9 @@ class ui(object): os.environ.get("EDITOR", "vi")) os.environ["HGUSER"] = self.username() - util.system("%s \"%s\"" % (editor, name), errprefix=_("edit failed")) + util.system("%s \"%s\"" % (editor, name), + environ={'HGUSER': self.username()}, + onerr=util.Abort, errprefix=_("edit failed")) t = open(name).read() t = re.sub("(?m)^HG:.*\n", "", t) diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -315,19 +315,13 @@ def _matcher(canonroot, cwd, names, inc, (files and filematch(fn)))), (inc or exc or (pats and pats != [('glob', '**')])) and True) -def system(cmd, errprefix=None): - """execute a shell command that must succeed""" - rc = os.system(cmd) - if rc: - errmsg = "%s %s" % (os.path.basename(cmd.split(None, 1)[0]), - explain_exit(rc)[0]) - if errprefix: - errmsg = "%s: %s" % (errprefix, errmsg) - raise Abort(errmsg) +def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None): + '''enhanced shell command execution. + run with environment maybe modified, maybe in different dir. -def esystem(cmd, environ={}, cwd=None): - '''enhanced shell command execution. - run with environment maybe modified, maybe in different dir.''' + if command fails and onerr is None, return status. if ui object, + print error message and return status, else raise onerr object as + exception.''' oldenv = {} for k in environ: oldenv[k] = os.environ.get(k) @@ -338,7 +332,17 @@ def esystem(cmd, environ={}, cwd=None): os.environ[k] = str(v) if cwd is not None and oldcwd != cwd: os.chdir(cwd) - return os.system(cmd) + rc = os.system(cmd) + if rc and onerr: + errmsg = '%s %s' % (os.path.basename(cmd.split(None, 1)[0]), + explain_exit(rc)[0]) + if errprefix: + errmsg = '%s: %s' % (errprefix, errmsg) + try: + onerr.warn(errmsg + '\n') + except AttributeError: + raise onerr(errmsg) + return rc finally: for k, v in oldenv.iteritems(): if v is None: