<s> import logging <EOL> import re <EOL> import os <EOL> import shutil <EOL> from cactus . utils import fileList <EOL> from cactus . plugin_base import CactusPluginBase <EOL> from slimit import minify <EOL> class MinifyJsPlugin ( CactusPluginBase ) : <EOL> infile = None <EOL> outfile = None <EOL> buildpath = None <EOL> js_dir = None <EOL> def _prepare ( self , * args , ** kwargs ) : <EOL> from django . conf import settings <EOL> dist = kwargs . get ( "<STR_LIT>" , False ) <EOL> self . buildpath = "<STR_LIT>" if dist else "<STR_LIT>" <EOL> self . js_dir = os . path . join ( self . site . paths [ self . buildpath ] , settings . STATIC_URL_REL , '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . js_dir ) or not os . listdir ( self . js_dir ) : <EOL> return False <EOL> self . infile = os . path . abspath ( <EOL> os . path . join ( <EOL> self . js_dir , <EOL> self . config . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> ) <EOL> ) <EOL> self . outfile = os . path . abspath ( <EOL> os . path . join ( <EOL> self . js_dir , <EOL> self . config . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> ) <EOL> ) <EOL> return True <EOL> def _minify ( self , infile , outfile = None ) : <EOL> if outfile is None : <EOL> outfile = infile <EOL> logging . info ( "<STR_LIT>" . format ( infile ) ) <EOL> f = open ( infile , '<STR_LIT:r>' ) <EOL> content = f . read ( ) <EOL> f . close ( ) <EOL> f = open ( outfile , '<STR_LIT:w>' ) <EOL> f . write ( minify ( content , mangle = False , mangle_toplevel = False ) ) <EOL> f . close ( ) <EOL> def postBuild ( self , * args , ** kwargs ) : <EOL> if not self . _prepare ( dist = False ) : <EOL> return <EOL> try : <EOL> shutil . move ( self . infile , self . outfile ) <EOL> except IOError : <EOL> pass <EOL> def postDist ( self , * args , ** kwargs ) : <EOL> if not self . _prepare ( dist = True ) : <EOL> return <EOL> self . _minify ( self . infile , self . outfile ) <EOL> if self . infile != self . outfile and not self . config . get ( '<STR_LIT>' , True ) : <EOL> os . remove ( self . infile ) <EOL> if self . config . get ( '<STR_LIT>' , False ) : <EOL> for filename in fileList ( self . js_dir ) : <EOL> if os . path . abspath ( filename ) != self . outfile and not re . search ( r'<STR_LIT>' , filename , re . I ) : <EOL> self . _minify ( filename ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> def templateContext ( self , * args , ** kwargs ) : <EOL> return { <EOL> "<STR_LIT>" : self . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> } </s>
