Skip to content
Permalink
Browse files

processing: when using batch jobs remove GISBASE from environment whe…

…n calling GRASS (fixes #13072)
  • Loading branch information
jef-n committed Aug 19, 2015
1 parent 16d7a06 commit d2282a77c7967a8c5bb9e0599b5602be5317a7db
@@ -30,12 +30,11 @@
import codecs
import subprocess
import os

from qgis.core import QgsApplication
from PyQt4.QtCore import QCoreApplication
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingLog import ProcessingLog
from processing.tools.system import userFolder, isMac, isWindows, mkdir, tempFolder
from processing.tools.system import userFolder, isWindows, isMac, tempFolder, mkdir
from processing.tests.TestData import points


@@ -150,8 +149,7 @@ def createGrassScript(commands):
output.write('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%PATH%\n')
output.write('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%GRASS_ADDON_PATH%;%PATH%\n')
output.write('\n')
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion()
+ '\n')
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion() + '\n')
output.write('if not "%LANG%"=="" goto langset\n')
output.write('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\etc\\winlocale"`) DO @set LANG=%%i\n')
output.write(':langset\n')
@@ -203,12 +201,12 @@ def createTempMapset():
folder = GrassUtils.grassMapsetFolder()
mkdir(os.path.join(folder, 'PERMANENT'))
mkdir(os.path.join(folder, 'PERMANENT', '.tmp'))
GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT',
'DEFAULT_WIND'))
GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT', 'DEFAULT_WIND'))
outfile = codecs.open(os.path.join(folder, 'PERMANENT', 'MYNAME'), 'w', encoding='utf-8')
outfile.write(
'QGIS GRASS interface: temporary data processing location.\n')
outfile.close()

GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT', 'WIND'))
mkdir(os.path.join(folder, 'PERMANENT', 'dbf'))
outfile = codecs.open(os.path.join(folder, 'PERMANENT', 'VAR'), 'w', encoding='utf-8')
@@ -242,14 +240,17 @@ def writeGrassWindow(filename):

@staticmethod
def prepareGrassExecution(commands):
env = os.environ.copy()

if isWindows():
GrassUtils.createGrassScript(commands)
command = ['cmd.exe', '/C ', GrassUtils.grassScriptFilename()]
else:
gisrc = userFolder() + os.sep + 'processing.gisrc'
os.putenv('GISRC', gisrc)
os.putenv('GRASS_MESSAGE_FORMAT', 'gui')
os.putenv('GRASS_BATCH_JOB', GrassUtils.grassBatchJobFilename())
env['GISRC'] = gisrc
env['GRASS_MESSAGE_FORMAT'] = 'gui'
env['GRASS_BATCH_JOB'] = GrassUtils.grassBatchJobFilename()
del env['GISBASE']
GrassUtils.createGrassBatchJobFileFromGrassCommands(commands)
os.chmod(GrassUtils.grassBatchJobFilename(), stat.S_IEXEC
| stat.S_IREAD | stat.S_IWRITE)
@@ -260,28 +261,28 @@ def prepareGrassExecution(commands):
command = 'grass64 ' + GrassUtils.grassMapsetFolder() \
+ '/PERMANENT'

return command
return command, env

@staticmethod
def executeGrass(commands, progress, outputCommands=None):
loglines = []
loglines.append('GRASS execution console output')
grassOutDone = False
command = GrassUtils.prepareGrassExecution(commands)
command, grassenv = GrassUtils.prepareGrassExecution(commands)
proc = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stdin=open(os.devnull),
stderr=subprocess.STDOUT,
universal_newlines=True,
env=grassenv
).stdout
progress.setInfo('GRASS commands output:')
for line in iter(proc.readline, ''):
if 'GRASS_INFO_PERCENT' in line:
try:
progress.setPercentage(int(line[len('GRASS_INFO_PERCENT')
+ 2:]))
progress.setPercentage(int(line[len('GRASS_INFO_PERCENT') + 2:]))
except:
pass
else:
@@ -297,14 +298,15 @@ def executeGrass(commands, progress, outputCommands=None):
# commands again.

if not grassOutDone and outputCommands:
command = GrassUtils.prepareGrassExecution(outputCommands)
command, grassenv = GrassUtils.prepareGrassExecution(outputCommands)
proc = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stdin=open(os.devnull),
stderr=subprocess.STDOUT,
universal_newlines=True,
env=grassenv
).stdout
for line in iter(proc.readline, ''):
if 'GRASS_INFO_PERCENT' in line:
@@ -320,8 +322,6 @@ def executeGrass(commands, progress, outputCommands=None):
if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)



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

@staticmethod
def addSessionLayers(exportedLayers):
GrassUtils.sessionLayers = dict(GrassUtils.sessionLayers.items()
+ exportedLayers.items())
GrassUtils.sessionLayers = dict(
GrassUtils.sessionLayers.items()
+ exportedLayers.items())

