add doc for cmd.run; run and run_with_en_env's parameter `shell` default to be False.

This commit is contained in:
Prodesire 2017-12-23 10:27:08 +08:00
parent 11e34cc573
commit 925e93aecb
2 changed files with 16 additions and 9 deletions

View File

@ -6,7 +6,14 @@ from .platform import WINDOWS
from .compat import PY2 from .compat import PY2
def run(cmd, wait=True, shell=True, timeout=0, timeinterval=1): def run(cmd, wait=True, shell=False, timeout=0, timeinterval=1):
"""
Run cmd.
If `wait` is True, run cmd util finish. Default to be True.
If `shell` is True, run cmd in shell. Default to be False.
if `timeout` is bigger than 0, run cmd with timeout. Default to be 0, means not timeout.
`timeinterval` comes with `timeout`, it means process status checking interval.
"""
p = Popen(cmd, shell=shell, stdout=PIPE, stderr=STDOUT) p = Popen(cmd, shell=shell, stdout=PIPE, stderr=STDOUT)
if not wait: if not wait:
return p return p
@ -23,7 +30,7 @@ def run(cmd, wait=True, shell=True, timeout=0, timeinterval=1):
return p.poll(), stdout return p.poll(), stdout
def run_with_en_env(cmd, wait=True, shell=True, timeout=0, timeinterval=1): def run_with_en_env(cmd, wait=True, shell=False, timeout=0, timeinterval=1):
""" """
Run cmd with English character sets environment, so that the output will Run cmd with English character sets environment, so that the output will
be in English. be in English.

View File

@ -7,25 +7,25 @@ from pydu.cmd import run, run_with_en_env, cmdline_argv
def test_run(): def test_run():
retcode, output = run('echo hello') retcode, output = run('echo hello', shell=True)
assert retcode == 0 assert retcode == 0
assert safeunicode(output).rstrip('\r\n') == 'hello' assert safeunicode(output).rstrip('\r\n') == 'hello'
p = run('echo hello', wait=False) p = run('echo hello', wait=False, shell=True)
assert p.wait() == 0 assert p.wait() == 0
retcode, output = run('{} -c "import time; time.sleep(1)"'.format(sys.executable), retcode, output = run('{} -c "import time; time.sleep(1)"'.format(sys.executable),
timeout=0.2, timeinterval=0.05) shell=True, timeout=0.2, timeinterval=0.05)
assert retcode != 0 assert retcode != 0
assert 'timeout' in output assert 'timeout' in output
def test_run_with_en_env(): def test_run_with_en_env():
_, output = run_with_en_env('nocmd') _, output = run_with_en_env('nocmd', shell=True)
assert output.decode('ascii') output.decode('ascii')
_, output = run_with_en_env(['nocmd']) _, output = run_with_en_env(['nocmd'], shell=True)
assert output.decode('ascii') output.decode('ascii')
def test_cmdline_argv(): def test_cmdline_argv():