<s> import collections <EOL> import optparse <EOL> import os <EOL> import subprocess <EOL> from django . core import management <EOL> from django . utils . importlib import import_module <EOL> from functools import reduce <EOL> def get_option_default ( option ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> option . dest , <EOL> None if option . default is optparse . NO_DEFAULT else option . default , <EOL> ) <EOL> def get_option_defaults ( command ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ <EOL> get_option_default ( option ) for option in command . option_list ] ) <EOL> def issubpackage ( package , parents ) : <EOL> if isinstance ( parents , str ) : <EOL> parents = ( parents , ) <EOL> return len ( parents ) > <NUM_LIT:0> and reduce ( <EOL> bool . __or__ , <EOL> [ parent == "<STR_LIT:.>" . join ( package . split ( "<STR_LIT:.>" ) [ : len ( parts ) ] ) <EOL> for parent , parts in [ ( parent , parent . split ( "<STR_LIT:.>" ) ) <EOL> for parent in parents ] ] , <EOL> False ) <EOL> def get_command_class_from_apps ( name , apps , exclude_packages = None , exclude_command_class = None ) : <EOL> """<STR_LIT>""" <EOL> if exclude_packages is None : <EOL> exclude_packages = [ ] <EOL> for app in reversed ( <EOL> [ app for app in apps if not issubpackage ( app , exclude_packages ) ] ) : <EOL> try : <EOL> command_class = import_module ( <EOL> "<STR_LIT>" . format ( <EOL> app = app , name = name ) ) . Command <EOL> except ( ImportError , AttributeError ) : <EOL> pass <EOL> else : <EOL> if exclude_command_class is None or not issubclass ( command_class , exclude_command_class ) : <EOL> return command_class <EOL> return None <EOL> def get_command_class ( name , exclude_packages = None , exclude_command_class = None ) : <EOL> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> return get_command_class_from_apps ( <EOL> name , <EOL> settings . INSTALLED_APPS if "<STR_LIT>" in settings . INSTALLED_APPS else ( "<STR_LIT>" , ) + tuple ( settings . INSTALLED_APPS ) , <EOL> exclude_packages = exclude_packages , <EOL> exclude_command_class = exclude_command_class ) <EOL> def get_command_and_defaults ( name , exclude_packages = None , exclude_command_class = None ) : <EOL> """<STR_LIT>""" <EOL> command = get_command_class ( name , <EOL> exclude_packages = exclude_packages , <EOL> exclude_command_class = exclude_command_class ) <EOL> defaults = { } <EOL> if command is not None : <EOL> command = command ( ) <EOL> defaults = command . get_option_defaults ( ) if isinstance ( command , Command ) else get_option_defaults ( command ) <EOL> return ( command , defaults ) <EOL> def check_command ( self , name , exclude_packages = None , exclude_command_class = None ) : <EOL> """<STR_LIT>""" <EOL> return get_command_class ( <EOL> name , <EOL> exclude_packages = exclude_packages , <EOL> exclude_command_class = exclude_command_class ) is not None <EOL> def check_program ( name ) : <EOL> """<STR_LIT>""" <EOL> with open ( os . devnull , "<STR_LIT:w>" ) as null : <EOL> try : <EOL> subprocess . check_call ( ( "<STR_LIT>" , name ) , stdout = null , stderr = null ) <EOL> except subprocess . CalledProcessError as e : <EOL> return False <EOL> return True <EOL> class devnull ( ) : <EOL> """<STR_LIT>""" <EOL> depth = <NUM_LIT:0> <EOL> null = None <EOL> def __enter__ ( self ) : <EOL> if self . depth == <NUM_LIT:0> : <EOL> self . null = open ( os . devnull , "<STR_LIT:w>" ) <EOL> self . depth += <NUM_LIT:1> <EOL> return self . null <EOL> def __exit__ ( self , * args , ** kwargs ) : <EOL> if self . depth == <NUM_LIT:1> : <EOL> self . null . close ( ) <EOL> self . null = None <EOL> self . depth -= <NUM_LIT:1> <EOL> class CommandOptions ( object ) : <EOL> """<STR_LIT>""" <EOL> args = "<STR_LIT>" <EOL> help = "<STR_LIT>" <EOL> option_list = ( ) <EOL> option_groups = ( ) <EOL> actions = ( ) <EOL> devnull = devnull ( ) <EOL> def get_option_list ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . option_list <EOL> def get_option_groups ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . option_groups <EOL> def get_actions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . actions <EOL> def get_option_lists ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . get_option_list ( ) ] + [ option_list <EOL> for name , description , option_list <EOL> in self . get_option_groups ( ) ] <EOL> def get_options ( self ) : <EOL> """<STR_LIT>""" <EOL> return reduce ( <EOL> list . __add__ , <EOL> [ list ( option_list ) for option_list in self . get_option_lists ( ) ] , <EOL> [ ] ) <EOL> def get_option_names ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ option . dest for option in self . get_options ( ) ] <EOL> def get_option_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ <EOL> get_option_default ( option ) for option in self . get_options ( ) ] ) <EOL> class Command ( CommandOptions , management . BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> def usage ( self , subcommand ) : <EOL> """<STR_LIT>""" <EOL> usage = "<STR_LIT>" . format ( <EOL> subcommand = subcommand , args = self . args ) <EOL> if self . help : <EOL> return "<STR_LIT>" . format ( <EOL> usage = usage , help = self . help ) <EOL> return usage <EOL> def create_parser ( self , prog_name , subcommand ) : <EOL> """<STR_LIT>""" <EOL> parser = optparse . OptionParser ( <EOL> prog = prog_name , <EOL> usage = self . usage ( subcommand ) , <EOL> version = self . get_version ( ) , <EOL> option_list = self . get_option_list ( ) ) <EOL> for name , description , option_list in self . get_option_groups ( ) : <EOL> group = optparse . OptionGroup ( parser , name , description ) ; <EOL> list ( map ( group . add_option , option_list ) ) <EOL> parser . add_option_group ( group ) <EOL> return parser <EOL> def parse_arguments ( self , arguments ) : <EOL> """<STR_LIT>""" <EOL> return arguments <EOL> def parse_options ( self , options ) : <EOL> """<STR_LIT>""" <EOL> for name in self . get_option_names ( ) : <EOL> parse = getattr ( self , "<STR_LIT>" . format ( <EOL> name = name ) , None ) <EOL> if parse is not None and isinstance ( parse , collections . Callable ) : <EOL> options [ name ] = parse ( ) <EOL> return options <EOL> def handle ( self , * arguments , ** options ) : <EOL> """<STR_LIT>""" <EOL> self . arguments = arguments <EOL> self . options = options <EOL> self . arguments = self . parse_arguments ( arguments ) <EOL> self . options = self . parse_options ( options ) <EOL> for name in self . get_actions ( ) : <EOL> validate = getattr ( self , "<STR_LIT>" . format ( <EOL> name = name ) , None ) <EOL> if validate is not None and isinstance ( validate , collections . Callable ) : <EOL> validate ( * arguments , ** options ) <EOL> for name in self . get_actions ( ) : <EOL> handle = getattr ( self , "<STR_LIT>" . format ( <EOL> name = name ) , None ) <EOL> if handle is not None and isinstance ( handle , collections . Callable ) : <EOL> handle ( * self . arguments , ** self . options ) <EOL> class BaseCommandOptions ( CommandOptions ) : <EOL> """<STR_LIT>""" <EOL> exclude_packages = ( "<STR_LIT>" , ) <EOL> option_list = management . BaseCommand . option_list <EOL> option_groups = ( <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> option_list , <EOL> ) , <EOL> ) <EOL> def get_exclude_packages ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . exclude_packages <EOL> def parse_option_verbosity ( self ) : <EOL> try : <EOL> verbosity = min ( <NUM_LIT:3> , max ( <NUM_LIT:0> , int ( self . options . get ( "<STR_LIT>" , <NUM_LIT:1> ) ) ) ) <EOL> except ( ValueError , TypeError ) : <EOL> verbosity = <NUM_LIT:1> <EOL> return verbosity <EOL> def check_command ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if not check_command ( <EOL> name , <EOL> exclude_packages = self . get_exclude_packages ( ) , <EOL> exclude_command_class = self . __class__ ) : <EOL> raise management . CommandError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> name = name ) ) <EOL> def check_program ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if not check_program ( name ) : <EOL> raise management . CommandError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> name = name ) ) <EOL> def call_command ( self , name , * arguments , ** options ) : <EOL> """<STR_LIT>""" <EOL> command , defaults = get_command_and_defaults ( <EOL> name , <EOL> exclude_packages = self . get_exclude_packages ( ) , <EOL> exclude_command_class = self . __class__ ) <EOL> if command is None : <EOL> raise management . CommandError ( <EOL> "<STR_LIT>" . format ( <EOL> name = name ) ) <EOL> defaults . update ( options ) <EOL> return command . execute ( * arguments , ** defaults ) <EOL> def call_program ( self , name , * arguments ) : <EOL> """<STR_LIT>""" <EOL> verbosity = self . options . get ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> with self . devnull as null : <EOL> try : <EOL> subprocess . check_call ( ( name , ) + tuple ( arguments ) , <EOL> stdout = null if verbosity == <NUM_LIT:0> else self . stdout , <EOL> stderr = null if verbosity == <NUM_LIT:0> else self . stderr ) <EOL> except subprocess . CalledProcessError as error : <EOL> raise management . CommandError ( <EOL> "<STR_LIT>" . format ( <EOL> name = name , code = error . returncode ) ) <EOL> return <NUM_LIT:0> <EOL> class BaseCommand ( BaseCommandOptions , Command ) : <EOL> """<STR_LIT>""" <EOL> option_list = ( ) </s>
