buildtest.utils.file

This module provides some generic file and directory level operation that include the following: 1. Check if path is a File or Directory via is_file(), is_dir() 2. Create a directory via create_dir() 3. Walk a directory tree based on single extension using walk_tree() 4. Resolve path including shell and user expansion along with getting realpath to file using resolve_path() 5. Read and write a file via read_file(), write_file()

Module Contents

Functions

create_dir(dirname)

Create a directory if it doesn’t exist. If directory contains variable

is_dir(dirname)

This method will check if a directory exist. If directory found we return

is_file(fname)

This method will check if file exist, if so returns True otherwise returns False

load_json(fname)

Given a filename, resolves full path to file and loads json file. This method will

read_file(filepath)

This method is used to read a file specified by argument filepath.

remove_file(fpath)

This method is responsible for removing a file. The input path is an absolute path

resolve_path(path, exist=True)

This method will resolve a file path to account for shell expansion and resolve paths in

walk_tree(root_dir, ext=None)

This method will traverse a directory tree and return list of files

write_file(filepath, content)

This method is used to write an input content to a file specified by

buildtest.utils.file.create_dir(dirname)

Create a directory if it doesn’t exist. If directory contains variable expansion ($HOME), user expansion (~) we resolve this before creating directory. If there is an error creating directory we raise an exception

Parameters

dirname (str, required) – directory path to create

Returns

creates the directory or print an exception message upon failure

Return type

Catches exception of type OSError

buildtest.utils.file.is_dir(dirname)

This method will check if a directory exist. If directory found we return True otherwise False.

Parameters

dir (str, required) – directory path

Returns

returns a boolean True/False depending on if input is a valid directory.

Return type

bool

buildtest.utils.file.is_file(fname)

This method will check if file exist, if so returns True otherwise returns False

Parameters

file (str, required) – file path

Returns

returns a boolean True/False depending on if input is a valid file.

Return type

bool

buildtest.utils.file.load_json(fname)

Given a filename, resolves full path to file and loads json file. This method will catch exception json.JSONDecodeError and raise an exception with useful message. If there is no error we return content of json file

buildtest.utils.file.read_file(filepath)

This method is used to read a file specified by argument filepath. If filepath is not a string we raise an error. We also run resolve_path to get realpath to file and account for shell or user expansion. The return from resolve_path will be a valid file or None so we check if input is an invalid file. Finally we read the file and return the content of the file as a string.

Parameters

filepath (str, required) – file name to read

Raises

BuildTestError: If filepath is not a string BuildTestError: If filepath is not valid file

Returns

return content of file as a string

Return type

str

buildtest.utils.file.remove_file(fpath)

This method is responsible for removing a file. The input path is an absolute path to file. We check for exceptions first, and return immediately before removing file.

Parameters

fpath (str, required) – full path to file

buildtest.utils.file.resolve_path(path, exist=True)

This method will resolve a file path to account for shell expansion and resolve paths in when a symlink is provided in the file. This method assumes file already exists.

Parameters

path (str, required) – file path to resolve

Returns

return realpath to file if found otherwise return None

Return type

str or None

buildtest.utils.file.walk_tree(root_dir, ext=None)

This method will traverse a directory tree and return list of files based on extension type. This method invokes is_dir() to check if directory exists before traversal.

Parameters:

Parameters
  • root_dir (str, required) – directory path to traverse

  • ext (str, optional) – file extensions to search in traversal

Returns

returns a list of file paths

Return type

list

buildtest.utils.file.write_file(filepath, content)

This method is used to write an input content to a file specified by filepath. Both filepath and content must be a str. An error is raised if filepath is not a string or a directory. If ``content is not a str, we return None since we can’t process the content for writing. Finally, we write the content to file and return. A successful write will return nothing otherwise an exception will occur during the write process.

Parameters
  • filepath (str, required) – file name to write

  • content (str, required) – content to write to file

Raises

BuildTestError: System error if filepath is not string BuildTestError: System error if filepath is a directory

Returns

Return nothing if write is successful. A system error if filepath is not str or directory. If argument content is not str we return None