processing: when using batch jobs remove GISBASE from environment when calling GRASS (fixes #13072)

This commit is contained in:
Juergen E. Fischer 2015-08-20 01:10:43 +02:00
parent 16d7a0659e
commit d2282a77c7
3 changed files with 41 additions and 38 deletions

View File

@ -30,12 +30,11 @@ import shutil
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 @@ class GrassUtils:
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 @@ class GrassUtils:
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 @@ class GrassUtils:
@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,14 +261,14 @@ class GrassUtils:
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,
@ -275,13 +276,13 @@ class GrassUtils:
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,7 +298,7 @@ class GrassUtils:
# commands again.
if not grassOutDone and outputCommands:
command = GrassUtils.prepareGrassExecution(outputCommands)
command, grassenv = GrassUtils.prepareGrassExecution(outputCommands)
proc = subprocess.Popen(
command,
shell=True,
@ -305,6 +306,7 @@ class GrassUtils:
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 @@ class GrassUtils:
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 @@ class GrassUtils:
@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):

View File

@ -95,8 +95,7 @@ class Grass7Utils:
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 @@ class Grass7Utils:
@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,14 +260,14 @@ class Grass7Utils:
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,
@ -273,6 +275,7 @@ class Grass7Utils:
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,7 +296,7 @@ class Grass7Utils:
# commands again.
if not grassOutDone and outputCommands:
command = Grass7Utils.prepareGrass7Execution(outputCommands)
command, grassenv = Grass7Utils.prepareGrass7Execution(outputCommands)
proc = subprocess.Popen(
command,
shell=True,
@ -301,6 +304,7 @@ class Grass7Utils:
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:

View File

@ -31,6 +31,7 @@ from PyQt4.QtCore import Qt, QCoreApplication
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 @@ class Processing:
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 @@ class Processing:
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 @@ class Processing:
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 @@ class Processing:
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 @@ class Processing:
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()