<s> from __future__ import unicode_literals <EOL> import os <EOL> import re <EOL> from importlib import import_module <EOL> from django import get_version <EOL> from django . apps import apps <EOL> from django . db import migrations <EOL> from django . db . migrations . loader import MigrationLoader <EOL> from django . db . migrations . serializer import serializer_factory <EOL> from django . utils . _os import upath <EOL> from django . utils . encoding import force_text <EOL> from django . utils . inspect import get_func_args <EOL> from django . utils . module_loading import module_dir <EOL> from django . utils . timezone import now <EOL> try : <EOL> import enum <EOL> except ImportError : <EOL> enum = None <EOL> class SettingsReference ( str ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( self , value , setting_name ) : <EOL> return str . __new__ ( self , value ) <EOL> def __init__ ( self , value , setting_name ) : <EOL> self . setting_name = setting_name <EOL> class OperationWriter ( object ) : <EOL> def __init__ ( self , operation , indentation = <NUM_LIT:2> ) : <EOL> self . operation = operation <EOL> self . buff = [ ] <EOL> self . indentation = indentation <EOL> def serialize ( self ) : <EOL> def _write ( _arg_name , _arg_value ) : <EOL> if ( _arg_name in self . operation . serialization_expand_args and <EOL> isinstance ( _arg_value , ( list , tuple , dict ) ) ) : <EOL> if isinstance ( _arg_value , dict ) : <EOL> self . feed ( '<STR_LIT>' % _arg_name ) <EOL> self . indent ( ) <EOL> for key , value in _arg_value . items ( ) : <EOL> key_string , key_imports = MigrationWriter . serialize ( key ) <EOL> arg_string , arg_imports = MigrationWriter . serialize ( value ) <EOL> args = arg_string . splitlines ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> self . feed ( '<STR_LIT>' % ( key_string , args [ <NUM_LIT:0> ] ) ) <EOL> for arg in args [ <NUM_LIT:1> : - <NUM_LIT:1> ] : <EOL> self . feed ( arg ) <EOL> self . feed ( '<STR_LIT>' % args [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> self . feed ( '<STR_LIT>' % ( key_string , arg_string ) ) <EOL> imports . update ( key_imports ) <EOL> imports . update ( arg_imports ) <EOL> self . unindent ( ) <EOL> self . feed ( '<STR_LIT>' ) <EOL> else : <EOL> self . feed ( '<STR_LIT>' % _arg_name ) <EOL> self . indent ( ) <EOL> for item in _arg_value : <EOL> arg_string , arg_imports = MigrationWriter . serialize ( item ) <EOL> args = arg_string . splitlines ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> for arg in args [ : - <NUM_LIT:1> ] : <EOL> self . feed ( arg ) <EOL> self . feed ( '<STR_LIT>' % args [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> self . feed ( '<STR_LIT>' % arg_string ) <EOL> imports . update ( arg_imports ) <EOL> self . unindent ( ) <EOL> self . feed ( '<STR_LIT>' ) <EOL> else : <EOL> arg_string , arg_imports = MigrationWriter . serialize ( _arg_value ) <EOL> args = arg_string . splitlines ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> self . feed ( '<STR_LIT>' % ( _arg_name , args [ <NUM_LIT:0> ] ) ) <EOL> for arg in args [ <NUM_LIT:1> : - <NUM_LIT:1> ] : <EOL> self . feed ( arg ) <EOL> self . feed ( '<STR_LIT>' % args [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> self . feed ( '<STR_LIT>' % ( _arg_name , arg_string ) ) <EOL> imports . update ( arg_imports ) <EOL> imports = set ( ) <EOL> name , args , kwargs = self . operation . deconstruct ( ) <EOL> operation_args = get_func_args ( self . operation . __init__ ) <EOL> if getattr ( migrations , name , None ) == self . operation . __class__ : <EOL> self . feed ( '<STR_LIT>' % name ) <EOL> else : <EOL> imports . add ( '<STR_LIT>' % ( self . operation . __class__ . __module__ ) ) <EOL> self . feed ( '<STR_LIT>' % ( self . operation . __class__ . __module__ , name ) ) <EOL> self . indent ( ) <EOL> for i , arg in enumerate ( args ) : <EOL> arg_value = arg <EOL> arg_name = operation_args [ i ] <EOL> _write ( arg_name , arg_value ) <EOL> i = len ( args ) <EOL> for arg_name in operation_args [ i : ] : <EOL> if arg_name in kwargs : <EOL> arg_value = kwargs [ arg_name ] <EOL> _write ( arg_name , arg_value ) <EOL> self . unindent ( ) <EOL> self . feed ( '<STR_LIT>' ) <EOL> return self . render ( ) , imports <EOL> def indent ( self ) : <EOL> self . indentation += <NUM_LIT:1> <EOL> def unindent ( self ) : <EOL> self . indentation -= <NUM_LIT:1> <EOL> def feed ( self , line ) : <EOL> self . buff . append ( '<STR_LIT:U+0020>' * ( self . indentation * <NUM_LIT:4> ) + line ) <EOL> def render ( self ) : <EOL> return '<STR_LIT:\n>' . join ( self . buff ) <EOL> class MigrationWriter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , migration ) : <EOL> self . migration = migration <EOL> self . needs_manual_porting = False <EOL> def as_string ( self ) : <EOL> """<STR_LIT>""" <EOL> items = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> imports = set ( ) <EOL> operations = [ ] <EOL> for operation in self . migration . operations : <EOL> operation_string , operation_imports = OperationWriter ( operation ) . serialize ( ) <EOL> imports . update ( operation_imports ) <EOL> operations . append ( operation_string ) <EOL> items [ "<STR_LIT>" ] = "<STR_LIT:\n>" . join ( operations ) + "<STR_LIT:\n>" if operations else "<STR_LIT>" <EOL> dependencies = [ ] <EOL> for dependency in self . migration . dependencies : <EOL> if dependency [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> dependencies . append ( "<STR_LIT>" % dependency [ <NUM_LIT:1> ] ) <EOL> imports . add ( "<STR_LIT>" ) <EOL> else : <EOL> dependency = tuple ( force_text ( s ) for s in dependency ) <EOL> dependencies . append ( "<STR_LIT>" % self . serialize ( dependency ) [ <NUM_LIT:0> ] ) <EOL> items [ "<STR_LIT>" ] = "<STR_LIT:\n>" . join ( dependencies ) + "<STR_LIT:\n>" if dependencies else "<STR_LIT>" <EOL> migration_imports = set ( ) <EOL> for line in list ( imports ) : <EOL> if re . match ( "<STR_LIT>" , line ) : <EOL> migration_imports . add ( line . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] . strip ( ) ) <EOL> imports . remove ( line ) <EOL> self . needs_manual_porting = True <EOL> if "<STR_LIT>" in imports : <EOL> imports . discard ( "<STR_LIT>" ) <EOL> imports . add ( "<STR_LIT>" ) <EOL> else : <EOL> imports . add ( "<STR_LIT>" ) <EOL> sorted_imports = sorted ( imports , key = lambda i : i . split ( ) [ <NUM_LIT:1> ] ) <EOL> items [ "<STR_LIT>" ] = "<STR_LIT:\n>" . join ( sorted_imports ) + "<STR_LIT:\n>" if imports else "<STR_LIT>" <EOL> if migration_imports : <EOL> items [ "<STR_LIT>" ] += ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % "<STR_LIT>" . join ( sorted ( migration_imports ) ) <EOL> if self . migration . replaces : <EOL> items [ '<STR_LIT>' ] = "<STR_LIT>" % self . serialize ( self . migration . replaces ) [ <NUM_LIT:0> ] <EOL> items . update ( <EOL> version = get_version ( ) , <EOL> timestamp = now ( ) . strftime ( "<STR_LIT>" ) , <EOL> ) <EOL> if self . migration . initial : <EOL> items [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> return ( MIGRATION_TEMPLATE % items ) . encode ( "<STR_LIT:utf8>" ) <EOL> @ property <EOL> def basedir ( self ) : <EOL> migrations_package_name = MigrationLoader ( None , load = False ) . migrations_module ( self . migration . app_label ) <EOL> if migrations_package_name is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . migration . app_label <EOL> ) <EOL> try : <EOL> migrations_module = import_module ( migrations_package_name ) <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> try : <EOL> return upath ( module_dir ( migrations_module ) ) <EOL> except ValueError : <EOL> pass <EOL> app_config = apps . get_app_config ( self . migration . app_label ) <EOL> maybe_app_name , _ , migrations_package_basename = migrations_package_name . rpartition ( "<STR_LIT:.>" ) <EOL> if app_config . name == maybe_app_name : <EOL> return os . path . join ( app_config . path , migrations_package_basename ) <EOL> existing_dirs , missing_dirs = migrations_package_name . split ( "<STR_LIT:.>" ) , [ ] <EOL> while existing_dirs : <EOL> missing_dirs . insert ( <NUM_LIT:0> , existing_dirs . pop ( - <NUM_LIT:1> ) ) <EOL> try : <EOL> base_module = import_module ( "<STR_LIT:.>" . join ( existing_dirs ) ) <EOL> except ImportError : <EOL> continue <EOL> else : <EOL> try : <EOL> base_dir = upath ( module_dir ( base_module ) ) <EOL> except ValueError : <EOL> continue <EOL> else : <EOL> break <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> migrations_package_name ) <EOL> final_dir = os . path . join ( base_dir , * missing_dirs ) <EOL> if not os . path . isdir ( final_dir ) : <EOL> os . makedirs ( final_dir ) <EOL> for missing_dir in missing_dirs : <EOL> base_dir = os . path . join ( base_dir , missing_dir ) <EOL> with open ( os . path . join ( base_dir , "<STR_LIT>" ) , "<STR_LIT:w>" ) : <EOL> pass <EOL> return final_dir <EOL> @ property <EOL> def filename ( self ) : <EOL> return "<STR_LIT>" % self . migration . name <EOL> @ property <EOL> def path ( self ) : <EOL> return os . path . join ( self . basedir , self . filename ) <EOL> @ classmethod <EOL> def serialize ( cls , value ) : <EOL> return serializer_factory ( value ) . serialize ( ) <EOL> MIGRATION_TEMPLATE = """<STR_LIT>""" </s>
