Skip to content

Commit c147ee2

Browse files
Médéric RIBREUXm-kuhn
Médéric RIBREUX
authored andcommitted
Add with .. to all subprocesses
1 parent c3ad30d commit c147ee2

File tree

3 files changed

+52
-51
lines changed

3 files changed

+52
-51
lines changed

python/plugins/processing/algs/gdal/GdalUtils.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ def runGdal(commands, progress=None):
8484
loglines = []
8585
loglines.append('GDAL execution console output')
8686
try:
87-
proc = subprocess.Popen(
87+
with subprocess.Popen(
8888
fused_command,
8989
shell=True,
9090
stdout=subprocess.PIPE,
9191
stdin=subprocess.DEVNULL,
9292
stderr=subprocess.STDOUT,
9393
universal_newlines=True,
94-
).stdout
95-
for line in proc:
96-
progress.setConsoleInfo(line)
97-
loglines.append(line)
98-
success = True
94+
) as proc:
95+
for line in proc.stdout:
96+
progress.setConsoleInfo(line)
97+
loglines.append(line)
98+
success = True
9999
except IOError as e:
100100
if retry_count < 5:
101101
retry_count += 1

python/plugins/processing/algs/otb/OTBUtils.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,18 @@ def executeOtb(commands, progress, addToLog=True):
161161
loglines.append(tr("OTB execution console output"))
162162
os.putenv('ITK_AUTOLOAD_PATH', otbLibPath())
163163
fused_command = ''.join(['"%s" ' % re.sub(r'^"|"$', '', c) for c in commands])
164-
proc = subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True).stdout
165-
if isMac(): # This trick avoids having an uninterrupted system call exception if OTB is not installed
166-
time.sleep(1)
167-
for line in iter(proc.readline, ""):
168-
if "[*" in line:
169-
idx = line.find("[*")
170-
perc = int(line[idx - 4:idx - 2].strip(" "))
171-
if perc != 0:
172-
progress.setPercentage(perc)
173-
else:
174-
loglines.append(line)
175-
progress.setConsoleInfo(line)
164+
with subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True) as proc:
165+
if isMac(): # This trick avoids having an uninterrupted system call exception if OTB is not installed
166+
time.sleep(1)
167+
for line in iter(proc.stdout.readline, ""):
168+
if "[*" in line:
169+
idx = line.find("[*")
170+
perc = int(line[idx - 4:idx - 2].strip(" "))
171+
if perc != 0:
172+
progress.setPercentage(perc)
173+
else:
174+
loglines.append(line)
175+
progress.setConsoleInfo(line)
176176

177177
if addToLog:
178178
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)

python/plugins/processing/algs/saga/SagaUtils.py

+34-33
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,28 @@ def getSagaInstalledVersion(runSaga=False):
128128
# (python docs advices to use subprocess32 instead of python2.7's subprocess)
129129
commands = ["saga_cmd -v"]
130130
while retries < maxRetries:
131-
proc = subprocess.Popen(
131+
with subprocess.Popen(
132132
commands,
133133
shell=True,
134134
stdout=subprocess.PIPE,
135135
stdin=subprocess.DEVNULL,
136136
stderr=subprocess.STDOUT,
137137
universal_newlines=True,
138-
).stdout
139-
if isMac(): # This trick avoids having an uninterrupted system call exception if SAGA is not installed
140-
time.sleep(1)
141-
try:
142-
lines = proc.readlines()
143-
for line in lines:
144-
if line.startswith("SAGA Version:"):
145-
_installedVersion = line[len("SAGA Version:"):].strip().split(" ")[0]
146-
_installedVersionFound = True
147-
return _installedVersion
148-
return None
149-
except IOError:
150-
retries += 1
151-
except:
152-
return None
138+
) as proc:
139+
if isMac(): # This trick avoids having an uninterrupted system call exception if SAGA is not installed
140+
time.sleep(1)
141+
try:
142+
lines = proc.stdout.readlines()
143+
for line in lines:
144+
if line.startswith("SAGA Version:"):
145+
_installedVersion = line[len("SAGA Version:"):].strip().split(" ")[0]
146+
_installedVersionFound = True
147+
return _installedVersion
148+
return None
149+
except IOError:
150+
retries += 1
151+
except:
152+
return None
153153

154154
return _installedVersion
155155

@@ -163,28 +163,29 @@ def executeSaga(progress):
163163
command = [sagaBatchJobFilename()]
164164
loglines = []
165165
loglines.append(QCoreApplication.translate('SagaUtils', 'SAGA execution console output'))
166-
proc = subprocess.Popen(
166+
with subprocess.Popen(
167167
command,
168168
shell=True,
169169
stdout=subprocess.PIPE,
170170
stdin=subprocess.DEVNULL,
171171
stderr=subprocess.STDOUT,
172172
universal_newlines=True,
173-
).stdout
174-
try:
175-
for line in iter(proc.readline, ''):
176-
if '%' in line:
177-
s = ''.join([x for x in line if x.isdigit()])
178-
try:
179-
progress.setPercentage(int(s))
180-
except:
181-
pass
182-
else:
183-
line = line.strip()
184-
if line != '/' and line != '-' and line != '\\' and line != '|':
185-
loglines.append(line)
186-
progress.setConsoleInfo(line)
187-
except:
188-
pass
173+
) as proc:
174+
try:
175+
for line in iter(proc.stdout.readline, ''):
176+
if '%' in line:
177+
s = ''.join([x for x in line if x.isdigit()])
178+
try:
179+
progress.setPercentage(int(s))
180+
except:
181+
pass
182+
else:
183+
line = line.strip()
184+
if line != '/' and line != '-' and line != '\\' and line != '|':
185+
loglines.append(line)
186+
progress.setConsoleInfo(line)
187+
except:
188+
pass
189+
189190
if ProcessingConfig.getSetting(SAGA_LOG_CONSOLE):
190191
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)

0 commit comments

Comments
 (0)