Ejemplo n.º 1
0
    def download_repository(self, url):
        """
        Downloads the ilimodels.xml and ilisite.xml files from the provided url
        and updates the local cache.
        """
        netloc = urllib.parse.urlsplit(url)[1]

        modeldir = os.path.join(self.cache_path, netloc)

        os.makedirs(modeldir, exist_ok=True)

        ilimodels_url = urllib.parse.urljoin(url, 'ilimodels.xml')
        ilimodels_path = os.path.join(self.cache_path, netloc, 'ilimodels.xml')
        ilisite_url = urllib.parse.urljoin(url, 'ilisite.xml')
        ilisite_path = os.path.join(self.cache_path, netloc, 'ilisite.xml')

        logger = logging.getLogger(__name__)

        # download ilimodels.xml
        download_file(
            ilimodels_url,
            ilimodels_path,
            on_success=lambda: self._process_ilimodels(ilimodels_path, netloc),
            on_error=lambda error, error_string: logger.warning(
                self.tr('Could not download {url} ({message})').format(
                    url=ilimodels_url, message=error_string)))

        # download ilisite.xml
        download_file(ilisite_url,
                      ilisite_path,
                      on_success=lambda: self._process_ilisite(ilisite_path),
                      on_error=lambda error, error_string: logger.warning(
                          self.tr('Could not download {url} ({message})').
                          format(url=ilisite_url, message=error_string)))
    def download_repository(self, url):
        """
        Downloads the informationfile (default: ilimodels.xml) and ilisite.xml files from the provided url
        and updates the local cache.
        """
        netloc = urllib.parse.urlsplit(url)[1] if not os.path.isdir(url) else url

        information_file_url = self.file_url(url, self.information_file)
        ilisite_url = self.file_url(url, "ilisite.xml")
        logger = logging.getLogger(__name__)

        if os.path.isdir(url):
            # continue with the local file
            if os.path.exists(information_file_url):
                self._process_informationfile(information_file_url, netloc, url)
            else:
                logger.warning(
                    self.tr("Could not find local file {}").format(information_file_url)
                )
            if os.path.exists(ilisite_url):
                self._process_ilisite(ilisite_url)
            else:
                logger.warning(
                    self.tr("Could not find local file  {}").format(ilisite_url)
                )
        else:
            netloc_dir = os.path.join(self.cache_path, netloc)
            os.makedirs(netloc_dir, exist_ok=True)
            information_file_path = os.path.join(netloc_dir, self.information_file)
            ilisite_path = os.path.join(self.cache_path, netloc, "ilisite.xml")

            # download ilimodels.xml
            download_file(
                information_file_url,
                information_file_path,
                on_success=lambda: self._process_informationfile(
                    information_file_path, netloc, url
                ),
                on_error=lambda error, error_string: logger.warning(
                    self.tr("Could not download {url} ({message})").format(
                        url=information_file_url, message=error_string
                    )
                ),
            )

            # download ilisite.xml
            download_file(
                ilisite_url,
                ilisite_path,
                on_success=lambda: self._process_ilisite(ilisite_path),
                on_error=lambda error, error_string: logger.warning(
                    self.tr("Could not download {url} ({message})").format(
                        url=ilisite_url, message=error_string
                    )
                ),
            )
    def download_file(self, netloc, url, file, dataset_id=None):
        """
        Downloads the given file from the given url to the local cache.
        passes the local file path or the id (for information) to signals.
        Returns the file path immediately (might not be downloaded yet)
        """
        file_url = self.file_url(url, file)

        if url is None or os.path.isdir(url):
            file_path = file_url
            # continue with the local file
            if os.path.exists(file_url):
                self.file_download_succeeded.emit(dataset_id, file_url)
            else:
                self.file_download_failed.emit(
                    dataset_id,
                    self.tr("Could not find local file  {}").format(file_url),
                )
        else:
            file_path = os.path.join(self.cache_path, netloc, file)
            file_dir = os.path.dirname(file_path)
            os.makedirs(file_dir, exist_ok=True)

            download_file(
                file_url,
                file_path,
                on_success=lambda: self.file_download_succeeded.emit(
                    dataset_id, file_path
                ),
                on_error=lambda error, error_string: self.file_download_failed.emit(
                    dataset_id,
                    self.tr("Could not download file {url} ({message})").format(
                        url=file_url, message=error_string
                    ),
                ),
            )
        return file_path
