a
    ÆZEg°.  ã                   @   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ÚnoneZeachÚrootN)Ú__name__Ú
__module__Ú__qualname__ÚNONEÚEACHÚROOT© r   r   úh/var/www/sistema_ama/venv/lib/python3.9/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_errorz¸Continue 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)   Útyper/   z--dir-policyz¼Policy 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 )r5   Úchoicesr/   z--chdirz^If dir-policy option is set to custom, than this option determines script execution directory.)r5   r/   )	r   Úadd_argumentsÚadd_argumentÚstrr   r   r   r   r   )r   Úparserr%   r   r   r7   /   sF    þþ
þ
þþþýþzCommand.add_argumentsc              	      s®  ˆj j‰ˆj j‰ˆj j‰ ˆj j‰g ‰ˆ
d }ˆ
d sFˆ ttddƒ¡ ˆ
d rXˆ d¡ ˆ
d ‰ˆ
d ‰ˆ
d	 ‰	ˆ
d
 ‰ˆ	r‚d‰nd‰ˆ
d ‰ˆršd‰d‰ˆ
d ‰tˆƒdk r¾t	ˆdƒƒ d S t|ƒdk rÚt	ˆ dƒƒ d S ‡
fdd„‰‡
‡fdd„‰‡‡‡
fdd„‰‡ ‡‡‡	‡‡‡‡‡f	dd„}‡ ‡‡‡‡‡‡fdd„‰‡‡fdd„}ˆ
d �rZˆ
d }ng }g }|D ]H}||ƒ}	|	�s¢dˆ_
ˆdk�rfˆ�sft	ˆ d | ƒƒ �qf| |	¡ �qfˆj
�rÞˆd!k �rÔˆ�sÔt	ˆ d"ƒƒ ˆ�sÞg }|D ]2}
ˆdk�rt	ˆd#|
j ƒƒ ||
g|¢R Ž  �qâˆj
dk�rªˆ�rDtˆd$ƒ�r8d S t ˆj
¡ ztd%ˆj
d&�‚W nR t�y¨   ˆj
dk�rztd%ƒ‚t	ˆ d%ƒƒ tˆd$ƒ�r˜Y d S t ˆj
¡ Y n0 d S )'Nr'   r0   ZRUNSCRIPT_SCRIPT_DIRÚscriptsr+   ZfixturesÚ	verbosityÚ	tracebackr3   r2   FTr1   r   Úemail_notificationsé   zNo subdirs to run left.zScript name required.c               
      sV   ˆ d pt tdd ƒ} zt| ƒ W n0 tyP } ztt|ƒƒ‚W Y d }~n
d }~0 0 | S )NÚchdirÚRUNSCRIPT_CHDIR)Úgetattrr   r   r   r   r9   )Ú	directoryÚe)Úoptionsr   r   Úget_directory_from_chdir   s    "z0Command.handle.<locals>.get_directory_from_chdirc                    sL   ˆ d pt tdtjƒ}|tjkr&tjS |tjkrBtj 	t
 | ¡¡S ˆjS d S )NÚ
