a
    ‹©lc¡.  ã                   @   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mZ e 	¡ Z
dd„ Zejdkrddd„ Znd	d„ Zd
d„ Zdd„ Zdd„ Zd/eedœdd„ZG dd„ deƒZd0edœdd„Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'Zd(d)„ Zd*d+„ Zd1d-d.„Z dS )2z
Utilities for path handling.
é    N)Úsystemc                 C   s   t j | ¡ot  | t j¡S )zBWhether `path` is a directory, to which the user has write access.)ÚosÚpathÚisdirÚaccessÚW_OK©r   © r	   úN/var/www/html/django/DPS/env/lib/python3.9/site-packages/IPython/utils/path.pyÚ_writable_dir   s    r   Úwin32c              
   C   sŒ   zddl }W n. ty: } ztdƒ|‚W Y d}~n
d}~0 0 |jjj}|j|j|jg|_| d¡}|| |dƒ}|dks~|dkr‚| S |j	S dS )zºGet a long path name (expand ~) on Windows using ctypes.

        Examples
        --------

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

        r   Nz2you need to have ctypes installed for this to worki  )
ÚctypesÚImportErrorÚwindllÚkernel32ZGetLongPathNameWÚ	c_wchar_pÚc_uintÚargtypesÚcreate_unicode_bufferÚvalue)r   r   ÚeZ_GetLongPathNameÚbufÚrvr	   r	   r
   Ú_get_long_path_name   s    
 
ÿ
r   c                 C   s   | S )zDummy no-op.r	   r   r	   r	   r
   r   5   s    c                 C   s   t | ƒS )z‡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   r	   r	   r
   Úget_long_path_name;   s    r   c                 C   s.   t j d¡}|  |¡r*d| t|ƒd…  } | S )z*Reverse of :func:`os.path.expanduser`
    ú~N)r   r   Ú
expanduserÚ
startswithÚlen)r   Úhomer	   r	   r
   Úcompress_userD   s    
r    c                 C   sN   t j | ¡} t j | ¡r| S |  d¡s>| d }t j |¡r>|S td|  ƒ‚dS )zÍ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.
    z.pyzFile `%r` not found.N)r   r   r   ÚisfileÚendswithÚIOError)ÚnameZpy_namer	   r	   r
   Úget_py_filenameL   s    
r%   )ÚfilenameÚreturnc                 C   s¦   |   d¡  d¡} tj | ¡r,tj | ¡r,| S |du r:d}nt|tƒrJ|f}|D ]B}|dkrbt ¡ }ttj 	|| ¡ƒ}tj |¡rNtj 
|¡  S qNtd| |f ƒ‚dS )aÝ  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 occurrence 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
    -------
    path : str
        returns absolute path to file.

    Raises
    ------
    IOError
    ú"ú'N)Ú Ú.z5File %r does not exist in any of the search paths: %r)Ústripr   r   Úisabsr!   Ú
isinstanceÚstrÚgetcwdÚexpand_pathÚjoinÚabspathr#   )r&   Ú	path_dirsr   Útestnamer	   r	   r
   Úfilefind]   s    '
ÿr6   c                   @   s   e Zd ZdS )ÚHomeDirErrorN)Ú__name__Ú
__module__Ú__qualname__r	   r	   r	   r
   r7   ˜   s   r7   F)r'   c                 C   s´   t j d¡}t j |¡}t|ƒs‚t jdkr‚zJddl}| |jd¡ }| 	|d¡d }W d  ƒ n1 sh0    Y  W n   Y n0 | rŽt|ƒr¤t
|tƒs J dƒ‚|S td| ƒ‚dS )	aV  Return the 'home' directory, as a unicode string.

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

    See stdlib docs for how this is determined.
    For Python <3.8, $HOME is first priority on *ALL* platforms.
    For Python >=3.8 on Windows, %HOME% is no longer considered.

    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   Úntr   Nz@Software\Microsoft\Windows\CurrentVersion\Explorer\Shell FoldersZPersonalz#Homedir should be unicode not byteszD%s is not a writable dir, set $HOME environment variable to override)r   r   r   Úrealpathr   r$   ÚwinregÚOpenKeyÚHKEY_CURRENT_USERÚQueryValueExr.   r/   r7   )Zrequire_writableÚhomedirZwregÚkeyr	   r	   r
   Úget_home_dirœ   s&    þ2ÿrC   c                  C   sN   t j} t jdkrJ|  dd¡p*t j tƒ d¡}|rJt|ƒrJt|t	ƒsFJ ‚|S dS )zˆ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.
    ÚposixÚXDG_CONFIG_HOMENz.config©
