def test_user_cache_dir_osx(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") assert appdirs.user_cache_dir("pip") == "/home/test/Library/Caches/pip"
def test_user_cache_dir_linux_home_slash( self, monkeypatch: pytest.MonkeyPatch) -> None: # Verify that we are not affected by https://bugs.python.org/issue14768 monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.setenv("HOME", "/") assert appdirs.user_cache_dir("pip") == "/.cache/pip"
def test_user_cache_dir_osx(self, monkeypatch): monkeypatch.setattr(_appdirs, "system", "darwin") monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") assert appdirs.user_cache_dir("pip") == "/home/test/Library/Caches/pip"
def virtualenv(tmpdir, monkeypatch, isolate): """ Return a virtual environment which is unique to each test function invocation created inside of a sub directory of the test function's temporary directory. The returned object is a ``tests.lib.venv.VirtualEnvironment`` object. """ # Force shutil to use the older method of rmtree that didn't use the fd # functions. These seem to fail on Travis (and only on Travis). monkeypatch.setattr(shutil, "_use_fd_functions", False, raising=False) # Copy over our source tree so that each virtual environment is self # contained pip_src = tmpdir.join("pip_src").abspath shutil.copytree( SRC_DIR, pip_src, ignore=shutil.ignore_patterns( "*.pyc", "__pycache__", "contrib", "docs", "tasks", "*.txt", "tests", "pip.egg-info", "build", "dist", ".tox", ".git", ), ) # Create the virtual environment venv = VirtualEnvironment.create( tmpdir.join("workspace", "venv"), pip_source_dir=pip_src, ) # Clean out our cache: creating the venv injects wheels into it. if os.path.exists(appdirs.user_cache_dir("pip")): shutil.rmtree(appdirs.user_cache_dir("pip")) # Undo our monkeypatching of shutil monkeypatch.undo() return venv
def test_user_cache_dir_linux_override(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_CACHE_HOME", "/home/test/.other-cache") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") assert appdirs.user_cache_dir("pip") == "/home/test/.other-cache/pip"
def test_user_cache_dir_linux_override(self, monkeypatch): monkeypatch.setattr(_appdirs, "system", "linux2") monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_CACHE_HOME", "/home/test/.other-cache") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") assert appdirs.user_cache_dir("pip") == "/home/test/.other-cache/pip"
def test_user_cache_dir_linux(self, monkeypatch): monkeypatch.setattr(_appdirs, "system", "linux2") monkeypatch.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") assert appdirs.user_cache_dir("pip") == "/home/test/.cache/pip"
def test_user_cache_dir_linux_home_slash(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setattr(os, "path", posixpath) # Verify that we are not affected by http://bugs.python.org/issue14768 monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.setenv("HOME", "/") monkeypatch.setattr(sys, "platform", "linux2") assert appdirs.user_cache_dir("pip") == "/.cache/pip"
def test_user_cache_dir_linux_home_slash(self, monkeypatch): monkeypatch.setattr(_appdirs, "system", "linux2") monkeypatch.setattr(os, "path", posixpath) # Verify that we are not affected by https://bugs.python.org/issue14768 monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.setenv("HOME", "/") monkeypatch.setattr(sys, "platform", "linux2") assert appdirs.user_cache_dir("pip") == "/.cache/pip"
def test_user_cache_dir_win(self, monkeypatch: pytest.MonkeyPatch) -> None: _get_win_folder = mock.Mock( return_value="C:\\Users\\test\\AppData\\Local") monkeypatch.setattr( platformdirs.windows, # type: ignore "get_win_folder", _get_win_folder, raising=False, ) assert (appdirs.user_cache_dir("pip") == "C:\\Users\\test\\AppData\\Local\\pip\\Cache") assert _get_win_folder.call_args_list == [ mock.call("CSIDL_LOCAL_APPDATA") ]
def test_user_cache_dir_win(self, monkeypatch): @pretend.call_recorder def _get_win_folder(base): return "C:\\Users\\test\\AppData\\Local" monkeypatch.setattr( _appdirs, "_get_win_folder", _get_win_folder, raising=False, ) monkeypatch.setattr(_appdirs, "system", "win32") monkeypatch.setattr(os, "path", ntpath) assert (appdirs.user_cache_dir("pip") == "C:\\Users\\test\\AppData\\Local\\pip\\Cache") assert _get_win_folder.calls == [pretend.call("CSIDL_LOCAL_APPDATA")]
def test_user_cache_dir_win(self, monkeypatch): @pretend.call_recorder def _get_win_folder(base): return "C:\\Users\\test\\AppData\\Local" monkeypatch.setattr( appdirs, "_get_win_folder", _get_win_folder, raising=False, ) monkeypatch.setattr(appdirs, "WINDOWS", True) monkeypatch.setattr(os, "path", ntpath) assert (appdirs.user_cache_dir("pip") == "C:\\Users\\test\\AppData\\Local\\pip\\Cache") assert _get_win_folder.calls == [pretend.call("CSIDL_LOCAL_APPDATA")]
def test_user_cache_dir_unicode(self, monkeypatch): if sys.platform != 'win32': return def my_get_win_folder(csidl_name): return u"\u00DF\u00E4\u03B1\u20AC" monkeypatch.setattr(appdirs, "_get_win_folder", my_get_win_folder) # Do not use the isinstance expression directly in the # assert statement, as the Unicode characters in the result # cause pytest to fail with an internal error on Python 2.7 result_is_str = isinstance(appdirs.user_cache_dir('test'), str) assert result_is_str, "user_cache_dir did not return a str" # Test against regression #3463 from pip._internal import create_main_parser create_main_parser().print_help() # This should not crash
def test_user_cache_dir_unicode(self, monkeypatch): if sys.platform != 'win32': return def my_get_win_folder(csidl_name): return u"\u00DF\u00E4\u03B1\u20AC" monkeypatch.setattr(_appdirs, "_get_win_folder", my_get_win_folder) # Do not use the isinstance expression directly in the # assert statement, as the Unicode characters in the result # cause pytest to fail with an internal error on Python 2.7 result_is_str = isinstance(appdirs.user_cache_dir('test'), str) assert result_is_str, "user_cache_dir did not return a str" # Test against regression #3463 from pip._internal.cli.main_parser import create_main_parser create_main_parser().print_help() # This should not crash
def test_install_builds_wheels(script, data, common_wheels): # NB This incidentally tests a local tree + tarball inputs # see test_install_editable_from_git_autobuild_wheel for editable # vcs coverage. script.pip('install', 'wheel', '--no-index', '-f', common_wheels) to_install = data.packages.join('requires_wheelbroken_upper') res = script.pip('install', '--no-index', '-f', data.find_links, '-f', common_wheels, to_install, expect_stderr=True) expected = ("Successfully installed requires-wheelbroken-upper-0" " upper-2.0 wheelbroken-0.1") # Must have installed it all assert expected in str(res), str(res) root = appdirs.user_cache_dir('pip') wheels = [] for top, dirs, files in os.walk(os.path.join(root, "wheels")): wheels.extend(files) # and built wheels for upper and wheelbroken assert "Running setup.py bdist_wheel for upper" in str(res), str(res) assert "Running setup.py bdist_wheel for wheelb" in str(res), str(res) # But not requires_wheel... which is a local dir and thus uncachable. assert "Running setup.py bdist_wheel for requir" not in str(res), str(res) # wheelbroken has to run install # into the cache assert wheels != [], str(res) # and installed from the wheel assert "Running setup.py install for upper" not in str(res), str(res) # the local tree can't build a wheel (because we can't assume that every # build will have a suitable unique key to cache on). assert "Running setup.py install for requires-wheel" in str(res), str(res) # wheelbroken has to run install assert "Running setup.py install for wheelb" in str(res), str(res) # We want to make sure we used the correct implementation tag assert wheels == [ "Upper-2.0-{0}-none-any.whl".format(pep425tags.implementation_tag), ]
def test_install_builds_wheels(script, data, common_wheels): # NB This incidentally tests a local tree + tarball inputs # see test_install_editable_from_git_autobuild_wheel for editable # vcs coverage. script.pip('install', 'wheel', '--no-index', '-f', common_wheels) to_install = data.packages.join('requires_wheelbroken_upper') res = script.pip( 'install', '--no-index', '-f', data.find_links, '-f', common_wheels, to_install, expect_stderr=True) expected = ("Successfully installed requires-wheelbroken-upper-0" " upper-2.0 wheelbroken-0.1") # Must have installed it all assert expected in str(res), str(res) root = appdirs.user_cache_dir('pip') wheels = [] for top, dirs, files in os.walk(os.path.join(root, "wheels")): wheels.extend(files) # and built wheels for upper and wheelbroken assert "Running setup.py bdist_wheel for upper" in str(res), str(res) assert "Running setup.py bdist_wheel for wheelb" in str(res), str(res) # But not requires_wheel... which is a local dir and thus uncachable. assert "Running setup.py bdist_wheel for requir" not in str(res), str(res) # wheelbroken has to run install # into the cache assert wheels != [], str(res) # and installed from the wheel assert "Running setup.py install for upper" not in str(res), str(res) # the local tree can't build a wheel (because we can't assume that every # build will have a suitable unique key to cache on). assert "Running setup.py install for requires-wheel" in str(res), str(res) # wheelbroken has to run install assert "Running setup.py install for wheelb" in str(res), str(res) # We want to make sure we used the correct implementation tag assert wheels == [ "Upper-2.0-{0}-none-any.whl".format(pep425tags.implementation_tag), ]
def test_user_cache_dir_unicode(self, monkeypatch: pytest.MonkeyPatch) -> None: if sys.platform != "win32": return def my_get_win_folder(csidl_name: str) -> str: return "\u00DF\u00E4\u03B1\u20AC" monkeypatch.setattr( platformdirs.windows, # type: ignore "get_win_folder", my_get_win_folder, ) # Do not use the isinstance expression directly in the # assert statement, as the Unicode characters in the result # cause pytest to fail with an internal error on Python 2.7 result_is_str = isinstance(appdirs.user_cache_dir("test"), str) assert result_is_str, "user_cache_dir did not return a str" # Test against regression #3463 from pip._internal.cli.main_parser import create_main_parser create_main_parser().print_help() # This should not crash
def test_user_cache_dir_osx(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("HOME", "/home/test") assert appdirs.user_cache_dir("pip") == "/home/test/Library/Caches/pip"
from distutils.command.install import SCHEME_KEYS # type: ignore from distutils.command.install import install as distutils_install_command from pip._internal.models.scheme import Scheme from pip._internal.utils import appdirs from pip._internal.utils.compat import WINDOWS from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast from pip._internal.utils.virtualenv import running_under_virtualenv if MYPY_CHECK_RUNNING: from typing import Dict, List, Optional, Union from distutils.cmd import Command as DistutilsCommand # Application Directories USER_CACHE_DIR = appdirs.user_cache_dir("pip") def get_major_minor_version(): # type: () -> str """ Return the major-minor version of the current Python as a string, e.g. "3.7" or "3.10". """ return '{}.{}'.format(*sys.version_info) def get_src_prefix(): # type: () -> str if running_under_virtualenv(): src_prefix = os.path.join(sys.prefix, 'src')
from pip._internal.utils import appdirs @click.group() def cli(): """Custodian Python Packaging Utility some simple tooling to sync poetry files to setup/pip """ # If there is a global installation of poetry, prefer that. poetry_python_lib = os.path.expanduser('~/.poetry/lib') sys.path.append(os.path.realpath(poetry_python_lib)) @cli.command() @click.option('--cache', default=appdirs.user_cache_dir('pip')) @click.option('--link-dir', type=click.Path()) def gen_links(cache, link_dir): # wheel only # # generate a find links directory to perform an install offline. # note there we still need to download any packages needed for # an offline install. this is effectively an alternative to # pip download -d to utilize already cached wheel resources. # found = {} link_dir = Path(link_dir) wrote = 0 for root, dirs, files in os.walk(cache): for f in files: if not f.endswith('whl'):
import site import sys import sysconfig from distutils import sysconfig as distutils_sysconfig from distutils.command.install import SCHEME_KEYS # type: ignore from pip._internal.utils import appdirs from pip._internal.utils.compat import WINDOWS, expanduser from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: from typing import Any, Union, Dict, List, Optional # Application Directories USER_CACHE_DIR = appdirs.user_cache_dir("pip") DELETE_MARKER_MESSAGE = '''\ This file is placed here by pip to indicate the source was put here by pip. Once this package is successfully installed this source code will be deleted (unless you remove this file). ''' PIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt' def write_delete_marker_file(directory): # type: (str) -> None """
from joblib import Memory from pip._internal.utils.appdirs import user_cache_dir memory = Memory(cachedir=user_cache_dir('pip'), verbose=0) big_triangle = """75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23""" def triangle_array(triangle): array = [line.split(" ") for line in triangle.split("\n")] sub_arrays = [] for l in array: sub_arrays.append(list(map(int, l))) return sub_arrays @memory.cache def max_path(array): if len(array) == 1:
if len(obj) == 1: return _normalize_type(next(iter(obj))) else: return tuple(_normalize_type(o) for o in obj) else: try: num = complex(obj) if round_digits: num = complex(round(num.real, round_digits), round(num.imag, round_digits)) return num except TypeError: return obj # Memoization decorator. CACHE_DIR = user_cache_dir('amphybio') os.makedirs(CACHE_DIR, exist_ok=True) @decorator_with_options def memoized(func, *, size_limit=10**8, eviction_policy='least-recently-used', cache_dir=CACHE_DIR, typed=False, round_digits=15, ignore_args=None): """Persistent memoization function decorator with argument normalization and ignore list. :func: a callable object that is not a method :size_limit: (int, in bytes) approximate size limit of cache - default 100 MB :eviction_policy: rule to evict cache if size_limit is reached, any of diskcache.EVICTION_POLICY :cache_dir: location (directory path) of persistent cache files :typed: wheter to consider lists of identically valued arguments of different types as different arguments lists :round_digits: number of digits to round to, pass False to disable rounding
def test_user_cache_dir_linux(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.setenv("HOME", "/home/test") assert appdirs.user_cache_dir("pip") == "/home/test/.cache/pip"
def test_user_cache_dir_linux_override( self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("XDG_CACHE_HOME", "/home/test/.other-cache") monkeypatch.setenv("HOME", "/home/test") assert appdirs.user_cache_dir("pip") == "/home/test/.other-cache/pip"
from pip._internal.utils.appdirs import user_cache_dir # since pip v.10 print(user_cache_dir('pip')) print(user_cache_dir('wheel'))
import os from shutil import rmtree from pip._internal.utils.appdirs import user_cache_dir from .click import secho # The user_cache_dir helper comes straight from pip itself CACHE_DIR = user_cache_dir("pip-tools") # NOTE # We used to store the cache dir under ~/.pip-tools, which is not the # preferred place to store caches for any platform. This has been addressed # in pip-tools==1.0.5, but to be good citizens, we point this out explicitly # to the user when this directory is still found. LEGACY_CACHE_DIR = os.path.expanduser("~/.pip-tools") if os.path.exists(LEGACY_CACHE_DIR): secho( "Removing old cache dir {} (new cache dir is {})".format( LEGACY_CACHE_DIR, CACHE_DIR), fg="yellow", ) rmtree(LEGACY_CACHE_DIR)