364 def set_hgexecutable(path): |
364 def set_hgexecutable(path): |
365 """set location of the 'hg' executable""" |
365 """set location of the 'hg' executable""" |
366 global _hgexecutable |
366 global _hgexecutable |
367 _hgexecutable = path |
367 _hgexecutable = path |
368 |
368 |
369 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None): |
369 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None, out=None): |
370 '''enhanced shell command execution. |
370 '''enhanced shell command execution. |
371 run with environment maybe modified, maybe in different dir. |
371 run with environment maybe modified, maybe in different dir. |
372 |
372 |
373 if command fails and onerr is None, return status. if ui object, |
373 if command fails and onerr is None, return status. if ui object, |
374 print error message and return status, else raise onerr object as |
374 print error message and return status, else raise onerr object as |
375 exception.''' |
375 exception. |
|
376 |
|
377 if out is specified, it is assumed to be a file-like object that has a |
|
378 write() method. stdout and stderr will be redirected to out.''' |
376 def py2shell(val): |
379 def py2shell(val): |
377 'convert python object into string that is useful to shell' |
380 'convert python object into string that is useful to shell' |
378 if val is None or val is False: |
381 if val is None or val is False: |
379 return '0' |
382 return '0' |
380 if val is True: |
383 if val is True: |
384 if os.name == 'nt': |
387 if os.name == 'nt': |
385 cmd = '"%s"' % cmd |
388 cmd = '"%s"' % cmd |
386 env = dict(os.environ) |
389 env = dict(os.environ) |
387 env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
390 env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
388 env['HG'] = hgexecutable() |
391 env['HG'] = hgexecutable() |
389 rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
392 if out is None: |
390 env=env, cwd=cwd) |
393 rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
|
394 env=env, cwd=cwd) |
|
395 else: |
|
396 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
|
397 env=env, cwd=cwd, stdout=subprocess.PIPE, |
|
398 stderr=subprocess.STDOUT) |
|
399 for line in proc.stdout: |
|
400 out.write(line) |
|
401 proc.wait() |
|
402 rc = proc.returncode |
391 if sys.platform == 'OpenVMS' and rc & 1: |
403 if sys.platform == 'OpenVMS' and rc & 1: |
392 rc = 0 |
404 rc = 0 |
393 if rc and onerr: |
405 if rc and onerr: |
394 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), |
406 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), |
395 explain_exit(rc)[0]) |
407 explain_exit(rc)[0]) |