o
    bT:                     @   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
 d dlmZmZ d dlmZ d d	lmZ G d
d deZdS )    N)apps)get_finders)staticfiles_storage)Tags)FileSystemStorage)BaseCommandCommandError)no_style)cached_propertyc                       s   e Zd ZdZdZejg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 )Commandzc
    Copies or symlinks static files from different locations to the
    settings.STATIC_ROOT.
    z*Collect static files in a single location.c                    s<   t  j|i | 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__ {/var/www/secure340b-portal/env/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.pyr      s   zCommand.__init__c                 C   s(   z	| j d W dS  ty   Y dS w )N FT)r   pathNotImplementedErrorr   r   r   r   local   s   zCommand.localc              	   C   s   |j dddddd |j dddd	d |j d
ddg dddd |j ddddd |j ddddd |j ddddd |j ddddd d S )Nz	--noinputz
--no-inputstore_falseinteractivez-Do NOT prompt the user for input of any kind.)actiondest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#   defaultr$   metavarr%   z-nz	--dry-run
store_truez+Do everything except modify the filesystem.)r#   r%   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   st   |d | _ |d | _|d | _|d | _|d | _|d }|d r)|tdj7 }td	d
 |D | _|d | _	dS )zA
        Set instance variables based on an options dict
        r"   	verbositylinkcleardry_runr(   r,   staticfilesc                 S   s   h | ]}t j|qS r   )osr   normpath).0pr   r   r   	<setcomp>S   s    z&Command.set_options.<locals>.<setcomp>r&   N)
r"   r0   symlinkr2   r3   r   Zget_app_configr(   listr&   )r   optionsr(   r   r   r   set_optionsG   s   




zCommand.set_optionsc                 C   s^  | j r
| js
td| jr| d | j r| j}n| j}i }t D ]8}|| j	D ]/\}}t
|ddr<tj|j|}n|}||vrO||f||< |||| q)| jd| dd q)q!| jrt| jdr| jj|| jd	}|D ]4\}}	}
t|
tr| jd
|  | j  |
|
r| jd||	f dd | j| qn| d|  qn| 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&   )r3   zPost-processing '%s' failed!zPost-processed '%s' as '%s'   zSkipped post-processing '%s')modified
unmodifiedpost_processed)r:   r    r   r2   	clear_dir	link_file	copy_filer   r;   r(   getattrr5   r   joinr>   logr&   hasattrr   r3   
isinstance	Exceptionstderrwriter   r'   r   r   r   )r   handlerZfound_filesfinderr   r   prefixed_path	processorZoriginal_pathZprocessed_path	processedr   r   r   collectV   sX   




zCommand.collectc           	      K   sf  | j di | dg}| jr|d |d |  r:| jjr:| jj}|d|  | j|o8t| j|}n	d }|d d}| j	rh|rh| j
rQ|d n|d |d	 td
|dkrhtd|  }| jdkrt|d }t|d }t|d }d|d|dkrd
nd | jrdnd|rd| nd
|d rd| nd
|d rd| pd
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.

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?   rC   rD   rE   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#   destinationrD   rE   r   )r=   r3   r'   is_local_storager   locationexistsanylistdirr"   r2   inputrJ   r   rV   r0   lenr:   )	r   r<   messageZdestination_pathZshould_warn_user	collectedr[   Zunmodified_countZpost_processed_countr   r   r   handle   s^   



zCommand.handlerB   c                 C   s   | j |kr| j| dS dS )z"
        Small log helper
        N)r0   stdoutrP   )r   msgrA   r   r   r   rK      s   
zCommand.logc                 C   s   t | jtS r   )rM   r   r   r   r   r   r   r^      s   zCommand.is_local_storagec              	   C   s   | j |sdS | j |\}}|D ]O}tj||}| jr(| jd| dd q| jd| dd z| j |}W n tyH   | j 	| Y qw tj|s[tj
|r[t| q| j 	| q|D ]}| tj|| qddS )zW
        Delete the given relative path using the destination storage backend.
        NPretending to delete '%s'r?   r@   Deleting '%s')r   r`   rb   r5   r   rJ   r3   rK   r   deletelexistsunlinkrF   )r   r   dirsfilesfZfpath	full_pathdr   r   r   rF      s&   zCommand.clear_dirc           	   
   C   s  | j |rz| j |}W n tttfy   Y nOw z||}W n tttfy.   Y n;w | jrC| j |}| jt	j
|A  }nd}|jdd|jddk}|ri|ri|| jvr`| j| | d|  dS | jru| d|  dS | d|  | j | dS )zR
        Check if the target file should be deleted if it already exists.
        Tr   )microsecondzSkipping '%s' (not modified)Frj   rk   )r   r`   Zget_modified_timeOSErrorr   AttributeErrorr    r   r:   r5   islinkreplacer   r'   rK   r3   rl   )	r   r   rS   source_storageZtarget_last_modifiedZsource_last_modifiedrr   Zcan_skip_unmodified_filesZfile_is_unmodifiedr   r   r   delete_file   s<   


zCommand.delete_filec              
   C   s0  || j v r| d| S | |||sdS ||}| jr'| jd| dd nb| jd| dd | j|}tjtj|dd	 ztj	|rMt
| t|| W n4 tyg   d
dl}td|   tyy   d
dl}td|   ty } zt|d}~ww || j vr| j | dS dS )z*
        Attempt to link ``path``
        z&Skipping '%s' (already linked earlier)NzPretending to link '%s'r?   r@   zLinking '%s'rB   T)exist_okr   z)Symlinking is not supported by Python %s.z2Symlinking is not supported in this platform (%s).)r   rK   rz   r   r3   r   r5   makedirsdirnamerm   rn   r:   rv   platformr   python_versionr   ru   r'   )r   r   rS   ry   source_pathrr   r~   er   r   r   rG   &  s>   



zCommand.link_filec                 C   s   || j v r| d| S | |||sdS ||}| jr'| jd| dd n%| jd| dd ||}| j|| W d   n1 sGw   Y  | 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'rB   )	r   rK   rz   r   r3   openr   saver'   )r   r   rS   ry   r   Zsource_filer   r   r   rH   J  s   

zCommand.copy_file)rB   )__name__
__module____qualname____doc__r%   r   r4   Zrequires_system_checksr   r
   r    r/   r=   rV   rg   rK   r^   rF   rz   rG   rH   __classcell__r   r   r   r   r      s"    	
!>
;2$r   )r5   Zdjango.appsr   Z"django.contrib.staticfiles.findersr   Z"django.contrib.staticfiles.storager   Zdjango.core.checksr   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    