def compareFileContents(file1=None, file2=None, file1_contents=None,
                        file2_contents=None):
    """
    Compare the contents of two files.
    IMP: Only one of file1 and file1_contents should be provided.
    IMP: Only one of file2 and file2_contents should be provided.

    Parameters:
    1. file1: File 1 to be compared
    2. file2: File 2 to be compared
    3. file1_contents: Contents of file 1 to be used for comparison.
    4. file2_contents: Contents of file 2 to be used for comparison.

    Return:
    1. returnValue: True if files match
    """
    returnValue = False

    if not ((file1 is None) ^ (file1_contents is None)):
        raise AttributeError('One of file1 or file1_contents must be given')
    if not ((file2 is None) ^ (file2_contents is None)):
        raise AttributeError('One of file2 or file2_contents must be given')

    if file1_contents is None:
        try:
            fd = open(file1, 'r')
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Opening file1: {0}'.format(sys.exc_info()[1]))
        else:
            file1_contents = fd.read()
            fd.close()

    if file2_contents is None:
        try:
            fd = open(file2, 'r')
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Opening file2: {0}'.format(sys.exc_info()[1]))
        else:
            file2_contents = fd.read()
            fd.close()

    if file1_contents is not None and file2_contents is not None:
        try:
            hashtotal_file1 = hashlib.md5()
            hashtotal_file2 = hashlib.md5()
            hashtotal_file1.update(file1_contents)
            hashtotal_file2.update(file2_contents)
            returnValue = hashtotal_file1.hexdigest() == hashtotal_file2.hexdigest()
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Getting hash: {0}'.format(sys.exc_info()[1]))

    return returnValue
示例#2
0
def backup_file(filePath, maxBackups=10):
    """
    Create backup for the file.
        File.txt -> File_1.txt

    Parameters:
    1. filePath: File to be backed up
    2. maxBackups: Maximum number of backups to create in the location

    Return:
    1. returnValue: True if file back up is successful
    """
    returnValue = False
    filename, extention = os.path.splitext(filePath)

    if c_path.validate_file(filePath) and c_path.validate_file_write(filePath):
        for index in reversed(range(0, maxBackups)):
            backup_file_path = filename + '_{0}'.format(index + 1) + extention
            origFile = filename + '_{0}'.format(index) + extention
            if c_path.validate_file(backup_file_path):
                try:
                    os.remove(backup_file_path)
                except Exception:
                    logger.debug(traceback.format_exc())
                    raise CoreError(
                        CoreErrorCode.GENERIC_FAILURE,
                        'Removing file: {0}'.format(sys.exc_info()[1]))
            if c_path.validate_file(origFile):
                try:
                    os.rename(origFile, backup_file_path)
                except Exception:
                    logger.debug(traceback.format_exc())
                    raise CoreError(
                        CoreErrorCode.GENERIC_FAILURE,
                        'Renaming file: {0}'.format(sys.exc_info()[1]))

        backup_file_path = filename + '_{0}'.format(1) + extention
        f_retValue, f_retErr = c_path.copyFile(filePath,
                                               backup_file_path,
                                               force=True)
        if not f_retValue:
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                            'Backing up: {0}'.format(f_retErr))
        else:
            returnValue = True
    return returnValue
示例#3
0
def getMD5Buffer(buffer_):
    hashValue = ''
    try:
        hashtotal = hashlib.md5()
        hashtotal.update(buffer_)
        hashValue = hashtotal.hexdigest()
    except Exception:
        raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                        'Getting hash: {0}'.format(sys.exc_info()[1]))
    return hashValue
示例#4
0
def getMD5File(filePath):
    """ Return md5 checksum for a file. """
    try:
        fd = open(filePath, 'r')
        contents = fd.read()
        fd.close()
    except Exception:
        raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                        'Opening file for hash: {0}'.format(sys.exc_info()[1]))
    hashValue = getMD5Buffer(contents)
    return hashValue
示例#5
0
def getMD5Directory(directory):
    """ Return md5 checksum for a directory. """
    hashValue = ''
    try:
        hashtotal = hashlib.md5()

        for eachFolder, eachSubFolder, eachFiles in os.walk(directory):
            for eachFile in eachFiles:
                fd = open(c_path.join(eachFolder, eachFile), 'r')
                contents = fd.read()
                fd.close()
                hashtotal.update(contents)

        hashValue = hashtotal.hexdigest()
    except Exception:
        raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                        'Getting hash: {0}'.format(sys.exc_info()[1]))
    return hashValue