o
    uvXjV$  ã                   @   s–   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 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)Ú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   úl/var/www/html/myproject/venv/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 t| ƒj|i |¤Ž t ¡ | _d S r   )Úsuperr   r   r   ÚgetcwdÚcurrent_directory)r   ÚargsÚkwargs©Ú	__class__r   r   r   (   s   zCommand.__init__c                    s´   t 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	� |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Úsilentz/Run silently, do not show errors and tracebacksz--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 )r4   Úchoicesr/   z--chdirz^If dir-policy option is set to custom, than this option determines script execution directory.)r4   r/   )
r    r   Úadd_argumentsÚadd_argumentÚstrr   r   r   r   r   )r   Úparserr%   r   r   r6   ,   s>   þþ
þþþý
þzCommand.add_argumentsc                    sÞ  ddl m‰ ˆ	jj‰ˆ	jj‰ˆ	jj‰ ˆ	jj‰g ‰ˆd }ˆd s)ˆ tˆddƒ¡ ˆd r2ˆ d¡ ˆd	 ‰ˆd
 ‰ˆd }|rCd‰nd‰ˆd ‰ˆrMd‰ˆ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 }|D ]:}||ƒ}	|	sÒˆdkrÒˆsÒt
ˆ d | ƒƒ ˆd!k rÒt
ˆ d"ƒƒ |	D ]}
ˆdkrãt
ˆd#|
j ƒƒ ||
g|¢R Ž  qÔq²d S )$Nr   )Úsettingsr'   r0   ÚRUNSCRIPT_SCRIPT_DIRÚscriptsr+   ÚfixturesÚ	verbosityÚ	tracebackr2   FTr1   Úemail_notificationsé   zNo subdirs to run left.zScript name required.c               
      sH   ˆ d p	t ˆdd ƒ} zt| ƒ W | S  ty# } ztt|ƒƒ‚d }~ww )NÚchdirÚRUNSCRIPT_CHDIR)Úgetattrr   r   r   r8   )Ú	directoryÚe)Úoptionsr:   r   r   Úget_directory_from_chdiru   s   
þ€ÿz0Command.handle.<locals>.get_directory_from_chdirc                    sH   ˆ d p
t ˆdtjƒ}|tjkrˆjS |tjkr!tj t	 
| ¡¡S ˆjS )NÚ
dir_policyÚRUNSCRIPT_CHDIR_POLICY)rD   r   r   r   ÚBASE_DIRr   r   r   ÚdirnameÚinspectÚgetfiler"   )Úscript_moduleÚpolicy)rG   r   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 ˆdd ƒrˆƒ }nˆ | ƒ}t tj |¡¡ d S )NrB   rI   rC   )rD   r   rB   r   Úabspath)rO   rE   )rQ   rH   rG   r:   r   r   Úset_directory†   s   
z%Command.handle.<locals>.set_directoryc              
      s¶   zˆ| ƒ | j |Ž  ˆrˆj| jd� W d S W d S  tyZ } z6ˆr)W Y d }~d S ˆdkr6tˆ d| j ƒƒ ˆr@ˆj| jdd� ˆrHt|tƒsO‚ W Y d }~d S W Y d }~d S d }~ww )N)Únotification_idr   z%Exception while running run() in '%s'T)rT   Úinclude_traceback)ÚrunÚsend_email_notificationr   Ú	ExceptionÚprintÚ
isinstancer   )ÚmodÚscript_argsrF   )ÚERRORr@   r   rS   Ú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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ˆjg| d¡¢R Ž d }t	j
 |¡s€Y W Y d }~dS Y nw ˆrŒW Y d }~dS ˆr’t ¡  ˆdkr t ˆ d	||f ƒƒ W Y d }~dS d }~ww t|d
ƒr¿ˆdkr½t ˆd| ƒƒ |S ˆdkrÍt ˆd| ƒƒ d S d S )Nú%s.%srA   zCheck for %szNo module namedFÚ.z.pyr   zCannot import module '%s': %s.rV   zFound script '%s' ...z.Found script '%s' but no run() function found.)rY   Ú	importlibÚimport_moduleÚImportErrorr8   Ú
startswithÚutilÚ	find_specrX   r   r   ÚjoinrK   ÚsplitÚisfiler?   Ú	print_excÚhasattr)Úparent_packageÚmodule_nameÚfull_module_pathrF   ÚtÚmodule_file)r]   ÚERROR2ÚNOTICEÚNOTICE2r:   r^   r1   r>   r   r   Ú	my_import£   sP   þ€ÿÿ ÿþ€ï
ÿ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 )z3 Find script module which contains 'run' attribute r`   ra   éÿÿÿÿrA   )r   Úget_app_configsÚnameÚappendÚfindÚrsplit)r'   ÚmodulesÚappÚsubdirr[   ÚparentÚmod_name)ru   Ú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' ...)Údjango.confr:   ÚstyleÚ	SQL_TABLEÚ	SQL_FIELDr]   rs   ry   rD   ÚlenrY   r   )r   r#   rG   r<   r2   r_   r‚   r\   r'   r|   r[   r   )r]   rr   rs   rt   r@   rQ   rH   ru   rG   r   rS   r:   r^   r1   r�   r>   r   ÚhandleQ   sb   
	)
ýùzCommand.handle)	r   r   r   r/   r   r6   r   r‰   Ú__classcell__r   r   r%   r   r   %   s    %r   )rb   rM   r   r?   Úargparser   Údjango.appsr   Údjango.core.management.baser   Ú0django_extensions.management.email_notificationsr   Ú"django_extensions.management.utilsr   r   r   rX   r   r   r   r   r   r   Ú<module>   s   	