
    kh2                         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dS )    N)ArgumentTypeError)apps)settings)CommandError)EmailNotificationCommand)signalcommandc                       e Zd ZdZdZdZdS )DirPolicyChoicesnoneeachrootN)__name__
__module____qualname__NONEEACHROOT     f/var/www/histauto/venv/lib/python3.11/site-packages/django_extensions/management/commands/runscript.pyr
   r
      s        DDDDDr   r
   c                 l    | t           j                            |           st          d| z            | S )Nz%s is not a directory!)ospathisdirr   )values    r   check_is_directoryr      s2    }BGMM%00} 85 @AAALr   c                       e Zd Zd Zd ZdS )BadCustomDirectoryExceptionc                     |dz   | _         d S )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     : : 	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      s2        
 
 
    r   r   c                   B     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                 x     t                      j        |i | t          j                    | _        d| _        d S )Nr   )superr#   r   getcwdcurrent_directorylast_exit_code)r"   argskwargs	__class__s      r   r#   zCommand.__init__,   s;    $)&)))!#r   c                 t   t                                          |           |                    dd           |                    ddddd	           |                    d
dddd	           |                    dddddd	           |                    dddddd	           |                    ddddd	           |                    ddt          d           |                    dt          t          j        t          j        t          j        gd           |                    dt          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.)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_arguments1   s   f%%%HC0003 	 	
 	
 	
 	4 	 	
 	
 	
 	1 	 	
 	
 	
 	!$D 	 		
 		
 		
 	) 	 	
 	
 	
 	F	 	 	
 	
 	
 	 % % %
> 	 	
 	
 	
 	#* 	 	
 	
 	
 	
 	
r   c           	           j         j         j         j         j         j         j         j        g d         }d         s)                    t          t          dd                     d         r                    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 fdfd f	d}fdfd}d         r	d         }ng }g }|D ]O} ||          }	|	s+d _
        dk    rst           d|z                       :|                    |	           P j
        r$dk     rst           d                     sg }|D ]1}
dk    r t           d|
j        z                        ||
g|R   2 j
        dk    rr+t           d          rd S t          j         j
                   	 t!          d j
                   # t"          $ rb  j
        dk    rt!          d          t           d                     t           d          rY d S t          j         j
                   Y d S w xY wd S )!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         pt          t          dd           } 	 t          |            n.# t          $ r!}t	          t          |                    d }~ww xY w| S )NchdirRUNSCRIPT_CHDIR)getattrr   r   r   r   rC   )	directoryeoptionss     r   get_directory_from_chdirz0Command.handle.<locals>.get_directory_from_chdir   so    (VGH>OQU,V,VI:"9----$ : : :1#a&&999:s   1 
AAAc                    d         pt          t          dt          j                  }|t          j        k    rt          j        S |t          j        k    r1t          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   sz    \* g24D4I/ /F )...((+000ww}'E'EFFF--r   c                    d         r             }n@d         r |           }n,t          t          dd           r             }n |           }t          j        t          j                            |                     d S )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 J4466		& J::=II		#4d;; J4466		::=II	HRW__Y//00000r   c                 4  	 d }	  	|             | j         | }t          |t                    r|rdnd}t          |t                    rK|dk    rE	 t	          d| j        d||          # t          $ r t	          d| j        d|          w xY wr                    | j                   d S d S # t          $ r}t          |t                    rt          |d          r|j
        }t          |t                    r|nd_        rY d }~d S dk    r t           d| j        z                       r
rt          j                     Y d }~d S r                    | 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   s.   I)c"""#CG[1	i.. 6%. 5AIi--  A~~
".,#&<<<!<+4# # # 
  )   ".,#&<<<!<# # 
 ' O000NNNNNO O   a.. -71l3K3K - !I3=i3M3M&TiiST# FFFFFq==%% G#, VWWXXX$ % .!+---FFFFF& 00(+ 1      0&tAww///)s7   AB6 A- -%B B6 6
F AF=F=FFc           
         | d|}dk    rt           d|z                       	 t          j        |            n?# t          $ r2}t	          |                              d          rY d }~dS Y d }~nd }~ww xY w	 t          j        |          }n# t          $ r}	 t          j                            |          Y d }~dS nm# t          $ r` t          j
        j        t          j        g|                    d          R  dz   }t          j
                            |          sY Y d }~dS Y nw xY wrY d }~dS 
rt!          j                     dk    rt           d|d	|d                     Y d }~dS d }~ww xY wt%          |d
          r#dk    rt           	d|z                       |S dk    rt           d|z                       d S d S )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}}ff^.>>??@@@!'7777 ! ! !q66$$%677 ! 55555! ! ! ! !!
+,<==   	% ~//0@AAI$uuuuu J  % % %X%6U9I9O9OPS9T9TUUU    7>>+66 %$uuuuuu% %%  ! 55555! *')))q==BRBRBRTUTUTUVWW   uuuuu-0 q%   q==''"9<L"LMMNNNq==L./      !=sZ   A   
A<
"A77A< B 
F  CE;A D0&E;-E;/D00E;;:E;;F c                    g }t          j                    D ]4}D ]/} |j        d||           }|r|                    |           05|                     d          dk    r=|                     dd          \  }} ||          }|r|                    |           n(D ]%} ||           }|r|                    |           &|S )z1Find script module which contains 'run' attributerz   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#)sxxx$@&IIC ,s+++,
 {{32%%#)==a#8#8 i11 (NN3''' & , ,F#)FF3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.handlew   s   %*&
 "(#{# 	QNN78-CYOOPPP<  	'NN:&&&K(	 -~.#$78 	""NN!N" 	%I $%&;<w<<!&&233444Fw<<!%%/00111F	 	 	 	 			. 		. 		. 		. 		. 		.		1 		1 		1 		1 		1 		1 		1+	 +	 +	 +	 +	 +	 +	 +	 +	 +	 +	 +	 +	Z1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	f	 	 	 	 	 	0 =! 	!-0KKK  	1 	1F44V<<N! &'#q===%% IF RSSTTT  0000 	#1}}V}EVWW   % # "' 	1 	1J1}}gg7*:MMNNOOOJz0K00000!## .411 F,---."N#2     . . .&!++ 'R   eeVWWXXX411 FF,------. $#s    I6 6AK"K"!K")	r   r   r   r9   r#   rA   r   r   __classcell__)r/   s   @r   r'   r'   )   s~        -D         
D
 D
 D
 D
 D
L l. l. ]l. l. l. l. l.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      sD   				 



          & & & & & &                   4 4 4 4 4 4 U U U U U U < < < < < <             )   {. {. {. {. {.& {. {. {. {. {.r   