Esempio n. 1
0
    def runGdal(commands, progress=None):
        if progress is None:
            progress = SilentProgress()
        envval = os.getenv('PATH')
        # We need to give some extra hints to get things picked up on OS X
        isDarwin = False
        try:
            isDarwin = platform.system() == 'Darwin'
        except IOError:  # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
            pass
        if isDarwin and os.path.isfile(
                os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
            # Looks like there's a bundled gdal. Let's use it.
            os.environ['PATH'] = "{}{}{}".format(
                os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep,
                envval)
            os.environ['DYLD_LIBRARY_PATH'] = os.path.join(
                QgsApplication.prefixPath(), "lib")
        else:
            # Other platforms should use default gdal finder codepath
            settings = QSettings()
            path = settings.value('/GdalTools/gdalPath', '')
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += '{}{}'.format(os.pathsep, path)
                os.putenv('PATH', envval)

        fused_command = ' '.join([str(c) for c in commands])
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, fused_command)
        progress.setInfo('GDAL command:')
        progress.setCommand(fused_command)
        progress.setInfo('GDAL command output:')
        success = False
        retry_count = 0
        while success == False:
            loglines = []
            loglines.append('GDAL execution console output')
            try:
                proc = subprocess.Popen(
                    fused_command,
                    shell=True,
                    stdout=subprocess.PIPE,
                    stdin=subprocess.DEVNULL,
                    stderr=subprocess.STDOUT,
                    universal_newlines=True,
                ).stdout
                for line in proc:
                    progress.setConsoleInfo(line)
                    loglines.append(line)
                success = True
            except IOError as e:
                if retry_count < 5:
                    retry_count += 1
                else:
                    raise IOError(
                        e.message +
                        u'\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}'
                        .format(len(loglines), u'\n'.join(loglines[-10:])))

        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        GdalUtils.consoleOutput = loglines
Esempio n. 2
0
    def runGdal(commands, progress=None):
        if progress is None:
            progress = SilentProgress()
        envval = os.getenv("PATH")
        # We need to give some extra hints to get things picked up on OS X
        isDarwin = False
        try:
            isDarwin = platform.system() == "Darwin"
        except IOError:  # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
            pass
        if isDarwin and os.path.isfile(os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
            # Looks like there's a bundled gdal. Let's use it.
            os.environ["PATH"] = "{}{}{}".format(os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep, envval)
            os.environ["DYLD_LIBRARY_PATH"] = os.path.join(QgsApplication.prefixPath(), "lib")
        else:
            # Other platforms should use default gdal finder codepath
            settings = QSettings()
            path = settings.value("/GdalTools/gdalPath", "")
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += "{}{}".format(os.pathsep, path)
                os.putenv("PATH", envval)

        fused_command = " ".join([str(c) for c in commands])
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, fused_command)
        progress.setInfo("GDAL command:")
        progress.setCommand(fused_command)
        progress.setInfo("GDAL command output:")
        success = False
        retry_count = 0
        while success == False:
            loglines = []
            loglines.append("GDAL execution console output")
            try:
                with subprocess.Popen(
                    fused_command,
                    shell=True,
                    stdout=subprocess.PIPE,
                    stdin=subprocess.DEVNULL,
                    stderr=subprocess.STDOUT,
                    universal_newlines=True,
                ) as proc:
                    for line in proc.stdout:
                        progress.setConsoleInfo(line)
                        loglines.append(line)
                    success = True
            except IOError as e:
                if retry_count < 5:
                    retry_count += 1
                else:
                    raise IOError(
                        e.message
                        + u"\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}".format(
                            len(loglines), u"\n".join(loglines[-10:])
                        )
                    )

        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        GdalUtils.consoleOutput = loglines
Esempio n. 3
0
    def runChole(commands, progress=None):
        cwd = os.path.dirname(__file__) + os.sep + 'Chloe2012'

        if progress is None:
            progress = SilentProgress()

        fused_command = ' '.join(commands)
        progress.setInfo('Command:')
        progress.setCommand(fused_command)
        progress.setInfo('Output:')

        # Execution Chloe with subprocess command system
        success = False
        retry_count = 0
        while success == False:
            loglines = []
            loglines.append('Execution console output :')
            try:
                process = subprocess.Popen(
                    fused_command,
                    shell=True,
                    stdout=subprocess.PIPE,
                    stdin=open(os.devnull),
                    stderr=subprocess.STDOUT,
                    universal_newlines=True,
                    cwd=cwd,
                )

                regex = re.compile(
                    r'^\s*##\s*(?P<percentage>\d+)\s*\/\s*\d+\s*$')
                while True:
                    output = process.stdout.readline()
                    if output == '' and process.poll() is not None:
                        break
                    if output:
                        line = output.strip()
                        res = regex.search(line)
                        if res:
                            percentage = int(res.group('percentage'))
                            progress.setPercentage(percentage)
                        else:
                            progress.setConsoleInfo(line)
                        loglines.append(line)
                    rc = process.poll()

                success = True
            except IOError as e:
                if retry_count < 5:
                    retry_count += 1
                else:
                    raise IOError(
                        e.message +
                        u'\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}'
                        .format(len(loglines), u'\n'.join(loglines[-10:])))

        # Save log
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        ChloeUtils.consoleOutput = loglines
Esempio n. 4
0
    def runGdal(commands, progress=None):
        if progress is None:
            progress = SilentProgress()
        envval = os.getenv('PATH')
        # We need to give some extra hints to get things picked up on OS X
        isDarwin = False
        try:
            isDarwin = platform.system() == 'Darwin'
        except IOError: # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
            pass
        if isDarwin and os.path.isfile(os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
            # Looks like there's a bundled gdal. Let's use it.
            os.environ['PATH'] = "{}{}{}".format(os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep, envval)
            os.environ['DYLD_LIBRARY_PATH'] = os.path.join(QgsApplication.prefixPath(), "lib")
        else:
            # Other platforms should use default gdal finder codepath
            settings = QSettings()
            path = settings.value('/GdalTools/gdalPath', '')
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += '{}{}'.format(os.pathsep, path)
                os.putenv('PATH', envval)

        loglines = []
        loglines.append('GDAL execution console output')
        fused_command = ''.join(['%s ' % c for c in commands])
        progress.setInfo('GDAL command:')
        progress.setCommand(fused_command)
        proc = subprocess.Popen(
            fused_command,
            shell=True,
            stdout=subprocess.PIPE,
            stdin=open(os.devnull),
            stderr=subprocess.STDOUT,
            universal_newlines=True,
        ).stdout
        progress.setInfo('GDAL command output:')
        for line in iter(proc.readline, ''):
            progress.setConsoleInfo(line)
            loglines.append(line)
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        GdalUtils.consoleOutput = loglines