Ejemplo n.º 4
0
def get_ili2db_bin(tool, stdout, stderr):
    from QgisModelBaker.libqgsprojectgen.db_factory.db_simple_factory import DbSimpleFactory

    if tool not in DbIliMode or tool == DbIliMode.ili:
        return None

    tool |= DbIliMode.ili  # Regardlless of the incoming form (i.e., pg or ili2pg), we need its corresponding ili tool
    tool_name = tool.name  # in fact, we need the name of the ili tool

    db_simple_factory = DbSimpleFactory()
    db_factory = db_simple_factory.create_factory(tool)

    ili_tool_version = db_factory.get_tool_version()
    ili_tool_url = db_factory.get_tool_url()

    dir_path = os.path.dirname(os.path.realpath(__file__))
    ili2db_dir = '{}-{}'.format(tool_name, ili_tool_version)

    ili2db_file = os.path.join(dir_path, 'bin', ili2db_dir,
                               '{}.jar'.format(tool_name))
    if not os.path.isfile(ili2db_file):
        try:
            os.mkdir(os.path.join(dir_path, 'bin'))
        except FileExistsError:
            pass

        tmpfile = tempfile.NamedTemporaryFile(suffix='.zip', delete=False)

        stdout.emit(
            QCoreApplication.translate(
                'ili2dbutils',
                'Downloading {} version {}…'.format(tool_name,
                                                    ili_tool_version)))

        try:
            download_file(ili_tool_url,
                          tmpfile.name,
                          on_progress=lambda received, total: stdout.emit('.'))
        except NetworkError as e:
            stderr.emit(
                QCoreApplication.translate(
                    'ili2dbutils',
                    'Could not download {tool_name}\n\n  Error: {error}\n\nFile "{file}" not found. Please download and extract <a href="{ili2db_url}">{tool_name}</a>'
                    .format(tool_name=tool_name,
                            ili2db_url=ili_tool_url,
                            error=e.msg,
                            file=ili2db_file)))
            return None

        try:
            with zipfile.ZipFile(tmpfile.name, "r") as z:
                z.extractall(os.path.join(dir_path, 'bin'))
        except zipfile.BadZipFile:
            # We will realize soon enough that the files were not extracted
            pass

        if not os.path.isfile(ili2db_file):
            stderr.emit(
                QCoreApplication.translate(
                    'ili2dbutils',
                    'File "{file}" not found. Please download and extract <a href="{ili2db_url}">{tool_name}</a>.'
                    .format(tool_name=tool_name,
                            file=ili2db_file,
                            ili2db_url=ili_tool_url)))
            return None

    return ili2db_file
Ejemplo n.º 5
0
def get_ili2db_bin(tool, db_ili_version, stdout, stderr):
    from QgisModelBaker.libqgsprojectgen.db_factory.db_simple_factory import (
        DbSimpleFactory, )

    if tool not in DbIliMode or tool == DbIliMode.ili:
        raise RuntimeError("Tool {} not found".format(tool))

    tool |= (
        DbIliMode.ili
    )  # Regardless of the incoming form (i.e., pg or ili2pg), we need its corresponding ili tool
    tool_name = tool.name  # in fact, we need the name of the ili tool

    db_simple_factory = DbSimpleFactory()
    db_factory = db_simple_factory.create_factory(tool)

    ili_tool_version = db_factory.get_tool_version(db_ili_version)
    ili_tool_url = db_factory.get_tool_url(db_ili_version)

    dir_path = os.path.dirname(os.path.realpath(__file__))
    ili2db_dir = "{}-{}".format(tool_name, ili_tool_version)

    # the structure changed since 3.12.2
    if version.Version(ili_tool_version) < version.Version("3.12.2"):
        ili2db_file = os.path.join(
            dir_path,
            "bin",
            ili2db_dir,
            "{tool}-{version}/{tool}.jar".format(tool=tool_name,
                                                 version=ili_tool_version),
        )
    else:
        ili2db_file = os.path.join(
            dir_path,
            "bin",
            ili2db_dir,
            "{tool}-{version}.jar".format(tool=tool_name,
                                          version=ili_tool_version),
        )

    if not os.path.isfile(ili2db_file):
        try:
            os.makedirs(os.path.join(dir_path, "bin", ili2db_dir),
                        exist_ok=True)
        except FileExistsError:
            pass

        tmpfile = tempfile.NamedTemporaryFile(suffix=".zip", delete=False)

        stdout.emit(
            QCoreApplication.translate(
                "ili2dbutils",
                "Downloading {} version {}…".format(tool_name,
                                                    ili_tool_version),
            ))

        try:
            download_file(
                ili_tool_url,
                tmpfile.name,
                on_progress=lambda received, total: stdout.emit("."),
            )
        except NetworkError as e:
            stderr.emit(
                QCoreApplication.translate(
                    "ili2dbutils",
                    'Could not download {tool_name}\n\n  Error: {error}\n\nFile "{file}" not found. Please download and extract <a href="{ili2db_url}">{tool_name}</a>'
                    .format(
                        tool_name=tool_name,
                        ili2db_url=ili_tool_url,
                        error=e.msg,
                        file=ili2db_file,
                    ),
                ))
            return None

        try:
            with zipfile.ZipFile(tmpfile.name, "r") as z:
                z.extractall(os.path.join(dir_path, "bin", ili2db_dir))
        except zipfile.BadZipFile:
            # We will realize soon enough that the files were not extracted
            pass

        if not os.path.isfile(ili2db_file):
            stderr.emit(
                QCoreApplication.translate(
                    "ili2dbutils",
                    'File "{file}" not found. Please download and extract <a href="{ili2db_url}">{tool_name}</a>.'
                    .format(tool_name=tool_name,
                            file=ili2db_file,
                            ili2db_url=ili_tool_url),
                ))
            return None

    return ili2db_file