Skip to content

Commit

Permalink
Use the dflow run_command (#115)
Browse files Browse the repository at this point in the history
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>
  • Loading branch information
wanghan-iapcm and Han Wang committed Jan 23, 2023
1 parent fe0fa83 commit 21bad63
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
20 changes: 7 additions & 13 deletions dpgen2/utils/run_command.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import sys, subprocess
from dflow.utils import run_command as dflow_run_command
from typing import Tuple, Union, List

def run_command(
cmd,
cmd : Union[str, List[str]],
shell: bool = False,
):
pp = subprocess.Popen(
) -> Tuple[int, str, str]:
return dflow_run_command(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=shell,
raise_error=False,
try_bash=shell,
)
out, err = pp.communicate()
return_code = pp.poll()
out = out.decode(sys.stdin.encoding)
err = err.decode(sys.stdin.encoding)
return return_code, out, err

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [
dependencies = [
'numpy',
'dpdata',
'pydflow>=1.6.23',
'pydflow>=1.6.30',
'dargs>=0.3.1',
'scipy',
'lbg',
Expand Down
8 changes: 5 additions & 3 deletions tests/utils/test_run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_success_shell(self):
ret, out, err = run_command(['ls | sort'], shell=True)
self.assertEqual(ret, 0)
self.assertEqual(out, 'bar\nfoo\n')
self.assertEqual(err, '')
# ignore the warnings
# self.assertEqual(err, '')
os.chdir('..')

def test_success(self):
Expand All @@ -42,7 +43,8 @@ def test_success_foo(self):
def test_failed(self):
os.chdir(self.work_path)
ret, out, err = run_command(['ls', 'tar'])
self.assertEqual(ret, 2)
self.assertNotEqual(ret, 0)
self.assertEqual(out, '')
self.assertEqual(err, "ls: cannot access 'tar': No such file or directory\n")
# self.assertEqual(err, "ls: cannot access 'tar': No such file or directory\n")
self.assertNotEqual(err, '')
os.chdir('..')

0 comments on commit 21bad63

Please sign in to comment.