Coverage for mfplugin/cli_tools/plugins_uninstall.py: 0%

43 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-13 15:57 +0000

1#!/usr/bin/env python3 

2 

3import os 

4import argparse 

5import sys 

6import io 

7import contextlib 

8import pathlib 

9from mfplugin.utils import inside_a_plugin_env 

10from mfplugin.manager import PluginsManager 

11from mfplugin.utils import NotInstalledPlugin 

12from mfutil.cli import echo_running, echo_nok, echo_ok 

13 

14DESCRIPTION = "uninstall a plugin" 

15MFMODULE_RUNTIME_HOME = os.environ.get('MFMODULE_RUNTIME_HOME', '/tmp') 

16MFMODULE_LOWERCASE = os.environ.get('MFMODULE_LOWERCASE', 'mfext') 

17 

18 

19def main(): 

20 arg_parser = argparse.ArgumentParser(description=DESCRIPTION) 

21 arg_parser.add_argument("name_or_path", type=str, 

22 help="plugin name (or path)") 

23 arg_parser.add_argument( 

24 "--clean", action="store_true", 

25 help="if set, we drop any configuration override " 

26 "under ${MODULE_HOME}/config/plugins/ for this " 

27 "plugin (warning: delete nothing under /etc/metwork.config.d/" 

28 f"{MFMODULE_LOWERCASE}/plugins/)") 

29 arg_parser.add_argument("--plugins-base-dir", type=str, default=None, 

30 help="can be use to set an alternate " 

31 "plugins-base-dir, if not set the value of " 

32 "MFMODULE_PLUGINS_BASE_DIR env var is used (or a " 

33 "hardcoded standard value).") 

34 args = arg_parser.parse_args() 

35 name = pathlib.PurePath(args.name_or_path).name 

36 if inside_a_plugin_env(): 

37 print("ERROR: Don't use plugins.install/uninstall inside a plugin_env") 

38 sys.exit(1) 

39 manager = PluginsManager(plugins_base_dir=args.plugins_base_dir) 

40 echo_running("- Uninstalling plugin %s..." % name) 

41 try: 

42 out = io.StringIO() 

43 err = io.StringIO() 

44 with contextlib.redirect_stdout(out): 

45 with contextlib.redirect_stderr(err): 

46 manager.uninstall_plugin(name) 

47 except NotInstalledPlugin: 

48 echo_nok("not installed") 

49 sys.exit(1) 

50 except Exception as e: 

51 echo_nok() 

52 print(err.getvalue(), file=sys.stderr) 

53 print(out.getvalue()) 

54 print(e) 

55 sys.exit(2) 

56 echo_ok() 

57 

58 

59if __name__ == '__main__': 

60 main()