FileSystem¶
Paths refer to the target filesystem. glob does not guarantee result
order; sort the returned list when order matters. read_str uses the target
Python process’s default text encoding.
- class rmote.tools.fs.FileSystem[source]¶
Bases:
ToolRemote filesystem operations - read, glob, and idempotent line-in-file.
Example using disposable files (the same methods can be called remotely):
>>> from tempfile import TemporaryDirectory >>> with TemporaryDirectory() as directory: ... path = str(Path(directory) / "app.conf") ... print(FileSystem.write(path, "enabled=no\n")) ... print(FileSystem.write(path, "enabled=no\n")) ... diff = FileSystem.line_in_file(path, line="enabled=yes", regexp="^enabled=") ... print(bool(diff), FileSystem.read_str(path).strip()) ... print(FileSystem.line_in_file(path, line="enabled=yes", regexp="^enabled=")) ... print(FileSystem.stat(path).is_file) ... print(FileSystem.absent(path), FileSystem.absent(path)) True False True enabled=yes True True False
- static directory(path, *, mode=493, owner=None, group=None)[source]¶
Ensure a directory exists at path with the given permissions.
Creates the directory (and any missing parents) if absent. If it already exists, applies mode, owner, and group if they differ.
- Parameters:
- Return type:
- Returns:
Trueif the directory was created or its attributes changed.
- static line_in_file(path, *, line, regexp=None, strip=True, create=False, match=LineInFileMatch.FIRST)[source]¶
Ensure line is present in the file at path, idempotently.
If regexp is given, replace the first (or all, with
match=ALL) lines that match the pattern with line. If no pattern matches, append line only when it is absent. Without regexp, append line only when it is absent.- Parameters:
path (
Text) – Path to the file to modify.line (
Text) – The desired line content to insert or substitute.regexp (
Text|None) – A regex pattern to match against existing lines. When a match is found, the matching line(s) are replaced with line.strip (
bool) – Compare lines after stripping whitespace when looking for an exact match, including when regexp has no matches. DefaultTrue.create (
bool) – Create the file if it does not exist. DefaultFalse.match (
LineInFileMatch) – Whether to replace theFIRSTmatching line orALLmatching lines.
- Return type:
- Returns:
A unified diff string describing the change, or an empty string if the file was already in the desired state.
- Raises:
FileNotFoundError – If path does not exist and create is
False.
- static stat(path)[source]¶
Return metadata for path without following symlinks.
- Parameters:
path (
Text) – Path to inspect.- Return type:
StatResult- Returns:
StatResultwith file metadata. If path does not exist, returns aStatResultwithexists=Falseand all numeric fields set to zero.
- static symlink(path, target)[source]¶
Ensure path is a symlink pointing to target.
If path already exists as the correct symlink, this is a no-op. If it exists as a wrong symlink or a regular file, it is replaced.
- static write(path, content, *, mode=420, owner=None, group=None)[source]¶
Write content to path idempotently.
All of content, mode, owner, and group are compared to the current state. Returns
Trueif any of them differed and the file was updated.- Parameters:
path (
Text) – Destination path on the remote filesystem.content (
Text|bytes) – File content as a string (UTF-8) or bytes.mode (
getint) – File permission bits (default0o644).owner (
Text|None) – Owner username;Noneleaves ownership unchanged.group (
Text|None) – Group name;Noneleaves group unchanged.
- Return type:
- Returns:
Trueif the file was created or updated,Falseif already in the desired state.