Coverage for mfplugin/cli_tools/plugins_install.py: 0%
89 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 io
5import contextlib
6import argparse
7import sys
8from mfplugin.utils import inside_a_plugin_env
9from mfplugin.manager import PluginsManager
10from mfplugin.file import PluginFile
11from mfplugin.utils import BadPluginFile, AlreadyInstalledPlugin, \
12 validate_plugin_name, BadPluginName, NotInstalledPlugin
13from mfutil.cli import echo_running, echo_nok, echo_ok, echo_bold, echo_warning
15DESCRIPTION = "install a plugin file"
16MFMODULE_LOWERCASE = os.environ.get('MFMODULE_LOWERCASE', 'mfext')
19def main():
20 arg_parser = argparse.ArgumentParser(description=DESCRIPTION)
21 arg_parser.add_argument("plugin_filepath", type=str,
22 help="plugin filepath")
23 arg_parser.add_argument("--plugins-base-dir", type=str, default=None,
24 help="can be use to set an alternate "
25 "plugins-base-dir, if not set the value of "
26 "MFMODULE_PLUGINS_BASE_DIR env var is used (or a "
27 "hardcoded standard value).")
28 arg_parser.add_argument("--force", action="store_true",
29 help="if set, automatically uninstall old plugin"
30 "with the same name (if already installed)")
31 arg_parser.add_argument("--new-name", type=str, default=None,
32 help="install the plugin but with a new name "
33 "given by this parameter")
34 args = arg_parser.parse_args()
35 if inside_a_plugin_env():
36 print("ERROR: Don't use plugins.install/uninstall inside a plugin_env")
37 sys.exit(1)
38 if args.new_name is not None:
39 try:
40 validate_plugin_name(args.new_name)
41 except BadPluginName as e:
42 echo_bold("ERROR: bad plugin name for --new-name option")
43 echo_bold(str(e))
44 sys.exit(3)
45 manager = PluginsManager(plugins_base_dir=args.plugins_base_dir)
46 echo_running("- Checking plugin file...")
47 try:
48 pf = PluginFile(args.plugin_filepath)
49 pf.load()
50 except BadPluginFile:
51 echo_nok()
52 sys.exit(1)
53 echo_ok()
54 name = pf.name
55 new_name = args.new_name if args.new_name else name
56 try:
57 manager.get_plugin(new_name)
58 if args.force:
59 try:
60 echo_running("- Uninstalling (old) plugin %s..." % new_name)
61 with contextlib.redirect_stdout(open(os.devnull, "w")):
62 with contextlib.redirect_stderr(open(os.devnull, "w")):
63 manager.uninstall_plugin(new_name)
64 echo_ok()
65 except Exception:
66 echo_nok()
67 echo_bold("=> try uninstalling with plugins.uninstall for "
68 "more details")
69 sys.exit(1)
70 except NotInstalledPlugin:
71 pass
73 if args.new_name is not None:
74 echo_running("- Installing plugin %s as %s..." % (name, args.new_name))
75 else:
76 echo_running("- Installing plugin %s..." % name)
77 try:
78 f = io.StringIO()
79 with contextlib.redirect_stderr(f):
80 manager.install_plugin(args.plugin_filepath,
81 new_name=args.new_name)
82 except AlreadyInstalledPlugin:
83 echo_nok("already installed")
84 sys.exit(1)
85 except Exception as e:
86 echo_nok()
87 stderr = f.getvalue()
88 if stderr != '':
89 print(stderr)
90 echo_bold(str(e))
91 sys.exit(2)
92 stderr = f.getvalue()
93 if stderr != '':
94 echo_warning()
95 if "pip's dependency resolver does not currently take into account" \
96 in stderr:
97 print(stderr.replace("ERROR", "WARNING"))
98 print("The above message is only a WARNING, don't panic !")
99 print("Your plugin should work anyway")
100 print("To get rid of it, maybe you should remove from your")
101 print(" plugin the optional layers (those starting by")
102 print(" '-' in .layerapi2_dependencies or ask for help from")
103 print(" a Metwork specialist")
104 else:
105 print(stderr)
106 else:
107 echo_ok()
108 p = manager.get_plugin(args.new_name if args.new_name is not None
109 else name)
110 p.print_dangerous_state()
113if __name__ == '__main__':
114 main()