B
    \'                 @   s  d Z ddlm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ZddlmZmZ ddlmZ ddlmZmZmZ ddlmZ eeddZeed	dZeed
dZeeddZereZdZdd Zdd Z dd Z!dd Z"d2ddZ#dd Z$dd Z%dd Z&dd  Z'e(d!Z)d"d# Z*d$d% Z+ej,fd&d'Z-e-Z.e
/ p^e
0 Z1d(d) Z2d*d+ Z3ej4d,d- Z5G d.d/ d/e6Z7d0d1 Z8dS )3z
    sphinx.util.osutil
    ~~~~~~~~~~~~~~~~~~

    Operating system-related utility functions for Sphinx.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    )print_functionN)BytesIOStringIO)path)PY2PY3	text_type)RemovedInSphinx30WarningEEXISTENOENTEPIPEEINVAL/c             C   s   |  ttjS )N)replaceSEPr   sep)Zcanonicalpath r   1lib/python3.7/site-packages/sphinx/util/osutil.pyos_path3   s    r   c             C   s   |  tjtS )z"Return path in OS-independent form)r   r   r   r   )Z
nativepathr   r   r   
canon_path8   s    r   c             C   s   | tr|S | t}|t}xDt|dd |dd D ]&\}}||krPP |d |d q>W ||krtdS t|dkr|dgkrdt S dt t|d  t| S )z.Return a relative URL from ``base`` to ``to``.Nr       .z..)
startswithr   splitzippoplenjoin)basetoZb2Zt2xyr   r   r   relative_uri>   s    


$
r$   c             C   s6   yt |  W n" tk
r0   t j| s, Y nX dS )zEnsure that a path exists.N)osmakedirsOSErrorr   isdir)r   r   r   r   	ensuredirV   s
    r)   TFc       	   	   c   s   t | }g g  }}xj|D ]b}yt| |}W n, tk
rZ   td|tf tjd wY nX t	|rr|
| q|
| qW |r| ||fV  xB|D ]:}t| |}|st|sxt|||D ]
}|V  qW qW |s| ||fV  dS )zRBackport of os.walk from 2.6, where the *followlinks* argument was
    added.
    zU%s:: ERROR: non-ASCII filename not supported on this filesystem encoding %r, skipped.)fileN)r%   listdirr   r   UnicodeErrorprintfs_encodingsysstderrr(   appendislinkwalk)	toptopdownfollowlinksnamesdirsnondirsnameZfullpathr"   r   r   r   r3   e   s,    





