Created
July 7, 2011 14:14
-
-
Save guyromm/1069599 to your computer and use it in GitHub Desktop.
комрессирует скрипты на лету, плагин для routes, инвокация в конце.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def cscript_expand(kw,style=False): | |
| if style: cachedir='styles' | |
| else: cachedir='scripts' | |
| #we need to hash our list of scripts to get a unique new compressed name for it | |
| m = hashlib.md5() | |
| last_mt = None | |
| sz = 0 | |
| #remove logging in compressed scripts | |
| if '/general_trawler.js' in kw['o']: kw['o'][kw['o'].index('/general_trawler.js')]='/general_trawler_nologging.js' | |
| for scr in kw['o']: | |
| m.update(scr) | |
| myst = os.stat(os.path.join(config['pylons.paths']['static_files'],scr[1:])) | |
| mt = myst.st_mtime | |
| sz += myst.st_size | |
| rlog.info('%s = %s bytes'%(scr,myst.st_size)) | |
| if not last_mt or mt>last_mt: last_mt=mt | |
| hd = m.hexdigest() | |
| kw['fnhash']=hd | |
| #then, actually generate our compressed script if we need to: | |
| if style: | |
| sfx = '.min.css' | |
| else: | |
| sfx = '.min.js' | |
| tfile = os.path.join(config['pylons.paths']['static_files'],cachedir,kw['fnhash']+sfx) | |
| fex = os.path.exists(tfile) | |
| st=None | |
| if fex: | |
| st = os.stat(tfile) | |
| rlog.info('source files are %s. destination is %s'%(sz,st.st_size)) | |
| #we need to generate either when comrpessed file is missing or is older than the newest source file | |
| if not fex or st.st_mtime<last_mt: | |
| if style: | |
| jarn = 'yuicompressor.jar' | |
| opopt='-o ' | |
| inopt='' | |
| else: | |
| jarn = 'google.closure.compiler.jar' | |
| opopt='--js_output_file=' | |
| inopt='--js=' | |
| cmd = 'java -jar %s %s %s%s'%(os.path.join(config['pylons.paths']['root'],'..',jarn) | |
| ,' '.join(['%s%s'%(inopt,os.path.join(config['pylons.paths']['static_files'],fn[1:])) for fn in kw['o']]) | |
| ,opopt | |
| ,tfile) | |
| rlog.info(cmd) | |
| st,op = commands.getstatusoutput(cmd) | |
| if st!=0: | |
| os.unlink(tfile) | |
| raise Exception('returned %s,%s'%(st,op)) | |
| del kw['o'] | |
| return kw | |
| def cstyle_expand(kw): | |
| return cscript_expand(kw,style=True) | |
| map.connect('js',"/cscript/{fnhash}.min.js",controller='interface',action='cscript',_filter=cscript_expand) | |
| map.connect('css',"/cstyle/{fnhash}.min.css",controller='interface',action='cstyle',_filter=cstyle_expand) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment