Skip to content

Commit d2282a7

Browse files
committedAug 19, 2015
processing: when using batch jobs remove GISBASE from environment when calling GRASS (fixes #13072)
1 parent 16d7a06 commit d2282a7

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed
 

‎python/plugins/processing/algs/grass/GrassUtils.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@
3030
import codecs
3131
import subprocess
3232
import os
33-
3433
from qgis.core import QgsApplication
3534
from PyQt4.QtCore import QCoreApplication
3635
from processing.core.ProcessingConfig import ProcessingConfig
3736
from processing.core.ProcessingLog import ProcessingLog
38-
from processing.tools.system import userFolder, isMac, isWindows, mkdir, tempFolder
37+
from processing.tools.system import userFolder, isWindows, isMac, tempFolder, mkdir
3938
from processing.tests.TestData import points
4039

4140

@@ -150,8 +149,7 @@ def createGrassScript(commands):
150149
output.write('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%PATH%\n')
151150
output.write('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%GRASS_ADDON_PATH%;%PATH%\n')
152151
output.write('\n')
153-
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion()
154-
+ '\n')
152+
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion() + '\n')
155153
output.write('if not "%LANG%"=="" goto langset\n')
156154
output.write('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\etc\\winlocale"`) DO @set LANG=%%i\n')
157155
output.write(':langset\n')
@@ -203,12 +201,12 @@ def createTempMapset():
203201
folder = GrassUtils.grassMapsetFolder()
204202
mkdir(os.path.join(folder, 'PERMANENT'))
205203
mkdir(os.path.join(folder, 'PERMANENT', '.tmp'))
206-
GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT',
207-
'DEFAULT_WIND'))
204+
GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT', 'DEFAULT_WIND'))
208205
outfile = codecs.open(os.path.join(folder, 'PERMANENT', 'MYNAME'), 'w', encoding='utf-8')
209206
outfile.write(
210207
'QGIS GRASS interface: temporary data processing location.\n')
211208
outfile.close()
209+
212210
GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT', 'WIND'))
213211
mkdir(os.path.join(folder, 'PERMANENT', 'dbf'))
214212
outfile = codecs.open(os.path.join(folder, 'PERMANENT', 'VAR'), 'w', encoding='utf-8')
@@ -242,14 +240,17 @@ def writeGrassWindow(filename):
242240

243241
@staticmethod
244242
def prepareGrassExecution(commands):
243+
env = os.environ.copy()
244+
245245
if isWindows():
246246
GrassUtils.createGrassScript(commands)
247247
command = ['cmd.exe', '/C ', GrassUtils.grassScriptFilename()]
248248
else:
249249
gisrc = userFolder() + os.sep + 'processing.gisrc'
250-
os.putenv('GISRC', gisrc)
251-
os.putenv('GRASS_MESSAGE_FORMAT', 'gui')
252-
os.putenv('GRASS_BATCH_JOB', GrassUtils.grassBatchJobFilename())
250+
env['GISRC'] = gisrc
251+
env['GRASS_MESSAGE_FORMAT'] = 'gui'
252+
env['GRASS_BATCH_JOB'] = GrassUtils.grassBatchJobFilename()
253+
del env['GISBASE']
253254
GrassUtils.createGrassBatchJobFileFromGrassCommands(commands)
254255
os.chmod(GrassUtils.grassBatchJobFilename(), stat.S_IEXEC
255256
| stat.S_IREAD | stat.S_IWRITE)
@@ -260,28 +261,28 @@ def prepareGrassExecution(commands):
260261
command = 'grass64 ' + GrassUtils.grassMapsetFolder() \
261262
+ '/PERMANENT'
262263

263-
return command
264+
return command, env
264265

265266
@staticmethod
266267
def executeGrass(commands, progress, outputCommands=None):
267268
loglines = []
268269
loglines.append('GRASS execution console output')
269270
grassOutDone = False
270-
command = GrassUtils.prepareGrassExecution(commands)
271+
command, grassenv = GrassUtils.prepareGrassExecution(commands)
271272
proc = subprocess.Popen(
272273
command,
273274
shell=True,
274275
stdout=subprocess.PIPE,
275276
stdin=open(os.devnull),
276277
stderr=subprocess.STDOUT,
277278
universal_newlines=True,
279+
env=grassenv
278280
).stdout
279281
progress.setInfo('GRASS commands output:')
280282
for line in iter(proc.readline, ''):
281283
if 'GRASS_INFO_PERCENT' in line:
282284
try:
283-
progress.setPercentage(int(line[len('GRASS_INFO_PERCENT')
284-
+ 2:]))
285+
progress.setPercentage(int(line[len('GRASS_INFO_PERCENT') + 2:]))
285286
except:
286287
pass
287288
else:
@@ -297,14 +298,15 @@ def executeGrass(commands, progress, outputCommands=None):
297298
# commands again.
298299

