summaryrefslogtreecommitdiff
path: root/gstlibtoolimporter.py
blob: 4b08de4804555bcefa03b68ccc12c9c4f9caf0de (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
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# gst-python
# Copyright (C) 2009 Alessandro Decina <alessandro.decina@collbora.co.uk>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author: Alessandro Decina <alessandro.decina@collabora.co.uk>

# importer for uninstalled setup, see PEP
# http://www.python.org/dev/peps/pep-0302/

import os
import sys
import imp

class Loader(object):
    def __init__(self, fileobj, filename, description):
        self.fileobj = fileobj
        self.filename = filename
        self.description = description

    def find_real_filename(self):
        dlname = None
        installed = False
        for line in self.fileobj:
            if len(line) > 7 and line[:7] == 'dlname=':
                dlname = line[8:-2]
            elif len(line) > 10 and line[:10] == 'installed=':
                installed = line[10:-1] == 'yes'

        if not dlname:
            return None

        if installed or os.path.dirname(self.filename).endswith('.libs'):
            filename = os.path.join(os.path.dirname(self.filename), dlname)
        else:
            filename = os.path.join(os.path.dirname(self.filename), '.libs', dlname)

        return filename

    def load_module(self, name):
        try:
            module = sys.modules[name]
            self.fileobj.close()

            return module
        except KeyError:
            pass

        filename = self.find_real_filename()
        self.fileobj.close()
        if filename is None:
            raise ImportError("No module named %s" % name)

        fileobj = file(filename, 'rb')

        module = imp.new_module(name)
        sys.modules[name] = module

        imp.load_module(name, fileobj, filename, self.description)
        fileobj.close()

        return module

class Importer(object):
    def find_module(self, name, path=None):
        if path is None:
            path = sys.path

        for directory in path:
            fileobj, filename, description = self.find_libtool_module(name, directory)
            if fileobj is not None:
                return Loader(fileobj, filename, description)

        return None

    def find_libtool_module(self, name, directory):
        name = name.split(".")[-1]
        absname = os.path.join(directory, name)
        for suffix in ('.la', '.module.la'):
            filename = absname + suffix
            try:
                fileobj = file(filename, 'rb')
            except IOError:
                continue

            return fileobj, filename, (suffix, 'rb', imp.C_EXTENSION)

        return None, None, None

importer = Importer()

def install():
    sys.meta_path.append(importer)

def uninstall():
    sys.meta_path.remove(importer)