Initial packaging for Tizen
[profile/ivi/gobject-introspection.git] / giscanner / libtoolimporter.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 imp
22 import os
23 import platform
24 import sys
25
26 from .utils import extract_libtool
27
28
29 class LibtoolImporter(object):
30
31     def __init__(self, name, path):
32         self.name = name
33         self.path = path
34
35     @classmethod
36     def find_module(cls, name, packagepath=None):
37         modparts = name.split('.')
38         filename = modparts.pop() + '.la'
39
40         # Given some.package.module 'path' is where subpackages of some.package
41         # should be looked for. See if we can find a ".libs/module.la" relative
42         # to those directories and failing that look for file
43         # "some/package/.libs/module.la" relative to sys.path
44         if len(modparts) > 0:
45             modprefix = os.path.join(*modparts)
46             modprefix = os.path.join(modprefix, '.libs')
47         else:
48             modprefix = '.libs'
49
50         for path in sys.path:
51             full = os.path.join(path, modprefix, filename)
52             if os.path.exists(full):
53                 return cls(name, full)
54
55     def load_module(self, name):
56         realpath = extract_libtool(self.path)
57         platform_system = platform.system()
58
59         if platform_system == 'Darwin':
60             extension = '.dylib'
61         elif platform_system == 'Windows':
62             extension = '.dll'
63         else:
64             extension = '.so'
65
66         mod = imp.load_module(name, open(realpath), realpath, (extension, 'rb', 3))
67         mod.__loader__ = self
68         return mod
69
70     @classmethod
71     def __enter__(cls):
72         sys.meta_path.append(cls)
73
74     @classmethod
75     def __exit__(cls, type, value, traceback):
76         sys.meta_path.remove(cls)