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 a directory if it doesn’t exist. If directory contains variable |
|
This method will check if a directory exist. If directory found we return |
|
This method will check if file exist, if so returns True otherwise returns False |
|
Given a filename, resolves full path to file and loads json file. This method will |
|
This method is used to read a file specified by argument |
|
This method is responsible for removing a file. The input path is an absolute path |
|
This method will resolve a file path to account for shell expansion and resolve paths in |
|
This method will traverse a directory tree and return list of files |
|
This method is used to write an input |
- 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 runresolve_path
to get realpath to file and account for shell or user expansion. The return fromresolve_path
will be a valid file orNone
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.
- param path
file path to resolve
- type path
str, required
- param exist
expects a boolean to determine if filepath should be returned or None. By default, exist is True
and file is checked using os.path.exist to return full path.
>>> a = resolve_path("$HOME/.bashrc") >>> assert a >>> b = resolve_path("$HOME/.bashrc1", exist=False) >>> assert b >>> c = resolve_path("$HOME/.bashrc1", exist=True) >>> assert not c
file is checked``os.path.exists`` after getting realpath using
os.path.realpath()
. resolve :return: return realpath to file if found otherwise return None :rtype: 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 byfilepath. 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 returnNone
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 argumentcontent
is not str we returnNone