<s> import os <EOL> import logging <EOL> import hashlib <EOL> import mimetypes <EOL> import socket <EOL> from . utils import compressString , getURLHeaders , fileSize , retry <EOL> class File ( object ) : <EOL> CACHE_EXPIRATION = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> <EOL> COMPRESS_TYPES = [ '<STR_LIT:html>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> COMPRESS_MIN_SIZE = <NUM_LIT> <EOL> PROGRESS_MIN_SIZE = ( <NUM_LIT> * <NUM_LIT> ) / <NUM_LIT:2> <EOL> def __init__ ( self , site , path , s3_domain ) : <EOL> self . site = site <EOL> self . path = path <EOL> self . s3_domain = s3_domain <EOL> self . paths = { <EOL> '<STR_LIT>' : os . path . join ( site . path , '<STR_LIT>' , self . path ) <EOL> } <EOL> def data ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> f = open ( self . paths [ '<STR_LIT>' ] , '<STR_LIT:r>' ) <EOL> self . _data = f . read ( ) <EOL> f . close ( ) <EOL> return self . _data <EOL> def payload ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> if self . shouldCompress ( ) : <EOL> self . _payload = compressString ( self . data ( ) ) <EOL> else : <EOL> self . _payload = self . data ( ) <EOL> return self . _payload <EOL> def checksum ( self ) : <EOL> """<STR_LIT>""" <EOL> return hashlib . md5 ( self . payload ( ) ) . hexdigest ( ) <EOL> def remoteChecksum ( self ) : <EOL> return getURLHeaders ( self . remoteURL ( ) ) . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( '<STR_LIT:">' ) <EOL> def remoteURL ( self ) : <EOL> return '<STR_LIT>' % ( self . s3_domain , self . path ) <EOL> def extension ( self ) : <EOL> return os . path . splitext ( self . path ) [ <NUM_LIT:1> ] . strip ( '<STR_LIT:.>' ) . lower ( ) <EOL> def shouldCompress ( self ) : <EOL> if not self . extension ( ) in self . COMPRESS_TYPES : <EOL> return False <EOL> if len ( self . data ( ) ) < self . COMPRESS_MIN_SIZE : <EOL> return False <EOL> return True <EOL> @ retry ( socket . error , tries = <NUM_LIT:5> , delay = <NUM_LIT:3> , backoff = <NUM_LIT:2> ) <EOL> def upload ( self , bucket ) : <EOL> self . lastUpload = <NUM_LIT:0> <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' % self . CACHE_EXPIRATION } <EOL> if self . shouldCompress ( ) : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> changed = self . checksum ( ) != self . remoteChecksum ( ) <EOL> if changed : <EOL> progressCallback = None <EOL> progressCallbackCount = int ( len ( self . payload ( ) ) / ( <NUM_LIT> * <NUM_LIT> ) ) <EOL> if len ( self . payload ( ) ) > self . PROGRESS_MIN_SIZE : <EOL> def progressCallback ( current , total ) : <EOL> if current > self . lastUpload : <EOL> uploadPercentage = ( float ( current ) / float ( total ) ) * <NUM_LIT:100> <EOL> logging . info ( '<STR_LIT>' % ( self . path , uploadPercentage ) ) <EOL> self . lastUpload = current <EOL> key = bucket . new_key ( self . path ) <EOL> mimeType = mimetypes . guess_type ( self . path ) <EOL> if mimeType : <EOL> if type ( mimeType ) == tuple or type ( mimeType ) == list : <EOL> mimeType = mimeType [ <NUM_LIT:0> ] <EOL> key . content_type = mimeType <EOL> key . set_contents_from_string ( self . payload ( ) , headers , <EOL> policy = '<STR_LIT>' , <EOL> cb = progressCallback , <EOL> num_cb = progressCallbackCount ) <EOL> op1 = '<STR_LIT:+>' if changed else '<STR_LIT:->' <EOL> op2 = '<STR_LIT>' % ( fileSize ( len ( self . payload ( ) ) ) ) if self . shouldCompress ( ) else '<STR_LIT>' <EOL> logging . info ( '<STR_LIT>' % ( op1 , self . path , fileSize ( len ( self . data ( ) ) ) , op2 ) ) <EOL> return { '<STR_LIT>' : changed , '<STR_LIT:size>' : len ( self . payload ( ) ) } </s>
