<s> import hashlib <EOL> import gzip <EOL> import mimetypes <EOL> import os <EOL> import shutil <EOL> import sys <EOL> import tempfile <EOL> from boto . exception import S3ResponseError <EOL> from boto . s3 . connection import S3Connection , OrdinaryCallingFormat <EOL> from boto . s3 . key import Key <EOL> from clint . textui import puts <EOL> from . utils import show_error <EOL> GZIP_TIMESTAMP = <NUM_LIT> <EOL> class S3Url ( str ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( self , content ) : <EOL> if not content . endswith ( "<STR_LIT:/>" ) : <EOL> content = "<STR_LIT>" . format ( content ) <EOL> if content . startswith ( "<STR_LIT>" ) : <EOL> content = content [ <NUM_LIT:5> : ] <EOL> self . root , self . path = content . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> return str . __new__ ( self , content . rstrip ( "<STR_LIT:/>" ) ) <EOL> class S3Sync : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , directory , bucket , access_key_id , secret_access_key , force = False ) : <EOL> if '<STR_LIT:.>' in bucket : <EOL> connection = S3Connection ( access_key_id , secret_access_key , calling_format = OrdinaryCallingFormat ( ) ) <EOL> else : <EOL> connection = S3Connection ( access_key_id , secret_access_key ) <EOL> self . force = force <EOL> self . bucket = bucket <EOL> self . directory = directory . rstrip ( '<STR_LIT:/>' ) <EOL> try : <EOL> self . connection = connection . get_bucket ( bucket . root ) <EOL> except S3ResponseError , e : <EOL> show_error ( "<STR_LIT>" ) <EOL> puts ( "<STR_LIT>" . format ( str ( e ) ) ) <EOL> sys . exit ( ) <EOL> def deploy_to_s3 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . tempdir = tempfile . mkdtemp ( '<STR_LIT>' ) <EOL> for keyname , absolute_path in self . find_file_paths ( ) : <EOL> self . s3_upload ( keyname , absolute_path ) <EOL> shutil . rmtree ( self . tempdir , True ) <EOL> return True <EOL> def s3_upload ( self , keyname , absolute_path ) : <EOL> """<STR_LIT>""" <EOL> mimetype = mimetypes . guess_type ( absolute_path ) <EOL> options = { '<STR_LIT:Content-Type>' : mimetype [ <NUM_LIT:0> ] } <EOL> if mimetype [ <NUM_LIT:0> ] is not None and mimetype [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) : <EOL> upload = open ( absolute_path ) <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> key_parts = keyname . split ( '<STR_LIT:/>' ) <EOL> filename = key_parts . pop ( ) <EOL> temp_path = os . path . join ( self . tempdir , filename ) <EOL> gzfile = gzip . GzipFile ( temp_path , '<STR_LIT:wb>' , <NUM_LIT:9> , None , GZIP_TIMESTAMP ) <EOL> gzfile . write ( upload . read ( ) ) <EOL> gzfile . close ( ) <EOL> absolute_path = temp_path <EOL> hash = '<STR_LIT>' . format ( hashlib . md5 ( open ( absolute_path , '<STR_LIT:rb>' ) . read ( ) ) . hexdigest ( ) ) <EOL> key = "<STR_LIT>" . format ( self . bucket . path , keyname ) <EOL> existing = self . connection . get_key ( key ) <EOL> if self . force or not existing or ( existing . etag != hash ) : <EOL> k = Key ( self . connection ) <EOL> k . key = key <EOL> puts ( "<STR_LIT>" . format ( self . bucket , keyname ) ) <EOL> k . set_contents_from_filename ( absolute_path , options , policy = '<STR_LIT>' ) <EOL> else : <EOL> puts ( "<STR_LIT>" . format ( self . bucket , keyname ) ) <EOL> def find_file_paths ( self ) : <EOL> """<STR_LIT>""" <EOL> paths = [ ] <EOL> for root , dirs , files in os . walk ( self . directory , topdown = True ) : <EOL> rel_path = os . path . relpath ( root , self . directory ) <EOL> for f in files : <EOL> if rel_path == '<STR_LIT:.>' : <EOL> path = ( f , os . path . join ( root , f ) ) <EOL> else : <EOL> path = ( os . path . join ( rel_path , f ) , os . path . join ( root , f ) ) <EOL> paths . append ( path ) <EOL> return paths </s>
