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

Issue #1525 Preinstall from requirements file #1536

Closed
Closed
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
1 change: 1 addition & 0 deletions changelog.d/1525.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add `pipx install --preinstall-from-file requirements.txt <pkg>` option
4 changes: 4 additions & 0 deletions src/pipx/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def install(
reinstall: bool,
include_dependencies: bool,
preinstall_packages: Optional[List[str]],
preinstall_from_file: Optional[List[str]],
suffix: str = "",
python_flag_passed=False,
) -> ExitCode:
Expand Down Expand Up @@ -96,6 +97,8 @@ def install(
venv.create_venv(venv_args, pip_args, override_shared)
for dep in preinstall_packages or []:
venv.upgrade_package_no_metadata(dep, [])
if preinstall_from_file:
venv.install_packages_from_file(preinstall_from_file, pip_args)
venv.install_package(
package_name=package_name,
package_or_url=package_spec,
Expand Down Expand Up @@ -213,6 +216,7 @@ def install_all(
reinstall=False,
include_dependencies=main_package.include_dependencies,
preinstall_packages=[],
preinstall_from_file=[],
suffix=main_package.suffix,
)

Expand Down
1 change: 1 addition & 0 deletions src/pipx/commands/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def reinstall(
reinstall=True,
include_dependencies=venv.pipx_metadata.main_package.include_dependencies,
preinstall_packages=[],
preinstall_from_file=[],
suffix=venv.pipx_metadata.main_package.suffix,
python_flag_passed=python_flag_passed,
)
Expand Down
1 change: 1 addition & 0 deletions src/pipx/commands/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def _upgrade_venv(
reinstall=False,
include_dependencies=False,
preinstall_packages=None,
preinstall_from_file=None,
python_flag_passed=python_flag_passed,
)
return 0
Expand Down
9 changes: 9 additions & 0 deletions src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
reinstall=False,
include_dependencies=args.include_deps,
preinstall_packages=args.preinstall,
preinstall_from_file=args.preinstall_from_file,
suffix=args.suffix,
python_flag_passed=python_flag_passed,
)
Expand Down Expand Up @@ -502,6 +503,14 @@ def _add_install(subparsers: argparse._SubParsersAction, shared_parser: argparse
),
)
add_pip_venv_args(p)
p.add_argument(
"--preinstall-from-file",
action="append",
help=(
"Absolute path to requirements file listing optional packages to be preinstalled into the "
"Virtual Environment before installing the main package. Use this flag multiple times for multiple files."
),
)


def _add_install_all(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
Expand Down
10 changes: 10 additions & 0 deletions src/pipx/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ def install_package_no_deps(self, package_or_url: str, pip_args: List[str]) -> s

return package_name

def install_packages_from_file(self, requirements_files: List[str], pip_args: List[str]) -> None:
msg = f"Preinstalling requirements from '{requirements_files}'"
logger.info(msg)
pip_cmd = ["--no-input", "install", "--no-deps"] + pip_args
for requirements_file in requirements_files:
pip_cmd += ["--requirement", requirements_file]
with animate(msg, self.do_animation):
pip_process = self._run_pip(pip_cmd)
subprocess_post_check(pip_process)

def get_venv_metadata_for_package(self, package_name: str, package_extras: Set[str]) -> VenvMetadata:
data_start = time.time()
venv_metadata = inspect_venv(package_name, package_extras, self.bin_path, self.python_path, self.man_path)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ def test_preinstall_specific_version(pipx_temp_env, caplog):
assert "black==22.8.0" in caplog.text


def test_preinstall_from_file(pipx_temp_env, tmp_path, caplog):
package = "black"
version = "22.8.0"
requirements_file_name = "requirements.txt"
requirements_file = tmp_path / requirements_file_name
requirements_file.write_text(f"{package}=={version}")
requirements_file.touch()
assert not run_pipx_cli(["install", "--preinstall-from-file", str(requirements_file), "nox"])
assert "nox" in caplog.text
assert str(requirements_file) in caplog.text


@pytest.mark.xfail
def test_do_not_wait_for_input(pipx_temp_env, pipx_session_shared_dir, monkeypatch):
monkeypatch.setenv("PIP_INDEX_URL", "http://127.0.0.1:8080/simple")
Expand Down