# This Python file uses the following encoding: utf-8 import glob import os import re ''' Fetch all the directories, and display all the unused properties. Check java, ftl and xml files. ''' dirToScan = '/home/quentin/dev/sources/' cssDir = '/home/quentin/dev/sources/src/main/webapp/styles' class_regex = re.compile('(\..* {)') intern_regex = re.compile('.*\.(.*)') files = os.listdir(cssDir) #Rajouter ici les fichier à ignorer specific_files = ['.svn'] styles = set([]) def cleanDeclaration (declaration) : if declaration.find(":") != -1 : declaration = declaration[:declaration.find(":")] if declaration.find(" ") != -1 : declaration = declaration[:declaration.find(" ")] if declaration.find(">") != -1 : declaration = declaration[:declaration.find(">")] return declaration def addStyle (clean_style): clean_styles = clean_style.split('.'); if len(clean_styles)>0: for cs in clean_styles: if cs.find(",") != -1 : cs = cs[:cs.find(",")] styles.add(cs) #print(cs) else: styles.add(clean_style) #print(clean_style) def findImages(dir): files = glob.glob(dir + '/*') for file in files: if file not in specific_files and file.find("jquery") == -1: if os.path.isdir(file): findImages(file) else: cursor = open(file,'r') #read the file and get the properties lines = cursor.readlines() back = False for line in lines: #at least a ligne with a character. if len(line)>1 : match = class_regex.findall(line) if len(match)>0: declaration = match[0] declaration = declaration[1:len(declaration)-2] clean_style = cleanDeclaration(declaration) addStyle(clean_style) while (len(intern_regex.findall(declaration))>0 and intern_regex.findall(declaration)[0] != ""): intern_match = intern_regex.findall(declaration) #print(intern_match) sub_declaration = intern_match[0] if len(sub_declaration)>0: clean_style = cleanDeclaration(sub_declaration) addStyle(clean_style) declaration = declaration[:len(declaration)-len(sub_declaration)] if declaration[len(declaration)-1:len(declaration)] == ".": declaration = declaration[:len(declaration)-1] findImages(cssDir) print(styles) def scanFolder(dir): files = glob.glob(dir+'/*') for file in files: if os.path.isdir(file): scanFolder(file) elif file.find(".java") != -1 or file.find(".properties") != -1 or file.find(".js") != -1 or file.find(".ftl") != -1 or file.find(".xml") != -1 : cursor = open(file,'r') lines = cursor.readlines() i=0 for line in lines: i=i+1 copy_styles = set(styles) for style in copy_styles: if line.find(style) != -1: print("removed "+style+" in "+file+" line "+str(i)) styles.remove(style) scanFolder(dirToScan) old_file = "" for style in styles: print(style)