comparison mercurial/util.py @ 9517:4368f582c806

util.system: Use subprocess instead of os.system subprocess allows the environment and working directory to be specified directly, so the hacks for making temporary changes while forking is no longer necessary. This also fixes failures on solaris where the temporary changes can't be undone because there is no unsetenv.
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 20 Sep 2009 22:19:18 +0200
parents e2fd9b62349b
children b6b0c42739e9
comparison
equal deleted inserted replaced
9516:f8048c334066 9517:4368f582c806
355 if val is None or val is False: 355 if val is None or val is False:
356 return '0' 356 return '0'
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'''
398 def check(*args, **kwargs): 383 def check(*args, **kwargs):
399 try: 384 try: