rez.utils.filesystem

Filesystem-related utilities.

class rez.utils.filesystem.TempDirs

Bases: object

Tempdir manager.

Makes tmpdirs and ensures they’re cleaned up on program exit.

instances_lock = <unlocked _thread.lock object>
instances = [<weakref at 0x760b68171da0; to 'rez.utils.filesystem.TempDirs'>, <weakref at 0x760b67e6b1f0; to 'rez.utils.filesystem.TempDirs'>, <weakref at 0x760b668c36f0; to 'rez.utils.filesystem.TempDirs'>]
__init__(tmpdir, prefix='rez_') None
mkdtemp(cleanup: bool = True)
clear() None
classmethod clear_all() None
rez.utils.filesystem.make_path_writable(path)

Temporarily make path writable, if possible.

Parameters:

path (str) – Path to make temporarily writable

rez.utils.filesystem.retain_cwd()

Context manager that keeps cwd unchanged afterwards.

rez.utils.filesystem.get_existing_path(path, topmost_path=None)

Get the longest parent path in path that exists.

If path exists, it is returned.

Parameters:
  • path (str) – Path to test

  • topmost_path (str) – Do not test this path or above

Returns:

Existing path, or None if no path was found.

Return type:

str

rez.utils.filesystem.safe_listdir(path)

Safe listdir.

Works in a multithread/proc scenario where dirs may be deleted at any time

rez.utils.filesystem.safe_remove(path)

Safely remove the given file or directory.

Works in a multithreaded scenario.

rez.utils.filesystem.forceful_rmtree(path) None

Like shutil.rmtree, but may change permissions.

Specifically, non-writable dirs within path can cause rmtree to fail. This func chmod’s to writable to avoid this issue, if possible.

Also handled:
  • path length over 259 char (on Windows)

  • unicode path

  • AppleDouble resource forks

rez.utils.filesystem.safe_rmtree(path) None

Like shutil.rmtree, but handles race condition caused by AppleDouble files.

On Mac OSX files may consist of a data fork and a resource fork. On a foreign file system these files are stored as AppleDouble files. The data fork is stored as “filename” and the resource fork is stored as “._filename”. When the data fork is removed the corresponding resource fork is also removed. This results in a FileNotFoundError when shutil.rmtree tries to remove the resource fork. This is addressed in Python 13.3 for another situation not related to AppleDouble files (https://github.com/python/cpython/pull/14064)

Create symlink that overwrites any existing target.

rez.utils.filesystem.replacing_copy(src, dest, follow_symlinks: bool = False) None

Perform copy that overwrites any existing target.

Will copy/copytree src to dest, and will remove dest if it exists, regardless of what it is.

If follow_symlinks is False, symlinks are preserved, otherwise their contents are copied.

Note that this behavior is different to shutil.copy, which copies src into dest if dest is an existing dir.

rez.utils.filesystem.replace_file_or_dir(dest, source)

Replace dest with source.

Acts like an os.rename if dest does not exist. Otherwise, dest is deleted and src is renamed to dest.

rez.utils.filesystem.additive_copytree(src, dst, symlinks: bool = False, ignore=None) None

Version of copytree that merges into an existing directory.

rez.utils.filesystem.make_tmp_name(name)

Generates a tmp name for a file or dir.

This is a tempname that sits in the same dir as name. If it exists on disk at context exit time, it is deleted.

rez.utils.filesystem.is_subdirectory(path_a, path_b) bool

Returns True if path_a is a subdirectory of path_b.

Find a symlink under path that points at source.

If source is relative, it is considered relative to path.

Returns:

Name of symlink found, or None.

Return type:

str

rez.utils.filesystem.copy_or_replace(src: str, dst: str)

try to copy with mode, and if it fails, try replacing

rez.utils.filesystem.copytree(src: str, dst: str, symlinks: bool = False, ignore=None, hardlinks: bool = False)

copytree that supports hard-linking

rez.utils.filesystem.movetree(src: str, dst: str) None

Attempts a move, and falls back to a copy+delete if this fails

rez.utils.filesystem.safe_chmod(path: str, mode) None

Set the permissions mode on path, but only if it differs from the current mode.

rez.utils.filesystem.to_nativepath(path: str)
rez.utils.filesystem.to_ntpath(path: str)
rez.utils.filesystem.to_posixpath(path: str)
rez.utils.filesystem.canonical_path(path: str, platform=None)

Resolves symlinks, and formats filepath.

Resolves symlinks, lowercases if filesystem is case-insensitive, formats filepath using slashes appropriate for platform.

Parameters:
  • path (str) – Filepath being formatted

  • platform (rez.utils.platform_.Platform) – Indicates platform path is being formatted for. Defaults to current platform.

Returns:

Provided path, formatted for platform.

Return type:

str

rez.utils.filesystem.walk_up_dirs(path: str)

Yields absolute directories starting with the given path, and iterating up through all it’s parents, until it reaches a root directory

rez.utils.filesystem.windows_long_path(dos_path: str)

Prefix ‘?’ for path longer than 259 char (Win32API limitation)

rez.utils.filesystem.rename(src: str, dst: str)

Utility function to rename a file or folder src to dst with retrying.

This function uses the built-in os.rename() function and falls back to robocopy tool if os.rename raises a PermissionError exception.

Parameters:
  • src (str) – The original name (path) of the file or folder.

  • dst (str) – The new name (path) for the file or folder.

Raises:

OSError – If renaming fails after all attempts.