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 4 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
Expand Up @@ -17,6 +17,7 @@
- Fix program name in generated manual page
- Print all environment variables in `pipx environment`
- Return an error message when directory can't be added to PATH successfully
- Add ability to install multiple packages at once with `pipx install one two three ...`
sam-sw marked this conversation as resolved.
Show resolved Hide resolved

## 1.2.1

Expand Down
125 changes: 70 additions & 55 deletions src/pipx/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,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,
python: str,
pip_args: List[str],
Expand All @@ -27,64 +27,79 @@ def install(
# package_spec is anything pip-installable, including package_name, vcs spec,
# zip file, or tar.gz file.

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, strict=True):
sam-sw marked this conversation as resolved.
Show resolved Hide resolved
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 force:
print(f"Installing to existing venv {venv.name!r}")
pip_args += ["--force-reinstall"]
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.
"""
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 force:
print(f"Installing to existing venv {venv.name!r}")
pip_args += ["--force-reinstall"]
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
return EXIT_CODE_INSTALL_VENV_EXISTS

try:
venv.create_venv(venv_args, pip_args)
for dep in preinstall_packages or []:
dep_name = package_name_from_spec(
dep, python, pip_args=pip_args, verbose=verbose
try:
venv.create_venv(venv_args, pip_args)
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,
venv_dir,
include_dependencies,
force=force,
)
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,
venv_dir,
include_dependencies,
force=force,
)
except (Exception, KeyboardInterrupt):
print()
venv.remove_venv()
raise
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 @@ -61,8 +61,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,
python,
venv.pipx_metadata.main_package.pip_args,
Expand Down
5 changes: 4 additions & 1 deletion src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def prog_name() -> str:
packages. 'sudo' is not required to do this.

pipx install PACKAGE_NAME
pipx install FIRST_PACKAGE_NAME SECOND_PACKAGE_NAME NTH_PACKAGE_NAME
sam-sw marked this conversation as resolved.
Show resolved Hide resolved
pipx install --python PYTHON PACKAGE_NAME
pipx install VCS_URL
pipx install ./LOCAL_PATH
Expand Down Expand Up @@ -325,7 +326,9 @@ 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
42 changes: 42 additions & 0 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ 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=()
):
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 @@ -51,6 +73,16 @@ def test_install_easy_packages(
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 @@ -71,6 +103,16 @@ def test_install_tricky_packages(
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