Skip to content
Permalink
Browse files
Add with .. to all subprocesses
  • Loading branch information
Médéric RIBREUX authored and m-kuhn committed Nov 28, 2016
1 parent c3ad30d commit c147ee2bf3fbbb0c318f9aa37a4bc8f08c178ffa
Showing with 52 additions and 51 deletions.
  1. +6 −6 python/plugins/processing/algs/gdal/GdalUtils.py
  2. +12 −12 python/plugins/processing/algs/otb/OTBUtils.py
  3. +34 −33 python/plugins/processing/algs/saga/SagaUtils.py
@@ -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
@@ -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)
@@ -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

@@ -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.