def _convert_to_rex(self, commands): if isinstance(commands, list): from rez.utils.backcompat import convert_old_commands msg = "package %r is using old-style commands." % self.uri if config.disable_rez_1_compatibility or config.error_old_commands: raise SchemaError(None, msg) elif config.warn("old_commands"): print_warning(msg) commands = convert_old_commands(commands) if isinstance(commands, basestring): return SourceCode(source=commands) elif callable(commands): return SourceCode(func=commands) else: return commands
def _convert_to_rex(self, commands): if isinstance(commands, list): from rez.utils.backcompat import convert_old_commands msg = "package %r is using old-style commands." % self.uri if config.disable_rez_1_compatibility or config.error_old_commands: raise SchemaError(None, msg) elif config.warn("old_commands"): print_warning(msg) commands = convert_old_commands(commands) if isinstance(commands, basestring): return SourceCode(commands) elif callable(commands): return SourceCode.from_function(commands) else: return commands
def _unsupported_option(cls, option, val): if val and config.warn("shell_startup"): print_warning("%s ignored, not supported by %s shell" % (option, cls.name()))
def spawn_shell(self, context_file, tmpdir, rcfile=None, norc=False, stdin=False, command=None, env=None, quiet=False, pre_command=None, add_rez=True, package_commands_sourced_first=None, **Popen_args): d = self.get_startup_sequence(rcfile, norc, bool(stdin), command) envvar = d["envvar"] files = d["files"] bind_files = d["bind_files"] do_rcfile = d["do_rcfile"] shell_command = None if package_commands_sourced_first is None: package_commands_sourced_first = config.package_commands_sourced_first def _record_shell(ex, files, bind_rez=True, print_msg=False): if bind_rez and package_commands_sourced_first: ex.source(context_file) for file_ in files: if os.path.exists(os.path.expanduser(file_)): ex.source(file_) if bind_rez and not package_commands_sourced_first: ex.source(context_file) if envvar: ex.unsetenv(envvar) if add_rez and bind_rez: ex.interpreter._bind_interactive_rez() if print_msg and add_rez and not quiet: ex.info('') ex.info('You are now in a rez-configured environment.') ex.info('') if system.is_production_rez_install: ex.command('rezolve context') def _write_shell(ex, filename): code = ex.get_output() target_file = os.path.join(tmpdir, filename) with open(target_file, 'w') as f: f.write(code) return target_file def _create_ex(): return RexExecutor(interpreter=self.new_shell(), parent_environ={}, add_default_namespaces=False) executor = _create_ex() if self.settings.prompt: newprompt = '${REZ_ENV_PROMPT}%s' % self.settings.prompt executor.interpreter._saferefenv('REZ_ENV_PROMPT') executor.env.REZ_ENV_PROMPT = newprompt if d["command"] is not None: _record_shell(executor, files=files) shell_command = d["command"] else: if d["stdin"]: assert(self.stdin_arg) shell_command = "%s %s" % (self.executable, self.stdin_arg) quiet = True elif do_rcfile: assert(self.rcfile_arg) shell_command = "%s %s" % (self.executable, self.rcfile_arg) else: shell_command = self.executable if do_rcfile: # hijack rcfile to insert our own script ex = _create_ex() _record_shell(ex, files=files, print_msg=(not quiet)) filename = "rcfile.%s" % self.file_extension() filepath = _write_shell(ex, filename) shell_command += " %s" % filepath elif envvar: # hijack env-var to insert our own script ex = _create_ex() _record_shell(ex, files=files, print_msg=(not quiet)) filename = "%s.%s" % (envvar, self.file_extension()) filepath = _write_shell(ex, filename) executor.setenv(envvar, filepath) else: # hijack $HOME to insert our own script files = [x for x in files if x not in bind_files] + list(bind_files) if files: for file in files: if file in bind_files: bind_rez = True files_ = [file] if d["source_bind_files"] else [] else: bind_rez = False files_ = [file] ex = _create_ex() ex.setenv('HOME', os.environ.get('HOME', '')) _record_shell(ex, files=files_, bind_rez=bind_rez, print_msg=bind_rez) _write_shell(ex, os.path.basename(file)) executor.setenv("HOME", tmpdir) # keep history if self.histfile and self.histvar: histfile = os.path.expanduser(self.histfile) if os.path.exists(histfile): executor.setenv(self.histvar, histfile) else: if config.warn("shell_startup"): print_warning( "WARNING: Could not configure environment from " "within the target shell (%s); this has been done " "in the parent process instead." % self.name()) executor.source(context_file) if shell_command: # an empty string means 'run no command and exit' executor.command(shell_command) executor.command("exit %s" % self.last_command_status) 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) if d["stdin"] and stdin and (stdin is not True): Popen_args["stdin"] = stdin cmd = [] if pre_command: if isinstance(pre_command, basestring): cmd = pre_command.strip().split() else: cmd = pre_command cmd.extend([self.executable, target_file]) try: p = subprocess.Popen(cmd, env=env, **Popen_args) except Exception as e: cmd_str = ' '.join(map(pipes.quote, cmd)) raise RezSystemError("Error running command:\n%s\n%s" % (cmd_str, str(e))) return p
def _overruled_option(cls, option, overruling_option, val): if val and config.warn("shell_startup"): print_warning("%s ignored by %s shell - overruled by %s option" % (option, cls.name(), overruling_option))
def _unsupported_option(cls, option, val): if val and config.warn("shell_startup"): print_warning("%s ignored, not supported by %s shell" % (option, cls.name()))
def spawn_shell(self, context_file, tmpdir, rcfile=None, norc=False, stdin=False, command=None, env=None, quiet=False, pre_command=None, add_rez=True, package_commands_sourced_first=None, **Popen_args): d = self.get_startup_sequence(rcfile, norc, bool(stdin), command) envvar = d["envvar"] files = d["files"] bind_files = d["bind_files"] do_rcfile = d["do_rcfile"] shell_command = None if package_commands_sourced_first is None: package_commands_sourced_first = config.package_commands_sourced_first def _record_shell(ex, files, bind_rez=True, print_msg=False): if bind_rez and package_commands_sourced_first: ex.source(context_file) for file_ in files: if os.path.exists(os.path.expanduser(file_)): ex.source(file_) if bind_rez and not package_commands_sourced_first: ex.source(context_file) if envvar: ex.unsetenv(envvar) if add_rez and bind_rez: ex.interpreter._bind_interactive_rez() if print_msg and add_rez and not quiet: ex.info('') ex.info('You are now in a rez-configured environment.') ex.info('') if system.is_production_rez_install: ex.command('rezolve context') def _write_shell(ex, filename): code = ex.get_output() target_file = os.path.join(tmpdir, filename) with open(target_file, 'w') as f: f.write(code) return target_file def _create_ex(): return RexExecutor(interpreter=self.new_shell(), parent_environ={}, add_default_namespaces=False) executor = _create_ex() if self.settings.prompt: newprompt = '${REZ_ENV_PROMPT}%s' % self.settings.prompt executor.interpreter._saferefenv('REZ_ENV_PROMPT') executor.env.REZ_ENV_PROMPT = newprompt if d["command"] is not None: _record_shell(executor, files=files) shell_command = d["command"] else: if d["stdin"]: assert (self.stdin_arg) shell_command = "%s %s" % (self.executable, self.stdin_arg) quiet = True elif do_rcfile: assert (self.rcfile_arg) shell_command = "%s %s" % (self.executable, self.rcfile_arg) else: shell_command = self.executable if do_rcfile: # hijack rcfile to insert our own script ex = _create_ex() _record_shell(ex, files=files, print_msg=(not quiet)) filename = "rcfile.%s" % self.file_extension() filepath = _write_shell(ex, filename) shell_command += " %s" % filepath elif envvar: # hijack env-var to insert our own script ex = _create_ex() _record_shell(ex, files=files, print_msg=(not quiet)) filename = "%s.%s" % (envvar, self.file_extension()) filepath = _write_shell(ex, filename) executor.setenv(envvar, filepath) else: # hijack $HOME to insert our own script files = [x for x in files if x not in bind_files ] + list(bind_files) if files: for file_ in files: if file_ in bind_files: bind_rez = True files_ = [file_] if d["source_bind_files"] else [] else: bind_rez = False files_ = [file_] ex = _create_ex() ex.setenv('HOME', os.environ.get('HOME', '')) _record_shell(ex, files=files_, bind_rez=bind_rez, print_msg=bind_rez) _write_shell(ex, os.path.basename(file_)) executor.setenv("HOME", tmpdir) # keep history if self.histfile and self.histvar: histfile = os.path.expanduser(self.histfile) if os.path.exists(histfile): executor.setenv(self.histvar, histfile) else: if config.warn("shell_startup"): print_warning( "WARNING: Could not configure environment from " "within the target shell (%s); this has been done " "in the parent process instead." % self.name()) executor.source(context_file) if shell_command: # an empty string means 'run no command and exit' executor.command(shell_command) executor.command("exit %s" % self.last_command_status) 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) if d["stdin"] and stdin and (stdin is not True): Popen_args["stdin"] = stdin cmd = [] if pre_command: if isinstance(pre_command, basestring): cmd = pre_command.strip().split() else: cmd = pre_command cmd.extend([self.executable, target_file]) try: p = subprocess.Popen(cmd, env=env, **Popen_args) except Exception as e: cmd_str = ' '.join(map(pipes.quote, cmd)) raise RezSystemError("Error running command:\n%s\n%s" % (cmd_str, str(e))) return p
def _overruled_option(cls, option, overruling_option, val): if val and config.warn("shell_startup"): print_warning("%s ignored by %s shell - overruled by %s option" % (option, cls.name(), overruling_option))