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
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
|
#!/usr/bin/env python
"""Create a "virtual" Python installation"""
# fmt: off
import os # isort:skip
import sys # isort:skip
# If we are running in a new interpreter to create a virtualenv,
# we do NOT want paths from our existing location interfering with anything,
# So we remove this file's directory from sys.path - most likely to be
# the previous interpreter's site-packages. Solves #705, #763, #779
if os.environ.get("VIRTUALENV_INTERPRETER_RUNNING"):
for path in sys.path[:]:
if os.path.realpath(os.path.dirname(__file__)) == os.path.realpath(path):
sys.path.remove(path)
# fmt: on
import base64
import codecs
import distutils.spawn
import distutils.sysconfig
import errno
import glob
import logging
import optparse
import os
import re
import shutil
import struct
import subprocess
import sys
import textwrap
import zlib
from distutils.util import strtobool
from os.path import join
try:
import ConfigParser
except ImportError:
# noinspection PyPep8Naming
import configparser as ConfigParser
__version__ = "16.2.0"
virtualenv_version = __version__ # legacy
DEBUG = os.environ.get("_VIRTUALENV_DEBUG", None) == "1"
if sys.version_info < (2, 7):
print("ERROR: {}".format(sys.exc_info()[1]))
print("ERROR: this script requires Python 2.7 or greater.")
sys.exit(101)
try:
# noinspection PyUnresolvedReferences,PyUnboundLocalVariable
basestring
except NameError:
basestring = str
PY_VERSION = "python{}.{}".format(sys.version_info[0], sys.version_info[1])
IS_JYTHON = sys.platform.startswith("java")
IS_PYPY = hasattr(sys, "pypy_version_info")
IS_WIN = sys.platform == "win32"
IS_CYGWIN = sys.platform == "cygwin"
IS_DARWIN = sys.platform == "darwin"
ABI_FLAGS = getattr(sys, "abiflags", "")
USER_DIR = os.path.expanduser("~")
if IS_WIN:
DEFAULT_STORAGE_DIR = os.path.join(USER_DIR, "virtualenv")
else:
DEFAULT_STORAGE_DIR = os.path.join(USER_DIR, ".virtualenv")
DEFAULT_CONFIG_FILE = os.path.join(DEFAULT_STORAGE_DIR, "virtualenv.ini")
if IS_PYPY:
EXPECTED_EXE = "pypy"
elif IS_JYTHON:
EXPECTED_EXE = "jython"
else:
EXPECTED_EXE = "python"
# Return a mapping of version -> Python executable
# Only provided for Windows, where the information in the registry is used
if not IS_WIN:
def get_installed_pythons():
return {}
else:
try:
import winreg
except ImportError:
# noinspection PyUnresolvedReferences
import _winreg as winreg
def get_installed_pythons():
exes = dict()
# If both system and current user installations are found for a
# particular Python version, the current user one is used
for key in (winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER):
try:
python_core = winreg.CreateKey(key, "Software\\Python\\PythonCore")
except WindowsError:
# No registered Python installations
continue
i = 0
while True:
try:
version = winreg.EnumKey(python_core, i)
i += 1
try:
at_path = winreg.QueryValue(python_core, "{}\\InstallPath".format(version))
except WindowsError:
continue
exes[version] = join(at_path, "python.exe")
except WindowsError:
break
winreg.CloseKey(python_core)
# For versions that track separate 32-bit (`X.Y-32`) & 64-bit (`X-Y`)
# installation registrations, add a `X.Y-64` version tag and make the
# extensionless `X.Y` version tag represent the 64-bit installation if
# available or 32-bit if it is not
updated = {}
for ver in exes:
if ver < "3.5":
continue
if ver.endswith("-32"):
base_ver = ver[:-3]
if base_ver not in exes:
updated[base_ver] = exes[ver]
else:
updated[ver + "-64"] = exes[ver]
exes.update(updated)
# Add the major versions
# Sort the keys, then repeatedly update the major version entry
# Last executable (i.e., highest version) wins with this approach,
# 64-bit over 32-bit if both are found
for ver in sorted(exes):
exes[ver[0]] = exes[ver]
return exes
REQUIRED_MODULES = [
"os",
"posix",
"posixpath",
"nt",
"ntpath",
"genericpath",
"fnmatch",
"locale",
"encodings",
"codecs",
"stat",
"UserDict",
"readline",
"copy_reg",
"types",
"re",
"sre",
"sre_parse",
"sre_constants",
"sre_compile",
"zlib",
]
REQUIRED_FILES = ["lib-dynload", "config"]
MAJOR, MINOR = sys.version_info[:2]
if MAJOR == 2:
if MINOR >= 6:
REQUIRED_MODULES.extend(["warnings", "linecache", "_abcoll", "abc"])
if MINOR >= 7:
REQUIRED_MODULES.extend(["_weakrefset"])
elif MAJOR == 3:
# Some extra modules are needed for Python 3, but different ones
# for different versions.
REQUIRED_MODULES.extend(
[
"_abcoll",
"warnings",
"linecache",
"abc",
"io",
"_weakrefset",
"copyreg",
"tempfile",
"random",
"__future__",
"collections",
"keyword",
"tarfile",
"shutil",
"struct",
"copy",
"tokenize",
"token",
"functools",
"heapq",
"bisect",
"weakref",
"reprlib",
]
)
if MINOR >= 2:
REQUIRED_FILES[-1] = "config-{}".format(MAJOR)
if MINOR >= 3:
import sysconfig
platform_dir = sysconfig.get_config_var("PLATDIR")
REQUIRED_FILES.append(platform_dir)
REQUIRED_MODULES.extend(["base64", "_dummy_thread", "hashlib", "hmac", "imp", "importlib", "rlcompleter"])
if MINOR >= 4:
REQUIRED_MODULES.extend(["operator", "_collections_abc", "_bootlocale"])
if MINOR >= 6:
REQUIRED_MODULES.extend(["enum"])
if IS_PYPY:
# these are needed to correctly display the exceptions that may happen
# during the bootstrap
REQUIRED_MODULES.extend(["traceback", "linecache"])
if MAJOR == 3:
# _functools is needed to import locale during stdio initialization and
# needs to be copied on PyPy because it's not built in
REQUIRED_MODULES.append("_functools")
class Logger(object):
"""
Logging object for use in command-line script. Allows ranges of
levels, to avoid some redundancy of displayed information.
"""
DEBUG = logging.DEBUG
INFO = logging.INFO
NOTIFY = (logging.INFO + logging.WARN) / 2
WARN = WARNING = logging.WARN
ERROR = logging.ERROR
FATAL = logging.FATAL
LEVELS = [DEBUG, INFO, NOTIFY, WARN, ERROR, FATAL]
def __init__(self, consumers):
self.consumers = consumers
self.indent = 0
self.in_progress = None
self.in_progress_hanging = False
def debug(self, msg, *args, **kw):
self.log(self.DEBUG, msg, *args, **kw)
def info(self, msg, *args, **kw):
self.log(self.INFO, msg, *args, **kw)
def notify(self, msg, *args, **kw):
self.log(self.NOTIFY, msg, *args, **kw)
def warn(self, msg, *args, **kw):
self.log(self.WARN, msg, *args, **kw)
def error(self, msg, *args, **kw):
self.log(self.ERROR, msg, *args, **kw)
def fatal(self, msg, *args, **kw):
self.log(self.FATAL, msg, *args, **kw)
def log(self, level, msg, *args, **kw):
if args:
if kw:
raise TypeError("You may give positional or keyword arguments, not both")
args = args or kw
rendered = None
for consumer_level, consumer in self.consumers:
if self.level_matches(level, consumer_level):
if self.in_progress_hanging and consumer in (sys.stdout, sys.stderr):
self.in_progress_hanging = False
print("")
sys.stdout.flush()
if rendered is None:
if args:
rendered = msg % args
else:
rendered = msg
rendered = " " * self.indent + rendered
if hasattr(consumer, "write"):
consumer.write(rendered + "\n")
else:
consumer(rendered)
def start_progress(self, msg):
assert not self.in_progress, "Tried to start_progress({!r}) while in_progress {!r}".format(
msg, self.in_progress
)
if self.level_matches(self.NOTIFY, self._stdout_level()):
print(msg)
sys.stdout.flush()
self.in_progress_hanging = True
else:
self.in_progress_hanging = False
self.in_progress = msg
def end_progress(self, msg="done."):
assert self.in_progress, "Tried to end_progress without start_progress"
if self.stdout_level_matches(self.NOTIFY):
if not self.in_progress_hanging:
# Some message has been printed out since start_progress
print("...{}{}".format(self.in_progress, msg))
sys.stdout.flush()
else:
print(msg)
sys.stdout.flush()
self.in_progress = None
self.in_progress_hanging = False
def show_progress(self):
"""If we are in a progress scope, and no log messages have been
shown, write out another '.'"""
if self.in_progress_hanging:
print(".")
sys.stdout.flush()
def stdout_level_matches(self, level):
"""Returns true if a message at this level will go to stdout"""
return self.level_matches(level, self._stdout_level())
def _stdout_level(self):
"""Returns the level that stdout runs at"""
for level, consumer in self.consumers:
if consumer is sys.stdout:
return level
return self.FATAL
@staticmethod
def level_matches(level, consumer_level):
"""
>>> l = Logger([])
>>> l.level_matches(3, 4)
False
>>> l.level_matches(3, 2)
True
>>> l.level_matches(slice(None, 3), 3)
False
>>> l.level_matches(slice(None, 3), 2)
True
>>> l.level_matches(slice(1, 3), 1)
True
>>> l.level_matches(slice(2, 3), 1)
False
"""
if isinstance(level, slice):
start, stop = level.start, level.stop
if start is not None and start > consumer_level:
return False
if stop is not None and stop <= consumer_level:
return False
return True
else:
return level >= consumer_level
@classmethod
def level_for_integer(cls, level):
levels = cls.LEVELS
if level < 0:
return levels[0]
if level >= len(levels):
return levels[-1]
return levels[level]
# create a silent logger just to prevent this from being undefined
# will be overridden with requested verbosity main() is called.
logger = Logger([(Logger.LEVELS[-1], sys.stdout)])
def mkdir(at_path):
if not os.path.exists(at_path):
logger.info("Creating %s", at_path)
os.makedirs(at_path)
else:
logger.info("Directory %s already exists", at_path)
def copy_file_or_folder(src, dest, symlink=True):
if os.path.isdir(src):
shutil.copytree(src, dest, symlink)
else:
shutil.copy2(src, dest)
def copyfile(src, dest, symlink=True):
if not os.path.exists(src):
# Some bad symlink in the src
logger.warn("Cannot find file %s (bad symlink)", src)
return
if os.path.exists(dest):
logger.debug("File %s already exists", dest)
return
if not os.path.exists(os.path.dirname(dest)):
logger.info("Creating parent directories for %s", os.path.dirname(dest))
os.makedirs(os.path.dirname(dest))
if symlink and hasattr(os, "symlink") and not IS_WIN:
logger.info("Symlinking %s", dest)
try:
os.symlink(os.path.realpath(src), dest)
except (OSError, NotImplementedError):
logger.info("Symlinking failed, copying to %s", dest)
copy_file_or_folder(src, dest, symlink)
else:
logger.info("Copying to %s", dest)
copy_file_or_folder(src, dest, symlink)
def writefile(dest, content, overwrite=True):
if not os.path.exists(dest):
logger.info("Writing %s", dest)
with open(dest, "wb") as f:
f.write(content.encode("utf-8"))
return
else:
with open(dest, "rb") as f:
c = f.read()
if c != content.encode("utf-8"):
if not overwrite:
logger.notify("File %s exists with different content; not overwriting", dest)
return
logger.notify("Overwriting %s with new content", dest)
with open(dest, "wb") as f:
f.write(content.encode("utf-8"))
else:
logger.info("Content %s already in place", dest)
def rm_tree(folder):
if os.path.exists(folder):
logger.notify("Deleting tree %s", folder)
shutil.rmtree(folder)
else:
logger.info("Do not need to delete %s; already gone", folder)
def make_exe(fn):
if hasattr(os, "chmod"):
old_mode = os.stat(fn).st_mode & 0xFFF # 0o7777
new_mode = (old_mode | 0x16D) & 0xFFF # 0o555, 0o7777
os.chmod(fn, new_mode)
logger.info("Changed mode of %s to %s", fn, oct(new_mode))
def _find_file(filename, folders):
for folder in reversed(folders):
files = glob.glob(os.path.join(folder, filename))
if files and os.path.isfile(files[0]):
return True, files[0]
return False, filename
def file_search_dirs():
here = os.path.dirname(os.path.abspath(__file__))
dirs = [here, join(here, "virtualenv_support")]
if os.path.splitext(os.path.dirname(__file__))[0] != "virtualenv":
# Probably some boot script; just in case virtualenv is installed...
try:
# noinspection PyUnresolvedReferences
import virtualenv
except ImportError:
pass
else:
dirs.append(os.path.join(os.path.dirname(virtualenv.__file__), "virtualenv_support"))
return [d for d in dirs if os.path.isdir(d)]
class UpdatingDefaultsHelpFormatter(optparse.IndentedHelpFormatter):
"""
Custom help formatter for use in ConfigOptionParser that updates
the defaults before expanding them, allowing them to show up correctly
in the help listing
"""
def expand_default(self, option):
if self.parser is not None:
self.parser.update_defaults(self.parser.defaults)
return optparse.IndentedHelpFormatter.expand_default(self, option)
class ConfigOptionParser(optparse.OptionParser):
"""
Custom option parser which updates its defaults by checking the
configuration files and environmental variables
"""
def __init__(self, *args, **kwargs):
self.config = ConfigParser.RawConfigParser()
self.files = self.get_config_files()
self.config.read(self.files)
optparse.OptionParser.__init__(self, *args, **kwargs)
@staticmethod
def get_config_files():
config_file = os.environ.get("VIRTUALENV_CONFIG_FILE", False)
if config_file and os.path.exists(config_file):
return [config_file]
return [DEFAULT_CONFIG_FILE]
def update_defaults(self, defaults):
"""
Updates the given defaults with values from the config files and
the environ. Does a little special handling for certain types of
options (lists).
"""
# Then go and look for the other sources of configuration:
config = {}
# 1. config files
config.update(dict(self.get_config_section("virtualenv")))
# 2. environmental variables
config.update(dict(self.get_environ_vars()))
# Then set the options with those values
for key, val in config.items():
key = key.replace("_", "-")
if not key.startswith("--"):
key = "--{}".format(key) # only prefer long opts
option = self.get_option(key)
if option is not None:
# ignore empty values
if not val:
continue
# handle multiline configs
if option.action == "append":
val = val.split()
else:
option.nargs = 1
if option.action == "store_false":
val = not strtobool(val)
elif option.action in ("store_true", "count"):
val = strtobool(val)
try:
val = option.convert_value(key, val)
except optparse.OptionValueError:
e = sys.exc_info()[1]
print("An error occurred during configuration: {!r}".format(e))
sys.exit(3)
defaults[option.dest] = val
return defaults
def get_config_section(self, name):
"""
Get a section of a configuration
"""
if self.config.has_section(name):
return self.config.items(name)
return []
def get_environ_vars(self, prefix="VIRTUALENV_"):
"""
Returns a generator with all environmental vars with prefix VIRTUALENV
"""
for key, val in os.environ.items():
if key.startswith(prefix):
yield (key.replace(prefix, "").lower(), val)
def get_default_values(self):
"""
Overriding to make updating the defaults after instantiation of
the option parser possible, update_defaults() does the dirty work.
"""
if not self.process_default_values:
# Old, pre-Optik 1.5 behaviour.
return optparse.Values(self.defaults)
defaults = self.update_defaults(self.defaults.copy()) # ours
for option in self._get_all_options():
default = defaults.get(option.dest)
if isinstance(default, basestring):
opt_str = option.get_opt_string()
defaults[option.dest] = option.check_value(opt_str, default)
return optparse.Values(defaults)
def main():
parser = ConfigOptionParser(
version=virtualenv_version, usage="%prog [OPTIONS] DEST_DIR", formatter=UpdatingDefaultsHelpFormatter()
)
parser.add_option(
"-v", "--verbose", action="count", dest="verbose", default=5 if DEBUG else 0, help="Increase verbosity."
)
parser.add_option("-q", "--quiet", action="count", dest="quiet", default=0, help="Decrease verbosity.")
parser.add_option(
"-p",
"--python",
dest="python",
metavar="PYTHON_EXE",
help="The Python interpreter to use, e.g., --python=python3.5 will use the python3.5 "
"interpreter to create the new environment. The default is the interpreter that "
"virtualenv was installed with ({})".format(sys.executable),
)
parser.add_option(
"--clear", dest="clear", action="store_true", help="Clear out the non-root install and start from scratch."
)
parser.set_defaults(system_site_packages=False)
parser.add_option(
"--no-site-packages",
dest="system_site_packages",
action="store_false",
help="DEPRECATED. Retained only for backward compatibility. "
"Not having access to global site-packages is now the default behavior.",
)
parser.add_option(
"--system-site-packages",
dest="system_site_packages",
action="store_true",
help="Give the virtual environment access to the global site-packages.",
)
parser.add_option(
"--always-copy",
dest="symlink",
action="store_false",
default=True,
help="Always copy files rather than symlinking.",
)
parser.add_option(
"--relocatable",
dest="relocatable",
action="store_true",
help="Make an EXISTING virtualenv environment relocatable. "
"This fixes up scripts and makes all .pth files relative.",
)
parser.add_option(
"--no-setuptools",
dest="no_setuptools",
action="store_true",
help="Do not install setuptools in the new virtualenv.",
)
parser.add_option("--no-pip", dest="no_pip", action="store_true", help="Do not install pip in the new virtualenv.")
parser.add_option(
"--no-wheel", dest="no_wheel", action="store_true", help="Do not install wheel in the new virtualenv."
)
default_search_dirs = file_search_dirs()
parser.add_option(
"--extra-search-dir",
dest="search_dirs",
action="append",
metavar="DIR",
default=default_search_dirs,
help="Directory to look for setuptools/pip distributions in. " "This option can be used multiple times.",
)
parser.add_option(
"--download",
dest="download",
default=True,
action="store_true",
help="Download pre-installed packages from PyPI.",
)
parser.add_option(
"--no-download",
"--never-download",
dest="download",
action="store_false",
help="Do not download pre-installed packages from PyPI.",
)
parser.add_option("--prompt", dest="prompt", help="Provides an alternative prompt prefix for this environment.")
parser.add_option(
"--setuptools",
dest="setuptools",
action="store_true",
help="DEPRECATED. Retained only for backward compatibility. This option has no effect.",
)
parser.add_option(
"--distribute",
dest="distribute",
action="store_true",
help="DEPRECATED. Retained only for backward compatibility. This option has no effect.",
)
parser.add_option(
"--unzip-setuptools",
action="store_true",
help="DEPRECATED. Retained only for backward compatibility. This option has no effect.",
)
if "extend_parser" in globals():
# noinspection PyUnresolvedReferences
extend_parser(parser) # noqa: F821
options, args = parser.parse_args()
global logger
if "adjust_options" in globals():
# noinspection PyUnresolvedReferences
adjust_options(options, args) # noqa: F821
verbosity = options.verbose - options.quiet
logger = Logger([(Logger.level_for_integer(2 - verbosity), sys.stdout)])
if options.python and not os.environ.get("VIRTUALENV_INTERPRETER_RUNNING"):
env = os.environ.copy()
interpreter = resolve_interpreter(options.python)
if interpreter == sys.executable:
logger.warn("Already using interpreter {}".format(interpreter))
else:
logger.notify("Running virtualenv with interpreter {}".format(interpreter))
env["VIRTUALENV_INTERPRETER_RUNNING"] = "true"
file = __file__
if file.endswith(".pyc"):
file = file[:-1]
sub_process_call = subprocess.Popen([interpreter, file] + sys.argv[1:], env=env)
raise SystemExit(sub_process_call.wait())
if not args:
print("You must provide a DEST_DIR")
parser.print_help()
sys.exit(2)
if len(args) > 1:
print("There must be only one argument: DEST_DIR (you gave {})".format(" ".join(args)))
parser.print_help()
sys.exit(2)
home_dir = args[0]
if os.path.exists(home_dir) and os.path.isfile(home_dir):
logger.fatal("ERROR: File already exists and is not a directory.")
logger.fatal("Please provide a different path or delete the file.")
sys.exit(3)
if os.environ.get("WORKING_ENV"):
logger.fatal("ERROR: you cannot run virtualenv while in a working env")
logger.fatal("Please deactivate your working env, then re-run this script")
sys.exit(3)
if "PYTHONHOME" in os.environ:
logger.warn("PYTHONHOME is set. You *must* activate the virtualenv before using it")
del os.environ["PYTHONHOME"]
if options.relocatable:
make_environment_relocatable(home_dir)
return
create_environment(
home_dir,
site_packages=options.system_site_packages,
clear=options.clear,
prompt=options.prompt,
search_dirs=options.search_dirs,
download=options.download,
no_setuptools=options.no_setuptools,
no_pip=options.no_pip,
no_wheel=options.no_wheel,
symlink=options.symlink,
)
if "after_install" in globals():
# noinspection PyUnresolvedReferences
after_install(options, home_dir) # noqa: F821
def call_subprocess(
cmd,
show_stdout=True,
filter_stdout=None,
cwd=None,
raise_on_return_code=True,
extra_env=None,
remove_from_env=None,
stdin=None,
):
cmd_parts = []
for part in cmd:
if len(part) > 45:
part = part[:20] + "..." + part[-20:]
if " " in part or "\n" in part or '"' in part or "'" in part:
part = '"{}"'.format(part.replace('"', '\\"'))
if hasattr(part, "decode"):
try:
part = part.decode(sys.getdefaultencoding())
except UnicodeDecodeError:
part = part.decode(sys.getfilesystemencoding())
cmd_parts.append(part)
cmd_desc = " ".join(cmd_parts)
if show_stdout:
stdout = None
else:
stdout = subprocess.PIPE
logger.debug("Running command {}".format(cmd_desc))
if extra_env or remove_from_env:
env = os.environ.copy()
if extra_env:
env.update(extra_env)
if remove_from_env:
for var_name in remove_from_env:
env.pop(var_name, None)
else:
env = None
try:
proc = subprocess.Popen(
cmd,
stderr=subprocess.STDOUT,
stdin=None if stdin is None else subprocess.PIPE,
stdout=stdout,
cwd=cwd,
env=env,
)
except Exception:
e = sys.exc_info()[1]
logger.fatal("Error {} while executing command {}".format(e, cmd_desc))
raise
all_output = []
if stdout is not None:
if stdin is not None:
proc.stdin.write(stdin)
proc.stdin.close()
stdout = proc.stdout
encoding = sys.getdefaultencoding()
fs_encoding = sys.getfilesystemencoding()
while 1:
line = stdout.readline()
try:
line = line.decode(encoding)
except UnicodeDecodeError:
line = line.decode(fs_encoding)
if not line:
break
line = line.rstrip()
all_output.append(line)
if filter_stdout:
level = filter_stdout(line)
if isinstance(level, tuple):
level, line = level
logger.log(level, line)
if not logger.stdout_level_matches(level):
logger.show_progress()
else:
logger.info(line)
else:
proc.communicate(stdin)
proc.wait()
if proc.returncode:
if raise_on_return_code:
if all_output:
logger.notify("Complete output from command {}:".format(cmd_desc))
logger.notify("\n".join(all_output) + "\n----------------------------------------")
raise OSError("Command {} failed with error code {}".format(cmd_desc, proc.returncode))
else:
logger.warn("Command {} had error code {}".format(cmd_desc, proc.returncode))
def filter_install_output(line):
if line.strip().startswith("running"):
return Logger.INFO
return Logger.DEBUG
def find_wheels(projects, search_dirs):
"""Find wheels from which we can import PROJECTS.
Scan through SEARCH_DIRS for a wheel for each PROJECT in turn. Return
a list of the first wheel found for each PROJECT
"""
wheels = []
# Look through SEARCH_DIRS for the first suitable wheel. Don't bother
# about version checking here, as this is simply to get something we can
# then use to install the correct version.
for project in projects:
for dirname in search_dirs:
# This relies on only having "universal" wheels available.
# The pattern could be tightened to require -py2.py3-none-any.whl.
files = glob.glob(os.path.join(dirname, project + "-*.whl"))
if files:
wheels.append(os.path.abspath(files[0]))
break
else:
# We're out of luck, so quit with a suitable error
logger.fatal("Cannot find a wheel for {}".format(project))
return wheels
def install_wheel(project_names, py_executable, search_dirs=None, download=False):
if search_dirs is None:
search_dirs = file_search_dirs()
wheels = find_wheels(["setuptools", "pip"], search_dirs)
python_path = os.pathsep.join(wheels)
# PIP_FIND_LINKS uses space as the path separator and thus cannot have paths
# with spaces in them. Convert any of those to local file:// URL form.
try:
from urlparse import urljoin
from urllib import pathname2url
except ImportError:
from urllib.parse import urljoin
from urllib.request import pathname2url
def space_path2url(p):
if " " not in p:
return p
return urljoin("file:", pathname2url(os.path.abspath(p)))
find_links = " ".join(space_path2url(d) for d in search_dirs)
extra_args = ["--ignore-installed"]
if DEBUG:
extra_args.append("-v")
if IS_JYTHON:
extra_args.append("--no-cache")
script = textwrap.dedent(
"""
import sys
import pkgutil
import tempfile
import os
try:
from pip._internal import main as _main
cert_data = pkgutil.get_data("pip._vendor.certifi", "cacert.pem")
except ImportError:
from pip import main as _main
cert_data = pkgutil.get_data("pip._vendor.requests", "cacert.pem")
except IOError:
cert_data = None
if cert_data is not None:
cert_file = tempfile.NamedTemporaryFile(delete=False)
cert_file.write(cert_data)
cert_file.close()
else:
cert_file = None
try:
args = ["install"] + [{}]
if cert_file is not None:
args += ["--cert", cert_file.name]
args += sys.argv[1:]
sys.exit(_main(args))
finally:
if cert_file is not None:
os.remove(cert_file.name)
""".format(
", ".join(repr(i) for i in extra_args)
)
).encode("utf8")
cmd = [py_executable, "-"] + project_names
logger.start_progress("Installing {}...".format(", ".join(project_names)))
logger.indent += 2
env = {
"PYTHONPATH": python_path,
"JYTHONPATH": python_path, # for Jython < 3.x
"PIP_FIND_LINKS": find_links,
"PIP_USE_WHEEL": "1",
"PIP_ONLY_BINARY": ":all:",
"PIP_USER": "0",
"PIP_NO_INPUT": "1",
}
if not download:
env["PIP_NO_INDEX"] = "1"
try:
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=script)
finally:
logger.indent -= 2
logger.end_progress()
def create_environment(
home_dir,
site_packages=False,
clear=False,
prompt=None,
search_dirs=None,
download=False,
no_setuptools=False,
no_pip=False,
no_wheel=False,
symlink=True,
):
"""
Creates a new environment in ``home_dir``.
If ``site_packages`` is true, then the global ``site-packages/``
directory will be on the path.
If ``clear`` is true (default False) then the environment will
first be cleared.
"""
home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
py_executable = os.path.abspath(
install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
)
install_distutils(home_dir)
to_install = []
if not no_setuptools:
to_install.append("setuptools")
if not no_pip:
to_install.append("pip")
if not no_wheel:
to_install.append("wheel")
if to_install:
install_wheel(to_install, py_executable, search_dirs, download=download)
install_activate(home_dir, bin_dir, prompt)
install_python_config(home_dir, bin_dir, prompt)
def is_executable_file(fpath):
return os.path.isfile(fpath) and is_executable(fpath)
def path_locations(home_dir, dry_run=False):
"""Return the path locations for the environment (where libraries are,
where scripts go, etc)"""
home_dir = os.path.abspath(home_dir)
lib_dir, inc_dir, bin_dir = None, None, None
# XXX: We'd use distutils.sysconfig.get_python_inc/lib but its
# prefix arg is broken: http://bugs.python.org/issue3386
if IS_WIN:
# Windows has lots of problems with executables with spaces in
# the name; this function will remove them (using the ~1
# format):
if not dry_run:
mkdir(home_dir)
if " " in home_dir:
import ctypes
get_short_path_name = ctypes.windll.kernel32.GetShortPathNameW
size = max(len(home_dir) + 1, 256)
buf = ctypes.create_unicode_buffer(size)
try:
# noinspection PyUnresolvedReferences
u = unicode
except NameError:
u = str
ret = get_short_path_name(u(home_dir), buf, size)
if not ret:
print('Error: the path "{}" has a space in it'.format(home_dir))
print("We could not determine the short pathname for it.")
print("Exiting.")
sys.exit(3)
home_dir = str(buf.value)
lib_dir = join(home_dir, "Lib")
inc_dir = join(home_dir, "Include")
bin_dir = join(home_dir, "Scripts")
if IS_JYTHON:
lib_dir = join(home_dir, "Lib")
inc_dir = join(home_dir, "Include")
bin_dir = join(home_dir, "bin")
elif IS_PYPY:
lib_dir = home_dir
inc_dir = join(home_dir, "include")
bin_dir = join(home_dir, "bin")
elif not IS_WIN:
lib_dir = join(home_dir, "lib", PY_VERSION)
inc_dir = join(home_dir, "include", PY_VERSION + ABI_FLAGS)
bin_dir = join(home_dir, "bin")
return home_dir, lib_dir, inc_dir, bin_dir
def change_prefix(filename, dst_prefix):
prefixes = [sys.prefix]
if IS_DARWIN:
prefixes.extend(
(
os.path.join("/Library/Python", sys.version[:3], "site-packages"),
os.path.join(sys.prefix, "Extras", "lib", "python"),
os.path.join("~", "Library", "Python", sys.version[:3], "site-packages"),
# Python 2.6 no-frameworks
os.path.join("~", ".local", "lib", "python", sys.version[:3], "site-packages"),
# System Python 2.7 on OSX Mountain Lion
os.path.join("~", "Library", "Python", sys.version[:3], "lib", "python", "site-packages"),
)
)
if hasattr(sys, "real_prefix"):
prefixes.append(sys.real_prefix)
if hasattr(sys, "base_prefix"):
prefixes.append(sys.base_prefix)
prefixes = list(map(os.path.expanduser, prefixes))
prefixes = list(map(os.path.abspath, prefixes))
# Check longer prefixes first so we don't split in the middle of a filename
prefixes = sorted(prefixes, key=len, reverse=True)
filename = os.path.abspath(filename)
# On Windows, make sure drive letter is uppercase
if IS_WIN and filename[0] in "abcdefghijklmnopqrstuvwxyz":
filename = filename[0].upper() + filename[1:]
for i, prefix in enumerate(prefixes):
if IS_WIN and prefix[0] in "abcdefghijklmnopqrstuvwxyz":
prefixes[i] = prefix[0].upper() + prefix[1:]
for src_prefix in prefixes:
if filename.startswith(src_prefix):
_, relative_path = filename.split(src_prefix, 1)
if src_prefix != os.sep: # sys.prefix == "/"
assert relative_path[0] == os.sep
relative_path = relative_path[1:]
return join(dst_prefix, relative_path)
assert False, "Filename {} does not start with any of these prefixes: {}".format(filename, prefixes)
def copy_required_modules(dst_prefix, symlink):
import warnings
with warnings.catch_warnings():
# Ignore deprecation of the imp module
# TODO: do not use deprecated imp module
warnings.simplefilter("ignore")
import imp
for modname in REQUIRED_MODULES:
if modname in sys.builtin_module_names:
logger.info("Ignoring built-in bootstrap module: %s" % modname)
continue
try:
f, filename, _ = imp.find_module(modname)
except ImportError:
logger.info("Cannot import bootstrap module: %s" % modname)
else:
if f is not None:
f.close()
# special-case custom readline.so on OS X, but not for pypy:
if (
modname == "readline"
and sys.platform == "darwin"
and not (IS_PYPY or filename.endswith(join("lib-dynload", "readline.so")))
):
dst_filename = join(dst_prefix, "lib", "python{}".format(sys.version[:3]), "readline.so")
elif modname == "readline" and sys.platform == "win32":
# special-case for Windows, where readline is not a standard module, though it may have been installed
# in site-packages by a third-party package
dst_filename = None
else:
dst_filename = change_prefix(filename, dst_prefix)
if dst_filename is not None:
copyfile(filename, dst_filename, symlink)
if filename.endswith(".pyc"):
py_file = filename[:-1]
if os.path.exists(py_file):
copyfile(py_file, dst_filename[:-1], symlink)
def copy_tcltk(src, dest, symlink):
""" copy tcl/tk libraries on Windows (issue #93) """
for lib_version in "8.5", "8.6":
for libname in "tcl", "tk":
src_dir = join(src, "tcl", libname + lib_version)
dest_dir = join(dest, "tcl", libname + lib_version)
# Only copy the dirs from the above combinations that exist
if os.path.exists(src_dir) and not os.path.exists(dest_dir):
copy_file_or_folder(src_dir, dest_dir, symlink)
def subst_path(prefix_path, prefix, home_dir):
prefix_path = os.path.normpath(prefix_path)
prefix = os.path.normpath(prefix)
home_dir = os.path.normpath(home_dir)
if not prefix_path.startswith(prefix):
logger.warn("Path not in prefix %r %r", prefix_path, prefix)
return
return prefix_path.replace(prefix, home_dir, 1)
def install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages, clear, symlink=True):
"""Install just the base environment, no distutils patches etc"""
if sys.executable.startswith(bin_dir):
print("Please use the *system* python to run this script")
return
if clear:
rm_tree(lib_dir)
# FIXME: why not delete it?
# Maybe it should delete everything with #!/path/to/venv/python in it
logger.notify("Not deleting %s", bin_dir)
if hasattr(sys, "real_prefix"):
logger.notify("Using real prefix %r", sys.real_prefix)
prefix = sys.real_prefix
elif hasattr(sys, "base_prefix"):
logger.notify("Using base prefix %r", sys.base_prefix)
prefix = sys.base_prefix
else:
prefix = sys.prefix
prefix = os.path.abspath(prefix)
mkdir(lib_dir)
fix_lib64(lib_dir, symlink)
stdlib_dirs = [os.path.dirname(os.__file__)]
if IS_WIN:
stdlib_dirs.append(join(os.path.dirname(stdlib_dirs[0]), "DLLs"))
elif IS_DARWIN:
stdlib_dirs.append(join(stdlib_dirs[0], "site-packages"))
if hasattr(os, "symlink"):
logger.info("Symlinking Python bootstrap modules")
else:
logger.info("Copying Python bootstrap modules")
logger.indent += 2
try:
# copy required files...
for stdlib_dir in stdlib_dirs:
if not os.path.isdir(stdlib_dir):
continue
for fn in os.listdir(stdlib_dir):
bn = os.path.splitext(fn)[0]
if fn != "site-packages" and bn in REQUIRED_FILES:
copyfile(join(stdlib_dir, fn), join(lib_dir, fn), symlink)
# ...and modules
copy_required_modules(home_dir, symlink)
finally:
logger.indent -= 2
# ...copy tcl/tk
if IS_WIN:
copy_tcltk(prefix, home_dir, symlink)
mkdir(join(lib_dir, "site-packages"))
import site
site_filename = site.__file__
if site_filename.endswith(".pyc") or site_filename.endswith(".pyo"):
site_filename = site_filename[:-1]
elif site_filename.endswith("$py.class"):
site_filename = site_filename.replace("$py.class", ".py")
site_filename_dst = change_prefix(site_filename, home_dir)
site_dir = os.path.dirname(site_filename_dst)
writefile(site_filename_dst, SITE_PY)
writefile(join(site_dir, "orig-prefix.txt"), prefix)
site_packages_filename = join(site_dir, "no-global-site-packages.txt")
if not site_packages:
writefile(site_packages_filename, "")
if IS_PYPY or IS_WIN:
standard_lib_include_dir = join(prefix, "include")
else:
standard_lib_include_dir = join(prefix, "include", PY_VERSION + ABI_FLAGS)
if os.path.exists(standard_lib_include_dir):
copyfile(standard_lib_include_dir, inc_dir, symlink)
else:
logger.debug("No include dir %s", standard_lib_include_dir)
platform_include_dir = distutils.sysconfig.get_python_inc(plat_specific=1)
if platform_include_dir != standard_lib_include_dir:
platform_include_dest = distutils.sysconfig.get_python_inc(plat_specific=1, prefix=home_dir)
if platform_include_dir == platform_include_dest:
# Do platinc_dest manually due to a CPython bug;
# not http://bugs.python.org/issue3386 but a close cousin
platform_include_dest = subst_path(platform_include_dir, prefix, home_dir)
if platform_include_dest:
# PyPy's stdinc_dir and prefix are relative to the original binary
# (traversing virtualenvs), whereas the platinc_dir is relative to
# the inner virtualenv and ignores the prefix argument.
# This seems more evolved than designed.
copyfile(platform_include_dir, platform_include_dest, symlink)
# pypy never uses exec_prefix, just ignore it
if sys.exec_prefix != prefix and not IS_PYPY:
if IS_WIN:
exec_dir = join(sys.exec_prefix, "lib")
elif IS_JYTHON:
exec_dir = join(sys.exec_prefix, "Lib")
else:
exec_dir = join(sys.exec_prefix, "lib", PY_VERSION)
if os.path.isdir(exec_dir):
for fn in os.listdir(exec_dir):
copyfile(join(exec_dir, fn), join(lib_dir, fn), symlink)
if IS_JYTHON:
# Jython has either jython-dev.jar and javalib/ dir, or just
# jython.jar
for name in "jython-dev.jar", "javalib", "jython.jar":
src = join(prefix, name)
if os.path.exists(src):
copyfile(src, join(home_dir, name), symlink)
# XXX: registry should always exist after Jython 2.5rc1
src = join(prefix, "registry")
if os.path.exists(src):
copyfile(src, join(home_dir, "registry"), symlink=False)
copyfile(join(prefix, "cachedir"), join(home_dir, "cachedir"), symlink=False)
mkdir(bin_dir)
py_executable = join(bin_dir, os.path.basename(sys.executable))
if "Python.framework" in prefix:
# OS X framework builds cause validation to break
# https://github.com/pypa/virtualenv/issues/322
if os.environ.get("__PYVENV_LAUNCHER__"):
del os.environ["__PYVENV_LAUNCHER__"]
if re.search(r"/Python(?:-32|-64)*$", py_executable):
# The name of the python executable is not quite what
# we want, rename it.
py_executable = os.path.join(os.path.dirname(py_executable), "python")
logger.notify("New %s executable in %s", EXPECTED_EXE, py_executable)
pc_build_dir = os.path.dirname(sys.executable)
pyd_pth = os.path.join(lib_dir, "site-packages", "virtualenv_builddir_pyd.pth")
if IS_WIN and os.path.exists(os.path.join(pc_build_dir, "build.bat")):
logger.notify("Detected python running from build directory %s", pc_build_dir)
logger.notify("Writing .pth file linking to build directory for *.pyd files")
writefile(pyd_pth, pc_build_dir)
else:
if os.path.exists(pyd_pth):
logger.info("Deleting %s (not Windows env or not build directory python)", pyd_pth)
os.unlink(pyd_pth)
if sys.executable != py_executable:
# FIXME: could I just hard link?
executable = sys.executable
shutil.copyfile(executable, py_executable)
make_exe(py_executable)
if IS_WIN or IS_CYGWIN:
python_w = os.path.join(os.path.dirname(sys.executable), "pythonw.exe")
if os.path.exists(python_w):
logger.info("Also created pythonw.exe")
shutil.copyfile(python_w, os.path.join(os.path.dirname(py_executable), "pythonw.exe"))
python_d = os.path.join(os.path.dirname(sys.executable), "python_d.exe")
python_d_dest = os.path.join(os.path.dirname(py_executable), "python_d.exe")
if os.path.exists(python_d):
logger.info("Also created python_d.exe")
shutil.copyfile(python_d, python_d_dest)
elif os.path.exists(python_d_dest):
logger.info("Removed python_d.exe as it is no longer at the source")
os.unlink(python_d_dest)
# we need to copy the DLL to enforce that windows will load the correct one.
# may not exist if we are cygwin.
if IS_PYPY:
py_executable_dll_s = [("libpypy-c.dll", "libpypy_d-c.dll")]
else:
py_executable_dll_s = [
("python{}.dll".format(sys.version_info[0]), "python{}_d.dll".format(sys.version_info[0])),
(
"python{}{}.dll".format(sys.version_info[0], sys.version_info[1]),
"python{}{}_d.dll".format(sys.version_info[0], sys.version_info[1]),
),
]
for py_executable_dll, py_executable_dll_d in py_executable_dll_s:
python_dll = os.path.join(os.path.dirname(sys.executable), py_executable_dll)
python_dll_d = os.path.join(os.path.dirname(sys.executable), py_executable_dll_d)
python_dll_d_dest = os.path.join(os.path.dirname(py_executable), py_executable_dll_d)
if os.path.exists(python_dll):
logger.info("Also created %s", py_executable_dll)
shutil.copyfile(python_dll, os.path.join(os.path.dirname(py_executable), py_executable_dll))
if os.path.exists(python_dll_d):
logger.info("Also created %s", py_executable_dll_d)
shutil.copyfile(python_dll_d, python_dll_d_dest)
elif os.path.exists(python_dll_d_dest):
logger.info("Removed %s as the source does not exist", python_dll_d_dest)
os.unlink(python_dll_d_dest)
if IS_PYPY:
# make a symlink python --> pypy-c
python_executable = os.path.join(os.path.dirname(py_executable), "python")
if sys.platform in ("win32", "cygwin"):
python_executable += ".exe"
logger.info("Also created executable %s", python_executable)
copyfile(py_executable, python_executable, symlink)
if IS_WIN:
for name in ["libexpat.dll", "libeay32.dll", "ssleay32.dll", "sqlite3.dll", "tcl85.dll", "tk85.dll"]:
src = join(prefix, name)
if os.path.exists(src):
copyfile(src, join(bin_dir, name), symlink)
for d in sys.path:
if d.endswith("lib_pypy"):
break
else:
logger.fatal("Could not find lib_pypy in sys.path")
raise SystemExit(3)
logger.info("Copying lib_pypy")
copyfile(d, os.path.join(home_dir, "lib_pypy"), symlink)
if os.path.splitext(os.path.basename(py_executable))[0] != EXPECTED_EXE:
secondary_exe = os.path.join(os.path.dirname(py_executable), EXPECTED_EXE)
py_executable_ext = os.path.splitext(py_executable)[1]
if py_executable_ext.lower() == ".exe":
# python2.4 gives an extension of '.4' :P
secondary_exe += py_executable_ext
if os.path.exists(secondary_exe):
logger.warn(
"Not overwriting existing {} script {} (you must use {})".format(
EXPECTED_EXE, secondary_exe, py_executable
)
)
else:
logger.notify("Also creating executable in %s", secondary_exe)
shutil.copyfile(sys.executable, secondary_exe)
make_exe(secondary_exe)
if ".framework" in prefix:
original_python = None
if "Python.framework" in prefix:
logger.debug("MacOSX Python framework detected")
# Make sure we use the embedded interpreter inside
# the framework, even if sys.executable points to
# the stub executable in ${sys.prefix}/bin
# See http://groups.google.com/group/python-virtualenv/
# browse_thread/thread/17cab2f85da75951
original_python = os.path.join(prefix, "Resources/Python.app/Contents/MacOS/Python")
if "EPD" in prefix:
logger.debug("EPD framework detected")
original_python = os.path.join(prefix, "bin/python")
shutil.copy(original_python, py_executable)
# Copy the framework's dylib into the virtual
# environment
virtual_lib = os.path.join(home_dir, ".Python")
if os.path.exists(virtual_lib):
os.unlink(virtual_lib)
copyfile(os.path.join(prefix, "Python"), virtual_lib, symlink)
# And then change the install_name of the copied python executable
# noinspection PyBroadException
try:
mach_o_change(py_executable, os.path.join(prefix, "Python"), "@executable_path/../.Python")
except Exception:
e = sys.exc_info()[1]
logger.warn("Could not call mach_o_change: %s. " "Trying to call install_name_tool instead.", e)
try:
call_subprocess(
[
"install_name_tool",
"-change",
os.path.join(prefix, "Python"),
"@executable_path/../.Python",
py_executable,
]
)
except Exception:
logger.fatal("Could not call install_name_tool -- you must " "have Apple's development tools installed")
raise
if not IS_WIN:
# Ensure that 'python', 'pythonX' and 'pythonX.Y' all exist
py_exe_version_major = "python{}".format(sys.version_info[0])
py_exe_version_major_minor = "python{}.{}".format(sys.version_info[0], sys.version_info[1])
py_exe_no_version = "python"
required_symlinks = [py_exe_no_version, py_exe_version_major, py_exe_version_major_minor]
py_executable_base = os.path.basename(py_executable)
if py_executable_base in required_symlinks:
# Don't try to symlink to yourself.
required_symlinks.remove(py_executable_base)
for pth in required_symlinks:
full_pth = join(bin_dir, pth)
if os.path.exists(full_pth):
os.unlink(full_pth)
if symlink:
os.symlink(py_executable_base, full_pth)
else:
copyfile(py_executable, full_pth, symlink)
cmd = [
py_executable,
"-c",
"import sys;out=sys.stdout;" 'getattr(out, "buffer", out).write(sys.prefix.encode("utf-8"))',
]
logger.info('Testing executable with %s %s "%s"', *cmd)
try:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
proc_stdout, proc_stderr = proc.communicate()
except OSError:
e = sys.exc_info()[1]
if e.errno == errno.EACCES:
logger.fatal("ERROR: The executable {} could not be run: {}".format(py_executable, e))
sys.exit(100)
else:
raise e
proc_stdout = proc_stdout.strip().decode("utf-8")
# normalize paths using realpath to ensure that a virtualenv correctly identifies itself even
# when addressed over a symlink
proc_stdout = os.path.normcase(os.path.realpath(proc_stdout))
norm_home_dir = os.path.normcase(os.path.realpath(home_dir))
if hasattr(norm_home_dir, "decode"):
norm_home_dir = norm_home_dir.decode(sys.getfilesystemencoding())
if proc_stdout != norm_home_dir:
logger.fatal("ERROR: The executable %s is not functioning", py_executable)
logger.fatal("ERROR: It thinks sys.prefix is {!r} (should be {!r})".format(proc_stdout, norm_home_dir))
logger.fatal("ERROR: virtualenv is not compatible with this system or executable")
if IS_WIN:
logger.fatal(
"Note: some Windows users have reported this error when they "
'installed Python for "Only this user" or have multiple '
"versions of Python installed. Copying the appropriate "
"PythonXX.dll to the virtualenv Scripts/ directory may fix "
"this problem."
)
sys.exit(100)
else:
logger.info("Got sys.prefix result: %r", proc_stdout)
pydistutils = os.path.expanduser("~/.pydistutils.cfg")
if os.path.exists(pydistutils):
logger.notify("Please make sure you remove any previous custom paths from " "your %s file.", pydistutils)
# FIXME: really this should be calculated earlier
fix_local_scheme(home_dir, symlink)
if site_packages:
if os.path.exists(site_packages_filename):
logger.info("Deleting %s", site_packages_filename)
os.unlink(site_packages_filename)
return py_executable
def install_activate(home_dir, bin_dir, prompt=None):
if IS_WIN or IS_JYTHON and getattr(os, "_name", None) == "nt":
files = {"activate.bat": ACTIVATE_BAT, "deactivate.bat": DEACTIVATE_BAT, "activate.ps1": ACTIVATE_PS}
# MSYS needs paths of the form /c/path/to/file
drive, tail = os.path.splitdrive(home_dir.replace(os.sep, "/"))
home_dir_msys = (drive and "/{}{}" or "{}{}").format(drive[:1], tail)
# Run-time conditional enables (basic) Cygwin compatibility
home_dir_sh = """$(if [ "$OSTYPE" "==" "cygwin" ]; then cygpath -u '{}'; else echo '{}'; fi;)""".format(
home_dir, home_dir_msys
)
files["activate"] = ACTIVATE_SH.replace("__VIRTUAL_ENV__", home_dir_sh)
else:
files = {
"activate": ACTIVATE_SH,
"activate.fish": ACTIVATE_FISH,
"activate.csh": ACTIVATE_CSH,
"activate.ps1": ACTIVATE_PS,
}
files["activate_this.py"] = ACTIVATE_THIS
if sys.version_info >= (3, 4):
# Add xonsh support
files["activate.xsh"] = ACTIVATE_XSH
install_files(home_dir, bin_dir, prompt, files)
def install_files(home_dir, bin_dir, prompt, files):
if hasattr(home_dir, "decode"):
home_dir = home_dir.decode(sys.getfilesystemencoding())
virtualenv_name = os.path.basename(home_dir)
for name, content in files.items():
content = content.replace("__VIRTUAL_PROMPT__", prompt or "")
content = content.replace("__VIRTUAL_WINPROMPT__", prompt or "({})".format(virtualenv_name))
content = content.replace("__VIRTUAL_ENV__", home_dir)
content = content.replace("__VIRTUAL_NAME__", virtualenv_name)
content = content.replace("__BIN_NAME__", os.path.basename(bin_dir))
content = content.replace("__PATH_SEP__", os.pathsep)
writefile(os.path.join(bin_dir, name), content)
def install_python_config(home_dir, bin_dir, prompt=None):
if sys.platform == "win32" or IS_JYTHON and getattr(os, "_name", None) == "nt":
files = {}
else:
files = {"python-config": PYTHON_CONFIG}
install_files(home_dir, bin_dir, prompt, files)
for name, _ in files.items():
make_exe(os.path.join(bin_dir, name))
def install_distutils(home_dir):
distutils_path = change_prefix(distutils.__path__[0], home_dir)
mkdir(distutils_path)
# FIXME: maybe this prefix setting should only be put in place if
# there's a local distutils.cfg with a prefix setting?
# FIXME: this is breaking things, removing for now:
# distutils_cfg = DISTUTILS_CFG + "\n[install]\nprefix=%s\n" home_dir
writefile(os.path.join(distutils_path, "__init__.py"), DISTUTILS_INIT)
writefile(os.path.join(distutils_path, "distutils.cfg"), DISTUTILS_CFG, overwrite=False)
def fix_local_scheme(home_dir, symlink=True):
"""
Platforms that use the "posix_local" install scheme (like Ubuntu with
Python 2.7) need to be given an additional "local" location, sigh.
"""
try:
import sysconfig
except ImportError:
pass
else:
# noinspection PyProtectedMember
if sysconfig._get_default_scheme() == "posix_local":
local_path = os.path.join(home_dir, "local")
if not os.path.exists(local_path):
os.mkdir(local_path)
for subdir_name in os.listdir(home_dir):
if subdir_name == "local":
continue
copyfile(
os.path.abspath(os.path.join(home_dir, subdir_name)),
os.path.join(local_path, subdir_name),
symlink,
)
def fix_lib64(lib_dir, symlink=True):
"""
Some platforms (particularly Gentoo on x64) put things in lib64/pythonX.Y
instead of lib/pythonX.Y. If this is such a platform we'll just create a
symlink so lib64 points to lib
"""
# PyPy's library path scheme is not affected by this.
# Return early or we will die on the following assert.
if IS_PYPY:
logger.debug("PyPy detected, skipping lib64 symlinking")
return
# Check we have a lib64 library path
if not [p for p in distutils.sysconfig.get_config_vars().values() if isinstance(p, basestring) and "lib64" in p]:
return
logger.debug("This system uses lib64; symlinking lib64 to lib")
assert os.path.basename(lib_dir) == "python{}".format(sys.version[:3]), "Unexpected python lib dir: {!r}".format(
lib_dir
)
lib_parent = os.path.dirname(lib_dir)
top_level = os.path.dirname(lib_parent)
lib_dir = os.path.join(top_level, "lib")
lib64_link = os.path.join(top_level, "lib64")
assert os.path.basename(lib_parent) == "lib", "Unexpected parent dir: {!r}".format(lib_parent)
if os.path.lexists(lib64_link):
return
if symlink:
os.symlink("lib", lib64_link)
else:
copyfile(lib_dir, lib64_link, symlink=False)
def resolve_interpreter(exe):
"""
If the executable given isn't an absolute path, search $PATH for the interpreter
"""
# If the "executable" is a version number, get the installed executable for
# that version
orig_exe = exe
python_versions = get_installed_pythons()
if exe in python_versions:
exe = python_versions[exe]
if os.path.abspath(exe) != exe:
exe = distutils.spawn.find_executable(exe) or exe
if not os.path.exists(exe):
logger.fatal("The path {} (from --python={}) does not exist".format(exe, orig_exe))
raise SystemExit(3)
if not is_executable(exe):
logger.fatal("The path {} (from --python={}) is not an executable file".format(exe, orig_exe))
raise SystemExit(3)
return exe
def is_executable(exe):
"""Checks a file is executable"""
return os.path.isfile(exe) and os.access(exe, os.X_OK)
# Relocating the environment:
def make_environment_relocatable(home_dir):
"""
Makes the already-existing environment use relative paths, and takes out
the #!-based environment selection in scripts.
"""
home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
activate_this = os.path.join(bin_dir, "activate_this.py")
if not os.path.exists(activate_this):
logger.fatal(
"The environment doesn't have a file %s -- please re-run virtualenv " "on this environment to update it",
activate_this,
)
fixup_scripts(home_dir, bin_dir)
fixup_pth_and_egg_link(home_dir)
# FIXME: need to fix up distutils.cfg
OK_ABS_SCRIPTS = [
"python",
"python{}".format(sys.version[:3]),
"activate",
"activate.bat",
"activate_this.py",
"activate.fish",
"activate.csh",
"activate.xsh",
]
def fixup_scripts(_, bin_dir):
if IS_WIN:
new_shebang_args = ("{} /c".format(os.path.normcase(os.environ.get("COMSPEC", "cmd.exe"))), "", ".exe")
else:
new_shebang_args = ("/usr/bin/env", sys.version[:3], "")
# This is what we expect at the top of scripts:
shebang = "#!{}".format(
os.path.normcase(os.path.join(os.path.abspath(bin_dir), "python{}".format(new_shebang_args[2])))
)
# This is what we'll put:
new_shebang = "#!{} python{}{}".format(*new_shebang_args)
for filename in os.listdir(bin_dir):
filename = os.path.join(bin_dir, filename)
if not os.path.isfile(filename):
# ignore child directories, e.g. .svn ones.
continue
with open(filename, "rb") as f:
try:
lines = f.read().decode("utf-8").splitlines()
except UnicodeDecodeError:
# This is probably a binary program instead
# of a script, so just ignore it.
continue
if not lines:
logger.warn("Script %s is an empty file", filename)
continue
old_shebang = lines[0].strip()
old_shebang = old_shebang[0:2] + os.path.normcase(old_shebang[2:])
if not old_shebang.startswith(shebang):
if os.path.basename(filename) in OK_ABS_SCRIPTS:
logger.debug("Cannot make script %s relative", filename)
elif lines[0].strip() == new_shebang:
logger.info("Script %s has already been made relative", filename)
else:
logger.warn(
"Script %s cannot be made relative (it's not a normal script that starts with %s)",
filename,
shebang,
)
continue
logger.notify("Making script %s relative", filename)
script = relative_script([new_shebang] + lines[1:])
with open(filename, "wb") as f:
f.write("\n".join(script).encode("utf-8"))
def relative_script(lines):
"""Return a script that'll work in a relocatable environment."""
activate = (
"import os; "
"activate_this=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'activate_this.py'); "
"exec(compile(open(activate_this).read(), activate_this, 'exec'), { '__file__': activate_this}); "
"del os, activate_this"
)
# Find the last future statement in the script. If we insert the activation
# line before a future statement, Python will raise a SyntaxError.
activate_at = None
for idx, line in reversed(list(enumerate(lines))):
if line.split()[:3] == ["from", "__future__", "import"]:
activate_at = idx + 1
break
if activate_at is None:
# Activate after the shebang.
activate_at = 1
return lines[:activate_at] + ["", activate, ""] + lines[activate_at:]
def fixup_pth_and_egg_link(home_dir, sys_path=None):
"""Makes .pth and .egg-link files use relative paths"""
home_dir = os.path.normcase(os.path.abspath(home_dir))
if sys_path is None:
sys_path = sys.path
for a_path in sys_path:
if not a_path:
a_path = "."
if not os.path.isdir(a_path):
continue
a_path = os.path.normcase(os.path.abspath(a_path))
if not a_path.startswith(home_dir):
logger.debug("Skipping system (non-environment) directory %s", a_path)
continue
for filename in os.listdir(a_path):
filename = os.path.join(a_path, filename)
if filename.endswith(".pth"):
if not os.access(filename, os.W_OK):
logger.warn("Cannot write .pth file %s, skipping", filename)
else:
fixup_pth_file(filename)
if filename.endswith(".egg-link"):
if not os.access(filename, os.W_OK):
logger.warn("Cannot write .egg-link file %s, skipping", filename)
else:
fixup_egg_link(filename)
def fixup_pth_file(filename):
lines = []
with open(filename) as f:
prev_lines = f.readlines()
for line in prev_lines:
line = line.strip()
if not line or line.startswith("#") or line.startswith("import ") or os.path.abspath(line) != line:
lines.append(line)
else:
new_value = make_relative_path(filename, line)
if line != new_value:
logger.debug("Rewriting path {} as {} (in {})".format(line, new_value, filename))
lines.append(new_value)
if lines == prev_lines:
logger.info("No changes to .pth file %s", filename)
return
logger.notify("Making paths in .pth file %s relative", filename)
with open(filename, "w") as f:
f.write("\n".join(lines) + "\n")
def fixup_egg_link(filename):
with open(filename) as f:
link = f.readline().strip()
if os.path.abspath(link) != link:
logger.debug("Link in %s already relative", filename)
return
new_link = make_relative_path(filename, link)
logger.notify("Rewriting link {} in {} as {}".format(link, filename, new_link))
with open(filename, "w") as f:
f.write(new_link)
def make_relative_path(source, dest, dest_is_directory=True):
"""
Make a filename relative, where the filename is dest, and it is
being referred to from the filename source.
>>> make_relative_path('/usr/share/something/a-file.pth',
... '/usr/share/another-place/src/Directory')
'../another-place/src/Directory'
>>> make_relative_path('/usr/share/something/a-file.pth',
... '/home/user/src/Directory')
'../../../home/user/src/Directory'
>>> make_relative_path('/usr/share/a-file.pth', '/usr/share/')
'./'
"""
source = os.path.dirname(source)
if not dest_is_directory:
dest_filename = os.path.basename(dest)
dest = os.path.dirname(dest)
else:
dest_filename = None
dest = os.path.normpath(os.path.abspath(dest))
source = os.path.normpath(os.path.abspath(source))
dest_parts = dest.strip(os.path.sep).split(os.path.sep)
source_parts = source.strip(os.path.sep).split(os.path.sep)
while dest_parts and source_parts and dest_parts[0] == source_parts[0]:
dest_parts.pop(0)
source_parts.pop(0)
full_parts = [".."] * len(source_parts) + dest_parts
if not dest_is_directory and dest_filename is not None:
full_parts.append(dest_filename)
if not full_parts:
# Special case for the current directory (otherwise it'd be '')
return "./"
return os.path.sep.join(full_parts)
FILE_PATH = __file__ if os.path.isabs(__file__) else os.path.join(os.getcwd(), __file__)
# Bootstrap script creation:
def create_bootstrap_script(extra_text, python_version=""):
"""
Creates a bootstrap script, which is like this script but with
extend_parser, adjust_options, and after_install hooks.
This returns a string that (written to disk of course) can be used
as a bootstrap script with your own customizations. The script
will be the standard virtualenv.py script, with your extra text
added (your extra text should be Python code).
If you include these functions, they will be called:
``extend_parser(optparse_parser)``:
You can add or remove options from the parser here.
``adjust_options(options, args)``:
You can change options here, or change the args (if you accept
different kinds of arguments, be sure you modify ``args`` so it is
only ``[DEST_DIR]``).
``after_install(options, home_dir)``:
After everything is installed, this function is called. This
is probably the function you are most likely to use. An
example would be::
def after_install(options, home_dir):
subprocess.call([join(home_dir, 'bin', 'easy_install'),
'MyPackage'])
subprocess.call([join(home_dir, 'bin', 'my-package-script'),
'setup', home_dir])
This example immediately installs a package, and runs a setup
script from that package.
If you provide something like ``python_version='2.5'`` then the
script will start with ``#!/usr/bin/env python2.5`` instead of
``#!/usr/bin/env python``. You can use this when the script must
be run with a particular Python version.
"""
filename = FILE_PATH
if filename.endswith(".pyc"):
filename = filename[:-1]
with codecs.open(filename, "r", encoding="utf-8") as f:
content = f.read()
py_exe = "python{}".format(python_version)
content = "#!/usr/bin/env {}\n# WARNING: This file is generated\n{}".format(py_exe, content)
# we build the string as two, to avoid replacing here, but yes further done
return content.replace("# EXTEND - " "bootstrap here", extra_text)
# EXTEND - bootstrap here
def convert(s):
b = base64.b64decode(s.encode("ascii"))
return zlib.decompress(b).decode("utf-8")
# file site.py
SITE_PY = convert(
"""
eJy1Pf1z2zaWv+uvQOnJmEplOnHaXtepe+MkTus7N/HG6WxuU5+OEiGJNUWqBGlZ2+n+7fc+ABAg
Kdm+7Wk6qUQCDw8P7xsPcBAEp6uVzBOxLJI6k0LJuJwuxCquFkrMilJUi7RMDlZxWW3g6fQmnksl
qkKojYqwVTQYPP0XP4On4uMiVQYF+BbXVbGMq3QaZ9lGpMtVUVYyEUldpvlcpHlapXGW/gNaFHkk
nv7rGAzOcwEzz1JZiltZKoCrRDETl5tqUeQirFc45+fR1/GL4UioaZmuKmhQapyBIou4GuRSJoAm
tKwVkDKt5IFayWk6S6e24bqos0Sssngqxf/8D0+Nmu7vD1SxlOuFLKXIARmAKQHWCvGAr2kppkUi
IyFeyWmMA/DzhlgDhjbCNVNIxrwQWZHPYU65nEql4nIjwkldESBCWSQF4JQCBlWaZYN1Ud6oISwp
rccaHomY2cOfDLMHzBPH73IO4Pg+H/ycp3cjhg3cg+CqBbNNKWfpnYgRLPyUd3I61s/CdCaSdDYD
GuTVEJsMGAElsnRyuKLl+E6v0PeHhJXlyhjGkIgyN+aX1CManFcizhSwbb1CGinC/I2cpHEO1Mhv
YTiACCQd9I2TpKqy49DsRAEASlzHCqRkqUS4jNMcmPWneEpo/y3Nk2KthkQBWC0lfq1V5c4/7CEA
tHYIMBrgYpnVrPMsvZHZZggIfATsS6nqrEKBSNJSTquiTKUiAIDaRsg7QHok4lJqEjJnGrkdEf2J
JmmOC4sChgKPL5Eks3RelyRhYpYC5wJXvH3/Qbw5e3V++k7zmAHGMjtfAs4AhRbawQkGEIe1Kg+z
AgQ6Glzg/0ScJChkcxwf8GoaHN670oMQ5r6K2n2cBQey68XVw8AcK1AmNN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51DRT3j9QNl99AP53T2Q=
"""
)
# file activate_this.py
ACTIVATE_THIS = convert(
"""
eJylVE1v2zAMvetXENqhNpZ5wHoL0EMOBdqh64Kt3RAEhaE4TMzOlgxJ+ULR/z7Sdpr041BsOUSS
9fj0SD5Jaz0qIq1NRFiTjytToV3DwnkoVt6jjUA2om888v9QqduAgFssEtegTWJJIV9QhWnm0cyT
dAAPJ3n7Jc9PhvC0/5hmSt3wCgpjYYawCjiHTYkWdm4F9SpE+QS8iVsKkewSxrtYOnt8/gCsi0z6
TOuM7OemhWZKa62obpyP4MJ+Fiji03wXlIp+N1TAv71ShdsCmwjXpsZz753vtr0hljQKAX0kZ9ud
RE+O9f5TKVKdKvUBOCcOnEsCEB2MRzcX0NKAwIBHsoHm2CYsoDl5LKLzu1TxMuclnHGeWWNimfHK
svxkvzazIGOyl5Cmire4YOSdnWo5Td8d4gM22b0jm0x76jv4CIeAbIkx6YIGoHWahaaimByCmV7N
DFfktaKesM257xtI4zhBT8sygpm59YsMn2n9dfnj5nZ0lZ9f/2olyzlCZubzYzNAH1Cza0Pb9U+N
Kf6YJUp5BVg6blvT26ozRI1FaSyFWl3+zMeT8YT5SxNMjD5hs3Cyza7Z5Wv0gS2Qk1047h5jv05u
Lr5fM5pRWVOZyHemzkI0PoYNceH1vVkbxtICnuCdr0Ra3ksLRwVr6y/J8alXNJNKH2cRmAyrjk6U
vp/sNUvALpqpfl++zALOzkBvyJ5+0S2oO5JxXcx/piDhBwHvJas6sq55D486E6EmSo+yvjnT4eld
+saBii/aWlLEDi7cqRJUxg6SkW2XPBPB2wzke1zlHLyg7b5C7UIdpkdu/CYmFpcxKb9tTFeHvfEW
bEt+khbtQs4f8N0GrneByuKGWSp+9I7V9bPpUAw/pfZFJgkSODeE2qdQSDg5uatvYvb76i9zKfxE
"""
)
# file python-config
PYTHON_CONFIG = convert(
"""
eJyNVV1P2zAUfc+v8ODBiSABxlulTipbO6p1LWqBgVhlhcZpPYUkctzSivHfd6+dpGloGH2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 0xFEEDFACE
MH_CIGAM = 0xCEFAEDFE
MH_MAGIC_64 = 0xFEEDFACF
MH_CIGAM_64 = 0xCFFAEDFE
FAT_MAGIC = 0xCAFEBABE
BIG_ENDIAN = ">"
LITTLE_ENDIAN = "<"
LC_LOAD_DYLIB = 0xC
maxint = MAJOR == 3 and getattr(sys, "maxsize") or getattr(sys, "maxint")
class FileView(object):
"""
A proxy for file-like objects that exposes a given view of a file.
Modified from macholib.
"""
def __init__(self, file_obj, start=0, size=maxint):
if isinstance(file_obj, FileView):
self._file_obj = file_obj._file_obj
else:
self._file_obj = file_obj
self._start = start
self._end = start + size
self._pos = 0
def __repr__(self):
return "<fileview [{:d}, {:d}] {!r}>".format(self._start, self._end, self._file_obj)
def tell(self):
return self._pos
def _checkwindow(self, seek_to, op):
if not (self._start <= seek_to <= self._end):
raise IOError(
"{} to offset {:d} is outside window [{:d}, {:d}]".format(op, seek_to, self._start, self._end)
)
def seek(self, offset, whence=0):
seek_to = offset
if whence == os.SEEK_SET:
seek_to += self._start
elif whence == os.SEEK_CUR:
seek_to += self._start + self._pos
elif whence == os.SEEK_END:
seek_to += self._end
else:
raise IOError("Invalid whence argument to seek: {!r}".format(whence))
self._checkwindow(seek_to, "seek")
self._file_obj.seek(seek_to)
self._pos = seek_to - self._start
def write(self, content):
here = self._start + self._pos
self._checkwindow(here, "write")
self._checkwindow(here + len(content), "write")
self._file_obj.seek(here, os.SEEK_SET)
self._file_obj.write(content)
self._pos += len(content)
def read(self, size=maxint):
assert size >= 0
here = self._start + self._pos
self._checkwindow(here, "read")
size = min(size, self._end - here)
self._file_obj.seek(here, os.SEEK_SET)
read_bytes = self._file_obj.read(size)
self._pos += len(read_bytes)
return read_bytes
def read_data(file, endian, num=1):
"""
Read a given number of 32-bits unsigned integers from the given file
with the given endianness.
"""
res = struct.unpack(endian + "L" * num, file.read(num * 4))
if len(res) == 1:
return res[0]
return res
def mach_o_change(at_path, what, value):
"""
Replace a given name (what) in any LC_LOAD_DYLIB command found in
the given binary with a new name (value), provided it's shorter.
"""
def do_macho(file, bits, endian):
# Read Mach-O header (the magic number is assumed read by the caller)
cpu_type, cpu_sub_type, file_type, n_commands, size_of_commands, flags = read_data(file, endian, 6)
# 64-bits header has one more field.
if bits == 64:
read_data(file, endian)
# The header is followed by n commands
for _ in range(n_commands):
where = file.tell()
# Read command header
cmd, cmd_size = read_data(file, endian, 2)
if cmd == LC_LOAD_DYLIB:
# The first data field in LC_LOAD_DYLIB commands is the
# offset of the name, starting from the beginning of the
# command.
name_offset = read_data(file, endian)
file.seek(where + name_offset, os.SEEK_SET)
# Read the NUL terminated string
load = file.read(cmd_size - name_offset).decode()
load = load[: load.index("\0")]
# If the string is what is being replaced, overwrite it.
if load == what:
file.seek(where + name_offset, os.SEEK_SET)
file.write(value.encode() + "\0".encode())
# Seek to the next command
file.seek(where + cmd_size, os.SEEK_SET)
def do_file(file, offset=0, size=maxint):
file = FileView(file, offset, size)
# Read magic number
magic = read_data(file, BIG_ENDIAN)
if magic == FAT_MAGIC:
# Fat binaries contain nfat_arch Mach-O binaries
n_fat_arch = read_data(file, BIG_ENDIAN)
for _ in range(n_fat_arch):
# Read arch header
cpu_type, cpu_sub_type, offset, size, align = read_data(file, BIG_ENDIAN, 5)
do_file(file, offset, size)
elif magic == MH_MAGIC:
do_macho(file, 32, BIG_ENDIAN)
elif magic == MH_CIGAM:
do_macho(file, 32, LITTLE_ENDIAN)
elif magic == MH_MAGIC_64:
do_macho(file, 64, BIG_ENDIAN)
elif magic == MH_CIGAM_64:
do_macho(file, 64, LITTLE_ENDIAN)
assert len(what) >= len(value)
with open(at_path, "r+b") as f:
do_file(f)
if __name__ == "__main__":
main()
|