# Copyright 2019 Atalaya Tech, Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import io
import os
import sys
import tarfile
import logging
import tempfile
import shutil
from functools import wraps
from contextlib import contextmanager
from urllib.parse import urlparse
from typing import TYPE_CHECKING
from pathlib import PureWindowsPath, PurePosixPath

from bentoml.utils.s3 import is_s3_url
from bentoml.utils.gcs import is_gcs_url
from bentoml.exceptions import BentoMLException
from bentoml.saved_bundle.config import SavedBundleConfig
from bentoml.saved_bundle.pip_pkg import ZIPIMPORT_DIR

if TYPE_CHECKING:
    from bentoml.yatai.proto.repository_pb2 import BentoServiceMetadata

logger = logging.getLogger(__name__)


def _is_http_url(bundle_path) -> bool:
    try:
        return urlparse(bundle_path).scheme in ["http", "https"]
    except ValueError:
        return False


def _is_remote_path(bundle_path) -> bool:
    return isinstance(bundle_path, str) and (
        is_s3_url(bundle_path) or is_gcs_url(bundle_path) or _is_http_url(bundle_path)
    )


@contextmanager
def _resolve_remote_bundle_path(bundle_path):
    if is_s3_url(bundle_path):
        import boto3

        parsed_url = urlparse(bundle_path)
        bucket_name = parsed_url.netloc
        object_name = parsed_url.path.lstrip('/')

        s3 = boto3.client('s3')
        fileobj = io.BytesIO()
        s3.download_fileobj(bucket_name, object_name, fileobj)
        fileobj.seek(0, 0)
    elif is_gcs_url(bundle_path):
        try:
            from google.cloud import storage
        except ImportError:
            raise BentoMLException(
                '"google-cloud-storage" package is required. You can install it with '
                'pip: "pip install google-cloud-storage"'
            )

        gcs = storage.Client()
        fileobj = io.BytesIO()
        gcs.download_blob_to_file(bundle_path, fileobj)
        fileobj.seek(0, 0)
    elif _is_http_url(bundle_path):
        import requests

        response = requests.get(bundle_path)
        if response.status_code != 200:
            raise BentoMLException(
                f"Error retrieving BentoService bundle. "
                f"{response.status_code}: {response.text}"
            )
        fileobj = io.BytesIO()
        fileobj.write(response.content)
        fileobj.seek(0, 0)
    else:
        raise BentoMLException(f"Saved bundle path: '{bundle_path}' is not supported")

    with tarfile.open(mode="r:gz", fileobj=fileobj) as tar:
        with tempfile.TemporaryDirectory() as tmpdir:
            filename = tar.getmembers()[0].name
            tar.extractall(path=tmpdir)
            yield os.path.join(tmpdir, filename)


def resolve_remote_bundle(func):
    """Decorate a function to handle remote bundles."""

    @wraps(func)
    def wrapper(bundle_path, *args):
        if _is_remote_path(bundle_path):
            with _resolve_remote_bundle_path(bundle_path) as local_bundle_path:
                return func(local_bundle_path, *args)

        return func(bundle_path, *args)

    return wrapper


@resolve_remote_bundle
def load_saved_bundle_config(bundle_path) -> "SavedBundleConfig":
    try:
        return SavedBundleConfig.load(os.path.join(bundle_path, "bentoml.yml"))
    except FileNotFoundError:
        raise BentoMLException(
            "BentoML can't locate config file 'bentoml.yml'"
            " in saved bundle in path: {}".format(bundle_path)
        )


def load_bento_service_metadata(bundle_path: str) -> "BentoServiceMetadata":
    return load_saved_bundle_config(bundle_path).get_bento_service_metadata_pb()


def _find_module_file(bundle_path, service_name, module_file):
    # Simply join full path when module_file is just a file name,
    # e.g. module_file=="iris_classifier.py"
    module_file_path = os.path.join(bundle_path, service_name, module_file)
    if not os.path.isfile(module_file_path):
        # Try loading without service_name prefix, for loading from a installed PyPi
        module_file_path = os.path.join(bundle_path, module_file)

    # When module_file is located in sub directory
    # e.g. module_file=="foo/bar/iris_classifier.py"
    # This needs to handle the path differences between posix and windows platform:
    if not os.path.isfile(module_file_path):
        if sys.platform == "win32":
            # Try load a saved bundle created from posix platform on windows
            module_file_path = os.path.join(
                bundle_path, service_name, str(PurePosixPath(module_file))
            )
            if not os.path.isfile(module_file_path):
                module_file_path = os.path.join(
                    bundle_path, str(PurePosixPath(module_file))
                )
        else:
            # Try load a saved bundle created from windows platform on posix
            module_file_path = os.path.join(
                bundle_path, service_name, PureWindowsPath(module_file).as_posix()
            )
            if not os.path.isfile(module_file_path):
                module_file_path = os.path.join(
                    bundle_path, PureWindowsPath(module_file).as_posix()
                )

    if not os.path.isfile(module_file_path):
        raise BentoMLException(
            "Can not locate module_file {} in saved bundle {}".format(
                module_file, bundle_path
            )
        )

    return module_file_path


