
v^5                 @   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 m 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 d  d	 l m Z Gd
 d   d e  Z d S)    N)import_module)urlretrieve)settings)BaseCommandCommandError)handle_extensions)ContextEngine)archive)get_docs_versionc               @   s   e  Z d  Z d Z d Z d d d g Z d Z d d	   Z d
 d d  Z d d   Z	 d d d  Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d
 S)TemplateCommanda  
    Copy 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
    Fhttphttpsftp.py-tpl.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)Nnamehelpz#Name of the application or project.	directorynargs?zOptional destination directoryz
--templatez*The path or URL to load the template from.z--extensionz-edest
extensionsactionappenddefaultpyztThe file extension(s) to render (default: "py"). Separate multiple extensions with commas, or use -e multiple times.z--namez-nfilesz_The file name(s) to render. Separate multiple file names with commas, or use -n multiple times.)add_argument)selfparser r!   B/tmp/pip-build-8lau8j11/django/django/core/management/templates.pyadd_arguments(   s    zTemplateCommand.add_argumentsNc       #   )   K   s  | |  _  | d k r d n d |  _ g  |  _ | d |  _ |  j |  | d  k r t j j t j   |  } y t j	 |  Wq?t
 k
 r t d |   Yq?t k
 r } z t |   WYd  d  } ~ Xq?Xnh | d k r |  j t j j |  d  t j j t j j |   } t j j |  s?t d |   t t | d   } g  } x7 | d	 D]+ }	 | j t d
 d   |	 j d    qfW|  j d k r|  j j d | d j |  f  |  j j d | d j |  f  d | }
 d | } d | } d | } d j d d   | j   D  } t | |
 | | | | | d t   d t j i  d d } t j st j   t j    |  j! | d |  } t" |  d } xt j# |  D]\ } } } | | d   } | j$ |
 |  } | r"t j j | |  } t j	 | d d x@ | d  d   D]. } | j% d   sT| d! k r3| j& |  q3Wx| D]} | j' d,  rqlt j j | |  } t j j | | | j$ |
 |   } xB |  j( D]7 \ } } | j' |  r| d  t" |   | } PqWt j j |  r0t d% | |  j | f   | j' |  sK| | k rt) | d& d'  } | j*   } Wd  QRXt+   j, |  }  |  j- |  } t) | d( d& d'  }! |! j |  Wd  QRXn t. j/ | |  |  j d k r|  j j d) |  y! t. j0 | |  |  j1 |  Wqlt k
 rH|  j2 j d* | |  j3 j4  YqlXqlWqW|  j r|  j d k ry|  j j d+  x@ |  j D]5 }" t j j5 |"  rt j& |"  qt. j6 |"  qWd  S)-Nappana	verbosityz'%s' already existsr   zBDestination directory '%s' does not exist, please create it first.r   r   c             S   s
   |  j    S)N)strip)xr!   r!   r"   <lambda>V   s    z(TemplateCommand.handle.<locals>.<lambda>,   z0Rendering %s template files with extensions: %s
z, z/Rendering %s template files with filenames: %s
z%s_namez%s_templatez%s_directoryzcamel_case_%s_name c             s   s!   |  ] } | d  k r | Vq d S)_Nr!   ).0r)   r!   r!   r"   	<genexpr>c   s    z)TemplateCommand.handle.<locals>.<genexpr>Zdocs_versionZdjango_versionZ
autoescapeFtemplate   exist_okT.__pycache__.pyo.pyc	.py.classz_%s already exists. Overlaying %s %s into an existing directory won't replace conflicting files.encodingzutf-8wzCreating %s
zkNotice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.zCleaning up temporary files.
)r6   r7   r8   )7app_or_projecta_or_anpaths_to_remover'   validate_nameospathjoingetcwdmakedirsFileExistsErrorr   OSErrorbasenameabspath
expanduserexiststupler   extendmapsplitstdoutwritetitler   r   django__version__r   Z
configured	configuresetuphandle_templatelenwalkreplace
startswithremoveendswithrewrite_template_suffixesopenreadr	   Zfrom_stringrendershutilcopyfilecopymodemake_writeablestderrstyleZNOTICEisfilermtree)#r   r;   r   targetoptionsZtop_direr   Zextra_filesfile	base_nameZbase_subdirZbase_directoryZcamel_case_nameZcamel_case_valuecontextZtemplate_dirZprefix_lengthrootdirsr   Z	path_restZrelative_dir
target_dirdirnamefilenameZold_pathnew_pathZ
old_suffix
new_suffixZtemplate_filecontentr1   Znew_fileZpath_to_remover!   r!   r"   handle:   s    		"
)



