forked from pypa/setuptools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pavement.py
143 lines (118 loc) · 3.95 KB
/
pavement.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
import os
import re
import sys
import shutil
import string
import subprocess
import venv
from fnmatch import fnmatch
from tempfile import TemporaryDirectory
from paver.easy import info, task, path as Path
def remove_all(paths):
for path in paths:
path.rmtree() if path.isdir() else path.remove()
@task
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 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)
move_licenses(vendor)
remove_all(vendor.glob('*.dist-info'))
remove_all(vendor.glob('*.egg-info'))
remove_all(vendor.glob('six.py'))
(vendor / '__init__.py').write_text('')
def move_licenses(vendor):
license_patterns = ("*LICEN[CS]E*", "COPYING*", "NOTICE*", "AUTHORS*")
licenses = (
entry
for path in vendor.glob("*.dist-info")
for entry in path.glob("*")
if any(fnmatch(str(entry), p) for p in license_patterns)
)
for file in licenses:
file.move(_find_license_dest(file, vendor))
def _find_license_dest(license_file, vendor):
basename = license_file.basename()
pkg = license_file.dirname().basename().replace(".dist-info", "")
parts = pkg.split("-")
acc = []
for part in parts:
# Find actual name from normalized name + version
acc.append(part)
for option in ("_".join(acc), "-".join(acc), ".".join(acc)):
candidate = vendor / option
if candidate.isdir():
return candidate / basename
if Path(f"{candidate}.py").isfile():
return Path(f"{candidate}.{basename}")
raise FileNotFoundError(f"No destination found for {license_file}")
def update_pkg_resources():
vendor = Path('pkg_resources/_vendor')
install(vendor)
rewrite_packaging(vendor / 'packaging', 'pkg_resources.extern')
def update_setuptools():
vendor = Path('setuptools/_vendor')
install(vendor)
install_validate_pyproject(vendor)
rewrite_packaging(vendor / 'packaging', '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)
info(f"Temporarily installing {pkg!r}...")
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_output(cmd)
info(f"{pkg!r} vendorized")