

_V7                 @   s   d  d l  m Z 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 d  d
 l m Z Gd d   d e  Z d S)    )unicode_literalsN)OrderedDict)get_finders)staticfiles_storage)FileSystemStorage)BaseCommandCommandError)no_style)
smart_text)inputc                   s   e  Z d  Z d Z d Z d Z   f 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)Commandzw
    Command that allows to copy or symlink static files from different
    locations to the settings.STATIC_ROOT.
    z*Collect static files in a single location.Fc                s   t  t |   j | |   g  |  _ g  |  _ g  |  _ g  |  _ t |  _ t	   |  _
 y |  j j d  Wn t k
 r d |  _ Yn
 Xd |  _ d  S)N FT)superr   __init__copied_filessymlinked_filesunmodified_filespost_processed_filesr   storager	   stylepathNotImplementedErrorlocal)selfargskwargs)	__class__ ^/tmp/pip-build-ghmbqnp_/Django/django/contrib/staticfiles/management/commands/collectstatic.pyr      s    					zCommand.__init__c             C   s  | j  d d d d d d d d d	 d
 | j  d d 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 d d d	 d | j  d d d d d d d d d	 d | j  d d  d d d d! d d d	 d" | j  d# d d d d$ d d d	 d% d  S)&Nz	--noinputz
--no-inputactionstore_falsedestinteractivedefaultT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ignore_patternsmetavarPATTERNz`Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.z-nz	--dry-run
store_truedry_runFz+Do everything except modify the filesystem.z-cz--clearclearz[Clear the existing files using the storage before trying to copy or link the original file.z-lz--linklinkz7Create a symbolic link to each file instead of copying.z--no-default-ignoreuse_default_ignore_patternszIDon't ignore the common private glob-style patterns '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 | d d	 d
 g 7} n  t t |   |  _ | d |  _ d S)zA
        Set instance variables based on an options dict
        r"   	verbosityr-   r,   r+   r'   r.   ZCVSz.*z*~r%   N)	r"   r2   symlinkr,   r+   listsetr'   r%   )r   optionsr'   r   r   r   set_options@   s    

zCommand.set_optionsc             C   s  |  j  r" |  j r" t d   n  |  j r; |  j d  n  |  j  rP |  j } n	 |  j } t   } 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 Wql W|  j rt |  j d  r|  j j | d	 |  j } x | D] \ } }	 }
 t |
 t  r|  j j d
 |  |  j j d  |
  n  |
 r|  j d | |	 f d d |  j j |  qO|  j d |  qOWn  i |  j |  j d 6|  j d 6|  j d 6S)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%   r+   zPost-processing '%s' failed!zPost-processed '%s' as '%s'zSkipped post-processing '%s'modified
unmodifiedpost_processed)r3   r   r   r,   	clear_dir	link_file	copy_filer   r   r4   r'   getattrosr   joinr8   logr%   hasattrr   r+   
isinstance	Exceptionstderrwriter   r&   r   r   r   )r   handlerZfound_filesfinderr   r   prefixed_path	processorZoriginal_pathZprocessed_path	processedr   r   r   collectO   sJ    						
zCommand.collectc       
      K   s  |  j  |   d g } |  j r/ | j d  n  | j d  |  j   rt |  j j rt |  j j } | j d |  n d  } | j d  |  j r | j d  n | j d  | j d  |  j r t d	 j	 |   d
 k r t
 d   n  |  j   } t | d  } t | d  } t | d  } |  j d k rd } | i | d 6d | d k r`d	 n d d 6|  j rwd n d d 6| rd | n d	 d 6| d rd | n d	 d 6| d rd | pd	 d 6}	 |  j j |	  n  d  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.

