chicken_turtle_util.path

Extensions to pathlib.

assert_equals Assert 2 files are equal
assert_mode Assert last 3 octal mode digits match given mode exactly
chmod Change file mode bits
hash Hash file or directory
read Get file contents
remove Remove file or directory (recursively), unless it’s missing
write Create or overwrite file with contents
chicken_turtle_util.path.assert_mode(path, mode)[source]

Assert last 3 octal mode digits match given mode exactly

Parameters:

path : pathlib.Path

Path whose mode to assert

mode : int

Expected mode

chicken_turtle_util.path.chmod(path, mode, operator='=', recursive=False)[source]

Change file mode bits

When recursively chmodding a directory, executable bits in mode are ignored when applying to a regular file. E.g. chmod(path, mode=0o777, recursive=True) would apply mode=0o666 to regular files.

Symlinks are ignored.

Parameters:

path : Path

Path to chmod

mode : int

Mode bits to apply, e.g. 0o777.

operator : ‘+’ or ‘-‘ or ‘=’

How to apply the mode bits to the file. If ‘=’, assign mode, if ‘+’, add to current mode, if ‘-‘, subtract from current mode.

recursive : bool

Whether to chmod recursively. If recursive, applies modes in a top-down fashion, like the chmod command.

chicken_turtle_util.path.hash(path, hash_function=<built-in function openssl_sha512>)[source]

Hash file or directory

Parameters:

path : pathlib.Path

File or directory to hash

hash_function : () -> hash

Function which returns hashlib hash objects

Returns:

hash

hashlib hash object of file/directory contents. File/directory stat data is ignored. The directory digest covers file/directory contents and their location relative to the directory being digested. The directory name itself is ignored.

chicken_turtle_util.path.read(path)[source]

Get file contents

Parameters:

path : pathlib.Path

Path of file to read

Returns:

str

File contents

chicken_turtle_util.path.remove(path, force=False)[source]

Remove file or directory (recursively), unless it’s missing

On NFS file systems, if a directory contains .nfs* temporary files (sometimes created when deleting a file), it waits for them to go away.

Parameters:

path : Path

Path to remove

force : bool

If True, will remove files and directories even if they are read-only (as if first doing chmod -R +w)

chicken_turtle_util.path.write(path, contents, mode=None)[source]

Create or overwrite file with contents

Missing parent directories of path will be created.

Parameters:

path : pathlib.Path

Path to file to write to

contents : str

Contents to write to file

mode : int or None

If set, also chmod file