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¶
|
Check if file exist and returns True/False |
|
Check if input directory exist and is a directory. If so return |
|
This method will traverse a directory tree and return list of files |
|
Create a directory if it doesn’t exist. If directory contains variable |
|
This method will resolve a file path to account for shell expansion and resolve paths in |
|
This method is used to read a file and return content of file. |
|
This method is used to write an input |
|
This method is responsible for removing a file. The input path is an absolute path |
|
Given a filename, resolves full path to file and loads json file. This method will |
- buildtest.utils.file.is_dir(dirname)[source]¶
Check if input directory exist and is a directory. If so return
True
otherwise returnsFalse
. We resolve path by invokingresolve_path()
- buildtest.utils.file.walk_tree(root_dir, ext=None)[source]¶
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.
- buildtest.utils.file.create_dir(dirname)[source]¶
Create a directory if it doesn’t exist. If directory contains variable expansion ($HOME) or user expansion (~), we resolve this before creating directory. If there is an error creating directory we raise an exception BuildTestError
- 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 and raise exception BuildTestError or returns None
- buildtest.utils.file.resolve_path(path, exist=True)[source]¶
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
- Returns
Full path to file if file exists or
exist=True
is set. We could returnNone
if path is not defined or file path doesn’t exist andexist=False
- Return type
- Raises
BuildTestError – If input path is not of type str
>>> 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
- buildtest.utils.file.read_file(filepath)[source]¶
This method is used to read a file and return content of file. If filepath is not a string we raise an error. We run
resolve_path()
to get realpath to file and account for shell or user expansion. The return 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) – File name to read
- Raises
- Returns
content of input file
- Return type
- buildtest.utils.file.write_file(filepath, content)[source]¶
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. Ifcontent
is not a str, we returnNone
since we can’t write the content to file. Finally, we write the content to file and return. A successful write will return nothing otherwise an exception will occur during the write process.
- buildtest.utils.file.remove_file(fpath)[source]¶
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) – full path to file to remove
- Raises
If fpath is not instance of
str
If fpath is not a file using
is_file()
An exception of type
OSError
when removing file viaos.remove()
- buildtest.utils.file.load_json(fname)[source]¶
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- Parameters
fname (str) – Name of file to read and load json content
- Raises
BuildTestError – Raise exception if file is not resolved via
resolve_path()
or failure to load JSON document