ó
ø¢TQc           @   s|  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 y d  d l
 m Z Wn! e k
 r¯ d  d l m Z n Xd  d l m Z d  d l m Z d  d l Z d  d l m Z m Z d  d l m Z d  d l m Z d  d l m Z m Z d  d	 l m Z e j d
 e j ƒ Z  e j d e j ƒ Z! d e f d „  ƒ  YZ" d S(   iÿÿÿÿN(   t   urlretrieve(   t   make_option(   t   path(   t   Templatet   Context(   t   archive(   t   rmtree_errorhandler(   t   BaseCommandt   CommandError(   t   handle_extensionss	   ^([a-z]):s   ^([a-z])[:|]t   TemplateCommandc           B   sä   e  Z d  Z d Z e j e d d d d d d d ƒe d	 d
 d d d d d d g d d ƒe d d d d d d d g  d d ƒf Z e Z e Z	 d d d g Z
 d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÝ  
    Copies either a Django application layout template or a Django project
    layout template into the specified directory.

    :param style: A color style object (see django.core.management.color).
    :param app_or_project: The string 'app' or 'project'.
    :param name: The name of the application or project.
    :param directory: The directory to which the template should be copied.
    :param options: The additional variables passed to project or app templates
    s'   [name] [optional destination directory]s
   --templatet   actiont   storet   destt   templatet   helps1   The dotted import path to load the template from.s   --extensions   -et
   extensionst   appendt   defaultt   pyst   The file extension(s) to render (default: "py"). Separate multiple extensions with commas, or use -e multiple times.s   --names   -nt   filess_   The file name(s) to render. Separate multiple extensions with commas, or use -n multiple times.t   httpt   httpst   ftpc   !   
   K   sL  | |  _  g  |  _ t | j d ƒ ƒ |  _ t j d | ƒ sy t j d | ƒ sW d } n d } t d | | | f ƒ ‚ n  | d  k rø t	 j
 t j ƒ  | ƒ } y t j | ƒ Wq8t k
 rô } | j t j k rß d | } n | } t | ƒ ‚ q8Xn@ t j	 j t	 j | ƒ ƒ } t j	 j | ƒ s8t d | ƒ ‚ n  t t | j d	 ƒ d
 d' ƒƒ } g  }	 x9 | j d ƒ D]( }
 |	 j t d „  |
 j d ƒ ƒ ƒ qoW|  j d k ró|  j j d | d j
 | ƒ f ƒ |  j j d | d j
 |	 ƒ f ƒ n  d | } d | } d | } t t | i | | 6| | 6 d t ƒ} d d l m } | j  s`| j! ƒ  n  |  j" | j d ƒ | ƒ } t# | ƒ d } xFt j$ | ƒ D]5\ } } } | | } | j% | | ƒ } | r t	 j
 | | ƒ } t	 j | ƒ s t j& | ƒ q n  x: | D]1 } | j' d ƒ s)| d k r| j( | ƒ qqWx| D]ˆ} | j) d( ƒ r_qDn  t	 j
 | | ƒ } t	 j
 | | | j% | | ƒ ƒ } t	 j | ƒ r´t d | ƒ ‚ n  t* | d  ƒ  } | j+ ƒ  } Wd  QX| j) | ƒ só| |	 k r/| j, d! ƒ } t- | ƒ } | j. | ƒ } | j/ d! ƒ } n  t* | d" ƒ  } | j | ƒ Wd  QX|  j d k rz|  j j d# | ƒ n  y! t0 j1 | | ƒ |  j2 | ƒ WqDt k
 rË|  j3 j d$ | |  j4 j5 ƒ qDXqDWq›W|  j rH|  j d k rÿ|  j j d% ƒ n  xF |  j D]8 }  t	 j6 |  ƒ r.t j( |  ƒ q	t0 j7 |  d& t8 ƒq	Wn  d  S()   Nt	   verbositys   ^[_a-zA-Z]\w*$s
   ^[_a-zA-Z]s5   make sure the name begins with a letter or underscores)   use only numbers, letters and underscoress%   %r is not a valid %s name. Please %s.s   '%s' already existssB   Destination directory '%s' does not exist, please create it first.R   t   ignoredR   c         S   s
   |  j  ƒ  S(   N(   t   strip(   t   x(    (    s9   ../Django//lib/python/django/core/management/templates.pyt   <lambda>f   s    t   ,i   s0   Rendering %s template files with extensions: %s
s   , s/   Rendering %s template files with filenames: %s
s   %s_names   %s_templates   %s_directoryt
   autoescapeiÿÿÿÿ(   t   settingsR   i   t   .t   __pycache__s   .pyos   .pycs	   .py.classsi   %s already exists, overlaying a project or app into an existing directory won't replace conflicting filest   rbs   utf-8t   wbs   Creating %s
sk   Notice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.s   Cleaning up temporary files.
t   onerror(    (   s   .pyos   .pycs	   .py.class(9   t   app_or_projectt   paths_to_removet   intt   getR   t   ret   searchR   t   NoneR   t   joint   ost   getcwdt   makedirst   OSErrort   errnot   EEXISTt   abspatht
   expandusert   existst   tupleR	   t   extendt   mapt   splitt   stdoutt   writeR   t   dictt   Falset   django.confR   t
   configuredt	   configuret   handle_templatet   lent   walkt   replacet   mkdirt
   startswitht   removet   endswitht   opent   readt   decodeR   t   rendert   encodet   shutilt   copymodet   make_writeablet   stderrt   stylet   NOTICEt   isfilet   rmtreeR   (!   t   selfR%   t   namet   targett   optionst   messaget   top_dirt   eR   t   extra_filest   filet	   base_namet   base_subdirt   base_directoryt   contextR   t   template_dirt   prefix_lengtht   roott   dirsR   t	   path_restt   relative_dirt
   target_dirt   dirnamet   filenamet   old_patht   new_patht   template_filet   contentR   t   new_filet   path_to_remove(    (    s9   ../Django//lib/python/django/core/management/templates.pyt   handleA   s´    			&


			
		c         C   sÜ   | d k r& t j t j d d | ƒ S| j d ƒ rB | d } n  t j | ƒ } t j | ƒ } t j | ƒ rs | S|  j	 | ƒ r” |  j
 | ƒ } n t j | ƒ } t j | ƒ r¿ |  j | ƒ St d |  j | f ƒ ‚ d S(   sÀ   
        Determines where the app or project templates are.
        Use django.__path__[0] as the default because we don't
        know into which directory Django has been installed.
        i    t   confs   file://i   s   couldn't handle %s template %s.N(   R+   R   R,   t   djangot   __path__RF   R4   t   normpatht   isdirt   is_urlt   downloadR3   R5   t   extractR   R%   (   RV   R   t   subdirt   expanded_templatet   absolute_path(    (    s9   ../Django//lib/python/django/core/management/templates.pyRA   ¼   s    c         C   s¯  d „  } d |  j  } t j d | d d ƒ } |  j j | ƒ | | ƒ \ } } |  j d k rv |  j j d | ƒ n  y% t | t	 j
 | | ƒ ƒ \ } } Wn, t k
 rÉ }	 t d | | |	 f ƒ ‚ n X| j d	 ƒ d
 }
 | j d ƒ } | rt j | ƒ \ } } | j d ƒ p|
 } n |
 } |  j | ƒ d } | j d ƒ } | ry| ryt j | ƒ } | ry| | 7} qyn  |
 | k r«t	 j
 | | ƒ } t j | | ƒ | S| S(   sD   
        Downloads the given URL and returns the file name.
        c         S   sN   |  j  d ƒ } | j d ƒ d } |  j d ƒ r> | d } n |  } | | f S(   Nt   /iÿÿÿÿ(   t   rstripR9   RH   (   t   urlt   tmpRk   t   display_url(    (    s9   ../Django//lib/python/django/core/management/templates.pyt   cleanup_urlÚ   s    s   django_%s_template_t   prefixt   suffixt	   _downloadi   s   Downloading %s
s"   couldn't download URL %s to %s: %sR~   iÿÿÿÿs   content-dispositionRk   i   s   content-type(   R%   t   tempfilet   mkdtempR&   R   R   R:   R;   R    R   R,   t   IOErrorR   R9   R(   t   cgit   parse_headert   splitextt	   mimetypest   guess_extensionRN   t   move(   RV   R€   Rƒ   R„   t   tempdirRk   R‚   t   the_patht   infoR\   t	   used_namet   content_dispositiont   _t   paramst   guessed_filenamet   extt   content_typet   guessed_path(    (    s9   ../Django//lib/python/django/core/management/templates.pyRy   Ö   s:    		%c         C   sO   t  j | ƒ \ } } | j ƒ  j d ƒ rE | d | } | d  } n  | | f S(   s@   
        Like os.path.splitext, but takes off .tar, too
        s   .tariüÿÿÿ(   t	   posixpathRŒ   t   lowerRH   (   RV   R‘   t   baseR˜   (    (    s9   ../Django//lib/python/django/core/management/templates.pyRŒ     s
    c         C   s¯   d |  j  } t j d | d d ƒ } |  j j | ƒ |  j d k r[ |  j j d | ƒ n  y t j	 | | ƒ | SWn5 t j
 t f k
 rª } t d | | | f ƒ ‚ n Xd S(	   s„   
        Extracts the given file to a temporarily and returns
        the path of the directory with the extracted content.
        s   django_%s_template_R„   R…   t   _extracti   s   Extracting %s
s"   couldn't extract file %s to %s: %sN(   R%   R‡   Rˆ   R&   R   R   R:   R;   R   Rz   t   ArchiveExceptionR‰   R   (   RV   Rk   R„   R   R\   (    (    s9   ../Django//lib/python/django/core/management/templates.pyRz     s    c         C   s9   d | k r t  S| j d d ƒ d j ƒ  } | |  j k S(   s;   
        Returns True if the name looks like a URL
        t   :i   i    (   R=   R9   Rœ   t   url_schemes(   RV   R   t   scheme(    (    s9   ../Django//lib/python/django/core/management/templates.pyRx   '  s    c         C   sj   t  j j d ƒ r d St j | t j ƒ sf t j | ƒ } t j | j ƒ t j	 B} t j
 | | ƒ n  d S(   sb   
        Make sure that the file is writeable.
        Useful if our source is read-only.
        t   javaN(   t   syst   platformRF   R-   t   accesst   W_OKt   statt   S_IMODEt   st_modet   S_IWUSRt   chmod(   RV   Rk   t   stt   new_permissions(    (    s9   ../Django//lib/python/django/core/management/templates.pyRP   0  s    N(   t   __name__t
   __module__t   __doc__t   argsR   t   option_listR   R=   t   requires_model_validationt   can_import_settingsR¡   R+   Rr   RA   Ry   RŒ   Rz   Rx   RP   (    (    (    s9   ../Django//lib/python/django/core/management/templates.pyR
      s,   
	{		6	
			(#   RŠ   R1   R   R-   R›   R)   RN   R¨   R¤   R‡   t   urllib.requestR    t   ImportErrort   urllibt   optparseR   R   Rt   t   django.templateR   R   t   django.utilsR   t   django.utils._osR   t   django.core.management.baseR   R   t,   django.core.management.commands.makemessagesR	   t   compilet   It	   _drive_ret   _url_drive_reR
   (    (    (    s9   ../Django//lib/python/django/core/management/templates.pyt   <module>   s0   