예제 #1
0
파일: suite.py 프로젝트: mstreatfield/rez
    def activation_shell_code(self, shell=None):
        """Get shell code that should be run to activate this suite."""
        from rez.shells import create_shell
        from rez.rex import RexExecutor

        executor = RexExecutor(interpreter=create_shell(shell), parent_variables=["PATH"], shebang=False)
        executor.env.PATH.append(self.tools_path)
        return executor.get_output().strip()
예제 #2
0
    def activation_shell_code(self, shell=None):
        """Get shell code that should be run to activate this suite."""
        from rez.shells import create_shell
        from rez.rex import RexExecutor

        executor = RexExecutor(interpreter=create_shell(shell),
                               parent_variables=["PATH"],
                               shebang=False)
        executor.env.PATH.append(self.tools_path)
        return executor.get_output().strip()
예제 #3
0
def command(opts, parser, extra_arg_groups=None):
    from rez.shells import create_shell
    from rez.utils.formatting import columnise
    from rez.rex import RexExecutor, Python
    from pprint import pformat

    with open(opts.FILE) as f:
        code = f.read()

    interp = None
    if opts.format is None:
        interp = create_shell()
    elif opts.format in ('dict', 'table'):
        interp = Python(passive=True)
    else:
        interp = create_shell(opts.format)

    parent_env = {} if opts.no_env else None

    if opts.parent_vars == "all":
        parent_vars = True
    else:
        parent_vars = opts.parent_vars

    ex = RexExecutor(interpreter=interp,
                     parent_environ=parent_env,
                     parent_variables=parent_vars)

    ex.execute_code(code, filename=opts.FILE)

    o = ex.get_output()
    if isinstance(o, dict):
        if opts.format == "table":
            rows = [x for x in sorted(o.iteritems())]
            print '\n'.join(columnise(rows))
        else:
            print pformat(o)
    else:
        print o
예제 #4
0
파일: interpret.py 프로젝트: skral/rez
def command(opts, parser, extra_arg_groups=None):
    from rez.shells import create_shell
    from rez.utils.formatting import columnise
    from rez.rex import RexExecutor, Python
    from pprint import pformat

    with open(opts.FILE) as f:
        code = f.read()

    interp = None
    if opts.format is None:
        interp = create_shell()
    elif opts.format in ('dict', 'table'):
        interp = Python(passive=True)
    else:
        interp = create_shell(opts.format)

    parent_env = {} if opts.no_env else None

    if opts.parent_vars == "all":
        parent_vars = True
    else:
        parent_vars = opts.parent_vars

    ex = RexExecutor(interpreter=interp,
                     parent_environ=parent_env,
                     parent_variables=parent_vars)

    ex.execute_code(code, filename=opts.FILE)

    o = ex.get_output()
    if isinstance(o, dict):
        if opts.format == "table":
            rows = [x for x in sorted(o.iteritems())]
            print '\n'.join(columnise(rows))
        else:
            print pformat(o)
    else:
        print o
