Format: application/x-python-code
⬇ Download File (10.6 KB)
   1  o
   2  
   3  ��hO0�@sdZddlZddlZddlZddlZddlZddlZddlZddlm	Z	e�
   4  �Zdd�Zej
   5  dkr6dd�Znd	d�Zd
   6  d�Zdd
   7  �Zdd�Zd/dedefdd�ZGdd�de�Zd0defdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'Zd(d)�Zd*d+�Z d1d-d.�Z!dS)2z
   8  Utilities for path handling.
   9  �N)�systemcCstj�|�ot�|tj�S)zBWhether `path` is a directory, to which the user has write access.)�os�path�isdir�access�W_OK�r�r	�Hc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/utils/path.py�
  10  _writable_dirsr�win32c
  11  Cs|zddl}Wnty}ztd�|�d}~ww|jjj}|j|j|jg|_|�d�}|||d�}|dks9|dkr;|S|j	S)z�Get a long path name (expand ~) on Windows using ctypes.
  12  
  13          Examples
  14          --------
  15  
  16          >>> get_long_path_name('c:\\docume~1')
  17          'c:\\Documents and Settings'
  18  
  19          rNz2you need to have ctypes installed for this to worki)
  20  �ctypes�ImportError�windll�kernel32ZGetLongPathNameW�	c_wchar_p�c_uint�argtypes�create_unicode_buffer�value)rr
  21  �eZ_GetLongPathName�buf�rvr	r	r
  22  �_get_long_path_names
  23  
  24  ��
  25  �
  26  rcC�|S)zDummy no-op.r	rr	r	r
  27  r6scCst|�S)z�Expand a path into its long form.
  28  
  29      On Windows this expands any ~ in the paths. On other platforms, it is
  30      a null operation.
  31      )rrr	r	r
  32  �get_long_path_name<srcCs.tj�d�}|�|�rd|t|�d�}|S)z*Reverse of :func:`os.path.expanduser`
  33      �~N)rr�
  34  expanduser�
  35  startswith�len)r�homer	r	r
  36  �
  37  compress_userEs
  38  r!cCsJtj�|�}tj�|�r|S|�d�s|d}tj�|�r|Std|��)z�Return a valid python filename in the current directory.
  39  
  40      If the given name is not a file, it adds '.py' and searches again.
  41      Raises IOError with an informative message if the file isn't found.
  42      z.pyzFile `%r` not found.)rrr�isfile�endswith�IOError)�nameZpy_namer	r	r
  43  �get_py_filenameMs
  44  r&�filename�returncCs�|�d��d�}tj�|�rtj�|�r|S|durd}nt|t�r%|f}|D]!}|dkr1t��}ttj�	||��}tj�|�rHtj�
  45  |�Sq'td||f��)a�Find a file by looking through a sequence of paths.
  46  
  47      This iterates through a sequence of paths looking for a file and returns
  48      the full, absolute path of the first occurrence of the file.  If no set of
  49      path dirs is given, the filename is tested as is, after running through
  50      :func:`expandvars` and :func:`expanduser`.  Thus a simple call::
  51  
  52          filefind('myfile.txt')
  53  
  54      will find the file in the current working dir, but::
  55  
  56          filefind('~/myfile.txt')
  57  
  58      Will find the file in the users home directory.  This function does not
  59      automatically try any paths, such as the cwd or the user's home directory.
  60  
  61      Parameters
  62      ----------
  63      filename : str
  64          The filename to look for.
  65      path_dirs : str, None or sequence of str
  66          The sequence of paths to look for the file in.  If None, the filename
  67          need to be absolute or be in the cwd.  If a string, the string is
  68          put into a sequence and the searched.  If a sequence, walk through
  69          each element and join with ``filename``, calling :func:`expandvars`
  70          and :func:`expanduser` before testing for existence.
  71  
  72      Returns
  73      -------
  74      path : str
  75          returns absolute path to file.
  76  
  77      Raises
  78      ------
  79      IOError
  80      �"�'N)��.z5File %r does not exist in any of the search paths: %r)�striprr�isabsr"�
  81  isinstance�str�getcwd�expand_path�join�abspathr$)r'�	path_dirsrZtestnamer	r	r
  82  �filefind^s '
  83  ��r6c@seZdZdS)�HomeDirErrorN)�__name__�
  84  __module__�__qualname__r	r	r	r
  85  r7�sr7FcCs�tj�d�}tj�|�}t|�s?tjdkr?z%ddl}|�|jd��}|�	|d�d}Wd�n1s4wYWnY|rEt|�rPt
  86  |t�sNJd��|Std|��)	aVReturn the 'home' directory, as a unicode string.
  87  
  88      Uses os.path.expanduser('~'), and checks for writability.
  89  
  90      See stdlib docs for how this is determined.
  91      For Python <3.8, $HOME is first priority on *ALL* platforms.
  92      For Python >=3.8 on Windows, %HOME% is no longer considered.
  93  
  94      Parameters
  95      ----------
  96      require_writable : bool [default: False]
  97          if True:
  98              guarantees the return value is a writable directory, otherwise
  99              raises HomeDirError
 100          if False:
 101              The path is resolved, but it is not guaranteed to exist or be writable.
 102      r�ntrNz@Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders�Personalz#Homedir should be unicode not byteszD%s is not a writable dir, set $HOME environment variable to override)
 103  rrr�realpathrr%�winreg�OpenKey�HKEY_CURRENT_USER�QueryValueExr/r0r7)Zrequire_writable�homedirZwreg�keyr	r	r
 104  �get_home_dir�s*����rDcC�Ntj}tjdkr%|�dd�ptj�t�d�}|r%t|�r%t|t	�s#J�|SdS)z�Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
 105  
 106      This is only for non-OS X posix (Linux,Unix,etc.) systems.
 107      �posixZXDG_CONFIG_HOMENz.config�
 108  r�environr%�getrr3rDrr/r0��envZxdgr	r	r
 109  �get_xdg_dir��
 110  rLcCrE)z�Return the XDG_CACHE_HOME, if it is defined and exists, else None.
 111  
 112      This is only for non-OS X posix (Linux,Unix,etc.) systems.
 113      rFZXDG_CACHE_HOMENz.cacherGrJr	r	r
 114  �get_xdg_cache_dir�rMrNcCsDtjdkr|�dd�}tj�tj�|��}tjdkr |�dd�}|S)z�Expand $VARS and ~names in a string, like a shell
 115  
 116      :Examples:
 117  
 118         In [2]: os.environ['FOO']='test'
 119  
 120         In [3]: expand_path('variable FOO is $FOO')
 121         Out[3]: 'variable FOO is test'
 122      r;z$\ZIPYTHON_TEMP)rr%�replacer�
 123  expandvarsr)�sr	r	r
 124  r2�s
 125  
 126  r2cCsdd�}d�t||�d���S)z"Unescape glob pattern in `string`.cSs dD]}|�d�|�|�}q|S)Nz*[]!?z\{0})rO�format)rQ�patternr	r	r
 127  �unescapeszunescape_glob.<locals>.unescape�\z\\)r3�map�split)�stringrTr	r	r
 128  �
 129  unescape_globsrYcCsBg}tjdkr	tndd�}|D]}|�t�|�p||�g�q|S)z�
 130      Do glob expansion for each element in `args` and return a flattened list.
 131  
 132      Unmatched glob pattern will remain as-is in the returned list.
 133  
 134      rcSr�Nr	)�xr	r	r
 135  �<lambda>szshellglob.<locals>.<lambda>)�sys�platformrY�extend�glob)�args�expandedrT�ar	r	r
 136  �	shellglobs
 137  rdcCs`tjdtdd�ztj�|�}WntjyYdSw|D]}tj�|�}||kr-dSqdS)aIDetermine whether a target is out of date.
 138  
 139      target_outdated(target,deps) -> 1/0
 140  
 141      deps: list of filenames which MUST exist.
 142      target: single filename which may or may not exist.
 143  
 144      If target doesn't exist or is older than any file listed in deps, return
 145      true, otherwise return false.
 146  
 147      .. deprecated:: 8.22
 148      zY`target_outdated` is deprecated since IPython 8.22 and will be removed in future versions���
 149  stacklevel�r)�warnings�warn�DeprecationWarningrr�getmtime�error)�target�depsZtarget_time�depZdep_timer	r	r
 150  �target_outdateds 
 151  ���rqcCs*tjdtdd�t||�rt|�dSdS)aUpdate a target with a given command given a list of dependencies.
 152  
 153      target_update(target,deps,cmd) -> runs cmd if target is outdated.
 154  
 155      This is just a wrapper around target_outdated() which calls the given
 156      command if target is outdated.
 157  
 158      .. deprecated:: 8.22
 159      zW`target_update` is deprecated since IPython 8.22 and will be removed in future versionsrerfN)rirjrkrqr)rnro�cmdr	r	r
 160  �
 161  target_update=s�
 162  �rsi�c
 163  CsRttd�stSd}z	t�||�W|Sty(}z
 164  |j}WYd}~|Sd}~ww)z�Hard links ``src`` to ``dst``, returning 0 or errno.
 165  
 166      Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't
 167      supported by the operating system.
 168      �linkrN)�hasattrr�ENOLINKrt�OSError�errno)�src�dst�
 169  link_errnorr	r	r
 170  rtSs
 171  ���rtcCs�tj�|�rtj�|tj�|��}t||�}|tjkrXt�|�j	t�|�j	kr)dS|dt
 172  �dd�f}zt||�Wnzt�
 173  |�W�tyOY�wt�||�dS|dkrdt�||�dSdS)a+Attempts to hardlink ``src`` to ``dst``, copying if the link fails.
 174  
 175      Attempts to maintain the semantics of ``shutil.copy``.
 176  
 177      Because ``os.link`` does not overwrite files, a unique temporary file
 178      will be used if the target already exists, then that file will be moved
 179      into place.
 180      Nz
 181  -temp-%04Xrhir)rrrr3�basenamertrx�EEXIST�stat�st_ino�random�randint�link_or_copy�removerw�rename�shutil�copy)ryrzr{Znew_dstr	r	r
 182  r�ds*
 183  
 184  
 185  ���r���c
 186  Csrtj�|�s+z
 187  tj||d�WdSty*}z|jtjkr�WYd}~dSd}~wwtj�|�s7td|��dS)z�ensure that a directory exists
 188  
 189      If it doesn't exist, try to create it and protect against a race condition
 190      if another process is doing the same.
 191  
 192      The default permissions are 755, which differ from os.makedirs default of 777.
 193      )�modeNz %r exists but is not a directory)	rr�exists�makedirsrwrxr}rr$)rr�rr	r	r
 194  �ensure_dir_exists�s����r�rZ)F)r�)"�__doc__rr]rxr�r�r`riZIPython.utils.processr�getfilesystemencodingZfs_encodingrr^rrr!r&r0r6�	Exceptionr7rDrLrNr2rYrdrqrsrvrtr�r�r	r	r	r
 195  �<module>s>
 196  
 197  	;+	$