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

Allow installing multiple applications at once #1102

Merged
merged 13 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## dev

- [docs] Add Scoop installation instructions
- Add ability to install multiple packages at once

## 1.3.3

Expand Down
139 changes: 76 additions & 63 deletions src/pipx/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

def install(
venv_dir: Optional[Path],
package_name: Optional[str],
package_spec: str,
package_names: Optional[List[str]],
package_specs: List[str],
local_bin_dir: Path,
local_man_dir: Path,
python: Optional[str],
Expand All @@ -33,73 +33,86 @@ def install(

python = python or DEFAULT_PYTHON

if package_name is None:
package_name = package_name_from_spec(package_spec, python, pip_args=pip_args, verbose=verbose)
if venv_dir is None:
venv_container = VenvContainer(constants.PIPX_LOCAL_VENVS)
venv_dir = venv_container.get_venv_dir(f"{package_name}{suffix}")
if package_names is None:
package_names = [
package_name_from_spec(package_spec, python, pip_args=pip_args, verbose=verbose)
for package_spec in package_specs
]
elif len(package_names) != len(package_specs):
sam-sw marked this conversation as resolved.
Show resolved Hide resolved
package_names = [
package_name_from_spec(package_spec, python, pip_args=pip_args, verbose=verbose)
for package_spec in package_specs
]

try:
exists = venv_dir.exists() and bool(next(venv_dir.iterdir()))
except StopIteration:
exists = False
for package_name, package_spec in zip(package_names, package_specs):
if venv_dir is None:
venv_container = VenvContainer(constants.PIPX_LOCAL_VENVS)
venv_dir = venv_container.get_venv_dir(f"{package_name}{suffix}")

venv = Venv(venv_dir, python=python, verbose=verbose)
if exists:
if not reinstall and force and python_flag_was_passed:
print(
pipx_wrap(
f"""
--python is ignored when --force is passed.
If you want to reinstall {package_name} with {python},
run `pipx reinstall {package_spec} --python {python}` instead.
"""
try:
exists = venv_dir.exists() and bool(next(venv_dir.iterdir()))
except StopIteration:
exists = False

venv = Venv(venv_dir, python=python, verbose=verbose)
if exists:
if not reinstall and force and python_flag_was_passed:
print(
pipx_wrap(
f"""
--python is ignored when --force is passed.
If you want to reinstall {package_name} with {python},
run `pipx reinstall {package_spec} --python {python}` instead.
"""
)
)
)
if force:
print(f"Installing to existing venv {venv.name!r}")
pip_args = ["--force-reinstall"] + pip_args
else:
print(
pipx_wrap(
f"""
{venv.name!r} already seems to be installed. Not modifying
existing installation in '{venv_dir}'. Pass '--force'
to force installation.
"""
if force:
print(f"Installing to existing venv {venv.name!r}")
pip_args = ["--force-reinstall"] + pip_args
else:
print(
pipx_wrap(
f"""
{venv.name!r} already seems to be installed. Not modifying
existing installation in '{venv_dir}'. Pass '--force'
to force installation.
"""
)
)
return EXIT_CODE_INSTALL_VENV_EXISTS

try:
# Enable installing shared library `pip` with `pipx`
override_shared = package_name == "pip"
venv.create_venv(venv_args, pip_args, override_shared)
for dep in preinstall_packages or []:
dep_name = package_name_from_spec(dep, python, pip_args=pip_args, verbose=verbose)
venv.upgrade_package_no_metadata(dep_name, [])
venv.install_package(
package_name=package_name,
package_or_url=package_spec,
pip_args=pip_args,
include_dependencies=include_dependencies,
include_apps=True,
is_main_package=True,
suffix=suffix,
)
run_post_install_actions(
venv,
package_name,
local_bin_dir,
local_man_dir,
venv_dir,
include_dependencies,
force=force,
)
return EXIT_CODE_INSTALL_VENV_EXISTS
except (Exception, KeyboardInterrupt):
print()
venv.remove_venv()
raise

try:
# Enable installing shared library `pip` with `pipx`
override_shared = package_name == "pip"
venv.create_venv(venv_args, pip_args, override_shared)
for dep in preinstall_packages or []:
dep_name = package_name_from_spec(dep, python, pip_args=pip_args, verbose=verbose)
venv.upgrade_package_no_metadata(dep_name, [])
venv.install_package(
package_name=package_name,
package_or_url=package_spec,
pip_args=pip_args,
include_dependencies=include_dependencies,
include_apps=True,
is_main_package=True,
suffix=suffix,
)
run_post_install_actions(
venv,
package_name,
local_bin_dir,
local_man_dir,
venv_dir,
include_dependencies,
force=force,
)
except (Exception, KeyboardInterrupt):
print()
venv.remove_venv()
raise
# Reset venv_dir to None ready to install the next package in the list
venv_dir = None

# Any failure to install will raise PipxError, otherwise success
return EXIT_CODE_OK
4 changes: 2 additions & 2 deletions src/pipx/commands/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def reinstall(
# install main package first
install(
venv_dir,
venv.main_package_name,
package_or_url,
[venv.main_package_name],
[package_or_url],
local_bin_dir,
local_man_dir,
python,
Expand Down
4 changes: 2 additions & 2 deletions src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def prog_name() -> str:
dependency version conflicts or interfere with your OS's python
packages. 'sudo' is not required to do this.

pipx install PACKAGE_NAME
pipx install PACKAGE_NAME ...
pipx install --python PYTHON PACKAGE_NAME
pipx install VCS_URL
pipx install ./LOCAL_PATH
Expand Down Expand Up @@ -334,7 +334,7 @@ def _add_install(subparsers: argparse._SubParsersAction) -> None:
formatter_class=LineWrapRawTextHelpFormatter,
description=INSTALL_DESCRIPTION,
)
p.add_argument("package_spec", help="package name or pip installation spec")
p.add_argument("package_spec", help="package name(s) or pip installation spec(s)", nargs="*")
add_include_dependencies(p)
p.add_argument("--verbose", action="store_true")
p.add_argument(
Expand Down
40 changes: 40 additions & 0 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ def install_package(capsys, pipx_temp_env, caplog, package, package_name=""):
assert "WARNING" not in caplog.text


def install_multiple_packages(capsys, pipx_temp_env, caplog, packages, package_names=()):
chrysle marked this conversation as resolved.
Show resolved Hide resolved
if not package_names:
package_names = packages
elif len(package_names) != len(packages):
package_names = packages

run_pipx_cli(["install", *packages, "--verbose"])
captured = capsys.readouterr()
for package_name in package_names:
assert f"installed package {package_name}" in captured.out
if not sys.platform.startswith("win"):
# TODO assert on windows too
# https://github.com/pypa/pipx/issues/217
assert "symlink missing or pointing to unexpected location" not in captured.out
assert "not modifying" not in captured.out
assert "is not on your PATH environment variable" not in captured.out
assert "⚠️" not in caplog.text
assert "WARNING" not in caplog.text


@pytest.mark.parametrize(
"package_name, package_spec",
[("pycowsay", "pycowsay"), ("black", PKG["black"]["spec"])],
Expand All @@ -47,6 +67,16 @@ def test_install_easy_packages(capsys, pipx_temp_env, caplog, package_name, pack
install_package(capsys, pipx_temp_env, caplog, package_spec, package_name)


def test_install_easy_multiple_packages(capsys, pipx_temp_env, caplog):
install_multiple_packages(
capsys,
pipx_temp_env,
caplog,
["pycowsay", PKG["black"]["spec"]],
["pycowsay", "black"],
)


@pytest.mark.parametrize(
"package_name, package_spec",
[
Expand All @@ -65,6 +95,16 @@ def test_install_tricky_packages(capsys, pipx_temp_env, caplog, package_name, pa
install_package(capsys, pipx_temp_env, caplog, package_spec, package_name)


def test_install_tricky_multiple_packages(capsys, pipx_temp_env, caplog):
if os.getenv("FAST"):
pytest.skip("skipping slow tests")

packages = ["cloudtoken", "awscli", "shell-functools"]
package_specs = [PKG[package]["spec"] for package in packages]

install_multiple_packages(capsys, pipx_temp_env, caplog, package_specs, packages)


@pytest.mark.parametrize(
"package_name, package_spec",
[
Expand Down