Ejemplo n.º 1
0
def run_rez_shell(command, rez_pkg, weight=False):
        """Runs provided command inside rez-resolved shell.

            Args: 
                command (str): custom command
            Returns:
                pid: Process object of subshell.

            Note: pid runs in separate process and needs to be waited with wait()
                command outside this function, if commad takes time.
        """
        if not REZ_FOUND:
            print "Can't execute command in rez configured subshell." 
            print "No rez package found! (does $REZ_CONFIG_FILE exist?) "
            return False

        from rez.resolved_context import ResolvedContext

        if not command:
            return self.EmtyProcess()

        context = ResolvedContext(rez_pkg)
        rez_pid = context.execute_command(command)  

        return rez_pid 
Ejemplo n.º 2
0
def run_rez_shell(command, rez_pkg, weight=False):
    """Runs provided command inside rez-resolved shell.

            Args: 
                command (str): custom command
            Returns:
                pid: Process object of subshell.

            Note: pid runs in separate process and needs to be waited with wait()
                command outside this function, if commad takes time.
        """
    if not REZ_FOUND:
        print "Can't execute command in rez configured subshell."
        print "No rez package found! (does $REZ_CONFIG_FILE exist?) "
        return False

    from rez.resolved_context import ResolvedContext

    if not command:
        return self.EmtyProcess()

    context = ResolvedContext(rez_pkg)
    rez_pid = context.execute_command(command)

    return rez_pid
Ejemplo n.º 3
0
    def test_execute_command(self):
        """Test command execution in context."""
        if platform_.name == "windows":
            self.skipTest("This test does not run on Windows due to problems"
                          " with the automated binding of the 'hello_world'"
                          " executable.")

        r = ResolvedContext(["hello_world"])
        p = r.execute_command(["hello_world"], stdout=subprocess.PIPE, text=True)
        stdout, _ = p.communicate()
        stdout = stdout.strip()
        self.assertEqual(stdout, "Hello Rez World!")
Ejemplo n.º 4
0
    def test_execute_command(self):
        """Test command execution in context."""
        if platform_.name == "windows":
            self.skipTest("This test does not run on Windows due to problems"
                          " with the automated binding of the 'hello_world'"
                          " executable.")

        r = ResolvedContext(["hello_world"])
        p = r.execute_command(["hello_world"], stdout=subprocess.PIPE)
        stdout, _ = p.communicate()
        stdout = stdout.strip()
        self.assertEqual(stdout, "Hello Rez World!")
Ejemplo n.º 5
0
    def test_execute_command_environ(self):
        """Test that execute_command properly sets environ dict."""
        parent_environ = {"BIGLY": "covfefe"}
        r = ResolvedContext(["hello_world"])

        pycode = ("import os; "
                  "print(os.getenv(\"BIGLY\")); "
                  "print(os.getenv(\"OH_HAI_WORLD\"))")

        args = ["python", "-c", pycode]

        p = r.execute_command(args, parent_environ=parent_environ,
                              stdout=subprocess.PIPE)
        stdout, _ = p.communicate()
        stdout = stdout.strip()
        parts = [x.strip() for x in stdout.decode("utf-8").split('\n')]

        self.assertEqual(parts, ["covfefe", "hello"])
Ejemplo n.º 6
0
    def test_execute_command_environ(self):
        """Test that execute_command properly sets environ dict."""
        parent_environ = {"BIGLY": "covfefe"}
        r = ResolvedContext(["hello_world"])

        pycode = ("import os; "
                  "print os.getenv(\"BIGLY\"); "
                  "print os.getenv(\"OH_HAI_WORLD\")")

        args = ["python", "-c", pycode]

        p = r.execute_command(args, parent_environ=parent_environ,
                              stdout=subprocess.PIPE)
        stdout, _ = p.communicate()
        stdout = stdout.strip()
        parts = [x.strip() for x in stdout.split('\n')]

        self.assertEqual(parts, ["covfefe", "hello"])
