
^Q\#                 @   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 Gd d   d  Z d	 d
   Z Gd d   d e  Z Gd 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noneZeachrootN)__name__
__module____qualname__NONEEACHROOT r   r   `/var/www/dbchiro/venv/build/django-extensions/django_extensions/management/commands/runscript.pyr      s   r   c             C   s6   |  d  k s t  j j |   r2 t d |    n  |  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                   sL   e  Z d  Z d Z   f d d   Z   f d d   Z e d d    Z   S)Commandz Runs a script in django context.c                s,   t  t |   j | |   t j   |  _ d  S)N)superr   r   r   getcwdcurrent_directory)r   argskwargs)	__class__r   r   r   (   s    zCommand.__init__c                s-  t  t |   j |  | j d d d | j d d d d d d	 d
 d d | j d d d d d d	 d
 d d | j d d d d d d d	 d
 d d | j d d d d d d	 d
 d d | j d d d d t d d | j d d t d t j t j t j g d d | j d d t	 d d d  S) Nscriptnargs+z
--fixturesaction
store_truedest
infixturesdefaultFhelpz Only look in app.fixtures subdirz--noscripts	noscriptsz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*typez|Space-separated argument list to be passed to the scripts. Note that the same arguments will be passed to all named scripts.z--dir-policychoiceszPolicy 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 z--chdirz^If dir-policy option is set to custom, than this option determines script execution directory.)
r   r   add_argumentsadd_argumentstrr   r   r   r   r   )r   parser)r"   r   r   r2   ,   s0    zCommand.add_argumentsc                s  d d l  m  	 j j  	 j j  	 j j   	 j j  g    d }  j d  so  j d  n   j d  r  j d  n  t	  j d d	     j d
 d    j d d  } | r d  n d   j d d    r d  n   j d d   t
   d	 k  r8t  d   d  St
 |  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   }  j d  r" d } n g  } x | D] } | |  }	 |	 s d k r rt   d |    d  k  rt   d!   qqn  xA |	 D]9 }
  d	 k rt  d" |
 j   n  | |
 |  qWq/Wd  S)#Nr   )settingsr#   r,   scriptsr)   Zfixtures	verbosity   	tracebackFr.   Tr-   email_notificationszNo subdirs to run left.zScript name required.c                 sm     j  d  p t  d d   }  y t |   Wn7 t k
 rh } z t t |    WYd  d  } ~ Xn X|  S)NchdirRUNSCRIPT_CHDIR)getgetattrr   r   r   r4   )	directorye)optionsr6   r   r   get_directory_from_chdiru   s    !%z0Command.handle.<locals>.get_directory_from_chdirc                sm     j  d  p! t  d t j  } | t j k r:  j S| t j k rb t j j	 t
 j |    S j Sd  S)N
dir_policyZRUNSCRIPT_CHDIR_POLICY)r>   r?   r   r   r   BASE_DIRr   r   r   dirnameinspectZgetfiler   )script_modulepolicy)rB   r   r6   r   r   get_directory_basing_on_policy}   s    $z6Command.handle.<locals>.get_directory_basing_on_policyc                s    j  d  r    } nH  j  d  r9   |   } n* t  d d   rW    } n   |   } t j t j j |   d  S)Nr<   rD   r=   )r>   r?   r   r<   r   abspath)rH   r@   )rJ   rC   rB   r6   r   r   set_directory   s    z%Command.handle.<locals>.set_directoryc                s   y7  |   |  j  |    r6  j d |  j  n  Wn t k
 r } zu  rV d  S d k r| t   d |  j   n   r  j d |  j d d  n   r t | t  s   q n  WYd  d  } ~ Xn Xd  S)NZnotification_idr   z%Exception while running run() in '%s'Zinclude_tracebackT)runZsend_email_notificationr
   	Exceptionprint
isinstancer   )modscript_argsrA   )ERRORr;   r   rL   show_tracebackr-   r8   r   r   
run_script   s     
	z"Command.handle.<locals>.run_scriptc                 s  d |  | f }  d k r3 t   d |   n  y t j |   Wn> t k
 r } z t |  j d  rr d SWYd  d  } ~ Xn Xy t j |  } Wn t k
 rD} z t j j  j	 | j
 d   d } t j j |  s d S r d S rt j   n   d k r.t    d	 | | f   n  d SWYd  d  } ~ Xn Xt | d
  r{ d k rwt   d |   n  | S d k rt   d |   n  d  S)Nz%s.%sr9   zCheck for %szNo module namedF.z.pyr   zCannot import module '%s': %s.rM   zFound script '%s' ...z.Found script '%s' but no run() function found.)rO   	importlibimport_moduleImportErrorr4   
startswithr   r   joinrE   splitisfiler:   	print_exchasattr)parent_packagemodule_nameZfull_module_pathrA   tZmodule_file)rS   ERROR2NOTICENOTICE2r6   rT   r-   r8   r   r   	my_import   s6    %z!Command.handle.<locals>.my_importc                s   g  } xW t  j   D]I } x@  D]8 }   d | j | f |   } | r  | j |  q  q  Wq W|  j d  d k r |  j d d  \ } }   | |  } | r | j |  q n6 x3  D]+ }   | |   } | r | j |  q q W| S)z3 find script module which contains 'run' attribute z%s.%srV   r9   )r   Zget_app_configsnameappendfindrsplit)r#   modulesZappsubdirrQ   parentZmod_name)rf   subdirsr   r   find_modules_for_script   s     z/Command.handle.<locals>.find_modules_for_scriptrR   z'No (valid) module for script '%s' found   z:Try running with a higher verbosity level like: -v2 or -v3zRunning script '%s' ...)Zdjango.confr6   styleZ	SQL_TABLEZ	SQL_FIELDrS   rd   r>   ri   intlenrO   r
   )r   r    rB   r7   r.   rU   rp   rR   r#   rl   rQ   r   )rS   rc   rd   re   r;   rJ   rC   rf   rB   r   rL   r6   rT   r-   ro   r8   r   handleQ   s^    
			$'%zCommand.handle)r
   r   r   r+   r   r2   r   ru   r   r   )r"   r   r   %   s   %r   )rW   rG   r   r:   argparser   Zdjango.appsr   Zdjango.core.management.baser   Z0django_extensions.management.email_notificationsr   Z"django_extensions.management.utilsr   r   r   rN   r   r   r   r   r   r   <module>   s   	