
v^G:                 @   s   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
 m Z d  d l m Z d  d l m Z Gd	 d
   d
 e
  Z d S)    N)apps)get_finders)staticfiles_storage)FileSystemStorage)BaseCommandCommandError)no_style)cached_propertyc                   s   e  Z d  Z d Z d Z d Z   f d d   Z e d d    Z d d	   Z	 d
 d   Z
 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   S)Commandzc
    Copies or symlinks static files from different locations to the
    settings.STATIC_ROOT.
    z*Collect static files in a single location.Fc                sP   t    j | |   g  |  _ g  |  _ g  |  _ g  |  _ t |  _ t   |  _	 d  S)N)
super__init__copied_filessymlinked_filesunmodified_filespost_processed_filesr   storager   style)selfargskwargs)	__class__ ^/tmp/pip-build-8lau8j11/django/django/contrib/staticfiles/management/commands/collectstatic.pyr      s    					zCommand.__init__c             C   s1   y |  j  j d  Wn t k
 r, d SYn Xd S)N FT)r   pathNotImplementedError)r   r   r   r   local   s
    	zCommand.localc             C   s   | j  d d d d d d d d | j  d	 d d d d
 d d | j  d d d d d g  d d d d d d | j  d d d d d d | j  d d d d d d | j  d d d d d d | j  d d d d d d d  d  S)!Nz	--noinputz