299300
if not grassOutDone and outputCommands:
300-
command = GrassUtils.prepareGrassExecution(outputCommands)
301+
command, grassenv = GrassUtils.prepareGrassExecution(outputCommands)
301302
proc = subprocess.Popen(
302303
command,
303304
shell=True,
304305
stdout=subprocess.PIPE,
305306
stdin=open(os.devnull),
306307
stderr=subprocess.STDOUT,
307308
universal_newlines=True,
309+
env=grassenv
308310
).stdout
309311
for line in iter(proc.readline, ''):
310312
if 'GRASS_INFO_PERCENT' in line:
@@ -320,8 +322,6 @@ def executeGrass(commands, progress, outputCommands=None):
320322
if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
321323
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
322324

323-
324-
325325
# GRASS session is used to hold the layers already exported or
326326
# produced in GRASS between multiple calls to GRASS algorithms.
327327
# This way they don't have to be loaded multiple times and
@@ -349,8 +349,9 @@ def getSessionLayers():
349349

350350
@staticmethod
351351
def addSessionLayers(exportedLayers):
352-
GrassUtils.sessionLayers = dict(GrassUtils.sessionLayers.items()
353-
+ exportedLayers.items())
352+
GrassUtils.sessionLayers = dict(
353+
GrassUtils.sessionLayers.items()
354+
+ exportedLayers.items())
354355

355356
@staticmethod
356357
def checkGrassIsInstalled(ignorePreviousState=False):

‎python/plugins/processing/algs/grass7/Grass7Utils.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ def grassPath():
9595
folder = os.path.join(testfolder, subfolder)
9696
break
9797
else:
98-
folder = os.path.join(unicode(QgsApplication.prefixPath()), 'grass7'
99-
)
98+
folder = os.path.join(unicode(QgsApplication.prefixPath()), 'grass7')
10099
if not os.path.isdir(folder):
101100
folder = '/Applications/GRASS-7.0.app/Contents/MacOS'
102101

@@ -240,14 +239,17 @@ def writeGrass7Window(filename):
240239

241240
@staticmethod
242241
def prepareGrass7Execution(commands):
242+
env = os.environ.copy()
243+
243244
if isWindows():
244245
Grass7Utils.createGrass7Script(commands)
245246
command = ['cmd.exe', '/C ', Grass7Utils.grassScriptFilename()]
246247
else:
247248
gisrc = userFolder() + os.sep + 'processing.gisrc7'
248-
os.putenv('GISRC', gisrc)
249-
os.putenv('GRASS_MESSAGE_FORMAT', 'gui')
250-
os.putenv('GRASS_BATCH_JOB', Grass7Utils.grassBatchJobFilename())
249+
env['GISRC'] = gisrc
250+
env['GRASS_MESSAGE_FORMAT'] = 'gui'
251+
env['GRASS_BATCH_JOB'] = Grass7Utils.grassBatchJobFilename()
252+
del env['GISBASE']
251253
Grass7Utils.createGrass7BatchJobFileFromGrass7Commands(commands)
252254
os.chmod(Grass7Utils.grassBatchJobFilename(), stat.S_IEXEC
253255
| stat.S_IREAD | stat.S_IWRITE)
@@ -258,21 +260,22 @@ def prepareGrass7Execution(commands):
258260
command = 'grass70 ' + Grass7Utils.grassMapsetFolder() \
259261
+ '/PERMANENT'
260262

261-
return command
263+
return command, env
262264

263265
@staticmethod
264266
def executeGrass7(commands, progress, outputCommands=None):
265267
loglines = []
266268
loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output'))
267269
grassOutDone = False
268-
command = Grass7Utils.prepareGrass7Execution(commands)
270+
command, grassenv = Grass7Utils.prepareGrass7Execution(commands)
269271
proc = subprocess.Popen(
270272
command,
271273
shell=True,
272274
stdout=subprocess.PIPE,
273275
stdin=open(os.devnull),
274276
stderr=subprocess.STDOUT,
275277
universal_newlines=True,
278+
env=grassenv
276279
).stdout
277280
for line in iter(proc.readline, ''):
278281
if 'GRASS_INFO_PERCENT' in line:
@@ -293,14 +296,15 @@ def executeGrass7(commands, progress, outputCommands=None):
293296
# commands again.
294297