r   Úenvironr$   Úgetr   r2   rC   r   r.   r/   ©ÚenvZxdgr	   r	   r
   Úget_xdg_dirÇ   s    
rK   c                  C   sN   t j} t jdkrJ|  dd¡p*t j tƒ d¡}|rJt|ƒrJt|t	ƒsFJ ‚|S dS )z‡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.
    rD   ÚXDG_CACHE_HOMENz.cacherF   rI   r	   r	   r
   Úget_xdg_cache_dirÚ   s    
rM   c                 C   sD   t jdkr|  dd¡} t j t j | ¡¡} t jdkr@|  dd¡} | S )zÉ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;   z$\ÚIPYTHON_TEMP)r   r$   Úreplacer   Ú
expandvarsr   )Úsr	   r	   r
   r1   í   s    

r1   c                 C   s   dd„ }d  t||  d¡ƒ¡S )z"Unescape glob pattern in `string`.c                 S   s    dD ]}|   d |¡|¡} q| S )Nz*[]!?z\{0})rO   Úformat)rQ   Úpatternr	   r	   r
   Úunescape  s    zunescape_glob.<locals>.unescapeú\z\\)r2   ÚmapÚsplit)ÚstringrT   r	   r	   r
   Úunescape_glob  s    rY   c                 C   sB   g }t jdkrtndd„ }| D ]}| t |¡p8||ƒg¡ q|S )z˜
    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 )Nr	   )Úxr	   r	   r
   Ú<lambda>  ó    zshellglob.<locals>.<lambda>)ÚsysÚplatformrY   ÚextendÚglob)ÚargsÚexpandedrT   Úar	   r	   r
   Ú	shellglob  s
    rd   c                 C   sP   zt j | ¡}W n t jy&   Y dS 0 |D ]}t j |¡}||kr, dS q,dS )a/  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.
    é   r   )r   r   ÚgetmtimeÚerror)ÚtargetÚdepsZtarget_timeÚdepZdep_timer	   r	   r
   Útarget_outdated  s    rk   c                 C   s   t | |ƒrt|ƒ dS )z÷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)rk   r   )rh   ri   Úcmdr	   r	   r
   Útarget_update5  s    
rm   iÎ  c              
   C   sR   t tdƒstS d}zt | |¡ W n* tyL } z|j}W Y d}~n
d}~0 0 |S )z³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.
    Úlinkr   N)Úhasattrr   ÚENOLINKrn   ÚOSErrorÚerrno)ÚsrcÚdstÚ
link_errnor   r	   r	   r
   rn   C  s    
rn   c                 C   sÎ   t j |¡r"t j |t j | ¡¡}t| |ƒ}|tjkr¶t  | ¡j	t  |¡j	krRdS |dt
 dd¡f  }zt| |ƒ W n0   zt  |¡ W n tyž   Y n0 ‚ Y n0 t  ||¡ n|dkrÊt | |¡ dS )a+  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.
    Nz
-temp-%04Xre   i   r   )r   r   r   r2   Úbasenamern   rr   ÚEEXISTÚstatÚst_inoÚrandomÚrandintÚlink_or_copyÚremoverq   ÚrenameÚshutilÚcopy)rs   rt   ru   Znew_dstr	   r	   r
   r|   T  s$    


r|   éí  c              
   C   sp   t j | ¡sTzt j| |d W ql tyP } z|jtjkr<‚ W Y d}~qld}~0 0 nt j | ¡sltd|  ƒ‚dS )zñ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.
    )ÚmodeNz %r exists but is not a directory)	r   r   ÚexistsÚmakedirsrq   rr   rw   r   r#   )r   r‚   r   r	   r	   r
   Úensure_dir_existsx  s    r…   )N)F)r   )!Ú__doc__r   r]   rr   r   rz   r`   ZIPython.utils.processr   ÚgetfilesystemencodingÚfs_encodingr   r^   r   r   r    r%   r/   r6   Ú	Exceptionr7   rC   rK   rM   r1   rY   rd   rk   rm   rp   rn   r|   r…   r	   r	   r	   r
   Ú<module>   s:   

	;+	$