rez.utils.filesystem
Filesystem-related utilities.
- class rez.utils.filesystem.TempDirs(tmpdir, prefix='rez_')[source]
Bases:
objectTempdir manager.
Makes tmpdirs and ensures they’re cleaned up on program exit.
- instances = [<weakref at 0x7fbe7fb75b30; to 'TempDirs'>, <weakref at 0x7fbe7f9de4d0; to 'TempDirs'>]
- instances_lock = <unlocked _thread.lock object>
- rez.utils.filesystem.additive_copytree(src, dst, symlinks=False, ignore=None)[source]
Version of copytree that merges into an existing directory.
- rez.utils.filesystem.canonical_path(path, platform=None)[source]
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
- rez.utils.filesystem.copy_or_replace(src, dst)[source]
try to copy with mode, and if it fails, try replacing
- rez.utils.filesystem.copytree(src, dst, symlinks=False, ignore=None, hardlinks=False)[source]
copytree that supports hard-linking
- rez.utils.filesystem.decode_filesystem_name(filename)[source]
Decodes a filename encoded using the rules given in encode_filesystem_name to a unicode string.
- rez.utils.filesystem.encode_filesystem_name(input_str)[source]
Encodes an arbitrary unicode string to a generic filesystem-compatible non-unicode filename.
The result after encoding will only contain the standard ascii lowercase letters (a-z), the digits (0-9), or periods, underscores, or dashes (“.”, “_”, or “-“). No uppercase letters will be used, for comaptibility with case-insensitive filesystems.
The rules for the encoding are:
1) Any lowercase letter, digit, period, or dash (a-z, 0-9, ., or -) is encoded as-is.
Any underscore is encoded as a double-underscore (“__”)
3) Any uppercase ascii letter (A-Z) is encoded as an underscore followed by the corresponding lowercase letter (ie, “A” => “_a”)
4) All other characters are encoded using their UTF-8 encoded unicode representation, in the following format: “_NHH…, where:
a) N represents the number of bytes needed for the UTF-8 encoding, except with N=0 for one-byte representation (the exception for N=1 is made both because it means that for “standard” ascii characters in the range 0-127, their encoding will be _0xx, where xx is their ascii hex code; and because it mirrors the ways UTF-8 encoding itself works, where the number of bytes needed for the character can be determined by counting the number of leading “1”s in the binary representation of the character, except that if it is a 1-byte sequence, there are 0 leading 1’s). b) HH represents the bytes of the corresponding UTF-8 encoding, in hexadecimal (using lower-case letters)
As an example, the character “*”, whose (hex) UTF-8 representation of 2A, would be encoded as “_02a”, while the “euro” symbol, which has a UTF-8 representation of E2 82 AC, would be encoded as “_3e282ac”. (Note that, strictly speaking, the “N” part of the encoding is redundant information, since it is essentially encoded in the UTF-8 representation itself, but it makes the resulting string more human-readable, and easier to decode).
- As an example, the string “Foo_Bar (fun).txt” would get encoded as:
_foo___bar_020_028fun_029.txt
- rez.utils.filesystem.find_matching_symlink(path, source)[source]
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
- rez.utils.filesystem.forceful_rmtree(path)[source]
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
- rez.utils.filesystem.get_existing_path(path, topmost_path=None)[source]
Get the longest parent path in path that exists.
If path exists, it is returned.
- rez.utils.filesystem.is_subdirectory(path_a, path_b)[source]
Returns True if path_a is a subdirectory of path_b.
- rez.utils.filesystem.make_path_writable(path)[source]
Temporarily make path writable, if possible.
- Parameters
path (str) – Path to make temporarily writable
- rez.utils.filesystem.make_tmp_name(name)[source]
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.movetree(src, dst)[source]
Attempts a move, and falls back to a copy+delete if this fails
- rez.utils.filesystem.replace_file_or_dir(dest, source)[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.replacing_copy(src, dest, follow_symlinks=False)[source]
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.replacing_symlink(source, link_name)[source]
Create symlink that overwrites any existing target.
- rez.utils.filesystem.safe_chmod(path, mode)[source]
Set the permissions mode on path, but only if it differs from the current mode.
- rez.utils.filesystem.safe_listdir(path)[source]
Safe listdir.
Works in a multithread/proc scenario where dirs may be deleted at any time
- rez.utils.filesystem.safe_remove(path)[source]
Safely remove the given file or directory.
Works in a multithreaded scenario.