From 77ca802522df1bcaa1c1db560dd649a5ca53419b Mon Sep 17 00:00:00 2001 From: volaya Date: Tue, 23 Jun 2015 12:01:21 +0200 Subject: [PATCH] [processing] better initialization of SAGA provider --- .../algs/saga/SagaAlgorithmProvider.py | 3 +- .../plugins/processing/algs/saga/SagaUtils.py | 38 ++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py b/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py index b2bc3caaa6b..9d61839e31d 100644 --- a/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py +++ b/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py @@ -54,7 +54,6 @@ class SagaAlgorithmProvider(AlgorithmProvider): self.activate = True def initializeSettings(self): - AlgorithmProvider.initializeSettings(self) if isWindows() or isMac(): ProcessingConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_FOLDER, self.tr('SAGA folder'), '')) @@ -92,7 +91,7 @@ class SagaAlgorithmProvider(AlgorithmProvider): return folder = SagaUtils.sagaDescriptionPath() - folder = os.path.join(folder, self.supportedVersions[SagaUtils.getSagaInstalledVersion()][0]) + folder = os.path.join(folder, self.supportedVersions[version][0]) for descriptionFile in os.listdir(folder): if descriptionFile.endswith('txt'): f = os.path.join(folder, descriptionFile) diff --git a/python/plugins/processing/algs/saga/SagaUtils.py b/python/plugins/processing/algs/saga/SagaUtils.py index df070b67d32..e46236f32de 100644 --- a/python/plugins/processing/algs/saga/SagaUtils.py +++ b/python/plugins/processing/algs/saga/SagaUtils.py @@ -106,16 +106,21 @@ def getSagaInstalledVersion(runSaga=False): global _installedVersion global _installedVersionFound - if not _installedVersionFound or runSaga: - 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"] + maxRetries = 5 + retries = 0 + if _installedVersionFound or 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, @@ -126,14 +131,19 @@ def getSagaInstalledVersion(runSaga=False): ).stdout 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 + except IOError: + retries += 1 except: return None - for line in lines: - if line.startswith("SAGA Version:"): - _installedVersion = line[len("SAGA Version:"):].strip().split(" ")[0] - _installedVersionFound = True + return _installedVersion + def executeSaga(progress): if isWindows(): command = ['cmd.exe', '/C ', sagaBatchJobFilename()]