--no-inputactionstore_falsedestinteractivehelpz-Do NOT prompt the user for input of any kind.z--no-post-processpost_processz$Do NOT post process collected files.z-iz--ignoreappenddefaultignore_patternsmetavarPATTERNz`Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.z-nz	--dry-run
store_truez+Do everything except modify the filesystem.z-cz--clearz[Clear the existing files using the storage before trying to copy or link the original file.z-lz--linkz7Create a symbolic link to each file instead of copying.z--no-default-ignoreuse_default_ignore_patternszWDon't ignore the common private glob-style patterns (defaults to 'CVS', '.*' and '*~').)add_argument)r   parserr   r   r   add_arguments%   s,    zCommand.add_argumentsc             K   s   | d |  _  | d |  _ | d |  _ | d |  _ | d |  _ | d } | d rk | t j d  j 7} t t	 d	 d
   | D   |  _ | d |  _
 d S)zA
        Set instance variables based on an options dict
        r    	verbositylinkcleardry_runr%   r)   Zstaticfilesc             s   s!   |  ] } t  j j |  Vq d  S)N)osr   normpath).0pr   r   r   	<genexpr>R   s    z&Command.set_options.<locals>.<genexpr>r"   N)r    r-   symlinkr/   r0   r   Zget_app_configr%   listsetr"   )r   optionsr%   r   r   r   set_optionsF   s    

"zCommand.set_optionsc             C   s  |  j  r |  j r t d   |  j r5 |  j d  |  j  rJ |  j } n	 |  j } i  } x t   D] } x | j |  j	  D] \ } } t
 | d d  r t j j | j |  } n | } | | k r | | f | | <| | | |  q| |  j d | d d q| Wqc W|  j rt |  j d  r|  j j | d	 |  j } x | D] \ } }	 }
 t |
 t  r|  j j d
 |  |  j j d  |
  |
 r|  j d | |	 f d d |  j j |  qF|  j d |  qFWd |  j |  j d |  j d |  j i S)zx
        Perform the bulk of the work of collectstatic.

        Split off from handle() to facilitate testing.
        z&Can't symlink to a remote destination.r   prefixNzFound another file with the destination path '%s'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.level   r"   r0   zPost-processing '%s' failed!zPost-processed '%s' as '%s'   zSkipped post-processing '%s'modified
unmodifiedpost_processed)r6   r   r   r/   	clear_dir	link_file	copy_filer   r7   r%   getattrr1   r   joinr;   logr"   hasattrr   r0   
isinstance	Exceptionstderrwriter   r#   r   r   r   )r   handlerZfound_filesfinderr   r   prefixed_path	processorZoriginal_pathZprocessed_path	processedr   r   r   collectU   sH    					zCommand.collectc             K   s  |  j  |   d g } |  j r, | j d  | j d  |  j   r |  j j r |  j j } | j d |  |  j j |  o t |  j j |   } n d  } | j d  d } |  j	 r| r|  j
 r | j d  n | j d  | j d	  t d
 j |   d k rt d   |  j   } t | d  } t | d  } t | d  } |  j d k rd }	 |	 d | d d | d k rd
 n d d |  j rd n d d | rd | n d
 d | d rd | n d
 d | d rd | pd
 i }
 |
 Sd  S)N
zGYou have activated the --dry-run option so no files will be modified.

zdYou have requested to collect static files at the destination
location as specified in your settingsz:

    %s

z.

Tz-This will DELETE ALL FILES in this location!
z$This will overwrite existing files!
zNAre you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: r   yesz"Collecting static files cancelled.r?   r@   rA   r=   z^
%(modified_count)s %(identifier)s %(action)s%(destination)s%(unmodified)s%(post_processed)s.
modified_countZ
identifierzstatic filesr   Z	symlinkedcopiedZdestinationz to '%s'z, %s unmodifiedz, %s post-processed)r:   r0   r#   is_local_storager   locationexistsanylistdirr    r/   inputrF   r   rR   lenr-   r6   )r   r9   messageZdestination_pathZshould_warn_userZ	collectedrU   Zunmodified_countZpost_processed_counttemplatesummaryr   r   r   handle   sN    			zCommand.handler>   c             C   s#   |  j  | k r |  j j |  d S)z"
        Small log helper
        N)r-   stdoutrL   )r   msgr<   r   r   r   rG      s    zCommand.logc             C   s   t  |  j t  S)N)rI   r   r   )r   r   r   r   rX      s    zCommand.is_local_storagec             C   s?  |  j  j |  s d S|  j  j |  \ } } x | D] } t j j | |  } |  j rs |  j d | d d q5 |  j d | d d y |  j  j |  } Wn" t k
 r |  j  j	 |  Yq5 Xt j j |  r t j j
 |  r t j |  q5 |  j  j	 |  q5 Wx* | D]" } |  j t j j | |   qWd S)zW
        Delete the given relative path using the destination storage backend.
        NzPretending to delete '%s'r<   r=   zDeleting '%s')r   rZ   r\   r1   r   rF   r0   rG   r   deletelexistsunlinkrB   )r   r   dirsfilesfZfpath	full_pathdr   r   r   rB      s"    	%zCommand.clear_dirc       	      C   s[  |  j  j |  rWy |  j  j |  } Wn t t t f k
 rE Yn Xy | j |  } Wn t t t f k
 rv Yn X|  j r |  j  j |  } |  j t	 j j
 |  A} n d } | j d d  | j d d  k } | r| r| |  j k r|  j j |  |  j d |  d S|  j r6|  j d |  n! |  j d |  |  j  j |  d S)zR
        Check if the target file should be deleted if it already exists.
        Tmicrosecondr   zSkipping '%s' (not modified)FzPretending to delete '%s'zDeleting '%s')r   rZ   Zget_modified_timeOSErrorr   AttributeErrorr   r   r6   r1   islinkreplacer   r#   rG   r0   re   )	r   r   rO   source_storageZtarget_last_modifiedZsource_last_modifiedrk   Zcan_skip_unmodified_filesZfile_is_unmodifiedr   r   r   delete_file   s2    		zCommand.delete_filec             C   s  | |  j  k r  |  j d |  S|  j | | |  s9 d S| j |  } |  j rk |  j d | d d n|  j d | d d |  j j |  } t j t j j |  d d	 y3 t j j	 |  r t j
 |  t j | |  Wn t k
 rd
 d l } t d | j     Ynd t k
 rOd
 d l } t d | j     Yn1 t k
 r} z t |   WYd d } ~ Xn X| |  j  k r|  j  j |  d S)z*
        Attempt to link ``path``
        z&Skipping '%s' (already linked earlier)NzPretending to link '%s'r<   r=   zLinking '%s'r>   exist_okTr   z)Symlinking is not supported by Python %s.z2Symlinking is not supported in this platform (%s).)r   rG   rs   r   r0   r   r1   makedirsdirnamerf   rg   r6   ro   platformr   python_versionr   rn   r#   )r   r   rO   rr   source_pathrk   rw   er   r   r   rC   &  s4    	zCommand.link_filec          
   C   s   | |  j  k r  |  j d |  S|  j | | |  s9 d S| j |  } |  j rk |  j d | d d nC |  j d | d d | j |   } |  j j | |  Wd QRX|  j  j |  d S)z7
        Attempt to copy ``path`` with storage
        z&Skipping '%s' (already copied earlier)NzPretending to copy '%s'r<   r=   zCopying '%s'r>   )	r   rG   rs   r   r0   openr   saver#   )r   r   rO   rr   ry   Zsource_filer   r   r   rD   J  s    	zCommand.copy_file)__name__
__module____qualname____doc__r!   Zrequires_system_checksr   r	   r   r,   r:   rR   rb   rG   rX   rB   rs   rC   rD   r   r   )r   r   r
      s   	!><2$r
   )r1   Zdjango.appsr   Z"django.contrib.staticfiles.findersr   Z"django.contrib.staticfiles.storager   Zdjango.core.files.storager   Zdjango.core.management.baser   r   Zdjango.core.management.colorr   Zdjango.utils.functionalr	   r
   r   r   r   r   <module>   s   