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

83 statements  

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

1#!/usr/bin/env python3 

2 

3import lazy_import 

4import os 

5import argparse 

6from mfplugin.compat import PluginsManager 

7from mfplugin.utils import NotInstalledPlugin 

8 

9sys = lazy_import.lazy_module("sys") 

10shlex = lazy_import.lazy_module("shlex") 

11 

12DESCRIPTION = "execute a command in a plugin environment" 

13 

14MFMODULE = os.environ.get("MFMODULE", "GENERIC") 

15MFMODULE_HOME = os.environ.get("MFMODULE_HOME", "/tmp") 

16MFMODULE_RUNTIME_HOME = os.environ.get("MFMODULE_RUNTIME_HOME", "/tmp") 

17MFMODULE_LOWERCASE = os.environ.get("MFMODULE_LOWERCASE", "generic") 

18MFMODULE_PLUGINS_BASE_DIR = os.environ.get('MFMODULE_PLUGINS_BASE_DIR', None) 

19LAYERAPI2_LAYERS_PATH = os.environ.get('LAYERAPI2_LAYERS_PATH', '') 

20 

21 

22def _prepend(original, new): 

23 return ":".join([new] + [x for x in original.split(':') if x != new]) 

24 

25 

26def get_new_layerapi2_layers_path(plugin_home, add_plugin_home=False): 

27 # prepend plugin_home in LAYERAPI2_LAYERS_PATH 

28 # can be usefull if the plugin is not already installed 

29 res = LAYERAPI2_LAYERS_PATH 

30 if MFMODULE_PLUGINS_BASE_DIR: 

31 # we are probably during a hotswap 

32 # Let's prepend this repository in LAYERAPI2_LAYERS_PATH 

33 res = _prepend(res, MFMODULE_PLUGINS_BASE_DIR) 

34 if add_plugin_home: 

35 res = _prepend(res, plugin_home) 

36 return res 

37 

38 

39def main(): 

40 parser = argparse.ArgumentParser(description=DESCRIPTION) 

41 parser.add_argument("--cwd", 

42 action="store_true", 

43 help="change working directory to plugin home") 

44 parser.add_argument("--empty", 

45 action="store_true", 

46 help="unload all layers before") 

47 parser.add_argument("--bash-cmds", action="store_true", 

48 help="if set don't execute command but output bash " 

49 "cmds to be execute in a fresh empty shell " 

50 "(--empty and COMMAND_AND_ARGS ignored)") 

51 parser.add_argument("--plugins-base-dir", type=str, default=None, 

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

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

54 "MFMODULE_PLUGINS_BASE_DIR env var is used (or a " 

55 "hardcoded standard value).") 

56 parser.add_argument("--ignore-cache", 

57 action="store_true", 

58 help="if set, don't use env cache") 

59 parser.add_argument("PLUGIN_NAME_OR_PLUGIN_HOME", type=str, 

60 help="plugin name or plugin home (if starting by /)") 

61 parser.add_argument("COMMAND_AND_ARGS", 

62 help="command (and args) to execute") 

63 args, command_args = parser.parse_known_args() 

64 cache = not args.ignore_cache 

65 

66 if args.plugins_base_dir is not None: 

67 plugins_base_dir = args.plugins_base_dir 

68 else: 

69 plugins_base_dir = None 

70 

71 manager = PluginsManager(plugins_base_dir) 

72 if '/' in args.PLUGIN_NAME_OR_PLUGIN_HOME: 

73 p = manager.make_plugin(args.PLUGIN_NAME_OR_PLUGIN_HOME) 

74 mode = "file" 

75 else: 

76 mode = "name" 

77 try: 

78 p = manager.get_plugin(args.PLUGIN_NAME_OR_PLUGIN_HOME) 

79 except NotInstalledPlugin: 

80 print("ERROR: the plugin %s does not seem to be " 

81 "installed/available" % args.PLUGIN_NAME_OR_PLUGIN_HOME, 

82 file=sys.stderr) 

83 sys.exit(1) 

84 

85 if args.bash_cmds: 

86 print("source /etc/profile") 

87 print("if test -f %s/.bash_profile; then source %s/.bash_profile; fi" % 

88 (MFMODULE_RUNTIME_HOME, MFMODULE_RUNTIME_HOME)) 

89 print("source %s/share/interactive_profile" % MFMODULE_HOME) 

90 plugin_env = p.get_plugin_env_dict(cache=cache) 

91 for k, v in plugin_env.items(): 

92 if k != 'PYTHONPATH': 

93 print("export %s=%s" % (k, shlex.quote(v))) 

94 new_layerapi2_layers_path = get_new_layerapi2_layers_path( 

95 p.home, add_plugin_home=(mode == "file")) 

96 if new_layerapi2_layers_path != LAYERAPI2_LAYERS_PATH: 

97 print("export LAYERAPI2_LAYERS_PATH=%s" % 

98 new_layerapi2_layers_path) 

99 print("layer_load %s >/dev/null" % p.layerapi2_layer_name) 

100 if p.configuration.add_plugin_dir_to_python_path: 

101 old_python_path = os.environ.get("PYTHONPATH", None) 

102 if old_python_path: 

103 print("export PYTHONPATH=\"%s:${PYTHONPATH}\"" % p.home) 

104 else: 

105 print("export PYTHONPATH=\"%s\"" % p.home) 

106 if args.cwd: 

107 print("cd %s" % p.home) 

108 return 

109 

110 with p.plugin_env_context(cache=cache): 

111 new_layerapi2_layers_path = get_new_layerapi2_layers_path( 

112 p.home, add_plugin_home=(mode == "file")) 

113 if new_layerapi2_layers_path != LAYERAPI2_LAYERS_PATH: 

114 os.environ["LAYERAPI2_LAYERS_PATH"] = new_layerapi2_layers_path 

115 lw_args = ["--empty", 

116 "--layers=%s" % p.layerapi2_layer_name] 

117 if args.cwd: 

118 lw_args.append("--cwd") 

119 if args.empty: 

120 lw_args.append("--empty") 

121 lw_args.append("--") 

122 lw_args.append(args.COMMAND_AND_ARGS) 

123 for cmd_arg in command_args: 

124 lw_args.append(cmd_arg) 

125 os.execvp("layer_wrapper", lw_args) 

126 

127 

128if __name__ == "__main__": 

129 main()