-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_vendor.sh
More file actions
executable file
·213 lines (181 loc) · 5.72 KB
/
03_vendor.sh
File metadata and controls
executable file
·213 lines (181 loc) · 5.72 KB
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
#!/usr/bin/env bash
set -euo pipefail
PACKAGES_ROOT="packages"
URLLIB3_VERSION_SPEC="urllib3>=2.1.0,<3.0.0"
CLIENT_DIRS=(
"${PACKAGES_ROOT}/openziti_edge_client"
"${PACKAGES_ROOT}/openziti_edge_management"
)
echo "[1/5] Creating temp venv and installing ${URLLIB3_VERSION_SPEC}..."
TMP="$(mktemp -d)"
python3 -m venv "${TMP}/venv"
"${TMP}/venv/bin/pip" -q install --upgrade pip
"${TMP}/venv/bin/pip" -q install "${URLLIB3_VERSION_SPEC}"
SITEPKG="$("${TMP}/venv/bin/python" -c 'import site; print(site.getsitepackages()[0])')"
echo "[2/5] Copying urllib3 into packages..."
for CLIENT_DIR in "${CLIENT_DIRS[@]}"; do
NAME="$(basename "${CLIENT_DIR}")"
VENDOR_DIR="${CLIENT_DIR}/${NAME}/_vendor"
echo "Copying urllib3 into ${VENDOR_DIR}/urllib3"
mkdir -p "${VENDOR_DIR}"
rm -rf "${VENDOR_DIR}/urllib3"
cp -R "${SITEPKG}/urllib3" "${VENDOR_DIR}/urllib3"
# Ensure vendor package is importable
touch "${VENDOR_DIR}/__init__.py"
done
# This python script generated by ChatGPT but it seems to work.
echo "[3/5] Rewriting imports in ${PACKAGES_ROOT} to use vendored urllib3..."
for CLIENT_DIR in "${CLIENT_DIRS[@]}"; do
NAME="$(basename "${CLIENT_DIR}")"
CLIENT_DIR="${CLIENT_DIR}" PKG_NAME="${NAME}" python3 - <<'PY'
import os
import pathlib
import re
import sys
# Inputs are passed from the shell per-client so this script can run in a loop.
client_dir = pathlib.Path(os.environ["CLIENT_DIR"])
pkg_name = os.environ["PKG_NAME"]
pkg_dir = client_dir / pkg_name
if not pkg_dir.exists():
print(f"ERROR: {pkg_dir} not found", file=sys.stderr)
sys.exit(1)
# Rewrite all Python files in the client package, including vendored urllib3.
py_files = [p for p in pkg_dir.rglob("*.py") if p.is_file()]
# Matches:
# import urllib3
# import urllib3 as u
# import urllib3.connection
# import urllib3.contrib.pyopenssl as pyopenssl
import_pat = re.compile(
r"""
^(?P<indent>\s*)
import\s+urllib3
(?P<rest>(?:\.[A-Za-z_]\w*)*)
(?:\s+as\s+(?P<alias>[A-Za-z_]\w*))?
\s*$
""",
re.M | re.X,
)
# Matches:
# from urllib3 import X
# from urllib3.contrib.socks import Y
from_pat = re.compile(
r"""
^(?P<indent>\s*)
from\s+urllib3
(?P<rest>(?:\.[A-Za-z_]\w*)*)
\s+import\s+
(?P<what>.+?)
\s*$
""",
re.M | re.X,
)
changed = 0
for p in py_files:
txt = p.read_text(encoding="utf-8")
orig = txt
# Rewrites import urllib3* to vendored urllib3 module path.
def repl_import(m: re.Match) -> str:
indent = m.group("indent") or ""
rest = m.group("rest") or ""
alias = m.group("alias")
if rest:
module = f"{pkg_name}._vendor.urllib3{rest}"
if alias:
return f"{indent}import {module} as {alias}"
return f"{indent}import {module}"
if alias:
return f"{indent}from {pkg_name}._vendor import urllib3 as {alias}"
return f"{indent}from {pkg_name}._vendor import urllib3"
# Rewrites from urllib3* imports to vendored urllib3 module path.
def repl_from(m: re.Match) -> str:
indent = m.group("indent") or ""
rest = m.group("rest") or ""
what = m.group("what")
return f"{indent}from {pkg_name}._vendor.urllib3{rest} import {what}"
txt = import_pat.sub(repl_import, txt)
txt = from_pat.sub(repl_from, txt)
if txt != orig:
p.write_text(txt, encoding="utf-8")
changed += 1
print(f"Patched {changed} file(s) in {pkg_dir}.")
PY
done
# This python script generated by ChatGPT but it seems to work.
echo "[4/5] Removing urllib3 dependency from pyproject.toml ..."
for CLIENT_DIR in "${CLIENT_DIRS[@]}"; do
echo "Processing ${CLIENT_DIR}'s pyproject.toml"
PYPROJECT="${CLIENT_DIR}/pyproject.toml"
PYPROJECT_PATH="${PYPROJECT}" python3 - <<'PY'
import os
import pathlib
import re
p = pathlib.Path(os.environ["PYPROJECT_PATH"])
txt = p.read_text(encoding="utf-8").splitlines(True)
out = []
removed = 0
dep_block = False
for line in txt:
if re.match(r'^\s*dependencies\s*=\s*\[\s*$', line):
dep_block = True
out.append(line)
continue
if dep_block:
if re.match(r'^\s*\]\s*$', line):
dep_block = False
out.append(line)
continue
if re.search(r'(?i)\burllib3\b', line):
removed += 1
continue
out.append(line)
continue
out.append(line)
p.write_text("".join(out), encoding="utf-8")
print(f"Removed {removed} dependency line(s) mentioning urllib3 from {p}.")
PY
done
echo "[5/6] Removing urllib3 dependency from setup.py and requirements.txt ..."
for CLIENT_DIR in "${CLIENT_DIRS[@]}"; do
SETUP_PY="${CLIENT_DIR}/setup.py"
REQUIREMENTS_TXT="${CLIENT_DIR}/requirements.txt"
if [[ -f "${SETUP_PY}" ]]; then
FILE_PATH="${SETUP_PY}" python3 - <<'PY'
import os
import pathlib
import re
p = pathlib.Path(os.environ["FILE_PATH"])
lines = p.read_text(encoding="utf-8").splitlines(True)
out = []
removed = 0
for line in lines:
if re.search(r'(?i)\burllib3\b', line):
removed += 1
continue
out.append(line)
p.write_text("".join(out), encoding="utf-8")
print(f"Removed {removed} line(s) mentioning urllib3 from {p}.")
PY
fi
if [[ -f "${REQUIREMENTS_TXT}" ]]; then
FILE_PATH="${REQUIREMENTS_TXT}" python3 - <<'PY'
import os
import pathlib
import re
p = pathlib.Path(os.environ["FILE_PATH"])
lines = p.read_text(encoding="utf-8").splitlines(True)
out = []
removed = 0
for line in lines:
if re.search(r'(?i)\burllib3\b', line):
removed += 1
continue
out.append(line)
p.write_text("".join(out), encoding="utf-8")
print(f"Removed {removed} line(s) mentioning urllib3 from {p}.")
PY
fi
done
echo "[6/6] Cleanup temp venv ..."
rm -rf "${TMP}"
echo "Done."