z-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<   r=   r:   z^
%(modified_count)s %(identifier)s %(action)s%(destination)s%(unmodified)s%(post_processed)s.
modified_countzstatic filesZ
identifierZ	symlinkedcopiedr   z to '%s'Zdestinationz, %s unmodifiedz, %s post-processed)r7   r+   r&   is_local_storager   locationr,   r"   r   rC   r   rO   lenr2   r3   stdoutrI   )
r   r6   messageZdestination_pathZ	collectedrR   Zunmodified_countZpost_processed_counttemplatesummaryr   r   r   handle   sF    		
	$
zCommand.handle   c             C   s&   |  j  | k r" |  j j |  n  d S)z"
        Small log helper
        N)r2   rX   rI   )r   msgr9   r   r   r   rD      s    zCommand.logc             C   s   t  |  j t  S)N)rF   r   r   )r   r   r   r   rU      s    zCommand.is_local_storagec             C   s"  |  j  j |  s d S|  j  j |  \ } } x | D] } t j j | |  } |  j ry |  j d t |  d d q5 |  j d t |  d d |  j  j |  } t j j |  r t j j	 |  r t j
 |  q5 |  j  j |  q5 Wx* | D]" } |  j t j j | |   q Wd S)zX
        Deletes the given relative path using the destination storage backend.
        NzPretending to delete '%s'r9   r:   zDeleting '%s')r   existslistdirrB   r   rC   r+   rD   r
   lexistsunlinkdeleter>   )r   r   dirsfilesfZfpath	full_pathdr   r   r   r>      s    		%zCommand.clear_dirc             C   s  |  j  j |  r~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 |  } n d } | j d d  | j d d  k r=|  j	 r | r t
 j j |  p |  j	 o | o t
 j j |  s=| |  j k r%|  j j |  n  |  j d |  d Sn  |  j rZ|  j d |  q~|  j d |  |  j  j |  n  d S)	zR
        Checks if the target file should be deleted if it already exists
        Nmicrosecondr   zSkipping '%s' (not modified)FzPretending to delete '%s'zDeleting '%s'T)r   r_   Zmodified_timeOSErrorr   AttributeErrorr   r   replacer3   rB   islinkr   r&   rD   r+   rc   )r   r   rL   source_storageZtarget_last_modifiedZsource_last_modifiedrg   r   r   r   delete_file   s6    		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 |  } y t j t j j |   Wn t	 k
 r Yn Xy6 t j j
 |  r t j |  n  t j | |  Wn t k
 r2d d l } t d | j     Ynd t k
 red d l } t d	 | j     Yn1 t	 k
 r} z t |   WYd d } ~ Xn X| |  j  k r|  j  j |  n  d S)
z*
        Attempt to link ``path``
        z&Skipping '%s' (already linked earlier)NzPretending to link '%s'r9   r:   zLinking '%s'r   z)Symlinking is not supported by Python %s.z2Symlinking is not supported in this platform (%s).)r   rD   ro   r   r+   r   rB   makedirsdirnamerj   ra   rb   r3   rk   platformr   python_versionr   r&   )r   r   rL   rn   source_pathrg   rr   er   r   r   r?     s:    	zCommand.link_filec          
   C   s   | |  j  k r  |  j d |  S|  j | | |  s9 d S| j |  } |  j rk |  j d | d d nB |  j d | d d | j |   } |  j j | |  Wd QX|  j  j |  d S)z7
        Attempt to copy ``path`` with storage
        z&Skipping '%s' (already copied earlier)NzPretending to copy '%s'r9   r:   zCopying '%s')	r   rD   ro   r   r+   openr   saver&   )r   r   rL   rn   rt   Zsource_filer   r   r   r@   9  s    	zCommand.copy_file)__name__
__module____qualname____doc__r$   Zrequires_system_checksr   r1   r7   rO   r\   rD   rU   r>   ro   r?   r@   r   r   )r   r   r      s   ?6,'r   )
__future__r   rB   collectionsr   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.encodingr
   Zdjango.utils.six.movesr   r   r   r   r   r   <module>   s   