3
(hS8                 @   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 d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ G dd deZdS )    )unicode_literalsN)OrderedDict)apps)get_finders)staticfiles_storage)FileSystemStorage)BaseCommandCommandError)no_style)
smart_text)cached_property)inputc                   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  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	 | _
d S )N)superr   __init__copied_filessymlinked_filesunmodified_filespost_processed_filesr   storager
   style)selfargskwargs)	__class__ `/tmp/pip-install-q3hcpn_q/Django/django/contrib/staticfiles/management/commands/collectstatic.pyr      s    zCommand.__init__c             C   s*   y| j jd W n tk
r$   dS X d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 |j ddd	dd
d |j dddg dddd |j ddddddd |j ddddddd |j ddddddd |j d dd!dd"d d S )#Nz	--noinputz
--no-inputstore_falseinteractiveTz-Do NOT prompt the user for input of any kind.)actiondestdefaulthelpz--no-post-processpost_processz$Do NOT post process collected files.z-iz--ignoreappendignore_patternsZPATTERNz`Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.)r#   r%   r$   metavarr&   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_patternszWDon't ignore the common private glob-style patterns (defaults to 'CVS', '.*' and '*~').)add_argument)r   parserr   r   r   add_arguments*   s8    zCommand.add_argumentsc             K   sn   |d | _ |d | _|d | _|d | _|d | _|d }|d rR|tjdj7 }tt	|| _|d	 | _
d
S )zA
        Set instance variables based on an options dict
        r"   	verbosityr.   r-   r,   r)   r/   Zstaticfilesr'   N)r"   r3   symlinkr-   r,   r   Zget_app_configr)   listsetr'   )r   optionsr)   r   r   r   set_optionsQ   s    




zCommand.set_optionsc             C   sv  | j r| j rtd| jr&| jd | j r4| j}n| j}t }x~t D ]t}xn|j	| j
D ]^\}}t|ddrtjj|j|}n|}||kr||f||< |||| qZ| jd| dd qZW qHW | jot| jdr^| jj|| jd	}xv|D ]n\}}	}
t|
tr"| jjd
|  | jjd |
|
rL| jd||	f dd | jj| q| jd|  qW | j| j | j| jd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.   )levelr'   )r,   zPost-processing '%s' failed!zPost-processed '%s' as '%s'zSkipped post-processing '%s')modified
unmodifiedpost_processed)r4   r    r	   r-   	clear_dir	link_file	copy_filer   r   r5   r)   getattrosr   joinr9   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   collect`   sH    


zCommand.collectc       
      K   sR  | j f | dg}| jr"|jd |jd | j rT| jjrT| jj}|jd|  nd }|jd | jrt|jd n
|jd |jd | jrtd	j	|d
krt
d| j }t|d }t|d }t|d }| jdkrNd}||d|dkrd	nd | jrdnd|rd| nd	|d r*d| nd	|d r@d| pBd	d }	|	S 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.
zstatic filesZ	symlinkedcopiedz to '%s'z, %s unmodifiedz, %s post-processed)modified_count
identifierr#   Zdestinationr=   r>   )r8   r,   r(   is_local_storager   locationr-   r"   r   rD   r	   rP   lenr3   r4   )
r   r7   messageZdestination_pathZ	collectedrU   Zunmodified_countZpost_processed_counttemplatesummaryr   r   r   handle   sF    


zCommand.handle   c             C   s   | j |kr| jj| dS )z"
        Small log helper
        N)r3   stdoutrJ   )r   msgr;   r   r   r   rE      s    
zCommand.logc             C   s   t | jtS )N)rG   r   r   )r   r   r   r   rW      s    zCommand.is_local_storagec             C   s   | j j|sdS | j j|\}}x|D ]}tjj||}| jrV| jdt| dd q&| jdt| dd y| j j|}W n  t	k
r   | j j
| Y q&X tjj| rtjj|rtj| q&| j j
| q&W x |D ]}| jtjj|| qW dS )zX
        Deletes the given relative path using the destination storage backend.
        NzPretending to delete '%s'r:   )r;   zDeleting '%s')r   existslistdirrC   r   rD   r,   rE   r   r   deletelexistsunlinkr?   )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|}W n tttfk
r8   Y nX y|j|}W n tttfk
rb   Y nrX | jrx| j j|}nd}|jdd|jddkr|r| j	t
jj|A  r|| jkr| jj| | jd|  dS | jr| jd|  n| jd|  | j j| dS )	zR
        Checks if the target file should be deleted if it already exists
        Nr   )microsecondzSkipping '%s' (not modified)FzPretending to delete '%s'zDeleting '%s'T)r   ra   Zget_modified_timeOSErrorr   AttributeErrorr    r   replacer4   rC   islinkr   r(   rE   r,   rc   )r   r   rM   source_storageZtarget_last_modifiedZsource_last_modifiedri   r   r   r   delete_file   s.    
zCommand.delete_filec             C   s^  || j kr| jd| S | j|||s*dS |j|}| jrN| jd| dd n| jd| dd | jj|}ytjtjj| W n t	k
r   Y nX y&tjj
|rtj| tj|| W n tk
r   ddl}td|j  Y nX tk
r   ddl}td	|j  Y n, t	k
r@ } zt|W Y dd}~X nX || j krZ| 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   z)Symlinking is not supported by Python %s.z2Symlinking is not supported in this platform (%s).)r   rE   rq   r   r,   r   rC   makedirsdirnamerl   rd   re   r4   rm   platformr	   python_versionr   r(   )r   r   rM   rp   source_pathri   rt   er   r   r   r@   "  s:    


zCommand.link_filec          
   C   s   || j kr| jd| S | j|||s*dS |j|}| jrN| jd| dd n6| jd| dd |j|}| jj|| W dQ R X | 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   rE   rq   r   r,   openr   saver(   )r   r   rM   rp   rv   Zsource_filer   r   r   rA   I  s    

zCommand.copy_file)r^   )__name__
__module____qualname____doc__r&   Zrequires_system_checksr   r   r    r2   r8   rP   r]   rE   rW   r?   rq   r@   rA   __classcell__r   r   )r   r   r      s   	'?6
''r   )
__future__r   rC   collectionsr   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.encodingr   Zdjango.utils.functionalr   Zdjango.utils.six.movesr   r   r   r   r   r   <module>   s   