-
Notifications
You must be signed in to change notification settings - Fork 2
/
crushpngs
executable file
·126 lines (108 loc) · 3.98 KB
/
crushpngs
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
#!/usr/bin/env python
#
# https://github.com/deadmoose/crushpngs
#
# Copyright (C) 2009-2011 David Hoover
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, subprocess, time, threading
from optparse import OptionParser
from Queue import Queue
# http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header
def isPng(path):
# not using 'with' for compatibility
f = open(path)
data = f.read(8)
f.close()
return data == '\x89PNG\r\n\x1a\n'
def crushPng(pngcrush, opts, file):
crushedFile = "%s.crushed" % file
process = subprocess.Popen(
' '.join([pngcrush, opts, file, crushedFile]),
stdout=open('/dev/null'),
stderr=subprocess.PIPE,
shell=True
)
if not process.wait() == 0:
stdout, stderr = process.communicate()
print "Error running pngcrush: %s" % stderr
os.remove(crushedFile)
exit(-1)
oldStat = os.stat(file)
newStat = os.stat(crushedFile)
if newStat.st_size < oldStat.st_size:
os.rename(crushedFile, file)
return (oldStat.st_size, oldStat.st_size - newStat.st_size)
else:
os.remove(crushedFile)
return (oldStat.st_size, 0)
if __name__ == "__main__":
parser = OptionParser(usage='''crushpngs [options] [path]
Runs pngcrush against all pngs found under a path and collects some information
about the savings.
''')
parser.add_option("--pngcrush", dest="path", default="pngcrush",
help="Path to the pngcrush executable")
parser.add_option("--pngcrush-opts", dest="opts", default="-q -brute -reduce",
help="Options to pass to pngcrush")
parser.add_option("-s", "--since", dest="date", default="1970-01-01",
help="Only crush files modified after the specified date")
parser.add_option("-t", "--threads", dest="threads", type="int", default=4,
help="Number of crushing threads to run simultaneously")
(options, args) = parser.parse_args()
if len(args) == 0:
args.append(os.getcwd())
earliest = time.mktime(time.strptime(options.date, '%Y-%m-%d'))
pngs = Queue()
# Keep some stats
filesChanged = 0
bytesSaved = 0
bytesOriginal = 0
def worker():
global filesChanged, bytesSaved, bytesOriginal
while True:
path = pngs.get()
original, saved = crushPng(options.path, options.opts, path)
bytesOriginal += original
if saved:
filesChanged += 1
bytesSaved += saved
pngs.task_done()
tooOld = set()
for arg in args:
for root, dirs, files in os.walk(arg):
for name in files:
path = os.path.join(root, name)
if isPng(path):
if os.path.getmtime(path) > earliest:
pngs.put(path)
else:
tooOld.add(path)
if not pngs:
print "No files to crush"
exit(0)
if tooOld:
print "Old files ignored: %s" % len(tooOld)
print "Files to process: %s" % pngs.qsize()
for i in range(options.threads):
t = threading.Thread(target=worker)
t.setDaemon(True)
t.start()
pngs.join()
# And print some summary info
print "Files changed: %d" % filesChanged
print "Bytes saved: %d / %d (%.2f%%)" % (
bytesSaved,
bytesOriginal,
(100 * float(bytesSaved) / bytesOriginal) if bytesOriginal else 0
)