@staticmethod
def checkGrassIsInstalled(ignorePreviousState=False):
@@ -95,8 +95,7 @@ def grassPath():
folder = os.path.join(testfolder, subfolder)
break
else:
folder = os.path.join(unicode(QgsApplication.prefixPath()), 'grass7'
)
folder = os.path.join(unicode(QgsApplication.prefixPath()), 'grass7')
if not os.path.isdir(folder):
folder = '/Applications/GRASS-7.0.app/Contents/MacOS'

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

@staticmethod
def prepareGrass7Execution(commands):
env = os.environ.copy()

if isWindows():
Grass7Utils.createGrass7Script(commands)
command = ['cmd.exe', '/C ', Grass7Utils.grassScriptFilename()]
else:
gisrc = userFolder() + os.sep + 'processing.gisrc7'
os.putenv('GISRC', gisrc)
os.putenv('GRASS_MESSAGE_FORMAT', 'gui')
os.putenv('GRASS_BATCH_JOB', Grass7Utils.grassBatchJobFilename())
env['GISRC'] = gisrc
env['GRASS_MESSAGE_FORMAT'] = 'gui'
env['GRASS_BATCH_JOB'] = Grass7Utils.grassBatchJobFilename()
del env['GISBASE']
Grass7Utils.createGrass7BatchJobFileFromGrass7Commands(commands)
os.chmod(Grass7Utils.grassBatchJobFilename(), stat.S_IEXEC
| stat.S_IREAD | stat.S_IWRITE)
@@ -258,21 +260,22 @@ def prepareGrass7Execution(commands):
command = 'grass70 ' + Grass7Utils.grassMapsetFolder() \
+ '/PERMANENT'

return command
return command, env

@staticmethod
def executeGrass7(commands, progress, outputCommands=None):
loglines = []
loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output'))
grassOutDone = False
command = Grass7Utils.prepareGrass7Execution(commands)
command, grassenv = Grass7Utils.prepareGrass7Execution(commands)
proc = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stdin=open(os.devnull),
stderr=subprocess.STDOUT,
universal_newlines=True,
env=grassenv
).stdout
for line in iter(proc.readline, ''):
if 'GRASS_INFO_PERCENT' in line:
@@ -293,14 +296,15 @@ def executeGrass7(commands, progress, outputCommands=None):
# commands again.

if not grassOutDone and outputCommands:
command = Grass7Utils.prepareGrass7Execution(outputCommands)
command, grassenv = Grass7Utils.prepareGrass7Execution(outputCommands)
proc = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stdin=open(os.devnull),
stderr=subprocess.STDOUT,
universal_newlines=True,
env=grassenv
).stdout
for line in iter(proc.readline, ''):
if 'GRASS_INFO_PERCENT' in line:
@@ -31,6 +31,7 @@
from PyQt4.QtGui import QApplication, QCursor

from qgis.utils import iface
from qgis.core import QgsMessageLog

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

@@ -288,7 +289,7 @@ def runAlgorithm(algOrName, onFinish, *args):
output = alg.getOutputFromName(name)
if output and output.setValue(value):
continue
print 'Error: Wrong parameter value %s for parameter %s.' % (value, name)
QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong parameter value {0} for parameter {1}.' ).format(value, name), Processing.tr( "Processing" ) )
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
Processing.tr('Error in %s. Wrong parameter value %s for parameter %s.') % (
@@ -299,7 +300,7 @@ def runAlgorithm(algOrName, onFinish, *args):
for param in alg.parameters:
if param.name not in setParams:
if not param.setValue(None):
print ('Error: Missing parameter value for parameter %s.' % (param.name))
QgsMessageLog.logMessage( Processing.tr( 'Error: Missing parameter value for parameter {0}.' ).format(param.name), Processing.tr( "Processing" ) )
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
Processing.tr('Error in %s. Missing parameter value for parameter %s.') % (
@@ -308,33 +309,31 @@ def runAlgorithm(algOrName, onFinish, *args):
return
else:
if len(args) != alg.getVisibleParametersCount() + alg.getVisibleOutputsCount():
print 'Error: Wrong number of parameters'
QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong number of parameters' ), Processing.tr( "Processing" ) )
processing.alghelp(algOrName)
return
i = 0
for param in alg.parameters:
if not param.hidden:
if not param.setValue(args[i]):
print 'Error: Wrong parameter value: ' \
+ unicode(args[i])
QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong parameter value: ' ) + unicode(args[i]), Processing.tr( "Processing" ) )
return
i = i + 1

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

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

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

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

if iface is not None:
QApplication.restoreOverrideCursor()

0 comments on commit d2282a7

Please sign in to comment.
You can’t perform that action at this time.