r3   c             c   sr   xl| D ]d}x^t |D ]P\}}}xD|D ]<}||r&ytt||V  W q& tk
r`   Y q&X q&W qW qW d S )N)r%   r3   endswithr   getmtimer   EnvironmentError)Zdirnamessuffixdirnamerootr8   filesZsfiler   r   r   mtimes_of_files   s    


rB   c             C   s@   t j|r0yt | W n tk
r.   Y nX t | | dS )z3Move a file, removing the destination if it exists.N)r%   r   existsunlinkr'   rename)sourcedestr   r   r   movefile   s    rH   c             C   s,   t | }tt dr(t ||j|jf dS )z!Copy a file's modification times.utimeN)r%   stathasattrrI   st_atimest_mtime)rF   rG   str   r   r   	copytimes   s    

rO   c             C   sJ   t |rt| |sFt| | yt| | W n tk
rD   Y nX dS )z{Copy a file and its modification times, if possible.

    Note: ``copyfile`` skips copying if the file has not been changedN)r   rC   filecmpZcmpshutilcopyfilerO   r'   )rF   rG   r   r   r   rR      s    rR   z[^a-zA-Z0-9_-]c             C   s   t d| pdS )Nr   Zsphinx)no_fn_resub)stringr   r   r   make_filename   s    rV   c             G   s   t jdtdd |s:td}|dk	r:tt|}|g}trrt	
t	jd pPd}tjt| |f| |S ytj| f| S  tk
r   tj| d f| }| dS X dS )	z*[DEPRECATED] strftime for unicode strings.z5sphinx.util.osutil.ustrtime is deprecated for removal   )
stacklevelSOURCE_DATE_EPOCHNr   zutf-8zunicode-escape)warningswarnr	   r%   getenvtimeZgmtimefloatr   localeZ	getlocaleLC_TIMEZstrftimer   encodedecodeUnicodeEncodeError)formatargsZsource_date_epochZtime_structencrr   r   r   	ustrftime   s    

 rh   c             C   s(   yt j| |S  tk
r"   | S X dS )a  Return a relative filepath to *path* either from the current directory or
    from an optional *start* directory.

    This is an alternative of ``os.path.relpath()``.  This returns original path
    if *path* and *start* are on different drives (for Windows platform).
    N)r%   r   relpath
ValueError)r   startr   r   r   ri      s    ri   c             C   sH   t | } t| trDy| t} W n  tk
rB   tdt Y nX | S )NzAmultibyte filename not supported on this filesystem encoding (%r))r   abspath
isinstancebytesrb   r.   UnicodeDecodeError)Zpathdirr   r   r   rl      s    

rl   c               C   s   t tdrt S t S )Ngetcwdu)rK   r%   rp   getcwdr   r   r   r   rq      s    
rq   c          	   c   s,   t  }zt|  d V  W d t| X d S )N)rq   r%   chdir)Z
target_dircwdr   r   r   cd   s
    

rt   c               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )FileAvoidWritea  File-like object that buffers output and only writes if content changed.

    Use this class like when writing to a file to avoid touching the original
    file if the content hasn't changed. This is useful in scenarios where file
    mtime is used to invalidate caches or trigger new behavior.

    When writing to this file handle, all writes are buffered until the object
    is closed.

    Objects can be used as context managers.
    c             C   s   || _ d | _d S )N)_path_io)selfr   r   r   r   __init__  s    zFileAvoidWrite.__init__c             C   s2   | j s"t|trt | _ nt | _ | j | d S )N)rw   rm   r   r   r   write)rx   datar   r   r   rz     s
    

zFileAvoidWrite.writec          	   C   s   | j std|  }| j   d}d}t| j tr<d}d}d}y0t| j|}| }||krddS W dQ R X W n t	k
r   Y nX t| j|}|
| W dQ R X dS )z0Stop accepting writes and write file, if needed.z,FileAvoidWrite does not support empty files.rg   wrbwbN)rw   	Exceptiongetvaluecloserm   r   openrv   readIOErrorrz   )rx   ZbufZr_modeZw_modeZold_contentZold_ffr   r   r   r   "  s&    
zFileAvoidWrite.closec             C   s   | S )Nr   )rx   r   r   r   	__enter__>  s    zFileAvoidWrite.__enter__c             C   s   |    d S )N)r   )rx   typevalue	tracebackr   r   r   __exit__B  s    zFileAvoidWrite.__exit__c             C   s   | j stdt| j |S )Nz=Must write to FileAvoidWrite before other methods can be used)rw   r   getattr)rx   r:   r   r   r   __getattr__F  s    zFileAvoidWrite.__getattr__N)
__name__
__module____qualname____doc__ry   rz   r   r   r   r   r   r   r   r   ru     s   
ru   c             C   s&   t j| rt|  n
t |  d S )N)r%   r   r(   rQ   rmtreeremove)r   r   r   r   r   P  s    r   )TF)9r   Z
__future__r   
contextliberrnorP   r_   r%   rerQ   r/   r]   rZ   ior   r   r   Zsixr   r   r   Zsphinx.deprecationr	   r   r
   r   r   r   strZunicoder   r   r   r$   r)   r3   rB   rH   rO   rR   compilerS   rV   rh   curdirri   Zsafe_relpathgetfilesystemencodinggetdefaultencodingr.   rl   rq   contextmanagerrt   objectru   r   r   r   r   r   <module>
   sT   
 
I