<s> import base64 <EOL> import cStringIO <EOL> import gzip <EOL> import hashlib <EOL> import mimetypes <EOL> import os <EOL> JS_MIMETYPES = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> CSS_MIMETYPES = ( <EOL> "<STR_LIT>" , <EOL> ) <EOL> TYPES_TO_COMPRESS = ( <EOL> "<STR_LIT:application/json>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) + JS_MIMETYPES + CSS_MIMETYPES <EOL> class SyncException ( Exception ) : <EOL> pass <EOL> def checksum ( data ) : <EOL> checksum = hashlib . md5 ( data ) <EOL> hexdigest = checksum . hexdigest ( ) <EOL> b64digest = base64 . b64encode ( checksum . digest ( ) ) <EOL> return ( hexdigest , b64digest ) <EOL> def compress ( s ) : <EOL> zbuf = cStringIO . StringIO ( ) <EOL> zfile = gzip . GzipFile ( mode = '<STR_LIT:wb>' , compresslevel = <NUM_LIT:6> , fileobj = zbuf ) <EOL> zfile . write ( s ) <EOL> zfile . close ( ) <EOL> return zbuf . getvalue ( ) <EOL> def is_syncable_dir ( dir_str ) : <EOL> return not dir_str . startswith ( '<STR_LIT:.>' ) and not dir_str . startswith ( '<STR_LIT:_>' ) <EOL> def is_syncable_file ( file_str ) : <EOL> return not file_str . startswith ( '<STR_LIT:.>' ) and not file_str . startswith ( '<STR_LIT:_>' ) <EOL> def listdir_recursive ( dir_str ) : <EOL> for root , dirs , files in os . walk ( dir_str ) : <EOL> for dir_candidate in dirs : <EOL> if not is_syncable_dir ( dir_candidate ) : <EOL> dirs . remove ( dir_candidate ) <EOL> basename = os . path . basename ( root ) <EOL> if is_syncable_dir ( basename ) : <EOL> for file in files : <EOL> fname = os . path . join ( root , file ) . replace ( dir_str , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if fname . startswith ( '<STR_LIT:/>' ) : <EOL> fname = fname [ <NUM_LIT:1> : ] <EOL> yield fname <EOL> else : <EOL> pass <EOL> def combine_files ( joinfile , sourcefiles , client ) : <EOL> """<STR_LIT>""" <EOL> from mediasync . conf import msettings <EOL> joinfile = joinfile . strip ( '<STR_LIT:/>' ) <EOL> if joinfile . endswith ( '<STR_LIT>' ) : <EOL> dirname = msettings [ '<STR_LIT>' ] . strip ( '<STR_LIT:/>' ) <EOL> separator = '<STR_LIT:\n>' <EOL> elif joinfile . endswith ( '<STR_LIT>' ) : <EOL> dirname = msettings [ '<STR_LIT>' ] . strip ( '<STR_LIT:/>' ) <EOL> separator = '<STR_LIT>' <EOL> else : <EOL> return None <EOL> buffer = cStringIO . StringIO ( ) <EOL> for sourcefile in sourcefiles : <EOL> sourcepath = os . path . join ( client . media_root , dirname , sourcefile ) <EOL> if os . path . isfile ( sourcepath ) : <EOL> f = open ( sourcepath ) <EOL> buffer . write ( f . read ( ) ) <EOL> f . close ( ) <EOL> buffer . write ( separator ) <EOL> filedata = buffer . getvalue ( ) <EOL> buffer . close ( ) <EOL> return ( filedata , dirname ) <EOL> def sync ( client = None , force = False , verbose = True ) : <EOL> """<STR_LIT>""" <EOL> from mediasync import backends <EOL> from mediasync . conf import msettings <EOL> from mediasync . signals import pre_sync , post_sync <EOL> if client is None : <EOL> client = backends . client ( ) <EOL> client . open ( ) <EOL> client . serve_remote = True <EOL> pre_sync . send ( sender = client ) <EOL> for joinfile , sourcefiles in msettings [ '<STR_LIT>' ] . iteritems ( ) : <EOL> filedata = combine_files ( joinfile , sourcefiles , client ) <EOL> if filedata is None : <EOL> continue <EOL> filedata , dirname = filedata <EOL> content_type = mimetypes . guess_type ( joinfile ) [ <NUM_LIT:0> ] or msettings [ '<STR_LIT>' ] <EOL> remote_path = joinfile <EOL> if dirname : <EOL> remote_path = "<STR_LIT>" % ( dirname , remote_path ) <EOL> if client . process_and_put ( filedata , content_type , remote_path , force = force ) : <EOL> if verbose : <EOL> print "<STR_LIT>" % ( content_type , remote_path ) <EOL> for dirname in os . listdir ( client . media_root ) : <EOL> dirpath = os . path . abspath ( os . path . join ( client . media_root , dirname ) ) <EOL> if os . path . isdir ( dirpath ) : <EOL> for filename in listdir_recursive ( dirpath ) : <EOL> filepath = os . path . join ( dirpath , filename ) <EOL> remote_path = "<STR_LIT>" % ( dirname , filename ) <EOL> content_type = mimetypes . guess_type ( filepath ) [ <NUM_LIT:0> ] or msettings [ '<STR_LIT>' ] <EOL> if not is_syncable_file ( os . path . basename ( filename ) ) or not os . path . isfile ( filepath ) : <EOL> continue <EOL> filedata = open ( filepath , '<STR_LIT:rb>' ) . read ( ) <EOL> if client . process_and_put ( filedata , content_type , remote_path , force = force ) : <EOL> if verbose : <EOL> print "<STR_LIT>" % ( content_type , remote_path ) <EOL> post_sync . send ( sender = client ) <EOL> client . close ( ) <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> __version__ = '<STR_LIT>' </s>
