-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanProject.py
More file actions
186 lines (145 loc) · 4.92 KB
/
CleanProject.py
File metadata and controls
186 lines (145 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
####################################
# File name: ClearProject.py #
# Author: AhMeDz #
# Submission: 5 May 2016 #
####################################
"""CleanProject.py: Deletes unused image drawables from given project directory"""
import os, re, sys, webbrowser
whiteList = set()
regexMatches = set()
regexList = set()
toBeDeleted = set()
outputFile = "output.txt"
f = open(outputFile, 'w')
deletedFiles = 0
bytesFreed = 0
#Check whether given directory holds drawables or not.
def isResourceDir(directory):
return (
(os.path.exists(directory+"/drawable")) or
(os.path.exists(directory+"/drawable-ldpi")) or
(os.path.exists(directory+"/drawable-mdpi")) or
(os.path.exists(directory+"/drawable-hdpi")) or
(os.path.exists(directory+"/drawable-xhdpi")) or
(os.path.exists(directory+"/drawable-xxhdpi")) or
(os.path.exists(directory+"/drawable-xxxhdpi"))) and "src" in directory
#We only want to remove unused PICTURES (pngs)
def addFileToWhiteList(fileName):
#United scheme for later processing
fileName = fileName.replace("R.drawable.", "").replace("@drawable/","")
whiteList.add(fileName)
#Find pattern in string and return list of matches.
def findAll(regex, content):
pattern = re.compile(regex)
return pattern.findall(content)
#Check to see what resources are referenced in this file.
def checkFileForResources(path):
file = open(path, 'r')
contents = file.read()
file.close()
#Search code files.
results = findAll('R.drawable.[a-zA-Z0-9_]*', contents)
for result in results:
addFileToWhiteList(result)
#Search layout files.
results = findAll('@drawable/[a-zA-Z0-9_]*', contents)
for result in results:
addFileToWhiteList(result)
#Get file size by path
def getFileSize(path):
return os.path.getsize(path) / 1024.0 / 1024.0
#Check if fileName matches any given regex or not.
def matchRegex(fileName):
for regex in regexList:
if len(findAll(regex, fileName)) > 0:
# print fileName, " matches ", regex
return True
return False
#We only want to if it's an unreferenced PNG.
def deleteIfUnused(directory, origFileName):
if origFileName.endswith(".png"):
fileName = os.path.splitext(origFileName)[0]
if fileName.endswith(".9.png"):
fileName = os.path.splitext(fileName)[0]
#Delete if not whiteListed nor matches any given Regex.
if fileName not in whiteList:
if matchRegex(fileName):
regexMatches.add(fileName)
else:
deleteResFile(directory, origFileName)
#Delete file and update status.
def deleteResFile(directory, fileName):
#Do stats tracking.
global deletedFiles
global bytesFreed
path = directory+"/"+fileName
deletedFiles += 1
fileSize = getFileSize(path)
bytesFreed += fileSize
#Actually delete the file.
toBeDeleted.add(path)
printToFile(("Detected (%.4f Mbs): " + path) % fileSize)
#Print to file.
def printToFile(s):
# print s
f.write(s + "\n")
#Delete queued files.
def deleteQueuedFiles():
for file in toBeDeleted:
os.unlink(file)
#Shows status of completion.
def showStatus():
if len(whiteList) != 0:
printToFile("")
printToFile("Files that has been whiteListed: ")
printToFile("--------------------------------\n")
for fileName in whiteList:
printToFile("--> " + fileName)
if len(regexMatches) != 0:
printToFile("")
printToFile("Files that matched an expression and were excluded: ")
printToFile("---------------------------------------------------\n")
for fileName in regexMatches:
printToFile("--> " + fileName)
printToFile("")
printToFile("%d file(s) deleted" % deletedFiles)
printToFile("%.4f Mbs were freed" % bytesFreed)
#Open file using the browser library (actually will open it in the default OS app for text files).
f.close()
print ("Opening output file.")
webbrowser.open(outputFile)
print ("Done!")
print "Are you sure you want to delete the files specified in the file? (Y/N).."
c = raw_input()[0]
if c == 'Y':
deleteQueuedFiles()
print 'Deleted successfully!'
else:
print 'Aborted!'
#Get real files path (.../app/src/...)
def getRootDir(path):
for root, dirs, files in os.walk(path):
if "app/src" in root:
return root
return ""
#######################################################################
#Make sure they passed in a project source directory.
args = sys.argv
if len(args) < 2 or not os.path.exists(args[1]):
print "Usage: python CleanProject.py 'directory_path' regex1 regex2 ... regexN"
quit()
rootDir = max(args[1], getRootDir(args[1]))
# rootDir = args[1]
resDir = rootDir
for i in range(2,len(args)):
regexList.add(args[i])
for root, dirs, files in os.walk(rootDir):
if isResourceDir(root):
resDir = root
for file in files:
checkFileForResources(root+"/"+file)
print ("Detected resource directory: %s" % resDir)
for root, dirs, files in os.walk(resDir):
for file in files:
deleteIfUnused(root, file)
showStatus()