o
    /h2                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ G dd	 d	Zd
d ZG dd deZG dd deZdS )    N)ArgumentTypeError)apps)settings)CommandError)EmailNotificationCommand)signalcommandc                   @   s   e Zd ZdZdZdZdS )DirPolicyChoicesnoneeachrootN)__name__
__module____qualname__NONEEACHROOT r   r   j/var/www/html/env_mimamsha/lib/python3.10/site-packages/django_extensions/management/commands/runscript.pyr      s    r   c                 C   s$   | d u s
t j| std|  | S )Nz%s is not a directory!)ospathisdirr   )valuer   r   r   check_is_directory   s   r   c                   @   s   e Zd Zdd Zdd ZdS )BadCustomDirectoryExceptionc                 C   s   |d | _ d S )Nzm If --dir-policy is custom than you must set correct directory in --dir option or in settings.RUNSCRIPT_CHDIRmessage)selfr   r   r   r   __init__   s   z$BadCustomDirectoryException.__init__c                 C   s   | j S )Nr   )r   r   r   r   __str__%   s   z#BadCustomDirectoryException.__str__N)r   r   r   r   r   r   r   r   r   r      s    r   c                       s8   e Zd ZdZ fddZ fddZedd Z  ZS )Commandz Runs a script in django context.c                    s&   t  j|i | t | _d| _d S )Nr   )superr   r   getcwdcurrent_directorylast_exit_code)r   argskwargs	__class__r   r   r   ,   s   

zCommand.__init__c                    s   t  | |jddd |jdddddd	 |jd
ddddd	 |jddddddd	 |jddddddd	 |jdddddd	 |jddtdd |jdttjtjtjgdd |jdtd d! d S )"Nscript+)nargsz
--fixtures
store_true
infixturesFz Also look in app.fixtures subdir)actiondestdefaulthelpz--noscripts	noscriptsz!Do not look in app.scripts subdirz-sz--silentsilentzRRun silently, do not show errors and tracebacks. Also implies --continue-on-error.z-cz--continue-on-errorcontinue_on_errorzContinue executing other scripts even though one has failed. It will print a traceback unless --no-traceback or --silent are given The exit code used when terminating will always be 1.z--no-tracebackno_tracebackzDo not show tracebacksz--script-args*z|Space-separated argument list to be passed to the scripts. Note that the same arguments will be passed to all named scripts.)r*   typer0   z--dir-policyzPolicy of selecting scripts execution directory: none - start all scripts in current directory each - start all scripts in their directories root - start all scripts in BASE_DIR directory )r6   choicesr0   z--chdirz^If dir-policy option is set to custom, than this option determines script execution directory.)r6   r0   )	r    add_argumentsadd_argumentstrr   r   r   r   r   )r   parserr&   r   r   r8   1   sv   	

zCommand.add_argumentsc              	      s  j jj jj j j jg 
d }
d s#ttdd 
d r,d 
d 
d 
d	 	
d
 	rAdnd
d rMdd
d tdk r_t	d d S t|dk rmt	 d d S 
fdd
fdd
fdd 	f	dd} fddfdd}
d r
d }ng }g }|D ]}||}	|	sd_
dkrˈst	 d |  q||	 qj
rd!k rst	 d" sg }|D ]}
dkrt	d#|
j  ||
g|R   qj
dkrHrtd$rd S tj
 ztd%j
d& tyG   j
dkr/td%t	 d% td$r>Y d S tj
 Y d S w d S )'Nr(   r1   RUNSCRIPT_SCRIPT_DIRscriptsr,   fixtures	verbosity	tracebackr4   r3   FTr2   r   email_notifications   zNo subdirs to run left.zScript name required.c               
      sH    d p	t tdd } zt|  W | S  ty# } ztt|d }~ww )NchdirRUNSCRIPT_CHDIR)getattrr   r   r   r   r:   )	directorye)optionsr   r   get_directory_from_chdir   s   