Ejemplo n.º 7
0
    def test_execute_command_environ(self):
        """Test that execute_command properly sets environ dict."""
        if platform_.name == "windows":
            self.skipTest("This test does not run on Windows due to problems"
                          " with the automated binding of the 'hello_world'"
                          " executable.")

        parent_environ = {"BIGLY": "covfefe"}
        r = ResolvedContext(["hello_world"])

        pycode = ("import os; "
                  "print(os.getenv('BIGLY')); "
                  "print(os.getenv('OH_HAI_WORLD'))")

        args = [sys.executable, "-c", pycode]
        p = r.execute_command(args, parent_environ=parent_environ,
                              stdout=subprocess.PIPE)
        stdout, _ = p.communicate()
        stdout = stdout.strip()
        parts = [x.strip() for x in stdout.split('\n')]

        self.assertEqual(parts, ["covfefe", "hello"])
Ejemplo n.º 8
0
def run_rez_shell(cmd, vars, rez_pkg=None):
    """Runs provided command inside rez-resolved shell.

        Args:
            cmd_key (str): Key in self defined dict of shell commands
                or custom command
            vars   (dict): Dictionary of command line args names and values
                to be replaced inside command. This usually is: input, output,
                but could be anything else, which will be recognized by command line.

        Returns:
            pid: Process object of subshell.

        Note: pid runs in separate process and needs to be waited with wait()
            command outside this function, if commad takes time.
    """
    from rez.resolved_context import ResolvedContext

    def resolve_vars(cmd, vars):
        """Expands vars into cmd."""
        from string import Template
        try:
            template = Template(cmd)
            convert_command = template.substitute(vars)
        except:
            print "[ERROR] can't expand %s: %s" % (vars, cmd)
            return None
        return convert_command

    command = resolve_vars(cmd, vars)
    print command

    context = ResolvedContext(rez_pkg)
    rez_pid = context.execute_command(command)

    return rez_pid
Ejemplo n.º 9
0
def find_pip_from_context(python_version, pip_version=None):
    """Find pip from rez context.

    Args:
        python_version (str or `Version`): Python version to use
        pip_version (str or `Version`): Version of pip to use, or latest.

    Returns:
        3-tuple:
        - str: Python executable or None if we fell back to system pip.
        - str: Pip version or None if we fell back to system pip.
        - `ResolvedContext`: Context containing pip, or None if we fell back
          to system pip.
    """
    target = "python"
    package_request = []

    if python_version:
        ver = Version(str(python_version))
        python_major_minor_ver = ver.trim(2)
    else:
        # use latest major.minor
        package = get_latest_package("python")
        if package:
            python_major_minor_ver = package.version.trim(2)
        else:
            raise BuildError("Found no python package.")

    python_package = "python-%s" % str(python_major_minor_ver)

    package_request.append(python_package)

    if pip_version:
        target = "pip"
        if pip_version == "latest":
            package_request.append("pip")
        else:
            package_request.append("pip-%s" % str(pip_version))

    print_info("Trying to use pip from %s package", target)

    try:
        context = ResolvedContext(package_request)
    except (PackageFamilyNotFoundError, PackageNotFoundError):
        print_debug("No rez package called %s found", target)
        return None, None, None

    py_exe_name = "python"
    if platform_.name != "windows":
        # Python < 2 on Windows doesn't have versionned executable.
        py_exe_name += str(python_major_minor_ver.trim(1))

    py_exe = context.which(py_exe_name)

    proc = context.execute_command(
        # -E and -s are used to isolate the environment as much as possible.
        # See python --help for more details. We absolutely don't want to get
        # pip from the user home.
        [
            py_exe, "-E", "-s", "-c",
            "import pip, sys; sys.stdout.write(pip.__version__)"
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True)
    out, err = proc.communicate()
    if proc.returncode:
        print_debug("Failed to get pip from package %s", target)
        print_debug(out)
        print_debug(err)
        return None, None, None

    pip_version = out.strip()

    variant = context.get_resolved_package(target)
    package = variant.parent
    print_info("Found pip-%s inside %s. Will use it with %s", pip_version,
               package.uri, py_exe)

    return py_exe, pip_version, context