<s> import sys <EOL> import os <EOL> import gzip <EOL> import zipfile <EOL> from optparse import make_option <EOL> from google . appengine . _internal . django . conf import settings <EOL> from google . appengine . _internal . django . core import serializers <EOL> from google . appengine . _internal . django . core . management . base import BaseCommand <EOL> from google . appengine . _internal . django . core . management . color import no_style <EOL> from google . appengine . _internal . django . db import connections , router , transaction , DEFAULT_DB_ALIAS <EOL> from google . appengine . _internal . django . db . models import get_apps <EOL> from google . appengine . _internal . django . utils . itercompat import product <EOL> try : <EOL> import bz2 <EOL> has_bz2 = True <EOL> except ImportError : <EOL> has_bz2 = False <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' <EOL> args = "<STR_LIT>" <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = DEFAULT_DB_ALIAS , help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ) <EOL> def handle ( self , * fixture_labels , ** options ) : <EOL> using = options . get ( '<STR_LIT>' , DEFAULT_DB_ALIAS ) <EOL> connection = connections [ using ] <EOL> self . style = no_style ( ) <EOL> verbosity = int ( options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> show_traceback = options . get ( '<STR_LIT>' , False ) <EOL> commit = options . get ( '<STR_LIT>' , True ) <EOL> fixture_count = <NUM_LIT:0> <EOL> loaded_object_count = <NUM_LIT:0> <EOL> fixture_object_count = <NUM_LIT:0> <EOL> models = set ( ) <EOL> humanize = lambda dirname : dirname and "<STR_LIT>" % dirname or '<STR_LIT>' <EOL> cursor = connection . cursor ( ) <EOL> if commit : <EOL> transaction . commit_unless_managed ( using = using ) <EOL> transaction . enter_transaction_management ( using = using ) <EOL> transaction . managed ( True , using = using ) <EOL> class SingleZipReader ( zipfile . ZipFile ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> zipfile . ZipFile . __init__ ( self , * args , ** kwargs ) <EOL> if settings . DEBUG : <EOL> assert len ( self . namelist ( ) ) == <NUM_LIT:1> , "<STR_LIT>" <EOL> def read ( self ) : <EOL> return zipfile . ZipFile . read ( self , self . namelist ( ) [ <NUM_LIT:0> ] ) <EOL> compression_types = { <EOL> None : file , <EOL> '<STR_LIT>' : gzip . GzipFile , <EOL> '<STR_LIT>' : SingleZipReader <EOL> } <EOL> if has_bz2 : <EOL> compression_types [ '<STR_LIT>' ] = bz2 . BZ2File <EOL> app_module_paths = [ ] <EOL> for app in get_apps ( ) : <EOL> if hasattr ( app , '<STR_LIT>' ) : <EOL> for path in app . __path__ : <EOL> app_module_paths . append ( path ) <EOL> else : <EOL> app_module_paths . append ( app . __file__ ) <EOL> app_fixtures = [ os . path . join ( os . path . dirname ( path ) , '<STR_LIT>' ) for path in app_module_paths ] <EOL> for fixture_label in fixture_labels : <EOL> parts = fixture_label . split ( '<STR_LIT:.>' ) <EOL> if len ( parts ) > <NUM_LIT:1> and parts [ - <NUM_LIT:1> ] in compression_types : <EOL> compression_formats = [ parts [ - <NUM_LIT:1> ] ] <EOL> parts = parts [ : - <NUM_LIT:1> ] <EOL> else : <EOL> compression_formats = compression_types . keys ( ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> fixture_name = parts [ <NUM_LIT:0> ] <EOL> formats = serializers . get_public_serializer_formats ( ) <EOL> else : <EOL> fixture_name , format = '<STR_LIT:.>' . join ( parts [ : - <NUM_LIT:1> ] ) , parts [ - <NUM_LIT:1> ] <EOL> if format in serializers . get_public_serializer_formats ( ) : <EOL> formats = [ format ] <EOL> else : <EOL> formats = [ ] <EOL> if formats : <EOL> if verbosity > <NUM_LIT:1> : <EOL> self . stdout . write ( "<STR_LIT>" % fixture_name ) <EOL> else : <EOL> self . stderr . write ( <EOL> self . style . ERROR ( "<STR_LIT>" % <EOL> ( fixture_name , format ) ) ) <EOL> if commit : <EOL> transaction . rollback ( using = using ) <EOL> transaction . leave_transaction_management ( using = using ) <EOL> return <EOL> if os . path . isabs ( fixture_name ) : <EOL> fixture_dirs = [ fixture_name ] <EOL> else : <EOL> fixture_dirs = app_fixtures + list ( settings . FIXTURE_DIRS ) + [ '<STR_LIT>' ] <EOL> for fixture_dir in fixture_dirs : <EOL> if verbosity > <NUM_LIT:1> : <EOL> self . stdout . write ( "<STR_LIT>" % humanize ( fixture_dir ) ) <EOL> label_found = False <EOL> for combo in product ( [ using , None ] , formats , compression_formats ) : <EOL> database , format , compression_format = combo <EOL> file_name = '<STR_LIT:.>' . join ( <EOL> p for p in [ <EOL> fixture_name , database , format , compression_format <EOL> ] <EOL> if p <EOL> ) <EOL> if verbosity > <NUM_LIT:1> : <EOL> self . stdout . write ( "<STR_LIT>" % ( humanize ( fixture_dir ) , file_name , fixture_name ) ) <EOL> full_path = os . path . join ( fixture_dir , file_name ) <EOL> open_method = compression_types [ compression_format ] <EOL> try : <EOL> fixture = open_method ( full_path , '<STR_LIT:r>' ) <EOL> if label_found : <EOL> fixture . close ( ) <EOL> self . stderr . write ( self . style . ERROR ( "<STR_LIT>" % <EOL> ( fixture_name , humanize ( fixture_dir ) ) ) ) <EOL> if commit : <EOL> transaction . rollback ( using = using ) <EOL> transaction . leave_transaction_management ( using = using ) <EOL> return <EOL> else : <EOL> fixture_count += <NUM_LIT:1> <EOL> objects_in_fixture = <NUM_LIT:0> <EOL> loaded_objects_in_fixture = <NUM_LIT:0> <EOL> if verbosity > <NUM_LIT:0> : <EOL> self . stdout . write ( "<STR_LIT>" % ( format , fixture_name , humanize ( fixture_dir ) ) ) <EOL> try : <EOL> objects = serializers . deserialize ( format , fixture , using = using ) <EOL> for obj in objects : <EOL> objects_in_fixture += <NUM_LIT:1> <EOL> if router . allow_syncdb ( using , obj . object . __class__ ) : <EOL> loaded_objects_in_fixture += <NUM_LIT:1> <EOL> models . add ( obj . object . __class__ ) <EOL> obj . save ( using = using ) <EOL> loaded_object_count += loaded_objects_in_fixture <EOL> fixture_object_count += objects_in_fixture <EOL> label_found = True <EOL> except ( SystemExit , KeyboardInterrupt ) : <EOL> raise <EOL> except Exception : <EOL> import traceback <EOL> fixture . close ( ) <EOL> if commit : <EOL> transaction . rollback ( using = using ) <EOL> transaction . leave_transaction_management ( using = using ) <EOL> if show_traceback : <EOL> traceback . print_exc ( ) <EOL> else : <EOL> self . stderr . write ( <EOL> self . style . ERROR ( "<STR_LIT>" % <EOL> ( full_path , '<STR_LIT>' . join ( traceback . format_exception ( sys . exc_type , <EOL> sys . exc_value , sys . exc_traceback ) ) ) ) ) <EOL> return <EOL> fixture . close ( ) <EOL> if objects_in_fixture == <NUM_LIT:0> : <EOL> self . stderr . write ( <EOL> self . style . ERROR ( "<STR_LIT>" % <EOL> ( fixture_name ) ) ) <EOL> if commit : <EOL> transaction . rollback ( using = using ) <EOL> transaction . leave_transaction_management ( using = using ) <EOL> return <EOL> except Exception , e : <EOL> if verbosity > <NUM_LIT:1> : <EOL> self . stdout . write ( "<STR_LIT>" % ( format , fixture_name , humanize ( fixture_dir ) ) ) <EOL> if loaded_object_count > <NUM_LIT:0> : <EOL> sequence_sql = connection . ops . sequence_reset_sql ( self . style , models ) <EOL> if sequence_sql : <EOL> if verbosity > <NUM_LIT:1> : <EOL> self . stdout . write ( "<STR_LIT>" ) <EOL> for line in sequence_sql : <EOL> cursor . execute ( line ) <EOL> if commit : <EOL> transaction . commit ( using = using ) <EOL> transaction . leave_transaction_management ( using = using ) <EOL> if fixture_object_count == <NUM_LIT:0> : <EOL> if verbosity > <NUM_LIT:0> : <EOL> self . stdout . write ( "<STR_LIT>" ) <EOL> else : <EOL> if verbosity > <NUM_LIT:0> : <EOL> if fixture_object_count == loaded_object_count : <EOL> self . stdout . write ( "<STR_LIT>" % ( <EOL> loaded_object_count , fixture_count ) ) <EOL> else : <EOL> self . stdout . write ( "<STR_LIT>" % ( <EOL> loaded_object_count , fixture_object_count , fixture_count ) ) <EOL> if commit : <EOL> connection . close ( ) </s>
