comparison mercurial/util.py @ 31108:3f8f53190d6a

chg: deduplicate error handling of ui.system() This moves 'onerr' handling from low-level util.system() to higher level, which seems better API separation.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 19 Feb 2017 01:16:45 +0900
parents 2912b06905dc
children b4cd912d7704
comparison
equal deleted inserted replaced
31107:fbce78c58f1e 31108:3f8f53190d6a
1007 if environ: 1007 if environ:
1008 env.update((k, py2shell(v)) for k, v in environ.iteritems()) 1008 env.update((k, py2shell(v)) for k, v in environ.iteritems())
1009 env['HG'] = hgexecutable() 1009 env['HG'] = hgexecutable()
1010 return env 1010 return env
1011 1011
1012 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None): 1012 def system(cmd, environ=None, cwd=None, out=None):
1013 '''enhanced shell command execution. 1013 '''enhanced shell command execution.
1014 run with environment maybe modified, maybe in different dir. 1014 run with environment maybe modified, maybe in different dir.
1015
1016 if command fails and onerr is None, return status, else raise onerr
1017 object as exception.
1018 1015
1019 if out is specified, it is assumed to be a file-like object that has a 1016 if out is specified, it is assumed to be a file-like object that has a
1020 write() method. stdout and stderr will be redirected to out.''' 1017 write() method. stdout and stderr will be redirected to out.'''
1021 try: 1018 try:
1022 stdout.flush() 1019 stdout.flush()
1023 except Exception: 1020 except Exception:
1024 pass 1021 pass
1025 origcmd = cmd
1026 cmd = quotecommand(cmd) 1022 cmd = quotecommand(cmd)
1027 if pycompat.sysplatform == 'plan9' and (sys.version_info[0] == 2 1023 if pycompat.sysplatform == 'plan9' and (sys.version_info[0] == 2
1028 and sys.version_info[1] < 7): 1024 and sys.version_info[1] < 7):
1029 # subprocess kludge to work around issues in half-baked Python 1025 # subprocess kludge to work around issues in half-baked Python
1030 # ports, notably bichued/python: 1026 # ports, notably bichued/python:
1044 out.write(line) 1040 out.write(line)
1045 proc.wait() 1041 proc.wait()
1046 rc = proc.returncode 1042 rc = proc.returncode
1047 if pycompat.sysplatform == 'OpenVMS' and rc & 1: 1043 if pycompat.sysplatform == 'OpenVMS' and rc & 1:
1048 rc = 0 1044 rc = 0
1049 if rc and onerr:
1050 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
1051 explainexit(rc)[0])
1052 if errprefix:
1053 errmsg = '%s: %s' % (errprefix, errmsg)
1054 raise onerr(errmsg)
1055 return rc 1045 return rc
1056 1046
1057 def checksignature(func): 1047 def checksignature(func):
1058 '''wrap a function with code to check for calling errors''' 1048 '''wrap a function with code to check for calling errors'''
1059 def check(*args, **kwargs): 1049 def check(*args, **kwargs):