#!/usr/bin/env python3
#
# Copyright (C) 2018 Linus Jahn <lnj@kaidan.im>
# Copyright (C) 2019,2020 Hiroshi Miura <miurahr@linux.com>
# Copyright (C) 2020, Aurélien Gâteau
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import concurrent.futures
import os
import pathlib
import subprocess
import sys
import time
from logging import getLogger

import py7zr
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from aqt.archives import QtPackage
from aqt.helper import altlink, versiontuple
from aqt.qtpatch import Updater
from aqt.settings import Settings


class ExtractionError(Exception):
    pass


class QtInstaller:
    """
    Installer class to download packages and extract it.
    """
    def __init__(self, qt_archives, logging=None, command=None, target_dir=None):
        self.qt_archives = qt_archives
        if logging:
            self.logger = logging
        else:
            self.logger = getLogger('aqt')
        self.command = command
        if target_dir is None:
            self.base_dir = os.getcwd()
        else:
            self.base_dir = target_dir
        self.settings = Settings()

    def retrieve_archive(self, package: QtPackage):
        archive = package.archive
        url = package.url
        name = package.name
        start_time = time.perf_counter()
        self.logger.info("Downloading {}...".format(name))
        self.logger.debug("Download URL: {}".format(url))
        session = requests.Session()
        retry = Retry(connect=5, backoff_factor=0.5)
        adapter = HTTPAdapter(max_retries=retry)
        session.mount('http://', adapter)
        session.mount('https://', adapter)
        try:
            r = session.get(url, allow_redirects=False, stream=True)
            if r.status_code == 302:
                newurl = altlink(r.url, r.headers['Location'], logger=self.logger)
                self.logger.info('Redirected URL: {}'.format(newurl))
                r = session.get(newurl, stream=True)
        except requests.exceptions.ConnectionError as e:
            self.logger.error("Connection error: %s" % e.args)
            raise e
        else:
            try:
                with open(archive, 'wb') as fd:
                    for chunk in r.iter_content(chunk_size=8196):
                        fd.write(chunk)
                        fd.flush()
                if self.command is None:
                    with open(archive, 'rb') as fd:
                        self.extract_archive(fd)
            except Exception as e:
                exc = sys.exc_info()
                self.logger.error("Download error: %s" % exc[1])
                raise e
            else:
                if self.command is not None:
                    self.extract_archive_ext(archive)
        os.unlink(archive)
        self.logger.info("Finish installation of {} in {}".format(archive, time.perf_counter() - start_time))

    def extract_archive(self, archive):
        szf = py7zr.SevenZipFile(archive)
        szf.extractall(path=self.base_dir)
        szf.close()

    def extract_archive_ext(self, archive):
        if self.base_dir is not None:
            command_args = [self.command, 'x', '-aoa', '-bd', '-y', '-o{}'.format(self.base_dir), archive]
        else:
            command_args = [self.command, 'x', '-aoa', '-bd', '-y', archive]
        try:
            proc = subprocess.run(command_args, stdout=subprocess.PIPE, check=True)
            self.logger.debug(proc.stdout)
        except subprocess.CalledProcessError as cpe:
            self.logger.error("Extraction error: %d" % cpe.returncode)
            if cpe.stdout is not None:
                self.logger.error(cpe.stdout)
            if cpe.stderr is not None:
                self.logger.error(cpe.stderr)
            raise cpe

    def get_arch_dir(self, arch):
        if arch.startswith('win64_mingw'):
            arch_dir = arch[6:] + '_64'
        elif arch.startswith('win32_mingw'):
            arch_dir = arch[6:] + '_32'
        elif arch.startswith('win'):
            arch_dir = arch[6:]
        else:
            arch_dir = arch
        return arch_dir

    def make_conf_files(self, qt_version, arch):
        """Make Qt configuration files, qt.conf and qtconfig.pri"""
        arch_dir = self.get_arch_dir(arch)
        try:
            # prepare qt.conf
            with open(os.path.join(self.base_dir, qt_version, arch_dir, 'bin', 'qt.conf'), 'w') as f:
                f.write("[Paths]\n")
                f.write("Prefix=..\n")
            # update qtconfig.pri only as OpenSource
            with open(os.path.join(self.base_dir, qt_version, arch_dir, 'mkspecs', 'qconfig.pri'), 'r+') as f:
                lines = f.readlines()
                f.seek(0)
                f.truncate()
                for line in lines:
                    if line.startswith('QT_EDITION ='):
                        line = 'QT_EDITION = OpenSource\n'
                    if line.startswith('QT_LICHECK ='):
                        line = 'QT_LICHECK =\n'
                    f.write(line)
        except IOError as e:
            self.logger.error("Configuration file generation error: %s\n", e.args, exc_info=True)
            raise e

    def install(self):
        with concurrent.futures.ThreadPoolExecutor(self.settings.concurrency) as executor:
            futures = [executor.submit(self.retrieve_archive, ar) for ar in self.qt_archives.get_archives()]
            done, not_done = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_EXCEPTION)
            if len(not_done) > 0:
                self.logger.error("Installation error detected.")
                exit(1)
            try:
                for feature in done:
                    feature.result()
            except Exception:
                exit(1)

    def finalize(self):
        target = self.qt_archives.get_target_config()
        self.make_conf_files(target.version, target.arch)
        prefix = pathlib.Path(self.base_dir) / target.version / target.arch
        updater = Updater(prefix, self.logger)
        if versiontuple(target.version) < (5, 14, 2):
            updater.patch_qt(target)

