def main(): arg_parser = argparse.ArgumentParser(description=DESCRIPTION) arg_parser.add_argument("PLUGIN_PATH", default=".", help="plugin directory path to check") arg_parser.add_argument("--debug", action="store_true", help="add some debug informations in " "case of problems") arg_parser.add_argument("--plugins-base-dir", type=str, default=None, help="can be use to set an alternate " "plugins-base-dir, if not set the value of " "MFMODULE_PLUGINS_BASE_DIR env var is used (or a " "hardcoded standard value).") args = arg_parser.parse_args() echo_running("- Checking plugin...") manager = PluginsManager(args.plugins_base_dir) try: plugin = manager.make_plugin(args.PLUGIN_PATH) plugin.load_full() if args.debug: print(get_nice_dump(plugin._get_debug())) except Exception as e: echo_nok() echo_bold(str(e)) if args.debug: print("details of the problem:") raise (e) else: print("(note: use 'plugins.check --debug /plugin/path' " "for more details)") sys.exit(1) echo_ok()
def main(): arg_parser = argparse.ArgumentParser(description=DESCRIPTION) arg_parser.add_argument("--plugin-path", default=".", help="plugin directory path") arg_parser.add_argument("--debug", action="store_true", help="add some debug informations in " "case of problems") arg_parser.add_argument("--show-plugin-path", action="store_true", default=False, help="show the generated plugin path") args = arg_parser.parse_args() echo_running("- Building plugin...") manager = PluginsManager() try: plugin = manager.make_plugin(args.plugin_path) path = plugin.build() except Exception as e: echo_nok() print(e) if args.debug: print("details of the problem:") raise(e) else: print("note: use --debug option for more details") else: echo_ok() if args.show_plugin_path: echo_bold("plugin file is ready at %s" % path)
def test_develop_plugin(): x = PluginsManager(plugins_base_dir=BASE) home = os.path.join(CURRENT_DIR, "data", "plugin1") x.develop_plugin(home) assert len(x.plugins) == 1 assert x.plugins["plugin1"].name == "plugin1" assert x.plugins["plugin1"].version == "dev_link" assert x.plugins["plugin1"].is_dev_linked
def get_installed_plugins(plugins_base_dir=None): """Get a detailled list of installed plugins. This is just a compat helper (as the real implementatin is in manager class). Args: plugins_base_dir (string): (optional) the plugin base directory path. If not set, the default plugins base directory path is used. Returns: (string): dict of installed plugins with following keys: name, version, release, home. Raises: FIXME. """ manager = PluginsManager(plugins_base_dir) res = [] for plugin in manager.plugins.values(): if not plugin.is_installed: continue tmp = { "name": plugin.name, "version": plugin.version, "release": plugin.release, "home": plugin.home } res.append(tmp) return res
def test_default_values(): x = PluginsManager(plugins_base_dir=BASE) _install_two_plugin(x) x.plugins["plugin1"].load_full() import json print(json.dumps(x.plugins["plugin1"].configuration._doc, indent=4)) assert x.plugins["plugin1"].configuration.add_plugin_dir_to_python_path
def main(): arg_parser = argparse.ArgumentParser(description=DESCRIPTION) arg_parser.add_argument("name", type=str, help="plugin name") arg_parser.add_argument("--plugins-base-dir", type=str, default=None, help="can be use to set an alternate " "plugins-base-dir, if not set the value of " "MFMODULE_PLUGINS_BASE_DIR env var is used (or a " "hardcoded standard value).") arg_parser.add_argument("--debug", action="store_true", help="add some debug informations in " "case of problems") args = arg_parser.parse_args() name = args.name if inside_a_plugin_env(): print("ERROR: Don't use plugins.install/uninstall inside a plugin_env") sys.exit(1) manager = PluginsManager(plugins_base_dir=args.plugins_base_dir) echo_running("- Repackaging plugin %s..." % name) try: f = io.StringIO() with contextlib.redirect_stderr(f): path = manager.repackage_plugin(name) except NotInstalledPlugin: echo_nok() echo_bold(" => not installed plugin") sys.exit(1) except Exception as e: echo_nok() stderr = f.getvalue() if stderr != '': print(stderr) print(e) if args.debug: print("details of the problem:") raise (e) else: print("note: use --debug option for more details") else: echo_ok() stderr = f.getvalue() if stderr != '': print(stderr) echo_bold("plugin file is ready at %s" % path)
def main(): arg_parser = argparse.ArgumentParser(description=DESCRIPTION) arg_parser.add_argument("name", type=str, help="plugin name") arg_parser.add_argument( "--clean", action="store_true", help="if set, we drop any configuration override " "under ${MODULE_HOME}/config/plugins/ for this " "plugin (warning: delete nothing under /etc/metwork.config.d/" f"{MFMODULE_LOWERCASE}/plugins/)") arg_parser.add_argument("--plugins-base-dir", type=str, default=None, help="can be use to set an alternate " "plugins-base-dir, if not set the value of " "MFMODULE_PLUGINS_BASE_DIR env var is used (or a " "hardcoded standard value).") args = arg_parser.parse_args() name = args.name if inside_a_plugin_env(): print("ERROR: Don't use plugins.install/uninstall inside a plugin_env") sys.exit(1) manager = PluginsManager(plugins_base_dir=args.plugins_base_dir) echo_running("- Uninstalling plugin %s..." % name) try: out = io.StringIO() err = io.StringIO() with contextlib.redirect_stdout(out): with contextlib.redirect_stderr(err): manager.uninstall_plugin(name) except NotInstalledPlugin: echo_nok("not installed") sys.exit(1) except Exception as e: echo_nok() print(err.getvalue(), file=sys.stderr) print(out.getvalue()) print(e) sys.exit(2) finally: try: os.unlink(f"{MFMODULE_RUNTIME_HOME}/config/plugins/{name}.ini") except Exception: pass echo_ok()
def test_plugin_env_context(): x = PluginsManager(plugins_base_dir=BASE) _install_two_plugin(x) assert "GENERIC_CURRENT_PLUGIN_CUSTOM_FOO" not in os.environ assert "GENERIC_CURRENT_PLUGIN_NAME" not in os.environ old = os.environ.get("MFCONFIG", None) os.environ["MFCONFIG"] = "SPECIFIC" with x.plugin_env_context("plugin1"): assert os.environ["MFCONFIG"] == "SPECIFIC" assert os.environ["GENERIC_CURRENT_PLUGIN_CUSTOM_FOO"] == "bar" assert os.environ["GENERIC_CURRENT_PLUGIN_CUSTOM_FOO2"] == "bar2" assert os.environ["GENERIC_CURRENT_PLUGIN_NAME"] == "plugin1" if old is None: del (os.environ["MFCONFIG"]) else: os.environ["MFCONFIG"] = old assert "GENERIC_CURRENT_PLUGIN_CUSTOM_FOO" not in os.environ assert "GENERIC_CURRENT_PLUGIN_NAME" not in os.environ
def test_install_plugin(): x = PluginsManager(plugins_base_dir=BASE) _install_two_plugin(x) x.plugins["plugin1"].load_full() assert int(x.plugins["plugin1"].size) > 0 assert len(x.plugins["plugin1"].build_date) > 8 assert len(x.plugins["plugin1"].build_host) > 0 assert x.plugins["plugin1"].format_version == [1, 0, 0] assert len(x.plugins["plugin1"].get_hash()) > 0 assert x.plugins["plugin1"].get_hash != x.plugins["plugin2"].get_hash()
def test_cache(): x = PluginsManager(plugins_base_dir=BASE) _install_two_plugin(x) x.plugins["plugin1"].get_configuration_hash() f = x.plugins["plugin1"].get_plugin_env_dict(cache=True) assert os.path.isfile("%s/.configuration_cache" % x.plugins["plugin1"].home) assert "%s_CURRENT_PLUGIN_CACHE" % MFMODULE not in f g = x.plugins["plugin1"].get_plugin_env_dict(cache=True) assert "%s_CURRENT_PLUGIN_CACHE" % MFMODULE in g del g["%s_CURRENT_PLUGIN_CACHE" % MFMODULE] assert len(g) == len(f)
def main(): arg_parser = argparse.ArgumentParser(description=DESCRIPTION) arg_parser.add_argument("--plugin-path", default=".", help="plugin directory path") arg_parser.add_argument("--ignore-already-installed", action="store_true", help="ignore already installed plugin " "(in dev mode)") arg_parser.add_argument("name", help="plugin name") args = arg_parser.parse_args() manager = PluginsManager() echo_running("- Devlinking plugin %s..." % args.name) try: manager.develop_plugin(args.plugin_path) except AlreadyInstalledPlugin: if args.ignore_already_installed: p = manager.make_plugin(args.plugin_path) if p.is_installed and p.is_dev_linked: echo_warning("(already installed)") sys.exit(0) echo_nok() echo_bold("ERROR: the plugin is already installed") sys.exit(3) except Exception as e: echo_nok() echo_bold(str(e)) sys.exit(2) echo_ok() p = manager.get_plugin(args.name) p.print_dangerous_state()
def test_compat(): x = PluginsManager(plugins_base_dir=BASE) _install_two_plugin(x) tmp = get_installed_plugins(plugins_base_dir=BASE) assert len(tmp) == 2 p1 = [x for x in tmp if x["name"] == "plugin1"][0] assert p1["name"] == "plugin1" assert p1["version"] == "1.2.3" info = get_plugin_info("plugin1", mode="name", plugins_base_dir=BASE) assert info["metadatas"]["name"] == "plugin1" assert info["metadatas"]["version"] == "1.2.3" assert "build_host" in info["metadatas"] assert len(info["files"]) > 0
def main(): arg_parser = argparse.ArgumentParser(description=DESCRIPTION) arg_parser.add_argument("--raw", action="store_true", help="raw mode") arg_parser.add_argument("--json", action="store_true", help="json mode " "(not compatible with raw mode)") arg_parser.add_argument("--plugins-base-dir", type=str, default=None, help="can be use to set an alternate " "plugins-base-dir, if not set the value of " "MFMODULE_PLUGINS_BASE_DIR env var is used (or a " "hardcoded standard value).") args = arg_parser.parse_args() if args.json and args.raw: print("ERROR: json and raw options are mutually exclusives") sys.exit(1) manager = PluginsManager(plugins_base_dir=args.plugins_base_dir) plugins = manager.plugins.values() json_output = [] table_data = [] table_data.append(["Name", "Version", "Release", "Home"]) for plugin in plugins: try: release = plugin.release version = plugin.version except Exception as e: LOGGER.warning("Bad plugin: (%s, %s) with exception: %s " % (plugin.name, plugin.home, e)) release = "error" version = "error" if args.raw: print("%s~~~%s~~~%s~~~%s" % (plugin.name, version, release, plugin.home)) elif args.json: json_output.append({ "name": plugin.name, "release": release, "version": version, "home": plugin.home }) else: table_data.append([plugin.name, version, release, plugin.home]) if not args.raw and not args.json: t = SingleTable(title="Installed plugins (%i)" % len(plugins), table_data=table_data) print(t.table) elif args.json: print(json.dumps(json_output, indent=4))
def get_plugin_info(name_or_filepath, mode="auto", plugins_base_dir=None): """Get detailed information about a plugin. This is just a compat helper (as the real implementatin is in manager class). Args: name_or_filepath (string): name or file path of the plugin. mode (string) - "name": get information from the plugin name (name_or_filepath is the name of the plugin). - "file": get information from the plutgin file (name_or_filepath is the plugin file path). - "auto": guess if the name_or_filepath parameter is the name or the file path of the plugin. plugins_base_dir (string): (optional) the plugin base directory path. If not set, the default plugins base directory path is used. Returns: (dict): dictionary containing plugin information (or None if the plugin is not installed (name mode). Raises: NotInstalledPlugin: is the plugin is not installed (if it is a "name"). """ res = {} if mode == "auto": mode = "name" if '/' in name_or_filepath or '.' in name_or_filepath: mode = "file" else: if os.path.isfile(name_or_filepath): mode = "file" if mode == "file": plugin = PluginFile(name_or_filepath) elif mode == "name": manager = PluginsManager(plugins_base_dir) try: plugin = manager.plugins[name_or_filepath] except KeyError: return None else: raise Exception("unknown mode: %s" % mode) res = { "files": plugin.files, "home": plugin.home } res['metadatas'] = { "name": plugin.name, "release": plugin.release, "version": plugin.version, "size": plugin.size, "build_host": plugin.build_host, "build_date": plugin.build_date } if mode == "file": res['metadatas'].update({ "license": plugin.license, "packager": plugin.packager, "vendor": plugin.vendor, "url": plugin.url, "summary": plugin.summary }) else: res['metadatas'].update({ "license": plugin.configuration.license, "packager": plugin.configuration.packager, "vendor": plugin.configuration.vendor, "url": plugin.configuration.url, "summary": plugin.configuration.summary }) return res
def test_get_plugin_env_dict(): x = PluginsManager(plugins_base_dir=BASE) _install_two_plugin(x) e = x.plugins["plugin1"].get_plugin_env_dict() assert e["GENERIC_CURRENT_PLUGIN_NAME"] == "plugin1" assert e["GENERIC_CURRENT_PLUGIN_CUSTOM_FOO"] == "bar"
def test_uninstall_plugin(): x = PluginsManager(plugins_base_dir=BASE) _install_two_plugin(x) x.uninstall_plugin("plugin1") assert len(x.plugins) == 1 assert list(x.plugins.keys())[0] == "plugin2"
def test_manager1(): x = PluginsManager(plugins_base_dir=BASE) assert list(x.plugins.items()) == []
#!/usr/bin/env python import os import glob import sys import logging from mfutil.cli import echo_running, echo_ok, echo_nok, echo_bold from mfplugin.compat import get_plugin_hash, get_plugin_info from mfplugin.manager import PluginsManager MFMODULE = os.environ['MFMODULE'] MFMODULE_RUNTIME_HOME = os.environ['MFMODULE_RUNTIME_HOME'] MFMODULE_HOME = os.environ['MFMODULE_HOME'] EXTERNAL_PLUGINS_PATH = \ "/etc/metwork.config.d/%s/plugins" % MFMODULE.lower() plugins_manager = PluginsManager() def i_plugin(typ, name, fil): echo_running("- Installing %s plugin: %s..." % (typ, name)) try: os.unlink("%s/config/plugins/%s.ini" % (MFMODULE_RUNTIME_HOME, name)) except Exception: pass try: plugins_manager.install_plugin(fil) except Exception as e: echo_nok() echo_bold("ERROR: %s" % e) else: echo_ok()
def test_manage_with_empty_base(): PluginsManager(plugins_base_dir=None)
if len(tmp) == 0: continue first = tmp[0] if first in ('[', '#'): continue return True return False if __name__ == '__main__': parser = argparse.ArgumentParser(description=DESCRIPTION) parser.add_argument("PLUGIN_NAME", help="plugin name") args = parser.parse_args() manager = PluginsManager() try: plugin = manager.get_plugin(args.PLUGIN_NAME) except NotInstalledPlugin: echo_bold("ERROR: not installed plugin: " % args.PLUGIN_NAME) sys.exit(1) if plugin.is_dev_linked: sys.exit(0) target = "%s/config/plugins/%s.ini" % (MFMODULE_RUNTIME_HOME, args.PLUGIN_NAME) if os.path.exists(target): if not has_an_interesting_line(target): # There is a configuration file but nothing has been changed
def main(): arg_parser = argparse.ArgumentParser(description=DESCRIPTION) arg_parser.add_argument("plugin_filepath", type=str, help="plugin filepath") arg_parser.add_argument("--plugins-base-dir", type=str, default=None, help="can be use to set an alternate " "plugins-base-dir, if not set the value of " "MFMODULE_PLUGINS_BASE_DIR env var is used (or a " "hardcoded standard value).") arg_parser.add_argument("--force", action="store_true", help="if set, automatically uninstall old plugin" "with the same name (if already installed)") arg_parser.add_argument("--new-name", type=str, default=None, help="install the plugin but with a new name " "given by this parameter") args = arg_parser.parse_args() if inside_a_plugin_env(): print("ERROR: Don't use plugins.install/uninstall inside a plugin_env") sys.exit(1) if args.new_name is not None: try: validate_plugin_name(args.new_name) except BadPluginName as e: echo_bold("ERROR: bad plugin name for --new-name option") echo_bold(str(e)) sys.exit(3) manager = PluginsManager(plugins_base_dir=args.plugins_base_dir) echo_running("- Checking plugin file...") try: pf = PluginFile(args.plugin_filepath) pf.load() except BadPluginFile: echo_nok() sys.exit(1) echo_ok() name = pf.name new_name = args.new_name if args.new_name else name try: manager.get_plugin(new_name) if args.force: try: echo_running("- Uninstalling (old) plugin %s..." % new_name) with contextlib.redirect_stdout(open(os.devnull, "w")): with contextlib.redirect_stderr(open(os.devnull, "w")): manager.uninstall_plugin(new_name) echo_ok() except Exception: echo_nok() echo_bold("=> try uninstalling with plugins.uninstall for " "more details") sys.exit(1) except NotInstalledPlugin: pass if args.new_name is not None: echo_running("- Installing plugin %s as %s..." % (name, args.new_name)) else: echo_running("- Installing plugin %s..." % name) try: f = io.StringIO() with contextlib.redirect_stderr(f): manager.install_plugin(args.plugin_filepath, new_name=args.new_name) except AlreadyInstalledPlugin: echo_nok("already installed") sys.exit(1) except Exception as e: echo_nok() stderr = f.getvalue() if stderr != '': print(stderr) echo_bold(str(e)) sys.exit(2) stderr = f.getvalue() if stderr != '': echo_warning() print(stderr) else: echo_ok() p = manager.get_plugin( args.new_name if args.new_name is not None else name) p.print_dangerous_state()