def _init_once()

in libcloud/__init__.py [0:0]


def _init_once():
    """
    Utility function that is ran once on Library import.

    This checks for the LIBCLOUD_DEBUG environment variable, which if it exists
    is where we will log debug information about the provider transports.

    This also checks for known environment/dependency incompatibilities.
    """
    path = os.getenv("LIBCLOUD_DEBUG")

    if path:
        mode = "a"

        # Special case for /dev/stderr and /dev/stdout on Python 3.
        from libcloud.utils.py3 import PY3

        # Opening those files in append mode will throw "illegal seek"
        # exception there.
        # Late import to avoid setup.py related side affects
        if path in ["/dev/stderr", "/dev/stdout"] and PY3:
            mode = "w"

        fo = codecs.open(path, mode, encoding="utf8")
        enable_debug(fo)

        # NOTE: We use lazy import to avoid unnecessary import time overhead
        try:
            import paramiko  # NOQA

            have_paramiko = True
        except ImportError:
            have_paramiko = False

        if have_paramiko and hasattr(paramiko.util, "log_to_file"):
            import logging

            # paramiko always tries to open file path in append mode which
            # won't work with /dev/{stdout, stderr} so we just ignore those
            # errors
            try:
                paramiko.util.log_to_file(filename=path, level=logging.DEBUG)
            except OSError as e:
                if "illegal seek" not in str(e).lower():
                    raise e

    # check for broken `yum install python-requests`
    if have_requests and requests.__version__ == "2.6.0":
        chardet_version = requests.packages.chardet.__version__
        required_chardet_version = "2.3.0"
        assert chardet_version == required_chardet_version, (
            "Known bad version of requests detected! This can happen when "
            "requests was installed from a source other than PyPI, e.g. via "
            "a package manager such as yum. Please either install requests "
            "from PyPI or run `pip install chardet==%s` to resolve this "
            "issue." % required_chardet_version
        )