z0Command.handle.<locals>.get_directory_from_chdirc                    sH    d p
t tdtj}|tjkrtjS |tjkr!tj	t
| S jS )N
dir_policyRUNSCRIPT_CHDIR_POLICY)rE   r   r   r   r   BASE_DIRr   r   r   dirnameinspectgetfiler"   )script_modulepolicy)rH   r   r   r   get_directory_basing_on_policy   s   


z6Command.handle.<locals>.get_directory_basing_on_policyc                    sT   d r }nd r | }nt tdd r }n | }ttj| d S )NrC   rJ   rD   )rE   r   r   rC   r   abspath)rP   rF   )rR   rI   rH   r   r   set_directory   s   
z%Command.handle.<locals>.set_directoryc              
      sL  d }zH|  | j | }t|tr|rdnd}t|tr<|dkr<ztd| j|f |d ty;   td| j|f w rHj| jd W d S W d S  ty } zOt|tr_t	|dr_|j
}t|trf|nd_rrW Y d }~d S dkrt d| j  rrt  W Y d }~d S rj| jdd	 rtt| d }~ww )
NrB   r   z'%s' failed with exit code %s
returncode)notification_idrV   z%Exception while running run() in '%s'T)rW   include_traceback)run
isinstanceboolintr   r   	TypeErrorsend_email_notification	ExceptionhasattrrV   r#   printr@   	print_excrepr)modscript_args	exit_coderG   )	ERRORr3   rA   r4   r   rT   show_tracebackr2   r?   r   r   
run_script   s\   


z"Command.handle.<locals>.run_scriptc                    s  d| |f }dkrt d|  zt|  W n  ty9 } zt|dr/W Y d }~dS W Y d }~nd }~ww zt|}W nj ty } z^ztj|d u rZW W Y d }~dS W n' ty   t	j
jtjg|dR  d }t	j
|sY W Y d }~dS Y nw rW Y d }~dS rt  dkrt  d	||f  W Y d }~dS d }~ww t|d
rdkrt d|  |S dkrt d|  d S d S )N%s.%srB   zCheck for %szNo module namedF.z.pyr   zCannot import module '%s': %s.rY   zFound script '%s' ...z.Found script '%s' but no run() function found.)ra   	importlibimport_moduleImportErrorr:   
startswithutil	find_specr_   r   r   joinr   rL   splitisfiler@   rb   r`   )parent_packagemodule_namefull_module_pathrG   tmodule_file)rg   ERROR2NOTICENOTICE2rh   r2   r?   r   r   	my_import   sf   
z!Command.handle.<locals>.my_importc                    s   g }t  D ]}D ]} d|j|f | }|r|| q
q| ddkr<| dd\}} ||}|r:|| |S D ]} || }|rL|| q>|S )z1Find script module which contains 'run' attributerj   rk   rB   )r   get_app_configsnameappendfindrsplit)r(   modulesappsubdirrd   parentmod_name)r}   subdirsr   r   find_modules_for_script  s(   




z/Command.handle.<locals>.find_modules_for_scriptre   z'No (valid) module for script '%s' found   z:Try running with a higher verbosity level like: -v2 or -v3zRunning script '%s' ...running_testsz8An error has occurred running scripts. See errors above.rU   )style	SQL_TABLE	SQL_FIELDrg   r{   r   rE   r   lenra   r#   extendr   r`   sysexitr   r]   )r   r$   rH   r=   ri   r   re   script_to_runr(   script_modules
script_modr   )rg   rz   r{   r|   r3   rA   rR   rI   r}   r4   rH   r   rT   rh   r2   r   r?   r   handlew   s   
-3
zCommand.handle)	r   r   r   r0   r   r8   r   r   __classcell__r   r   r&   r   r   )   s    Fr   )r   r   rl   rN   r@   argparser   django.appsr   django.confr   django.core.management.baser   0django_extensions.management.email_notificationsr   "django_extensions.management.utilsr   r   r   r_   r   r   r   r   r   r   <module>   s   