ó
¸9—^c           @   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 d d d „  ƒ  YZ d	 „  Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   ArgumentTypeError(   t   apps(   t   CommandError(   t   EmailNotificationCommand(   t   signalcommandt   DirPolicyChoicesc           B   s   e  Z d  Z d Z d Z RS(   t   nonet   eacht   root(   t   __name__t
   __module__t   NONEt   EACHt   ROOT(    (    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyR      s   c         C   s6   |  d  k s t j j |  ƒ r2 t d |  ƒ ‚ n  |  S(   Ns   %s is not a directory!(   t   Nonet   ost   patht   isdirR    (   t   value(    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   check_is_directory   s    t   BadCustomDirectoryExceptionc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | d |  _  d  S(   Nsm    If --dir-policy is custom than you must set correct directory in --dir option or in settings.RUNSCRIPT_CHDIR(   t   message(   t   selfR   (    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   __init__   s    c         C   s   |  j  S(   N(   R   (   R   (    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   __str__!   s    (   R	   R
   R   R   (    (    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyR      s   	t   Commandc           B   s/   e  Z d  Z d „  Z d „  Z e d „  ƒ Z RS(   s    Runs a script in django context.c         O   s,   t  t |  ƒ j | | Ž  t j ƒ  |  _ d  S(   N(   t   superR   R   R   t   getcwdt   current_directory(   R   t   argst   kwargs(    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyR   (   s    c         C   s-  t  t |  ƒ j | ƒ | j d d d ƒ| j d d d d d d	 t d
 d ƒ| j d d d d d d	 t d
 d ƒ| j d d d d d d d	 t d
 d ƒ| j d d d d d d	 t 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(   Nt   scriptt   nargst   +s
   --fixturest   actiont
   store_truet   destt
   infixturest   defaultt   helps    Also look in app.fixtures subdirs   --noscriptst	   noscriptss!   Do not look in app.scripts subdirs   -ss   --silentt   silents/   Run silently, do not show errors and tracebackss   --no-tracebackt   no_tracebacks   Do not show tracebackss   --script-argst   *t   types|   Space-separated argument list to be passed to the scripts. Note that the same arguments will be passed to all named scripts.s   --dir-policyt   choicess¼   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 s   --chdirs^   If dir-policy option is set to custom, than this option determines script execution directory.(   R   R   t   add_argumentst   add_argumentt   Falset   strR   R   R   R   R   (   R   t   parser(    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyR.   ,   s0    c            sƒ  d d l  m ‰ ˆ	 j j ‰ ˆ	 j j ‰ ˆ	 j j ‰  ˆ	 j j ‰ g  ‰ ˆ d } ˆ d sv ˆ j t ˆ d d ƒ ƒ n  ˆ d r ˆ j d ƒ n  ˆ d	 ‰ ˆ d
 ‰ ˆ d } | r½ t	 ‰ n t
 ‰ ˆ d ‰ ˆ rÜ d ‰ n  ˆ d ‰ t ˆ ƒ d k  rˆ d ƒ GHd  St | ƒ d k  r(ˆ  d ƒ GHd  S‡ ‡ f d †  ‰ ‡ ‡	 ‡ f d †  ‰ ‡ ‡ ‡ ‡ f d †  ‰
 ‡  ‡ ‡	 ‡
 ‡ ‡ ‡ f d †  } ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d †  ‰ ‡ ‡ f d †  } ˆ d rÕˆ d } n g  } x¡ | D]™ } | | ƒ }	 |	 s<ˆ d k r<ˆ r<ˆ  d | ƒ GHˆ d k  r9ˆ  d ƒ GHq9q<n  x< |	 D]4 }
 ˆ d k rjˆ d |
 j ƒ GHn  | |
 | Œ qCWqâWd  S(   Niÿÿÿÿ(   t   settingsR   R(   t   RUNSCRIPT_SCRIPT_DIRt   scriptsR%   t   fixturest	   verbosityt	   tracebackR*   R)   i    t   email_notificationsi   s   No subdirs to run left.s   Script name required.c             sV   ˆ  d p t  ˆ d d  ƒ }  y t |  ƒ Wn% t k
 rQ } t t | ƒ ƒ ‚ n X|  S(   Nt   chdirt   RUNSCRIPT_CHDIR(   t   getattrR   R   R    R   R1   (   t	   directoryt   e(   t   optionsR3   (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   get_directory_from_chdiru   s    c            sh   ˆ  d p t  ˆ d t j ƒ } | t j k r5 ˆ j S| t j k r] t j j t	 j
 |  ƒ ƒ Sˆ j Sd  S(   Nt
   dir_policyt   RUNSCRIPT_CHDIR_POLICY(   R<   R   R   R   t   BASE_DIRR   R   R   t   dirnamet   inspectt   getfileR   (   t   script_modulet   policy(   R?   R   R3   (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   get_directory_basing_on_policy}   s    c            sv   ˆ d r ˆ ƒ  } nC ˆ d r/ ˆ  |  ƒ } n* t  ˆ d d  ƒ rM ˆ ƒ  } n ˆ  |  ƒ } t j t j j | ƒ ƒ d  S(   NR:   RA   R;   (   R<   R   R   R:   R   t   abspath(   RG   R=   (   RI   R@   R?   R3   (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   set_directory†   s    

c            s¼   y7 ˆ |  ƒ |  j  | Œ  ˆ r6 ˆ j d |  j ƒ n  Wn~ t k
 r· } ˆ rS d  Sˆ d k rt ˆ  d |  j ƒ GHn  ˆ r– ˆ j d |  j d t ƒ n  ˆ r¸ t | t ƒ s´ ‚  q´ q¸ n Xd  S(   Nt   notification_idi    s%   Exception while running run() in '%s't   include_traceback(   t   runt   send_email_notificationR	   t	   Exceptiont   Truet
   isinstanceR   (   t   modt   script_argsR>   (   t   ERRORR9   R   RK   t   show_tracebackR)   R7   (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt
   run_script‘   s     
	c            s›  d |  | f } ˆ d k r. ˆ d | ƒ GHn  y t  j |  ƒ Wn, t k
 rm } t | ƒ j d ƒ rn t Sn Xy t  j | ƒ } WnÄ t k
 rG} y  t  j j | ƒ d  k r² t SWnL t	 k
 rt
 j j ˆ j | j d ƒ Œ d } t
 j j | ƒ st Sn Xˆ rt Sˆ rt j ƒ  n  ˆ d k rCˆ  d | | f ƒ GHn  t SXt | d	 ƒ ryˆ d k ruˆ d
 | ƒ GHn  | Sˆ d k r—ˆ d | ƒ GHn  d  S(   Ns   %s.%si   s   Check for %ss   No module namedt   .s   .pyi    s   Cannot import module '%s': %s.RN   s   Found script '%s' ...s.   Found script '%s' but no run() function found.(   t	   importlibt   import_modulet   ImportErrorR1   t
   startswithR0   t   utilt	   find_specR   RP   R   R   t   joinRC   t   splitt   isfileR8   t	   print_exct   hasattr(   t   parent_packaget   module_namet   full_module_pathR>   t   tt   module_file(   RU   t   ERROR2t   NOTICEt   NOTICE2R3   RV   R)   R7   (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt	   my_import£   s>    %c            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(   s3    Find script module which contains 'run' attribute s   %s.%sRX   iÿÿÿÿi   (   R   t   get_app_configst   namet   appendt   findt   rsplit(   R   t   modulest   appt   subdirRS   t   parentt   mod_name(   Rl   t   subdirs(    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   find_modules_for_scriptÌ   s     RT   s'   No (valid) module for script '%s' foundi   s:   Try running with a higher verbosity level like: -v2 or -v3s   Running script '%s' ...(   t   django.confR3   t   stylet	   SQL_TABLEt	   SQL_FIELDRU   Rj   Ro   R<   R0   RQ   t   lenR	   (   R   R   R?   R5   R*   RW   Rx   RT   R   Rr   RS   (    (   RU   Ri   Rj   Rk   R9   RI   R@   Rl   R?   R   RK   R3   RV   R)   Rw   R7   sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   handleQ   s^    





	
	
	!$)
(   R	   R
   R'   R   R.   R   R~   (    (    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyR   %   s   		%(    (   RY   RE   R   R8   t   argparseR    t   django.appsR   t   django.core.management.baseR   t0   django_extensions.management.email_notificationsR   t"   django_extensions.management.utilsR   R   R   RP   R   R   (    (    (    sQ   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runscript.pyt   <module>   s   		