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

is_file(fname)

Check if file exist and returns True/False

is_dir(dirname)

Check if input directory exist and is a directory. If so return True otherwise returns False.

walk_tree(root_dir, ext=None)

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

create_dir(dirname)

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

resolve_path(path, exist=True)

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

read_file(filepath)

This method is used to read a file and return content of file.

write_file(filepath, content)

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

remove_file(fpath)

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

load_json(fname)

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

buildtest.utils.file.is_file(fname)[source]

Check if file exist and returns True/False

Parameters

fname (str) – file path to check

Returns

True if path is a file and is a realpath otherwise returns False

Return type

bool

buildtest.utils.file.is_dir(dirname)[source]

Check if input directory exist and is a directory. If so return True otherwise returns False. We resolve path by invoking resolve_path()

Parameters

dirname (str) – directory path to check

Returns

True if directory exists otherwise returns False.

Return type

bool

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.

Parameters
  • root_dir (str) – directory path to traverse

  • ext (str) – File extension to search in traversal

Returns

A list of file paths for a directory traversal based on extension type. If ext is None we retrieve all files

Return type

list

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
  • path (str) – file path to resolve

  • exist (bool) – a boolean to determine if filepath should be returned if filepath doesn’t exist on filesystem.

Returns

Full path to file if file exists or exist=True is set. We could return None if path is not defined or file path doesn’t exist and exist=False

Return type

str

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 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) – File name to read

Raises

BuildTestError

  • if filepath is invalid

  • filepath is not an instance of type str.

  • An exception can be raised if there is an issue reading file with an exception of IOError

Returns

content of input file

Return type

str

buildtest.utils.file.write_file(filepath, content)[source]

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 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.

Parameters
  • filepath (str) – file name to write

  • content (str) – content to write to file

Raises

BuildTestError

  • filepath is not str

  • filepath is directory via is_dir

  • content of file is not of type str

  • Error writing file with an exception of type IOError

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

BuildTestError

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