1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
|
import os
import re
import shutil
import textwrap
import uuid
from dataclasses import dataclass
from enum import Enum
from hashlib import sha256
from pathlib import Path
from textwrap import dedent
from typing import Callable, Dict, List, Tuple
import pytest
from pip._internal.cli.status_codes import ERROR
from pip._internal.utils.urls import path_to_url
from tests.conftest import MockServer, ScriptFactory
from tests.lib import (
PipTestEnvironment,
TestData,
TestPipResult,
create_basic_sdist_for_package,
create_really_basic_wheel,
)
from tests.lib.server import file_response
def fake_wheel(data: TestData, wheel_path: str) -> None:
wheel_name = os.path.basename(wheel_path)
name, version, rest = wheel_name.split("-", 2)
wheel_data = create_really_basic_wheel(name, version)
data.packages.joinpath(wheel_path).write_bytes(wheel_data)
@pytest.mark.network
def test_download_if_requested(script: PipTestEnvironment) -> None:
"""
It should download (in the scratch path) and not install if requested.
"""
result = script.pip("download", "-d", "pip_downloads", "INITools==0.1")
result.did_create(Path("scratch") / "pip_downloads" / "INITools-0.1.tar.gz")
result.did_not_create(script.site_packages / "initools")
@pytest.mark.network
def test_basic_download_setuptools(script: PipTestEnvironment) -> None:
"""
It should download (in the scratch path) and not install if requested.
"""
result = script.pip("download", "setuptools")
setuptools_prefix = str(Path("scratch") / "setuptools")
assert any(os.fspath(p).startswith(setuptools_prefix) for p in result.files_created)
def test_download_wheel(script: PipTestEnvironment, data: TestData) -> None:
"""
Test using "pip download" to download a *.whl archive.
"""
result = script.pip(
"download", "--no-index", "-f", data.packages, "-d", ".", "meta"
)
result.did_create(Path("scratch") / "meta-1.0-py2.py3-none-any.whl")
result.did_not_create(script.site_packages / "piptestpackage")
@pytest.mark.network
def test_single_download_from_requirements_file(script: PipTestEnvironment) -> None:
"""
It should support download (in the scratch path) from PyPI from a
requirements file
"""
script.scratch_path.joinpath("test-req.txt").write_text(
textwrap.dedent(
"""
INITools==0.1
"""
)
)
result = script.pip(
"download",
"-r",
script.scratch_path / "test-req.txt",
"-d",
".",
)
result.did_create(Path("scratch") / "INITools-0.1.tar.gz")
result.did_not_create(script.site_packages / "initools")
@pytest.mark.network
def test_basic_download_should_download_dependencies(
script: PipTestEnvironment,
) -> None:
"""
It should download dependencies (in the scratch path)
"""
result = script.pip("download", "Paste[openid]==1.7.5.1", "-d", ".")
result.did_create(Path("scratch") / "Paste-1.7.5.1.tar.gz")
openid_tarball_prefix = str(Path("scratch") / "python-openid-")
assert any(
os.fspath(path).startswith(openid_tarball_prefix)
for path in result.files_created
)
result.did_not_create(script.site_packages / "openid")
def test_download_wheel_archive(script: PipTestEnvironment, data: TestData) -> None:
"""
It should download a wheel archive path
"""
wheel_filename = "colander-0.9.9-py2.py3-none-any.whl"
wheel_path = "/".join((data.find_links, wheel_filename))
result = script.pip("download", wheel_path, "-d", ".", "--no-deps")
result.did_create(Path("scratch") / wheel_filename)
def test_download_should_download_wheel_deps(
script: PipTestEnvironment, data: TestData
) -> None:
"""
It should download dependencies for wheels(in the scratch path)
"""
wheel_filename = "colander-0.9.9-py2.py3-none-any.whl"
dep_filename = "translationstring-1.1.tar.gz"
wheel_path = "/".join((data.find_links, wheel_filename))
result = script.pip(
"download", wheel_path, "-d", ".", "--find-links", data.find_links, "--no-index"
)
result.did_create(Path("scratch") / wheel_filename)
result.did_create(Path("scratch") / dep_filename)
@pytest.mark.network
def test_download_should_skip_existing_files(script: PipTestEnvironment) -> None:
"""
It should not download files already existing in the scratch dir
"""
script.scratch_path.joinpath("test-req.txt").write_text(
textwrap.dedent(
"""
INITools==0.1
"""
)
)
result = script.pip(
"download",
"-r",
script.scratch_path / "test-req.txt",
"-d",
".",
)
result.did_create(Path("scratch") / "INITools-0.1.tar.gz")
result.did_not_create(script.site_packages / "initools")
# adding second package to test-req.txt
script.scratch_path.joinpath("test-req.txt").write_text(
textwrap.dedent(
"""
INITools==0.1
python-openid==2.2.5
"""
)
)
# only the second package should be downloaded
result = script.pip(
"download",
"-r",
script.scratch_path / "test-req.txt",
"-d",
".",
)
openid_tarball_prefix = str(Path("scratch") / "python-openid-")
assert any(
os.fspath(path).startswith(openid_tarball_prefix)
for path in result.files_created
)
result.did_not_create(Path("scratch") / "INITools-0.1.tar.gz")
result.did_not_create(script.site_packages / "initools")
result.did_not_create(script.site_packages / "openid")
@pytest.mark.network
def test_download_vcs_link(script: PipTestEnvironment) -> None:
"""
It should allow -d flag for vcs links, regression test for issue #798.
"""
result = script.pip(
"download", "-d", ".", "git+https://github.com/pypa/pip-test-package.git"
)
result.did_create(Path("scratch") / "pip-test-package-0.1.1.zip")
result.did_not_create(script.site_packages / "piptestpackage")
def test_only_binary_set_then_download_specific_platform(
script: PipTestEnvironment, data: TestData
) -> None:
"""
Confirm that specifying an interpreter/platform constraint
is allowed when ``--only-binary=:all:`` is set.
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"linux_x86_64",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
def test_no_deps_set_then_download_specific_platform(
script: PipTestEnvironment, data: TestData
) -> None:
"""
Confirm that specifying an interpreter/platform constraint
is allowed when ``--no-deps`` is set.
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--no-deps",
"--dest",
".",
"--platform",
"linux_x86_64",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
def test_download_specific_platform_fails(
script: PipTestEnvironment, data: TestData
) -> None:
"""
Confirm that specifying an interpreter/platform constraint
enforces that ``--no-deps`` or ``--only-binary=:all:`` is set.
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--dest",
".",
"--platform",
"linux_x86_64",
"fake",
expect_error=True,
)
assert "--only-binary=:all:" in result.stderr
def test_no_binary_set_then_download_specific_platform_fails(
script: PipTestEnvironment, data: TestData
) -> None:
"""
Confirm that specifying an interpreter/platform constraint
enforces that ``--only-binary=:all:`` is set without ``--no-binary``.
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--no-binary=fake",
"--dest",
".",
"--platform",
"linux_x86_64",
"fake",
expect_error=True,
)
assert "--only-binary=:all:" in result.stderr
def test_download_specify_platform(script: PipTestEnvironment, data: TestData) -> None:
"""
Test using "pip download --platform" to download a .whl archive
supported for a specific platform
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
# Confirm that universal wheels are returned even for specific
# platforms.
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"linux_x86_64",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"macosx_10_9_x86_64",
"fake",
)
data.reset()
fake_wheel(data, "fake-1.0-py2.py3-none-macosx_10_9_x86_64.whl")
fake_wheel(data, "fake-2.0-py2.py3-none-linux_x86_64.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"macosx_10_10_x86_64",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-macosx_10_9_x86_64.whl")
# OSX platform wheels are not backward-compatible.
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"macosx_10_8_x86_64",
"fake",
expect_error=True,
)
# No linux wheel provided for this version.
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"linux_x86_64",
"fake==1",
expect_error=True,
)
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"linux_x86_64",
"fake==2",
)
result.did_create(Path("scratch") / "fake-2.0-py2.py3-none-linux_x86_64.whl")
# Test with multiple supported platforms specified.
data.reset()
fake_wheel(data, "fake-3.0-py2.py3-none-linux_x86_64.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"manylinux1_x86_64",
"--platform",
"linux_x86_64",
"--platform",
"any",
"fake==3",
)
result.did_create(Path("scratch") / "fake-3.0-py2.py3-none-linux_x86_64.whl")
class TestDownloadPlatformManylinuxes:
"""
"pip download --platform" downloads a .whl archive supported for
manylinux platforms.
"""
@pytest.mark.parametrize(
"platform",
[
"linux_x86_64",
"manylinux1_x86_64",
"manylinux2010_x86_64",
"manylinux2014_x86_64",
],
)
def test_download_universal(
self, platform: str, script: PipTestEnvironment, data: TestData
) -> None:
"""
Universal wheels are returned even for specific platforms.
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
platform,
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
@pytest.mark.parametrize(
"wheel_abi,platform",
[
("manylinux1_x86_64", "manylinux1_x86_64"),
("manylinux1_x86_64", "manylinux2010_x86_64"),
("manylinux2010_x86_64", "manylinux2010_x86_64"),
("manylinux1_x86_64", "manylinux2014_x86_64"),
("manylinux2010_x86_64", "manylinux2014_x86_64"),
("manylinux2014_x86_64", "manylinux2014_x86_64"),
],
)
def test_download_compatible_manylinuxes(
self,
wheel_abi: str,
platform: str,
script: PipTestEnvironment,
data: TestData,
) -> None:
"""
Earlier manylinuxes are compatible with later manylinuxes.
"""
wheel = f"fake-1.0-py2.py3-none-{wheel_abi}.whl"
fake_wheel(data, wheel)
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
platform,
"fake",
)
result.did_create(Path("scratch") / wheel)
def test_explicit_platform_only(
self, data: TestData, script: PipTestEnvironment
) -> None:
"""
When specifying the platform, manylinux1 needs to be the
explicit platform--it won't ever be added to the compatible
tags.
"""
fake_wheel(data, "fake-1.0-py2.py3-none-linux_x86_64.whl")
script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--platform",
"linux_x86_64",
"fake",
)
def test_download__python_version(script: PipTestEnvironment, data: TestData) -> None:
"""
Test using "pip download --python-version" to download a .whl archive
supported for a specific interpreter
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"2",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"3",
"fake",
)
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"27",
"fake",
)
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"33",
"fake",
)
data.reset()
fake_wheel(data, "fake-1.0-py2-none-any.whl")
fake_wheel(data, "fake-2.0-py3-none-any.whl")
# No py3 provided for version 1.
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"3",
"fake==1.0",
expect_error=True,
)
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"2",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"26",
"fake",
)
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"3",
"fake",
)
result.did_create(Path("scratch") / "fake-2.0-py3-none-any.whl")
def make_wheel_with_python_requires(
script: PipTestEnvironment, package_name: str, python_requires: str
) -> Path:
"""
Create a wheel using the given python_requires.
:return: the path to the wheel file.
"""
package_dir = script.scratch_path / package_name
package_dir.mkdir()
text = textwrap.dedent(
"""\
from setuptools import setup
setup(name='{}',
python_requires='{}',
version='1.0')
"""
).format(package_name, python_requires)
package_dir.joinpath("setup.py").write_text(text)
script.run(
"python",
"setup.py",
"bdist_wheel",
"--universal",
cwd=package_dir,
)
file_name = f"{package_name}-1.0-py2.py3-none-any.whl"
return package_dir / "dist" / file_name
def test_download__python_version_used_for_python_requires(
script: PipTestEnvironment, data: TestData
) -> None:
"""
Test that --python-version is used for the Requires-Python check.
"""
wheel_path = make_wheel_with_python_requires(
script,
"mypackage",
python_requires="==3.2",
)
wheel_dir = os.path.dirname(wheel_path)
def make_args(python_version: str) -> List[str]:
return [
"download",
"--no-index",
"--find-links",
wheel_dir,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
python_version,
"mypackage==1.0",
]
args = make_args("33")
result = script.pip(*args, expect_error=True)
expected_err = (
"ERROR: Package 'mypackage' requires a different Python: "
"3.3.0 not in '==3.2'"
)
assert expected_err in result.stderr, f"stderr: {result.stderr}"
# Now try with a --python-version that satisfies the Requires-Python.
args = make_args("32")
script.pip(*args) # no exception
def test_download_ignore_requires_python_dont_fail_with_wrong_python(
script: PipTestEnvironment,
) -> None:
"""
Test that --ignore-requires-python ignores Requires-Python check.
"""
wheel_path = make_wheel_with_python_requires(
script,
"mypackage",
python_requires="==999",
)
wheel_dir = os.path.dirname(wheel_path)
result = script.pip(
"download",
"--ignore-requires-python",
"--no-index",
"--find-links",
wheel_dir,
"--only-binary=:all:",
"--dest",
".",
"mypackage==1.0",
)
result.did_create(Path("scratch") / "mypackage-1.0-py2.py3-none-any.whl")
def test_download_specify_abi(script: PipTestEnvironment, data: TestData) -> None:
"""
Test using "pip download --abi" to download a .whl archive
supported for a specific abi
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--implementation",
"fk",
"--abi",
"fake_abi",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--implementation",
"fk",
"--abi",
"none",
"fake",
)
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--abi",
"cp27m",
"fake",
)
data.reset()
fake_wheel(data, "fake-1.0-fk2-fakeabi-fake_platform.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"2",
"--implementation",
"fk",
"--platform",
"fake_platform",
"--abi",
"fakeabi",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-fk2-fakeabi-fake_platform.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--implementation",
"fk",
"--platform",
"fake_platform",
"--abi",
"none",
"fake",
expect_error=True,
)
data.reset()
fake_wheel(data, "fake-1.0-fk2-otherabi-fake_platform.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--python-version",
"2",
"--implementation",
"fk",
"--platform",
"fake_platform",
"--abi",
"fakeabi",
"--abi",
"otherabi",
"--abi",
"none",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-fk2-otherabi-fake_platform.whl")
def test_download_specify_implementation(
script: PipTestEnvironment, data: TestData
) -> None:
"""
Test using "pip download --abi" to download a .whl archive
supported for a specific abi
"""
fake_wheel(data, "fake-1.0-py2.py3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--implementation",
"fk",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
data.reset()
fake_wheel(data, "fake-1.0-fk3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--implementation",
"fk",
"--python-version",
"3",
"fake",
)
result.did_create(Path("scratch") / "fake-1.0-fk3-none-any.whl")
result = script.pip(
"download",
"--no-index",
"--find-links",
data.find_links,
"--only-binary=:all:",
"--dest",
".",
"--implementation",
"fk",
"--python-version",
"2",
"fake",
expect_error=True,
)
def test_download_exit_status_code_when_no_requirements(
script: PipTestEnvironment,
) -> None:
"""
Test download exit status code when no requirements specified
"""
result = script.pip("download", expect_error=True)
assert "You must give at least one requirement to download" in result.stderr
assert result.returncode == ERROR
def test_download_exit_status_code_when_blank_requirements_file(
script: PipTestEnvironment,
) -> None:
"""
Test download exit status code when blank requirements file specified
"""
script.scratch_path.joinpath("blank.txt").write_text("\n")
script.pip("download", "-r", "blank.txt")
def test_download_prefer_binary_when_tarball_higher_than_wheel(
script: PipTestEnvironment, data: TestData
) -> None:
fake_wheel(data, "source-0.8-py2.py3-none-any.whl")
result = script.pip(
"download",
"--prefer-binary",
"--no-index",
"-f",
data.packages,
"-d",
".",
"source",
)
result.did_create(Path("scratch") / "source-0.8-py2.py3-none-any.whl")
result.did_not_create(Path("scratch") / "source-1.0.tar.gz")
def test_prefer_binary_tarball_higher_than_wheel_req_file(
script: PipTestEnvironment, data: TestData
) -> None:
fake_wheel(data, "source-0.8-py2.py3-none-any.whl")
script.scratch_path.joinpath("test-req.txt").write_text(
textwrap.dedent(
"""
--prefer-binary
source
"""
)
)
result = script.pip(
"download",
"-r",
script.scratch_path / "test-req.txt",
"--no-index",
"-f",
data.packages,
"-d",
".",
)
result.did_create(Path("scratch") / "source-0.8-py2.py3-none-any.whl")
result.did_not_create(Path("scratch") / "source-1.0.tar.gz")
def test_download_prefer_binary_when_wheel_doesnt_satisfy_req(
script: PipTestEnvironment, data: TestData
) -> None:
fake_wheel(data, "source-0.8-py2.py3-none-any.whl")
script.scratch_path.joinpath("test-req.txt").write_text(
textwrap.dedent(
"""
source>0.9
"""
)
)
result = script.pip(
"download",
"--prefer-binary",
"--no-index",
"-f",
data.packages,
"-d",
".",
"-r",
script.scratch_path / "test-req.txt",
)
result.did_create(Path("scratch") / "source-1.0.tar.gz")
result.did_not_create(Path("scratch") / "source-0.8-py2.py3-none-any.whl")
def test_prefer_binary_when_wheel_doesnt_satisfy_req_req_file(
script: PipTestEnvironment, data: TestData
) -> None:
fake_wheel(data, "source-0.8-py2.py3-none-any.whl")
script.scratch_path.joinpath("test-req.txt").write_text(
textwrap.dedent(
"""
--prefer-binary
source>0.9
"""
)
)
result = script.pip(
"download",
"--no-index",
"-f",
data.packages,
"-d",
".",
"-r",
script.scratch_path / "test-req.txt",
)
result.did_create(Path("scratch") / "source-1.0.tar.gz")
result.did_not_create(Path("scratch") / "source-0.8-py2.py3-none-any.whl")
def test_download_prefer_binary_when_only_tarball_exists(
script: PipTestEnvironment, data: TestData
) -> None:
result = script.pip(
"download",
"--prefer-binary",
"--no-index",
"-f",
data.packages,
"-d",
".",
"source",
)
result.did_create(Path("scratch") / "source-1.0.tar.gz")
def test_prefer_binary_when_only_tarball_exists_req_file(
script: PipTestEnvironment, data: TestData
) -> None:
script.scratch_path.joinpath("test-req.txt").write_text(
textwrap.dedent(
"""
--prefer-binary
source
"""
)
)
result = script.pip(
"download",
"--no-index",
"-f",
data.packages,
"-d",
".",
"-r",
script.scratch_path / "test-req.txt",
)
result.did_create(Path("scratch") / "source-1.0.tar.gz")
@pytest.fixture(scope="session")
def shared_script(
tmpdir_factory: pytest.TempPathFactory, script_factory: ScriptFactory
) -> PipTestEnvironment:
tmpdir = tmpdir_factory.mktemp("download_shared_script")
script = script_factory(tmpdir.joinpath("workspace"))
return script
def test_download_file_url(
shared_script: PipTestEnvironment, shared_data: TestData, tmpdir: Path
) -> None:
download_dir = tmpdir / "download"
download_dir.mkdir()
downloaded_path = download_dir / "simple-1.0.tar.gz"
simple_pkg = shared_data.packages / "simple-1.0.tar.gz"
shared_script.pip(
"download",
"-d",
str(download_dir),
"--no-index",
simple_pkg.as_uri(),
)
assert downloaded_path.exists()
assert simple_pkg.read_bytes() == downloaded_path.read_bytes()
def test_download_file_url_existing_ok_download(
shared_script: PipTestEnvironment, shared_data: TestData, tmpdir: Path
) -> None:
download_dir = tmpdir / "download"
download_dir.mkdir()
downloaded_path = download_dir / "simple-1.0.tar.gz"
fake_existing_package = shared_data.packages / "simple-2.0.tar.gz"
shutil.copy(str(fake_existing_package), str(downloaded_path))
downloaded_path_bytes = downloaded_path.read_bytes()
simple_pkg = shared_data.packages / "simple-1.0.tar.gz"
url = f"{simple_pkg.as_uri()}#sha256={sha256(downloaded_path_bytes).hexdigest()}"
shared_script.pip("download", "-d", str(download_dir), url)
assert downloaded_path_bytes == downloaded_path.read_bytes()
def test_download_file_url_existing_bad_download(
shared_script: PipTestEnvironment, shared_data: TestData, tmpdir: Path
) -> None:
download_dir = tmpdir / "download"
download_dir.mkdir()
downloaded_path = download_dir / "simple-1.0.tar.gz"
fake_existing_package = shared_data.packages / "simple-2.0.tar.gz"
shutil.copy(str(fake_existing_package), str(downloaded_path))
simple_pkg = shared_data.packages / "simple-1.0.tar.gz"
simple_pkg_bytes = simple_pkg.read_bytes()
url = f"{simple_pkg.as_uri()}#sha256={sha256(simple_pkg_bytes).hexdigest()}"
result = shared_script.pip(
"download",
"-d",
str(download_dir),
url,
allow_stderr_warning=True, # bad hash
)
assert simple_pkg_bytes == downloaded_path.read_bytes()
assert "WARNING: Previously-downloaded file" in result.stderr
assert "has bad hash. Re-downloading." in result.stderr
def test_download_http_url_bad_hash(
shared_script: PipTestEnvironment,
shared_data: TestData,
tmpdir: Path,
mock_server: MockServer,
) -> None:
"""
If already-downloaded file has bad checksum, re-download.
"""
download_dir = tmpdir / "download"
download_dir.mkdir()
downloaded_path = download_dir / "simple-1.0.tar.gz"
fake_existing_package = shared_data.packages / "simple-2.0.tar.gz"
shutil.copy(str(fake_existing_package), str(downloaded_path))
simple_pkg = shared_data.packages / "simple-1.0.tar.gz"
simple_pkg_bytes = simple_pkg.read_bytes()
digest = sha256(simple_pkg_bytes).hexdigest()
mock_server.set_responses([file_response(simple_pkg)])
mock_server.start()
base_address = f"http://{mock_server.host}:{mock_server.port}"
url = f"{base_address}/simple-1.0.tar.gz#sha256={digest}"
result = shared_script.pip(
"download",
"-d",
str(download_dir),
url,
allow_stderr_warning=True, # bad hash
)
assert simple_pkg_bytes == downloaded_path.read_bytes()
assert "WARNING: Previously-downloaded file" in result.stderr
assert "has bad hash. Re-downloading." in result.stderr
mock_server.stop()
requests = mock_server.get_requests()
assert len(requests) == 1
assert requests[0]["PATH_INFO"] == "/simple-1.0.tar.gz"
assert requests[0]["HTTP_ACCEPT_ENCODING"] == "identity"
def test_download_editable(
script: PipTestEnvironment, data: TestData, tmpdir: Path
) -> None:
"""
Test 'pip download' of editables in requirement file.
"""
editable_path = str(data.src / "simplewheel-1.0").replace(os.path.sep, "/")
requirements_path = tmpdir / "requirements.txt"
requirements_path.write_text("-e " + editable_path + "\n")
download_dir = tmpdir / "download_dir"
script.pip(
"download", "--no-deps", "-r", str(requirements_path), "-d", str(download_dir)
)
downloads = os.listdir(download_dir)
assert len(downloads) == 1
assert downloads[0].endswith(".zip")
def test_download_use_pep517_propagation(
script: PipTestEnvironment, tmpdir: Path, common_wheels: Path
) -> None:
"""
Check that --use-pep517 applies not just to the requirements specified
on the command line, but to their dependencies too.
"""
create_basic_sdist_for_package(script, "fake_proj", "1.0", depends=["fake_dep"])
# If --use-pep517 is in effect, then setup.py should be running in an isolated
# environment that doesn't have pip in it.
create_basic_sdist_for_package(
script,
"fake_dep",
"1.0",
setup_py_prelude=textwrap.dedent(
"""\
try:
import pip
except ImportError:
pass
else:
raise Exception(f"not running in isolation")
"""
),
)
download_dir = tmpdir / "download_dir"
script.pip(
"download",
f"--dest={download_dir}",
"--no-index",
f"--find-links={common_wheels}",
f"--find-links={script.scratch_path}",
"--use-pep517",
"fake_proj",
)
downloads = os.listdir(download_dir)
assert len(downloads) == 2
class MetadataKind(Enum):
"""All the types of values we might be provided for the data-dist-info-metadata
attribute from PEP 658."""
# Valid: will read metadata from the dist instead.
No = "none"
# Valid: will read the .metadata file, but won't check its hash.
Unhashed = "unhashed"
# Valid: will read the .metadata file and check its hash matches.
Sha256 = "sha256"
# Invalid: will error out after checking the hash.
WrongHash = "wrong-hash"
# Invalid: will error out after failing to fetch the .metadata file.
NoFile = "no-file"
@dataclass(frozen=True)
class Package:
"""Mock package structure used to generate a PyPI repository.
Package name and version should correspond to sdists (.tar.gz files) in our test
data."""
name: str
version: str
filename: str
metadata: MetadataKind
# This will override any dependencies specified in the actual dist's METADATA.
requires_dist: Tuple[str, ...] = ()
def metadata_filename(self) -> str:
"""This is specified by PEP 658."""
return f"{self.filename}.metadata"
def generate_additional_tag(self) -> str:
"""This gets injected into the <a> tag in the generated PyPI index page for this
package."""
if self.metadata == MetadataKind.No:
return ""
if self.metadata in [MetadataKind.Unhashed, MetadataKind.NoFile]:
return 'data-dist-info-metadata="true"'
if self.metadata == MetadataKind.WrongHash:
return 'data-dist-info-metadata="sha256=WRONG-HASH"'
assert self.metadata == MetadataKind.Sha256
checksum = sha256(self.generate_metadata()).hexdigest()
return f'data-dist-info-metadata="sha256={checksum}"'
def requires_str(self) -> str:
if not self.requires_dist:
return ""
joined = " and ".join(self.requires_dist)
return f"Requires-Dist: {joined}"
def generate_metadata(self) -> bytes:
"""This is written to `self.metadata_filename()` and will override the actual
dist's METADATA, unless `self.metadata == MetadataKind.NoFile`."""
return dedent(
f"""\
Metadata-Version: 2.1
Name: {self.name}
Version: {self.version}
{self.requires_str()}
"""
).encode("utf-8")
@pytest.fixture(scope="function")
def write_index_html_content(tmpdir: Path) -> Callable[[str], Path]:
"""Generate a PyPI package index.html within a temporary local directory."""
html_dir = tmpdir / "index_html_content"
html_dir.mkdir()
def generate_index_html_subdir(index_html: str) -> Path:
"""Create a new subdirectory after a UUID and write an index.html."""
new_subdir = html_dir / uuid.uuid4().hex
new_subdir.mkdir()
with open(new_subdir / "index.html", "w") as f:
f.write(index_html)
return new_subdir
return generate_index_html_subdir
@pytest.fixture(scope="function")
def html_index_for_packages(
shared_data: TestData,
write_index_html_content: Callable[[str], Path],
) -> Callable[..., Path]:
"""Generate a PyPI HTML package index within a local directory pointing to
blank data."""
def generate_html_index_for_packages(packages: Dict[str, List[Package]]) -> Path:
"""
Produce a PyPI directory structure pointing to the specified packages.
"""
# (1) Generate the content for a PyPI index.html.
pkg_links = "\n".join(
f' <a href="{pkg}/index.html">{pkg}</a>' for pkg in packages.keys()
)
index_html = f"""\
<!DOCTYPE html>
<html>
<head>
<meta name="pypi:repository-version" content="1.0">
<title>Simple index</title>
</head>
<body>
{pkg_links}
</body>
</html>"""
# (2) Generate the index.html in a new subdirectory of the temp directory.
index_html_subdir = write_index_html_content(index_html)
# (3) Generate subdirectories for individual packages, each with their own
# index.html.
for pkg, links in packages.items():
pkg_subdir = index_html_subdir / pkg
pkg_subdir.mkdir()
download_links: List[str] = []
for package_link in links:
# (3.1) Generate the <a> tag which pip can crawl pointing to this
# specific package version.
download_links.append(
f' <a href="{package_link.filename}" {package_link.generate_additional_tag()}>{package_link.filename}</a><br/>' # noqa: E501
)
# (3.2) Copy over the corresponding file in `shared_data.packages`.
shutil.copy(
shared_data.packages / package_link.filename,
pkg_subdir / package_link.filename,
)
# (3.3) Write a metadata file, if applicable.
if package_link.metadata != MetadataKind.NoFile:
with open(pkg_subdir / package_link.metadata_filename(), "wb") as f:
f.write(package_link.generate_metadata())
# (3.4) After collating all the download links and copying over the files,
# write an index.html with the generated download links for each
# copied file for this specific package name.
download_links_str = "\n".join(download_links)
pkg_index_content = f"""\
<!DOCTYPE html>
<html>
<head>
<meta name="pypi:repository-version" content="1.0">
<title>Links for {pkg}</title>
</head>
<body>
<h1>Links for {pkg}</h1>
{download_links_str}
</body>
</html>"""
with open(pkg_subdir / "index.html", "w") as f:
f.write(pkg_index_content)
return index_html_subdir
return generate_html_index_for_packages
@pytest.fixture(scope="function")
def download_generated_html_index(
script: PipTestEnvironment,
html_index_for_packages: Callable[[Dict[str, List[Package]]], Path],
tmpdir: Path,
) -> Callable[..., Tuple[TestPipResult, Path]]:
"""Execute `pip download` against a generated PyPI index."""
download_dir = tmpdir / "download_dir"
def run_for_generated_index(
packages: Dict[str, List[Package]],
args: List[str],
allow_error: bool = False,
) -> Tuple[TestPipResult, Path]:
"""
Produce a PyPI directory structure pointing to the specified packages, then
execute `pip download -i ...` pointing to our generated index.
"""
index_dir = html_index_for_packages(packages)
pip_args = [
"download",
"-d",
str(download_dir),
"-i",
path_to_url(str(index_dir)),
*args,
]
result = script.pip(*pip_args, allow_error=allow_error)
return (result, download_dir)
return run_for_generated_index
# The package database we generate for testing PEP 658 support.
_simple_packages: Dict[str, List[Package]] = {
"simple": [
Package("simple", "1.0", "simple-1.0.tar.gz", MetadataKind.Sha256),
Package("simple", "2.0", "simple-2.0.tar.gz", MetadataKind.No),
# This will raise a hashing error.
Package("simple", "3.0", "simple-3.0.tar.gz", MetadataKind.WrongHash),
],
"simple2": [
# Override the dependencies here in order to force pip to download
# simple-1.0.tar.gz as well.
Package(
"simple2",
"1.0",
"simple2-1.0.tar.gz",
MetadataKind.Unhashed,
("simple==1.0",),
),
# This will raise an error when pip attempts to fetch the metadata file.
Package("simple2", "2.0", "simple2-2.0.tar.gz", MetadataKind.NoFile),
],
"colander": [
# Ensure we can read the dependencies from a metadata file within a wheel
# *without* PEP 658 metadata.
Package(
"colander", "0.9.9", "colander-0.9.9-py2.py3-none-any.whl", MetadataKind.No
),
],
"compilewheel": [
# Ensure we can override the dependencies of a wheel file by injecting PEP
# 658 metadata.
Package(
"compilewheel",
"1.0",
"compilewheel-1.0-py2.py3-none-any.whl",
MetadataKind.Unhashed,
("simple==1.0",),
),
],
"has-script": [
# Ensure we check PEP 658 metadata hashing errors for wheel files.
Package(
"has-script",
"1.0",
"has.script-1.0-py2.py3-none-any.whl",
MetadataKind.WrongHash,
),
],
"translationstring": [
Package(
"translationstring", "1.1", "translationstring-1.1.tar.gz", MetadataKind.No
),
],
"priority": [
# Ensure we check for a missing metadata file for wheels.
Package(
"priority", "1.0", "priority-1.0-py2.py3-none-any.whl", MetadataKind.NoFile
),
],
}
@pytest.mark.parametrize(
"requirement_to_download, expected_outputs",
[
("simple2==1.0", ["simple-1.0.tar.gz", "simple2-1.0.tar.gz"]),
("simple==2.0", ["simple-2.0.tar.gz"]),
(
"colander",
["colander-0.9.9-py2.py3-none-any.whl", "translationstring-1.1.tar.gz"],
),
(
"compilewheel",
["compilewheel-1.0-py2.py3-none-any.whl", "simple-1.0.tar.gz"],
),
],
)
def test_download_metadata(
download_generated_html_index: Callable[..., Tuple[TestPipResult, Path]],
requirement_to_download: str,
expected_outputs: List[str],
) -> None:
"""Verify that if a data-dist-info-metadata attribute is present, then it is used
instead of the actual dist's METADATA."""
_, download_dir = download_generated_html_index(
_simple_packages,
[requirement_to_download],
)
assert sorted(os.listdir(download_dir)) == expected_outputs
@pytest.mark.parametrize(
"requirement_to_download, real_hash",
[
(
"simple==3.0",
"95e0f200b6302989bcf2cead9465cf229168295ea330ca30d1ffeab5c0fed996",
),
(
"has-script",
"16ba92d7f6f992f6de5ecb7d58c914675cf21f57f8e674fb29dcb4f4c9507e5b",
),
],
)
def test_incorrect_metadata_hash(
download_generated_html_index: Callable[..., Tuple[TestPipResult, Path]],
requirement_to_download: str,
real_hash: str,
) -> None:
"""Verify that if a hash for data-dist-info-metadata is provided, it must match the
actual hash of the metadata file."""
result, _ = download_generated_html_index(
_simple_packages,
[requirement_to_download],
allow_error=True,
)
assert result.returncode != 0
expected_msg = f"""\
Expected sha256 WRONG-HASH
Got {real_hash}"""
assert expected_msg in result.stderr
@pytest.mark.parametrize(
"requirement_to_download, expected_url",
[
("simple2==2.0", "simple2-2.0.tar.gz.metadata"),
("priority", "priority-1.0-py2.py3-none-any.whl.metadata"),
],
)
def test_metadata_not_found(
download_generated_html_index: Callable[..., Tuple[TestPipResult, Path]],
requirement_to_download: str,
expected_url: str,
) -> None:
"""Verify that if a data-dist-info-metadata attribute is provided, that pip will
fetch the .metadata file at the location specified by PEP 658, and error
if unavailable."""
result, _ = download_generated_html_index(
_simple_packages,
[requirement_to_download],
allow_error=True,
)
assert result.returncode != 0
expected_re = re.escape(expected_url)
pattern = re.compile(
f"ERROR: 404 Client Error: FileNotFoundError for url:.*{expected_re}"
)
assert pattern.search(result.stderr), (pattern, result.stderr)
|