<s> from collections import namedtuple <EOL> import logging <EOL> from optparse import make_option <EOL> from django . core . management import BaseCommand <EOL> from corehq . apps . app_manager . models import Application <EOL> from corehq . util . couch import iter_update , DocUpdate <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . setLevel ( '<STR_LIT>' ) <EOL> def get_all_app_ids ( include_builds = False ) : <EOL> key = [ None ] <EOL> if not include_builds : <EOL> key += [ None ] <EOL> return { r [ '<STR_LIT:id>' ] for r in Application . get_db ( ) . view ( <EOL> '<STR_LIT>' , <EOL> startkey = key , <EOL> endkey = key + [ { } ] , <EOL> reduce = False , <EOL> ) . all ( ) } <EOL> SaveError = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class AppMigrationCommandBase ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> include_builds = False <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle ( self , * args , ** options ) : <EOL> self . options = options <EOL> app_ids = self . get_app_ids ( ) <EOL> logger . info ( '<STR_LIT>' . format ( len ( app_ids ) ) ) <EOL> results = iter_update ( Application . get_db ( ) , self . _migrate_app , app_ids , verbose = True ) <EOL> self . results_callback ( results ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> def _migrate_app ( self , app_doc ) : <EOL> try : <EOL> if app_doc [ "<STR_LIT>" ] in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> migrated_app = self . migrate_app ( app_doc ) <EOL> if migrated_app : <EOL> return DocUpdate ( migrated_app ) <EOL> except Exception , e : <EOL> logger . exception ( "<STR_LIT>" . format ( id = app_doc [ '<STR_LIT>' ] ) ) <EOL> if self . options [ '<STR_LIT>' ] : <EOL> raise e <EOL> def get_app_ids ( self ) : <EOL> return get_all_app_ids ( self . include_builds ) <EOL> def migrate_app ( self , app ) : <EOL> raise NotImplementedError ( ) <EOL> def results_callback ( self , results ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
