<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import datetime <EOL> import glob <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> from nova . i18n import _ <EOL> def dump_db ( db_driver , db_name , db_url , migration_version , dump_filename ) : <EOL> if not db_url . endswith ( '<STR_LIT:/>' ) : <EOL> db_url += '<STR_LIT:/>' <EOL> db_url += db_name <EOL> db_driver . create ( db_name ) <EOL> try : <EOL> _migrate ( db_url , migration_version ) <EOL> db_driver . dump ( db_name , dump_filename ) <EOL> finally : <EOL> db_driver . drop ( db_name ) <EOL> def diff_files ( filename1 , filename2 ) : <EOL> pipeline = [ '<STR_LIT>' <EOL> % { '<STR_LIT>' : filename1 , '<STR_LIT>' : filename2 } ] <EOL> if subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' ] ) == <NUM_LIT:0> : <EOL> pipeline . append ( '<STR_LIT>' ) <EOL> pipeline . append ( '<STR_LIT>' ) <EOL> cmd = '<STR_LIT>' . join ( pipeline ) <EOL> subprocess . check_call ( cmd , shell = True ) <EOL> class Mysql ( object ) : <EOL> def create ( self , name ) : <EOL> subprocess . check_call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:root>' , '<STR_LIT>' , name ] ) <EOL> def drop ( self , name ) : <EOL> subprocess . check_call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:root>' , '<STR_LIT>' , name ] ) <EOL> def dump ( self , name , dump_filename ) : <EOL> subprocess . check_call ( <EOL> '<STR_LIT>' <EOL> % { '<STR_LIT:name>' : name , '<STR_LIT>' : dump_filename } , <EOL> shell = True ) <EOL> class Postgresql ( object ) : <EOL> def create ( self , name ) : <EOL> subprocess . check_call ( [ '<STR_LIT>' , name ] ) <EOL> def drop ( self , name ) : <EOL> subprocess . check_call ( [ '<STR_LIT>' , name ] ) <EOL> def dump ( self , name , dump_filename ) : <EOL> subprocess . check_call ( <EOL> '<STR_LIT>' <EOL> % { '<STR_LIT:name>' : name , '<STR_LIT>' : dump_filename } , <EOL> shell = True ) <EOL> class Ibm_db_sa ( object ) : <EOL> @ classmethod <EOL> def db2cmd ( cls , cmd ) : <EOL> """<STR_LIT>""" <EOL> subprocess . check_call ( '<STR_LIT>' % cmd , shell = True ) <EOL> def create ( self , name ) : <EOL> self . db2cmd ( '<STR_LIT>' % name ) <EOL> def drop ( self , name ) : <EOL> self . db2cmd ( '<STR_LIT>' % name ) <EOL> def dump ( self , name , dump_filename ) : <EOL> self . db2cmd ( '<STR_LIT>' % <EOL> { '<STR_LIT:name>' : name , '<STR_LIT>' : dump_filename } ) <EOL> subprocess . check_call ( '<STR_LIT>' % dump_filename , <EOL> shell = True ) <EOL> def _get_db_driver_class ( db_url ) : <EOL> try : <EOL> return globals ( ) [ db_url . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . capitalize ( ) ] <EOL> except KeyError : <EOL> raise Exception ( _ ( "<STR_LIT>" ) % db_url ) <EOL> MIGRATE_REPO = os . path . join ( os . getcwd ( ) , "<STR_LIT>" ) <EOL> def _migrate ( db_url , migration_version ) : <EOL> earliest_version = _migrate_get_earliest_version ( ) <EOL> _migrate_cmd ( <EOL> db_url , '<STR_LIT>' , str ( earliest_version - <NUM_LIT:1> ) ) <EOL> upgrade_cmd = [ '<STR_LIT>' ] <EOL> if migration_version != '<STR_LIT>' : <EOL> upgrade_cmd . append ( str ( migration_version ) ) <EOL> _migrate_cmd ( db_url , * upgrade_cmd ) <EOL> def _migrate_cmd ( db_url , * cmd ) : <EOL> manage_py = os . path . join ( MIGRATE_REPO , '<STR_LIT>' ) <EOL> args = [ '<STR_LIT>' , manage_py ] <EOL> args += cmd <EOL> args += [ '<STR_LIT>' % MIGRATE_REPO , <EOL> '<STR_LIT>' % db_url ] <EOL> subprocess . check_call ( args ) <EOL> def _migrate_get_earliest_version ( ) : <EOL> versions_glob = os . path . join ( MIGRATE_REPO , '<STR_LIT>' , '<STR_LIT>' ) <EOL> versions = [ ] <EOL> for path in glob . iglob ( versions_glob ) : <EOL> filename = os . path . basename ( path ) <EOL> prefix = filename . split ( '<STR_LIT:_>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> try : <EOL> version = int ( prefix ) <EOL> except ValueError : <EOL> pass <EOL> versions . append ( version ) <EOL> versions . sort ( ) <EOL> return versions [ <NUM_LIT:0> ] <EOL> def git_current_branch_name ( ) : <EOL> ref_name = git_symbolic_ref ( '<STR_LIT>' , quiet = True ) <EOL> current_branch_name = ref_name . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return current_branch_name <EOL> def git_symbolic_ref ( ref , quiet = False ) : <EOL> args = [ '<STR_LIT>' , '<STR_LIT>' , ref ] <EOL> if quiet : <EOL> args . append ( '<STR_LIT>' ) <EOL> proc = subprocess . Popen ( args , stdout = subprocess . PIPE ) <EOL> stdout , stderr = proc . communicate ( ) <EOL> return stdout . strip ( ) <EOL> def git_checkout ( branch_name ) : <EOL> subprocess . check_call ( [ '<STR_LIT>' , '<STR_LIT>' , branch_name ] ) <EOL> def git_has_uncommited_changes ( ) : <EOL> return subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) == <NUM_LIT:1> <EOL> def die ( msg ) : <EOL> print ( "<STR_LIT>" % msg , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def usage ( msg = None ) : <EOL> if msg : <EOL> print ( "<STR_LIT>" % msg , file = sys . stderr ) <EOL> prog = "<STR_LIT>" <EOL> args = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> print ( "<STR_LIT>" % ( prog , '<STR_LIT:U+0020>' . join ( args ) ) , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def parse_options ( ) : <EOL> try : <EOL> db_url = sys . argv [ <NUM_LIT:1> ] <EOL> except IndexError : <EOL> usage ( "<STR_LIT>" ) <EOL> try : <EOL> orig_branch , orig_version = sys . argv [ <NUM_LIT:2> ] . split ( '<STR_LIT::>' ) <EOL> except IndexError : <EOL> usage ( '<STR_LIT>' ) <EOL> try : <EOL> new_branch , new_version = sys . argv [ <NUM_LIT:3> ] . split ( '<STR_LIT::>' ) <EOL> except IndexError : <EOL> usage ( '<STR_LIT>' ) <EOL> return db_url , orig_branch , orig_version , new_branch , new_version <EOL> def main ( ) : <EOL> timestamp = datetime . datetime . utcnow ( ) . strftime ( "<STR_LIT>" ) <EOL> ORIG_DB = '<STR_LIT>' % timestamp <EOL> NEW_DB = '<STR_LIT>' % timestamp <EOL> ORIG_DUMP = ORIG_DB + "<STR_LIT>" <EOL> NEW_DUMP = NEW_DB + "<STR_LIT>" <EOL> options = parse_options ( ) <EOL> db_url , orig_branch , orig_version , new_branch , new_version = options <EOL> if git_has_uncommited_changes ( ) : <EOL> die ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> db_driver = _get_db_driver_class ( db_url ) ( ) <EOL> users_branch = git_current_branch_name ( ) <EOL> git_checkout ( orig_branch ) <EOL> try : <EOL> dump_db ( db_driver , ORIG_DB , db_url , orig_version , ORIG_DUMP ) <EOL> git_checkout ( new_branch ) <EOL> dump_db ( db_driver , NEW_DB , db_url , new_version , NEW_DUMP ) <EOL> diff_files ( ORIG_DUMP , NEW_DUMP ) <EOL> finally : <EOL> git_checkout ( users_branch ) <EOL> if os . path . exists ( ORIG_DUMP ) : <EOL> os . unlink ( ORIG_DUMP ) <EOL> if os . path . exists ( NEW_DUMP ) : <EOL> os . unlink ( NEW_DUMP ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