예제 #5
0
    def spawn_shell(self,
                    context_file,
                    tmpdir,
                    rcfile=None,
                    norc=False,
                    stdin=False,
                    command=None,
                    env=None,
                    quiet=False,
                    pre_command=None,
                    **Popen_args):

        startup_sequence = self.get_startup_sequence(rcfile, norc, bool(stdin),
                                                     command)
        shell_command = None

        def _record_shell(ex, files, bind_rez=True, print_msg=False):
            ex.source(context_file)
            if startup_sequence["envvar"]:
                ex.unsetenv(startup_sequence["envvar"])
            if bind_rez:
                ex.interpreter._bind_interactive_rez()
            if print_msg and not quiet:
                ex.info('You are now in a rez-configured environment.')

                # Rez may not be available.
                # Or it may not have an associated Python interpreter
                # available, in which case the command will succeed
                # but output to stderr, muted with 2>$null
                ex.command("Try { rez context 2>$null } Catch { }")

        executor = RexExecutor(interpreter=self.new_shell(),
                               parent_environ={},
                               add_default_namespaces=False)

        if startup_sequence["command"] is not None:
            _record_shell(executor, files=startup_sequence["files"])
            shell_command = startup_sequence["command"]
        else:
            _record_shell(executor,
                          files=startup_sequence["files"],
                          print_msg=(not quiet))

        if shell_command:
            executor.command(shell_command)

        # Forward exit call to parent PowerShell process
        executor.command("exit $LastExitCode")

        code = executor.get_output()
        target_file = os.path.join(tmpdir,
                                   "rez-shell.%s" % self.file_extension())

        with open(target_file, 'w') as f:
            f.write(code)

        cmd = []
        if pre_command:
            cmd = pre_command

            if not isinstance(cmd, (tuple, list)):
                cmd = pre_command.rstrip().split()

        cmd += [
            self.executable,
            "-NoLogo ",

            # Prevent custom user profile from leaking into
            # the resulting environment.
            "-NoProfile ",

            # Establish a session within which arbitrary
            # PowerShell scripts may be run.
            "-ExecutionPolicy",
            "Unrestricted",

            # Start from this script
            '. "{}"'.format(target_file)
        ]

        if shell_command is None:
            cmd.insert(1, "-NoExit")

        # No environment was explicity passed
        if not env and not config.inherit_parent_environment:
            env = self.environment()

        p = popen(cmd, env=env, universal_newlines=True, **Popen_args)
        return p
예제 #6
0
    def spawn_shell(self,
                    context_file,
                    tmpdir,
                    rcfile=None,
                    norc=False,
                    stdin=False,
                    command=None,
                    env=None,
                    quiet=False,
                    pre_command=None,
                    **Popen_args):

        startup_sequence = self.get_startup_sequence(rcfile, norc, bool(stdin),
                                                     command)
        shell_command = None

        def _record_shell(ex, files, bind_rez=True, print_msg=False):
            ex.source(context_file)
            if startup_sequence["envvar"]:
                ex.unsetenv(startup_sequence["envvar"])
            if bind_rez:
                ex.interpreter._bind_interactive_rez()
            if print_msg and not quiet:
                # Rez may not be available
                ex.command("Try { rez context } Catch { }")

        executor = RexExecutor(interpreter=self.new_shell(),
                               parent_environ={},
                               add_default_namespaces=False)

        if startup_sequence["command"] is not None:
            _record_shell(executor, files=startup_sequence["files"])
            shell_command = startup_sequence["command"]
        else:
            _record_shell(executor,
                          files=startup_sequence["files"],
                          print_msg=(not quiet))

        self._additional_commands(executor)

        if shell_command:
            executor.command(shell_command)

        # Forward exit call to parent PowerShell process
        executor.command("exit $LastExitCode")

        code = executor.get_output()
        target_file = os.path.join(tmpdir,
                                   "rez-shell.%s" % self.file_extension())

        with open(target_file, 'w') as f:
            f.write(code)

        cmd = []
        if pre_command:
            cmd = pre_command

            if not isinstance(cmd, (tuple, list)):
                cmd = pre_command.rstrip().split()

        cmd += [self.executable]

        # Suppresses copyright message of PowerShell and pwsh
        cmd += ["-NoLogo"]

        # Generic form of sourcing that works in powershell and pwsh
        cmd += ["-File", target_file]

        if shell_command is None:
            cmd.insert(1, "-NoExit")

        p = popen(cmd, env=env, **Popen_args)
        return p
예제 #7
0
 def _create_ex():
     return RexExecutor(interpreter=self.new_shell(),
                        parent_environ={},
                        add_default_namespaces=False)
예제 #8
0
 def _create_executor(self, env, **kwargs):
     interp = Python(target_environ={}, passive=True)
     return RexExecutor(interpreter=interp,
                        parent_environ=env,
                        shebang=False,
                        **kwargs)
예제 #9
0
 def _create_ex():
     parent_vars = True if config.all_parent_variables \
         else config.parent_variables
     return RexExecutor(interpreter=self.new_shell(),
                        parent_variables=parent_vars,
                        add_default_namespaces=False)