import os.path
import shutil
from ansible.module_utils import common_utils
class VenvInstaller(object):
_WAIT_TIME = 60
def __init__(self, module, venv_dir, pylibs_dir, python_dir, pkg_cmd=""):
self.module = module
self.venv_dir = venv_dir
self.pylibs_dir = pylibs_dir
self.python_dir = python_dir
self.pkg_cmd = pkg_cmd
self.venv_pip_path = os.path.join(venv_dir, "bin", "pip3")
def create_venv_dir(self):
if os.path.exists(self.venv_dir):
shutil.rmtree(self.venv_dir)
return common_utils.run_command(self.module, "python3 -m venv {}".format(self.venv_dir))
def update_pip(self):
cmd = "{} install --upgrade pip --no-index --find-links {}".format(self.venv_pip_path, self.pylibs_dir)
return common_utils.run_command(self.module, cmd)
def install_pkg(self, pkg):
cmd = "{} install {} --no-index --find-links {}".format(self.venv_pip_path, pkg, self.pylibs_dir)
return common_utils.run_command(self.module, cmd)
def create_link(self):
if not self.pkg_cmd:
return
source = os.path.join(self.venv_dir, "bin", self.pkg_cmd)
target = os.path.join(self.python_dir, "bin")
if not target:
raise Exception("Target path not existed: {}.".format(target))
shutil.copy(source, target)