295298
if not grassOutDone and outputCommands:
296-
command = Grass7Utils.prepareGrass7Execution(outputCommands)
299+
command, grassenv = Grass7Utils.prepareGrass7Execution(outputCommands)
297300
proc = subprocess.Popen(
298301
command,
299302
shell=True,
300303
stdout=subprocess.PIPE,
301304
stdin=open(os.devnull),
302305
stderr=subprocess.STDOUT,
303306
universal_newlines=True,
307+
env=grassenv
304308
).stdout
305309
for line in iter(proc.readline, ''):
306310
if 'GRASS_INFO_PERCENT' in line:

‎python/plugins/processing/core/Processing.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from PyQt4.QtGui import QApplication, QCursor
3232

3333
from qgis.utils import iface
34+
from qgis.core import QgsMessageLog
3435

3536
import processing
3637
from processing.gui import AlgorithmClassification
@@ -272,7 +273,7 @@ def runAlgorithm(algOrName, onFinish, *args):
272273
else:
273274
alg = Processing.getAlgorithm(algOrName)
274275
if alg is None:
275-
print 'Error: Algorithm not found\n'
276+
QgsMessageLog.logMessage( Processing.tr( 'Error: Algorithm {0} not found\n' ).format( algOrName ), Processing.tr( "Processing" ) )
276277
return
277278
alg = alg.getCopy()
278279

@@ -288,7 +289,7 @@ def runAlgorithm(algOrName, onFinish, *args):
288289
output = alg.getOutputFromName(name)
289290
if output and output.setValue(value):
290291
continue
291-
print 'Error: Wrong parameter value %s for parameter %s.' % (value, name)
292+
QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong parameter value {0} for parameter {1}.' ).format(value, name), Processing.tr( "Processing" ) )
292293
ProcessingLog.addToLog(
293294
ProcessingLog.LOG_ERROR,
294295
Processing.tr('Error in %s. Wrong parameter value %s for parameter %s.') % (
@@ -299,7 +300,7 @@ def runAlgorithm(algOrName, onFinish, *args):
299300
for param in alg.parameters:
300301
if param.name not in setParams:
301302
if not param.setValue(None):
302-
print ('Error: Missing parameter value for parameter %s.' % (param.name))
303+
QgsMessageLog.logMessage( Processing.tr( 'Error: Missing parameter value for parameter {0}.' ).format(param.name), Processing.tr( "Processing" ) )
303304
ProcessingLog.addToLog(
304305
ProcessingLog.LOG_ERROR,
305306
Processing.tr('Error in %s. Missing parameter value for parameter %s.') % (
@@ -308,33 +309,31 @@ def runAlgorithm(algOrName, onFinish, *args):
308309
return
309310
else:
310311
if len(args) != alg.getVisibleParametersCount() + alg.getVisibleOutputsCount():
311-
print 'Error: Wrong number of parameters'
312+
QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong number of parameters' ), Processing.tr( "Processing" ) )
312313
processing.alghelp(algOrName)
313314
return
314315
i = 0
315316
for param in alg.parameters:
316317
if not param.hidden:
317318
if not param.setValue(args[i]):
318-
print 'Error: Wrong parameter value: ' \
319-
+ unicode(args[i])
319+
QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong parameter value: ' ) + unicode(args[i]), Processing.tr( "Processing" ) )
320320
return
321321
i = i + 1
322322

323323
for output in alg.outputs:
324324
if not output.hidden:
325325
if not output.setValue(args[i]):
326-
print 'Error: Wrong output value: ' + unicode(args[i])
326+
QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong output value: ' ) + unicode(args[i]), Processing.tr( "Processing" ) )
327327
return
328328
i = i + 1
329329

330330
msg = alg._checkParameterValuesBeforeExecuting()
331331
if msg:
332-
print 'Unable to execute algorithm\n' + msg
332+
QgsMessageLog( Processing.tr( 'Unable to execute algorithm\n{0}' ).format( msg ), Processing.tr( "Processing" ) )
333333
return
334334

335335
if not alg.checkInputCRS():
336-
print 'Warning: Not all input layers use the same CRS.\n' \
337-
+ 'This can cause unexpected results.'
336+
QgsMessageLog( Processing.tr( 'Warning: Not all input layers use the same CRS.\nThis can cause unexpected results.' ), Processing.tr("Processing") )
338337

339338
if iface is not None:
340339
# Don't set the wait cursor twice, because then when you
@@ -353,8 +352,7 @@ def runAlgorithm(algOrName, onFinish, *args):
353352
if onFinish is not None:
354353
onFinish(alg, progress)
355354
else:
356-
print ("There were errors executing the algorithm.\n"
357-
"Check the QGIS log to get more information")
355+
QgsMessageLog( Processing.tr( "There were errors executing the algorithm.\nCheck the QGIS log to get more information"), Processing.tr("Processing") )
358356

359357
if iface is not None:
360358
QApplication.restoreOverrideCursor()

0 commit comments

Comments
 (0)
Please sign in to comment.