Skip to content

Commit 1be2581

Browse files
committed
use subprocess run instead of popen
1 parent 034a811 commit 1be2581

2 files changed

Lines changed: 20 additions & 24 deletions

File tree

PyStemmusScope/stemmus_scope.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import logging
5-
from pathlib import Path
65
from typing import Tuple, Iterable, Any
76
import subprocess
87
from . import forcing_io
@@ -83,24 +82,18 @@ def run(self) -> Tuple[str, str, str]:
8382

8483
# run the model
8584
args = [f"{self.exe_file} {self.cfg_file}"]
86-
result = subprocess.Popen(
87-
args, preexec_fn=os.setsid, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
85+
result = subprocess.run(
86+
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, check=True,
8887
)
89-
exit_code = result.wait()
90-
stdout, stderr = result.communicate()
88+
stdout = result.stdout
89+
9190
# TODO return log info line by line!
9291
logger.info("%s", stdout)
9392

94-
if exit_code != 0:
95-
raise subprocess.CalledProcessError(
96-
returncode=exit_code, cmd=args, stderr=stderr, output=stdout
97-
)
98-
9993
return stdout
10094

10195

10296
@property
10397
def configs(self) -> Iterable[Tuple[str, Any]]:
10498
"""Return the configurations for this model."""
10599
return self._configs
106-

tests/test_stemmus_scope.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ class TestWithDefaults:
1515
def model(self, tmp_path):
1616
config_file = str(data_folder / "config_file_test.txt")
1717
exe_file = Path(tmp_path) / "STEMUUS_SCOPE"
18+
1819
# create dummy exe file
19-
f = open(exe_file, "x")
20+
with open(exe_file, "x", encoding="utf8") as dummy_file:
21+
dummy_file.close()
22+
2023
yield StemmusScope(config_file, exe_file)
2124

2225
@pytest.fixture
@@ -41,23 +44,23 @@ def test_setup(self, model_with_setup):
4144
# matlab log dir
4245
assert os.environ['MATLAB_LOG_DIR'] == str(model.configs["InputPath"])
4346

44-
@patch("subprocess.Popen")
45-
def test_run(self, mocked_popen, model_with_setup):
47+
@patch("subprocess.run")
48+
def test_run(self, mocked_run, model_with_setup):
4649

4750
actual_cfg_file = f"{data_folder}/directories/input/XX-dummy_2022-07-11-1200_config.txt"
4851
output = (
4952
f"b'Reading config from {actual_cfg_file}\n\n "
5053
"The calculations start now \r\n The calculations end now \r'"
5154
)
52-
mocked_popen.return_value.communicate.return_value = (output, "error")
53-
mocked_popen.return_value.wait.return_value = 0
55+
56+
mocked_run.return_value.stdout = output
5457

5558
model, cfg_file = model_with_setup
5659
result = model.run()
5760

5861
expected = [f"{model.exe_file} {cfg_file}"]
59-
mocked_popen.assert_called_with(
60-
expected, preexec_fn=os.setsid, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
62+
mocked_run.assert_called_with(
63+
expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, check=True,
6164
)
6265

6366
# output of subprocess
@@ -102,22 +105,22 @@ def test_configs(self, model_with_setup):
102105
actual = config_io.read_config(cfg_file)
103106
assert actual == model.configs
104107

105-
@patch("subprocess.Popen")
106-
def test_run(self, mocked_popen, model_with_setup, tmp_path):
108+
@patch("subprocess.run")
109+
def test_run(self, mocked_run, model_with_setup, tmp_path):
107110
actual_cfg_file = f"{tmp_path}/input/dummy_2022-07-11-1200_config.txt"
108111
output = (
109112
f"b'Reading config from {actual_cfg_file}\n\n "
110113
"The calculations start now \r\n The calculations end now \r'"
111114
)
112-
mocked_popen.return_value.communicate.return_value = (output, "error")
113-
mocked_popen.return_value.wait.return_value = 0
115+
116+
mocked_run.return_value.stdout = output
114117

115118
model, cfg_file = model_with_setup
116119
result = model.run()
117120

118121
expected = [f"{model.exe_file} {cfg_file}"]
119-
mocked_popen.assert_called_with(
120-
expected, preexec_fn=os.setsid, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
122+
mocked_run.assert_called_with(
123+
expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, check=True,
121124
)
122125

123126
# output of subprocess

0 commit comments

Comments
 (0)