3
g.                 @   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   m/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django_extensions/management/commands/runscript.pyr      s   r   c             C   s&   | d kst jj|  r"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 )N)r   )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|| tj | _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| |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'   typer-   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 )r3   choicesr-   z--chdirz^If dir-policy option is set to custom, than this option determines script execution directory.)r3   r-   )	r   add_argumentsadd_argumentstrr   r   r   r   r   )r   parser)r$   r   r   r5   /   s6    

zCommand.add_argumentsc                s  j jj jj j j jg 
d }
d sFjttdd 
d rXjd 
d 
d 
d	 	
d
 	rdnd
d rdd
d tdk rt	d d S t|dk rt	 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 }xR|D ]J}||}	|	sd_
dkrh rht	 d |  qh|j|	 qhW j
rd!k r܈ rt	 d" sg }x8|D ]0}
dkrt	d#|
j  ||
f|  qW j
dkrrNtd$rBd S tjj
 ytd%j
d&W nR tk
r   j
dkrtd%t	 d% td$rd S tjj
 Y nX d S )'Nr%   r.   ZRUNSCRIPT_SCRIPT_DIRscriptsr)   Zfixtures	verbosity	tracebackr1   r0   FTr/   r   email_notifications   zNo subdirs to run left.zScript name required.c                 sT    d pt tdd } yt|  W n. tk
rN } ztt|W Y d d }~X nX | S )NchdirRUNSCRIPT_CHDIR)getattrr   r   r   r   r7   )	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j	t
j| S jS d S )N
dir_policyZRUNSCRIPT_CHDIR_POLICY)r@   r   r   r   r   BASE_DIRr   r   r   dirnameinspectgetfiler    )script_modulepolicy)rC   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jtjj| d S )Nr>   rE   r?   )r@   r   r   r>   r   abspath)rJ   rA   )rL   rD   rC   r   r   set_directory   s    
z%Command.handle.<locals>.set_directoryc                sR  d }y|  | j | }t|tr.|r*dnd}t|tr|dkrytd| j|f |dW n& tk
r   td| j|f Y nX rj| jd W n tk
rL } zt|trt	|dr|j
}t|tr|nd_rd S dkrt d| j  rrtj  d S r(j| jdd	 r:tt| W Y d d }~X nX d S )
Nr=   r   z'%s' failed with exit code %s)
returncode)notification_idrO   z%Exception while running run() in '%s'T)rP   Zinclude_traceback)run
isinstanceboolintr   r   	TypeErrorZsend_email_notification	ExceptionhasattrrO   r!   printr;   	print_excrepr)modscript_argsZ	exit_coderB   )	ERRORr0   r<   r1   r   rN   show_tracebackr/   r:   r   r   
run_script   s>    


z"Command.handle.<locals>.run_scriptc          '      sp  d| |f }dkr$t d|  ytj|  W n4 tk
rf } zt|jdrVdS W Y d d }~X nX ytj|}W n tk
r& } zytjj|d krdS W nB tk
r   t	j
jtjf|jd d }t	j
j|sdS Y nX rdS rtj  dkrt  d	||f  dS d }~X nX t|d
rRdkrNt d|  |S dkrlt d|  d S )Nz%s.%sr=   zCheck for %szNo module namedF.z.pyr   zCannot import module '%s': %s.rQ   zFound script '%s' ...z.Found script '%s' but no run() function found.)rX   	importlibimport_moduleImportErrorr7   
startswithutil	find_specrV   r   r   joinr   rF   splitisfiler;   rY   rW   )parent_packagemodule_nameZfull_module_pathrB   tZmodule_file)r]   ERROR2NOTICENOTICE2r^   r/   r:   r   r   	my_import   s>    



z!Command.handle.<locals>.my_importc                s   g }x@t j D ]4}x.D ]&} d|j|f | }|r|j| qW qW | jddkr~| jdd\}} ||}|r|j| n&x$D ]} || }|r|j| qW |S )z3 Find script module which contains 'run' attribute z%s.%sr`   r=   )r   Zget_app_configsnameappendfindrsplit)r%   modulesZappsubdirr[   parentmod_name)rp   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.)rO   )styleZ	SQL_TABLEZ	SQL_FIELDr]   rn   rs   r@   r   lenrX   r!   extendr   rW   sysexitr   rU   )r   r"   rC   r9   r_   r{   r\   Zscript_to_runr%   Zscript_modulesZ
script_modr   )r]   rm   rn   ro   r0   r<   rL   rD   rp   r1   rC   r   rN   r^   r/   rz   r:   r   handleZ   s    
	$)




zCommand.handle)	r   r   r   r-   r   r5   r   r   __classcell__r   r   )r$   r   r   '   s   +r   )r   r   ra   rH   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   rV   r   r   r   r   r   r   <module>   s   	