pytest_mh.utils.fs

Classes

LinuxFileSystem(*args, **kwargs)

Perform file system operations on remote host.

class pytest_mh.utils.fs.LinuxFileSystem(*args, **kwargs)

Bases: MultihostReentrantUtility[MultihostHost]

Perform file system operations on remote host.

All changes are automatically reverted when a test is finished.

Parameters:

host (MultihostHost) – Remote host instance.

mkdir(path: str, *, mode: str | None = None, user: str | None = None, group: str | None = None) None

Create directory on remote host.

Parameters:
  • path (str) – Path of the directory.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

mkdir_p(path: str, *, mode: str | None = None, user: str | None = None, group: str | None = None) None

Create directory on remote host, including all missing parent directories.

Parameters:
  • path (str) – Path of the directory.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

mktmp(contents: str | None = None, *, mode: str | None = None, user: str | None = None, group: str | None = None, dedent: bool = True) str

Create temporary file on remote host.

Parameters:
  • contents (str | None) – File contents to write.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

  • dedent (bool, optional) – Automatically dedent and strip file contents, defaults to True

Raises:

OSError – If the file can not be created.

Returns:

Temporary file path.

Return type:

str

rm(path: str) None

Remove remote file or directory.

Parameters:

path (str) – File path.

read(path: str) str

Read remote file and return its contents.

Parameters:

path (str) – File path.

Returns:

File contents.

Return type:

str

exists(path: str) bool

Checks file or directory to see if they exist.

Parameters:

path (str) – File path.

Returns:

True or False

Return type:

bool

write(path: str, contents: str, *, mode: str | None = None, user: str | None = None, group: str | None = None, dedent: bool = True) None

Write to a remote file.

Parameters:
  • path (str) – File path.

  • contents (str) – File contents to write.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

  • dedent (bool, optional) – Automatically dedent and strip file contents, defaults to True

append(path: str, contents: str, *, dedent: bool = True) None

Append to a remote file.

Parameters:
  • path (str) – File path.

  • contents (str) – File contents to write.

  • dedent (bool, optional) – Automatically dedent and strip file contents, defaults to True

touch(path: str, *, mode: str | None = None, user: str | None = None, group: str | None = None) None

Touch a remote file.

Parameters:
  • path (str) – File path.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

  • dedent (bool, optional) – Automatically dedent and strip file contents, defaults to True

truncate(path: str, *, size: int = 0) None

Truncate remote file.

Parameters:
  • path (str) – File path.

  • size (int, optional) – Target file size, defaults to 0

copy(srcpath: str, dstpath: str, *, mode: str | None = None, user: str | None = None, group: str | None = None) None

Copy a remote file @srcpath to remote @dstpath.

Parameters:
  • srcpath (str) – Remote source file path.

  • dstpath (str) – Remote destination file path.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

  • dedent (bool, optional) – Automatically dedent and strip file contents, defaults to True

upload(local_path: str, remote_path: str, *, mode: str | None = None, user: str | None = None, group: str | None = None) None

Upload local file.

Parameters:
  • local_path (str) – Source local path.

  • remote_path (str) – Destination remote path.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

upload_to_tmp(local_path: str, *, mode: str | None = None, user: str | None = None, group: str | None = None) str

Upload local file to a new temporary file on remote host.

Parameters:
  • local_path (str) – Source local path.

  • mode (str | None, optional) – Access mode (chmod value), defaults to None

  • user (str | None, optional) – Owner, defaults to None

  • group (str | None, optional) – Group, defaults to None

Returns:

Temporary file path.

Return type:

str

download(remote_path: str, local_path: str) None

Download file from remote host to local machine.

Parameters:
  • remote_path (str) – Remote path.

  • local_path (str) – Local path.

download_files(paths: list[str], local_path: str) None

Download multiple files from remote host. The files are stored in single gzipped tarball on the local machine. The remote file path may contain glob pattern.

Parameters:
  • paths (list[str]) – List of remote file paths. May contain glob pattern.

  • local_path (str) – Path to the gzipped tarball destination file on local machine.

backup(path: str) bool

Backup file or directory.

The path is automatically restored from the backup when a test is finished.

Note

It is also possible that the file or directory does not exist. In that case, the path is removed during the teardown process to remove any file or directory that might have been created.

Parameters:

path (str) – Path to back up.

Returns:

True if the path exists and backup was done, False otherwise.

Return type:

bool

restore(path: str) bool

Restore file or directory from previous backup.

Note

It is also possible that the file or directory does not exist. In that case, the path is removed to remove any file or directory that might have been created.

Parameters:

path (str) – Path to restore.

Returns:

True if the backup of path exists and it was restored, False otherwise.

Return type:

bool

wc(file: str, lines: bool = False, word: bool = False, bytes: bool = False, chars: bool = False) ProcessResult

Print newline, word, and byte counts for specific file.

Output example without additional arguments: 67 564 3514 file_name

Parameters:
  • file (str) – File whose content is counted

  • lines (bool, optional) – Print the newline counts, defaults to False

  • word (bool, optional) – Print the word counts, defaults to False

  • bytes (bool, optional) – Print the byte counts, defaults to False

  • chars (bool, optional) – Print the character counts, defaults to False

Returns:

Result of process

Return type:

ProcessResult

diff(path1: str, path2: str, *, brief: bool = False, recursive: bool = False, ignore_case: bool = False, args: list[str] | None = None) ProcessResult

Compare files line by line. Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.

Parameters:
  • path1 (str) – Path to file or directory to be compared

  • path2 (str) – Path to file or directory to be compared

  • brief (bool, optional) – Report only when files differ, but do not print the diff itself, defaults to False

  • recursive (bool, optional) – Recursively compare any subdirectories found, defaults to False

  • ignore_case (bool, optional) – Ignore case differences in file contents, defaults to False

  • args (list[str] | None, optional) – Additional options, defaults to None

Returns:

Result of process

Return type:

ProcessResult

chmod(mode: str, path: str, args: list[str] | None = None) ProcessResult

Change file/folder mode bits. Mode can be specified in two ways: octal number e.g. “666”, “444” or a symbolic representation of changes e.g. “u=rw,go=r”, “go-rw”

Parameters:
  • mode (str) – New mode of file/folder

  • path (str) – File or folder whose permissions change

  • args (list[str] | None, optional) – Additional options, defaults to None

Returns:

Result of process

Return type:

ProcessResult

chown(path: str, user: str | None = None, group: str | None = None, args: list[str] | None = None) ProcessResult

Change file owner and group.

Parameters:
  • path (str) – Path to file

  • user (str | None, optional) – New file owner, if None then user remains same, defaults to None

  • group (str | None, optional) – New file group, if None then group remains same, defaults to None

  • args (list[str] | None, optional) – Additional options, defaults to None

Returns:

Result of process

Return type:

ProcessResult

sed(command: str, path: str, args: list[str] | None = None) ProcessResult

SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, find and replace, insertion or deletion.

Parameters:
  • command (str) – Sed command

  • path (str) – File where changes will happen

  • args (list[str] | None, optional) – Additional options, defaults to None

Returns:

Result of process

Return type:

ProcessResult