Skip to content

Commit b3daa7f

Browse files
committed
Improve central command invocation function
1 parent 348fc4f commit b3daa7f

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ in progress
99
- Increase resources assigned to Vagrant environment.
1010
Now, it gets 4 VCPUs and 4096 MB RAM.
1111
- Improve platform guards and naming things
12+
- Improve central command invocation function
1213

1314

1415
2022-05-20 0.2.0

postroj/util.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,31 @@ def is_dir_empty(path: Path, missing_ok=False):
4343
return next(scan, None) is None
4444

4545

46-
def cmd(command, check: bool = True, passthrough: bool = True, capture: bool = False, use_pty: bool = False):
46+
def cmd(command, check: bool = True, passthrough: bool = None, capture: bool = False, use_stderr: bool = False, use_pty: bool = False, cwd: Union[Path, str] = None, **kwargs):
4747
"""
4848
Run command in a separate process.
4949
"""
5050
try:
51-
if use_pty:
52-
p = subprocess.run(shlex.split(command))
51+
command_encoded = shlex.split(command)
52+
kwargs.update({
53+
"cwd": cwd,
54+
})
55+
if use_stderr:
56+
kwargs["stdout"] = sys.stderr
57+
kwargs["stderr"] = sys.stderr
58+
59+
if passthrough in [True, False] or capture and not use_pty:
60+
logger.debug(f"Running with subprocess_tee: {command}")
61+
p = subprocess_tee.run(command_encoded, tee=passthrough, **kwargs)
5362
else:
54-
p = subprocess_tee.run(shlex.split(command), tee=passthrough)
63+
logger.debug(f"Running with subprocess: {command} {kwargs}")
64+
p = subprocess.run(command_encoded, **kwargs)
65+
"""
66+
if (not passthrough and not capture) or use_pty:
67+
p = subprocess.run(command, **kwargs)
68+
else:
69+
p = subprocess_tee.run(command, tee=passthrough, **kwargs)
70+
"""
5571
"""
5672
if capture:
5773
p = subprocess_tee.run(shlex.split(command), tee=passthrough)
@@ -107,7 +123,9 @@ def fix_tty():
107123
if not sys.stdin.isatty():
108124
return
109125

110-
os.system("stty sane")
126+
if sys.stdout.isatty():
127+
os.system("stty sane")
128+
os.system("tput cnorm")
111129

112130
# Clears the whole screen.
113131
# os.system("tput reset")
@@ -158,6 +176,8 @@ def port_is_up(host: str, port: int):
158176
https://github.com/lovelysystems/lovely.testlayers/blob/0.7.0/src/lovely/testlayers/util.py#L6-L13
159177
"""
160178
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
179+
# TODO: Make connect timeout configurable.
180+
s.settimeout(5)
161181
ex = s.connect_ex((host, port))
162182
if ex == 0:
163183
s.close()

0 commit comments

Comments
 (0)