summaryrefslogtreecommitdiff
path: root/g-ir-diff
blob: c27993c2b1ceb088142996072d3ce8ccba3e27ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright © 2015 Collabora Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

import os
import sys
import __builtin__

if os.name == 'nt':
    datadir = os.path.join(os.path.dirname(__file__), '..', 'share')
else:
    datadir = "/opt/gnome3/build/share"

__builtin__.__dict__['DATADIR'] = datadir

if 'GI_SCANNER_DEBUG' in os.environ:
    def on_exception(exctype, value, tb):
        print("Caught exception: %r %r" % (exctype, value))
        import pdb
        pdb.pm()
    sys.excepthook = on_exception

srcdir = os.getenv('UNINSTALLED_INTROSPECTION_SRCDIR', None)
if srcdir is not None:
    path = srcdir
else:
    # This is a private directory, we don't want to pollute the global
    # namespace.
    if os.name == 'nt':
        # Makes g-ir-scanner 'relocatable' at runtime on Windows.
        path = os.path.join(os.path.dirname(__file__),
                            '..', 'lib', 'gobject-introspection')
    else:
        # TODO
        path = os.path.join('/opt/gnome3/build/lib', 'gobject-introspection')
sys.path.insert(0, path)

import argparse
from giscanner.girparser import GIRParser
from giscanner.gircomparator import GIRComparator

# Warning categories.
WARNING_CATEGORIES = [
    'info',
    'backwards-compatibility',
    'forwards-compatibility',
]


if __name__ == '__main__':
    # Parse command line arguments.
    parser = argparse.ArgumentParser(
        description='Comparing GIR APIs for stability')
    parser.add_argument('old_file', type=str, help='Old GIR file')
    parser.add_argument('new_file', type=str, help='New GIR file')
    parser.add_argument('--warnings', dest='warnings', metavar='CATEGORY,…',
                        type=str,
                        help='Warning categories (%s)' %
                             ', '.join(WARNING_CATEGORIES))

    args = parser.parse_args()

    if not args.old_file or not args.new_file:
        parser.print_help()
        sys.exit(1)

    if args.warnings is None:
        # Enable all warnings by default
        _enabled_warnings = WARNING_CATEGORIES
    else:
        _enabled_warnings = args.warnings.split(',')

    enabled_warnings = []
    i = 0
    for category in _enabled_warnings:
        if category not in WARNING_CATEGORIES:
            parser.print_help()
            sys.exit(1)

        # TODO: this is really unneat
        enabled_warnings.append(i)
        i += 1

    # Parse the two files.
    old_parser = GIRParser()
    new_parser = GIRParser()

    try:
        filename = args.old_file
        old_parser.parse(args.old_file)
        filename = args.new_file
        new_parser.parse(args.new_file)
    except Exception as e:
        sys.stderr.write('Error parsing ‘%s’:\n' % filename)
        sys.stderr.write(e)
        sys.exit(1)

    old_namespace = old_parser.get_namespace()
    new_namespace = new_parser.get_namespace()

    # Compare the interfaces.
    comparator = GIRComparator(old_namespace, new_namespace, enabled_warnings)
    out = comparator.compare()
    comparator.print_output()
    sys.exit(out)