ó
öĄ\c           @  sZ  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 rd d l m Z m Z m Z m Z m Z n  e e d	 d
  Z e e d d
  Z  e e d d
  Z! e e d d
  Z" e rhe# Z$ n  d Z% d   Z& d   Z' d   Z( d   Z) e* e d  Z+ d   Z, d   Z- d   Z. d   Z/ e j0 d  Z1 d   Z2 d   Z3 e j4 d  Z5 e5 Z6 e
 j7   pe
 j8   Z9 d   Z: d   Z; e j< d    Z= d e> f d      YZ? d!   Z@ d S("   sŻ   
    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.
i’’’’(   t   print_functionN(   t   BytesIOt   StringIO(   t   path(   t   PY2t   PY3t	   text_type(   t   RemovedInSphinx30Warning(   t   Anyt   Iteratort   Listt   Tuplet   Uniont   EEXISTi    t   ENOENTt   EPIPEt   EINVALt   /c         C  s   |  j  t t j  S(   N(   t   replacet   SEPR   t   sep(   t   canonicalpath(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   os_path3   s    c         C  s   |  j  t j t  S(   s"   Return path in OS-independent form(   R   R   R   R   (   t
   nativepath(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt
   canon_path8   s    c         C  sß   | j  t  r | S|  j t  } | j t  } xO t | d  | d   D]6 \ } } | | k re Pn  | j d  | j d  qI W| | k r d St |  d k r¼ | d g k r¼ d t Sd t t |  d t j |  S(   s.   Return a relative URL from ``base`` to ``to``.i’’’’i    t    i   t   .s   ..(   t
   startswithR   t   splitt   zipt   popt   lent   join(   t   baset   tot   b2t   t2t   xt   y(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   relative_uri>   s    $!c         C  sA   y t  j |   Wn) t k
 r< t  j j |   s=   q= n Xd S(   s   Ensure that a path exists.N(   t   ost   makedirst   OSErrorR   t   isdir(   R   (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt	   ensuredirV   s
    c   	      c  s3  t  j |   } g  g  } } x | D]| } y t j |  |  } Wn1 t k
 rr t d | t f d t j q# n Xt j	 |  r | j
 |  q# | j
 |  q# W| rŗ |  | | f Vn  x[ | D]S } t j |  |  } | sļ t j |  rĮ x" t | | |  D] } | VqWqĮ qĮ W| s/|  | | f Vn  d S(   sR   Backport of os.walk from 2.6, where the *followlinks* argument was
    added.
    sU   %s:: ERROR: non-ASCII filename not supported on this filesystem encoding %r, skipped.t   fileN(   R(   t   listdirR   R    t   UnicodeErrort   printt   fs_encodingt   syst   stderrR+   t   appendt   islinkt   walk(	   t   topt   topdownt   followlinkst   namest   dirst   nondirst   namet   fullpathR%   (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyR6   e   s,    
c         c  s   x |  D]~ } xu t  j |  D]d \ } } } xR | D]J } | j |  r3 y t j t j | |   VWq} t k
 ry q} Xq3 q3 Wq Wq Wd  S(   N(   R(   R6   t   endswithR   t   getmtimeR    t   EnvironmentError(   t   dirnamest   suffixt   dirnamet   rootR;   t   filest   sfile(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   mtimes_of_files   s    c         C  sN   t  j j |  r: y t  j |  Wq: t k
 r6 q: Xn  t  j |  |  d S(   s3   Move a file, removing the destination if it exists.N(   R(   R   t   existst   unlinkR*   t   rename(   t   sourcet   dest(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   movefile   s    c         C  sA   t  j |   } t t  d  r= t  j | | j | j f  n  d S(   s!   Copy a file's modification times.t   utimeN(   R(   t   statt   hasattrRO   t   st_atimet   st_mtime(   RL   RM   t   st(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt	   copytimes   s    c         C  s_   t  j |  s# t j |  |  r[ t j |  |  y t |  |  Wq[ t k
 rW q[ Xn  d S(   s{   Copy a file and its modification times, if possible.

    Note: ``copyfile`` skips copying if the file has not been changedN(   R   RI   t   filecmpt   cmpt   shutilt   copyfileRU   R*   (   RL   RM   (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyRY   ¤   s    #s   [^a-zA-Z0-9_-]c         C  s   t  j d |   p d S(   NR   t   sphinx(   t   no_fn_ret   sub(   t   string(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   make_filenameµ   s    c         G  s  t  j d t d d | s[ t j d  } | d k	 r[ t j t |   } | g } q[ n  t	 r„ t
 j t
 j  d pz d } t j t |   j |  |  j |  Sy t j |  |  SWnB t k
 rż t j |  j d  j   |  } | j   j d  SXd S(	   s*   [DEPRECATED] strftime for unicode strings.s5   sphinx.util.osutil.ustrtime is deprecated for removalt
   stackleveli   t   SOURCE_DATE_EPOCHi   s   utf-8s   unicode-escapeN(   t   warningst   warnR   R(   t   getenvt   Nonet   timet   gmtimet   floatR   t   localet	   getlocalet   LC_TIMEt   strftimeR   t   encodet   decodet   UnicodeEncodeError(   t   formatt   argst   source_date_epocht   time_structt   enct   r(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt	   ustrftimeŗ   s    	(!c         C  s0   y t  j j |  |  SWn t k
 r+ |  SXd S(   s  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   t   relpatht
   ValueError(   R   t   start(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyRv   Ö   s    c         C  s\   t  j |   }  t |  t  rX y |  j t  }  WqX t k
 rT t d t   qX Xn  |  S(   NsA   multibyte filename not supported on this filesystem encoding (%r)(   R   t   abspatht
   isinstancet   bytesRm   R1   t   UnicodeDecodeError(   t   pathdir(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyRy   č   s    c           C  s#   t  t d  r t j   St j   S(   Nt   getcwdu(   RQ   R(   R~   t   getcwd(    (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyR   õ   s    
c         c  s4   t    } z t j |   d  VWd  t j |  Xd  S(   N(   R   R(   t   chdir(   t
   target_dirt   cwd(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   cdü   s
    		t   FileAvoidWritec           B  sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   sø  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(   t   _pathRd   t   _io(   t   selfR   (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   __init__  s    	c         C  sJ   |  j  s6 t | t  r' t   |  _  q6 t   |  _  n  |  j  j |  d  S(   N(   R   Rz   R   R   R   t   write(   R   t   data(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyR     s
    	c         C  sß   |  j  s t d   n  |  j   } |  j  j   d } d } t |  j  t  r^ d } d } n  d } y; t |  j |  # } | j	   } | | k r d SWd QXWn t
 k
 r² n Xt |  j |   } | j |  Wd QXd S(   s0   Stop accepting writes and write file, if needed.s,   FileAvoidWrite does not support empty files.Rt   t   wt   rbt   wbN(   R   t	   Exceptiont   getvaluet   closeRz   R   Rd   t   openR   t   readt   IOErrorR   (   R   t   buft   r_modet   w_modet   old_contentt   old_ft   f(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyR   "  s&    		c         C  s   |  S(   N(    (   R   (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt	   __enter__>  s    c         C  s   |  j    d  S(   N(   R   (   R   t   typet   valuet	   traceback(    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   __exit__B  s    c         C  s(   |  j  s t d   n  t |  j  |  S(   Ns=   Must write to FileAvoidWrite before other methods can be used(   R   R   t   getattr(   R   R=   (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   __getattr__F  s    	(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R    (    (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyR     s   		
			c         C  s3   t  j j |   r" t j |   n t  j |   d  S(   N(   R(   R   R+   RX   t   rmtreet   remove(   R   (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyR¤   P  s    (A   R£   t
   __future__R    t
   contextlibt   errnoRV   Rh   R(   t   reRX   R2   Re   Ra   t   ioR   R   R   t   sixR   R   R   t   sphinx.deprecationR   t   Falset   typingR   R	   R
   R   R   R   R   R   R   R   t   strt   unicodeR   R   R   R'   R,   t   TrueR6   RH   RN   RU   RY   t   compileR[   R^   Ru   t   curdirRv   t   safe_relpatht   getfilesystemencodingt   getdefaultencodingR1   Ry   R   t   contextmanagerR   t   objectR   R¤   (    (    (    s1   lib/python2.7/site-packages/sphinx/util/osutil.pyt   <module>
   sX   +					 								I