comparison mercurial/util.py @ 1882:c0320567931f

merge util.esystem and util.system.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Fri, 10 Mar 2006 22:42:59 -0800
parents 05c7d75be925
children b7cc0f323a4c
comparison
equal deleted inserted replaced
1880:05c7d75be925 1882:c0320567931f
313 (not pats and not files) or 313 (not pats and not files) or
314 (pats and patmatch(fn)) or 314 (pats and patmatch(fn)) or
315 (files and filematch(fn)))), 315 (files and filematch(fn)))),
316 (inc or exc or (pats and pats != [('glob', '**')])) and True) 316 (inc or exc or (pats and pats != [('glob', '**')])) and True)
317 317
318 def system(cmd, errprefix=None): 318 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
319 """execute a shell command that must succeed"""
320 rc = os.system(cmd)
321 if rc:
322 errmsg = "%s %s" % (os.path.basename(cmd.split(None, 1)[0]),
323 explain_exit(rc)[0])
324 if errprefix:
325 errmsg = "%s: %s" % (errprefix, errmsg)
326 raise Abort(errmsg)
327
328 def esystem(cmd, environ={}, cwd=None):
329 '''enhanced shell command execution. 319 '''enhanced shell command execution.
330 run with environment maybe modified, maybe in different dir.''' 320 run with environment maybe modified, maybe in different dir.
321
322 if command fails and onerr is None, return status. if ui object,
323 print error message and return status, else raise onerr object as
324 exception.'''
331 oldenv = {} 325 oldenv = {}
332 for k in environ: 326 for k in environ:
333 oldenv[k] = os.environ.get(k) 327 oldenv[k] = os.environ.get(k)
334 if cwd is not None: 328 if cwd is not None:
335 oldcwd = os.getcwd() 329 oldcwd = os.getcwd()
336 try: 330 try:
337 for k, v in environ.iteritems(): 331 for k, v in environ.iteritems():
338 os.environ[k] = str(v) 332 os.environ[k] = str(v)
339 if cwd is not None and oldcwd != cwd: 333 if cwd is not None and oldcwd != cwd:
340 os.chdir(cwd) 334 os.chdir(cwd)
341 return os.system(cmd) 335 rc = os.system(cmd)
336 if rc and onerr:
337 errmsg = '%s %s' % (os.path.basename(cmd.split(None, 1)[0]),
338 explain_exit(rc)[0])
339 if errprefix:
340 errmsg = '%s: %s' % (errprefix, errmsg)
341 try:
342 onerr.warn(errmsg + '\n')
343 except AttributeError:
344 raise onerr(errmsg)
345 return rc
342 finally: 346 finally:
343 for k, v in oldenv.iteritems(): 347 for k, v in oldenv.iteritems():
344 if v is None: 348 if v is None:
345 del os.environ[k] 349 del os.environ[k]
346 else: 350 else: