Skip to content

Commit 6047baf

Browse files
committed
typping for run_process and kafka_utils
1 parent 098929e commit 6047baf

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

installation_and_upgrade/ibex_install_utils/kafka_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
]
1616

1717

18-
def _get_existing_topics(kafka_broker):
18+
def _get_existing_topics(kafka_broker: str) -> set[str]:
1919
return KafkaConsumer(bootstrap_servers=[kafka_broker]).topics()
2020

2121

22-
def add_required_topics(kafka_broker, instrument):
22+
def add_required_topics(kafka_broker: str, instrument: str) -> None:
2323
"""
2424
Adds required Kafka topics for the instrument
2525

installation_and_upgrade/ibex_install_utils/run_process.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import os
66
import subprocess
7-
from typing import List, Union
7+
from typing import IO, List, Optional
88

99
from ibex_install_utils.exceptions import ErrorInRun
1010

@@ -16,38 +16,42 @@ class RunProcess:
1616

1717
def __init__(
1818
self,
19-
working_dir,
20-
executable_file,
21-
executable_directory=None,
22-
press_any_key=False,
23-
prog_args=None,
24-
capture_pipes=True,
25-
std_in=None,
26-
log_command_args=True,
27-
expected_return_codes: Union[int, List[int], None] = [0],
28-
capture_last_output=False,
29-
progress_metric=[],
19+
working_dir: Optional[str | bytes | os.PathLike[str] | os.PathLike[bytes]],
20+
executable_file: str,
21+
executable_directory: Optional[str | bytes | os.PathLike[str] | os.PathLike[bytes]] = None,
22+
press_any_key: bool = False,
23+
prog_args: Optional[List[str]] = None,
24+
capture_pipes: bool = True,
25+
std_in: Optional[int, IO] = None,
26+
log_command_args: bool = True,
27+
expected_return_codes: Optional[int, List[int]] = None,
28+
capture_last_output: bool = False,
29+
progress_metric: Optional[List[str]] = None,
3030
env: dict | None = None,
31-
):
31+
) -> None:
3232
"""
3333
Create a process that needs running
3434
3535
Args:
3636
working_dir: working directory of the process
3737
executable_file: file of the process to run, e.g. a bat file
38-
executable_directory: the directory in which the executable file lives, if None, default, use working dir
38+
executable_directory: the directory in which the executable file lives,
39+
if None, default, use working dir
3940
press_any_key: if true then press a key to finish the run process
4041
prog_args(list[string]): arguments to pass to the program
41-
capture_pipes: whether to capture pipes and manage in python or let the process access the console
42+
capture_pipes: whether to capture pipes and manage in python or let the
43+
process access the console
4244
std_in: std_in sets the stdin pipe to be this
4345
log_command_args (bool): Whether to show the full command line that is being executed.
44-
expected_return_codes (int, None, List[int]): The expected return code for this command. Set it to None to
45-
disable return-code checking.
46-
capture_last_output: Whether to record the last console output of a command while pipes are captured.
47-
progress_metric: A list that is either empty if progress is not being calculated, or contains the output to
46+
expected_return_codes (int, None, List[int]): The expected return code for this command.
47+
Set it to None to disable return-code checking.
48+
capture_last_output: Whether to record the last console output of a command
49+
while pipes are captured.
50+
progress_metric: A list that is either empty if progress is
51+
not being calculated, or contains the output to
4852
count, the 100% value, and optionally a label for printing progress
49-
env: Environment variable mapping to pass to subprocess.POpen. Passing None inherits the parent process'
50-
environment.
53+
env: Environment variable mapping to pass to subprocess.POpen.
54+
Passing None inherits the parent process' environment.
5155
"""
5256
self._working_dir = working_dir
5357
self._bat_file = executable_file
@@ -57,11 +61,13 @@ def __init__(
5761
self._stdin = std_in
5862
self._capture_last_output = capture_last_output
5963
self.captured_output = ""
60-
self._progress_metric = progress_metric
64+
self._progress_metric = progress_metric if progress_metric is not None else []
6165
self._env = env
6266
if isinstance(expected_return_codes, int):
6367
expected_return_codes = [expected_return_codes]
64-
self._expected_return_codes = expected_return_codes
68+
self._expected_return_codes = (
69+
expected_return_codes if expected_return_codes is not None else []
70+
)
6571
self.log_command_args = log_command_args
6672
if std_in is not None and self._capture_pipes:
6773
raise NotImplementedError("Capturing pipes and set standard in is not implemented.")
@@ -70,7 +76,7 @@ def __init__(
7076
else:
7177
self._full_path_to_process_file = os.path.join(executable_directory, executable_file)
7278

73-
def run(self):
79+
def run(self) -> None:
7480
"""
7581
Run the process
7682
@@ -140,37 +146,41 @@ def run(self):
140146
except subprocess.CalledProcessError as ex:
141147
if ex.output:
142148
print(
143-
f"Process failed with return code {ex.returncode} (expected {self._expected_return_codes}). "
149+
f"Process failed with return code {ex.returncode}"
150+
f" (expected {self._expected_return_codes}). "
144151
f"Output was: "
145152
)
146153
for line in ex.output.splitlines():
147154
print(f" > {line}")
148155
print(" --- ")
149156
else:
150157
print(
151-
f"Process failed with return code {ex.returncode} (expected {self._expected_return_codes}) "
158+
f"Process failed with return code {ex.returncode}"
159+
f" (expected {self._expected_return_codes}) "
152160
f"and no output."
153161
)
154162

155163
raise ErrorInRun(
156-
f"Command failed with return code {ex.returncode} (expected {self._expected_return_codes})"
164+
f"Command failed with return code {ex.returncode}"
165+
f" (expected {self._expected_return_codes})"
157166
)
158167
except WindowsError as ex:
159168
if ex.errno == 2:
160169
raise ErrorInRun(f"Command '{self._bat_file}' not found in '{self._working_dir}'")
161170
elif ex.errno == 22:
162171
raise ErrorInRun(
163-
f"Directory not found to run command '{self._bat_file}', command is in : '{self._working_dir}'"
172+
f"Directory not found to run command '{self._bat_file}',"
173+
f" command is in : '{self._working_dir}'"
164174
)
165175
raise ex
166176

167-
def output_no_progress(self, process):
177+
def output_no_progress(self, process: subprocess.Popen) -> None:
168178
for stdout_line in iter(process.stdout.readline, ""):
169179
print(f" > {stdout_line}")
170180
if self._capture_last_output:
171181
self.captured_output = stdout_line
172182

173-
def output_progress(self, process):
183+
def output_progress(self, process: subprocess.Popen) -> None:
174184
count = 0
175185
label = ""
176186
if len(self._progress_metric) > 2:

installation_and_upgrade/ibex_install_utils/tasks/system_tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def update_kafka_topics(self) -> None:
192192
add_required_topics("livedata.isis.cclrc.ac.uk:31092", self._get_instrument_name())
193193

194194
@task("Create virtual environments for python processes")
195-
def create_virtual_envs(self):
195+
def create_virtual_envs(self) -> None:
196196
dirs_with_venvs = []
197197
for root, _, files in os.walk(EPICS_PATH):
198198
for file in files:

0 commit comments

Comments
 (0)