未验证 提交 35f12d5b 编写于 作者: F Frost Ming 提交者: GitHub

feat: Remove dependency of pip (#1268)

上级 e2a24dca
Remove the dependency of `pip`.
此差异已折叠。
from __future__ import annotations
import argparse
import os
import shlex
import subprocess
import sys
from pip import __file__ as pip_location
from pdm import termui
from pdm.cli.commands.base import BaseCommand
from pdm.cli.options import verbose_option
from pdm.cli.utils import Package, build_dependency_graph
from pdm.compat import importlib_metadata
from pdm.models.environment import WorkingSet
from pdm.models.environment import BareEnvironment, WorkingSet
from pdm.project import Project
from pdm.utils import normalize_name
......@@ -26,11 +23,10 @@ def _all_plugins() -> list[str]:
return sorted(result)
def run_pip(args: list[str]) -> bytes:
return subprocess.check_output(
[sys.executable, "-I", os.path.dirname(pip_location)] + args,
stderr=subprocess.STDOUT,
)
def run_pip(project: Project, args: list[str]) -> bytes:
env = BareEnvironment(project)
project.environment = env
return subprocess.check_output(env.pip_command + args, stderr=subprocess.STDOUT)
class Command(BaseCommand):
......@@ -101,7 +97,7 @@ class AddCommand(BaseCommand):
with project.core.ui.open_spinner(
f"Installing plugins: {options.packages}"
):
run_pip(pip_args)
run_pip(project, pip_args)
except subprocess.CalledProcessError as e:
project.core.ui.echo(
"Installation failed: \n" + e.output.decode("utf8"), err=True
......@@ -182,7 +178,7 @@ class RemoveCommand(BaseCommand):
with project.core.ui.open_spinner(
f"Uninstalling plugins: {valid_packages}"
):
run_pip(pip_args)
run_pip(project, pip_args)
except subprocess.CalledProcessError as e:
project.core.ui.echo(
"Uninstallation failed: \n" + e.output.decode("utf8"), err=True
......
......@@ -9,7 +9,6 @@ requires-python = ">=3.7"
license = {text = "MIT"}
dependencies = [
"blinker",
"pip>=20",
"packaging",
"platformdirs",
"rich>=12.3.0",
......
from unittest.mock import Mock
from unittest.mock import ANY, Mock
import pytest
......@@ -34,13 +34,15 @@ def test_plugin_list(invoke):
def test_plugin_add(invoke, mock_pip):
result = invoke(["plugin", "add", "foo"])
assert result.exit_code == 0, result.stderr
mock_pip.assert_called_with(["install", "foo"])
mock_pip.assert_called_with(ANY, ["install", "foo"])
result = invoke(
["plugin", "add", "--pip-args", "--force-reinstall --upgrade", "foo"]
)
assert result.exit_code == 0, result.stderr
mock_pip.assert_called_with(["install", "--force-reinstall", "--upgrade", "foo"])
mock_pip.assert_called_with(
ANY, ["install", "--force-reinstall", "--upgrade", "foo"]
)
@pytest.mark.usefixtures("mock_all_plugins")
......@@ -59,4 +61,4 @@ def test_plugin_remove(invoke, mock_pip, monkeypatch):
result = invoke(["plugin", "remove", "-y", "demo"])
assert result.exit_code == 0, result.stderr
mock_pip.assert_called_with(["uninstall", "-y", "demo", "pytz"])
mock_pip.assert_called_with(ANY, ["uninstall", "-y", "demo", "pytz"])
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册