Skip to content

Commit 1ee4e16

Browse files
committed
refactored revision for pip --editable
pip flag `-e` and `--target` cannot be used at the same time, they are trying to resolve this but could take a while. See pypa/pip#9636. So I decide not to install rez with `--target` for all venv to access, just re-install it in every venv.
1 parent 6e30ae8 commit 1ee4e16

File tree

1 file changed

+43
-52
lines changed

1 file changed

+43
-52
lines changed

src/rezup/container.py

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import sys
3131
import json
3232
import shutil
33-
import pkgutil
3433
import subprocess
3534
import virtualenv
3635
from distlib.scripts import ScriptMaker
@@ -303,27 +302,28 @@ def __init__(self, data):
303302

304303
class Installer:
305304

306-
site_template = r"""# -*- coding: utf-8 -*-
307-
import site
308-
site.addsitedir(r"{revision}")
309-
"""
310-
311305
def __init__(self, revision):
312306
self._container = revision.container()
313307
self._revision = revision
314308
self._default_venv = None
309+
self._shared_tools = list()
315310

316311
def install(self, tool):
317312
if tool.isolation or tool.name == "rez":
318313
venv_session = self.create_venv(tool)
319314
if tool.name == "rez":
320315
self._default_venv = venv_session
316+
self._shared_tools.append(tool)
321317
else:
322318
venv_session = self._default_venv
323319

324320
if venv_session is None:
325321
raise Exception("No python venv created, this is a bug.")
326322

323+
if venv_session is not self._default_venv:
324+
for shared in self._shared_tools:
325+
self.install_package(shared, venv_session)
326+
327327
self.install_package(tool, venv_session)
328328

329329
def create_venv(self, tool):
@@ -337,81 +337,72 @@ def create_venv(self, tool):
337337
]
338338
session = virtualenv.cli_run(args)
339339

340-
# add sitecustomize.py
341-
site_script = session.creator.purelib / "sitecustomize.py"
342-
sitecustomize = self.site_template.format(revision=self._revision.path())
343-
with open(site_script, "w") as f:
344-
f.write(sitecustomize)
345-
346340
return session
347341

348342
def install_package(self, tool, venv_session):
349343
python_exec = str(venv_session.creator.exe)
350344
cmd = [python_exec, "-m", "pip", "install"]
351345

352346
if tool.edit:
353-
cmd.append("-e")
354-
cmd.append(tool.url)
355-
356-
# don't make noise
357-
cmd.append("--disable-pip-version-check")
347+
cmd += ["--editable", tool.url]
348+
else:
349+
cmd.append(tool.url)
358350

359-
if tool.name == "rez":
360-
cmd += ["--target", str(self._revision.path())]
351+
cmd += [
352+
# don't make noise
353+
"--disable-pip-version-check",
354+
]
361355

362356
print("Installing %s.." % tool.name)
363357
subprocess.check_output(cmd)
364358

359+
self.create_production_scripts(tool, venv_session)
365360
if tool.name == "rez":
366-
self.rez_production_install(tool, venv_session)
367-
else:
368-
self.create_production_scripts(tool, venv_session)
361+
self.mark_as_rez_production_install(tool, venv_session)
362+
363+
def mark_as_rez_production_install(self, tool, venv_session):
364+
validator = self._revision.path() / "bin" / ".rez_production_install"
365+
version_py = (
366+
Path(tool.url) / "src" if tool.edit
367+
else venv_session.creator.purelib # site-packages
368+
) / "rez" / "utils" / "_version.py"
369369

370-
def rez_production_install(self, tool, venv_session):
371-
bin_path = self._revision.path() / "bin"
372-
rez_cli = self._revision.path() / "rez" / "cli"
373-
version_py = self._revision.path() / "rez" / "utils" / "_version.py"
374-
375-
rez_entries = None
376-
for importer, modname, _ in pkgutil.iter_modules([str(rez_cli)]):
377-
if modname == "_entry_points":
378-
loader = importer.find_module(modname)
379-
rez_entries = loader.load_module(modname)
380-
break
381-
382-
for file in os.listdir(bin_path):
383-
os.remove(bin_path / file)
384-
specifications = rez_entries.get_specifications().values()
385-
self.create_production_scripts(tool, venv_session, specifications)
386-
387-
# mark as production install
388370
_locals = {"_rez_version": ""}
389371
with open(version_py) as f:
390372
exec(f.read(), globals(), _locals)
391-
with open(bin_path / ".rez_production_install", "w") as f:
373+
with open(validator, "w") as f:
392374
f.write(_locals["_rez_version"])
393375

394376
def create_production_scripts(self,
395377
tool,
396-
venv_session,
397-
specifications=None):
378+
venv_session):
398379
"""Create Rez production used binary scripts
399380
400381
The binary script will be executed with Python interpreter flag -E,
401382
which will ignore all PYTHON* env vars, e.g. PYTHONPATH and PYTHONHOME.
402383
403384
"""
404-
if specifications is None:
405-
site_packages = str(venv_session.creator.purelib)
406-
dists = Distribution.discover(name=tool.name, path=[site_packages])
407-
specifications = [
408-
f"{ep.name} = {ep.value}"
409-
for dist in dists
410-
for ep in dist.entry_points
411-
if ep.group == "console_scripts"
412-
]
385+
site_packages = venv_session.creator.purelib
386+
387+
if tool.edit:
388+
egg_link = site_packages / ("%s.egg-link" % tool.name)
389+
with open(egg_link, "r") as f:
390+
package_location = f.readline().strip()
391+
path = [str(package_location)]
392+
else:
393+
path = [str(site_packages)]
394+
395+
dists = Distribution.discover(name=tool.name, path=path)
396+
specifications = [
397+
f"{ep.name} = {ep.value}"
398+
for dist in dists
399+
for ep in dist.entry_points
400+
if ep.group == "console_scripts"
401+
]
413402

414403
bin_path = self._revision.path() / "bin"
404+
if not bin_path.is_dir():
405+
os.makedirs(bin_path)
415406

416407
maker = ScriptMaker(source_dir=None, target_dir=bin_path)
417408
maker.executable = str(venv_session.creator.exe)

0 commit comments

Comments
 (0)