comparison mercurial/util.py @ 36793:eca1051e6c22

util: add public isstdin/isstdout() functions
author Yuya Nishihara <yuya@tcha.org>
date Tue, 06 Mar 2018 02:28:59 -0600
parents 30742c216abb
children 6bdea0efdab5
comparison
equal deleted inserted replaced
36792:15c050b5d599 36793:eca1051e6c22
1428 def _sethgexecutable(path): 1428 def _sethgexecutable(path):
1429 """set location of the 'hg' executable""" 1429 """set location of the 'hg' executable"""
1430 global _hgexecutable 1430 global _hgexecutable
1431 _hgexecutable = path 1431 _hgexecutable = path
1432 1432
1433 def _isstdout(f): 1433 def _testfileno(f, stdf):
1434 fileno = getattr(f, 'fileno', None) 1434 fileno = getattr(f, 'fileno', None)
1435 try: 1435 try:
1436 return fileno and fileno() == sys.__stdout__.fileno() 1436 return fileno and fileno() == stdf.fileno()
1437 except io.UnsupportedOperation: 1437 except io.UnsupportedOperation:
1438 return False # fileno() raised UnsupportedOperation 1438 return False # fileno() raised UnsupportedOperation
1439
1440 def isstdin(f):
1441 return _testfileno(f, sys.__stdin__)
1442
1443 def isstdout(f):
1444 return _testfileno(f, sys.__stdout__)
1439 1445
1440 def shellenviron(environ=None): 1446 def shellenviron(environ=None):
1441 """return environ with optional override, useful for shelling out""" 1447 """return environ with optional override, useful for shelling out"""
1442 def py2shell(val): 1448 def py2shell(val):
1443 'convert python object into string that is useful to shell' 1449 'convert python object into string that is useful to shell'
1462 stdout.flush() 1468 stdout.flush()
1463 except Exception: 1469 except Exception:
1464 pass 1470 pass
1465 cmd = quotecommand(cmd) 1471 cmd = quotecommand(cmd)
1466 env = shellenviron(environ) 1472 env = shellenviron(environ)
1467 if out is None or _isstdout(out): 1473 if out is None or isstdout(out):
1468 rc = subprocess.call(cmd, shell=True, close_fds=closefds, 1474 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
1469 env=env, cwd=cwd) 1475 env=env, cwd=cwd)
1470 else: 1476 else:
1471 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, 1477 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
1472 env=env, cwd=cwd, stdout=subprocess.PIPE, 1478 env=env, cwd=cwd, stdout=subprocess.PIPE,