File: package_mf.py

package info (click to toggle)
breezy 3.3.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,768 kB
  • sloc: python: 279,477; ansic: 1,093; makefile: 367; sh: 284; lisp: 107
file content (120 lines) | stat: -rw-r--r-- 4,417 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
# Copyright (C) 2008 Canonical Ltd
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Custom module finder for entire package."""

import os
import sys

# At present, this is only used on Windows (see setup.py)
from py2exe import mf310 as modulefinder


class CustomModuleFinder(modulefinder.ModuleFinder):
    """Custom module finder for processing python packages,
    e.g. brz plugins packages.

    :param  path:   list of directories to search for modules;
                    if not specified, python standard library only is used.
    """

    def __init__(self, path=None, debug=0, excludes=None, replace_paths=None):
        if replace_paths is None:
            replace_paths = []
        if excludes is None:
            excludes = []
        if path is None:
            path = [os.path.dirname(os.__file__)]  # only python std lib
        modulefinder.ModuleFinder.__init__(self, path, debug, excludes, replace_paths)

    def load_package_recursive(self, fqname):
        """Recursively process each module in package.

        :param  fqname:   name of the package.
        """
        # Load all the parents
        parent = None
        path = []
        for partname in fqname.split("."):
            parent_path = ".".join(path)
            path.append(partname)
            # import_module works recursively,
            # and some of the dependencies may try
            # to import modules not present on the system.
            # (The actual error is
            # AttributeError: 'NoneType' object has no attribute 'is_package')
            # Ignore errors here and bail out in the collection loop.
            try:
                self.import_module(
                    partname, ".".join(path), self.modules.get(parent_path, None)
                )
            except BaseException:
                pass
        stack = [(fqname, parent_path)]
        while stack:
            (package, parent_path) = stack.pop(0)
            # Here we assume that all parents have already been imported.
            # Abort when a parent is missing.
            parent = self.modules[parent_path]
            pkg_module = self.import_module(package, package, parent)
            curdir = pkg_module.__file__
            dirlist = os.listdir(curdir)
            for filename in dirlist:
                full = os.path.join(curdir, filename)
                if os.path.isdir(full):
                    if filename == "tests":
                        continue
                    init = os.path.join(full, "__init__.py")
                    if os.path.isfile(init):
                        stack.append((".".join((package, filename)), package))
                    continue
                if not filename.endswith(".py"):
                    continue
                if filename == "setup.py":  # skip
                    continue
                # We only accept .py files, so could use [:-3] too - faster...
                partname = os.path.splitext(filename)[0]
                self.import_module(partname, ".".join((package, partname)), pkg_module)

    def get_result(self):
        """Return 2-tuple: (list of packages, list of modules)."""
        keys = sorted(self.modules.keys())
        mods = []
        packs = []
        for key in keys:
            m = self.modules[key]
            if not m.__file__:  # skip builtins
                continue
            if m.__path__:
                packs.append(key)
            elif key != "__main__":
                mods.append(key)
        return (packs, mods)


if __name__ == "__main__":
    package = sys.argv[1]

    mf = CustomModuleFinder()
    mf.run_package(package)

    packs, mods = mf.get_result()

    print("Packages:")
    print(packs)

    print("Modules:")
    print(mods)