Coverage for mfplugin/compat.py: 57%

47 statements  

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

1import os 

2import hashlib 

3from mfplugin.manager import PluginsManager 

4from mfplugin.file import PluginFile 

5 

6 

7def get_installed_plugins(plugins_base_dir=None): 

8 """Get a detailled list of installed plugins. 

9 

10 This is just a compat helper 

11 (as the real implementatin is in manager class). 

12 

13 Args: 

14 plugins_base_dir (string): (optional) the plugin base directory path. 

15 If not set, the default plugins base directory path is used. 

16 

17 Returns: 

18 (string): dict of installed plugins with following keys: name, version, 

19 release, home. 

20 

21 Raises: 

22 FIXME. 

23 

24 """ 

25 manager = PluginsManager(plugins_base_dir) 

26 res = [] 

27 for plugin in manager.plugins.values(): 

28 if not plugin.is_installed: 

29 continue 

30 tmp = { 

31 "name": plugin.name, 

32 "version": plugin.version, 

33 "release": plugin.release, 

34 "home": plugin.home 

35 } 

36 res.append(tmp) 

37 return res 

38 

39 

40def get_plugin_info(name_or_filepath, mode="auto", plugins_base_dir=None): 

41 """Get detailed information about a plugin. 

42 

43 This is just a compat helper 

44 (as the real implementatin is in manager class). 

45 

46 Args: 

47 name_or_filepath (string): name or file path of the plugin. 

48 mode (string) 

49 - "name": get information from the plugin name 

50 (name_or_filepath is the name of the plugin). 

51 - "file": get information from the plutgin file 

52 (name_or_filepath is the plugin file path). 

53 - "auto": guess if the name_or_filepath parameter is the name 

54 or the file path of the plugin. 

55 plugins_base_dir (string): (optional) the plugin base directory path. 

56 If not set, the default plugins base directory path is used. 

57 

58 Returns: 

59 (dict): dictionary containing plugin information (or None if the 

60 plugin is not installed (name mode). 

61 

62 Raises: 

63 NotInstalledPlugin: is the plugin is not installed (if it is a "name"). 

64 

65 """ 

66 res = {} 

67 if mode == "auto": 

68 mode = "name" 

69 if '/' in name_or_filepath or '.' in name_or_filepath: 

70 mode = "file" 

71 else: 

72 if os.path.isfile(name_or_filepath): 

73 mode = "file" 

74 if mode == "file": 

75 plugin = PluginFile(name_or_filepath) 

76 elif mode == "name": 

77 manager = PluginsManager(plugins_base_dir) 

78 try: 

79 plugin = manager.plugins[name_or_filepath] 

80 except KeyError: 

81 return None 

82 else: 

83 raise Exception("unknown mode: %s" % mode) 

84 res = { 

85 "files": plugin.files, 

86 "home": plugin.home 

87 } 

88 res['metadatas'] = { 

89 "name": plugin.name, 

90 "release": plugin.release, 

91 "version": plugin.version, 

92 "size": plugin.size, 

93 "build_host": plugin.build_host, 

94 "build_date": plugin.build_date 

95 } 

96 if mode == "file": 

97 res['metadatas'].update({ 

98 "license": plugin.license, 

99 "packager": plugin.packager, 

100 "vendor": plugin.vendor, 

101 "url": plugin.url, 

102 "summary": plugin.summary 

103 }) 

104 else: 

105 res['metadatas'].update({ 

106 "license": plugin.configuration.license, 

107 "packager": plugin.configuration.packager, 

108 "vendor": plugin.configuration.vendor, 

109 "url": plugin.configuration.url, 

110 "summary": plugin.configuration.summary 

111 }) 

112 return res 

113 

114 

115def get_plugin_hash(name_or_filepath, mode="auto", plugins_base_dir=None): 

116 """Get a hash about a plugin. 

117 

118 This is just a compat helper 

119 (as the real implementatin is in manager class). 

120 

121 Args: 

122 name_or_filepath (string): name or file path of the plugin. 

123 mode (string) 

124 - "name": get information from the plugin name 

125 (name_or_filepath is the name of the plugin). 

126 - "file": get information from the plutgin file 

127 (name_or_filepath is the plugin file path). 

128 - "auto": guess if the name_or_filepath parameter is the name 

129 or the file path of the plugin. 

130 plugins_base_dir (string): (optional) the plugin base directory path. 

131 If not set, the default plugins base directory path is used. 

132 

133 Returns: 

134 (string): string digest data for the plugin. 

135 

136 """ 

137 infos = get_plugin_info(name_or_filepath, mode=mode, 

138 plugins_base_dir=plugins_base_dir) 

139 if infos is None: 

140 return None 

141 sid = ", ".join([infos['metadatas'].get('build_host', 'unknown'), 

142 infos['metadatas'].get('build_date', 'unknown'), 

143 infos['metadatas'].get('size', 'unknown'), 

144 infos['metadatas'].get('version', 'unknown'), 

145 infos['metadatas'].get('release', 'unknown')]) 

146 return hashlib.md5(sid.encode('utf8')).hexdigest() 

147 

148 

149def get_layer_home_from_plugin_name(name, plugins_base_dir=None): 

150 infos = get_plugin_info(name, mode="name", 

151 plugins_base_dir=plugins_base_dir) 

152 if infos is None: 

153 return None 

154 return infos['home']