Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run pip-compile in env python rather than pip-tools python #1998

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import itertools
import os
import shlex
import shutil
import sys
import tempfile
import typing
from pathlib import Path
from subprocess import run # nosec
from typing import IO, Any, BinaryIO, cast

import click
Expand Down Expand Up @@ -79,9 +82,9 @@ def _determine_linesep(
)
@click.pass_context
@options.version
@options.color
@options.verbose
@options.quiet
@options.color
@options.dry_run
@options.pre
@options.rebuild
Expand Down Expand Up @@ -122,11 +125,69 @@ def _determine_linesep(
@options.build_deps_for
@options.all_build_deps
@options.only_build_deps
@typing.no_type_check
def cli(
*args,
verbose: int,
quiet: int,
**kwargs,
):
"""
Compiles requirements.txt from requirements.in, pyproject.toml, setup.cfg,
or setup.py specs.
"""
log.verbosity = verbose - quiet
current_python = sys.executable
log.debug(f"{current_python=}")
env_python = shutil.which("python")
log.debug(f"{env_python=}")
if current_python != env_python:
# pip-tools probably installed globally (e.g. via pipx)
# install pip-tools in a venv made by env_python and run it there
click.echo("Installing pip-tools in temporary venv...", err=True)
with tempfile.TemporaryDirectory(prefix="pip-tools-env-") as venv_path:
log.debug(f"Installing venv at {venv_path}")
run( # nosec
[
env_python,
"-m",
"venv",
venv_path,
],
capture_output=(not log.verbosity),
check=True,
)
temp_python = venv_path + "/bin/python"
log.debug(f"{temp_python=}")
run( # nosec
[
temp_python,
"-m",
"pip",
"install",
"pip-tools", # TODO fix to this version of pip-tools?
],
capture_output=(not log.verbosity),
check=True,
)
env = {**os.environ, "PATH": f"{venv_path}/bin:{os.environ['PATH']}"}
run( # nosec
[
"pip-compile",
*sys.argv[1:],
],
env=env,
)
else:
# pip-tools running in current env python
cli_runner(*args, verbose, quiet, **kwargs)


def cli_runner(
ctx: click.Context,
color: bool | None,
verbose: int,
quiet: int,
color: bool | None,
dry_run: bool,
pre: bool,
rebuild: bool,
Expand Down
9 changes: 9 additions & 0 deletions piptools/scripts/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def cli(
if config:
log.debug(f"Using pip-tools configuration defaults found in '{config !s}'.")

current_python = sys.executable
log.debug(f"{current_python=}")
env_python = shutil.which("python")
log.debug(f"{env_python=}")

if python_executable is None and current_python != env_python:
# pip-tools probably installed globally (e.g. via pipx)
python_executable = env_python

if python_executable:
_validate_python_executable(python_executable)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil
import subprocess
import sys
import venv
from textwrap import dedent
from unittest import mock
from unittest.mock import MagicMock
Expand Down Expand Up @@ -38,6 +39,12 @@
)


@pytest.fixture(autouse=True)
def mock_run():
with mock.patch("piptools.scripts.compile.run") as mock_run:
yield mock_run


@pytest.fixture(
autouse=True,
params=[
Expand Down Expand Up @@ -3771,3 +3778,11 @@ def test_stdout_should_not_be_read_when_stdin_is_not_a_plain_file(
out = runner.invoke(cli, [req_in.as_posix(), "--output-file", fifo.as_posix()])

assert out.exit_code == 0, out


@mock.patch("piptools.scripts.compile.cli_runner")
def test_pip_compile_run_with_env_python(mock_cli_runner, mock_run, runner, tmpdir):
venv.EnvBuilder(with_pip=True, symlinks=True).create(tmpdir)
runner.invoke(cli, ["--verbose"], env={"PATH": f"{tmpdir}/bin"})
mock_run.assert_called()
mock_cli_runner.assert_not_called()
Loading