2019-12-30 12:59:52 -05:00
|
|
|
# This can set up an environment at runtime.
|
|
|
|
# This removes the necessity of extra libs to be installed persistently.
|
|
|
|
# However, it is recommended that you install all dependencies in the system itself, because some aren't available
|
|
|
|
# through pip/PyPi.
|
2019-09-30 22:08:37 -04:00
|
|
|
# Before you hoot and holler about this, Let's Encrypt's certbot-auto does the same thing.
|
|
|
|
# Except I segregate it out even further; I don't even install pip into the system python.
|
|
|
|
|
|
|
|
import ensurepip
|
|
|
|
import json
|
2019-12-30 12:59:52 -05:00
|
|
|
import logging
|
2019-09-30 22:08:37 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-10-09 07:18:10 -04:00
|
|
|
import tempfile
|
2019-09-30 22:08:37 -04:00
|
|
|
import venv
|
2019-10-30 03:46:33 -04:00
|
|
|
##
|
2019-11-06 12:48:18 -05:00
|
|
|
import aif.constants_fallback
|
2019-09-30 22:08:37 -04:00
|
|
|
|
2019-12-30 12:59:52 -05:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-09-30 22:08:37 -04:00
|
|
|
class EnvBuilder(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.vdir = tempfile.mkdtemp(prefix = '.aif_', suffix = '_VENV')
|
|
|
|
self.venv = venv.create(self.vdir, system_site_packages = True, clear = True, with_pip = True)
|
|
|
|
ensurepip.bootstrap(root = self.vdir)
|
|
|
|
# pip does some dumb env var things and doesn't clean up after itself.
|
|
|
|
for v in ('PIP_CONFIG_FILE', 'ENSUREPIP_OPTIONS', 'PIP_REQ_TRACKER', 'PLAT'):
|
|
|
|
if os.environ.get(v):
|
|
|
|
del(os.environ[v])
|
|
|
|
moddir_raw = subprocess.run([os.path.join(self.vdir,
|
|
|
|
'bin',
|
|
|
|
'python3'),
|
|
|
|
'-c',
|
|
|
|
('import site; '
|
|
|
|
'import json; '
|
|
|
|
'print(json.dumps(site.getsitepackages(), indent = 4))')],
|
2019-12-30 12:59:52 -05:00
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.PIPE)
|
|
|
|
_logger.info('Executed: {0}'.format(' '.join(moddir_raw.args)))
|
|
|
|
if moddir_raw.returncode != 0:
|
|
|
|
_logger.warning('Command returned non-zero status')
|
|
|
|
_logger.debug('Exit status: {0}'.format(str(moddir_raw.returncode)))
|
|
|
|
for a in ('stdout', 'stderr'):
|
|
|
|
x = getattr(moddir_raw, a)
|
|
|
|
if x:
|
|
|
|
_logger.debug('{0}: {1}'.format(a.upper(), x.decode('utf-8').strip()))
|
|
|
|
raise RuntimeError('Failed to establish environment successfully')
|
2019-09-30 22:08:37 -04:00
|
|
|
self.modulesdir = json.loads(moddir_raw.stdout.decode('utf-8'))[0]
|
|
|
|
# This is SO. DUMB. WHY DO I HAVE TO CALL PIP FROM A SHELL. IT'S WRITTEN IN PYTHON.
|
|
|
|
# https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program
|
2019-11-06 12:48:18 -05:00
|
|
|
for m in aif.constants_fallback.EXTERNAL_DEPS:
|
2019-09-30 22:08:37 -04:00
|
|
|
pip_cmd = [os.path.join(self.vdir,
|
|
|
|
'bin',
|
|
|
|
'python3'),
|
|
|
|
'-m',
|
|
|
|
'pip',
|
|
|
|
'install',
|
|
|
|
'--disable-pip-version-check',
|
|
|
|
m]
|
2019-12-30 12:59:52 -05:00
|
|
|
cmd = subprocess.run(pip_cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
|
|
|
|
_logger.info('Executed: {0}'.format(' '.join(cmd.args)))
|
|
|
|
if cmd.returncode != 0:
|
|
|
|
_logger.warning('Command returned non-zero status')
|
|
|
|
_logger.debug('Exit status: {0}'.format(str(cmd.returncode)))
|
|
|
|
for a in ('stdout', 'stderr'):
|
|
|
|
x = getattr(cmd, a)
|
|
|
|
if x:
|
|
|
|
_logger.debug('{0}: {1}'.format(a.upper(), x.decode('utf-8').strip()))
|
|
|
|
raise RuntimeError('Failed to install module successfully')
|
2019-09-30 22:08:37 -04:00
|
|
|
# And now make it available to other components.
|
|
|
|
sys.path.insert(1, self.modulesdir)
|