From ff32a68771212f789973a067c637b41af15e2963 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 8 Nov 2021 18:54:07 +0800 Subject: [PATCH] Remove some usages of vistir.compat --- pipenv/__init__.py | 2 +- pipenv/_compat.py | 7 +------ pipenv/core.py | 15 ++++++++------- pipenv/environment.py | 24 ++++++++++++------------ pipenv/project.py | 11 ++++++----- pipenv/shells.py | 2 +- pipenv/utils.py | 6 +----- tasks/vendoring/__init__.py | 8 ++++---- tests/integration/conftest.py | 14 +++++--------- tests/integration/test_dot_venv.py | 18 ++++++++++-------- tests/integration/test_install_basic.py | 6 ++++-- tests/integration/test_install_twists.py | 3 +-- tests/integration/test_install_uri.py | 7 ++++--- tests/integration/test_lock.py | 2 +- tests/integration/test_windows.py | 3 +-- tests/unit/test_core.py | 8 ++++---- 16 files changed, 64 insertions(+), 72 deletions(-) diff --git a/pipenv/__init__.py b/pipenv/__init__.py index 240e089e..aeb88a3e 100644 --- a/pipenv/__init__.py +++ b/pipenv/__init__.py @@ -19,7 +19,7 @@ sys.path.insert(0, PIPENV_VENDOR) sys.path.insert(0, PIPENV_PATCHED) from pipenv.vendor.urllib3.exceptions import DependencyWarning -from pipenv.vendor.vistir.compat import ResourceWarning, fs_str +from pipenv.vendor.vistir.compat import fs_str warnings.filterwarnings("ignore", category=DependencyWarning) warnings.filterwarnings("ignore", category=ResourceWarning) diff --git a/pipenv/_compat.py b/pipenv/_compat.py index c23c5e58..4c8b643f 100644 --- a/pipenv/_compat.py +++ b/pipenv/_compat.py @@ -6,17 +6,12 @@ operating systems, etc. import sys import warnings -import vistir - -from pipenv.vendor.vistir.compat import ( - NamedTemporaryFile, Path, ResourceWarning, TemporaryDirectory -) +from pipenv.vendor import vistir warnings.filterwarnings("ignore", category=ResourceWarning) __all__ = [ - "NamedTemporaryFile", "Path", "ResourceWarning", "TemporaryDirectory", "getpreferredencoding", "DEFAULT_ENCODING", "canonical_encoding_name", "force_encoding", "UNICODE_TO_ASCII_TRANSLATION_MAP", "decode_output", "fix_utf8" ] diff --git a/pipenv/core.py b/pipenv/core.py index 75fa9793..38929b83 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -1,6 +1,7 @@ import json as simplejson import logging import os +from pathlib import Path from posixpath import expandvars import sys import time @@ -896,7 +897,7 @@ def do_create_virtualenv(project, python=None, site_packages=None, pypi_mirror=N ) cmd = [ - vistir.compat.Path(sys.executable).absolute().as_posix(), + Path(sys.executable).absolute().as_posix(), "-m", "virtualenv", f"--prompt=({project.name}) ", @@ -1461,7 +1462,7 @@ def pip_install( pip_command.extend(prepare_pip_source_args(sources)) if project.s.is_verbose(): click.echo(f"$ {cmd_list_to_shell(pip_command)}", err=True) - cache_dir = vistir.compat.Path(project.s.PIPENV_CACHE_DIR) + cache_dir = Path(project.s.PIPENV_CACHE_DIR) DEFAULT_EXISTS_ACTION = "w" if selective_upgrade: DEFAULT_EXISTS_ACTION = "i" @@ -1487,7 +1488,7 @@ def pip_install( def pip_download(project, package_name): - cache_dir = vistir.compat.Path(project.s.PIPENV_CACHE_DIR) + cache_dir = Path(project.s.PIPENV_CACHE_DIR) pip_config = { "PIP_CACHE_DIR": vistir.misc.fs_str(cache_dir.as_posix()), "PIP_WHEEL_DIR": vistir.misc.fs_str(cache_dir.joinpath("wheels").as_posix()), @@ -2564,9 +2565,9 @@ def do_check( if not python: click.echo(crayons.red("The Python interpreter can't be found."), err=True) sys.exit(1) - _cmd = [vistir.compat.Path(python).as_posix()] + _cmd = [Path(python).as_posix()] # Run the PEP 508 checker in the virtualenv. - cmd = _cmd + [vistir.compat.Path(pep508checker_path).as_posix()] + cmd = _cmd + [Path(pep508checker_path).as_posix()] c = run_command(cmd, is_verbose=project.s.is_verbose()) if c.returncode is not None: try: @@ -2684,8 +2685,8 @@ def do_graph(project, bare=False, json=False, json_tree=False, reverse=False): pass else: if not os.name == 'nt': # bugfix #4388 - python_path = vistir.compat.Path(python_path).as_posix() - pipdeptree_path = vistir.compat.Path(pipdeptree_path).as_posix() + python_path = Path(python_path).as_posix() + pipdeptree_path = Path(pipdeptree_path).as_posix() if reverse and json: click.echo( diff --git a/pipenv/environment.py b/pipenv/environment.py index b07ef842..4eee3ded 100644 --- a/pipenv/environment.py +++ b/pipenv/environment.py @@ -6,7 +6,7 @@ import operator import os import site import sys - +from pathlib import Path from sysconfig import get_paths, get_python_version import pkg_resources @@ -53,7 +53,7 @@ class Environment: prefix = normalize_path(prefix) self._python = None if python is not None: - self._python = vistir.compat.Path(python).absolute().as_posix() + self._python = Path(python).absolute().as_posix() self.is_venv = is_venv or prefix != normalize_path(sys.prefix) if not sources: sources = [] @@ -66,7 +66,7 @@ class Environment: self.pipfile = pipfile self.extra_dists = [] prefix = prefix if prefix else sys.prefix - self.prefix = vistir.compat.Path(prefix) + self.prefix = Path(prefix) self._base_paths = {} if self.is_venv: self._base_paths = self.get_paths() @@ -139,7 +139,7 @@ class Environment: return py_version def find_libdir(self): - # type: () -> Optional[vistir.compat.Path] + # type: () -> Optional[Path] libdir = self.prefix / "lib" return next(iter(list(libdir.iterdir())), None) @@ -153,7 +153,7 @@ class Environment: include_path = include_dirs.get("include", include_dirs.get("platinclude")) if not include_path: return {} - include_dir = vistir.compat.Path(include_path) + include_dir = Path(include_path) python_path = next(iter(list(include_dir.iterdir())), None) if python_path and python_path.name.startswith("python"): python_version = python_path.name.replace("python", "") @@ -270,11 +270,11 @@ class Environment: if self._python is not None: return self._python if os.name == "nt" and not self.is_venv: - py = vistir.compat.Path(self.prefix).joinpath("python").absolute().as_posix() + py = Path(self.prefix).joinpath("python").absolute().as_posix() else: - py = vistir.compat.Path(self.script_basedir).joinpath("python").absolute().as_posix() + py = Path(self.script_basedir).joinpath("python").absolute().as_posix() if not py: - py = vistir.compat.Path(sys.executable).as_posix() + py = Path(sys.executable).as_posix() self._python = py return py @@ -289,7 +289,7 @@ class Environment: """ from .vendor.vistir.compat import JSONDecodeError - current_executable = vistir.compat.Path(sys.executable).as_posix() + current_executable = Path(sys.executable).as_posix() if not self.python or self.python == current_executable: return sys.path elif any([sys.prefix == self.prefix, not self.is_venv]): @@ -463,7 +463,7 @@ class Environment: command = [self.python, "-c", "import sys; print(sys.prefix)"] c = subprocess_run(command) - sys_prefix = vistir.compat.Path(c.stdout.strip()).as_posix() + sys_prefix = Path(c.stdout.strip()).as_posix() return sys_prefix @cached_property @@ -515,7 +515,7 @@ class Environment: reinstall """ prefixes = [ - vistir.compat.Path(prefix) + Path(prefix) for prefix in self.base_paths["libdirs"].split(os.pathsep) if vistir.path.is_in_path(prefix, self.prefix.as_posix()) ] @@ -855,7 +855,7 @@ class Environment: extra_dists = [] original_path = sys.path original_prefix = sys.prefix - parent_path = vistir.compat.Path(__file__).absolute().parent + parent_path = Path(__file__).absolute().parent vendor_dir = parent_path.joinpath("vendor").as_posix() patched_dir = parent_path.joinpath("patched").as_posix() parent_path = parent_path.as_posix() diff --git a/pipenv/project.py b/pipenv/project.py index dd6fa7ed..cf80f4c5 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -6,6 +6,7 @@ import io import json import operator import os +from pathlib import Path import re import sys import urllib.parse @@ -64,7 +65,7 @@ class _LockFileEncoder(json.JSONEncoder): ) def default(self, obj): - if isinstance(obj, vistir.compat.Path): + if isinstance(obj, Path): obj = obj.as_posix() return super(_LockFileEncoder, self).default(obj) @@ -277,7 +278,7 @@ class Project: # If content looks like a path, use it as a relative path. # Otherwise use directory named after content in WORKON_HOME. if looks_like_dir(name): - path = vistir.compat.Path(self.project_directory, name) + path = Path(self.project_directory, name) return path.absolute().as_posix() return str(get_workon_home().joinpath(name)) @@ -467,7 +468,7 @@ class Project: def proper_names_db_path(self): # type: () -> str if self._proper_names_db_path is None: - self._proper_names_db_path = vistir.compat.Path( + self._proper_names_db_path = Path( self.virtualenv_location, "pipenv-proper-names.txt" ) self._proper_names_db_path.touch() # Ensure the file exists. @@ -832,8 +833,8 @@ class Project: formatted_data = tomlkit.dumps(document).rstrip() if ( - vistir.compat.Path(path).absolute() - == vistir.compat.Path(self.pipfile_location).absolute() + Path(path).absolute() + == Path(self.pipfile_location).absolute() ): newlines = self._pipfile_newlines else: diff --git a/pipenv/shells.py b/pipenv/shells.py index a2f4b52e..d9abc490 100644 --- a/pipenv/shells.py +++ b/pipenv/shells.py @@ -141,7 +141,7 @@ class Bash(Shell): # https://github.com/berdario/pew/issues/58#issuecomment-102182346 @contextlib.contextmanager def inject_path(self, venv): - from ._compat import NamedTemporaryFile + from tempfile import NamedTemporaryFile bashrc_path = Path.home().joinpath(".bashrc") with NamedTemporaryFile("w+") as rcfile: diff --git a/pipenv/utils.py b/pipenv/utils.py index 7f7a0589..7f0da541 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -15,6 +15,7 @@ import warnings from contextlib import contextmanager from distutils.spawn import find_executable +from pathlib import Path from urllib.parse import urlparse import crayons @@ -1508,7 +1509,6 @@ def is_editable(pipfile_entry): def is_installable_file(path): """Determine if a path can potentially be installed""" - from ._compat import Path from .patched.notpip._internal.utils.packaging import specifiers from .vendor.pip_shims.shims import is_archive_file, is_installable_dir @@ -1620,7 +1620,6 @@ def find_windows_executable(bin_path, exe_name): def path_to_url(path): - from ._compat import Path return Path(normalize_drive(os.path.abspath(path))).as_uri() @@ -1989,8 +1988,6 @@ def clean_resolved_dep(dep, is_top_level=False, pipfile_entry=None): def get_workon_home(): - from ._compat import Path - workon_home = os.environ.get("WORKON_HOME") if not workon_home: if os.name == "nt": @@ -2044,7 +2041,6 @@ def locked_repository(requirement): @contextmanager def chdir(path): """Context manager to change working directories.""" - from ._compat import Path if not path: return prev_cwd = Path.cwd().as_posix() diff --git a/tasks/vendoring/__init__.py b/tasks/vendoring/__init__.py index ec0bb072..298f4feb 100644 --- a/tasks/vendoring/__init__.py +++ b/tasks/vendoring/__init__.py @@ -19,7 +19,7 @@ import requests from urllib3.util import parse_url as urllib3_parse -from pipenv.vendor.vistir.compat import TemporaryDirectory +from tempfile import TemporaryDirectory from pipenv.vendor.vistir.contextmanagers import open_file import pipenv.vendor.parse as parse @@ -258,13 +258,13 @@ def _ensure_package_in_requirements(ctx, requirements_file, package): def install_pyyaml(ctx, vendor_dir): with TemporaryDirectory(prefix="pipenv-", suffix="-yaml") as download_dir: pip_command = "pip download --no-binary=:all: --no-clean --no-deps -d {} pyyaml".format( - download_dir.name + download_dir ) log(f"downloading deps via pip: {pip_command}") ctx.run(pip_command) - downloaded = next(Path(download_dir.name).glob("*.tar.gz")) + downloaded = next(Path(download_dir).glob("*.tar.gz")) with tarfile.open(downloaded, mode="r:gz") as tf: - tf.extractall(download_dir.name) + tf.extractall(download_dir) extracted = next((p for p in downloaded.parent.iterdir() if p != downloaded)) yaml_dir = vendor_dir / "yaml" path_dict = { diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 93166072..50f0ea80 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -8,25 +8,21 @@ import shutil import traceback import sys import warnings - +from pathlib import Path from shutil import rmtree as _rmtree +from tempfile import TemporaryDirectory import pytest import requests - from click.testing import CliRunner from pytest_pypi.app import prepare_fixtures from pytest_pypi.app import prepare_packages as prepare_pypi_packages -from pipenv._compat import Path from pipenv.cli import cli from pipenv.exceptions import VirtualenvActivationException from pipenv.utils import subprocess_run from pipenv.vendor import toml, tomlkit -from pipenv.vendor.vistir.compat import ( - FileNotFoundError, PermissionError, ResourceWarning, TemporaryDirectory, - fs_encode, fs_str -) +from pipenv.vendor.vistir.compat import fs_encode, fs_str from pipenv.vendor.vistir.contextmanagers import temp_environ from pipenv.vendor.vistir.misc import run from pipenv.vendor.vistir.path import ( @@ -165,7 +161,7 @@ def local_tempdir(request): request.addfinalizer(finalize) with TemporaryDirectory(dir=new_temp.as_posix()) as temp_dir: - yield Path(temp_dir.name) + yield Path(temp_dir) @pytest.fixture(name='create_tmpdir') @@ -381,7 +377,7 @@ class _PipenvInstance: with TemporaryDirectory(prefix='pipenv-', suffix='-cache') as tempdir: cmd_args = shlex.split(cmd) - env = {**self.env, **{'PIPENV_CACHE_DIR': tempdir.name}} + env = {**self.env, **{'PIPENV_CACHE_DIR': tempdir}} self.capfd.readouterr() r = cli_runner.invoke(cli, cmd_args, env=env) r.returncode = r.exit_code diff --git a/tests/integration/test_dot_venv.py b/tests/integration/test_dot_venv.py index 85013153..96ad37a7 100644 --- a/tests/integration/test_dot_venv.py +++ b/tests/integration/test_dot_venv.py @@ -1,10 +1,12 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function + import os +from pathlib import Path +from tempfile import TemporaryDirectory import pytest -from pipenv._compat import Path, TemporaryDirectory from pipenv.utils import normalize_drive, temp_environ @@ -56,7 +58,7 @@ def test_venv_file(venv_name, PipenvInstance): with temp_environ(), TemporaryDirectory( prefix='pipenv-', suffix='temp_workon_home' ) as workon_home: - os.environ['WORKON_HOME'] = workon_home.name + os.environ['WORKON_HOME'] = workon_home if 'PIPENV_VENV_IN_PROJECT' in os.environ: del os.environ['PIPENV_VENV_IN_PROJECT'] @@ -72,7 +74,7 @@ def test_venv_file(venv_name, PipenvInstance): if os.path.sep in venv_name: venv_expected_path = Path(p.path).joinpath(venv_name).absolute().as_posix() else: - venv_expected_path = Path(workon_home.name).joinpath(venv_name).absolute().as_posix() + venv_expected_path = Path(workon_home).joinpath(venv_name).absolute().as_posix() assert venv_path == normalize_drive(venv_expected_path) @@ -82,13 +84,13 @@ def test_empty_venv_file(PipenvInstance): """ with PipenvInstance(chdir=True) as p: file_path = os.path.join(p.path, '.venv') - with open(file_path, 'w') as f: + with open(file_path, 'w'): pass with temp_environ(), TemporaryDirectory( prefix='pipenv-', suffix='temp_workon_home' ) as workon_home: - os.environ['WORKON_HOME'] = workon_home.name + os.environ['WORKON_HOME'] = workon_home if 'PIPENV_VENV_IN_PROJECT' in os.environ: del os.environ['PIPENV_VENV_IN_PROJECT'] @@ -103,7 +105,7 @@ def test_empty_venv_file(PipenvInstance): from pathlib import PurePosixPath venv_path = normalize_drive(venv_loc.as_posix()) venv_path_parent = str(PurePosixPath(venv_path).parent) - assert venv_path_parent == Path(workon_home.name).absolute().as_posix() + assert venv_path_parent == Path(workon_home).absolute().as_posix() @pytest.mark.dotvenv @@ -120,7 +122,7 @@ def test_venv_file_with_path(PipenvInstance): file_path = os.path.join(p.path, '.venv') with open(file_path, 'w') as f: - f.write(venv_path.name) + f.write(venv_path) c = p.pipenv('install') assert c.returncode == 0 @@ -129,4 +131,4 @@ def test_venv_file_with_path(PipenvInstance): venv_loc = Path(c.stdout.strip()) assert venv_loc.joinpath('.project').exists() - assert venv_loc == Path(venv_path.name) + assert venv_loc == Path(venv_path) diff --git a/tests/integration/test_install_basic.py b/tests/integration/test_install_basic.py index a4af99ec..f30c792a 100644 --- a/tests/integration/test_install_basic.py +++ b/tests/integration/test_install_basic.py @@ -1,10 +1,12 @@ import os +from pathlib import Path +from tempfile import TemporaryDirectory + import pytest from flaky import flaky -from pipenv._compat import Path, TemporaryDirectory from pipenv.utils import subprocess_run, temp_environ @@ -396,7 +398,7 @@ def test_install_venv_project_directory(PipenvInstance): with temp_environ(), TemporaryDirectory( prefix="pipenv-", suffix="temp_workon_home" ) as workon_home: - os.environ["WORKON_HOME"] = workon_home.name + os.environ["WORKON_HOME"] = workon_home if "PIPENV_VENV_IN_PROJECT" in os.environ: del os.environ["PIPENV_VENV_IN_PROJECT"] diff --git a/tests/integration/test_install_twists.py b/tests/integration/test_install_twists.py index a07c27c7..ecb1d143 100644 --- a/tests/integration/test_install_twists.py +++ b/tests/integration/test_install_twists.py @@ -1,12 +1,11 @@ import os import shutil import sys +from pathlib import Path import pytest - from flaky import flaky -from pipenv._compat import Path from pipenv.utils import mkdir_p, temp_environ diff --git a/tests/integration/test_install_uri.py b/tests/integration/test_install_uri.py index 3c6b8a0d..069e9b3c 100644 --- a/tests/integration/test_install_uri.py +++ b/tests/integration/test_install_uri.py @@ -1,11 +1,12 @@ # -*- coding=utf-8 -*- from __future__ import absolute_import, print_function -import os -import pytest +import os +from pathlib import Path + +import pytest from flaky import flaky -from pipenv._compat import Path from pipenv.utils import subprocess_run diff --git a/tests/integration/test_lock.py b/tests/integration/test_lock.py index 19197d6c..9168b6b7 100644 --- a/tests/integration/test_lock.py +++ b/tests/integration/test_lock.py @@ -1,11 +1,11 @@ import json import os import sys +from pathlib import Path import pytest from flaky import flaky -from vistir.compat import Path from vistir.misc import to_text from pipenv.utils import temp_environ diff --git a/tests/integration/test_windows.py b/tests/integration/test_windows.py index 727bdae8..8ece859c 100644 --- a/tests/integration/test_windows.py +++ b/tests/integration/test_windows.py @@ -1,9 +1,8 @@ import os +from pathlib import Path import pytest -from pipenv._compat import Path - # This module is run only on Windows. pytestmark = pytest.mark.skipif(os.name != 'nt', reason="only relevant on windows") diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py index f20e0071..66e28a1f 100644 --- a/tests/unit/test_core.py +++ b/tests/unit/test_core.py @@ -1,8 +1,8 @@ import os +from tempfile import TemporaryDirectory import pytest -from pipenv._compat import TemporaryDirectory from pipenv.core import load_dot_env, warn_in_virtualenv from pipenv.utils import temp_environ @@ -25,7 +25,7 @@ def test_load_dot_env_from_environment_variable_location(monkeypatch, capsys, pr import click is_console = False m.setattr(click._winconsole, "_is_console", lambda x: is_console) - dotenv_path = os.path.join(tempdir.name, 'test.env') + dotenv_path = os.path.join(tempdir, 'test.env') key, val = 'SOME_KEY', 'some_value' with open(dotenv_path, 'w') as f: f.write(f'{key}={val}') @@ -42,7 +42,7 @@ def test_doesnt_load_dot_env_if_disabled(monkeypatch, capsys, project): import click is_console = False m.setattr(click._winconsole, "_is_console", lambda x: is_console) - dotenv_path = os.path.join(tempdir.name, 'test.env') + dotenv_path = os.path.join(tempdir, 'test.env') key, val = 'SOME_KEY', 'some_value' with open(dotenv_path, 'w') as f: f.write(f'{key}={val}') @@ -63,7 +63,7 @@ def test_load_dot_env_warns_if_file_doesnt_exist(monkeypatch, capsys, project): import click is_console = False m.setattr(click._winconsole, "_is_console", lambda x: is_console) - dotenv_path = os.path.join(tempdir.name, 'does-not-exist.env') + dotenv_path = os.path.join(tempdir, 'does-not-exist.env') project.s.PIPENV_DOTENV_LOCATION = str(dotenv_path) load_dot_env(project) output, err = capsys.readouterr()