<s> import os <EOL> import sys <EOL> from django . contrib . staticfiles . management . commands . collectstatic import Command as CollectStatic <EOL> from django . core . management . base import CommandError <EOL> from django . contrib . staticfiles import finders <EOL> from django . utils . datastructures import SortedDict <EOL> from hashlib import md5 <EOL> from optparse import make_option <EOL> from . . utils import StorageOverrideMixin <EOL> class CollectNewMixin ( object ) : <EOL> comparison_method_aliases = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . option_list = list ( self . option_list ) + [ <EOL> make_option ( '<STR_LIT>' , default = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , default = '<STR_LIT:default>' , <EOL> action = "<STR_LIT:store>" , dest = '<STR_LIT>' , type = "<STR_LIT:string>" , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ] <EOL> super ( CollectNewMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> def collect ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . symlink : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> raise CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" % sys . platform ) <EOL> if not self . local : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> if self . clear : <EOL> self . clear_dir ( '<STR_LIT>' ) <EOL> handler = self . _get_handler ( ) <EOL> do_post_process = self . post_process and hasattr ( self . storage , '<STR_LIT>' ) <EOL> found_files = SortedDict ( ) <EOL> for finder in finders . get_finders ( ) : <EOL> for path , storage in finder . list ( self . ignore_patterns ) : <EOL> if getattr ( storage , '<STR_LIT>' , None ) : <EOL> prefixed_path = os . path . join ( storage . prefix , path ) <EOL> else : <EOL> prefixed_path = path <EOL> if prefixed_path not in found_files : <EOL> found_files [ prefixed_path ] = ( storage , path ) <EOL> handler ( path , prefixed_path , storage ) <EOL> if self . progressive_post_process and do_post_process : <EOL> try : <EOL> self . _post_process ( <EOL> { prefixed_path : ( storage , path ) } , <EOL> self . dry_run ) <EOL> except ValueError as e : <EOL> message = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % e ) <EOL> raise ValueError ( message ) <EOL> if not self . progressive_post_process and do_post_process : <EOL> self . _post_process ( found_files , self . dry_run ) <EOL> return { <EOL> '<STR_LIT>' : self . copied_files + self . symlinked_files , <EOL> '<STR_LIT>' : self . unmodified_files , <EOL> '<STR_LIT>' : self . post_processed_files , <EOL> } <EOL> def set_options ( self , ** options ) : <EOL> super ( CollectNewMixin , self ) . set_options ( ** options ) <EOL> comparison_method = options . get ( '<STR_LIT>' ) <EOL> self . comparison_method = self . comparison_method_aliases . get ( comparison_method , comparison_method ) <EOL> pp = options . get ( '<STR_LIT>' ) <EOL> if pp == '<STR_LIT:default>' : <EOL> self . progressive_post_process = False <EOL> elif pp == '<STR_LIT>' : <EOL> self . progressive_post_process = True <EOL> else : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> def delete_file ( self , path , prefixed_path , source_storage ) : <EOL> if self . comparison_method == '<STR_LIT>' : <EOL> return CollectStatic . delete_file ( self , path , prefixed_path , source_storage ) <EOL> elif self . storage . exists ( prefixed_path ) : <EOL> should_delete = self . compare ( path , prefixed_path , source_storage ) <EOL> if should_delete : <EOL> if self . dry_run : <EOL> self . log ( u"<STR_LIT>" % path ) <EOL> else : <EOL> self . log ( u"<STR_LIT>" % path ) <EOL> self . storage . delete ( prefixed_path ) <EOL> else : <EOL> self . log ( u"<STR_LIT>" % path ) <EOL> return False <EOL> return True <EOL> def compare ( self , path , prefixed_path , source_storage ) : <EOL> """<STR_LIT>""" <EOL> comparitor = getattr ( self , '<STR_LIT>' % self . comparison_method , None ) <EOL> if not comparitor : <EOL> comparitor = self . _create_comparitor ( self . comparison_method ) <EOL> return comparitor ( path , prefixed_path , source_storage ) <EOL> def _create_comparitor ( self , comparison_method ) : <EOL> def comparitor ( path , prefixed_path , source_storage ) : <EOL> source_fn = getattr ( source_storage , comparison_method ) <EOL> dest_fn = getattr ( self . storage , comparison_method ) <EOL> source_value = source_fn ( path ) <EOL> dest_value = dest_fn ( path ) <EOL> return source_value == dest_value <EOL> return comparitor <EOL> def compare_file_hash ( self , path , prefixed_path , source_storage ) : <EOL> old_md5 = self . _get_md5 ( self . storage , path ) <EOL> new_md5 = self . _get_md5 ( source_storage , prefixed_path ) <EOL> return old_md5 != new_md5 <EOL> def _get_md5 ( self , storage , name ) : <EOL> fn = getattr ( storage , '<STR_LIT>' , None ) <EOL> if fn : <EOL> return fn ( name ) <EOL> else : <EOL> file = storage . open ( name ) <EOL> contents = file . read ( ) <EOL> return md5 ( contents ) . hexdigest ( ) <EOL> def _post_process ( self , found_files , dry_run ) : <EOL> processor = self . storage . post_process ( found_files , dry_run = dry_run ) <EOL> for original_path , processed_path , processed in processor : <EOL> if processed : <EOL> self . log ( u"<STR_LIT>" % <EOL> ( original_path , processed_path ) , level = <NUM_LIT:1> ) <EOL> self . post_processed_files . append ( original_path ) <EOL> else : <EOL> self . log ( u"<STR_LIT>" % original_path ) <EOL> def _get_handler ( self ) : <EOL> return self . link_file if self . symlink else self . copy_file <EOL> class Command ( StorageOverrideMixin , CollectNewMixin , CollectStatic ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
