Browse Source

Adding a -c --clean option to plugins action in lodel_admin

Clean take care of deleting plugins with same name and different versions
(the lower versions will be deleted)
Yann Weber 8 years ago
parent
commit
eac6df20fa
1 changed files with 58 additions and 4 deletions
  1. 58
    4
      lodel/plugin/core_scripts.py

+ 58
- 4
lodel/plugin/core_scripts.py View File

@@ -81,6 +81,9 @@ class PluginManager(lodel_script.LodelScript):
81 81
         parser.add_argument('-u', '--uninstall',
82 82
             help="Uninstall specified plugin",
83 83
             action='store_true')
84
+        parser.add_argument('-c', '--clean',
85
+            help="Uninstall duplicated plugins with smallest version number",
86
+            action="store_true")
84 87
         parser.add_argument('-n', '--plugin-name', nargs='*',
85 88
             default = list(),
86 89
             help="Indicate a plugin name to uninstall",
@@ -96,6 +99,11 @@ class PluginManager(lodel_script.LodelScript):
96 99
 
97 100
     @classmethod
98 101
     def run(cls, args):
102
+        if args.clean:
103
+            if args.uninstall or len(args.directory) > 0 or len(args.file) > 0:
104
+                raise RuntimeError("When using -c --clean option you can \
105
+only use option -n --name to clean plugins with specified names")
106
+            return cls.clean(args)
99 107
         if args.uninstall:
100 108
             return cls.uninstall(args)
101 109
         return cls.install(args)
@@ -136,7 +144,7 @@ We do not know where to find it...")
136 144
                 #Found an installed plugin with the same name
137 145
                 #Cehcking both versions
138 146
                 if plist[pname]['version'] == pinfos['version']:
139
-                    errors[pinfos['path']] = 'Abording installation of %s\
147
+                    errors[pinfos['path']] = 'Abording installation of %s \
140 148
 found in %s because it seems to be allready installed in %s' % (
141 149
     pname, pinfos['path'], plist[pname]['path'])
142 150
                     continue
@@ -163,12 +171,58 @@ in %s" % (orig_path, dst_path))
163 171
             shutil.copytree(pinfos['path'], dst_path, symlinks = False)
164 172
             print("%s(%s) installed in %s" % (
165 173
                 pname, pinfos['version'], dst_path))
174
+        if len(errors) > 0:
175
+            msg = "Following errors occurs during instalation process :\n"
176
+            for path, error_msg in errors.items():
177
+                msg += "\t- For '%s' : %s" % (path, error_msg)
178
+            print(msg)
166 179
     
167
-    ##@brief Handles plugin uninstall
180
+    ##@brief Handles plugins uninstall
168 181
     @classmethod
169 182
     def uninstall(cls, args):
170
-        pass
171
-        
183
+        raise NotImplementedError("Not yet implemented")
184
+
185
+    ##@brief Handles plugins clean
186
+    @classmethod
187
+    def clean(cls, args):
188
+        import lodel.plugin.plugins
189
+        from lodel.plugin.plugins import Plugin
190
+        if len(args.file) > 0:
191
+            raise RuntimeError("Cannot specify plugins to uninstall using \
192
+-f --file option. You have to use -d --directory or -n --name")
193
+        if len(args.plugin_name) > 0:
194
+            names = args.plugin_name
195
+        else:
196
+            names = list(Plugin.discover().keys())
197
+        #_dicover do not remove duplicated names
198
+        full_list = Plugin._discover(lodel.plugin.plugins.PLUGINS_PATH)
199
+        #Casting into a dict with list of plugins infos
200
+        pdict = dict()
201
+        for pinfos in full_list:
202
+            if pinfos['name'] in names:
203
+                if pinfos['name'] in pdict:
204
+                    pdict[pinfos['name']].append(pinfos)
205
+                else:
206
+                    pdict[pinfos['name']] = [pinfos]
207
+        to_clean = list()
208
+        clean_count = 0
209
+        for pname, pinfos_l in pdict.items():
210
+            if len(pinfos_l) > 1:
211
+                #There are some plugins to clean
212
+                tmp_l = sorted(pinfos_l, key=lambda item: item['version'])
213
+                to_clean += tmp_l[:-1]
214
+                msg = "Found %s(%s). Cleaning " % (
215
+                    pname, tmp_l[-1]['version'])
216
+                for pinfos in to_clean:
217
+                    clean_count += 1
218
+                    str_info = '%s(%s)' % (pname, pinfos['version'])
219
+                    msg += "%s, " % (str_info)
220
+                    shutil.rmtree(pinfos['path'])
221
+                print(msg)
222
+        if clean_count > 0:
223
+            print("%d plugins were uninstalled" % clean_count)
224
+        else:
225
+            print("Allready clean")
172 226
 
173 227
 
174 228
 ##@brief Implements lodel_admin.py **hooks-list** action

Loading…
Cancel
Save