[((62, 24, 62, 34), 'aqt.settings.Settings', 'Settings', ({}, {}), '()', False, 'from aqt.settings import Settings\n'), ((68, 21, 68, 40), 'time.perf_counter', 'time.perf_counter', ({}, {}), '()', False, 'import time\n'), ((71, 18, 71, 36), 'requests.Session', 'requests.Session', ({}, {}), '()', False, 'import requests\n'), ((72, 16, 72, 52), 'urllib3.util.retry.Retry', 'Retry', (), '', False, 'from urllib3.util.retry import Retry\n'), ((73, 18, 73, 48), 'requests.adapters.HTTPAdapter', 'HTTPAdapter', (), '', False, 'from requests.adapters import HTTPAdapter\n'), ((101, 8, 101, 26), 'os.unlink', 'os.unlink', ({(101, 18, 101, 25): 'archive'}, {}), '(archive)', False, 'import os\n'), ((105, 14, 105, 41), 'py7zr.SevenZipFile', 'py7zr.SevenZipFile', ({(105, 33, 105, 40): 'archive'}, {}), '(archive)', False, 'import py7zr\n'), ((176, 18, 176, 46), 'aqt.qtpatch.Updater', 'Updater', ({(176, 26, 176, 32): 'prefix', (176, 34, 176, 45): 'self.logger'}, {}), '(prefix, self.logger)', False, 'from aqt.qtpatch import Updater\n'), ((56, 26, 56, 42), 'logging.getLogger', 'getLogger', ({(56, 36, 56, 41): '"""aqt"""'}, {}), "('aqt')", False, 'from logging import getLogger\n'), ((59, 28, 59, 39), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((115, 19, 115, 83), 'subprocess.run', 'subprocess.run', (), '', False, 'import subprocess\n'), ((177, 11, 177, 39), 'aqt.helper.versiontuple', 'versiontuple', ({(177, 24, 177, 38): 'target.version'}, {}), '(target.version)', False, 'from aqt.helper import altlink, versiontuple\n'), ((79, 25, 79, 82), 'aqt.helper.altlink', 'altlink', (), '', False, 'from aqt.helper import altlink, versiontuple\n'), ((175, 17, 175, 44), 'pathlib.Path', 'pathlib.Path', ({(175, 30, 175, 43): 'self.base_dir'}, {}), '(self.base_dir)', False, 'import pathlib\n'), ((95, 22, 95, 36), 'sys.exc_info', 'sys.exc_info', ({}, {}), '()', False, 'import sys\n'), ((102, 75, 102, 94), 'time.perf_counter', 'time.perf_counter', ({}, {}), '()', False, 'import time\n'), ((141, 22, 141, 89), 'os.path.join', 'os.path.join', ({(141, 35, 141, 48): 'self.base_dir', (141, 50, 141, 60): 'qt_version', (141, 62, 141, 70): 'arch_dir', (141, 72, 141, 77): '"""bin"""', (141, 79, 141, 88): '"""qt.conf"""'}, {}), "(self.base_dir, qt_version, arch_dir, 'bin', 'qt.conf')", False, 'import os\n'), ((145, 22, 145, 97), 'os.path.join', 'os.path.join', ({(145, 35, 145, 48): 'self.base_dir', (145, 50, 145, 60): 'qt_version', (145, 62, 145, 70): 'arch_dir', (145, 72, 145, 81): '"""mkspecs"""', (145, 83, 145, 96): '"""qconfig.pri"""'}, {}), "(self.base_dir, qt_version, arch_dir, 'mkspecs', 'qconfig.pri')", False, 'import os\n')]