Skip to content

Commit

Permalink
Add with .. to all subprocesses
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric RIBREUX authored and m-kuhn committed Nov 28, 2016
1 parent c3ad30d commit c147ee2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
12 changes: 6 additions & 6 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -84,18 +84,18 @@ def runGdal(commands, progress=None):
loglines = []
loglines.append('GDAL execution console output')
try:
proc = subprocess.Popen(
with subprocess.Popen(
fused_command,
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
universal_newlines=True,
).stdout
for line in proc:
progress.setConsoleInfo(line)
loglines.append(line)
success = True
) as proc:
for line in proc.stdout:
progress.setConsoleInfo(line)
loglines.append(line)
success = True
except IOError as e:
if retry_count < 5:
retry_count += 1
Expand Down
24 changes: 12 additions & 12 deletions python/plugins/processing/algs/otb/OTBUtils.py
Expand Up @@ -161,18 +161,18 @@ def executeOtb(commands, progress, addToLog=True):
loglines.append(tr("OTB execution console output"))
os.putenv('ITK_AUTOLOAD_PATH', otbLibPath())
fused_command = ''.join(['"%s" ' % re.sub(r'^"|"$', '', c) for c in commands])
proc = subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True).stdout
if isMac(): # This trick avoids having an uninterrupted system call exception if OTB is not installed
time.sleep(1)
for line in iter(proc.readline, ""):
if "[*" in line:
idx = line.find("[*")
perc = int(line[idx - 4:idx - 2].strip(" "))
if perc != 0:
progress.setPercentage(perc)
else:
loglines.append(line)
progress.setConsoleInfo(line)
with subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True) as proc:
if isMac(): # This trick avoids having an uninterrupted system call exception if OTB is not installed
time.sleep(1)
for line in iter(proc.stdout.readline, ""):
if "[*" in line:
idx = line.find("[*")
perc = int(line[idx - 4:idx - 2].strip(" "))
if perc != 0:
progress.setPercentage(perc)
else:
loglines.append(line)
progress.setConsoleInfo(line)

if addToLog:
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
Expand Down
67 changes: 34 additions & 33 deletions python/plugins/processing/algs/saga/SagaUtils.py
Expand Up @@ -128,28 +128,28 @@ def getSagaInstalledVersion(runSaga=False):
# (python docs advices to use subprocess32 instead of python2.7's subprocess)
commands = ["saga_cmd -v"]
while retries < maxRetries:
proc = subprocess.Popen(
with subprocess.Popen(
commands,
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
universal_newlines=True,
).stdout
if isMac(): # This trick avoids having an uninterrupted system call exception if SAGA is not installed
time.sleep(1)
try:
lines = proc.readlines()
for line in lines:
if line.startswith("SAGA Version:"):
_installedVersion = line[len("SAGA Version:"):].strip().split(" ")[0]
_installedVersionFound = True
return _installedVersion
return None
except IOError:
retries += 1
except:
return None
) as proc:
if isMac(): # This trick avoids having an uninterrupted system call exception if SAGA is not installed
time.sleep(1)
try:
lines = proc.stdout.readlines()
for line in lines:
if line.startswith("SAGA Version:"):
_installedVersion = line[len("SAGA Version:"):].strip().split(" ")[0]
_installedVersionFound = True
return _installedVersion
return None
except IOError:
retries += 1
except:
return None

return _installedVersion

Expand All @@ -163,28 +163,29 @@ def executeSaga(progress):
command = [sagaBatchJobFilename()]
loglines = []
loglines.append(QCoreApplication.translate('SagaUtils', 'SAGA execution console output'))
proc = subprocess.Popen(
with subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
universal_newlines=True,
).stdout
try:
for line in iter(proc.readline, ''):
if '%' in line:
s = ''.join([x for x in line if x.isdigit()])
try:
progress.setPercentage(int(s))
except:
pass
else:
line = line.strip()
if line != '/' and line != '-' and line != '\\' and line != '|':
loglines.append(line)
progress.setConsoleInfo(line)
except:
pass
) as proc:
try:
for line in iter(proc.stdout.readline, ''):
if '%' in line:
s = ''.join([x for x in line if x.isdigit()])
try:
progress.setPercentage(int(s))
except:
pass
else:
line = line.strip()
if line != '/' and line != '-' and line != '\\' and line != '|':
loglines.append(line)
progress.setConsoleInfo(line)
except:
pass

if ProcessingConfig.getSetting(SAGA_LOG_CONSOLE):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)

0 comments on commit c147ee2

Please sign in to comment.