# Path: venv_update.py
# def colorize(cmd):
#     from os import isatty
# 
#     if isatty(1):
#         template = '\033[36m>\033[m \033[32m{0}\033[m'
#     else:
#         template = '> {0}'
# 
#     return template.format(shellescape(cmd))
# 
# Path: venv_update.py
# def raise_on_failure(mainfunc, ignore_return=False):
#     """raise if and only if mainfunc fails"""
#     try:
#         errors = mainfunc()
#         if not ignore_return and errors:
#             exit(errors)
#     except CalledProcessError as error:
#         exit(error.returncode)
#     except SystemExit as error:
#         if error.code:
#             raise
#     except KeyboardInterrupt:  # I don't plan to test-cover this.  :pragma:nocover:
#         exit(1)
# 
# Path: venv_update.py
# def timid_relpath(arg):
#     """convert an argument to a relative path, carefully"""
#     # TODO-TEST: unit tests
#     from os.path import isabs, relpath, sep
#     if isabs(arg):
#         result = relpath(arg)
#         if result.count(sep) + 1 < arg.count(sep):
#             return result
# 
#     return arg
# 
# Path: venv_update.py
# def user_cache_dir():
#     # stolen from pip.utils.appdirs.user_cache_dir
#     from os import getenv
#     from os.path import expanduser
#     return getenv('XDG_CACHE_HOME', expanduser('~/.cache'))


import errno
import glob
import os
import random
import re
import shutil
import sys
import pip as pipmodule
    import pkg_resources
from contextlib import contextmanager
from pip._internal import logger
from pip._internal.commands.install import InstallCommand
from pip._internal.exceptions import DistributionNotFound
from pip._internal.exceptions import InstallationError
from pip._internal.index import BestVersionAlreadyInstalled
from pip._internal.index import HTMLPage
from pip._internal.index import Link
from pip._internal.index import PackageFinder
from pip._internal.req import InstallRequirement
from pip._internal.wheel import Wheel
from venv_update import colorize
from venv_update import raise_on_failure
from venv_update import timid_relpath
from venv_update import user_cache_dir
    from pip._vendor import pkg_resources
    from pip._internal.req.constructors import install_req_from_line
    from sys import stdout
        from pip._internal.operations.freeze import FrozenRequirement
        from pip import FrozenRequirement
    from pip._internal.utils.misc import dist_is_local
    from copy import copy
    from collections import deque
        from pip._internal.cli import base_command
        from pip._internal import basecommand as base_command
    from pip._internal import download
    .cache/pip-faster/wheelhouse/$index_url/$wheel
    """
    for installed_package in installed_packages:
        if not _can_be_cached(installed_package):
            continue
        _store_wheel_in_cache(installed_package.link.path, index_url)


def get_patched_download_http_url(orig_download_http_url, index_urls):
    def pipfaster_download_http_url(link, *args, **kwargs):
        file_path, content_type = orig_download_http_url(link, *args, **kwargs)
        if link.is_wheel:
            for index_url in index_urls:
                if (
                        # pip <18.1
                        isinstance(link.comes_from, HTMLPage) and
                        link.comes_from.url.startswith(index_url)
                ) or (
                        # pip >= 18.1
                        isinstance(link.comes_from, (str, type(''))) and
                        link.comes_from.startswith(index_url)
                ):
                    _store_wheel_in_cache(file_path, index_url)
                    break
        return file_path, content_type
    return pipfaster_download_http_url


def pip(args):
    """Run pip, in-process."""

# Path: venv_update.py
# def colorize(cmd):
#     from os import isatty
# 
#     if isatty(1):
#         template = '\033[36m>\033[m \033[32m{0}\033[m'
#     else:
#         template = '> {0}'
# 
#     return template.format(shellescape(cmd))
# 
# Path: venv_update.py
# def raise_on_failure(mainfunc, ignore_return=False):
#     """raise if and only if mainfunc fails"""
#     try:
#         errors = mainfunc()
#         if not ignore_return and errors:
#             exit(errors)
#     except CalledProcessError as error:
#         exit(error.returncode)
#     except SystemExit as error:
#         if error.code:
#             raise
#     except KeyboardInterrupt:  # I don't plan to test-cover this.  :pragma:nocover:
#         exit(1)
# 
# Path: venv_update.py
# def timid_relpath(arg):
#     """convert an argument to a relative path, carefully"""
#     # TODO-TEST: unit tests
#     from os.path import isabs, relpath, sep
#     if isabs(arg):
#         result = relpath(arg)
#         if result.count(sep) + 1 < arg.count(sep):
#             return result
# 
#     return arg
# 
# Path: venv_update.py
# def user_cache_dir():
#     # stolen from pip.utils.appdirs.user_cache_dir
#     from os import getenv
#     from os.path import expanduser
#     return getenv('XDG_CACHE_HOME', expanduser('~/.cache'))


# Path: pip_faster.py
import errno
import glob
import os
import random
import re
import shutil
import sys
import pip as pipmodule
    import pkg_resources
from contextlib import contextmanager
from pip._internal import logger
from pip._internal.commands.install import InstallCommand
from pip._internal.exceptions import DistributionNotFound
from pip._internal.exceptions import InstallationError
from pip._internal.index import BestVersionAlreadyInstalled
from pip._internal.index import HTMLPage
from pip._internal.index import Link
from pip._internal.index import PackageFinder
from pip._internal.req import InstallRequirement
from pip._internal.wheel import Wheel
from venv_update import colorize
from venv_update import raise_on_failure
from venv_update import timid_relpath
from venv_update import user_cache_dir
    from pip._vendor import pkg_resources
    from pip._internal.req.constructors import install_req_from_line
    from sys import stdout
        from pip._internal.operations.freeze import FrozenRequirement
        from pip import FrozenRequirement
    from pip._internal.utils.misc import dist_is_local
    from copy import copy
    from collections import deque
        from pip._internal.cli import base_command
        from pip._internal import basecommand as base_command
    from pip._internal import download

    .cache/pip-faster/wheelhouse/$index_url/$wheel
    """
    for installed_package in installed_packages:
        if not _can_be_cached(installed_package):
            continue
        _store_wheel_in_cache(installed_package.link.path, index_url)


def get_patched_download_http_url(orig_download_http_url, index_urls):
    def pipfaster_download_http_url(link, *args, **kwargs):
        file_path, content_type = orig_download_http_url(link, *args, **kwargs)
        if link.is_wheel:
            for index_url in index_urls:
                if (
                        # pip <18.1
                        isinstance(link.comes_from, HTMLPage) and
                        link.comes_from.url.startswith(index_url)
                ) or (
                        # pip >= 18.1
                        isinstance(link.comes_from, (str, type(''))) and
                        link.comes_from.startswith(index_url)
                ):
                    _store_wheel_in_cache(file_path, index_url)
                    break
        return file_path, content_type
    return pipfaster_download_http_url


def pip(args):
    """Run pip, in-process."""

    stdout.write(colorize(('pip',) + args))