##// END OF EJS Templates
util.system: Use subprocess instead of os.system...
Mads Kiilerich -
r9517:4368f582 default
parent child Browse files
Show More
@@ -165,17 +165,11 b' def testpid(pid):'
165 return inst.errno != errno.ESRCH
165 return inst.errno != errno.ESRCH
166
166
167 def explain_exit(code):
167 def explain_exit(code):
168 """return a 2-tuple (desc, code) describing a process's status"""
168 """return a 2-tuple (desc, code) describing a subprocess status
169 if os.WIFEXITED(code):
169 (codes from kill are negative - not os.system/wait encoding)"""
170 val = os.WEXITSTATUS(code)
170 if code >= 0:
171 return _("exited with status %d") % val, val
171 return _("exited with status %d") % code, code
172 elif os.WIFSIGNALED(code):
172 return _("killed by signal %d") % -code, -code
173 val = os.WTERMSIG(code)
174 return _("killed by signal %d") % val, val
175 elif os.WIFSTOPPED(code):
176 val = os.WSTOPSIG(code)
177 return _("stopped by signal %d") % val, val
178 raise ValueError(_("invalid exit code"))
179
173
180 def isowner(st):
174 def isowner(st):
181 """Return True if the stat object st is from the current user."""
175 """Return True if the stat object st is from the current user."""
@@ -357,41 +357,26 b' def system(cmd, environ={}, cwd=None, on'
357 if val is True:
357 if val is True:
358 return '1'
358 return '1'
359 return str(val)
359 return str(val)
360 oldenv = {}
361 for k in environ:
362 oldenv[k] = os.environ.get(k)
363 if cwd is not None:
364 oldcwd = os.getcwd()
365 origcmd = cmd
360 origcmd = cmd
366 if os.name == 'nt':
361 if os.name == 'nt':
367 cmd = '"%s"' % cmd
362 cmd = '"%s"' % cmd
368 try:
363 env = dict(os.environ)
369 for k, v in environ.iteritems():
364 env.update((k, py2shell(v)) for k, v in environ.iteritems())
370 os.environ[k] = py2shell(v)
365 env['HG'] = hgexecutable()
371 os.environ['HG'] = hgexecutable()
366 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
372 if cwd is not None and oldcwd != cwd:
367 env=env, cwd=cwd)
373 os.chdir(cwd)
368 if sys.platform == 'OpenVMS' and rc & 1:
374 rc = os.system(cmd)
369 rc = 0
375 if sys.platform == 'OpenVMS' and rc & 1:
370 if rc and onerr:
376 rc = 0
371 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
377 if rc and onerr:
372 explain_exit(rc)[0])
378 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
373 if errprefix:
379 explain_exit(rc)[0])
374 errmsg = '%s: %s' % (errprefix, errmsg)
380 if errprefix:
375 try:
381 errmsg = '%s: %s' % (errprefix, errmsg)
376 onerr.warn(errmsg + '\n')
382 try:
377 except AttributeError:
383 onerr.warn(errmsg + '\n')
378 raise onerr(errmsg)
384 except AttributeError:
379 return rc
385 raise onerr(errmsg)
386 return rc
387 finally:
388 for k, v in oldenv.iteritems():
389 if v is None:
390 del os.environ[k]
391 else:
392 os.environ[k] = v
393 if cwd is not None and oldcwd != cwd:
394 os.chdir(oldcwd)
395
380
396 def checksignature(func):
381 def checksignature(func):
397 '''wrap a function with code to check for calling errors'''
382 '''wrap a function with code to check for calling errors'''
General Comments 0
You need to be logged in to leave comments. Login now