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

[WIP] direct usage of pip install #2625

Closed
wants to merge 4 commits into from
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
8 changes: 2 additions & 6 deletions aea/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# ------------------------------------------------------------------------------
"""Implementation of the 'aea install' subcommand."""

import pprint
from typing import Optional, cast

import click
Expand All @@ -29,7 +28,7 @@
from aea.configurations.data_types import Dependencies
from aea.configurations.pypi import is_satisfiable, is_simple_dep, to_set_specifier
from aea.exceptions import AEAException
from aea.helpers.install_dependency import call_pip, install_dependency
from aea.helpers.install_dependency import call_pip, install_dependencies


@click.command()
Expand Down Expand Up @@ -79,10 +78,7 @@ def do_install(ctx: Context, requirement: Optional[str] = None) -> None:
]
)
)

for name, d in dependencies.items():
click.echo(f"Installing {pprint.pformat(name)}...")
install_dependency(name, d, logger)
install_dependencies(list(dependencies.values()), logger=logger)
except AEAException as e:
raise click.ClickException(str(e))

Expand Down
50 changes: 49 additions & 1 deletion aea/helpers/install_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
#
# ------------------------------------------------------------------------------
"""Helper to install python dependencies."""
import logging
import subprocess # nosec
import sys
from io import StringIO
from itertools import chain
from logging import Logger
from subprocess import PIPE # nosec
from typing import List
from typing import List, Tuple

from pip._internal.commands.install import InstallCommand

from aea.configurations.base import Dependency
from aea.exceptions import AEAException, enforce
Expand Down Expand Up @@ -51,6 +56,49 @@ def install_dependency(
)


def install_dependencies(
dependencies: List[Dependency], logger: Logger, install_timeout: float = 300,
) -> None:
"""
Install python dependencies to the current python environment.

:param dependencies: dict of dependency name and specification
:param logger: the logger
:param install_timeout: timeout to wait pip to install
"""
del install_timeout
try:
pip_args = list(chain(*[d.get_pip_install_args() for d in dependencies]))
pip_args = [("--extra-index" if i == "-i" else i) for i in pip_args]
logger.debug("Calling 'pip install {}'".format(" ".join(pip_args)))
exit_code, err_logs = pip_install(*pip_args)
if exit_code != 0:
raise Exception(err_logs)
except Exception as e:
raise AEAException(
f"An error occurred while installing with pip install {' '.join(pip_args)}: {e}"
)


def pip_install(*args: str) -> Tuple[int, str]:
"""Use pip install command directly."""
buf = StringIO()
buf_handler = logging.StreamHandler(buf)
logger = logging.getLogger("pip")
logger.addHandler(buf_handler)
logger.setLevel(logging.ERROR)
exit_code = -100
try:
cmd = InstallCommand("install", "install")
exit_code = cmd.main(list(args))
finally:
error_logs = buf.getvalue()
logger.removeHandler(buf_handler)
del buf_handler
del buf
return exit_code, error_logs


def call_pip(pip_args: List[str], timeout: float = 300, retry: bool = False) -> None:
"""
Run pip install command.
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def get_all_extras() -> Dict:
"pyyaml>=4.2b1,<6.0",
"requests>=2.22.0,<3.0.0",
"python-dotenv>=0.14.0,<0.18.0",
"ecdsa>=0.15,<0.17.0"
"ecdsa>=0.15,<0.17.0",
"pip",
]

if os.name == "nt" or os.getenv("WIN_BUILD_WHEEL", None) == "1":
Expand Down
8 changes: 3 additions & 5 deletions tests/test_cli/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def test_exit_code_equal_to_zero(self):
class TestInstallFailsWhenDependencyDoesNotExist(AEATestCaseEmpty):
"""Test that the command 'aea install' fails when a dependency is not found."""

capture_log = True

@classmethod
def setup_class(cls):
"""Set the test up."""
Expand All @@ -94,10 +96,6 @@ def setup_class(cls):
"version": "==0.1.0",
"index": "https://test.pypi.org/simple",
},
"this_is_a_test_dependency_on_git": {
"git": "https://github.com/an_user/a_repo.git",
"ref": "master",
},
}
)

Expand All @@ -108,7 +106,7 @@ def test_error(self):
"""Assert an error occurs."""
with pytest.raises(
ClickException,
match="An error occurred while installing this_is_a_test_dependency.*",
match="An error occurred while installing.*this_is_a_test_dependency.*",
):
self.run_cli_command("install", cwd=self._get_cwd())

Expand Down
34 changes: 33 additions & 1 deletion tests/test_helpers/test_install_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from aea.configurations.base import Dependency
from aea.exceptions import AEAException
from aea.helpers.install_dependency import install_dependency
from aea.helpers.install_dependency import install_dependencies, install_dependency


class InstallDependencyTestCase(TestCase):
Expand All @@ -51,3 +51,35 @@ def test__install_dependency_fails_real_pip_call(self):
install_dependency(
"testnotexists", Dependency("testnotexists", "==10.0.0"), mock.Mock()
)


class InstallDependenciesTestCase(TestCase):
"""Test case for _install_dependencies method."""

def test_fails(self, *mocks):
"""Test for install_dependency method fails."""
result = 1
with mock.patch(
"pip._internal.commands.install.InstallCommand.main", return_value=result
):
with self.assertRaises(AEAException):
install_dependencies([Dependency("test", "==10.0.0")], mock.Mock())

def test_ok(self, *mocks):
"""Test for install_dependency method ok."""
result = 0
with mock.patch(
"pip._internal.commands.install.InstallCommand.main", return_value=result
):
install_dependencies([Dependency("test", "==10.0.0")], mock.Mock())

def test_fails_real_pip_call(self):
"""Test for install_dependency method fails."""
with pytest.raises(AEAException, match=r"No matching distribution found"):
install_dependencies([Dependency("testnotexists", "==10.0.0")], mock.Mock())

"""Test for install_dependency method fails."""
with pytest.raises(AEAException, match=r"No matching distribution found"):
install_dependency(
"testnotexists", Dependency("testnotexists", "==10.0.0"), mock.Mock()
)