gimarshallingtests: Add string_ to boxed structure
[platform/upstream/gobject-introspection.git] / giscanner / utils.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008  Johan Dahlin
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 #
20
21 import re
22 import os
23 import subprocess
24
25 _debugflags = None
26 def have_debug_flag(flag):
27     """Check for whether a specific debugging feature is enabled.
28 Well-known flags:
29  * start: Drop into debugger just after processing arguments
30  * exception: Drop into debugger on fatalexception
31  * warning: Drop into debugger on warning
32  * posttrans: Drop into debugger just before introspectable pass
33 """
34     global _debugflags
35     if _debugflags is None:
36         _debugflags = os.environ.get('GI_SCANNER_DEBUG', '').split(',')
37         if '' in _debugflags:
38             _debugflags.remove('')
39     return flag in _debugflags
40
41 def break_on_debug_flag(flag):
42     if have_debug_flag(flag):
43         import pdb
44         pdb.set_trace()
45
46 # Copied from h2defs.py
47 _upperstr_pat1 = re.compile(r'([^A-Z])([A-Z])')
48 _upperstr_pat2 = re.compile(r'([A-Z][A-Z])([A-Z][0-9a-z])')
49 _upperstr_pat3 = re.compile(r'^([A-Z])([A-Z])')
50
51
52 def to_underscores(name):
53     """Converts a typename to the equivalent underscores name.
54     This is used to form the type conversion macros and enum/flag
55     name variables.
56     In particular, and differently from to_underscores_noprefix(),
57     this function treats the first character differently if it is
58     uppercase and followed by another uppercase letter."""
59     name = _upperstr_pat1.sub(r'\1_\2', name)
60     name = _upperstr_pat2.sub(r'\1_\2', name)
61     name = _upperstr_pat3.sub(r'\1_\2', name, count=1)
62     return name
63
64
65 def to_underscores_noprefix(name):
66     """Like to_underscores, but designed for "unprefixed" names.
67     to_underscores("DBusFoo") => dbus_foo, not d_bus_foo."""
68     name = _upperstr_pat1.sub(r'\1_\2', name)
69     name = _upperstr_pat2.sub(r'\1_\2', name)
70     return name
71
72 _libtool_pat = re.compile("dlname='([A-z0-9\.\-\+]+)'\n")
73
74 def _extract_dlname_field(la_file):
75     f = open(la_file)
76     data = f.read()
77     f.close()
78     m = _libtool_pat.search(data)
79     if m:
80         return m.groups()[0]
81     else:
82         return None
83
84 # Returns the name that we would pass to dlopen() the library
85 # corresponding to this .la file
86 def extract_libtool_shlib(la_file):
87     dlname = _extract_dlname_field(la_file)
88     if dlname is None:
89         return None
90
91     # From the comments in extract_libtool(), older libtools had
92     # a path rather than the raw dlname
93     return os.path.basename(dlname)
94
95 def extract_libtool(la_file):
96     dlname = _extract_dlname_field(la_file)
97     if dlname is None:
98         raise ValueError("%s has no dlname. Not a shared library?" % la_file)
99     libname = os.path.join(os.path.dirname(la_file),
100                            '.libs', dlname)
101     # FIXME: This hackish, but I'm not sure how to do this
102     #        in a way which is compatible with both libtool 2.2
103     #        and pre-2.2. Johan 2008-10-21
104     libname = libname.replace('.libs/.libs', '.libs')
105     return libname
106
107 # Returns arguments for invoking libtool, if applicable, otherwise None
108 def get_libtool_command(options):
109     libtool_infection = not options.nolibtool
110     if not libtool_infection:
111         return None
112
113     libtool_path = options.libtool_path
114     if libtool_path:
115         # Automake by default sets:
116         # LIBTOOL = $(SHELL) $(top_builddir)/libtool
117         # To be strictly correct we would have to parse shell.  For now
118         # we simply split().
119         return libtool_path.split(' ')
120
121     try:
122         subprocess.check_call(['libtool', '--version'],
123                               stdout=open(os.devnull))
124     except (subprocess.CalledProcessError, OSError):
125         # If libtool's not installed, assume we don't need it
126         return None
127
128     return ['libtool']
129
130
131 def files_are_identical(path1, path2):
132     f1 = open(path1)
133     f2 = open(path2)
134     buf1 = f1.read(8192)
135     buf2 = f2.read(8192)
136     while buf1 == buf2 and buf1 != '':
137         buf1 = f1.read(8192)
138         buf2 = f2.read(8192)
139     f1.close()
140     f2.close()
141     return buf1 == buf2