import filecmp
import os

from lib_python import steps, execution, logger


def compare_files(params):
    first_file = params["first_file"]
    second_file = params["second_file"]

    logger.debug("Comparing file content of files: {0} and {1}".format(first_file, second_file))

    if not filecmp.cmp(first_file, second_file, shallow=False):
        # stops step execution
        execution.error("Files {0} and {1} DO NOT match".format(first_file, second_file))

    execution.passed()


steps.new_step("Common - Check that file {first_file} and file {second_file} are identical", compare_files)


def delete_file(params):
    file_name = params["file_name"]

    try:
        os.remove(file_name)
    except OSError as er:
        logger.error("Unable to remove file: {0}, original error: {1}".format(file_name, repr(er)))
        execution.error("Unable to remove file: {0}, original error {1}".format(file_name, repr(er)))

    execution.passed()


steps.new_step("Common - Delete file: {file_name}", delete_file)
