~ubuntu-branches/ubuntu/raring/python3.3/raring-proposed

« back to all changes in this revision

Viewing changes to debian/pymindeps.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-22 06:14:01 UTC
  • Revision ID: package-import@ubuntu.com-20120322061401-vvrgvw3nvi68rtqq
Tags: 3.3.0~a1-1
* Python 3.3.0 alpha1 release.
* Update to 20120321 from the trunk.
* Update debian/copyright.
* Build-depend on expat (>= 2.1~).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
 
3
# Matthias Klose
 
4
# Modified to only exclude module imports from a given module.
 
5
 
 
6
# Copyright 2004 Toby Dickenson
 
7
#
 
8
# Permission is hereby granted, free of charge, to any person obtaining
 
9
# a copy of this software and associated documentation files (the
 
10
# "Software"), to deal in the Software without restriction, including
 
11
# without limitation the rights to use, copy, modify, merge, publish,
 
12
# distribute, sublicense, and/or sell copies of the Software, and to
 
13
# permit persons to whom the Software is furnished to do so, subject
 
14
# to the following conditions:
 
15
#
 
16
# The above copyright notice and this permission notice shall be included
 
17
# in all copies or substantial portions of the Software.
 
18
#
 
19
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
20
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
21
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
22
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 
23
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 
24
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 
25
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
26
 
 
27
import os, sys, pprint
 
28
import modulefinder
 
29
import imp
 
30
 
 
31
class mymf(modulefinder.ModuleFinder):
 
32
    def __init__(self,*args,**kwargs):
 
33
        self._depgraph = {}
 
34
        self._types = {}
 
35
        self._last_caller = None
 
36
        modulefinder.ModuleFinder.__init__(self, *args, **kwargs)
 
37
        
 
38
    def import_hook(self, name, caller=None, fromlist=None, level=-1):
 
39
        old_last_caller = self._last_caller
 
40
        try:
 
41
            self._last_caller = caller
 
42
            return modulefinder.ModuleFinder.import_hook(self, name, caller,
 
43
                                                         fromlist, level)
 
44
        finally:
 
45
            self._last_caller = old_last_caller
 
46
            
 
47
    def import_module(self, partnam, fqname, parent):
 
48
        m = modulefinder.ModuleFinder.import_module(self,
 
49
                                                    partnam, fqname, parent)
 
50
        if m is not None and self._last_caller:
 
51
            caller = self._last_caller.__name__
 
52
            if '.' in caller:
 
53
                caller = caller[:caller.index('.')]
 
54
            callee =  m.__name__
 
55
            if '.' in callee:
 
56
                callee = callee[:callee.index('.')]
 
57
            #print "XXX last_caller", caller, "MOD", callee
 
58
            #self._depgraph.setdefault(self._last_caller.__name__,{})[r.__name__] = 1
 
59
            #if caller in ('pdb', 'doctest') or callee in ('pdb', 'doctest'):
 
60
            #    print caller, "-->", callee
 
61
            if caller != callee:
 
62
                self._depgraph.setdefault(caller,{})[callee] = 1
 
63
        return m
 
64
    
 
65
    def find_module(self, name, path, parent=None):
 
66
        if parent is not None:
 
67
            # assert path is not None
 
68
            fullname = parent.__name__+'.'+name
 
69
        elif name == "__init__":
 
70
            fullname = os.path.basename(path[0])
 
71
        else:
 
72
            fullname = name
 
73
        if self._last_caller:
 
74
            caller = self._last_caller.__name__
 
75
            if fullname in excluded_imports.get(caller, []):
 
76
                #self.msgout(3, "find_module -> Excluded", fullname)
 
77
                raise ImportError(name)
 
78
 
 
79
        if fullname in self.excludes:
 
80
            #self.msgout(3, "find_module -> Excluded", fullname)
 
81
            raise ImportError(name)
 
82
 
 
83
        if path is None:
 
84
            if name in sys.builtin_module_names:
 
85
                return (None, None, ("", "", imp.C_BUILTIN))
 
86
 
 
87
            path = self.path
 
88
        return imp.find_module(name, path)
 
89
 
 
90
    def load_module(self, fqname, fp, pathname, file_info):
 
91
        suffix, mode, type = file_info
 
92
        m = modulefinder.ModuleFinder.load_module(self, fqname,
 
93
                                                  fp, pathname, file_info)
 
94
        if m is not None:
 
95
            self._types[m.__name__] = type
 
96
        return m
 
97
 
 
98
    def load_package(self, fqname, pathname):
 
99
        m = modulefinder.ModuleFinder.load_package(self, fqname,pathname)
 
100
        if m is not None:
 
101
            self._types[m.__name__] = imp.PKG_DIRECTORY
 
102
        return m
 
103
 
 
104
def reduce_depgraph(dg):
 
105
    pass
 
106
 
 
107
# guarded imports, which don't need to be included in python-minimal
 
108
excluded_imports = {
 
109
    'argparse': set(('gettext',)),
 
110
    'codecs': set(('encodings',)),
 
111
    'collections': set(('cPickle', 'pickle', 'doctest')),
 
112
    'copy': set(('reprlib',)),
 
113
    'functools': set(('_dummy_thread',)),
 
114
    'hashlib': set(('logging',)),
 
115
    #'hashlib': set(('_hashlib', '_md5', '_sha', '_sha256','_sha512',)),
 
116
    'heapq': set(('doctest',)),
 
117
    'io': set(('_dummy_thread',)),
 
118
    'logging': set(('multiprocessing',)),
 
119
    'os': set(('nt', 'ntpath', 'os2', 'os2emxpath', 'mac', 'macpath',
 
120
               'riscos', 'riscospath', 'riscosenviron')),
 
121
    'optparse': set(('gettext',)),
 
122
    'pickle': set(('argparse', 'doctest', 'pprint')),
 
123
    'platform': set(('plistlib', 'tempfile')),
 
124
    #'socket': set(('_ssl',)),
 
125
    'subprocess': set(('threading',)),
 
126
    'sysconfig': set(('pprint',)),
 
127
    }
 
128
 
 
129
def main(argv):
 
130
    # Parse command line
 
131
    import getopt
 
132
    try:
 
133
        opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:")
 
134
    except getopt.error as msg:
 
135
        print(msg)
 
136
        return
 
137
 
 
138
    # Process options
 
139
    debug = 1
 
140
    domods = 0
 
141
    addpath = []
 
142
    exclude = []
 
143
    for o, a in opts:
 
144
        if o == '-d':
 
145
            debug = debug + 1
 
146
        if o == '-m':
 
147
            domods = 1
 
148
        if o == '-p':
 
149
            addpath = addpath + a.split(os.pathsep)
 
150
        if o == '-q':
 
151
            debug = 0
 
152
        if o == '-x':
 
153
            exclude.append(a)
 
154
 
 
155
    path = sys.path[:]
 
156
    path = addpath + path
 
157
 
 
158
    if debug > 1:
 
159
        print("version:", sys.version)
 
160
        print("path:")
 
161
        for item in path:
 
162
            print("   ", repr(item))
 
163
 
 
164
    #exclude = ['__builtin__', 'sys', 'os']
 
165
    exclude = []
 
166
    mf = mymf(path, debug, exclude)
 
167
    for arg in args:
 
168
        mf.run_script(arg)
 
169
 
 
170
    depgraph = reduce_depgraph(mf._depgraph)
 
171
    
 
172
    pprint.pprint({'depgraph':mf._depgraph, 'types':mf._types})
 
173
    
 
174
if __name__=='__main__':
 
175
    main(sys.argv[1:])