forked from pypa/setuptools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendored.py
149 lines (121 loc) · 4.32 KB
/
vendored.py
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
import os
import re
import sys
import shutil
import string
import subprocess
import venv
from tempfile import TemporaryDirectory
from path import Path
def remove_all(paths):
for path in paths:
path.rmtree() if path.isdir() else path.remove()
def update_vendored():
update_pkg_resources()
update_setuptools()
def rewrite_packaging(pkg_files, new_root):
"""
Rewrite imports in packaging to redirect to vendored copies.
"""
for file in pkg_files.glob('*.py'):
text = file.text()
text = re.sub(r' (pyparsing)', rf' {new_root}.\1', text)
text = text.replace(
'from six.moves.urllib import parse',
'from urllib import parse',
)
file.write_text(text)
def rewrite_jaraco_text(pkg_files, new_root):
"""
Rewrite imports in jaraco.text to redirect to vendored copies.
"""
for file in pkg_files.glob('*.py'):
text = file.read_text()
text = re.sub(r' (jaraco\.)', rf' {new_root}.\1', text)
text = re.sub(r' (importlib_resources)', rf' {new_root}.\1', text)
# suppress loading of lorem_ipsum; ref #3072
text = re.sub(r'^lorem_ipsum.*\n$', '', text, flags=re.M)
file.write_text(text)
def rewrite_jaraco(pkg_files, new_root):
"""
Rewrite imports in jaraco.functools to redirect to vendored copies.
"""
for file in pkg_files.glob('*.py'):
text = file.read_text()
text = re.sub(r' (more_itertools)', rf' {new_root}.\1', text)
file.write_text(text)
# required for zip-packaged setuptools #3084
pkg_files.joinpath('__init__.py').write_text('')
def rewrite_importlib_resources(pkg_files, new_root):
"""
Rewrite imports in importlib_resources to redirect to vendored copies.
"""
for file in pkg_files.glob('*.py'):
text = file.read_text().replace('importlib_resources.abc', '.abc')
text = text.replace('zipp', '..zipp')
file.write_text(text)
def clean(vendor):
"""
Remove all files out of the vendor directory except the meta
data (as pip uninstall doesn't support -t).
"""
remove_all(
path
for path in vendor.glob('*')
if path.basename() != 'vendored.txt'
)
def install(vendor):
clean(vendor)
install_args = [
sys.executable,
'-m', 'pip',
'install',
'-r', str(vendor / 'vendored.txt'),
'-t', str(vendor),
]
subprocess.check_call(install_args)
(vendor / '__init__.py').write_text('')
def update_pkg_resources():
vendor = Path('pkg_resources/_vendor')
install(vendor)
rewrite_packaging(vendor / 'packaging', 'pkg_resources.extern')
rewrite_jaraco_text(vendor / 'jaraco/text', 'pkg_resources.extern')
rewrite_jaraco(vendor / 'jaraco', 'pkg_resources.extern')
rewrite_importlib_resources(vendor / 'importlib_resources', 'pkg_resources.extern')
def update_setuptools():
vendor = Path('setuptools/_vendor')
install(vendor)
install_validate_pyproject(vendor)
rewrite_packaging(vendor / 'packaging', 'setuptools.extern')
rewrite_jaraco_text(vendor / 'jaraco/text', 'setuptools.extern')
rewrite_jaraco(vendor / 'jaraco', 'setuptools.extern')
rewrite_importlib_resources(vendor / 'importlib_resources', 'setuptools.extern')
def install_validate_pyproject(vendor):
"""``validate-pyproject`` can be vendorized to remove all dependencies"""
req = next(
(x for x in (vendor / "vendored.txt").lines() if 'validate-pyproject' in x),
"validate-pyproject[all]"
)
pkg, _, _ = req.strip(string.whitespace + "#").partition("#")
pkg = pkg.strip()
opts = {}
if sys.version_info[:2] >= (3, 10):
opts["ignore_cleanup_errors"] = True
with TemporaryDirectory(**opts) as tmp:
venv.create(tmp, with_pip=True)
path = os.pathsep.join(Path(tmp).glob("*"))
venv_python = shutil.which("python", path=path)
subprocess.check_call([venv_python, "-m", "pip", "install", pkg])
cmd = [
venv_python,
"-m",
"validate_pyproject.vendoring",
"--output-dir",
str(vendor / "_validate_pyproject"),
"--enable-plugins",
"setuptools",
"distutils",
"--very-verbose"
]
subprocess.check_call(cmd)
__name__ == '__main__' and update_vendored()