Coverage for mfplugin/file.py: 66%

98 statements  

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

1import os 

2import tarfile 

3import json 

4from mfplugin.utils import get_default_plugins_base_dir, \ 

5 BadPluginFile, layerapi2_label_to_plugin_name 

6 

7 

8class PluginFile(object): 

9 

10 def __init__(self, plugin_filepath): 

11 self.plugins_base_dir = get_default_plugins_base_dir() 

12 """Plugins base directory (string).""" 

13 if not os.path.isfile(plugin_filepath): 

14 raise BadPluginFile("file: %s not found" % plugin_filepath) 

15 self.plugin_filepath = plugin_filepath 

16 """Plugin file path (string).""" 

17 self.__loaded = False 

18 

19 def load(self): 

20 if self.__loaded: 

21 return 

22 self.__loaded = True 

23 if not os.path.isfile(self.plugin_filepath): 

24 raise BadPluginFile("%s does not exist" % self.plugin_filepath) 

25 try: 

26 tf = tarfile.open(name=self.plugin_filepath, mode='r') 

27 except Exception as e: 

28 raise BadPluginFile( 

29 "can't open %s as a tar.gz file => this is probably not a " 

30 "metwork >= 1.0 plugin" % self.plugin_filepath, 

31 original_exception=e) 

32 try: 

33 reader1 = tf.extractfile("metwork_plugin/.layerapi2_label") 

34 label = reader1.read().decode('utf8').strip() 

35 self._name = layerapi2_label_to_plugin_name(label) 

36 except Exception as e: 

37 raise BadPluginFile( 

38 "can't read/find metwork_plugin/.layerapi2_label file in " 

39 "plugin", original_exception=e) 

40 try: 

41 reader2 = tf.extractfile("metwork_plugin/.metadata.json") 

42 metadata = json.loads(reader2.read().decode('utf8').strip()) 

43 self._version = metadata['version'] 

44 self._release = metadata['release'] 

45 self._build_host = metadata['build_host'] 

46 self._build_date = metadata['build_date'] 

47 self._size = metadata['size'] 

48 self._summary = metadata['summary'] 

49 self._license = metadata['license'] 

50 self._packager = metadata['packager'] 

51 self._vendor = metadata['vendor'] 

52 self._url = metadata['url'] 

53 except Exception as e: 

54 raise BadPluginFile( 

55 "can't read/find metwork_plugin/.metadata.json file in " 

56 "plugin", original_exception=e) 

57 try: 

58 reader3 = tf.extractfile("metwork_plugin/.files.json") 

59 self._files = json.loads(reader3.read().decode('utf8').strip()) 

60 except Exception as e: 

61 raise BadPluginFile( 

62 "can't read/find metwork_plugin/.files.json file in " 

63 "plugin", original_exception=e) 

64 

65 @property 

66 def summary(self): 

67 self.load() 

68 return self._summary 

69 

70 @property 

71 def license(self): 

72 self.load() 

73 return self._license 

74 

75 @property 

76 def packager(self): 

77 self.load() 

78 return self._packager 

79 

80 @property 

81 def name(self): 

82 self.load() 

83 return self._name 

84 

85 @property 

86 def vendor(self): 

87 self.load() 

88 return self._vendor 

89 

90 @property 

91 def url(self): 

92 self.load() 

93 return self._url 

94 

95 @property 

96 def version(self): 

97 self.load() 

98 return self._version 

99 

100 @property 

101 def release(self): 

102 self.load() 

103 return self._release 

104 

105 @property 

106 def size(self): 

107 self.load() 

108 return self._size 

109 

110 @property 

111 def build_host(self): 

112 self.load() 

113 return self._build_host 

114 

115 @property 

116 def build_date(self): 

117 self.load() 

118 return self._build_date 

119 

120 @property 

121 def files(self): 

122 self.load() 

123 return self._files 

124 

125 @property 

126 def home(self): 

127 return None