
    g.                         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	      Zd
 Z G d de      Z G d de      Zy)    N)ArgumentTypeError)apps)settings)CommandError)EmailNotificationCommand)signalcommandc                       e Zd ZdZdZdZy)DirPolicyChoicesnoneeachrootN)__name__
__module____qualname__NONEEACHROOT     l/var/www/django_project/virt/lib/python3.12/site-packages/django_extensions/management/commands/runscript.pyr
   r
      s    DDDr   r
   c                 d    | t         j                  j                  |       st        d| z        | S )Nz%s is not a directory!)ospathisdirr   )values    r   check_is_directoryr      s,    }BGGMM%0 85 @AALr   c                       e Zd Zd Zd Zy)BadCustomDirectoryExceptionc                     |dz   | _         y )Nzm If --dir-policy is custom than you must set correct directory in --dir option or in settings.RUNSCRIPT_CHDIRmessage)selfr   s     r   __init__z$BadCustomDirectoryException.__init__   s      M Mr   c                     | j                   S )Nr    )r"   s    r   __str__z#BadCustomDirectoryException.__str__#   s    ||r   N)r   r   r   r#   r%   r   r   r   r   r      s    Mr   r   c                   <     e Zd ZdZ fdZ fdZed        Z xZS )Commandz Runs a script in django context.c                 d    t        |   |i | t        j                         | _        d| _        y )Nr   )superr#   r   getcwdcurrent_directorylast_exit_code)r"   argskwargs	__class__s      r   r#   zCommand.__init__*   s+    $)&)!#r   c                    t         |   |       |j                  dd       |j                  ddddd	       |j                  d
dddd	       |j                  dddddd	       |j                  dddddd	       |j                  ddddd	       |j                  ddt        d       |j                  dt        t        j
                  t        j                  t        j                  gd       |j                  dt        d !       y )"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.)r3   typer9   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 )r?   choicesr9   z--chdirz^If dir-policy option is set to custom, than this option determines script execution directory.)r?   r9   )	r)   add_argumentsadd_argumentstrr
   r   r   r   r   )r"   parserr/   s     r   rA   zCommand.add_arguments/   sO   f%HC0L%3 	 	
 	,[%4 	 	
 	*\%e 	 	
 	'CV`eI 	 	
 	\PU) 	 	
 	3SG 	 	

 	%**,<,A,ACSCXCXYC 	 	
 	.q 	 	
r   c           	      f     j                   j                   j                   j                   j                   j                   j                   j                  g d   }d   s j                  t        t        dd             d   rj                  d       d   d   d	   d
   rdndd   rddd   t              dk  rt         d             y t        |      dk  rt         d             y fd fdfd f	d}fdfd}d   rd   }ng }g }|D ]@  } ||      }	|	s#d _
        dkD  rst         d|z               0|j                  |	       B  j                  rdk  rst         d             sg }|D ].  }
dkD  rt         d|
j                  z                ||
g|  0  j                  dk7  rFr,t         d      ry t        j                   j                         	 t!        d j                         y # t"        $ r[  j                  dk(  rt!        d      t         d             t         d      rY y t        j                   j                         Y y w xY w)!Nr1   r:   RUNSCRIPT_SCRIPT_DIRscriptsr5   fixtures	verbosity	tracebackr=   r<   FTr;   r   email_notifications   zNo subdirs to run left.zScript name required.c                      d   xs t        t        dd       } 	 t        |        | S # t        $ r}t	        t        |            d }~ww xY w)NchdirRUNSCRIPT_CHDIR)getattrr   r   r   r   rC   )	directoryeoptionss     r   get_directory_from_chdirz0Command.handle.<locals>.get_directory_from_chdir   sS    (VGH>OQU,VI:"9-  % :1#a&99:s   ( 	A
AA
c                 8   d   xs t        t        dt        j                        }|t        j                  k(  rt        j
                  S |t        j                  k(  r2t        j                  j                  t        j                  |             S j                  S )N
dir_policyRUNSCRIPT_CHDIR_POLICY)rP   r   r
   r   r   BASE_DIRr   r   r   dirnameinspectgetfiler+   )script_modulepolicyrS   r"   s     r   get_directory_basing_on_policyz6Command.handle.<locals>.get_directory_basing_on_policy   su    \*pgh@XZjZoZo.pF)...(((+000www}'EFF---r   c                     d   r        }n/d   r	 |       }n!t        t        dd       r        }n |       }t        j                  t        j                  j                  |             y )NrN   rV   rO   )rP   r   r   rN   r   abspath)r\   rQ   r^   rT   rS   s     r   set_directoryz%Command.handle.<locals>.set_directory   s]    w46	&:=I	#4d;46	:=I	HHRWW__Y/0r   c                   	 d }	  	|         | j                   | }t        |t              r|rdnd}t        |t              r#|dk7  r	 t	        d| j
                  d||      rj                  | j
                         y y # t        $ r t	        d| j
                  d|      w xY w# t        $ r}t        |t              rt        |d      r|j                  }t        |t              r|nd_        rY d }~y dkD  rt         d| j
                  z               r
