forked from zenml-io/zenml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvenv_clone_utils.py
482 lines (413 loc) · 16.4 KB
/
venv_clone_utils.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
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
# Copyright (c) 2011, Edward George, based on code contained within the
# virtualenv project.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Copyright (c) ZenML GmbH 2022. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
"""The below code is copied from the virtualenv-clone source repo with minor
changes to make it work for the specific usecase of zenml. All credits go to
https://github.com/edwardgeorge for the core implementation"""
import itertools
import os.path
import re
import shutil
import subprocess
import sys
from typing import List, Optional, Tuple
from zenml.logger import get_logger
logger = get_logger(__name__)
env_bin_dir = "bin"
if sys.platform == "win32":
env_bin_dir = "Scripts"
python_exe = "python"
if sys.platform == "win32":
python_exe = "python.exe"
class UserError(Exception):
pass
def _dirmatch(path: str, matchwith: str) -> bool:
"""Check if path is within matchwith's tree.
Args:
path: Path to be checked
matchwith: Path to be matched with
Returns:
Boolean, true if matched, false else
>>> _dirmatch('/home/foo/bar', '/home/foo/bar')
True
>>> _dirmatch('/home/foo/bar/', '/home/foo/bar')
True
>>> _dirmatch('/home/foo/bar/etc', '/home/foo/bar')
True
>>> _dirmatch('/home/foo/bar2', '/home/foo/bar')
False
>>> _dirmatch('/home/foo/bar2/etc', '/home/foo/bar')
False
"""
matchlen = len(matchwith)
if path.startswith(matchwith) and path[matchlen : matchlen + 1] in [
os.sep,
"",
]:
return True
return False
def _virtualenv_sys(venv_path: str) -> Tuple[str, list]:
"""Obtain python version and path info from a virtualenv.
Args:
venv_path: Location of the virtual environment
Returns:
tuple with two entries, python version and a list of paths in the
sys path of the virtual environment
"""
executable = os.path.join(venv_path, env_bin_dir, "python")
# Must use "executable" as the first argument rather than as the
# keyword argument "executable" to get correct value from sys.path
p = subprocess.Popen(
[
executable,
"-c",
"import sys;"
'print ("%d.%d" % (sys.version_info.major,'
" sys.version_info.minor));"
'print ("\\n".join(sys.path));',
],
env=os.environ.copy(),
stdout=subprocess.PIPE,
)
stdout, err = p.communicate()
assert not p.returncode and stdout
lines = stdout.decode("utf-8").splitlines()
return lines[0], list(filter(bool, lines[1:]))
def clone_virtualenv(src_dir: str, dst_dir: str) -> None:
"""Clone virtual environment from src_dir into new dst_dir.
Args:
src_dir: Source directory of the original virtual environment
dst_dir: Destination directory where it should be cloned to
"""
src_dir = os.path.realpath(src_dir)
dst_dir = os.path.realpath(dst_dir)
if not os.path.exists(src_dir):
raise UserError("src dir %r does not exist" % src_dir)
if os.path.exists(dst_dir):
raise UserError("dest dir %r exists" % dst_dir)
logger.info("Cloning virtualenv '%s' => '%s'..." % (src_dir, dst_dir))
shutil.copytree(
src_dir, dst_dir, symlinks=True, ignore=shutil.ignore_patterns("*.pyc")
)
version, sys_path = _virtualenv_sys(dst_dir)
logger.info("Fixing scripts in bin...")
fixup_scripts(src_dir, dst_dir, version)
if old_path_in_sys_path(sys_path, src_dir):
# only need to fix stuff in sys.path if we have old
# paths in the sys.path of new python env. right?
logger.info("Fixing paths in sys.path...")
fixup_syspath_items(sys_path, src_dir, dst_dir)
v_sys = _virtualenv_sys(dst_dir)
remaining = old_path_in_sys_path(v_sys[1], src_dir)
assert not remaining, v_sys
fix_symlink_if_necessary(src_dir, dst_dir)
def old_path_in_sys_path(sys_path: List[str], src_dir: str) -> bool:
"""Checks if sys path contains reference to the old venv
Args:
sys_path: Contents of the sys path as list
src_dir: Path to the old virtualenv
"""
return any(i for i in sys_path if _dirmatch(i, src_dir))
def fix_symlink_if_necessary(src_dir: str, dst_dir: str) -> None:
"""Sometimes the source virtual environment has symlinks that point to
itself. One example is $OLD_VIRTUAL_ENV/local/lib points to
$OLD_VIRTUAL_ENV/lib. This function makes sure $NEW_VIRTUAL_ENV/local/lib
will point to $NEW_VIRTUAL_ENV/lib. Usually this goes unnoticed unless one
tries to upgrade a package though pip, so this bug would be hard to find.
Args:
src_dir: Source directory of the original virtual environment
dst_dir: Destination directory where it should be cloned to
"""
logger.info(
"Scanning for internal symlinks that point to the original virtual env."
)
for dirpath, dirnames, filenames in os.walk(dst_dir):
for a_file in itertools.chain(filenames, dirnames):
full_file_path = os.path.join(dirpath, a_file)
if os.path.islink(full_file_path):
target = os.path.realpath(full_file_path)
if target.startswith(src_dir):
new_target = target.replace(src_dir, dst_dir)
logger.debug("Fixing symlink in %s" % (full_file_path,))
os.remove(full_file_path)
os.symlink(new_target, full_file_path)
def fixup_scripts(
old_dir: str, new_dir: str, version: str, rewrite_env_python: bool = False
) -> None:
"""Fix paths and links within files of the environments so that
they point at the right locations.
Args:
old_dir: Source directory where original virtual environment resides
new_dir: New location of the virtual environment
version: Python version associated with the venv
rewrite_env_python: Boolean to declare if standard python env
shebang like `#!/usr/bin/env python` should be rewritten to point at
the python executable of the new virtual environment
"""
bin_dir = os.path.join(new_dir, env_bin_dir)
root, dirs, files = next(os.walk(bin_dir))
pybinre = re.compile(r"pythonw?([0-9]+(\.[0-9]+(\.[0-9]+)?)?)?$")
for file_ in files:
filename = os.path.join(root, file_)
if file_ in ["python", "python%s" % version, "activate_this.py"]:
continue
elif file_.startswith("python") and pybinre.match(file_):
# ignore other possible python binaries
continue
elif file_.endswith(".pyc"):
# ignore compiled files
continue
elif file_ == "activate" or file_.startswith("activate."):
fixup_activate(os.path.join(root, file_), old_dir, new_dir)
elif os.path.islink(filename):
fixup_link(filename, old_dir, new_dir)
elif os.path.isfile(filename):
fixup_script_(
root,
file_,
old_dir,
new_dir,
version,
rewrite_env_python=rewrite_env_python,
)
def fixup_script_(
root: str,
file_: str,
old_dir: str,
new_dir: str,
py_version: str,
rewrite_env_python: bool = False,
) -> None:
"""This is meant to rewrite the shebang of files in the destination
venv that still point at the python executable of the original venv
or even the system python executable.
Args:
root: Directory root of the file
file_: Filename
old_dir: Directory location of the old virtual environment
new_dir: Directory location of the new virtual environment
py_version: Python version
rewrite_env_python: Boolean to declare if standard python env
shebang like `#!/usr/bin/env python` should be rewritten to point at
the python executable of the new virtual environment
"""
old_shebang = (
f"#!{os.path.abspath(old_dir)}{os.sep}{env_bin_dir}"
f"{os.sep}{python_exe}"
)
new_shebang = (
f"#!{os.path.abspath(new_dir)}{os.sep}{env_bin_dir}"
f"{os.sep}{python_exe}"
)
env_shebang = "#!/usr/bin/env python"
filename = os.path.join(root, file_)
with open(filename, "rb") as f:
if f.read(2) != b"#!":
# no shebang
return
f.seek(0)
lines = f.readlines()
if not lines:
# warn: empty script
return
def rewrite_shebang(python_version: Optional[str] = None):
"""Overwrite shebang of a given file with new_shebang
Args:
python_version: Python version
"""
logger.debug("Fixing shebang within %s" % filename)
shebang = new_shebang
if python_version:
shebang = shebang + python_version
shebang = (shebang + "\n").encode("utf-8")
with open(filename, "wb") as f:
f.write(shebang)
f.writelines(lines[1:])
try:
bang = lines[0].decode("utf-8").strip()
except UnicodeDecodeError:
# binary file
return
# This takes care of the scheme in which shebang is of type
# '#!/venv/bin/python3' while the version of system python
# is of type 3.x e.g. 3.5.
short_version = bang[len(old_shebang) :]
if not bang.startswith("#!"):
return
elif bang == old_shebang:
rewrite_shebang()
elif (
bang.startswith(old_shebang) and bang[len(old_shebang) :] == py_version
):
rewrite_shebang(py_version)
elif (
bang.startswith(old_shebang)
and short_version
and bang[len(old_shebang) :] == short_version
):
rewrite_shebang(short_version)
elif rewrite_env_python and bang.startswith(env_shebang):
if bang == env_shebang:
rewrite_shebang()
elif bang[len(env_shebang) :] == py_version:
rewrite_shebang(py_version)
else:
# Nothing to do here
return
def fixup_activate(filename: str, old_dir: str, new_dir: str) -> None:
"""Within activate file, replace all mentions of the old venv with the path
of the new venv.
Args:
filename: Name of the file
old_dir: Directory location of the old virtual environment
new_dir: Directory location of the new virtual environment
"""
logger.debug("Replacing links in %s" % filename)
with open(filename, "rb") as f:
data = f.read().decode("utf-8")
data = data.replace(old_dir, new_dir)
with open(filename, "wb") as f:
f.write(data.encode("utf-8"))
def fixup_link(
filename: str, old_dir: str, new_dir: str, target: Optional[str] = None
) -> None:
"""Within a given file, replace all mentions of the old venv with the path
of the new venv.
Args:
filename: Name of the file
old_dir: Directory location of the old virtual environment
new_dir: Directory location of the new virtual environment
target: ???
"""
logger.debug("Replacing links in %s" % filename)
if target is None:
target = os.readlink(filename)
origdir = os.path.dirname(os.path.abspath(filename)).replace(
new_dir, old_dir
)
if not os.path.isabs(target):
target = os.path.abspath(os.path.join(origdir, target))
rellink = True
else:
rellink = False
if _dirmatch(target, old_dir):
if rellink:
# keep relative links, but don't keep original in case it
# traversed up out of, then back into the venv.
# so, recreate a relative link from absolute.
target = target[len(origdir) :].lstrip(os.sep)
else:
target = target.replace(old_dir, new_dir, 1)
# else: links outside the venv, replaced with absolute path to target.
_replace_symlink(filename, target)
def _replace_symlink(filename: str, newtarget: str) -> None:
"""Replace any symlinks with absolute path target
Args:
filename: Name of the file
newtarget: Symlink target
"""
logger.debug("Replacing links outside of path for %s" % filename)
tmpfn = "%s.new" % filename
os.symlink(newtarget, tmpfn)
try:
shutil.move(tmpfn, filename)
except OSError:
os.remove(filename)
shutil.move(tmpfn, filename)
def fixup_syspath_items(syspath: List[str], old_dir: str, new_dir: str) -> None:
"""Replace mentions of the old venv in sys path with the new venv.
Args:
syspath: List of paths in sys path
old_dir: Directory location of the old virtual environment
new_dir: Directory location of the new virtual environment
"""
for path in syspath:
if not os.path.isdir(path):
continue
path = os.path.normcase(os.path.abspath(path))
if _dirmatch(path, old_dir):
path = path.replace(old_dir, new_dir, 1)
if not os.path.exists(path):
continue
elif not _dirmatch(path, new_dir):
continue
root, dirs, files = next(os.walk(path))
for file_ in files:
filename = os.path.join(root, file_)
if filename.endswith(".pth"):
fixup_pth_file(filename, old_dir, new_dir)
elif filename.endswith(".egg-link"):
fixup_egglink_file(filename, old_dir, new_dir)
def fixup_pth_file(filename: str, old_dir: str, new_dir: str) -> None:
"""Within a given file, replace all mentions of the old venv with the path
of the new venv.
Args:
filename: Name of the file
old_dir: Directory location of the old virtual environment
new_dir: Directory location of the new virtual environment
"""
logger.debug("Replacing paths within .pth file: %s" % filename)
with open(filename, "r") as f:
lines = f.readlines()
has_change = False
for num, line in enumerate(lines):
line = (
line.decode("utf-8") if hasattr(line, "decode") else line
).strip()
if not line or line.startswith("#") or line.startswith("import "):
continue
elif _dirmatch(line, old_dir):
lines[num] = line.replace(old_dir, new_dir, 1)
has_change = True
if has_change:
with open(filename, "w") as f:
payload = (
os.linesep.join([line.strip() for line in lines]) + os.linesep
)
f.write(payload)
def fixup_egglink_file(filename: str, old_dir: str, new_dir: str) -> None:
"""Within a given file, replace all mentions of the old venv with the path
of the new venv.
Args:
filename: Name of the file
old_dir: Directory location of the old virtual environment
new_dir: Directory location of the new virtual environment
"""
logger.debug("Replacing paths within .egg-link file: %s" % filename)
with open(filename, "rb") as f:
link = f.read().decode("utf-8").strip()
if _dirmatch(link, old_dir):
link = link.replace(old_dir, new_dir, 1)
with open(filename, "wb") as f:
link = (link + "\n").encode("utf-8")
f.write(link)