Created
December 2, 2015 21:06
-
-
Save kikocorreoso/284e1ad934325ca6ed0c to your computer and use it in GitHub Desktop.
Proof of concept to see the possibility to inherit venvs in an easy way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import argparse | |
import subprocess | |
from venv import EnvBuilder | |
# Custom options | |
parser = argparse.ArgumentParser(description='inherit venvs') | |
parser.add_argument( | |
'--other-venv-paths', | |
nargs='+', | |
help="Paths to other venvs" | |
) | |
# default venv options | |
parser.add_argument('dirs', metavar='ENV_DIR', nargs='+', | |
help='A directory to create the environment in.') | |
parser.add_argument('--system-site-packages', default=False, | |
action='store_true', dest='system_site', | |
help='Give the virtual environment access to the ' | |
'system site-packages dir.') | |
if os.name == 'nt': | |
use_symlinks = False | |
else: | |
use_symlinks = True | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument('--symlinks', default=use_symlinks, | |
action='store_true', dest='symlinks', | |
help='Try to use symlinks rather than copies, ' | |
'when symlinks are not the default for ' | |
'the platform.') | |
group.add_argument('--copies', default=not use_symlinks, | |
action='store_false', dest='symlinks', | |
help='Try to use copies rather than symlinks, ' | |
'even when symlinks are the default for ' | |
'the platform.') | |
parser.add_argument('--clear', default=False, action='store_true', | |
dest='clear', help='Delete the contents of the ' | |
'environment directory if it ' | |
'already exists, before ' | |
'environment creation.') | |
parser.add_argument('--upgrade', default=False, action='store_true', | |
dest='upgrade', help='Upgrade the environment ' | |
'directory to use this version ' | |
'of Python, assuming Python ' | |
'has been upgraded in-place.') | |
parser.add_argument('--without-pip', dest='with_pip', | |
default=True, action='store_false', | |
help='Skips installing or upgrading pip in the ' | |
'virtual environment (pip is bootstrapped ' | |
'by default)') | |
args = parser.parse_args() | |
def parse_stdout(mystr): | |
mystr = mystr.decode('utf') | |
mystr = (mystr.replace('"','') | |
.replace("'",'') | |
.replace("[",'') | |
.replace("]",'') | |
.replace("\n",'') | |
) | |
mystr = mystr.split(',') | |
return [v.rstrip().lstrip() for v in mystr if 'site-packages' in v] | |
def main(): | |
if args.upgrade and args.clear: | |
raise ValueError('you cannot supply --upgrade and --clear together.') | |
builder = EnvBuilder(system_site_packages=args.system_site, | |
clear=args.clear, | |
symlinks=args.symlinks, | |
upgrade=args.upgrade, | |
with_pip=args.with_pip) | |
for d in args.dirs: | |
builder.create(d) | |
if __name__ == '__main__': | |
# Create the new venv | |
rc = 1 | |
try: | |
main() | |
rc = 0 | |
except Exception as e: | |
print('Error: %s' % e, file=sys.stderr) | |
#sys.exit(rc) | |
if args.other_venv_paths: | |
other_paths = args.other_venv_paths | |
sys_path = [] | |
for path in other_paths: | |
python_path = os.path.join(path, 'bin', 'python') | |
print(python_path) | |
proc = subprocess.Popen( | |
[python_path, "-c", "import sys; print(sys.path)"], | |
stdout=subprocess.PIPE | |
) | |
sys_path.append(parse_stdout(proc.communicate()[0])) | |
pyvers = 'python{0}.{1}'.format(*sys.version_info[0:2]) | |
for d in args.dirs: | |
path = os.path.join(d, 'lib', pyvers, 'site-packages') | |
usercustomize = open( | |
os.path.join(path, 'sitecustomize.py'), | |
'w' | |
) | |
usercustomize.write( | |
"""print('Loading superior venvs') | |
import sys | |
""") | |
for other_path in sys_path: | |
print(other_path) | |
usercustomize.write( | |
"sys.path.append('{}')\n".format(other_path[0]) | |
) | |
usercustomize.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment