aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/util/plugin.py
blob: 1fee690fe0aa1db7119bf9204bee983402747d0c (plain) (blame)
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
# Copyright (C) 2005-2012 Aaron Bentley <abentley@panoramicfeedback.com>
#                         Chris Ball <cjb@laptop.org>
#                         Gianluca Montecchi <gian@grys.it>
#                         Marien Zwart <marien.zwart@gmail.com>
#                         W. Trevor King <wking@drexel.edu>
#
# This file is part of Bugs Everywhere.
#
# Bugs Everywhere 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 2 of the License, or (at your option) any
# later version.
#
# Bugs Everywhere 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
# Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.

"""
Allow simple listing and loading of the various becommands and libbe
submodules (i.e. "plugins").
"""

import os
import os.path
import sys
import zipfile


_PLUGIN_PATH = os.path.realpath(
    os.path.dirname(
        os.path.dirname(
            os.path.dirname(__file__))))
if _PLUGIN_PATH not in sys.path:
    sys.path.append(_PLUGIN_PATH)

def import_by_name(modname):
    """
    >>> mod = import_by_name('libbe.bugdir')
    >>> 'BugDir' in dir(mod)
    True
    >>> import_by_name('libbe.highly_unlikely')
    Traceback (most recent call last):
      ...
    ImportError: No module named highly_unlikely
    """
    module = __import__(modname)
    components = modname.split('.')
    for comp in components[1:]:
        module = getattr(module, comp)
    return module

def ziplistdir(path):
    """Lists items in a directory contained in a zip file"""
    zipidx=path.find('.zip')
    zippath=path[:4+zipidx]
    path=path[5+zipidx:].replace(os.sep, '/')
    with zipfile.ZipFile(zippath, 'r') as zf:
        files=[f[len(path)+1:] for f in zf.namelist() if f[:len(path)]==path and '/' not in f[len(path)+1:]]
        return files

def modnames(prefix):
    """
    >>> 'list' in [n for n in modnames('libbe.command')]
    True
    >>> 'plugin' in [n for n in modnames('libbe.util')]
    True
    """
    components = prefix.split('.')
    modfilespath=os.path.join(_PLUGIN_PATH, *components)
    # Cope if we are executing from inside a zip archive full of precompiled .pyc's
    modfiles=ziplistdir(modfilespath) if '.zip' in modfilespath else os.listdir(modfilespath)
    modfiles.sort()
    # Eliminate .py/.pyc duplicates
    print modfiles
    x=1
    while x<len(modfiles):
        if modfiles[x].endswith('.pyc') and modfiles[x-1]==modfiles[x][:len(modfiles[x-1])]:
            del modfiles[x-1]
        else:
            x+=1
    print modfiles
    for modfile in modfiles:
        if modfile.startswith('.'):
            continue # the occasional emacs temporary file
        if modfile == '__init__.py' or modfile == '__init__.pyc':
            continue
        if modfile.endswith('.py'):
            yield modfile[:-3]
        elif modfile.endswith('.pyc'):
            yield modfile[:-4]