Example #1
0
    def ensure_venv(self, path_services):
        # ensure env exists
        path = path_services.venv(self.resolved_py)
        if not os.path.exists(path):
            fmt = "info: SnapshotSession.ensure_venv - venv do not exist; building..."
            logg.out(fmt, flush=True)
            diagnostic, py_cmd_venv = make_venv(path, self.cmd_python,
                                                self.asked_py)
            if diagnostic != "":
                return diagnostic
            logg.out("info: SnapshotSession.ensure_venv - py_cmd_venv:",
                     py_cmd_venv,
                     flush=True)
            print("SnapshotSession.ensure_venv - py_cmd_venv:", py_cmd_venv)
            print("path logs:", path_services.venv_logs(self.resolved_py))
            os.mkdir(path_services.venv_logs(self.resolved_py))

            # install remembercases in the venv
            diagnostic = pip_dev_install_in_venv(py_cmd_venv,
                                                 path_services.remembercases)
            if diagnostic != "":
                return diagnostic
        else:
            logg.out(
                "info: SnapshotSession.ensure_venv - venv exist, recycling",
                flush=True)
            py_cmd_venv = get_py_cmd_venv(path)
        self.py_cmd_venv = py_cmd_venv

        # smoketest: 'python' version must be resolved_py
        diag = "ERROR: SnapshotSession.ensure_venv, #smoke test failed: "
        try:
            diagnostic, venv_py = query_venv_python_version(self.py_cmd_venv)
        except Exception as ex:
            diagnostic = ''.join(
                tb.format_exception(None, ex, ex.__traceback__))
        if diagnostic != "":
            diagnostic = diag + diagnostic
            return diagnostic
        if venv_py != self.resolved_py:
            msg = diag + "unexpected py version in venv. Expected: %s, found: %s"
            diagnostic = msg % (self.resolved_py, venv_py)
        if diagnostic != "":
            return diagnostic

        # update pip for less noise in logs; log errors but dont force bailout,
        # maybe it is only no internet
        logg.out("info: updating pip in the venv")
        cmdline = self.py_cmd_venv + [
            "-m", "pip", "install", "pip", "--upgrade"
        ]
        try:
            cm.cmd_run_ok(cmdline)
        except cm.CmdExecutionError:
            msg = "SnapshotSession.ensure_venv #pip update failed for venv: %s"
            logg.out("WARN:",
                     msg % path_services.venv(self.resolved_py),
                     flush=True)
        diagnostic = ""
        return diagnostic
Example #2
0
def package_in_venv(py_cmd_venv, import_name):
    cmdline = py_cmd_venv + ["-c", "import %s" % import_name]
    #    cmdline = ["python", "-c", "import %s" % import_name]
    try:
        cm.cmd_run_ok(cmdline)
        found = True
    except cm.CmdExecutionError:
        found = False
    return found
Example #3
0
def pip_dev_install_in_venv(py_cmd_venv, path):
    cmdline = py_cmd_venv + ["-m", "pip", "install", "-e", path]
    #    cmdline = ["python", "-m", "pip", "install", "-e", path]
    try:
        cm.cmd_run_ok(cmdline)
        diagnostic = ""
    except cm.CmdExecutionError:
        msg = "Error while trying an install with cmdline:\n%s"
        diagnostic = msg % " ".join(cmdline)
    return diagnostic
Example #4
0
def make_venv(path_venv, python_cmdline, asked_py):
    cmdline = list(python_cmdline)
    if asked_py.startswith("2"):
        cmdline.extend(["-m", "virtualenv", path_venv])
    else:
        cmdline.extend(["-m", "venv", path_venv])
    try:
        cm.cmd_run_ok(cmdline)
        diagnostic = ""
        #?
        py_cmd_venv = get_py_cmd_venv(path_venv)
    except cm.CmdExecutionError:
        msg = ("Error, venv creation failed for py_asked version: %s\n" +
               "cmdline was: %s\n")
        diagnostic = msg % (asked_py, cmdline)
        py_cmd_venv = None
    return diagnostic, py_cmd_venv
Example #5
0
def pip_uninstall_in_venv(py_cmd_venv, pip_name):
    cmdline = py_cmd_venv + ["-m", "pip", "uninstall", "-y", pip_name]
    #    ["python", "-m", "pip", "uninstall", pip_name]
    try:
        out = cm.cmd_run_ok(cmdline)
    except cm.CmdExecutionError:
        out = ("Error in pip_uninstall_in_venv, got cm.CmdExecutionError.\n" +
               "Cmdline was: %s" % " ".join(cmdline))
    logg.out("pip_uninstall_in_venv out: " + out)
    diagnostic = ""
    return diagnostic
Example #6
0
def package_dir_in_venv(py_cmd_venv, import_name):
    prog = ("import os, sys, %s;" +
            "pkgdir = os.path.dirname(os.path.abspath(%s.__file__));" +
            "sys.stdout(pkgdir") % (import_name, import_name)
    cmdline = py_cmd_venv + ["-c", prog]
    out = None
    try:
        out = cm.cmd_run_ok(cmdline)
        diagnostic = ""
    except cm.CmdExecutionError:
        msg = ("Error while getting a pkgdir in the venv\n" + "cmdline:%s\n")
        diagnostic = msg % " ".join(cmdline)
    return diagnostic, out
Example #7
0
def query_venv_python_version(py_cmd_venv):
    cmdline = py_cmd_venv + ["-c", "import sys;sys.stdout.write(sys.version)"]
    vs = None
    try:
        out = cm.cmd_run_ok(cmdline)
        diagnostic = ""
    except cm.CmdExecutionError:
        msg = ("Error while probing activated venv\n" + "cmdline:\n")
        diagnostic = msg % " ".join(cmdline)
    if diagnostic == "":
        try:
            vs = out.split(" ")[0]
        except Exception:
            diagnostic = "Unexpected output of command '%s'\n" % " ".join(
                cmdline)
    return diagnostic, vs
Example #8
0
def query_python_version(python_cmdline):
    cmdline = list(python_cmdline)
    cmdline.extend(["-c", "import sys;sys.stdout.write(sys.version)"])
    vs = None
    try:
        out = cm.cmd_run_ok(cmdline)
        diagnostic = ""
    except Exception:
        msg = (
            "Error while running: '%s'\n" +
            "Probably that python version is not available or wasn't correctly\n"
            + "configured in test_conf.py")
        diagnostic = msg % " ".join(cmdline)
    if diagnostic == "":
        try:
            vs = out.split(" ")[0]
        except Exception:
            diagnostic = "Unexpected output of command '%s'\n" % " ".join(
                cmdline)
    return diagnostic, vs