<s> from __future__ import absolute_import , unicode_literals <EOL> import django <EOL> import os <EOL> import sys <EOL> from django . core . management . base import BaseCommand <EOL> import celery <EOL> import djcelery <EOL> DB_SHARED_THREAD = """<STR_LIT>""" <EOL> VALIDATE_MODELS = not django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:7> ) <EOL> def patch_thread_ident ( ) : <EOL> if getattr ( patch_thread_ident , '<STR_LIT>' , False ) : <EOL> return <EOL> try : <EOL> from django . db . backends import BaseDatabaseWrapper , DatabaseError <EOL> if '<STR_LIT>' in BaseDatabaseWrapper . __dict__ : <EOL> import thread <EOL> _get_ident = thread . get_ident <EOL> __old__init__ = BaseDatabaseWrapper . __init__ <EOL> def _init ( self , * args , ** kwargs ) : <EOL> __old__init__ ( self , * args , ** kwargs ) <EOL> self . _thread_ident = _get_ident ( ) <EOL> def _validate_thread_sharing ( self ) : <EOL> if ( not self . allow_thread_sharing <EOL> and self . _thread_ident != _get_ident ( ) ) : <EOL> raise DatabaseError ( <EOL> DB_SHARED_THREAD % ( <EOL> self . alias , self . _thread_ident , _get_ident ( ) ) , <EOL> ) <EOL> BaseDatabaseWrapper . __init__ = _init <EOL> BaseDatabaseWrapper . validate_thread_sharing = _validate_thread_sharing <EOL> patch_thread_ident . called = True <EOL> except ImportError : <EOL> pass <EOL> patch_thread_ident ( ) <EOL> class CeleryCommand ( BaseCommand ) : <EOL> options = BaseCommand . option_list <EOL> skip_opts = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> requires_model_validation = VALIDATE_MODELS <EOL> keep_base_opts = False <EOL> stdout , stderr = sys . stdout , sys . stderr <EOL> def get_version ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> c = celery , d = djcelery , <EOL> ) <EOL> def execute ( self , * args , ** options ) : <EOL> broker = options . get ( '<STR_LIT>' ) <EOL> if broker : <EOL> self . set_broker ( broker ) <EOL> super ( CeleryCommand , self ) . execute ( * args , ** options ) <EOL> def set_broker ( self , broker ) : <EOL> os . environ [ '<STR_LIT>' ] = broker <EOL> def run_from_argv ( self , argv ) : <EOL> self . handle_default_options ( argv [ <NUM_LIT:2> : ] ) <EOL> return super ( CeleryCommand , self ) . run_from_argv ( argv ) <EOL> def handle_default_options ( self , argv ) : <EOL> acc = [ ] <EOL> broker = None <EOL> for i , arg in enumerate ( argv ) : <EOL> if '<STR_LIT>' in arg : <EOL> _ , settings_module = arg . split ( '<STR_LIT:=>' ) <EOL> os . environ [ '<STR_LIT>' ] = settings_module <EOL> elif '<STR_LIT>' in arg : <EOL> _ , pythonpath = arg . split ( '<STR_LIT:=>' ) <EOL> sys . path . insert ( <NUM_LIT:0> , pythonpath ) <EOL> elif '<STR_LIT>' in arg : <EOL> _ , broker = arg . split ( '<STR_LIT:=>' ) <EOL> elif arg == '<STR_LIT>' : <EOL> broker = argv [ i + <NUM_LIT:1> ] <EOL> else : <EOL> acc . append ( arg ) <EOL> if broker : <EOL> self . set_broker ( broker ) <EOL> return argv if self . keep_base_opts else acc <EOL> def die ( self , msg ) : <EOL> sys . stderr . write ( msg ) <EOL> sys . stderr . write ( '<STR_LIT:\n>' ) <EOL> sys . exit ( ) <EOL> def _is_unwanted_option ( self , option ) : <EOL> return option . _long_opts and option . _long_opts [ <NUM_LIT:0> ] in self . skip_opts <EOL> @ property <EOL> def option_list ( self ) : <EOL> return [ x for x in self . options if not self . _is_unwanted_option ( x ) ] </s>
