#!/usr/bin/env python
# Copyright (C) 2013, 2014 Igalia S.L.
# Copyright (C) 2013 Gustavo Noronha Silva <gns@gnome.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import common
import os
import sys
import webkitdom

EXPECTED_API_PATH = common.top_level_path('Source', 'WebCore', 'bindings', 'gobject', 'webkitdom.symbols')

def read_built_api():
    apis = set()
    for file_path in webkitdom.get_all_webkitdom_symbol_files():
        with open(file_path) as file_handle:
            apis.update(set(file_handle.readlines()))
    return apis

def read_expected_api():
    with open(EXPECTED_API_PATH) as file_handle:
        return set([symbol.strip('\n').split('@', 1)[0] + '\n' for symbol in file_handle.readlines()])

def check_api(expected_api, built_api):
    missing_api = expected_api.difference(built_api)
    new_api = built_api.difference(expected_api)

    if missing_api:
        sys.stderr.write("Missing API (API break!) detected in GObject DOM bindings\n")
        sys.stderr.write("    %s\n" % "    ".join(missing_api))
        sys.stderr.flush()

    if new_api:
        sys.stdout.write("New API detected in GObject DOM bindings\n")
        sys.stdout.write("    %s\n" % "    ".join(new_api))

    if missing_api:
        # This test can give false positives because the GObject
        # DOM bindings API varies depending on the compilation options.
        # So this shouldn't be made fatal until we figure out a way to handle it.
        # See https://bugs.webkit.org/show_bug.cgi?id=121481
        return 0

    if new_api:
        sys.stdout.write("API compatible changes found in GObject DOM bindings.\n")

    return 0

if __name__ == '__main__':
    sys.exit(check_api(read_expected_api(), read_built_api()))
