def _structure_check_core_logic()

in atr/tasks/checks/zipformat.py [0:0]


def _structure_check_core_logic(artifact_path: str) -> dict[str, Any]:
    """Verify the internal structure of the zip archive."""
    try:
        with zipfile.ZipFile(artifact_path, "r") as zf:
            members = zf.namelist()
            if not members:
                return {"error": "Archive is empty"}

            base_name = os.path.basename(artifact_path)
            name_part = base_name.removesuffix(".zip")
            # # TODO: Airavata has e.g. "-source-release"
            # # It would be useful if there were a function in analysis.py for stripping these
            # # But the root directory should probably always match the name of the file sans suffix
            # # (This would also be easier to implement)
            # if name_part.endswith(("-src", "-bin", "-dist")):
            #     name_part = "-".join(name_part.split("-")[:-1])
            expected_root = name_part

            root_dirs, non_rooted_files = _structure_check_core_logic_find_roots(zf, members)
            actual_root, error_msg = _structure_check_core_logic_validate_root(
                members, root_dirs, non_rooted_files, expected_root
            )

            if error_msg:
                if error_msg.startswith("Root directory mismatch"):
                    return {"warning": error_msg}
                else:
                    return {"error": error_msg}
            if actual_root:
                return {"root_dir": actual_root}
            return {"error": "Unknown structure validation error"}

    except zipfile.BadZipFile as e:
        return {"error": f"Bad zip file: {e}"}
    except FileNotFoundError:
        return {"error": "File not found"}
    except Exception as e:
        return {"error": f"Unexpected error: {e}"}