How to remove old versions of Eclipse plugins?

后端 未结 11 2145
南方客
南方客 2020-12-13 02:06

After update, old Eclipse plugins remain in \"plugins\" folder (there are also leftovers in \"features\" folder).

Is there a way to remove those automatically?

11条回答
  •  眼角桃花
    2020-12-13 03:10

    I've created a script in python to move the old plugins to a backup folder, so if something goes wrong it can go back. The script has two modes of operation: Manual mode asks you what to do with each duplicated plugin detected, and automatic only question in cases where the length of the string has changed and therefore may have changed the system, or version numbering.

    I hope this helps someone

    # -*- coding: utf-8 -*-
    import os
    import re
    from datetime import datetime
    
    directory="C:\\eclipse64\\plugins"
    dirBackup="C:\\eclipse64\\PluginsBackup"        #This folder is a kind of recycle bin for save deleted plugins. In case you have problems running eclipse after remove them you can restore them. If you don't detect any problem you can erase this folder to save disk space
    manual=False    #Verifying deletion of each plugin manually (True) or automatic (False) 
    
    def globRegEx(directory,pat,absolutePath=True,type_=0):
        '''Function that given a directory and a regular pattern returns a list of files that meets the pattern
    
        :param str directory: Base path where we search for files that meet the pattern
        :param str pat: Regular expression that selected files must match 
        :param bool absolutePath: Optional parameter that indicates if the returned list contains absolute (True) or relative paths (False)
        :param int type_: Type of selection 0: selects files and directories 1: only selects files 2: only selects directories
        :return: a list with the paths that meet the regular pattern
        '''
        names=os.listdir(directory)
        pat=re.compile(pat)
        res=[]
    
        for name in names:
            if pat.match(name):
                path=directory+os.sep+name
    
                if type_==1 and os.path.isfile(path):
                    res.append(path if absolutePath else name)
                elif type_==2 and os.path.isdir(path):
                    res.append(path if absolutePath else name)
                elif type_==0:
                    res.append(path if absolutePath else name)
    
        return(res)
    
    def processRepeated(repList):
        ''' this function is responsible for leaving only the newer version of the plugin
        '''
    
        if repList and len(repList)>1:     #If the plugin is repeated
            repList.sort(reverse=True)
            print("Repeated plugins found:")
            min=len(repList[0])    # If strings haven't got the same length indicates a change in the numeration version system
            max=min
            newer=datetime.fromtimestamp(0)
            sel=0
    
            for i,path in enumerate(repList):
                lr=len(path)
                modifDate=datetime.fromtimestamp((os.path.getctime(path)))
                if modifDate>newer:     #Keep the last creation date and its index
                    newer=modifDate
                    sel=i+1
    
                if lrmax: 
                    max=lr
    
                print(str(i+1) + " " + modifDate.strftime("%Y-%m-%d") + ": " + path)
            print(" ")
    
            if manual or min!=max:      #If manual mode is enabled or if there is a string length diference between different version of plugins
                selec=raw_input("Which version do you want to keep?: ["+str(sel)+"] ")
                if selec:
                    selec=int(selec)
                else: 
                    selec=sel   #Newer is the Default value
            else:
                selec=1
    
    
            del(repList[selec-1])      #Delete selected plugin from the list
    
            for path in repList:  #Move the rest of the list to the backup folder
                print("Deleting: "+ path)
                os.renames(path,os.path.join(dirBackup,os.path.basename(path)))
    
            print("-------------------------------------\n\n")
    
    def main():
    
        filePlugins=globRegEx(directory,"^.*$",False,1)      #Creates a list with all the files only
        dirPlugins=globRegEx(directory,"^.*$",False,2)       #Creates a list with all the folders only
    
    
        #Process files first
    
        for plugin in filePlugins:
            m=re.match(r"(.*_)\d.*?\.jar$",plugin)   #Creates the glob pattern
            if m:
                patAux=m.groups()[0]+".*?\.jar$"
                find=globRegEx(directory,patAux,True,1)
                processRepeated(find)
    
        #Now Directories 
    
        for plugin in dirPlugins:
            m=re.match(r"(.*_)\d.*$",plugin)   #Creates the glob pattern
            if m:
                patAux=m.groups()[0]+".*$"
                find=globRegEx(directory,patAux,True,2)
                processRepeated(find)
    
    if __name__=="__main__":
        main()
    

提交回复
热议问题