[scanner] Catch OSError too when checking for libtool
[gnome.gobject-introspection] / 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 # Copied from h2defs.py
26 _upperstr_pat1 = re.compile(r'([^A-Z])([A-Z])')
27 _upperstr_pat2 = re.compile(r'([A-Z][A-Z])([A-Z][0-9a-z])')
28 _upperstr_pat3 = re.compile(r'^([A-Z])([A-Z])')
29
30
31 def to_underscores(name):
32     """Converts a typename to the equivalent underscores name.
33     This is used to form the type conversion macros and enum/flag
34     name variables"""
35     name = _upperstr_pat1.sub(r'\1_\2', name)
36     name = _upperstr_pat2.sub(r'\1_\2', name)
37     name = _upperstr_pat3.sub(r'\1_\2', name, count=1)
38     return name
39
40
41 def to_underscores_noprefix(name):
42     """Like to_underscores, but designed for "unprefixed" names.
43     to_underscores("DBusFoo") => dbus_foo, not d_bus_foo."""
44     name = _upperstr_pat1.sub(r'\1_\2', name)
45     name = _upperstr_pat2.sub(r'\1_\2', name)
46     return name
47
48 _libtool_pat = re.compile("dlname='([A-z0-9\.\-\+]+)'\n")
49
50 def _extract_dlname_field(la_file):
51     f = open(la_file)
52     data = f.read()
53     f.close()
54     m = _libtool_pat.search(data)
55     if m:
56         return m.groups()[0]
57     else:
58         return None
59
60 # Returns the name that we would pass to dlopen() the library
61 # corresponding to this .la file
62 def extract_libtool_shlib(la_file):
63     dlname = _extract_dlname_field(la_file)
64     if dlname is None:
65         return None
66
67     # From the comments in extract_libtool(), older libtools had
68     # a path rather than the raw dlname
69     return os.path.basename(dlname)
70
71 def extract_libtool(la_file):
72     dlname = _extract_dlname_field(la_file)
73     if dlname is None:
74         raise ValueError("%s has no dlname. Not a shared library?" % la_file)
75     libname = os.path.join(os.path.dirname(la_file),
76                            '.libs', dlname)
77     # FIXME: This hackish, but I'm not sure how to do this
78     #        in a way which is compatible with both libtool 2.2
79     #        and pre-2.2. Johan 2008-10-21
80     libname = libname.replace('.libs/.libs', '.libs')
81     return libname
82
83 # Returns arguments for invoking libtool, if applicable, otherwise None
84 def get_libtool_command(options):
85     libtool_infection = not options.nolibtool
86     if not libtool_infection:
87         return None
88
89     libtool_path = options.libtool_path
90     if libtool_path:
91         # Automake by default sets:
92         # LIBTOOL = $(SHELL) $(top_builddir)/libtool
93         # To be strictly correct we would have to parse shell.  For now
94         # we simply split().
95         return libtool_path.split(' ')
96
97     try:
98         subprocess.check_call(['libtool', '--version'],
99                               stdout=open(os.devnull))
100     except:
101         # If libtool's not installed, assume we don't need it
102         return None
103
104     return ['libtool']