30
30
import codecs
31
31
import subprocess
32
32
import os
33
-
34
33
from qgis .core import QgsApplication
35
34
from PyQt4 .QtCore import QCoreApplication
36
35
from processing .core .ProcessingConfig import ProcessingConfig
37
36
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
39
38
from processing .tests .TestData import points
40
39
41
40
@@ -150,8 +149,7 @@ def createGrassScript(commands):
150
149
output .write ('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\ bin;%WINGISBASE%\\ lib;%PATH%\n ' )
151
150
output .write ('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\ bin;%WINGISBASE%\\ lib;%GRASS_ADDON_PATH%;%PATH%\n ' )
152
151
output .write ('\n ' )
153
- output .write ('set GRASS_VERSION=' + GrassUtils .getGrassVersion ()
154
- + '\n ' )
152
+ output .write ('set GRASS_VERSION=' + GrassUtils .getGrassVersion () + '\n ' )
155
153
output .write ('if not "%LANG%"=="" goto langset\n ' )
156
154
output .write ('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\ etc\\ winlocale"`) DO @set LANG=%%i\n ' )
157
155
output .write (':langset\n ' )
@@ -203,12 +201,12 @@ def createTempMapset():
203
201
folder = GrassUtils .grassMapsetFolder ()
204
202
mkdir (os .path .join (folder , 'PERMANENT' ))
205
203
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' ))
208
205
outfile = codecs .open (os .path .join (folder , 'PERMANENT' , 'MYNAME' ), 'w' , encoding = 'utf-8' )
209
206
outfile .write (
210
207
'QGIS GRASS interface: temporary data processing location.\n ' )
211
208
outfile .close ()
209
+
212
210
GrassUtils .writeGrassWindow (os .path .join (folder , 'PERMANENT' , 'WIND' ))
213
211
mkdir (os .path .join (folder , 'PERMANENT' , 'dbf' ))
214
212
outfile = codecs .open (os .path .join (folder , 'PERMANENT' , 'VAR' ), 'w' , encoding = 'utf-8' )
@@ -242,14 +240,17 @@ def writeGrassWindow(filename):
242
240
243
241
@staticmethod
244
242
def prepareGrassExecution (commands ):
243
+ env = os .environ .copy ()
244
+
245
245
if isWindows ():
246
246
GrassUtils .createGrassScript (commands )
247
247
command = ['cmd.exe' , '/C ' , GrassUtils .grassScriptFilename ()]
248
248
else :
249
249
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' ]
253
254
GrassUtils .createGrassBatchJobFileFromGrassCommands (commands )
254
255
os .chmod (GrassUtils .grassBatchJobFilename (), stat .S_IEXEC
255
256
| stat .S_IREAD | stat .S_IWRITE )
@@ -260,28 +261,28 @@ def prepareGrassExecution(commands):
260
261
command = 'grass64 ' + GrassUtils .grassMapsetFolder () \
261
262
+ '/PERMANENT'
262
263
263
- return command
264
+ return command , env
264
265
265
266
@staticmethod
266
267
def executeGrass (commands , progress , outputCommands = None ):
267
268
loglines = []
268
269
loglines .append ('GRASS execution console output' )
269
270
grassOutDone = False
270
- command = GrassUtils .prepareGrassExecution (commands )
271
+ command , grassenv = GrassUtils .prepareGrassExecution (commands )
271
272
proc = subprocess .Popen (
272
273
command ,
273
274
shell = True ,
274
275
stdout = subprocess .PIPE ,
275
276
stdin = open (os .devnull ),
276
277
stderr = subprocess .STDOUT ,
277
278
universal_newlines = True ,
279
+ env = grassenv
278
280
).stdout
279
281
progress .setInfo ('GRASS commands output:' )
280
282
for line in iter (proc .readline , '' ):
281
283
if 'GRASS_INFO_PERCENT' in line :
282
284
try :
283
- progress .setPercentage (int (line [len ('GRASS_INFO_PERCENT' )
284
- + 2 :]))
285
+ progress .setPercentage (int (line [len ('GRASS_INFO_PERCENT' ) + 2 :]))
285
286
except :
286
287
pass
287
288
else :
@@ -297,14 +298,15 @@ def executeGrass(commands, progress, outputCommands=None):
297
298
# commands again.
298
299
299
300
if not grassOutDone and outputCommands :
300
- command = GrassUtils .prepareGrassExecution (outputCommands )
301
+ command , grassenv = GrassUtils .prepareGrassExecution (outputCommands )
301
302
proc = subprocess .Popen (
302
303
command ,
303
304
shell = True ,
304
305
stdout = subprocess .PIPE ,
305
306
stdin = open (os .devnull ),
306
307
stderr = subprocess .STDOUT ,
307
308
universal_newlines = True ,
309
+ env = grassenv
308
310
).stdout
309
311
for line in iter (proc .readline , '' ):
310
312
if 'GRASS_INFO_PERCENT' in line :
@@ -320,8 +322,6 @@ def executeGrass(commands, progress, outputCommands=None):
320
322
if ProcessingConfig .getSetting (GrassUtils .GRASS_LOG_CONSOLE ):
321
323
ProcessingLog .addToLog (ProcessingLog .LOG_INFO , loglines )
322
324
323
-
324
-
325
325
# GRASS session is used to hold the layers already exported or
326
326
# produced in GRASS between multiple calls to GRASS algorithms.
327
327
# This way they don't have to be loaded multiple times and
@@ -349,8 +349,9 @@ def getSessionLayers():
349
349
350
350
@staticmethod
351
351
def addSessionLayers (exportedLayers ):
352
- GrassUtils .sessionLayers = dict (GrassUtils .sessionLayers .items ()
353
- + exportedLayers .items ())
352
+ GrassUtils .sessionLayers = dict (
353
+ GrassUtils .sessionLayers .items ()
354
+ + exportedLayers .items ())
354
355
355
356
@staticmethod
356
357
def checkGrassIsInstalled (ignorePreviousState = False ):
0 commit comments