更新STM32CubeIDE及旧文件的清理

清理原因

  • 1、电脑每次更新完STM32CubeIDE后,都会残留上个版本的插件和一些替换掉的文件,而STM32CubeIDE是基于eclipse的,默认情况下不会清理这些文件。
  • 2、STM32CubeIDE更新后残留的旧文件会不断累积,我电脑的硬盘空间快不够用了。
  • 3、对照着时间一个个删去替换掉的文件是一个办法,但是遇到1000多项甚至更多文件,效率会很低。

解决方案

  • 使用python3,可以编写脚本解决上述问题。(声明:以下脚本是我测试时修改的,原作者是基于python2.x编写的。传送门:原作者python2脚本地址)
# -*- coding: utf-8 -*-
import os
import re
from datetime import datetime

# directory="D:\\STM32CubeIDE\\STM32CubeIDE_1.0.2\\STM32CubeIDE\\features"
directory="D:\\STM32CubeIDE\\STM32CubeIDE_1.0.2\\STM32CubeIDE\\plugins"
dirBackup="D:\\STM32CubeIDE\\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 lr<min:
                min=lr
            elif lr>max:
                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=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("-------------------------------------")

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()

执行结果

更新STM32CubeIDE及旧文件的清理_第1张图片

  • 此方法也适用于eclipse的旧文件清理。

你可能感兴趣的:(STM32CubeIDE)