ó
ęNXc           @   s  d  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	 d d l
 m Z d d l m Z d d l m Z d d l m Z e j   Z d   Z e j d	 k rŚ d
   Z n	 d   Z d   Z e j d	 k d  Z d   Z d d  Z d d  Z d e f d     YZ e d  Z  d   Z! d   Z" e d    Z# e d    Z$ e d    Z% e d    Z& e d d   Z' d   Z( d   Z) d   Z* d   Z+ d    Z, e d!    Z- d" Z. d#   Z/ d$   Z0 d% d&  Z1 d S('   s   
Utilities for path handling.
i’’’’N(   t   warn(   t   md5(   t   system(   t	   py3compat(   t   undocc         C   s%   t  j j |   o$ t  j |  t  j  S(   sB   Whether `path` is a directory, to which the user has write access.(   t   ost   patht   isdirt   accesst   W_OK(   R   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   _writable_dir   s    t   win32c         C   s¢   y d d l  } Wn t k
 r/ t d   n X| j j j } | j | j | j g | _ | j d  } | |  | d  } | d k s | d k r |  S| j	 Sd S(   sŗ   Get a long path name (expand ~) on Windows using ctypes.

        Examples
        --------

        >>> get_long_path_name('c:\docume~1')
        u'c:\\Documents and Settings'

        i’’’’Ns2   you need to have ctypes installed for this to worki  i    (
   t   ctypest   ImportErrort   windllt   kernel32t   GetLongPathNameWt	   c_wchar_pt   c_uintt   argtypest   create_unicode_buffert   value(   R   R   t   _GetLongPathNamet   buft   rv(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   _get_long_path_name"   s    
c         C   s   |  S(   s   Dummy no-op.(    (   R   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR   ;   s    c         C   s
   t  |   S(   s   Expand a path into its long form.

    On Windows this expands any ~ in the paths. On other platforms, it is
    a null operation.
    (   R   (   R   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   get_long_path_nameA   s    c         C   s;   | r7 |  j  d  r7 |  j d  r7 |  d d !}  q7 n  |  S(   sD    On Windows, remove leading and trailing quotes from filenames.
    t   't   "i   i’’’’(   R   R   (   R   R   (   t
   startswitht   endswith(   t   nameR   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   unquote_filenameJ   s    c         C   sT   t  j |  t j    }  t j j d  } |  j |  rP d |  t |  }  n  |  S(   s*   Reverse of :func:`os.path.expanduser`
    t   ~(	   R   t   unicode_to_strt   syst   getfilesystemencodingR   R   t
   expanduserR   t   len(   R   t   home(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   compress_userR   s
    c         C   s¢   t  j j |   }  | d k r0 t j d k } n | } t |  d | }  t  j j |   rx |  j d  rx |  d 7}  n  t  j j |   r |  St	 d |    d S(   s~  Return a valid python filename in the current directory.

    If the given name is not a file, it adds '.py' and searches again.
    Raises IOError with an informative message if the file isn't found.

    On Windows, apply Windows semantics to the filename. In particular, remove
    any quoting that has been applied to it. This option can be forced for
    testing purposes.
    R   s   .pys   File `%r` not found.N(
   R   R   R%   t   NoneR#   t   platformR    t   isfileR   t   IOError(   R   t   force_win32R   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   get_py_filename[   s    #c         C   sö   |  j  d  j  d  }  t j j |   r@ t j j |   r@ |  S| d k rU d } n t | t j  rs | f } n  xf | D]^ } | d k r t j	   } n  t
 t j j | |    } t j j |  rz t j j |  Sqz Wt d |  | f   d S(   sæ  Find a file by looking through a sequence of paths.

    This iterates through a sequence of paths looking for a file and returns
    the full, absolute path of the first occurence of the file.  If no set of
    path dirs is given, the filename is tested as is, after running through
    :func:`expandvars` and :func:`expanduser`.  Thus a simple call::

        filefind('myfile.txt')

    will find the file in the current working dir, but::

        filefind('~/myfile.txt')

    Will find the file in the users home directory.  This function does not
    automatically try any paths, such as the cwd or the user's home directory.

    Parameters
    ----------
    filename : str
        The filename to look for.
    path_dirs : str, None or sequence of str
        The sequence of paths to look for the file in.  If None, the filename
        need to be absolute or be in the cwd.  If a string, the string is
        put into a sequence and the searched.  If a sequence, walk through
        each element and join with ``filename``, calling :func:`expandvars`
        and :func:`expanduser` before testing for existence.

    Returns
    -------
    Raises :exc:`IOError` or returns absolute path to file.
    R   R   t    t   .s5   File %r does not exist in any of the search paths: %rN(   R/   (   t   stripR   R   t   isabsR+   R)   t
   isinstanceR   t   string_typest   getcwdt   expand_patht   joint   abspathR,   (   t   filenamet	   path_dirsR   t   testname(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   filefindt   s    "$	 t   HomeDirErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR=   Ŗ   s   c         C   sķ   t  j j d  } t  j j |  } t |  r¶ t  j d k r¶ yi y d d l } Wn t k
 rr d d l } n X| j	 | j
 d  } | j | d  d } | j   Wq¶ q¶ Xn  |  sÉ t |  rŁ t j | t  St d |   d S(	   s  Return the 'home' directory, as a unicode string.

    Uses os.path.expanduser('~'), and checks for writability.

    See stdlib docs for how this is determined.
    $HOME is first priority on *ALL* platforms.

    Parameters
    ----------

    require_writable : bool [default: False]
        if True:
            guarantees the return value is a writable directory, otherwise
            raises HomeDirError
        if False:
            The path is resolved, but it is not guaranteed to exist or be writable.
    R!   t   nti’’’’Ns@   Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folderst   Personali    sD   %s is not a writable dir, set $HOME environment variable to override(   R   R   R%   t   realpathR
   R   t   winregR   t   _winregt   OpenKeyt   HKEY_CURRENT_USERt   QueryValueExt   CloseR   t   cast_unicodet   fs_encodingR=   (   t   require_writablet   homedirt   wregt   key(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   get_home_dir®   s&    	c          C   sz   t  j }  t  j d k rv t j d k rv |  j d d  pN t  j j t	   d  } | rv t
 |  rv t j | t  Sn  d S(   s   Return the XDG_CONFIG_HOME, if it is defined and exists, else None.

    This is only for non-OS X posix (Linux,Unix,etc.) systems.
    t   posixt   darwint   XDG_CONFIG_HOMEs   .configN(   R   t   environR   R#   R*   t   getR)   R   R7   RO   R
   R   RI   RJ   (   t   envt   xdg(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   get_xdg_dirÜ   s    	*c          C   sz   t  j }  t  j d k rv t j d k rv |  j d d  pN t  j j t	   d  } | rv t
 |  rv t j | t  Sn  d S(   s   Return the XDG_CACHE_HOME, if it is defined and exists, else None.

    This is only for non-OS X posix (Linux,Unix,etc.) systems.
    RP   RQ   t   XDG_CACHE_HOMEs   .cacheN(   R   RS   R   R#   R*   RT   R)   R   R7   RO   R
   R   RI   RJ   (   RU   RV   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   get_xdg_cache_dirī   s    	*c          C   s!   t  d  d d l m }  |    S(   Ns5   get_ipython_dir has moved to the IPython.paths modulei’’’’(   t   get_ipython_dir(   R    t   IPython.pathsRZ   (   RZ   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyRZ      s    
c          C   s!   t  d  d d l m }  |    S(   Ns;   get_ipython_cache_dir has moved to the IPython.paths modulei’’’’(   t   get_ipython_cache_dir(   R    R[   R\   (   R\   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR\     s    
c          C   s!   t  d  d d l m }  |    S(   Ns=   get_ipython_package_dir has moved to the IPython.paths modulei’’’’(   t   get_ipython_package_dir(   R    R[   R]   (   R]   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR]     s    
c         C   s$   t  d  d d l m } | |   S(   Ns=   get_ipython_module_path has moved to the IPython.paths modulei’’’’(   t   get_ipython_module_path(   R    R[   R^   (   t
   module_strR^   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR^     s    
t   defaultc         C   s'   t  d  d d l m } | d |   S(   Ns4   locate_profile has moved to the IPython.paths modulei’’’’(   t   locate_profilet   profile(   R    R[   Ra   (   Rb   Ra   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyRa     s    
c         C   sj   t  j d k r$ |  j d d  }  n  t  j j t  j j |    }  t  j d k rf |  j d d  }  n  |  S(   sÉ   Expand $VARS and ~names in a string, like a shell

    :Examples:

       In [2]: os.environ['FOO']='test'

       In [3]: expand_path('variable FOO is $FOO')
       Out[3]: 'variable FOO is test'
    R@   s   $\t   IPYTHON_TEMP(   R   R   t   replaceR   t
   expandvarsR%   (   t   s(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR6     s    c         C   s(   d   } d j  t | |  j d    S(   s"   Unescape glob pattern in `string`.c         S   s0   x) d D]! } |  j  d j |  |  }  q W|  S(   Ns   *[]!?s   \{0}(   Rd   t   format(   Rf   t   pattern(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   unescape7  s    s   \s   \\(   R7   t   mapt   split(   t   stringRi   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   unescape_glob5  s    	c         C   s^   g  } t  j d k r t n d   } x3 |  D]+ } | j t j |  pR | |  g  q+ W| S(   s   
    Do glob expansion for each element in `args` and return a flattened list.

    Unmatched glob pattern will remain as-is in the returned list.

    R   c         S   s   |  S(   N(    (   t   x(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   <lambda>H  s    (   R#   R*   Rm   t   extendt   glob(   t   argst   expandedRi   t   a(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt	   shellglob>  s
    )c         C   se   y t  j j |   } Wn t  j k
 r- d SXx0 | D]( } t  j j |  } | | k r5 d Sq5 Wd S(   s/  Determine whether a target is out of date.

    target_outdated(target,deps) -> 1/0

    deps: list of filenames which MUST exist.
    target: single filename which may or may not exist.

    If target doesn't exist or is older than any file listed in deps, return
    true, otherwise return false.
    i   i    (   R   R   t   getmtimet   error(   t   targett   depst   target_timet   dept   dep_time(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   target_outdatedN  s    c         C   s    t  |  |  r t |  n  d S(   s÷   Update a target with a given command given a list of dependencies.

    target_update(target,deps,cmd) -> runs cmd if target is outdated.

    This is just a wrapper around target_outdated() which calls the given
    command if target is outdated.N(   R}   R   (   Rx   Ry   t   cmd(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   target_updatef  s    c         C   sE   t  d  t |  d  & } t t j | j     j   SWd QXd S(   sS   Make an MD5 hash of a file, ignoring any differences in line
    ending characters.s   filehash() is deprecatedt   rUN(   R    t   openR   R   t   str_to_bytest   readt	   hexdigest(   R   t   f(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   filehashq  s    
iĪ  c         C   sP   t  t d  s t Sd } y t j |  |  Wn t k
 rK } | j } n X| S(   s³   Hard links ``src`` to ``dst``, returning 0 or errno.

    Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't
    supported by the operating system.
    t   linki    (   t   hasattrR   t   ENOLINKR   t   OSErrort   errno(   t   srct   dstt
   link_errnot   e(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR   {  s    c         C   s  t  j j |  r6 t  j j | t  j j |    } n  t |  |  } | t j k rļ t  j |   j	 t  j |  j	 k r| d S| d t
 j d d  f } y t |  |  Wn/ y t  j |  Wn t k
 rŌ n X  n Xt  j | |  n | d k rt j |  |  n  d S(   s+  Attempts to hardlink ``src`` to ``dst``, copying if the link fails.

    Attempts to maintain the semantics of ``shutil.copy``.

    Because ``os.link`` does not overwrite files, a unique temporary file
    will be used if the target already exists, then that file will be moved
    into place.
    Ns
   -temp-%04Xi   i   i   i    i   (   R   R   R   R7   t   basenameR   R   t   EEXISTt   statt   st_inot   randomt   randintt   link_or_copyt   removeR   t   renamet   shutilt   copy(   R   R   R   t   new_dst(    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyR     s$    
$$iķ  c         C   s   t  j j |   sZ y t  j |  d | Wq t k
 rV } | j t j k rW   qW q Xn% t  j j |   s t d |    n  d S(   sń   ensure that a directory exists

    If it doesn't exist, try to create it and protect against a race condition
    if another process is doing the same.

    The default permissions are 755, which differ from os.makedirs default of 777.
    t   modes    %r exists but is not a directoryN(	   R   R   t   existst   makedirsR   R   R   R   R,   (   R   R   R   (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   ensure_dir_exists°  s    (2   t   __doc__R   R#   R   R   R   t   tempfileRq   t   warningsR    t   hashlibR   t   IPython.utils.processR   t   IPython.utilsR   t   IPython.utils.decoratorsR   R$   RJ   R
   R*   R   R   R    R(   R)   R.   R<   t	   ExceptionR=   t   FalseRO   RW   RY   RZ   R\   R]   R^   Ra   R6   Rm   Ru   R}   R   R   R   R   R   R   (    (    (    sJ   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/path.pyt   <module>   sT   						6.										$