blob: 267e89e30ce84b725539ffd2a5363ee7c4c259a1 [file] [log] [blame]
Jack Jansen0aa97821998-02-25 15:42:48 +00001"""Findmodulefiles - Find out where modules are loaded from.
2Run findmodulefiles() after running a program with "python -i". The
3resulting file list can be given to mkfrozenresources or to a
4(non-existent) freeze-like application.
5
6findmodulefiles will create a file listing all modules and where they have
7been imported from.
8
9findunusedbuiltins takes a list of (modules, file) and prints all builtin modules
10that are _not_ in the list. The list is created by opening the findmodulefiles
11output, reading it and eval()ing that.
12
13mkpycresource takes a list of (module, file) and creates a resourcefile with all those
14modules and (optionally) a main module.
15"""
16
17def findmodulefiles(output=None):
18 """Produce a file containing a list of (modulename, filename-or-None)
19 tuples mapping module names to source files"""
20 # Immedeately grab the names
21 import sys
22 module_names = sys.modules.keys()[:]
23 import os
24 if not output:
25 if os.name == 'mac':
26 import macfs
27
28 output, ok = macfs.StandardPutFile('Module file listing output:')
29 if not ok: sys.exit(0)
30 output = output.as_pathname()
31 if not output:
32 output = sys.stdout
33 elif type(output) == type(''):
34 output = open(output, 'w')
35 output.write('[\n')
36 for name in module_names:
37 try:
38 source = sys.modules[name].__file__
39 except AttributeError:
40 source = None
41 else:
42 source = `source`
43 output.write('\t(%s,\t%s),\n' % (`name`, source))
44 output.write(']\n')
45 del output
46
47def findunusedbuiltins(list):
48 """Produce (on stdout) a list of unused builtin modules"""
49 import sys
50 dict = {}
51 for name, location in list:
52 if location == None:
53 dict[name] = 1
54 for name in sys.builtin_module_names:
55 if not dict.has_key(name):
56 print 'Unused builtin module:', name
57
58
59def mkpycresourcefile(list, main='', dst=None):
60 """Copy list-of-modules to resource file dst."""
61 import py_resource
62 import Res
63 import sys
64
65 if dst == None:
66 import macfs
67 fss, ok = macfs.StandardPutFile("PYC Resource output file")
68 if not ok: sys.exit(0)
69 dst = fss.as_pathname()
70 if main == '':
71 import macfs
72 fss, ok = macfs.PromptGetFile("Main program:", "TEXT")
73 if ok:
74 main = fss.as_pathname()
75
76 fsid = py_resource.create(dst)
77
78 if main:
79 id, name = py_resource.frompyfile(main, '__main__', preload=1)
80 print '%5d\t%s\t%s'%(id, name, main)
81 for name, location in list:
82 if not location: continue
Jack Jansen1301f2b1998-03-02 16:57:01 +000083 if location[-4:] == '.pyc':
84 # Attempt corresponding .py
85 location = location[:-1]
Jack Jansen0aa97821998-02-25 15:42:48 +000086 if location[-3:] != '.py':
87 print '*** skipping', location
88 continue
89 id, name = py_resource.frompyfile(location, name, preload=1)
90 print '%5d\t%s\t%s'%(id, name, location)
91
92 Res.CloseResFile(fsid)
93
94