"			

				zTemplateCommand.handlec             C   s   | d k r) t  j j t j d d |  S| j d  rH | d d  } t  j j |  } t  j j |  } t  j j |  r | S|  j	 |  r |  j
 |  } n t  j j |  } t  j j |  r |  j |  St d |  j | f   d S)z
        Determine where the app or project templates are.
        Use django.__path__[0] as the default because the Django install
        directory isn't known.
        Nr   Zconfzfile://   zcouldn't handle %s template %s.)r?   r@   rA   rQ   __path__rY   rH   normpathisdiris_urldownloadrG   rI   extractr   r;   )r   r1   subdirZexpanded_templateZabsolute_pathr!   r!   r"   rU      s    zTemplateCommand.handle_templater   c             C   s   | d  k r0 t  d j d |  j d |  j    | j   sc t  d j d | d |  j d |    y t |  Wn t k
 r Yn1 Xt  d j d | d |  j d |  j d |    d  S)Nz you must provide {an} {app} namer%   r$   zX'{name}' is not a valid {app} {type}. Please make sure the {type} is a valid identifier.r   typez'{name}' conflicts with the name of an existing Python module and cannot be used as {an} {app} {type}. Please try another {type}.)r   formatr<   r;   isidentifierr   ImportError)r   r   Zname_or_dirr!   r!   r"   r>      s(    						zTemplateCommand.validate_namec             C   s  d d   } d |  j  } t j d | d d  } |  j j |  | |  \ } } |  j d k rv |  j j d |  y( t | t	 j
 j | |   \ } } Wn> t k
 r }	 z t d	 | | |	 f   WYd
 d
 }	 ~	 Xn X| j d  d }
 | j d  } | r4t j |  \ } } | j d  p.|
 } n |
 } |  j |  d } | j d  } | r| rt j |  } | r| | 7} |
 | k rt	 j
 j | |  } t j | |  | S| S)zB
        Download the given URL and return the file name.
        c             S   sN   |  j  d  } | j d  d } |  j d  r> | d } n |  } | | f S)N/r2   )rstriprM   r[   )urltmprr   display_urlr!   r!   r"   cleanup_url   s    z-TemplateCommand.download.<locals>.cleanup_urlzdjango_%s_template_prefixsuffixZ	_downloadr,   zDownloading %s
z"couldn't download URL %s to %s: %sNr   r2   zcontent-dispositionrr   zcontent-typer   )r;   tempfilemkdtempr=   r   r'   rN   rO   r   r?   r@   rA   rE   r   rM   getcgiparse_headersplitext	mimetypesguess_extensionr`   move)r   r   r   r   tempdirrr   r   the_pathinforj   Z	used_namecontent_dispositionr.   paramsZguessed_filenameextcontent_typeZguessed_pathr!   r!   r"   r|      s:    	(&
zTemplateCommand.downloadc             C   sX   t  j |  \ } } | j   j d  rN | d d  | } | d d  } | | f S)z@
        Like os.path.splitext, but takes off .tar, too
        z.tar   Nr   )	posixpathr   lowerr[   )r   r   baser   r!   r!   r"   r   )  s
    zTemplateCommand.splitextc             C   s   d |  j  } t j d | d d  } |  j j |  |  j d k rX |  j j d |  y t j	 | |  | SWnG t j
 t f k
 r } z t d | | | f   WYd d } ~ Xn Xd S)	z
        Extract the given file to a temporarily and return
        the path of the directory with the extracted content.
        zdjango_%s_template_r   r   Z_extractr,   zExtracting %s
z"couldn't extract file %s to %s: %sN)r;   r   r   r=   r   r'   rN   rO   r
   r}   ZArchiveExceptionrE   r   )r   rr   r   r   rj   r!   r!   r"   r}   3  s    zTemplateCommand.extractc             C   s9   d | k r d S| j  d d  d j   } | |  j k S)z)Return True if the name looks like a URL.:Fr2   r   )rM   r   url_schemes)r   r1   schemer!   r!   r"   r{   D  s    zTemplateCommand.is_urlc             C   sQ   t  j | t  j  sM t  j |  } t j | j  t j B} t  j | |  d S)zb
        Make sure that the file is writeable.
        Useful if our source is read-only.
        N)r?   accessW_OKstatS_IMODEst_modeS_IWUSRchmod)r   rr   stZnew_permissionsr!   r!   r"   rc   K  s    zTemplateCommand.make_writeabler   r   )r   )__name__
__module____qualname____doc__Zrequires_system_checksr   r\   r#   rv   rU   r>   r|   r   r}   r{   rc   r!   r!   r!   r"   r      s   
~!6
r   )r   r   r?   r   r`   r   r   	importlibr   urllib.requestr   rQ   Zdjango.confr   Zdjango.core.management.baser   r   Zdjango.core.management.utilsr   Zdjango.templater   r	   Zdjango.utilsr
   Zdjango.utils.versionr   r   r!   r!   r!   r"   <module>   s    