usercustomize.py
2.13 KB
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
# Libtool library loader for Python.
# This file is part of GNU Mailutils.
# Copyright (C) 2011-2012, 2014 Free Software Foundation, Inc.
#
# GNU Mailutils is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or (at
# your option) any later version.
#
# GNU Mailutils 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
# This file modifies the normal import routine by looking additionally
# into module.la file and trying to load corresponding so library (as
# determined by the dlname variable in it) from the .libs subdirectory.
#
# It is used to load Mailutils Python modules from the source tree.
#
# Example usage:
#
# PYTHONPATH=/path/to/mailutils-3.0/python python test.py
import sys,os,imp
class laloader(object):
def __init__(self, filename, libdir):
self.filename = filename
self.libdir = libdir
def load_module(self, name):
with open(self.filename) as f:
for line in f:
s = line.strip()
if s.startswith('dlname='):
s = s[7:].strip("'\"")
libname = self.libdir + '/' + s
if os.path.exists(libname):
return imp.load_dynamic(name, libname)
return None
class lafinder(object):
def __init__(self):
pass
def find_module(self, fullname, path=None):
ml = fullname.split(".")
s = '/'.join(ml)+'.la'
libs = '/'.join(ml[:-1]) + '/.libs'
for dir in sys.path:
name = dir + '/' + s
libdir = dir + '/' + libs
if os.path.exists(name) and os.path.exists(libdir):
return laloader(name, libdir)
return None
sys.meta_path.append(lafinder())
# End of file