<s> """<STR_LIT>""" <EOL> from . utils import ( tree_walk , match_regexes , move_path , minified_filename , <EOL> get_extension , default_minifier , should_update_key ) <EOL> from . exceptions import MinifierError <EOL> import re <EOL> import subprocess <EOL> from boto . s3 . connection import S3Connection <EOL> from boto . exception import S3ResponseError <EOL> from boto . s3 . key import Key <EOL> class StaticDir ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , directory ) : <EOL> self . directory = directory <EOL> self . input_directory = None <EOL> self . minifier = default_minifier ( ) <EOL> self . force_update = False <EOL> self . ignore = [ ] <EOL> self . metadata = { } <EOL> def update ( self , bucket_name ) : <EOL> """<STR_LIT>""" <EOL> self . minify_files ( ) <EOL> files = self . enumerate_files ( self . directory ) <EOL> conn = self . connect_s3 ( ) <EOL> self . upload_files ( files , bucket_name , conn ) <EOL> return <EOL> def enumerate_files ( self , directory ) : <EOL> """<STR_LIT>""" <EOL> files = tree_walk ( directory ) <EOL> files = self . filter_files ( files ) <EOL> return files <EOL> def filter_files ( self , files ) : <EOL> """<STR_LIT>""" <EOL> tests = [ re . compile ( x ) for x in self . ignore ] <EOL> if tests : <EOL> files = [ name for name in files if not match_regexes ( tests , name ) ] <EOL> return files <EOL> def minify_files ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . input_directory : <EOL> files = self . enumerate_files ( self . input_directory ) <EOL> else : <EOL> files = self . enumerate_files ( self . directory ) <EOL> if isinstance ( self . minifier , basestring ) : <EOL> minifier = { '<STR_LIT>' : self . minifier , '<STR_LIT>' : self . minifier } <EOL> else : <EOL> minifier = self . minifier <EOL> for name in files : <EOL> try : <EOL> command = minifier [ get_extension ( name ) ] <EOL> command = command . format ( in_name = name , <EOL> out_name = self . get_output_name ( name ) ) <EOL> rc = subprocess . call ( command , shell = True ) <EOL> if rc != <NUM_LIT:0> : <EOL> raise MinifierError ( "<STR_LIT>" ) <EOL> except KeyError : <EOL> continue <EOL> return <EOL> def get_output_name ( self , input_filename ) : <EOL> """<STR_LIT>""" <EOL> if self . input_directory : <EOL> filename = move_path ( self . input_directory , <EOL> self . directory , <EOL> input_filename ) <EOL> filename = minified_filename ( filename ) <EOL> else : <EOL> filename = minified_filename ( input_filename ) <EOL> return filename <EOL> def connect_s3 ( self ) : <EOL> """<STR_LIT>""" <EOL> return S3Connection ( ) <EOL> def upload_files ( self , files , bucket_name , connection ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> bucket = connection . get_bucket ( bucket_name ) <EOL> except S3ResponseError : <EOL> bucket = connection . create_bucket ( bucket_name ) <EOL> for path in files : <EOL> key = self . find_or_create_key ( path ) <EOL> if self . force_update or should_update_key ( key , path ) : <EOL> key . set_contents_from_filename ( path ) <EOL> for metakey , metavalue in self . metadata . iteritems ( ) : <EOL> key . set_metadata ( metakey , metavalue ) <EOL> key . set_acl ( '<STR_LIT>' ) <EOL> return <EOL> def apply_metadata ( self , key ) : <EOL> """<STR_LIT>""" <EOL> for metakey , metavalue in self . metadata . iteritems ( ) : <EOL> if isinstance ( metavalue , dict ) and re . search ( metakey , key . key ) : <EOL> for newkey , newvalue in metavalue . iteritems ( ) : <EOL> key . set_metadata ( newkey , newvalue ) <EOL> elif not isinstance ( metavalue , dict ) : <EOL> key . set_metadata ( metakey , metavalue ) <EOL> return <EOL> def find_or_create_key ( self , path , bucket ) : <EOL> """<STR_LIT>""" <EOL> name = self . key_name_from_path ( path ) <EOL> key = bucket . lookup ( name ) <EOL> if not key : <EOL> key = Key ( bucket ) <EOL> key . key = name <EOL> return key <EOL> def key_name_from_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if self . directory [ - <NUM_LIT:1> ] != '<STR_LIT:/>' : <EOL> temp_directory = self . directory + '<STR_LIT:/>' <EOL> else : <EOL> temp_directory = self . directory <EOL> return path . replace ( temp_directory , '<STR_LIT>' , <NUM_LIT:1> ) </s>
