30
30
import sys
31
31
import json
32
32
import shutil
33
- import pkgutil
34
33
import subprocess
35
34
import virtualenv
36
35
from distlib .scripts import ScriptMaker
@@ -303,27 +302,28 @@ def __init__(self, data):
303
302
304
303
class Installer :
305
304
306
- site_template = r"""# -*- coding: utf-8 -*-
307
- import site
308
- site.addsitedir(r"{revision}")
309
- """
310
-
311
305
def __init__ (self , revision ):
312
306
self ._container = revision .container ()
313
307
self ._revision = revision
314
308
self ._default_venv = None
309
+ self ._shared_tools = list ()
315
310
316
311
def install (self , tool ):
317
312
if tool .isolation or tool .name == "rez" :
318
313
venv_session = self .create_venv (tool )
319
314
if tool .name == "rez" :
320
315
self ._default_venv = venv_session
316
+ self ._shared_tools .append (tool )
321
317
else :
322
318
venv_session = self ._default_venv
323
319
324
320
if venv_session is None :
325
321
raise Exception ("No python venv created, this is a bug." )
326
322
323
+ if venv_session is not self ._default_venv :
324
+ for shared in self ._shared_tools :
325
+ self .install_package (shared , venv_session )
326
+
327
327
self .install_package (tool , venv_session )
328
328
329
329
def create_venv (self , tool ):
@@ -337,81 +337,72 @@ def create_venv(self, tool):
337
337
]
338
338
session = virtualenv .cli_run (args )
339
339
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
-
346
340
return session
347
341
348
342
def install_package (self , tool , venv_session ):
349
343
python_exec = str (venv_session .creator .exe )
350
344
cmd = [python_exec , "-m" , "pip" , "install" ]
351
345
352
346
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 )
358
350
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
+ ]
361
355
362
356
print ("Installing %s.." % tool .name )
363
357
subprocess .check_output (cmd )
364
358
359
+ self .create_production_scripts (tool , venv_session )
365
360
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"
369
369
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
388
370
_locals = {"_rez_version" : "" }
389
371
with open (version_py ) as f :
390
372
exec (f .read (), globals (), _locals )
391
- with open (bin_path / ".rez_production_install" , "w" ) as f :
373
+ with open (validator , "w" ) as f :
392
374
f .write (_locals ["_rez_version" ])
393
375
394
376
def create_production_scripts (self ,
395
377
tool ,
396
- venv_session ,
397
- specifications = None ):
378
+ venv_session ):
398
379
"""Create Rez production used binary scripts
399
380
400
381
The binary script will be executed with Python interpreter flag -E,
401
382
which will ignore all PYTHON* env vars, e.g. PYTHONPATH and PYTHONHOME.
402
383
403
384
"""
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
+ ]
413
402
414
403
bin_path = self ._revision .path () / "bin"
404
+ if not bin_path .is_dir ():
405
+ os .makedirs (bin_path )
415
406
416
407
maker = ScriptMaker (source_dir = None , target_dir = bin_path )
417
408
maker .executable = str (venv_session .creator .exe )
0 commit comments