Skip to content

Commit f17af3d

Browse files
praiskupFrostyX
authored andcommitted
pylint: wrap Popen() into a context manager
1 parent ccff92d commit f17af3d

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

resallocserver/manager.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,40 +106,38 @@ def _run_command(log, logdir, pool_id, res_id, res_name, id_in_pool, command, lt
106106
stdout_stopped = False
107107

108108
# Run the sub-command to be captured.
109-
sp = subprocess.Popen(command, env=env, shell=True,
110-
stdout=subprocess.PIPE, stderr=logfile)
111109

112-
captured_string = b""
110+
with subprocess.Popen(command, env=env, shell=True,
111+
stdout=subprocess.PIPE, stderr=logfile) as sp:
112+
captured_string = b""
113113

114+
for line in iter(sp.stdout.readline, b''):
115+
# Write to the log.
116+
logfile.write(line)
117+
logfile.flush()
114118

115-
for line in iter(sp.stdout.readline, b''):
116-
# Write to the log.
117-
logfile.write(line)
118-
logfile.flush()
119-
120-
if stdout_stopped:
121-
continue
122-
123-
if stdout_written + len(line) > catch_stdout_bytes:
124-
if stdout_written == 0 and not catch_stdout_lines_securely:
125-
# Even the first line is too long for this buffer. Catch at
126-
# least part of it.
127-
line = line[:catch_stdout_bytes]
128-
captured_string += line
119+
if stdout_stopped:
120+
continue
129121

130-
stdout_stopped = True
131-
if not catch_stdout_lines_securely:
132-
captured_string += b"<< trimmed >>\n"
133-
continue
122+
if stdout_written + len(line) > catch_stdout_bytes:
123+
if stdout_written == 0 and not catch_stdout_lines_securely:
124+
# Even the first line is too long for this buffer. Catch at
125+
# least part of it.
126+
line = line[:catch_stdout_bytes]
127+
captured_string += line
134128

135-
stdout_written += len(line)
136-
captured_string += line
129+
stdout_stopped = True
130+
if not catch_stdout_lines_securely:
131+
captured_string += b"<< trimmed >>\n"
132+
continue
137133

134+
stdout_written += len(line)
135+
captured_string += line
138136

139-
return {
140-
'status': sp.wait(),
141-
'stdout': captured_string,
142-
}
137+
return {
138+
'status': sp.wait(),
139+
'stdout': captured_string,
140+
}
143141

144142

145143
def normalize_tags(tags):

0 commit comments

Comments
 (0)