def load_plugins(self): import pkgutil from rez.backport.importlib import import_module type_module_name = 'rezplugins.' + self.type_name package = import_module(type_module_name) # on import, the `__path__` variable of the imported package is extended # to include existing directories on the plugin search path (via # extend_path, above). this means that `walk_packages` will walk over all # modules on the search path at the same level (.e.g in a # 'rezplugins/type_name' sub-directory). paths = [package.__path__] if isinstance(package.__path__, basestring) \ else package.__path__ for path in paths: for loader, modname, ispkg in pkgutil.walk_packages( [path], package.__name__ + '.'): if loader is not None: plugin_name = modname.split('.')[-1] if plugin_name.startswith('_'): continue if config.debug("plugins"): print_debug("loading %s plugin at %s: %s..." % (self.type_name, path, modname)) try: # load_module will force reload the module if it's # already loaded, so check for that module = sys.modules.get(modname) if module is None: module = loader.find_module(modname).load_module(modname) if hasattr(module, 'register_plugin') and \ hasattr(module.register_plugin, '__call__'): plugin_class = module.register_plugin() if plugin_class != None: self.register_plugin(plugin_name, plugin_class, module) else: if config.debug("plugins"): print_warning( "'register_plugin' function at %s: %s did not return a class." % (path, modname)) else: if config.debug("plugins"): print_warning( "no 'register_plugin' function at %s: %s" % (path, modname)) # delete from sys.modules? except Exception as e: nameish = modname.split('.')[-1] self.failed_plugins[nameish] = str(e) if config.debug("plugins"): import traceback from StringIO import StringIO out = StringIO() traceback.print_exc(file=out) print_debug(out.getvalue()) # load config data, _ = _load_config_from_filepaths([os.path.join(path, "rezconfig")]) deep_update(self.config_data, data)
def command(opts, parser, extra_arg_groups=None): from rez.config import config from rez.utils.platform_ import platform_ from rez.exceptions import RezSystemError from rez.vendor import yaml from rez.vendor.yaml.error import YAMLError from rez.utils import py23 import os.path # we don't usually want warnings printed in a wrapped tool. But in cases # where we do (for debugging) we leave a backdoor - setting $REZ_QUIET=0 # will stop this warning suppression. if "REZ_QUIET" not in os.environ: config.override("quiet", True) yaml_file = os.path.abspath(opts.YAML) cli_args = opts.ARG for arg_group in (extra_arg_groups or []): cli_args.extend(arg_group) if platform_.name == "windows" and yaml_file.lower().endswith(".cmd"): with open(yaml_file) as f: content = "\n".join(f.readlines()[4:]) # strip batch script else: with open(yaml_file) as f: content = f.read() try: doc = yaml.load(content, Loader=yaml.FullLoader) except YAMLError as e: raise RezSystemError("Invalid executable file %s: %s" % (yaml_file, str(e))) func_name = doc["func_name"] nargs = doc.get("nargs", []) kwargs = doc.get("kwargs", {}) if isinstance(doc["module"], basestring): # refers to a rez module from rez.backport.importlib import import_module namespace = "rez.%s" % doc["module"] module = import_module(namespace) else: # refers to a rez plugin module from rez.plugin_managers import plugin_manager plugin_type, plugin_name = doc["module"] module = plugin_manager.get_plugin_module(plugin_type, plugin_name) target_func = getattr(module, func_name) func_args = py23.get_function_arg_names(target_func) if "_script" in func_args: kwargs["_script"] = yaml_file if "_cli_args" in func_args: kwargs["_cli_args"] = cli_args target_func(*nargs, **kwargs)
def command(opts, parser, extra_arg_groups=None): from rez.config import config from rez.exceptions import RezSystemError from rez.vendor import yaml from rez.vendor.yaml.error import YAMLError import inspect import os.path # we don't usually want warnings printed in a wrapped tool. But in cases # where we do (for debugging) we leave a backdoor - setting $REZ_QUIET=0 # will stop this warning suppression. if "REZ_QUIET" not in os.environ: config.override("quiet", True) yaml_file = os.path.abspath(opts.YAML) cli_args = opts.ARG for arg_group in (extra_arg_groups or []): cli_args.append("--") cli_args.extend(arg_group) with open(yaml_file) as f: content = f.read() try: doc = yaml.load(content) except YAMLError as e: raise RezSystemError("Invalid executable file %s: %s" % (yaml_file, str(e))) func_name = doc["func_name"] nargs = doc.get("nargs", []) kwargs = doc.get("kwargs", {}) if isinstance(doc["module"], basestring): # refers to a rez module from rez.backport.importlib import import_module namespace = "rez.%s" % doc["module"] module = import_module(namespace) else: # refers to a rez plugin module from rez.plugin_managers import plugin_manager plugin_type, plugin_name = doc["module"] module = plugin_manager.get_plugin_module(plugin_type, plugin_name) target_func = getattr(module, func_name) func_args = inspect.getargspec(target_func).args if "_script" in func_args: kwargs["_script"] = yaml_file if "_cli_args" in func_args: kwargs["_cli_args"] = cli_args target_func(*nargs, **kwargs)
def command(opts, parser, extra_arg_groups=None): from rez.cli._util import subcommands import os import re # get comp info from environment variables comp_line = os.getenv("COMP_LINE", "") comp_point = os.getenv("COMP_POINT", "") try: comp_point = int(comp_point) except: comp_point = len(comp_line) last_word = comp_line.split()[-1] if comp_line.endswith(last_word): prefix = last_word else: prefix = None def _pop_arg(l, p): words = l.split() arg = None if words: arg = words[0] l_ = l.lstrip() p -= (len(l) - len(l_) + len(arg)) l = l_[len(arg):] return l, p, arg return l, p, arg # determine subcommand, possibly give subcommand completion subcommand = None comp_line, comp_point, cmd = _pop_arg(comp_line, comp_point) if cmd in ("rez", "rezolve"): comp_line, comp_point, arg = _pop_arg(comp_line, comp_point) if arg: if prefix != arg: subcommand = arg else: subcommand = cmd.split("-", 1)[-1] if subcommand is None: cmds = [k for k, v in subcommands.items() if not v.get("hidden")] if prefix: cmds = (x for x in cmds if x.startswith(prefix)) print(" ".join(cmds)) if subcommand not in subcommands: return # replace '--' with special '--N#' flag so that subcommands can specify # custom completions. regex = re.compile("\s--\s") ddashes = regex.findall(comp_line) for i, ddash in enumerate(ddashes): j = comp_line.find(ddash) while comp_line[j] != "-": j += 1 j += 2 s = "N%d" % i comp_line = comp_line[:j] + s + comp_line[j:] if comp_point >= j: comp_point += len(s) # create parser for subcommand from rez.backport.importlib import import_module module_name = "rez.cli.%s" % subcommand mod = import_module(module_name) parser = argparse.ArgumentParser() mod.setup_parser(parser, completions=True) # have to massage input a little so argcomplete behaves cmd = "rez-%s" % subcommand comp_line = cmd + comp_line comp_point += len(cmd) # generate the completions from rez.cli._complete_util import RezCompletionFinder completer = RezCompletionFinder(parser=parser, comp_line=comp_line, comp_point=comp_point) words = completer.completions words = [w.decode() if hasattr(w, 'decode') else w for w in words] print(' '.join(words))
def load_plugins(self): import pkgutil from rez.backport.importlib import import_module type_module_name = 'rezplugins.' + self.type_name package = import_module(type_module_name) # on import, the `__path__` variable of the imported package is extended # to include existing directories on the plugin search path (via # extend_path, above). this means that `walk_packages` will walk over all # modules on the search path at the same level (.e.g in a # 'rezplugins/type_name' sub-directory). paths = [package.__path__] if isinstance(package.__path__, basestring) \ else package.__path__ # reverse plugin path order, so that custom plugins have a chance to # be found before the builtin plugins (from /rezplugins). paths = reversed(paths) for path in paths: if config.debug("plugins"): print_debug("searching plugin path %s...", path) for importer, modname, ispkg in pkgutil.iter_modules( [path], package.__name__ + '.'): if importer is None: continue plugin_name = modname.split('.')[-1] if plugin_name.startswith('_') or plugin_name == 'rezconfig': continue if plugin_name in self.plugin_modules: # same named plugins will have identical module name, # which will just reuse previous imported module from # `sys.modules` below. skipping the rest of the process # for good. if config.debug("plugins"): print_warning( "skipped same named %s plugin at %s: %s" % (self.type_name, path, modname)) continue if config.debug("plugins"): print_debug("loading %s plugin at %s: %s..." % (self.type_name, path, modname)) try: # nerdvegas/rez#218 # load_module will force reload the module if it's # already loaded, so check for that plugin_module = sys.modules.get(modname) if plugin_module is None: loader = importer.find_module(modname) plugin_module = loader.load_module(modname) elif os.path.dirname(plugin_module.__file__) != path: if config.debug("plugins"): # this should not happen but if it does, tell why. print_warning( "plugin module %s is not loaded from current " "load path but reused from previous imported " "path: %s" % (modname, plugin_module.__file__)) if (hasattr(plugin_module, "register_plugin") and callable(plugin_module.register_plugin)): plugin_class = plugin_module.register_plugin() if plugin_class is not None: self.register_plugin(plugin_name, plugin_class, plugin_module) else: if config.debug("plugins"): print_warning( "'register_plugin' function at %s: %s did " "not return a class." % (path, modname)) else: if config.debug("plugins"): print_warning( "no 'register_plugin' function at %s: %s" % (path, modname)) # delete from sys.modules? except Exception as e: nameish = modname.split('.')[-1] self.failed_plugins[nameish] = str(e) if config.debug("plugins"): import traceback from rez.vendor.six.six import StringIO out = StringIO() traceback.print_exc(file=out) print_debug(out.getvalue()) # load config data, _ = _load_config_from_filepaths( [os.path.join(path, "rezconfig")]) deep_update(self.config_data, data)
def command(opts, parser, extra_arg_groups=None): from rez.cli._util import subcommands import os import re # get comp info from environment variables comp_line = os.getenv("COMP_LINE", "") comp_point = os.getenv("COMP_POINT", "") try: comp_point = int(comp_point) except: comp_point = len(comp_line) last_word = comp_line.split()[-1] if comp_line.endswith(last_word): prefix = last_word else: prefix = None def _pop_arg(l, p): words = l.split() arg = None if words: arg = words[0] l_ = l.lstrip() p -= (len(l) - len(l_) + len(arg)) l = l_[len(arg):] return l, p, arg return l, p, arg # determine subcommand, possibly give subcommand completion subcommand = None comp_line, comp_point, cmd = _pop_arg(comp_line, comp_point) if cmd in ("rez", "rezolve"): comp_line, comp_point, arg = _pop_arg(comp_line, comp_point) if arg: if prefix != arg: subcommand = arg else: subcommand = cmd.split("-", 1)[-1] if subcommand is None: cmds = [k for k, v in subcommands.iteritems() if not v.get("hidden")] if prefix: cmds = (x for x in cmds if x.startswith(prefix)) print " ".join(cmds) if subcommand not in subcommands: return # replace '--' with special '--N#' flag so that subcommands can specify # custom completions. regex = re.compile("\s--\s") ddashes = regex.findall(comp_line) for i, ddash in enumerate(ddashes): j = comp_line.find(ddash) while comp_line[j] != "-": j += 1 j += 2 s = "N%d" % i comp_line = comp_line[:j] + s + comp_line[j:] if comp_point >= j: comp_point += len(s) # create parser for subcommand from rez.backport.importlib import import_module module_name = "rez.cli.%s" % subcommand mod = import_module(module_name) parser = argparse.ArgumentParser() mod.setup_parser(parser, completions=True) # have to massage input a little so argcomplete behaves cmd = "rez-%s" % subcommand comp_line = cmd + comp_line comp_point += len(cmd) # generate the completions from rez.cli._complete_util import RezCompletionFinder completer = RezCompletionFinder(parser=parser, comp_line=comp_line, comp_point=comp_point) words = completer.completions print ' '.join(words)