
 X6                 @   sJ  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 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 Gd d   d e  Z d S)    N)path)BaseCommandCommandError)handle_extensions)ContextEngine)archivesix)urlretrieve)get_docs_versionz	^([a-z]):z^([a-z])[:|]c               @   s   e  Z d  Z d Z d Z d Z d d d g Z d Z 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 d   Z d d   Z d S)TemplateCommanda  
    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
    FhttphttpsftpT.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 extensions with commas, or use -n multiple times.)add_argument)selfparser r!   N/home/ubuntu/projects/ifolica/build/django/django/core/management/templates.pyadd_arguments3   s    zTemplateCommand.add_argumentsNc       %   '   K   s  | |  _  g  |  _ | d |  _ |  j | |  | d  k r t j t j   |  } y t j |  Wq t	 k
 r } z6 | j
 t
 j k r d | } n | } t |   WYd  d  } ~ Xq Xn@ t j j t j |   } t j j |  s t d |   n  t t | d   } g  }	 x7 | d D]+ }
 |	 j t d 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 d   | j   D  } t t | i | | 6| | 6| | 6t   d 6t j d 6t j r<d n d d 6 d d } d d l m  } | j! sx| j"   n  |  j# | d |  } t$ |  d } xt j% |  D]\ } } } | | d   } | j& | |  } | rt j | |  } t j |  st j' |  qn  xC | d  d   D]1 } | j( d  sK| d k r*| j) |  q*q*Wx| D]} | j* d*  rqfn  t j | |  } t j | | | j& | |   } xE |  j+ D]: \ } } | j* |  r| d  t$ |   | } PqqWt j |  rt d# |   n  t, | d$   }  |  j-   }! Wd  QX| j* |  s]| |	 k r|! j. d%  }! t/   j0 |!  }" |" j1 |  }! |! j2 d%  }! n  t, | d&   }# |# j |!  Wd  QX|  j d	 k r|  j j d' |  n  y! t3 j4 | |  |  j5 |  Wqft	 k
 r<|  j6 j d( | |  j7 j8  YqfXqfWqW|  j r|  j d	 k rp|  j j d)  n  x@ |  j D]2 }$ t j9 |$  rt j) |$  qzt3 j: |$  qzWn  d  S)+N	verbosityz'%s' already existszBDestination directory '%s' does not exist, please create it first.r   r   c             S   s
   |  j    S)N)strip)xr!   r!   r"   <lambda>`   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>m   s    z)TemplateCommand.handle.<locals>.<genexpr>Zdocs_versionZdjango_versionz)from __future__ import unicode_literals

unicode_literalsZ
autoescapeFr   )settingstemplate   .__pycache__.pyo.pyc	.py.classzi%s already exists, overlaying a project or app into an existing directory won't replace conflicting filesrbzutf-8wbz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.
)r4   r5   r6   );app_or_projectpaths_to_remover$   validate_namer   joinosgetcwdmakedirsOSErrorerrnoEEXISTr   abspath
expanduserexiststupler   extendmapsplitstdoutwritetitler   dictr   django__version__r	   PY3Zdjango.confr/   Z
configured	configurehandle_templatelenwalkreplacemkdir
startswithremoveendswithrewrite_template_suffixesopenreaddecoder   Zfrom_stringrenderencodeshutilcopymodemake_writeablestderrstyleZNOTICEisfilermtree)%r   r9   r   targetoptionsZtop_diremessager   Zextra_filesfile	base_nameZbase_subdirZbase_directoryZcamel_case_nameZcamel_case_valuecontextr/   Z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contentr0   Znew_fileZpath_to_remover!   r!   r"   handleE   s    		")



"

					zTemplateCommand.handlec             C   s   | d k r& t  j t j d d |  S| j d  rH | d d  } n  t  j |  } t  j |  } t  j |  ry | S|  j |  r |  j	 |  } n t  j
 |  } t  j |  r |  j |  St d |  j | f   d S)z
        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.
        Nr   confzfile://   zcouldn't handle %s template %s.)r   r<   rN   __path__rW   rD   normpathisdiris_urldownloadrC   rE   extractr   r9   )r   r0   subdirZexpanded_templateZabsolute_pathr!   r!   r"   rR      s    zTemplateCommand.handle_templatec             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)Nzyou must provide %s %s nameZappZanaz^[_a-zA-Z]\w*$z
^[_a-zA-Z]z5make sure the name begins with a letter or underscorez)use only numbers, letters and underscoresz%%r is not a valid %s name. Please %s.zK%r is not a valid %s name. Please make sure the name is a valid identifier.)r   r	   PY2researchisidentifier)r   r   r9   rj   r!   r!   r"   r;      s    %		zTemplateCommand.validate_namec             C   s  d d   } d |  j  } t j d | d d  } |  j j |  | |  \ } } |  j d k ry |  j j d |  n  y% t | t	 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} qn  |
 | k rt	 j
 | |  } t j | |  | S| S)zD
        Downloads the given URL and returns the file name.
        c             S   sN   |  j  d  } | j d  d } |  j d  r> | d } n |  } | | f S)N/r1   )rstriprI   rY   )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   r1   zcontent-dispositionrr   zcontent-typer   )r9   tempfilemkdtempr:   r   r$   rJ   rK   r
   r   r<   IOErrorr   rI   getcgiparse_headersplitext	mimetypesguess_extensionr`   move)r   r   r   r   tempdirrr   r   the_pathinfori   Z	used_namecontent_dispositionr+   paramsZguessed_filenameextcontent_typeZguessed_pathr!   r!   r"   r}      s:    	%&zTemplateCommand.downloadc             C   s[   t  j |  \ } } | j   j d  rQ | d d  | } | d d  } n  | | f S)z@
        Like os.path.splitext, but takes off .tar, too
        z.tar   Nr   )	posixpathr   lowerrY   )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 r[ |  j j d |  n  y t j	 | |  | SWnG t j
 t f k
 r } z t d | | | f   WYd d } ~ Xn Xd S)	z
        Extracts the given file to a temporarily and returns
        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)r9   r   r   r:   r   r$   rJ   rK   r   r~   ZArchiveExceptionr   r   )r   rr   r   r   ri   r!   r!   r"   r~   /  s    zTemplateCommand.extractc             C   s9   d | k r d S| j  d d  d j   } | |  j k S)z;
        Returns True if the name looks like a URL
        :Fr1   r   )rI   r   url_schemes)r   r0   schemer!   r!   r"   r|   @  s    zTemplateCommand.is_urlc             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)zb
        Make sure that the file is writeable.
        Useful if our source is read-only.
        javaN)sysplatformrW   r=   accessW_OKstatS_IMODEst_modeS_IWUSRchmod)r   rr   stZnew_permissionsr!   r!   r"   rb   I  s    zTemplateCommand.make_writeabler   r   )r   )__name__
__module____qualname____doc__Zrequires_system_checksZcan_import_settingsr   Zleave_locale_alonerZ   r#   rv   rR   r;   r}   r   r~   r|   rb   r!   r!   r!   r"   r      s   
{6
	r   ) r   rA   r   r=   r   r   r`   r   r   r   r   rN   Zdjango.core.management.baser   r   Zdjango.core.management.utilsr   Zdjango.templater   r   Zdjango.utilsr   r	   Z%django.utils.six.moves.urllib.requestr
   Zdjango.utils.versionr   compileI	_drive_re_url_drive_rer   r!   r!   r!   r"   <module>   s(   