<s> """<STR_LIT>""" <EOL> from __future__ import ( absolute_import , division , <EOL> print_function , unicode_literals ) <EOL> import os <EOL> import sys <EOL> import warnings <EOL> from optparse import make_option <EOL> from django . conf import settings <EOL> from django . core . management . base import CommandError <EOL> from django . utils import six <EOL> from django . db import connection <EOL> from . _base import BaseDbBackupCommand <EOL> from ... import utils <EOL> from ... dbcommands import DBCommands , MongoDBCommands <EOL> from ... storage . base import BaseStorage , StorageError <EOL> input = raw_input if six . PY2 else input <EOL> class Command ( BaseDbBackupCommand ) : <EOL> help = """<STR_LIT>""" <EOL> option_list = BaseDbBackupCommand . option_list + ( <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , action = '<STR_LIT:store_true>' , default = False , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT:-c>" , "<STR_LIT>" , help = "<STR_LIT>" , default = False , action = '<STR_LIT:store_true>' ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , default = None ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , action = '<STR_LIT:store_true>' ) , <EOL> ) <EOL> def handle ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> self . verbosity = int ( options . get ( '<STR_LIT>' ) ) <EOL> self . quiet = options . get ( '<STR_LIT>' ) <EOL> try : <EOL> connection . close ( ) <EOL> self . filename = options . get ( '<STR_LIT>' ) <EOL> self . path = options . get ( '<STR_LIT>' ) <EOL> self . backup_extension = options . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> self . servername = options . get ( '<STR_LIT>' ) <EOL> self . decrypt = options . get ( '<STR_LIT>' ) <EOL> self . uncompress = options . get ( '<STR_LIT>' ) <EOL> self . passphrase = options . get ( '<STR_LIT>' ) <EOL> self . interactive = options . get ( '<STR_LIT>' ) <EOL> self . database = self . _get_database ( options ) <EOL> self . storage = BaseStorage . storage_factory ( ) <EOL> self . database = self . _get_database ( options ) <EOL> if '<STR_LIT>' in self . database [ '<STR_LIT>' ] : <EOL> self . dbcommands = MongoDBCommands ( self . database ) <EOL> else : <EOL> self . dbcommands = DBCommands ( self . database ) <EOL> if not self . backup_extension : <EOL> self . backup_extension = self . dbcommands . settings . extension or '<STR_LIT>' <EOL> if options . get ( '<STR_LIT:list>' ) : <EOL> return self . _list_backups ( ) <EOL> self . _restore_backup ( ) <EOL> except StorageError as err : <EOL> raise CommandError ( err ) <EOL> def _get_database ( self , options ) : <EOL> """<STR_LIT>""" <EOL> database_key = options . get ( '<STR_LIT>' ) <EOL> if not database_key : <EOL> if len ( settings . DATABASES ) >= <NUM_LIT:2> : <EOL> errmsg = "<STR_LIT>" "<STR_LIT>" <EOL> raise CommandError ( errmsg ) <EOL> database_key = list ( settings . DATABASES . keys ( ) ) [ <NUM_LIT:0> ] <EOL> return settings . DATABASES [ database_key ] <EOL> def _restore_backup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logger . info ( "<STR_LIT>" , self . database [ '<STR_LIT>' ] ) <EOL> if self . path : <EOL> input_filename = self . path <EOL> input_file = self . read_local_file ( self . path ) <EOL> else : <EOL> if self . filename : <EOL> input_filename = self . filename <EOL> else : <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> try : <EOL> input_filename = self . storage . get_latest_backup ( encrypted = self . decrypt , <EOL> compressed = self . uncompress ) <EOL> except StorageError as err : <EOL> raise CommandError ( err . args [ <NUM_LIT:0> ] ) <EOL> input_file = self . storage . read_file ( input_filename ) <EOL> self . logger . info ( "<STR_LIT>" % input_filename ) <EOL> if self . decrypt : <EOL> unencrypted_file , input_filename = utils . unencrypt_file ( input_file , input_filename , <EOL> self . passphrase ) <EOL> input_file . close ( ) <EOL> input_file = unencrypted_file <EOL> if self . uncompress : <EOL> uncompressed_file , input_filename = utils . uncompress_file ( input_file , input_filename ) <EOL> input_file . close ( ) <EOL> input_file = uncompressed_file <EOL> self . logger . info ( "<STR_LIT>" , utils . handle_size ( input_file ) ) <EOL> if self . interactive : <EOL> answer = input ( "<STR_LIT>" ) <EOL> if answer . lower ( ) . startswith ( '<STR_LIT:n>' ) : <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> input_file . seek ( <NUM_LIT:0> ) <EOL> self . dbcommands . run_restore_commands ( input_file ) <EOL> def _list_backups ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> warnings . warn ( msg , DeprecationWarning ) <EOL> self . logger . info ( "<STR_LIT>" , self . storage . name , self . storage . backup_dir ) <EOL> for filepath in self . storage . list_directory ( ) : <EOL> self . logger . info ( "<STR_LIT>" , os . path . basename ( filepath ) ) <EOL> def read_local_file ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return open ( path , '<STR_LIT:rb>' ) </s>
