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 directory if it doesn’t exist. Runs a “try” block |
|
This method will check if a directory exist and if not found throws an exception. |
|
This method will check if file exist and if not found throws an exception. |
|
This method is used to read a file specified by argument |
|
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 directory if it doesn’t exist. Runs a “try” block to run os.makedirs() which creates all sub-directories if they dont exist. Catches exception of type OSError and prints message
Parameters:
- Parameters
dirname (string, 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 and if not found throws an exception.
Parameters:
- 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 and if not found throws an exception.
- 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.
logger
¶
-
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:
- Parameters
filepath (str, required) – file name to read
- Raises
SystemError: If filepath is not a string SystemError: If filepath is not valid file
- Returns
return content of file as a string
- Return type
str
-
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:
- 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 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:
- Parameters
filepath (str, required) – file name to write
content (str, required) – content to write to file
- Raises
SystemError: System error if filepath is not string SystemError: 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