Commit 6c129b98 authored by Asitha Senanayake's avatar Asitha Senanayake
Browse files

feat(#112): add SHA256 hash of checked file to error report

parent 27180c5b
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -715,6 +715,7 @@ def check_file(filepath_or_buffer, standard_AGS4_dictionary=None, rename_duplica
        Dictionary contains AGS4 error in input file.
    """

    import hashlib
    from python_ags4 import check

    ags_errors = {}
@@ -733,7 +734,9 @@ def check_file(filepath_or_buffer, standard_AGS4_dictionary=None, rename_duplica
        close_file = True

    try:
        # Preflight check for AGS3 files
        # Preflight check for AGS3 files and to calculate SHA256 hash of file
        sha256_hash = hashlib.sha256()

        for i, line in enumerate(f, start=1):
            ags_errors = check.is_ags3_like(line, i, ags_errors=ags_errors)

@@ -744,6 +747,9 @@ def check_file(filepath_or_buffer, standard_AGS4_dictionary=None, rename_duplica
                ags_errors = check.add_meta_data(filepath_or_buffer, standard_AGS4_dictionary, ags_errors=ags_errors, encoding=encoding)
                return ags_errors

            # Perform SHA256 checksum calculation
            sha256_hash.update(line.encode(encoding))

        # Reset file stream to the beginning to start AGS4 checks
        f.seek(0)

@@ -902,6 +908,15 @@ def check_file(filepath_or_buffer, standard_AGS4_dictionary=None, rename_duplica
        # Add metadata
        ags_errors = check.add_meta_data(filepath_or_buffer, standard_AGS4_dictionary, ags_errors=ags_errors,
                                         encoding=encoding)

        if ('AGS Format Rule 3' in ags_errors) and ('AGS3' in ags_errors['AGS Format Rule 3'][0]['desc']):
            # If AGS3 file is detected, the for loop in which the SHA256 hash is
            # calculated will be terminated, therefore report it as "Not calculated"
            ags_errors = check.add_error_msg(ags_errors, 'Metadata', 'SHA256 hash', '', 'Not calculated')

        else:
            ags_errors = check.add_error_msg(ags_errors, 'Metadata', 'SHA256 hash', '', sha256_hash.hexdigest())

        return ags_errors


+6 −0
Original line number Diff line number Diff line
@@ -782,6 +782,12 @@ def test_get_TRAN_AGS():
    assert check.get_TRAN_AGS(tables) == '4.0.4'


def test_sha256_hash():
    error_list = AGS4.check_file(TEST_DATA)

    assert error_list['Metadata'][6]['desc'] == 'ade6e26a9b48320647835a7aa957207c67708a433720a8b1b89d8c9f86b3e937'


def test_data_summary():
    error_list = AGS4.check_file('tests/test_files/4.1-rule2.ags', standard_AGS4_dictionary='python_ags4/Standard_dictionary_v4_1.ags')