#NEWLINE# @ contextmanager #NEWLINE# def temporary_migration_module( self , app_label = 'migrations' , module = None ) : #NEWLINE# #INDENT# "\n        Allows testing management commands in a temporary migrations module.\n\n        Wrap all invocations to makemigrations and squashmigrations with this\n        context manager in order to avoid creating migration files in your\n        source tree inadvertently.\n\n        Takes the application label that will be passed to makemigrations or\n        squashmigrations and the Python path to a migrations module.\n\n        The migrations module is used as a template for creating the temporary\n        migrations module. If it isn't provided, the application's migrations\n        module is used, if it exists.\n\n        Returns the filesystem path to the temporary migrations module.\n        " #NEWLINE# temp_dir = tempfile . mkdtemp ( ) #NEWLINE# try : #NEWLINE# #INDENT# target_dir = tempfile . mkdtemp ( dir = temp_dir ) #NEWLINE# with open ( os . path . join ( target_dir , '__init__.py' ) , 'w' ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# target_migrations_dir = os . path . join ( target_dir , 'migrations' ) #NEWLINE# if ( module is None ) : #NEWLINE# #INDENT# module = ( apps . get_app_config ( app_label ) . name + '.migrations' ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# source_migrations_dir = module_dir ( import_module ( module ) ) #NEWLINE# #UNINDENT# except ( ImportError , ValueError ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# shutil . copytree ( source_migrations_dir , target_migrations_dir ) #NEWLINE# #UNINDENT# with extend_sys_path ( temp_dir ) : #NEWLINE# #INDENT# new_module = ( os . path . basename ( target_dir ) + '.migrations' ) #NEWLINE# with self . settings ( MIGRATION_MODULES = { #NEWLINE# #INDENT# app_label : new_module , #NEWLINE# #UNINDENT# } ) : #NEWLINE# #INDENT# ( yield self ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# shutil . rmtree ( temp_dir )