5
5
import os .path
6
6
import re
7
7
import shutil
8
+ import zipfile
8
9
10
+ from pip ._vendor .packaging .utils import canonicalize_name , canonicalize_version
11
+ from pip ._vendor .packaging .version import InvalidVersion , Version
12
+ from pip ._vendor .pkg_resources import Distribution
13
+
14
+ from pip ._internal .exceptions import InvalidWheelFilename , UnsupportedWheel
9
15
from pip ._internal .models .link import Link
16
+ from pip ._internal .models .wheel import Wheel
10
17
from pip ._internal .operations .build .wheel import build_wheel_pep517
11
18
from pip ._internal .operations .build .wheel_legacy import build_wheel_legacy
12
19
from pip ._internal .utils .logging import indent_log
16
23
from pip ._internal .utils .temp_dir import TempDirectory
17
24
from pip ._internal .utils .typing import MYPY_CHECK_RUNNING
18
25
from pip ._internal .utils .urls import path_to_url
26
+ from pip ._internal .utils .wheel import pkg_resources_distribution_for_wheel
19
27
from pip ._internal .vcs import vcs
20
28
21
29
if MYPY_CHECK_RUNNING :
@@ -163,9 +171,49 @@ def _always_true(_):
163
171
return True
164
172
165
173
174
+ def _get_metadata_version (dist ):
175
+ # type: (Distribution) -> Optional[Version]
176
+ for line in dist .get_metadata_lines (dist .PKG_INFO ):
177
+ if line .lower ().startswith ("metadata-version:" ):
178
+ value = line .split (":" , 1 )[- 1 ].strip ()
179
+ try :
180
+ return Version (value )
181
+ except InvalidVersion :
182
+ msg = "Invalid Metadata-Version: {}" .format (value )
183
+ raise UnsupportedWheel (msg )
184
+ raise UnsupportedWheel ("Missing Metadata-Version" )
185
+
186
+
187
+ def _verify_one (req , wheel_path ):
188
+ # type: (InstallRequirement, str) -> None
189
+ canonical_name = canonicalize_name (req .name )
190
+ w = Wheel (os .path .basename (wheel_path ))
191
+ if canonicalize_name (w .name ) != canonical_name :
192
+ raise InvalidWheelFilename (
193
+ "Wheel has unexpected file name: expected {!r}, "
194
+ "got {!r}" .format (canonical_name , w .name ),
195
+ )
196
+ with zipfile .ZipFile (wheel_path , allowZip64 = True ) as zf :
197
+ dist = pkg_resources_distribution_for_wheel (
198
+ zf , canonical_name , wheel_path ,
199
+ )
200
+ if canonicalize_version (dist .version ) != canonicalize_version (w .version ):
201
+ raise InvalidWheelFilename (
202
+ "Wheel has unexpected file name: expected {!r}, "
203
+ "got {!r}" .format (dist .version , w .version ),
204
+ )
205
+ if (_get_metadata_version (dist ) >= Version ("1.2" )
206
+ and not isinstance (dist .parsed_version , Version )):
207
+ raise UnsupportedWheel (
208
+ "Metadata 1.2 mandates PEP 440 version, "
209
+ "but {!r} is not" .format (dist .version )
210
+ )
211
+
212
+
166
213
def _build_one (
167
214
req , # type: InstallRequirement
168
215
output_dir , # type: str
216
+ verify , # type: bool
169
217
build_options , # type: List[str]
170
218
global_options , # type: List[str]
171
219
):
@@ -185,9 +233,16 @@ def _build_one(
185
233
186
234
# Install build deps into temporary directory (PEP 518)
187
235
with req .build_env :
188
- return _build_one_inside_env (
236
+ wheel_path = _build_one_inside_env (
189
237
req , output_dir , build_options , global_options
190
238
)
239
+ if wheel_path and verify :
240
+ try :
241
+ _verify_one (req , wheel_path )
242
+ except (InvalidWheelFilename , UnsupportedWheel ) as e :
243
+ logger .warning ("Built wheel for %s is invalid: %s" , req .name , e )
244
+ return None
245
+ return wheel_path
191
246
192
247
193
248
def _build_one_inside_env (
@@ -260,6 +315,7 @@ def _clean_one_legacy(req, global_options):
260
315
def build (
261
316
requirements , # type: Iterable[InstallRequirement]
262
317
wheel_cache , # type: WheelCache
318
+ verify , # type: bool
263
319
build_options , # type: List[str]
264
320
global_options , # type: List[str]
265
321
):
@@ -283,7 +339,7 @@ def build(
283
339
for req in requirements :
284
340
cache_dir = _get_cache_dir (req , wheel_cache )
285
341
wheel_file = _build_one (
286
- req , cache_dir , build_options , global_options
342
+ req , cache_dir , verify , build_options , global_options
287
343
)
288
344
if wheel_file :
289
345
# Update the link for this.
0 commit comments