o 3aB@sddlZddlZddlZddlZddlmZmZmZmZddl m Z ddl m Z ddl mZddlZddlmZddlmZddlmZdd lmZmZmZmZdd lmZdd lmZd d ZddZ ej!ddddZ"ddZ#GdddZ$dddZ%dS)N)_AppendConstAction _CountAction_StoreConstAction_SubParsersAction) defaultdict)get_close_matches) import_module)apps)settings)ImproperlyConfigured) BaseCommand CommandError CommandParserhandle_default_options) color_style) autoreloadcCs$tj|d}ddt|gDS)zp Given a path to a management directory, return a list of all the command names that are available. commandscSs$g|]\}}}|s|ds|qS)_ startswith).0rnameis_pkgrA/usr/lib/python3/dist-packages/django/core/management/__init__.py s  z!find_commands..)ospathjoinpkgutil iter_modules)management_dir command_dirrrr find_commandssr#cCstd||f}|S)z Given a command name and an application name, return the Command class instance. Allow all errors raised by the import process (ImportError, AttributeError) to propagate. z%s.management.commands.%s)rCommand)app_namermodulerrrload_command_class!sr')maxsizecsfddttdD}tjs|StttD]tj j d}| fddt|Dq|S)a> Return a dictionary mapping command names to their callback applications. Look for a management.commands package in django.core, and in each installed application -- if a commands package exists, register all commands in that package. Core commands are always included. If a settings module has been specified, also include user-defined commands. The dictionary is in the format {command_name: app_name}. Key-value pairs from this dictionary can then be used in calls to load_command_class(app_name, command_name) If a specific version of a command must be loaded (e.g., with the startapp command), the instantiated module can be placed in the dictionary in place of the application name. The dictionary is cached on the first call and reused on subsequent calls. cSsi|]}|dqS) django.corerrrrrr Bz get_commands..r managementcsi|]}|jqSr)rr* app_configrrr+Is) r#__path__r configuredreversedlistr get_app_configsrrrupdate)rrrr.r get_commands+sr6cs$t|tr|}|jjdd}n"zt|}Wn ty%td|wt|tr.|}nt||}| d|}dd|j Dfdd| D}g}|D]}t|t t fra|tt|7}qP|t|qPfdd t |} d d |jD} | D]:} | j|vr| js| | vr|t| jt| tttfrq|| j} t| t t fr|tt| 7}q|t| q|j|d } t| fi|} t|j|j}d d | D}||B}t||}|rt d|d!t"|d!t"|f| #dd}d|vr d| d<|j$|i| S)a{ Call the given command, with the given options and args/kwargs. This is the primary API you should use for calling specific commands. `command_name` may be a string or a command object. Using a string is preferred unless the command object is required for further processing or testing. Some examples: call_command('migrate') call_command('shell', plain=True) call_command('sqlmigrate', 'myapp') from django.core.management.commands import flush cmd = flush.Command() call_command(cmd, verbosity=0, interactive=False) # Do something with cmd ... .Unknown command: %rcSs.i|]}|jrt|jddd|jqS)-r)option_stringsminlstripreplacedestrs_optrrrr+vs z call_command..csi|] \}}|||qSr)get)rkeyvalue) opt_mappingrrr+zsc3sB|jD]}t|tr|jD] }|EdHqq|VqdSN)_actions isinstancerchoicesvalues)parseroptsub_opt) get_actionsrrrOs  z!call_command..get_actionscSs"h|] }|jD]}|jr|qqSr)_group_actionsrequired)rgrouprMrrr szcall_command..)argscSsh|]}|jqSr)r@)ractionrrrrSr,zType '%s help ' for help on a specific subcommand.zAvailable subcommands:cSsgSrGrrrrrsz2ManagementUtility.main_help_text..r)djangor7r8z[%s]z %sNzcNote that only Django core commands are listed as settings are not properly configured (error: %s). ) rjr6r}rr\ rpartitionr`rNOTICEr~r)r commands_onlyusage commands_dictrappstylerrrmain_help_texts6      z ManagementUtility.main_help_textcCst}z||}WnCtyLtjdrtjn tjs#tj dt ||}tj d||r.r)dumpdata sqlmigratesqlsequenceresettestcss|]}|jdfVqdSrN)label)rr/rrr 4sz1ManagementUtility.autocomplete..css*|]}|jrt|j|jdkfVqdSr)r<r=nargsrArrrr:s  cSsh|] }|ddqS)=r)rY)rrrrrrS?sz1ManagementUtility.autocomplete..c3s |] }|dvr|VqdSrr)rrM) prev_optsrrr@sc3s&|]\}}|r||fVqdSrGr)rkvrrrrCs$r)rrrYint IndexErrorr6printrrjfilterrr r4extend ImportErrorr[rHrzr) rcwordscword subcommandsrnsubcommand_cls app_configsrL opt_label require_argr)rrr autocompletesD  "     zManagementUtility.autocompletec Csz|jd}Wn tyd}Ynwt|jdddd}|d|d|jdd d z||jd d \}}t|Wn tyHYnwztj Wn)t yc}z ||_ WYd }~nd }~wt yw}z ||_ WYd }~nd }~wwtj r|d krd|jvrz ttjWn>tyttt_it_dt_t_t_|d dd }||jd d \}}|D]} |j| qYnwt||dkrd|vrtj !|j"dddd S|j#stj !|"dd S||j#d$|j|j#dd S|dks|jdd dgkr$tj !t%dd S|jdd dgdgfvr>tj !|"dd S||&|jd S)z Given the command-line arguments, figure out which subcommand is being run, create a parser appropriate to that command, and run it. rrz$%(prog)s subcommand [options] [args]F)progradd_help allow_abbrevz --settingsz --pythonpathrT*)rN runserverz --noreloadTrz --commands)rrrversionz --versionrz-h)'r{rrr} add_argumentparse_known_argsrr r rr r~rr1r check_errorsrsetup Exceptionrrcr all_modelsr apps_ready models_readyreadyrr[removerrzstdoutrrrT print_help get_version run_from_argv) rrrLrnrTexc_parser_options_args_argrrrrlNsp         $ zManagementUtility.executerG)F) __name__rX __qualname____doc__rrrrrlrrrrrys  ! JrycCst|}|dS)zRun a ManagementUtility.N)ryrl)r{utilityrrrexecute_from_command_lines rrG)& functoolsrrrzargparserrrr collectionsrdifflibr importlibrr django.appsr django.confr django.core.exceptionsr django.core.management.baser r rrdjango.core.management.colorr django.utilsrr#r' lru_cacher6rxryrrrrrs.         "ji