[scanner] Catch OSError too when checking for libtool
[gnome.gobject-introspection] / giscanner / utils.py
index 021ffea..3bbf33b 100644 (file)
@@ -20,6 +20,7 @@
 
 import re
 import os
+import subprocess
 
 # Copied from h2defs.py
 _upperstr_pat1 = re.compile(r'([^A-Z])([A-Z])')
@@ -46,25 +47,58 @@ def to_underscores_noprefix(name):
 
 _libtool_pat = re.compile("dlname='([A-z0-9\.\-\+]+)'\n")
 
+def _extract_dlname_field(la_file):
+    f = open(la_file)
+    data = f.read()
+    f.close()
+    m = _libtool_pat.search(data)
+    if m:
+        return m.groups()[0]
+    else:
+        return None
 
-def extract_libtool(libname):
-    data = open(libname).read()
-    filename = _libtool_pat.search(data).groups()[0]
-    libname = os.path.join(os.path.dirname(libname),
-                           '.libs', filename)
+# Returns the name that we would pass to dlopen() the library
+# corresponding to this .la file
+def extract_libtool_shlib(la_file):
+    dlname = _extract_dlname_field(la_file)
+    if dlname is None:
+        return None
+
+    # From the comments in extract_libtool(), older libtools had
+    # a path rather than the raw dlname
+    return os.path.basename(dlname)
+
+def extract_libtool(la_file):
+    dlname = _extract_dlname_field(la_file)
+    if dlname is None:
+        raise ValueError("%s has no dlname. Not a shared library?" % la_file)
+    libname = os.path.join(os.path.dirname(la_file),
+                           '.libs', dlname)
     # FIXME: This hackish, but I'm not sure how to do this
     #        in a way which is compatible with both libtool 2.2
     #        and pre-2.2. Johan 2008-10-21
     libname = libname.replace('.libs/.libs', '.libs')
     return libname
 
+# Returns arguments for invoking libtool, if applicable, otherwise None
+def get_libtool_command(options):
+    libtool_infection = not options.nolibtool
+    if not libtool_infection:
+        return None
+
+    libtool_path = options.libtool_path
+    if libtool_path:
+        # Automake by default sets:
+        # LIBTOOL = $(SHELL) $(top_builddir)/libtool
+        # To be strictly correct we would have to parse shell.  For now
+        # we simply split().
+        return libtool_path.split(' ')
+
+    try:
+        subprocess.check_call(['libtool', '--version'],
+                              stdout=open(os.devnull))
+    except:
+        # If libtool's not installed, assume we don't need it
+        return None
 
-def strip_common_prefix(first, second):
-    max_index = second.rfind('_')
-    second_len = len(second)
-    second = second.replace('_', '')
-    max_index -= second_len - len(second) - 1
-    for i, c in enumerate(first.upper()):
-        if i >= len(second) or c != second[i] or i == max_index:
-            return second[i:]
-    return second[i + 1:]
+    return ['libtool']