4
4
from __future__ import absolute_import
5
5
6
6
import logging
7
+ from functools import partial
7
8
8
9
from pip ._internal .utils .logging import indent_log
9
10
from pip ._internal .utils .typing import MYPY_CHECK_RUNNING
15
16
try :
16
17
from multiprocessing .pool import Pool
17
18
except ImportError : # Platform-specific: No multiprocessing available
18
- Pool = None
19
+ Pool = None # type: ignore
19
20
20
21
if MYPY_CHECK_RUNNING :
21
- from typing import List , Optional , Sequence
22
+ from typing import List , Optional , Sequence , Any
22
23
23
24
__all__ = [
24
25
"RequirementSet" , "InstallRequirement" ,
@@ -63,16 +64,21 @@ def install_given_reqs(
63
64
)
64
65
65
66
# pre allocate installed package names
66
- installed = [None ] * len (to_install )
67
+ installed = \
68
+ [None ] * len (to_install ) # type: List[Optional[InstallationResult]]
67
69
install_args = [install_options , global_options , dict (
68
- root = root , home = home , prefix = prefix , warn_script_location = warn_script_location ,
69
- use_user_site = use_user_site , pycompile = pycompile )]
70
+ root = root , home = home , prefix = prefix ,
71
+ warn_script_location = warn_script_location ,
72
+ use_user_site = use_user_site , pycompile = pycompile )]
70
73
71
74
if Pool is not None :
72
75
# first let's try to install in parallel, if we fail we do it by order.
73
76
pool = Pool ()
74
77
try :
75
- pool_result = pool .starmap_async (__single_install , [(install_args , r ) for r in to_install ])
78
+ pool_result = pool .map_async (partial (
79
+ __single_install , install_args , allow_raise = False ),
80
+ to_install
81
+ )
76
82
# python 2.7 timeout=None will not catch KeyboardInterrupt
77
83
installed = pool_result .get (timeout = 999999 )
78
84
except (KeyboardInterrupt , SystemExit ):
@@ -87,12 +93,18 @@ def install_given_reqs(
87
93
with indent_log ():
88
94
for i , requirement in enumerate (to_install ):
89
95
if installed [i ] is None :
90
- installed [i ] = __single_install (install_args , requirement , allow_raise = True )
96
+ installed [i ] = __single_install (
97
+ install_args , requirement , allow_raise = True )
91
98
92
99
return [i for i in installed if i is not None ]
93
100
94
101
95
- def __single_install (args , a_requirement , allow_raise = False ):
102
+ def __single_install (
103
+ args , # type: Sequence[Any]
104
+ a_requirement , # type: InstallRequirement
105
+ allow_raise = False , # type: bool
106
+ ):
107
+ # type: (...) -> (Optional[InstallationResult])
96
108
if a_requirement .should_reinstall :
97
109
logger .info ('Attempting uninstall: %s' , a_requirement .name )
98
110
with indent_log ():
@@ -106,20 +118,16 @@ def __single_install(args, a_requirement, allow_raise=False):
106
118
** args [2 ] # **kwargs
107
119
)
108
120
except Exception :
109
- should_rollback = (
110
- a_requirement .should_reinstall and
111
- not a_requirement .install_succeeded
112
- )
121
+ should_rollback = (a_requirement .should_reinstall and
122
+ not a_requirement .install_succeeded )
113
123
# if install did not succeed, rollback previous uninstall
114
124
if should_rollback :
115
125
uninstalled_pathset .rollback ()
116
126
if allow_raise :
117
127
raise
118
128
else :
119
- should_commit = (
120
- a_requirement .should_reinstall and
121
- a_requirement .install_succeeded
122
- )
129
+ should_commit = (a_requirement .should_reinstall and
130
+ a_requirement .install_succeeded )
123
131
if should_commit :
124
132
uninstalled_pathset .commit ()
125
133
return InstallationResult (a_requirement .name )
0 commit comments