<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> from hashlib import md5 , sha1 <EOL> import os <EOL> import shutil <EOL> from media_bundler . conf import bundler_settings <EOL> _bundle_versions = None <EOL> def get_bundle_versions ( ) : <EOL> global _bundle_versions <EOL> if not bundler_settings . BUNDLE_VERSION_FILE : <EOL> _bundle_versions = { } <EOL> if _bundle_versions is None : <EOL> update_versions ( ) <EOL> return _bundle_versions <EOL> def update_versions ( ) : <EOL> """<STR_LIT>""" <EOL> global _bundle_versions <EOL> vars = { } <EOL> try : <EOL> execfile ( bundler_settings . BUNDLE_VERSION_FILE , vars ) <EOL> except IOError : <EOL> _bundle_versions = { } <EOL> else : <EOL> _bundle_versions = vars [ '<STR_LIT>' ] <EOL> def write_versions ( versions ) : <EOL> global _bundle_versions <EOL> _bundle_versions = _bundle_versions . copy ( ) <EOL> _bundle_versions . update ( versions ) <EOL> with open ( bundler_settings . BUNDLE_VERSION_FILE , '<STR_LIT:w>' ) as output : <EOL> versions_str = '<STR_LIT:\n>' . join ( '<STR_LIT>' % ( name , vers ) <EOL> for ( name , vers ) in versions . iteritems ( ) ) <EOL> output . write ( '''<STR_LIT>''' % versions_str ) <EOL> class VersioningError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class VersioningBase ( object ) : <EOL> def __init__ ( self ) : <EOL> self . versions = get_bundle_versions ( ) . copy ( ) <EOL> def get_version ( self , source_files ) : <EOL> raise NotImplementedError <EOL> def update_bundle_version ( self , bundle ) : <EOL> version = self . get_version ( bundle ) <EOL> orig_path = bundle . get_bundle_path ( ) <EOL> dir , basename = os . path . split ( orig_path ) <EOL> if '<STR_LIT:.>' in basename : <EOL> name , _ , extension = basename . rpartition ( '<STR_LIT:.>' ) <EOL> versioned_basename = '<STR_LIT:.>' . join ( ( name , version , extension ) ) <EOL> else : <EOL> versioned_basename += '<STR_LIT:.>' + version <EOL> self . versions [ bundle . name ] = versioned_basename <EOL> versioned_path = os . path . join ( dir , versioned_basename ) <EOL> shutil . copy ( orig_path , versioned_path ) <EOL> class MtimeVersioning ( VersioningBase ) : <EOL> def get_version ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> return str ( max ( int ( os . stat ( f ) . st_mtime ) for f in bundle . get_paths ( ) ) ) <EOL> class HashVersioningBase ( VersioningBase ) : <EOL> def __init__ ( self , hash_method ) : <EOL> super ( HashVersioningBase , self ) . __init__ ( ) <EOL> self . hash_method = hash_method <EOL> def get_version ( self , bundle ) : <EOL> buf = open ( bundle . get_bundle_path ( ) ) <EOL> return self . get_hash ( buf ) <EOL> def get_hash ( self , f , chunk_size = <NUM_LIT:2> ** <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> m = self . hash_method ( ) <EOL> while <NUM_LIT:1> : <EOL> chunk = f . read ( chunk_size ) <EOL> if not chunk : <EOL> break <EOL> m . update ( chunk ) <EOL> return m . hexdigest ( ) <EOL> class Md5Versioning ( HashVersioningBase ) : <EOL> def __init__ ( self ) : <EOL> super ( Md5Versioning , self ) . __init__ ( md5 ) <EOL> class Sha1Versioning ( HashVersioningBase ) : <EOL> def __init__ ( self ) : <EOL> super ( Sha1Versioning , self ) . __init__ ( sha1 ) <EOL> VERSIONERS = { <EOL> '<STR_LIT>' : Sha1Versioning , <EOL> '<STR_LIT>' : Md5Versioning , <EOL> '<STR_LIT>' : MtimeVersioning , <EOL> } </s>