@resolve_remote_bundle
def load_bento_service_class(bundle_path):
    """
    Load a BentoService class from saved bundle in given path

    :param bundle_path: A path to Bento files generated from BentoService#save,
        #save_to_dir, or the path to pip installed BentoService directory
    :return: BentoService class
    """
    config = load_saved_bundle_config(bundle_path)
    metadata = config["metadata"]

    # Find and load target module containing BentoService class from given path
    module_file_path = _find_module_file(
        bundle_path, metadata["service_name"], metadata["module_file"]
    )

    # Prepend bundle_path to sys.path for loading extra python dependencies
    sys.path.insert(0, bundle_path)
    sys.path.insert(0, os.path.join(bundle_path, metadata["service_name"]))
    # Include zipimport modules
    zipimport_dir = os.path.join(bundle_path, metadata["service_name"], ZIPIMPORT_DIR)
    if os.path.exists(zipimport_dir):
        for p in os.listdir(zipimport_dir):
            logger.debug('adding %s to sys.path', p)
            sys.path.insert(0, os.path.join(zipimport_dir, p))

    module_name = metadata["module_name"]
    if module_name in sys.modules:
        logger.warning(
            "Module `%s` already loaded, using existing imported module.", module_name
        )
        module = sys.modules[module_name]
    elif sys.version_info >= (3, 5):
        import importlib.util

        spec = importlib.util.spec_from_file_location(module_name, module_file_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
    elif sys.version_info >= (3, 3):
        from importlib.machinery import SourceFileLoader

        # pylint:disable=deprecated-method
        module = SourceFileLoader(module_name, module_file_path).load_module(
            module_name
        )
        # pylint:enable=deprecated-method
    else:
        raise BentoMLException("BentoML requires Python 3.4 and above")

    # Remove bundle_path from sys.path to avoid import naming conflicts
    sys.path.remove(bundle_path)

    model_service_class = module.__getattribute__(metadata["service_name"])
    # Set _bento_service_bundle_path, where BentoService will load its artifacts
    model_service_class._bento_service_bundle_path = bundle_path
    # Set cls._version, service instance can access it via svc.version
    model_service_class._bento_service_bundle_version = metadata["service_version"]

    if (
        model_service_class._env
        and model_service_class._env._requirements_txt_file is not None
    ):
        # Load `requirement.txt` from bundle directory instead of the user-provided
        # file path, which may only available during the bundle save process
        model_service_class._env._requirements_txt_file = os.path.join(
            bundle_path, "requirements.txt"
        )

    return model_service_class


@resolve_remote_bundle
def safe_retrieve(bundle_path: str, target_dir: str):
    """Safely retrieve bento service to local path

    Args:
        bundle_path (:obj:`str`):
            The path that contains saved BentoService bundle, supporting
            both local file path and s3 path
        target_dir (:obj:`str`):
            Where the service contents should end up.

    Returns:
        :obj:`str`: location of safe local path
    """
    shutil.copytree(bundle_path, target_dir)


@resolve_remote_bundle
def load_from_dir(bundle_path):
    """Load bento service from local file path or s3 path

    Args:
        bundle_path (str): The path that contains saved BentoService bundle,
            supporting both local file path and s3 path

    Returns:
        bentoml.service.BentoService: a loaded BentoService instance
    """
    svc_cls = load_bento_service_class(bundle_path)
    return svc_cls()


@resolve_remote_bundle
def load_bento_service_api(bundle_path, api_name=None):
    bento_service = load_from_dir(bundle_path)
    return bento_service.get_inference_api(api_name)

[((37, 9, 37, 36), 'logging.getLogger', 'logging.getLogger', ({(37, 27, 37, 35): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((55, 7, 55, 29), 'bentoml.utils.s3.is_s3_url', 'is_s3_url', ({(55, 17, 55, 28): 'bundle_path'}, {}), '(bundle_path)', False, 'from bentoml.utils.s3 import is_s3_url\n'), ((104, 5, 104, 16), 'functools.wraps', 'wraps', ({(104, 11, 104, 15): 'func'}, {}), '(func)', False, 'from functools import wraps\n'), ((133, 23, 133, 75), 'os.path.join', 'os.path.join', ({(133, 36, 133, 47): 'bundle_path', (133, 49, 133, 61): 'service_name', (133, 63, 133, 74): 'module_file'}, {}), '(bundle_path, service_name, module_file)', False, 'import os\n'), ((189, 4, 189, 35), 'sys.path.insert', 'sys.path.insert', ({(189, 20, 189, 21): '(0)', (189, 23, 189, 34): 'bundle_path'}, {}), '(0, bundle_path)', False, 'import sys\n'), ((192, 20, 192, 86), 'os.path.join', 'os.path.join', ({(192, 33, 192, 44): 'bundle_path', (192, 46, 192, 70): "metadata['service_name']", (192, 72, 192, 85): 'ZIPIMPORT_DIR'}, {}), "(bundle_path, metadata['service_name'], ZIPIMPORT_DIR)", False, 'import os\n'), ((193, 7, 193, 36), 'os.path.exists', 'os.path.exists', ({(193, 22, 193, 35): 'zipimport_dir'}, {}), '(zipimport_dir)', False, 'import os\n'), ((222, 4, 222, 32), 'sys.path.remove', 'sys.path.remove', ({(222, 20, 222, 31): 'bundle_path'}, {}), '(bundle_path)', False, 'import sys\n'), ((257, 4, 257, 44), 'shutil.copytree', 'shutil.copytree', ({(257, 20, 257, 31): 'bundle_path', (257, 33, 257, 43): 'target_dir'}, {}), '(bundle_path, target_dir)', False, 'import shutil\n'), ((58, 21, 58, 42), 'urllib.parse.urlparse', 'urlparse', ({(58, 30, 58, 41): 'bundle_path'}, {}), '(bundle_path)', False, 'from urllib.parse import urlparse\n'), ((62, 13, 62, 31), 'boto3.client', 'boto3.client', ({(62, 26, 62, 30): '"""s3"""'}, {}), "('s3')", False, 'import boto3\n'), ((63, 18, 63, 30), 'io.BytesIO', 'io.BytesIO', ({}, {}), '()', False, 'import io\n'), ((66, 9, 66, 32), 'bentoml.utils.gcs.is_gcs_url', 'is_gcs_url', ({(66, 20, 66, 31): 'bundle_path'}, {}), '(bundle_path)', False, 'from bentoml.utils.gcs import is_gcs_url\n'), ((94, 9, 94, 51), 'tarfile.open', 'tarfile.open', (), '', False, 'import tarfile\n'), ((134, 11, 134, 43), 'os.path.isfile', 'os.path.isfile', ({(134, 26, 134, 42): 'module_file_path'}, {}), '(module_file_path)', False, 'import os\n'), ((136, 27, 136, 65), 'os.path.join', 'os.path.join', ({(136, 40, 136, 51): 'bundle_path', (136, 53, 136, 64): 'module_file'}, {}), '(bundle_path, module_file)', False, 'import os\n'), ((141, 11, 141, 43), 'os.path.isfile', 'os.path.isfile', ({(141, 26, 141, 42): 'module_file_path'}, {}), '(module_file_path)', False, 'import os\n'), ((161, 11, 161, 43), 'os.path.isfile', 'os.path.isfile', ({(161, 26, 161, 42): 'module_file_path'}, {}), '(module_file_path)', False, 'import os\n'), ((190, 23, 190, 74), 'os.path.join', 'os.path.join', ({(190, 36, 190, 47): 'bundle_path', (190, 49, 190, 73): "metadata['service_name']"}, {}), "(bundle_path, metadata['service_name'])", False, 'import os\n'), ((194, 17, 194, 42), 'os.listdir', 'os.listdir', ({(194, 28, 194, 41): 'zipimport_dir'}, {}), '(zipimport_dir)', False, 'import os\n'), ((236, 58, 238, 9), 'os.path.join', 'os.path.join', ({(237, 12, 237, 23): 'bundle_path', (237, 25, 237, 43): '"""requirements.txt"""'}, {}), "(bundle_path, 'requirements.txt')", False, 'import os\n'), ((49, 8, 49, 30), 'bentoml.utils.s3.is_s3_url', 'is_s3_url', ({(49, 18, 49, 29): 'bundle_path'}, {}), '(bundle_path)', False, 'from bentoml.utils.s3 import is_s3_url\n'), ((49, 34, 49, 57), 'bentoml.utils.gcs.is_gcs_url', 'is_gcs_url', ({(49, 45, 49, 56): 'bundle_path'}, {}), '(bundle_path)', False, 'from bentoml.utils.gcs import is_gcs_url\n'), ((75, 14, 75, 30), 'google.cloud.storage.Client', 'storage.Client', ({}, {}), '()', False, 'from google.cloud import storage\n'), ((76, 18, 76, 30), 'io.BytesIO', 'io.BytesIO', ({}, {}), '()', False, 'import io\n'), ((95, 13, 95, 42), 'tempfile.TemporaryDirectory', 'tempfile.TemporaryDirectory', ({}, {}), '()', False, 'import tempfile\n'), ((118, 38, 118, 78), 'os.path.join', 'os.path.join', ({(118, 51, 118, 62): 'bundle_path', (118, 64, 118, 77): '"""bentoml.yml"""'}, {}), "(bundle_path, 'bentoml.yml')", False, 'import os\n'), ((42, 15, 42, 36), 'urllib.parse.urlparse', 'urlparse', ({(42, 24, 42, 35): 'bundle_path'}, {}), '(bundle_path)', False, 'from urllib.parse import urlparse\n'), ((82, 19, 82, 44), 'requests.get', 'requests.get', ({(82, 32, 82, 43): 'bundle_path'}, {}), '(bundle_path)', False, 'import requests\n'), ((88, 18, 88, 30), 'io.BytesIO', 'io.BytesIO', ({}, {}), '()', False, 'import io\n'), ((92, 14, 92, 86), 'bentoml.exceptions.BentoMLException', 'BentoMLException', ({(92, 31, 92, 85): 'f"""Saved bundle path: \'{bundle_path}\' is not supported"""'}, {}), '(f"Saved bundle path: \'{bundle_path}\' is not supported")', False, 'from bentoml.exceptions import BentoMLException\n'), ((98, 18, 98, 48), 'os.path.join', 'os.path.join', ({(98, 31, 98, 37): 'tmpdir', (98, 39, 98, 47): 'filename'}, {}), '(tmpdir, filename)', False, 'import os\n'), ((147, 19, 147, 51), 'os.path.isfile', 'os.path.isfile', ({(147, 34, 147, 50): 'module_file_path'}, {}), '(module_file_path)', False, 'import os\n'), ((156, 19, 156, 51), 'os.path.isfile', 'os.path.isfile', ({(156, 34, 156, 50): 'module_file_path'}, {}), '(module_file_path)', False, 'import os\n'), ((196, 31, 196, 61), 'os.path.join', 'os.path.join', ({(196, 44, 196, 57): 'zipimport_dir', (196, 59, 196, 60): 'p'}, {}), '(zipimport_dir, p)', False, 'import os\n'), ((219, 14, 219, 71), 'bentoml.exceptions.BentoMLException', 'BentoMLException', ({(219, 31, 219, 70): '"""BentoML requires Python 3.4 and above"""'}, {}), "('BentoML requires Python 3.4 and above')", False, 'from bentoml.exceptions import BentoMLException\n'), ((70, 18, 73, 13), 'bentoml.exceptions.BentoMLException', 'BentoMLException', ({(71, 16, 72, 57): '""""google-cloud-storage" package is required. You can install it with pip: "pip install google-cloud-storage\\""""'}, {}), '(\n    \'"google-cloud-storage" package is required. You can install it with pip: "pip install google-cloud-storage"\'\n    )', False, 'from bentoml.exceptions import BentoMLException\n'), ((84, 18, 87, 13), 'bentoml.exceptions.BentoMLException', 'BentoMLException', ({(85, 16, 86, 58): 'f"""Error retrieving BentoService bundle. {response.status_code}: {response.text}"""'}, {}), "(\n    f'Error retrieving BentoService bundle. {response.status_code}: {response.text}'\n    )", False, 'from bentoml.exceptions import BentoMLException\n'), ((145, 47, 145, 73), 'pathlib.PurePosixPath', 'PurePosixPath', ({(145, 61, 145, 72): 'module_file'}, {}), '(module_file)', False, 'from pathlib import PureWindowsPath, PurePosixPath\n'), ((149, 37, 149, 63), 'pathlib.PurePosixPath', 'PurePosixPath', ({(149, 51, 149, 62): 'module_file'}, {}), '(module_file)', False, 'from pathlib import PureWindowsPath, PurePosixPath\n'), ((154, 43, 154, 71), 'pathlib.PureWindowsPath', 'PureWindowsPath', ({(154, 59, 154, 70): 'module_file'}, {}), '(module_file)', False, 'from pathlib import PureWindowsPath, PurePosixPath\n'), ((214, 17, 214, 64), 'importlib.machinery.SourceFileLoader', 'SourceFileLoader', ({(214, 34, 214, 45): 'module_name', (214, 47, 214, 63): 'module_file_path'}, {}), '(module_name, module_file_path)', False, 'from importlib.machinery import SourceFileLoader\n'), ((158, 33, 158, 61), 'pathlib.PureWindowsPath', 'PureWindowsPath', ({(158, 49, 158, 60): 'module_file'}, {}), '(module_file)', False, 'from pathlib import PureWindowsPath, PurePosixPath\n')]