diff --git a/pydu/cmd.py b/pydu/cmd.py index de2f0d0..fccc147 100644 --- a/pydu/cmd.py +++ b/pydu/cmd.py @@ -6,7 +6,14 @@ from .platform import WINDOWS 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) if not wait: return p @@ -23,7 +30,7 @@ def run(cmd, wait=True, shell=True, timeout=0, timeinterval=1): 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 be in English. diff --git a/tests/test_cmd.py b/tests/test_cmd.py index 73c4150..14086d1 100644 --- a/tests/test_cmd.py +++ b/tests/test_cmd.py @@ -7,25 +7,25 @@ from pydu.cmd import run, run_with_en_env, cmdline_argv def test_run(): - retcode, output = run('echo hello') + retcode, output = run('echo hello', shell=True) assert retcode == 0 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 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 'timeout' in output def test_run_with_en_env(): - _, output = run_with_en_env('nocmd') - assert output.decode('ascii') + _, output = run_with_en_env('nocmd', shell=True) + output.decode('ascii') - _, output = run_with_en_env(['nocmd']) - assert output.decode('ascii') + _, output = run_with_en_env(['nocmd'], shell=True) + output.decode('ascii') def test_cmdline_argv():