<s> import os <EOL> import sys <EOL> import shutil <EOL> from optparse import make_option <EOL> from django . conf import settings <EOL> from django . core . files . storage import get_storage_class <EOL> from django . core . management . base import CommandError , NoArgsCommand <EOL> from django . utils . encoding import smart_str <EOL> from django . contrib . staticfiles import finders <EOL> class Command ( NoArgsCommand ) : <EOL> """<STR_LIT>""" <EOL> option_list = NoArgsCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = True , help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , default = [ ] , <EOL> dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , default = True , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ) <EOL> help = "<STR_LIT>" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( NoArgsCommand , self ) . __init__ ( * args , ** kwargs ) <EOL> self . copied_files = [ ] <EOL> self . symlinked_files = [ ] <EOL> self . unmodified_files = [ ] <EOL> self . storage = get_storage_class ( settings . STATICFILES_STORAGE ) ( ) <EOL> try : <EOL> self . storage . path ( '<STR_LIT>' ) <EOL> except NotImplementedError : <EOL> self . local = False <EOL> else : <EOL> self . local = True <EOL> os . stat_float_times ( False ) <EOL> def handle_noargs ( self , ** options ) : <EOL> symlink = options [ '<STR_LIT>' ] <EOL> ignore_patterns = options [ '<STR_LIT>' ] <EOL> if options [ '<STR_LIT>' ] : <EOL> ignore_patterns += [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ignore_patterns = list ( set ( ignore_patterns ) ) <EOL> self . verbosity = int ( options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if 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 options . get ( '<STR_LIT>' ) : <EOL> confirm = raw_input ( u"""<STR_LIT>""" ) <EOL> if confirm != '<STR_LIT:yes>' : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> for finder in finders . get_finders ( ) : <EOL> for path , storage in finder . list ( 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 symlink : <EOL> self . link_file ( path , prefixed_path , storage , ** options ) <EOL> else : <EOL> self . copy_file ( path , prefixed_path , storage , ** options ) <EOL> actual_count = len ( self . copied_files ) + len ( self . symlinked_files ) <EOL> unmodified_count = len ( self . unmodified_files ) <EOL> if self . verbosity >= <NUM_LIT:1> : <EOL> self . stdout . write ( smart_str ( u"<STR_LIT>" <EOL> % ( actual_count , actual_count != <NUM_LIT:1> and '<STR_LIT:s>' or '<STR_LIT>' , <EOL> symlink and '<STR_LIT>' or '<STR_LIT>' , <EOL> settings . STATIC_ROOT , <EOL> unmodified_count and '<STR_LIT>' <EOL> % unmodified_count or '<STR_LIT>' ) ) ) <EOL> def log ( self , msg , level = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> msg = smart_str ( msg ) <EOL> if not msg . endswith ( "<STR_LIT:\n>" ) : <EOL> msg += "<STR_LIT:\n>" <EOL> if self . verbosity >= level : <EOL> self . stdout . write ( msg ) <EOL> def delete_file ( self , path , prefixed_path , source_storage , ** options ) : <EOL> symlink = options [ '<STR_LIT>' ] <EOL> if self . storage . exists ( prefixed_path ) : <EOL> try : <EOL> target_last_modified = self . storage . modified_time ( prefixed_path ) <EOL> except ( OSError , NotImplementedError ) : <EOL> pass <EOL> else : <EOL> try : <EOL> source_last_modified = source_storage . modified_time ( path ) <EOL> except ( OSError , NotImplementedError ) : <EOL> pass <EOL> else : <EOL> if self . local : <EOL> full_path = self . storage . path ( prefixed_path ) <EOL> else : <EOL> full_path = None <EOL> if target_last_modified >= source_last_modified : <EOL> if not ( ( symlink and full_path and not os . path . islink ( full_path ) ) or <EOL> ( not symlink and full_path and os . path . islink ( full_path ) ) ) : <EOL> if prefixed_path not in self . unmodified_files : <EOL> self . unmodified_files . append ( prefixed_path ) <EOL> self . log ( u"<STR_LIT>" % path ) <EOL> return False <EOL> if options [ '<STR_LIT>' ] : <EOL> self . log ( u"<STR_LIT>" % path ) <EOL> else : <EOL> self . log ( u"<STR_LIT>" % path ) <EOL> self . storage . delete ( prefixed_path ) <EOL> return True <EOL> def link_file ( self , path , prefixed_path , source_storage , ** options ) : <EOL> """<STR_LIT>""" <EOL> if prefixed_path in self . symlinked_files : <EOL> return self . log ( u"<STR_LIT>" % path ) <EOL> if not self . delete_file ( path , prefixed_path , source_storage , ** options ) : <EOL> return <EOL> source_path = source_storage . path ( path ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . log ( u"<STR_LIT>" % source_path , level = <NUM_LIT:1> ) <EOL> else : <EOL> self . log ( u"<STR_LIT>" % source_path , level = <NUM_LIT:1> ) <EOL> full_path = self . storage . path ( prefixed_path ) <EOL> try : <EOL> os . makedirs ( os . path . dirname ( full_path ) ) <EOL> except OSError : <EOL> pass <EOL> os . symlink ( source_path , full_path ) <EOL> if prefixed_path not in self . symlinked_files : <EOL> self . symlinked_files . append ( prefixed_path ) <EOL> def copy_file ( self , path , prefixed_path , source_storage , ** options ) : <EOL> """<STR_LIT>""" <EOL> if prefixed_path in self . copied_files : <EOL> return self . log ( u"<STR_LIT>" % path ) <EOL> if not self . delete_file ( path , prefixed_path , source_storage , ** options ) : <EOL> return <EOL> source_path = source_storage . path ( path ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . log ( u"<STR_LIT>" % source_path , level = <NUM_LIT:1> ) <EOL> else : <EOL> self . log ( u"<STR_LIT>" % source_path , level = <NUM_LIT:1> ) <EOL> if self . local : <EOL> full_path = self . storage . path ( prefixed_path ) <EOL> try : <EOL> os . makedirs ( os . path . dirname ( full_path ) ) <EOL> except OSError : <EOL> pass <EOL> shutil . copy2 ( source_path , full_path ) <EOL> else : <EOL> source_file = source_storage . open ( path ) <EOL> self . storage . save ( prefixed_path , source_file ) <EOL> if not prefixed_path in self . copied_files : <EOL> self . copied_files . append ( prefixed_path ) </s>