dir_policyZRUNSCRIPT_CHDIR_POLICY)rB   r   r   r   r   ÚBASE_DIRr   r   r   ÚdirnameÚinspectÚgetfiler!   )Úscript_moduleÚpolicy)rE   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 ƒr6ˆƒ }nˆ | ƒ}t tj |¡¡ d S )Nr@   rG   rA   )rB   r   r   r@   r   Úabspath)rL   rC   )rN   rF   rE   r   r   Úset_directory�   s    
z%Command.handle.<locals>.set_directoryc              
      sh  d }z�ˆ| ƒ | j |Ž }t|tƒr.|r*dnd}t|tƒr€|dkr€ztd| j|f |d�‚W n$ ty~   td| j|f ƒ‚Y n0 ˆr’ˆj| jd� W nÎ t�yb } z´t|tƒrÀt	|dƒrÀ|j
}t|tƒrÎ|ndˆ_ˆræW Y d }~d S ˆdk�rtˆ d| j ƒƒ ˆ�r$ˆ�rt ¡  W Y d }~d S ˆ�r:ˆj| jdd	� ˆ�rLtt|ƒƒ‚‚ W Y d }~n
d }~0 0 d S )
Nr?   r   z'%s' failed with exit code %s©Ú
returncode)Únotification_idrR   z%Exception while running run() in '%s'T)rS   Zinclude_traceback)ÚrunÚ
isinstanceÚboolÚintr   r   Ú	TypeErrorZsend_email_notificationÚ	ExceptionÚhasattrrR   r"   Úprintr=   Ú	print_excÚrepr)ÚmodÚscript_argsZ	exit_coderD   )	ÚERRORr2   r>   r3   r   rP   Úshow_tracebackr1   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yr } z(t|ƒ d¡r^W Y d }~dS W Y d }~n
d }~0 0 zt |¡}W nà t�yb } zÆz$tj |¡d u r¶W W Y d }~dS W nR t�y
   t	j
jtjg| d¡¢R Ž d }t	j
 |¡�sY W Y d }~dS Y n0 ˆ�r W Y d }~dS ˆ�r.t ¡  ˆdk�rLt ˆ d	||f ƒƒ W Y d }~dS d }~0 0 t|d
ƒ�rŽˆdk�rŠt ˆd| ƒƒ |S ˆdk�r¨t ˆd| ƒƒ d S )Nú%s.%sr?   zCheck for %szNo module namedFÚ.z.pyr   zCannot import module '%s': %s.rT   zFound script '%s' ...z.Found script '%s' but no run() function found.)r[   Ú	importlibÚimport_moduleÚImportErrorr9   Ú
startswithÚutilÚ	find_specrY   r   r   Újoinr   rH   ÚsplitÚisfiler=   r\   rZ   )Úparent_packageÚmodule_nameZfull_module_pathrD   ÚtZmodule_file)r`   ÚERROR2ÚNOTICEÚNOTICE2ra   r1   r<   r   r   Ú	my_import¿   s>    $ 


z!Command.handle.<locals>.my_importc                    sœ   g }t  ¡ D ]0}ˆD ]&}ˆ d|j|f | ƒ}|r| |¡ qq|  d¡dkrv|  dd¡\}}ˆ ||ƒ}|r˜| |¡ n"ˆD ]}ˆ || ƒ}|rz| |¡ qz|S )z3 Find script module which contains 'run' attribute rc   rd   éÿÿÿÿr?   )r   Zget_app_configsÚnameÚappendÚfindÚrsplit)r'   ÚmodulesZappÚsubdirr^   ÚparentÚmod_name)rt   Úsubdirsr   r   Úfind_modules_for_scriptè   s     

z/Command.handle.<locals>.find_modules_for_scriptr_   z'No (valid) module for script '%s' foundé   z:Try running with a higher verbosity level like: -v2 or -v3zRunning script '%s' ...Zrunning_testsz8An error has occurred running scripts. See errors above.rQ   )ÚstyleZ	SQL_TABLEZ	SQL_FIELDr`   rr   rw   rB   r   Úlenr[   r"   Úextendr   rZ   ÚsysÚexitr   rX   )r   r#   rE   r;   rb   r   r_   Zscript_to_runr'   Zscript_modulesZ
script_modr   )r`   rq   rr   rs   r2   r>   rN   rF   rt   r3   rE   r   rP   ra   r1   r~   r<   r   ÚhandleZ   sŠ    
	$)


zCommand.handle)	r   r   r   r/   r   r7   r   r†   Ú__classcell__r   r   r%   r   r   '   s
   +r   )r   r„   re   rJ   r=   Úargparser   Zdjango.appsr   Zdjango.confr   Zdjango.core.management.baser   Z0django_extensions.management.email_notificationsr   Z"django_extensions.management.utilsr   r   r   rY   r   r   r   r   r   r   Ú<module>   s   	