quemb.shared.manage_scratch.WorkDir

class quemb.shared.manage_scratch.WorkDir(path, cleanup_at_end=True)

Manage a scratch area.

Upon initialisation of the object the workdir path is created, if it does not exist yet. One can either enter a path themselves, or if it is None, then the path is automatically determined by the environment, i.e. are we on SLURM, where is the scratch etc. Read more at from_environment() for more details.

Note that the / is overloaded for this class and it can be used as pathlib.Path in that regard, see example below.

If cleanup_at_end is true, then cleanup() method is registered to be called when the program terminates with no errors and deletes the scratch directory. The WorkDir also exists as a ContextManager; then the cleanup is performed when leaving the ContextManager. Again, assuming that cleanup_at_end is true.

Examples

>>> with WorkDir('./test_dir', cleanup_at_end=True) as scratch:
>>>     with open(scratch / 'test.txt', 'w') as f:
>>>         f.write('hello world')
'./test_dir' does not exist anymore, if the outer contextmanager is left
without errors.

Attributes

path: Final[Annotated[Path]]
cleanup_at_end: Final[bool]

Methods

__init__(path[, cleanup_at_end])

Method generated by attrs for class WorkDir.

cleanup([ignore_error])

Conditionally cleanup the working directory.

from_environment(*[, user_defined_root, ...])

Create a WorkDir based on the environment.