Coverage for mfplugin/cli_tools/plugins_list.py: 0%
44 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-13 15:57 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-13 15:57 +0000
1#!/usr/bin/env python3
3import os
4import argparse
5import sys
6import json
7from mfplugin.manager import PluginsManager
8from terminaltables import DoubleTable
9from mflog import get_logger
11DESCRIPTION = "get the installed plugins list"
12MFMODULE_LOWERCASE = os.environ.get('MFMODULE_LOWERCASE', 'generic')
13LOGGER = get_logger("mfplugin/plugins_list")
16def main():
17 arg_parser = argparse.ArgumentParser(description=DESCRIPTION)
18 arg_parser.add_argument("--raw", action="store_true", help="raw mode")
19 arg_parser.add_argument("--json", action="store_true", help="json mode "
20 "(not compatible with raw mode)")
21 arg_parser.add_argument("--plugins-base-dir", type=str, default=None,
22 help="can be use to set an alternate "
23 "plugins-base-dir, if not set the value of "
24 "MFMODULE_PLUGINS_BASE_DIR env var is used (or a "
25 "hardcoded standard value).")
26 args = arg_parser.parse_args()
27 if args.json and args.raw:
28 print("ERROR: json and raw options are mutually exclusives")
29 sys.exit(1)
30 manager = PluginsManager(plugins_base_dir=args.plugins_base_dir)
31 plugins = manager.plugins.values()
32 json_output = []
33 table_data = []
34 table_data.append(["Name", "Version", "Release", "Home"])
35 for plugin in plugins:
36 try:
37 release = plugin.release
38 version = plugin.version
39 except Exception as e:
40 LOGGER.warning("Bad plugin: (%s, %s) with exception: %s " %
41 (plugin.name, plugin.home, e))
42 release = "error"
43 version = "error"
44 if args.raw:
45 print("%s~~~%s~~~%s~~~%s" % (plugin.name, version, release,
46 plugin.home))
47 elif args.json:
48 json_output.append({
49 "name": plugin.name,
50 "release": release,
51 "version": version,
52 "home": plugin.home
53 })
54 else:
55 table_data.append([plugin.name, version, release, plugin.home])
56 if not args.raw and not args.json:
57 t = DoubleTable(title="Installed plugins (%i)" % len(plugins),
58 table_data=table_data)
59 print(t.table)
60 elif args.json:
61 print(json.dumps(json_output, indent=4))
64if __name__ == '__main__':
65 main()