ó
®â0_c           @   sf  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	 d  d l
 Z
 d  d l m Z d  d l Z d  d l m Z d  d l m Z m Z d  d l m Z d  d l m Z m Z d  d l m Z m Z d  d l 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   path(   t   settings(   t   BaseCommandt   CommandError(   t   handle_extensions(   t   Contextt   Engine(   t   archivet   six(   t   urlretrieve(   t   get_docs_versions	   ^([a-z]):s   ^([a-z])[:|]t   TemplateCommandc           B   sƒ   e  Z d  Z e Z d d d g Z e Z d Z d „  Z	 d d „ Z 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
    t   httpt   httpst   ftps   .py-tpls   .pyc         C   s–   | j  d d d ƒ| j  d d d d d ƒ| j  d d d	 ƒ| j  d
 d d d d d d d g d d ƒ| j  d d d d d d d g  d d ƒd  S(   Nt   namet   helps#   Name of the application or project.t	   directoryt   nargst   ?s   Optional destination directorys
   --templates*   The path or URL to load the template from.s   --extensions   -et   destt
   extensionst   actiont   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   add_argument(   t   selft   parser(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyt   add_arguments2   s    c   $   	   K   s‡  | |  _  g  |  _ | d |  _ |  j | | ƒ | d  k r® t j t j ƒ  | ƒ } y t j	 | ƒ Wqî t
 k
 rª } | j t j k r• d | } n | } t | ƒ ‚ qî Xn@ t j j t j | ƒ ƒ } t j j | ƒ sî t d | ƒ ‚ n  t t | d ƒ ƒ } g  }	 x4 | d D]( }
 |	 j t d „  |
 j d ƒ ƒ ƒ qW|  j d k r™|  j j d	 | d
 j | ƒ f ƒ |  j j d | d
 j |	 ƒ f ƒ n  d | } d | } d | } d | } d j d „  | j ƒ  Dƒ ƒ } t t | i | | 6| | 6| | 6t ƒ  d 6t j d 6t j r$d n d d 6 d t  ƒ} t! j" sZt! j# ƒ  t j$ ƒ  n  |  j% | d | ƒ } t& | ƒ d } x’t j' | ƒ D]\ } } } | | } | 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& ƒ rTq9n  t j | | ƒ } t j | | | j( | | ƒ ƒ } x? |  j- D]4 \ } } | j, | ƒ r‘| t& | ƒ  | } Pq‘q‘Wt j | ƒ rët d | ƒ ‚ n  | j, | ƒ s| |	 k r…t. j/ | d d  d! ƒ } | j0 ƒ  }  Wd  QXt1 ƒ  j2 |  ƒ }! |! j3 | ƒ }  t. j/ | d" d  d! ƒ }" |" j |  ƒ Wd  QXn t4 j5 | | ƒ |  j d k r»|  j j d# | ƒ n  y! t4 j6 | | ƒ |  j7 | ƒ Wq9t
 k
 r|  j8 j d$ | |  j9 j: ƒ q9Xq9WqW|  j rƒ|  j d k r@|  j j d% ƒ n  x@ |  j D]2 }# t j; |# ƒ rot j+ |# ƒ qJt4 j< |# ƒ qJWn  d  S('   Nt	   verbositys   '%s' already existssB   Destination directory '%s' does not exist, please create it first.R   R   c         S   s
   |  j  ƒ  S(   N(   t   strip(   t   x(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyt   <lambda>_   t    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_directorys   camel_case_%s_nameR#   c         s   s!   |  ] } | d  k r | Vq d S(   t   _N(    (   t   .0R!   (    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pys	   <genexpr>l   s    t   docs_versiont   django_versionsA   # -*- coding: utf-8 -*-
from __future__ import unicode_literals

t   unicode_literalst
   autoescapet   templatei   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   rt   encodings   utf-8t   ws   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.
(   s   .pyos   .pycs	   .py.class(=   t   app_or_projectt   paths_to_removeR   t   validate_namet   NoneR    t   joint   ost   getcwdt   makedirst   OSErrort   errnot   EEXISTR   t   abspatht
   expandusert   existst   tupleR   t   extendt   mapt   splitt   stdoutt   writet   titleR   t   dictR
   t   djangot   __version__R   t   PY3t   FalseR   t
   configuredt	   configuret   setupt   handle_templatet   lent   walkt   replacet   mkdirt
   startswitht   removet   endswitht   rewrite_template_suffixest   iot   opent   readR   t   from_stringt   rendert   shutilt   copyfilet   copymodet   make_writeablet   stderrt   stylet   NOTICEt   isfilet   rmtree($   R   R1   R   t   targett   optionst   top_dirt   et   messageR   t   extra_filest   filet	   base_namet   base_subdirt   base_directoryt   camel_case_namet   camel_case_valuet   contextt   template_dirt   prefix_lengtht   roott   dirsR   t	   path_restt   relative_dirt
   target_dirt   dirnamet   filenamet   old_patht   new_patht
   old_suffixt
   new_suffixt   template_filet   contentR+   t   new_filet   path_to_remove(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyt   handleD   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(   R4   R    R5   RG   t   __path__RS   R=   t   normpatht   isdirt   is_urlt   downloadR<   R>   t   extractR   R1   (   R   R+   t   subdirt   expanded_templatet   absolute_path(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyRN   À   s    c         C   s»   | d  k r7 t d | d k r$ d n d | f ƒ ‚ n  t j r’ t j d | ƒ s· t j d | ƒ sm d } n d } t d	 | | | f ƒ ‚ q· n% | j ƒ  s· t d
 | | f ƒ ‚ n  d  S(   Ns   you must provide %s %s namet   appt   ant   as   ^[_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.sK   %r is not a valid %s name. Please make sure the name is a valid identifier.(   R4   R   R   t   PY2t   ret   searcht   isidentifier(   R   R   R1   Ri   (    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyR3   Ú   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   rstripRB   RU   (   t   urlt   tmpRz   t   display_url(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/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-dispositionRz   i   s   content-type(   R1   t   tempfilet   mkdtempR2   R   R   RC   RD   R	   R    R5   t   IOErrorR   RB   t   gett   cgit   parse_headert   splitextt	   mimetypest   guess_extensionR\   t   move(   R   R—   Rš   R›   t   tempdirRz   R™   t   the_patht   infoRh   t	   used_namet   content_dispositionR%   t   paramst   guessed_filenamet   extt   content_typet   guessed_path(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyR‰   ï   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   lowerRU   (   R   R©   t   baseR¯   (    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/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(   R1   Rž   RŸ   R2   R   R   RC   RD   R   RŠ   t   ArchiveExceptionR    R   (   R   Rz   R›   R¨   Rh   (    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyRŠ   /  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    (   RJ   RB   R³   t   url_schemes(   R   R+   t   scheme(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyRˆ   @  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   platformRS   R6   t   accesst   W_OKt   statt   S_IMODEt   st_modet   S_IWUSRt   chmod(   R   Rz   t   stt   new_permissions(    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyR_   I  s    (   s   .py-tpls   .py(   (   s   .py-tpls   .pyN(   t   __name__t
   __module__t   __doc__RJ   t   requires_system_checksR¸   t   Truet   leave_locale_aloneRV   R   R4   Rƒ   RN   R3   R‰   R¤   RŠ   Rˆ   R_   (    (    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyR      s   
	|			6	
			(#   R¢   R:   RW   R¥   R6   R²   R’   R\   R¿   R»   Rž   R    RG   t   django.confR   t   django.core.management.baseR   R   t   django.core.management.utilsR   t   django.templateR   R   t   django.utilsR   R   t%   django.utils.six.moves.urllib.requestR	   t   django.utils.versionR
   t   compilet   It	   _drive_ret   _url_drive_reR   (    (    (    sB   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/templates.pyt   <module>   s,   