##// END OF EJS Templates
Merge branch 'main' into shaperilio/autoreload-verbosity
Merge branch 'main' into shaperilio/autoreload-verbosity

File last commit:

r27764:aefe51c6
r27961:0aa7a071 merge
Show More
path.py
391 lines | 11.7 KiB | text/x-python | PythonLexer
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 # encoding: utf-8
"""
Utilities for path handling.
"""
MinRK
add utils.path.ensure_dir_exists...
r16486 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
import os
import sys
David Wolever
Add link_or_copy to IPython.utils.path
r11647 import errno
import shutil
import random
Takafumi Arakaki
Import glob as a module
r8066 import glob
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 from IPython.utils.process import system
Thomas Kluyver
Remove checks for pre-0.11 config files
r21039
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 #-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
Thomas Kluyver
Check return value of all directory-finding functions is unicode....
r3808 fs_encoding = sys.getfilesystemencoding()
MinRK
check for writable dirs, not just existence, in utils.path...
r4473 def _writable_dir(path):
"""Whether `path` is a directory, to which the user has write access."""
return os.path.isdir(path) and os.access(path, os.W_OK)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if sys.platform == 'win32':
def _get_long_path_name(path):
"""Get a long path name (expand ~) on Windows using ctypes.
Examples
--------
Nikita Kniazev
Fix escaping warning in _get_long_path_name doctest
r26934 >>> get_long_path_name('c:\\\\docume~1')
Srinivas Reddy Thatiparthy
remove unicode specifier
r23213 'c:\\\\Documents and Settings'
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
"""
try:
import ctypes
Ram Rachum
Fix exception causes all over the codebase
r25833 except ImportError as e:
raise ImportError('you need to have ctypes installed for this to work') from e
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
_GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
ctypes.c_uint ]
buf = ctypes.create_unicode_buffer(260)
rv = _GetLongPathName(path, buf, 260)
if rv == 0 or rv > 260:
return path
else:
return buf.value
Matthias Bussonnier
Some code cleanup in javascript and python...
r19739 else:
def _get_long_path_name(path):
"""Dummy no-op."""
return path
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
def get_long_path_name(path):
"""Expand a path into its long form.
On Windows this expands any ~ in the paths. On other platforms, it is
a null operation.
"""
return _get_long_path_name(path)
Thomas Kluyver
Clean up formatting sys info for test report
r13173 def compress_user(path):
"""Reverse of :func:`os.path.expanduser`
Antony Lee
PR cleanup according to comments.
r22438 """
Thomas Kluyver
Clean up formatting sys info for test report
r13173 home = os.path.expanduser('~')
if path.startswith(home):
path = "~" + path[len(home):]
return path
Robert Kern
BUG: break out the filename-unquoting from get_py_filename to be used in other contexts. Fix %save, in this respect.
r4696
Matthias Bussonnier
Remove many deprecation and bump traitlets to 5+...
r27272 def get_py_filename(name):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """Return a valid python filename in the current directory.
If the given name is not a file, it adds '.py' and searches again.
Robert Kern
BUG: Allow %magic argument filenames with spaces to be specified with quotes under win32.
r4688 Raises IOError with an informative message if the file isn't found.
"""
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
name = os.path.expanduser(name)
if os.path.isfile(name):
return name
satoru
Run darker as instructed
r27744 if not name.endswith(".py"):
py_name = name + ".py"
satoru
Fix #13700, consistently use the original name in error message
r27743 if os.path.isfile(py_name):
return py_name
satoru
Run darker as instructed
r27744 raise IOError("File `%r` not found." % name)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Matthias Bussonnier
reformat docstring in IPython utils
r26419 def filefind(filename: str, path_dirs=None) -> str:
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """Find a file by looking through a sequence of paths.
This iterates through a sequence of paths looking for a file and returns
luzpaz
Misc. typo fixes...
r24084 the full, absolute path of the first occurrence of the file. If no set of
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 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.
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 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.
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 Returns
-------
Matthias Bussonnier
reformat docstring in IPython utils
r26419 path : str
returns absolute path to file.
Raises
------
IOError
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 # If paths are quoted, abspath gets confused, strip them...
filename = filename.strip('"').strip("'")
# If the input is an absolute path, just check it exists
if os.path.isabs(filename) and os.path.isfile(filename):
return filename
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if path_dirs is None:
path_dirs = ("",)
Srinivas Reddy Thatiparthy
convert string_types to str
r23037 elif isinstance(path_dirs, str):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 path_dirs = (path_dirs,)
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 for path in path_dirs:
Srinivas Reddy Thatiparthy
rename py3compat.getcwd() -> os.getcwd()
r23045 if path == '.': path = os.getcwd()
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 testname = expand_path(os.path.join(path, filename))
if os.path.isfile(testname):
return os.path.abspath(testname)
Bernardo B. Marques
remove all trailling spaces
r4872
raise IOError("File %r does not exist in any of the search paths: %r" %
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 (filename, path_dirs) )
class HomeDirError(Exception):
pass
Matthias Bussonnier
More cleaning and enforcing of types....
r25290 def get_home_dir(require_writable=False) -> str:
MinRK
allow IPython to run without writable home dir...
r5384 """Return the 'home' directory, as a unicode string.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
MinRK
remove special-case in get_home_dir for frozen dists...
r11499 Uses os.path.expanduser('~'), and checks for writability.
Sebastiaan Mathot
Safely encode paths in compress_user...
r21517
MinRK
defer to stdlib for path.get_home_dir()...
r5383 See stdlib docs for how this is determined.
Nicholas Bollweg
skip %HOME% 3.8 test on windows, update docs for get_home_dir
r25223 For Python <3.8, $HOME is first priority on *ALL* platforms.
For Python >=3.8 on Windows, %HOME% is no longer considered.
Sebastiaan Mathot
Safely encode paths in compress_user...
r21517
MinRK
allow IPython to run without writable home dir...
r5384 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.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """
MinRK
defer to stdlib for path.get_home_dir()...
r5383 homedir = os.path.expanduser('~')
Paul Ivanov
fix symlinked /home issue for FreeBSD...
r6112 # Next line will make things work even when /home/ is a symlink to
# /usr/home as it is on FreeBSD, for example
homedir = os.path.realpath(homedir)
Sebastiaan Mathot
Safely encode paths in compress_user...
r21517
MinRK
restore My Documents fallback for get_home_dir on Windows
r5385 if not _writable_dir(homedir) and os.name == 'nt':
# expanduser failed, use the registry to get the 'My Documents' folder.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 try:
Matthias Bussonnier
More cleaning and enforcing of types....
r25290 import winreg as wreg
Matthias Bussonnier
correct patching ?
r25293 with wreg.OpenKey(
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 wreg.HKEY_CURRENT_USER,
Matthias Bussonnier
Use raw string when invalid escape sequence present....
r24451 r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Matthias Bussonnier
correct patching ?
r25293 ) as key:
homedir = wreg.QueryValueEx(key,'Personal')[0]
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 except:
pass
Sebastiaan Mathot
Safely encode paths in compress_user...
r21517
MinRK
allow IPython to run without writable home dir...
r5384 if (not require_writable) or _writable_dir(homedir):
Dimitri Papadopoulos
Typos found by codespell
r26875 assert isinstance(homedir, str), "Homedir should be unicode not bytes"
Matthias Bussonnier
More cleaning and enforcing of types....
r25290 return homedir
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 else:
MinRK
restore My Documents fallback for get_home_dir on Windows
r5385 raise HomeDirError('%s is not a writable dir, '
'set $HOME environment variable to override' % homedir)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
MinRK
use XDG_CONFIG_HOME if available...
r3347 def get_xdg_dir():
"""Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
don't use XDG path on OS X...
r7086 This is only for non-OS X posix (Linux,Unix,etc.) systems.
MinRK
use XDG_CONFIG_HOME if available...
r3347 """
env = os.environ
Bernardo B. Marques
remove all trailling spaces
r4872
Julien Rabinow
add optional support for xdg_config|cache_dir
r26954 if os.name == "posix":
MinRK
don't use XDG path on OS X...
r7086 # Linux, Unix, AIX, etc.
MinRK
allow IPython to run without writable home dir...
r5384 # use ~/.config if empty OR not set
MinRK
use XDG_CONFIG_HOME if available...
r3347 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
MinRK
don't use isdir=_writable_dir alias
r4474 if xdg and _writable_dir(xdg):
Matthias Bussonnier
Remove py3compat cast_unicode from utils/path....
r25353 assert isinstance(xdg, str)
return xdg
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
use XDG_CONFIG_HOME if available...
r3347 return None
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Julian Taylor
add path.get_ipython_cache_dir to retrieve a cache directory
r10230 def get_xdg_cache_dir():
"""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.
"""
env = os.environ
Julien Rabinow
add optional support for xdg_config|cache_dir
r26954 if os.name == "posix":
Julian Taylor
add path.get_ipython_cache_dir to retrieve a cache directory
r10230 # Linux, Unix, AIX, etc.
# use ~/.cache if empty OR not set
xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache')
if xdg and _writable_dir(xdg):
Matthias Bussonnier
Remove py3compat cast_unicode from utils/path....
r25353 assert isinstance(xdg, str)
return xdg
Julian Taylor
add path.get_ipython_cache_dir to retrieve a cache directory
r10230
return None
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def expand_path(s):
"""Expand $VARS and ~names in a string, like a shell
:Examples:
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 In [2]: os.environ['FOO']='test'
In [3]: expand_path('variable FOO is $FOO')
Out[3]: 'variable FOO is test'
"""
# This is a pretty subtle hack. When expand user is given a UNC path
# on Windows (\\server\share$\%username%), os.path.expandvars, removes
# the $ to get (\\server\share\%username%). I think it considered $
# alone an empty var. But, we need the $ to remains there (it indicates
# a hidden share).
if os.name=='nt':
s = s.replace('$\\', 'IPYTHON_TEMP')
s = os.path.expandvars(os.path.expanduser(s))
if os.name=='nt':
s = s.replace('IPYTHON_TEMP', '$\\')
return s
Takafumi Arakaki
Unescape failed glob patterns in shellglob
r8119 def unescape_glob(string):
"""Unescape glob pattern in `string`."""
Takafumi Arakaki
Fix unescape_glob: support escaping "\"
r8122 def unescape(s):
for pattern in '*[]!?':
s = s.replace(r'\{0}'.format(pattern), pattern)
return s
return '\\'.join(map(unescape, string.split('\\\\')))
Takafumi Arakaki
Unescape failed glob patterns in shellglob
r8119
Takafumi Arakaki
Rename globlist to shellglob
r8067 def shellglob(args):
Takafumi Arakaki
Move globlist and its test under utils.path
r8014 """
Do glob expansion for each element in `args` and return a flattened list.
Unmatched glob pattern will remain as-is in the returned list.
"""
expanded = []
Takafumi Arakaki
Do not unescape backslashes in Windows
r8646 # Do not unescape backslash in Windows as it is interpreted as
# path separator:
unescape = unescape_glob if sys.platform != 'win32' else lambda x: x
Takafumi Arakaki
Move globlist and its test under utils.path
r8014 for a in args:
Takafumi Arakaki
Do not unescape backslashes in Windows
r8646 expanded.extend(glob.glob(a) or [unescape(a)])
Takafumi Arakaki
Move globlist and its test under utils.path
r8014 return expanded
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def target_outdated(target,deps):
"""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.
"""
try:
target_time = os.path.getmtime(target)
except os.error:
return 1
for dep in deps:
dep_time = os.path.getmtime(dep)
if dep_time > target_time:
#print "For target",target,"Dep failed:",dep # dbg
#print "times (dep,tar):",dep_time,target_time # dbg
return 1
return 0
def target_update(target,deps,cmd):
"""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."""
if target_outdated(target,deps):
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 system(cmd)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Thomas Kluyver
Remove old config files on startup if they match the default (i.e. were never modified by the user)
r4177
David Wolever
Add ENOLINK constant
r11701 ENOLINK = 1998
David Wolever
Add link_or_copy to IPython.utils.path
r11647 def link(src, dst):
David Wolever
Fix a couple typos
r11650 """Hard links ``src`` to ``dst``, returning 0 or errno.
David Wolever
Add link_or_copy to IPython.utils.path
r11647
David Wolever
Add ENOLINK constant
r11701 Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't
David Wolever
Add link_or_copy to IPython.utils.path
r11647 supported by the operating system.
"""
if not hasattr(os, "link"):
David Wolever
Add ENOLINK constant
r11701 return ENOLINK
David Wolever
Add link_or_copy to IPython.utils.path
r11647 link_errno = 0
try:
os.link(src, dst)
except OSError as e:
link_errno = e.errno
return link_errno
def link_or_copy(src, dst):
"""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.
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
link_errno = link(src, dst)
if link_errno == errno.EEXIST:
Thomas Kluyver
Don't link file again if it's already a correct hard link...
r20038 if os.stat(src).st_ino == os.stat(dst).st_ino:
# dst is already a hard link to the correct file, so we don't need
# to do anything else. If we try to link and rename the file
# anyway, we get duplicate files - see http://bugs.python.org/issue21876
return
David Wolever
Add link_or_copy to IPython.utils.path
r11647 new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), )
try:
link_or_copy(src, new_dst)
except:
try:
os.remove(new_dst)
except OSError:
pass
raise
os.rename(new_dst, dst)
elif link_errno != 0:
# Either link isn't supported, or the filesystem doesn't support
# linking, or 'src' and 'dst' are on different filesystems.
shutil.copy(src, dst)
MinRK
add utils.path.ensure_dir_exists...
r16486
MinRK
change default dir permissions to 755...
r16488 def ensure_dir_exists(path, mode=0o755):
MinRK
add utils.path.ensure_dir_exists...
r16486 """ensure that a directory exists
Sebastiaan Mathot
Safely encode paths in compress_user...
r21517
MinRK
add utils.path.ensure_dir_exists...
r16486 If it doesn't exist, try to create it and protect against a race condition
if another process is doing the same.
Sebastiaan Mathot
Safely encode paths in compress_user...
r21517
MinRK
change default dir permissions to 755...
r16488 The default permissions are 755, which differ from os.makedirs default of 777.
MinRK
add utils.path.ensure_dir_exists...
r16486 """
if not os.path.exists(path):
try:
os.makedirs(path, mode=mode)
except OSError as e:
if e.errno != errno.EEXIST:
raise
MinRK
better error message when path exists but is not dir
r16487 elif not os.path.isdir(path):
raise IOError("%r exists but is not a directory" % path)