Skip to content

Commit

Permalink
mypy, type hint, pep8, py27
Browse files Browse the repository at this point in the history
  • Loading branch information
bmartinn committed May 31, 2020
1 parent 47f2047 commit 260d4fe
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/pip/_internal/req/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import

import logging
from functools import partial

from pip._internal.utils.logging import indent_log
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
Expand All @@ -15,10 +16,10 @@
try:
from multiprocessing.pool import Pool
except ImportError: # Platform-specific: No multiprocessing available
Pool = None
Pool = None # type: ignore

if MYPY_CHECK_RUNNING:
from typing import List, Optional, Sequence
from typing import List, Optional, Sequence, Any

__all__ = [
"RequirementSet", "InstallRequirement",
Expand Down Expand Up @@ -63,16 +64,21 @@ def install_given_reqs(
)

# pre allocate installed package names
installed = [None] * len(to_install)
installed = \
[None] * len(to_install) # type: List[Optional[InstallationResult]]
install_args = [install_options, global_options, dict(
root=root, home=home, prefix=prefix, warn_script_location=warn_script_location,
use_user_site=use_user_site, pycompile=pycompile)]
root=root, home=home, prefix=prefix,
warn_script_location=warn_script_location,
use_user_site=use_user_site, pycompile=pycompile)]

if Pool is not None:
# first let's try to install in parallel, if we fail we do it by order.
pool = Pool()
try:
pool_result = pool.starmap_async(__single_install, [(install_args, r) for r in to_install])
pool_result = pool.map_async(partial(
__single_install, install_args, allow_raise=False),
to_install
)
# python 2.7 timeout=None will not catch KeyboardInterrupt
installed = pool_result.get(timeout=999999)
except (KeyboardInterrupt, SystemExit):
Expand All @@ -87,12 +93,18 @@ def install_given_reqs(
with indent_log():
for i, requirement in enumerate(to_install):
if installed[i] is None:
installed[i] = __single_install(install_args, requirement, allow_raise=True)
installed[i] = __single_install(
install_args, requirement, allow_raise=True)

return [i for i in installed if i is not None]


def __single_install(args, a_requirement, allow_raise=False):
def __single_install(
args, # type: Sequence[Any]
a_requirement, # type: InstallRequirement
allow_raise=False, # type: bool
):
# type: (...) -> (Optional[InstallationResult])
if a_requirement.should_reinstall:
logger.info('Attempting uninstall: %s', a_requirement.name)
with indent_log():
Expand All @@ -106,20 +118,16 @@ def __single_install(args, a_requirement, allow_raise=False):
**args[2] # **kwargs
)
except Exception:
should_rollback = (
a_requirement.should_reinstall and
not a_requirement.install_succeeded
)
should_rollback = (a_requirement.should_reinstall and
not a_requirement.install_succeeded)
# if install did not succeed, rollback previous uninstall
if should_rollback:
uninstalled_pathset.rollback()
if allow_raise:
raise
else:
should_commit = (
a_requirement.should_reinstall and
a_requirement.install_succeeded
)
should_commit = (a_requirement.should_reinstall and
a_requirement.install_succeeded)
if should_commit:
uninstalled_pathset.commit()
return InstallationResult(a_requirement.name)
Expand Down

0 comments on commit 260d4fe

Please sign in to comment.