vistir.path module

vistir.path.check_for_unc_path(path)[source]

Checks to see if a pathlib Path object is a unc path or not.

vistir.path.get_converted_relative_path(path, relative_to=None)[source]

Convert path to be relative.

Given a vague relative path, return the path relative to the given location.

Parameters:
  • path (str) – The location of a target path
  • relative_to (str) – The starting path to build against, optional
Returns:

A relative posix-style path with a leading ./

This performs additional conversion to ensure the result is of POSIX form, and starts with ./, or is precisely ..

>>> os.chdir('/home/user/code/myrepo/myfolder')
>>> vistir.path.get_converted_relative_path('/home/user/code/file.zip')
'./../../file.zip'
>>> vistir.path.get_converted_relative_path('/home/user/code/myrepo/myfolder/mysubfolder')
'./mysubfolder'
>>> vistir.path.get_converted_relative_path('/home/user/code/myrepo/myfolder')
'.'
vistir.path.handle_remove_readonly(func, path, exc)[source]

Error handler for shutil.rmtree.

Windows source repo folders are read-only by default, so this error handler attempts to set them as writeable and then proceed with deletion.

Parameters:
  • func (function) – The caller function
  • path (str) – The target path for removal
  • exc (Exception) – The raised exception

This function will call check is_readonly_path() before attempting to call set_write_bit() on the target path and try again.

vistir.path.normalize_path(path)[source]

Return a case-normalized absolute variable-expanded path.

Parameters:path (str) – The non-normalized path
Returns:A normalized, expanded, case-normalized path
Return type:str
vistir.path.is_in_path(path, parent)[source]

Determine if the provided full path is in the given parent root.

Parameters:
  • path (str) – The full path to check the location of.
  • parent (str) – The parent path to check for membership in
Returns:

Whether the full path is a member of the provided parent.

Return type:

bool

vistir.path.is_file_url(url)[source]

Returns true if the given url is a file url.

vistir.path.is_readonly_path(fn)[source]

Check if a provided path exists and is readonly.

Permissions check is bool(path.stat & stat.S_IREAD) or not os.access(path, os.W_OK)

vistir.path.is_valid_url(url)[source]

Checks if a given string is an url.

vistir.path.mkdir_p(newdir, mode=511)[source]

Recursively creates the target directory and all of its parents if they do not already exist. Fails silently if they do.

Parameters:newdir (str) – The directory path to ensure
Raises:OSError if a file is encountered along the way
vistir.path.ensure_mkdir_p(mode=511)[source]

Decorator to ensure mkdir_p is called to the function’s return value.

vistir.path.create_tracked_tempdir(*args, **kwargs)[source]

Create a tracked temporary directory.

This uses TemporaryDirectory, but does not remove the directory when the return value goes out of scope, instead registers a handler to cleanup on program exit.

The return value is the path to the created directory.

vistir.path.create_tracked_tempfile(*args, **kwargs)[source]

Create a tracked temporary file.

This uses the NamedTemporaryFile construct, but does not remove the file until the interpreter exits.

The return value is the file object.

vistir.path.path_to_url(path)[source]

Convert the supplied local path to a file uri.

Parameters:path (str) – A string pointing to or representing a local path
Returns:A file:// uri for the same location
Return type:str
>>> path_to_url("/home/user/code/myrepo/myfile.zip")
'file:///home/user/code/myrepo/myfile.zip'
vistir.path.rmtree(directory, ignore_errors=False, onerror=None)[source]

Stand-in for rmtree() with additional error-handling.

This version of rmtree handles read-only paths, especially in the case of index files written by certain source control systems.

Parameters:
  • directory (str) – The target directory to remove
  • ignore_errors (bool) – Whether to ignore errors, defaults to False
  • onerror (func) – An error handling function, defaults to handle_remove_readonly()

Note

Setting ignore_errors=True may cause this to silently fail to delete the path

vistir.path.safe_expandvars(value)[source]

Call os.path.expandvars if value is a string, otherwise do nothing.

vistir.path.set_write_bit(fn)[source]

Set read-write permissions for the current user on the target path. Fail silently if the path doesn’t exist.

Parameters:fn (str) – The target filename or path
Returns:None
vistir.path.url_to_path(url)[source]

Convert a valid file url to a local filesystem path.

Follows logic taken from pip’s equivalent function

vistir.path.walk_up(bottom)[source]

Mimic os.walk, but walk ‘up’ instead of down the directory tree.

From: https://gist.github.com/zdavkeos/1098474