better zip_static_files.py, thanks Niphlod

This commit is contained in:
mdipierro
2012-09-20 11:19:11 -05:00
parent afbbfd7e46
commit 358ec48d8a
2 changed files with 40 additions and 27 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.0.9 (2012-09-20 11:16:45) stable Version 2.0.9 (2012-09-20 11:19:07) stable
+22 -9
View File
@@ -3,16 +3,13 @@
## launch with python web2py.py -S myapp -R scripts/zip_static_files.py ## launch with python web2py.py -S myapp -R scripts/zip_static_files.py
ALLOWED_EXTS = ['.css', '.js']
import os import os
import gzip import gzip
static_path = os.path.abspath(os.path.join(request.folder, 'static'))
filelist = [] def zip_static(filelist=[]):
for root, dir, files in os.walk(static_path): tsave = 0
for file in files: for fi in filelist:
filelist.append(os.path.join(root, file))
tsave = 0
for fi in filelist:
extension = os.path.splitext(fi) extension = os.path.splitext(fi)
extension = len(extension) > 1 and extension[1] or None extension = len(extension) > 1 and extension[1] or None
if not extension or extension not in ALLOWED_EXTS: if not extension or extension not in ALLOWED_EXTS:
@@ -21,6 +18,12 @@ for fi in filelist:
fstats = os.stat(fi) fstats = os.stat(fi)
atime, mtime = fstats.st_atime, fstats.st_mtime atime, mtime = fstats.st_atime, fstats.st_mtime
gfi = fi + '.gz' gfi = fi + '.gz'
if os.path.isfile(gfi):
zstats = os.stat(gfi)
zatime, zmtime = zstats.st_atime, zstats.st_mtime
if zatime == atime and zmtime == mtime:
print 'skipping %s, already gzipped to the latest version' % os.path.basename(fi)
continue
print 'gzipping %s to %s' % (os.path.basename(fi), os.path.basename(gfi)) print 'gzipping %s to %s' % (os.path.basename(fi), os.path.basename(gfi))
f_in = open(fi, 'rb') f_in = open(fi, 'rb')
f_out = gzip.open(gfi, 'wb') f_out = gzip.open(gfi, 'wb')
@@ -31,4 +34,14 @@ for fi in filelist:
saved = fstats.st_size - os.stat(gfi).st_size saved = fstats.st_size - os.stat(gfi).st_size
tsave+= saved tsave+= saved
print 'saved %s KB' % (int(tsave)/1000.0) print 'saved %s KB' % (int(tsave)/1000.0)
if __name__ == '__main__':
ALLOWED_EXTS = ['.css', '.js']
static_path = os.path.abspath(os.path.join(request.folder, 'static'))
filelist = []
for root, dir, files in os.walk(static_path):
for file in files:
filelist.append(os.path.join(root, file))
zip_static(filelist)