def minify(self, in_files, outfile, concat=False): """ Compress in_files into outfile, give some stats """ from build import verbose import os # concat everything into temp outtype = outfile.split('.')[-1] temp = self.concat(in_files, is_js=True) out = open(outfile, 'w') org_size = len(temp.getvalue()) temp.seek(0) # minify jsm = JavascriptMinify() jsm.minify(temp, out) out.close() self.vc.repo.add(outfile) new_size = os.path.getsize(outfile) if verbose: print '=> %s' % outfile print 'Original: %.2f kB' % (org_size / 1024.0) print 'Compressed: %.2f kB' % (new_size / 1024.0) print 'Reduction: %.1f%%' % (float(org_size - new_size) / org_size * 100)
def concat(self, filelist, outfile=None): """ Concat css and js files into a bundle """ import os from cStringIO import StringIO from build import verbose out_type = outfile and outfile.split('.')[-1] or 'js' outtxt = '' for f in filelist: suffix = None if ':' in f: f, suffix = f.split(':') # print f + ' | ' + str(int(os.path.getsize(f)/1024)) + 'k' # get data with open(f, 'r') as infile: # get file type ftype = f.split('.')[-1] data = infile.read() # css -> js if out_type=='js' and ftype =='css': data = "\nwn.assets.handler.css('%s');\n" %\ data.replace("'", "\\'").replace('\n', '\\\n') outtxt += ('\n/*\n *\t%s\n */' % f) # append if suffix=='concat' or out_type != 'js': outtxt += data else: jsm = JavascriptMinify() tmpin = StringIO(data) tmpout = StringIO() jsm.minify(tmpin, tmpout) tmpmin = tmpout.getvalue() or '' tmpmin.strip('\n') outtxt += tmpmin with open(outfile, 'w') as f: f.write(outtxt) print "Wrote %s - %sk" % (outfile, str(int(os.path.getsize(outfile)/1024)))