rt        j                          Y d }~y rj                  | j
                  d	
       rt	        t        |             d }~ww xY w)NrL   r   'z' failed with exit code 
returncode)notification_idre   z%Exception while running run() in '%s'T)rf   include_traceback)run
isinstanceboolintr   r   	TypeErrorsend_email_notification	Exceptionhasattrre   r,   printrJ   	print_excrepr)modscript_args	exit_coderR   ERRORr<   rK   r=   r"   ra   show_tracebackr;   rI   s       r   
run_scriptz"Command.handle.<locals>.run_script   sh   I c"#CGG[1	i.%.AIi- A~l".RUR^R^`i/j  xA  #B  B '000N '  ) l".RUR^R^`i/j"kkl  a.71l3K !I3=i3MiST#q=% G#,, VWX$%!++-&00ae0f&tAw//%s=   AB- B %B- %B**B- -	E96AE4>;E4>6E44E9c           
      ^   | d|}dkD  rt         d|z               	 t        j                  |        	 t        j                  |      }t%        |d
      rdkD  rt         	d|z               |S dkD  rt         d|z               y y # t        $ r)}t	        |      j                  d      rY d }~yY d }~d }~ww xY w# t        $ r}	 t        j                  j                  |      Y d }~ynt# t        $ rh t        j                  j                  t        j                  g|j                  d       dz   }t        j                  j                  |      sY Y d }~yY nw xY wrY d }~y
rt!        j"                          dkD  rt         d|d	|d             Y d }~yd }~ww xY w)N.rL   zCheck for %szNo module namedFz.pyr   zCannot import module 'z': rh   zFound script '%s' ...z.Found script '%s' but no run() function found.)rp   	importlibimport_moduleImportErrorrC   
startswithutil	find_specrn   r   r   joinr   rX   splitisfilerJ   rq   ro   )parent_packagemodule_namefull_module_pathrR   tmodule_filerv   ERROR2NOTICENOTICE2rw   r;   rI   s         r   	my_importz!Command.handle.<locals>.my_import   s   *8+F1}f^.>>?@!''7++,<=( q% q='"9<L"LMNq=&!QTd!def !?  !q6$$%67  8!  % ~~//0@AI$ J  %"$'',,x/@/@"_CSCYCYZ]C^"_bg"gK77>>+6$ 7%
  !'')q=%DTVW XYZ#sY   B C 	CB==C	F,C43F'4A(E%F'"F'$E%%F'/3F''F,c                 z   g }t        j                         D ]5  }D ].  } |j                  d||       }|s|j                  |       0 7 | j	                  d      dk7  r3| j                  dd      \  }} ||      }|r|j                  |       |S D ]  } ||       }|s|j                  |       ! |S )z3 Find script module which contains 'run' attribute rz   rL   )r   get_app_configsnameappendfindrsplit)	r1   modulesappsubdirrs   parentmod_namer   subdirss	          r   find_modules_for_scriptz/Command.handle.<locals>.find_modules_for_script   s    G++- ,% ,F#sxx$@&ICs+,, {{32%#)==a#8 1NN3' N & ,F#FF3Cs+,
 Nr   rt   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.rd   )style	SQL_TABLE	SQL_FIELDrv   r   r   rP   r   lenrp   r,   extendr   ro   sysexitr   rl   )r"   r-   rS   rG   rx   r   rt   script_to_runr1   script_modules
script_modrv   r   r   r   r<   rK   r^   rT   r   r=   ra   rw   r;   r   rI   s   ` `        @@@@@@@@@@@@@@@r   handlezCommand.handleZ   s    %%**&&

  ""(#{#NN78-CYOP< NN:&K(	 -~.#$78"N!N"I $%&;<w<!&234w<!%/01		.		1"	 "	H'	g '	gR	0 =!!-0KK  	1F4V<N!&'#q=% IF RST  0	1 1}VeXYZ$ "' 	1J1}g7*:M:MMNOz0K0	1
 !#41,,-
."#]jnj}j}~~ $  .&&!+&'abbeVWX41,,-.s   4I A J0J0/J0)	r   r   r   r9   r#   rA   r   r   __classcell__)r/   s   @r   r'   r'   '   s)    -D 
)
V P. P.r   r'   )r   r   r{   rZ   rJ   argparser   django.appsr   django.confr   django.core.management.baser   0django_extensions.management.email_notificationsr   "django_extensions.management.utilsr   r
   r   rn   r   r'   r   r   r   <module>r      sP    	 
    &    4 U < ) D.& D.r   