7
7
8
8
9
9
REQUIREMENTS_FILE = os .path .join (pyperformance .DATA_DIR , 'requirements.txt' )
10
+ PYPERF_OPTIONAL = ['psutil' ]
10
11
11
12
12
13
class Requirements (object ):
13
14
14
15
@classmethod
15
- def from_file (cls , filename , optional = None ):
16
+ def from_file (cls , filename ):
16
17
self = cls ()
17
- self ._add_from_file (filename , optional )
18
+ self ._add_from_file (filename )
18
19
return self
19
20
20
21
@classmethod
@@ -32,44 +33,25 @@ def __init__(self):
32
33
# requirements
33
34
self .specs = []
34
35
35
- # optional requirements
36
- self ._optional = set ()
37
-
38
36
def __len__ (self ):
39
37
return len (self .specs )
40
38
41
- def iter_non_optional (self ):
42
- for spec in self .specs :
43
- if spec in self ._optional :
44
- continue
45
- yield spec
46
-
47
- def iter_optional (self ):
39
+ def __iter__ (self ):
48
40
for spec in self .specs :
49
- if spec not in self ._optional :
50
- continue
51
41
yield spec
52
42
53
- def _add_from_file (self , filename , optional = None ):
43
+ def _add_from_file (self , filename ):
54
44
if not os .path .exists (filename ):
55
45
return
56
46
for line in _utils .iter_clean_lines (filename ):
57
- self ._add (line , optional )
47
+ self ._add (line )
58
48
59
- def _add (self , line , optional = None ):
49
+ def _add (self , line ):
60
50
self .specs .append (line )
61
- if optional :
62
- # strip env markers
63
- req = line .partition (';' )[0 ]
64
- # strip version
65
- req = req .partition ('==' )[0 ]
66
- req = req .partition ('>=' )[0 ]
67
- if req in optional :
68
- self ._optional .add (line )
69
51
70
52
def get (self , name ):
71
53
for req in self .specs :
72
- if req . startswith ( name ) :
54
+ if _pip . get_pkg_name ( req ) == name :
73
55
return req
74
56
return None
75
57
@@ -186,8 +168,10 @@ def install_pyperformance(self):
186
168
print ("installing pyperformance in the venv at %s" % self .root )
187
169
# Install pyperformance inside the virtual environment.
188
170
if pyperformance .is_dev ():
189
- basereqs = Requirements .from_file (REQUIREMENTS_FILE , [ 'psutil' ] )
171
+ basereqs = Requirements .from_file (REQUIREMENTS_FILE )
190
172
self .ensure_reqs (basereqs )
173
+ if basereqs .get ('pyperf' ):
174
+ self ._install_pyperf_optional_dependencies ()
191
175
192
176
root_dir = os .path .dirname (pyperformance .PKG_ROOT )
193
177
ec , _ , _ = _pip .install_editable (
@@ -202,9 +186,18 @@ def install_pyperformance(self):
202
186
python = self .info ,
203
187
env = self ._env ,
204
188
)
189
+ self ._install_pyperf_optional_dependencies ()
205
190
if ec != 0 :
206
191
sys .exit (ec )
207
192
193
+ def _install_pyperf_optional_dependencies (self ):
194
+ for req in PYPERF_OPTIONAL :
195
+ try :
196
+ self .ensure_reqs ([req ])
197
+ except _venv .RequirementsInstallationFailedError :
198
+ print ("WARNING: failed to install %s" % req )
199
+ pass
200
+
208
201
def ensure_reqs (self , requirements = None , * , exitonerror = False ):
209
202
# parse requirements
210
203
bench = None
@@ -216,7 +209,7 @@ def ensure_reqs(self, requirements=None, *, exitonerror=False):
216
209
217
210
# Every benchmark must depend on pyperf.
218
211
if bench is not None and not requirements .get ('pyperf' ):
219
- basereqs = Requirements .from_file (REQUIREMENTS_FILE , [ 'psutil' ] )
212
+ basereqs = Requirements .from_file (REQUIREMENTS_FILE )
220
213
pyperf_req = basereqs .get ('pyperf' )
221
214
if not pyperf_req :
222
215
raise NotImplementedError
@@ -229,21 +222,16 @@ def ensure_reqs(self, requirements=None, *, exitonerror=False):
229
222
# install requirements
230
223
try :
231
224
super ().ensure_reqs (
232
- * requirements . iter_non_optional () ,
225
+ * requirements ,
233
226
upgrade = False ,
234
227
)
235
228
except _venv .RequirementsInstallationFailedError :
236
229
if exitonerror :
237
230
sys .exit (1 )
238
231
raise # re-raise
239
232
240
- # install optional requirements
241
- for req in requirements .iter_optional ():
242
- try :
243
- super ().ensure_reqs (req , upgrade = True )
244
- except _venv .RequirementsInstallationFailedError :
245
- print ("WARNING: failed to install %s" % req )
246
- print ()
233
+ if bench is not None :
234
+ self ._install_pyperf_optional_dependencies ()
247
235
248
236
# Dump the package list and their versions: pip freeze
249
237
_pip .run_pip ('freeze' , python = self .python , env = self ._env )
0 commit comments