1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
# -*- coding: utf-8 -*-
"""
***************************************************************************
SagaUtils.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import stat
import subprocess
import time
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsApplication
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingLog import ProcessingLog
from processing.tools.system import isWindows, isMac, userFolder
SAGA_LOG_COMMANDS = 'SAGA_LOG_COMMANDS'
SAGA_LOG_CONSOLE = 'SAGA_LOG_CONSOLE'
SAGA_IMPORT_EXPORT_OPTIMIZATION = 'SAGA_IMPORT_EXPORT_OPTIMIZATION'
_installedVersion = None
_installedVersionFound = False
def sagaBatchJobFilename():
if isWindows():
filename = 'saga_batch_job.bat'
else:
filename = 'saga_batch_job.sh'
batchfile = userFolder() + os.sep + filename
return batchfile
def findSagaFolder():
folder = None
if isMac():
testfolder = os.path.join(QgsApplication.prefixPath(), 'bin')
if os.path.exists(os.path.join(testfolder, 'saga_cmd')):
folder = testfolder
else:
testfolder = '/usr/local/bin'
if os.path.exists(os.path.join(testfolder, 'saga_cmd')):
folder = testfolder
elif isWindows():
folders = []
folders.append(os.path.join(os.path.dirname(QgsApplication.prefixPath()), 'saga-ltr'))
folders.append(os.path.join(os.path.dirname(QgsApplication.prefixPath()), 'saga'))
if "OSGEO4W_ROOT" in os.environ:
folders.append(os.path.join(str(os.environ['OSGEO4W_ROOT']), "apps", "saga-ltr"))
folders.append(os.path.join(str(os.environ['OSGEO4W_ROOT']), "apps", "saga"))
for testfolder in folders:
if os.path.exists(os.path.join(testfolder, 'saga_cmd.exe')):
folder = testfolder
break
return folder
def sagaPath():
if not isWindows() and not isMac():
return ''
folder = findSagaFolder()
return folder or ''
def sagaDescriptionPath():
return os.path.join(os.path.dirname(__file__), 'description')
def createSagaBatchJobFileFromSagaCommands(commands):
fout = open(sagaBatchJobFilename(), 'w')
if isWindows():
fout.write('set SAGA=' + sagaPath() + '\n')
fout.write('set SAGA_MLB=' + sagaPath() + os.sep +
'modules' + '\n')
fout.write('PATH=%PATH%;%SAGA%;%SAGA_MLB%\n')
elif isMac():
fout.write('export SAGA_MLB=' + sagaPath() +
'/../lib/saga\n')
fout.write('export PATH=' + sagaPath() + ':$PATH\n')
else:
pass
for command in commands:
try:
# Python 2
fout.write('saga_cmd ' + command.encode('utf8') + '\n')
except TypeError:
# Python 3
fout.write('saga_cmd ' + command + '\n')
fout.write('exit')
fout.close()
def getSagaInstalledVersion(runSaga=False):
global _installedVersion
global _installedVersionFound
maxRetries = 5
retries = 0
if _installedVersionFound and not runSaga:
return _installedVersion
if isWindows():
commands = [os.path.join(sagaPath(), "saga_cmd.exe"), "-v"]
elif isMac():
commands = [os.path.join(sagaPath(), "saga_cmd -v")]
else:
# for Linux use just one string instead of separated parameters as the list
# does not work well together with shell=True option
# (python docs advices to use subprocess32 instead of python2.7's subprocess)
commands = ["saga_cmd -v"]
while retries < maxRetries:
proc = subprocess.Popen(
commands,
shell=True,
stdout=subprocess.PIPE,
stdin=open(os.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
return _installedVersion
def executeSaga(progress):
if isWindows():
command = ['cmd.exe', '/C ', sagaBatchJobFilename()]
else:
os.chmod(sagaBatchJobFilename(), stat.S_IEXEC |
stat.S_IREAD | stat.S_IWRITE)
command = [sagaBatchJobFilename()]
loglines = []
loglines.append(QCoreApplication.translate('SagaUtils', 'SAGA execution console output'))
proc = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stdin=open(os.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
if ProcessingConfig.getSetting(SAGA_LOG_CONSOLE):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
|