1 """Findmodulefiles - Find out where modules are loaded from.
2 Run findmodulefiles() after running a program with "python -i". The
3 resulting file list can be given to mkfrozenresources or to a
4 (non-existent) freeze-like application.
6 findmodulefiles will create a file listing all modules and where they have
9 findunusedbuiltins takes a list of (modules, file) and prints all builtin modules
10 that are _not_ in the list. The list is created by opening the findmodulefiles
11 output, reading it and eval()ing that.
13 mkpycresource takes a list of (module, file) and creates a resourcefile with all those
14 modules and (optionally) a main module.
17 def 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
22 module_names
= sys
.modules
.keys()[:]
28 output
, ok
= macfs
.StandardPutFile('Module file listing output:')
29 if not ok
: sys
.exit(0)
30 output
= output
.as_pathname()
33 elif type(output
) == type(''):
34 output
= open(output
, 'w')
36 for name
in module_names
:
38 source
= sys
.modules
[name
].__file
__
39 except AttributeError:
43 output
.write('\t(%s,\t%s),\n' % (`name`
, source
))
47 def findunusedbuiltins(list):
48 """Produce (on stdout) a list of unused builtin modules"""
51 for name
, location
in list:
54 for name
in sys
.builtin_module_names
:
55 if not dict.has_key(name
):
56 print 'Unused builtin module:', name
59 def mkpycresourcefile(list, main
='', dst
=None):
60 """Copy list-of-modules to resource file dst."""
67 fss
, ok
= macfs
.StandardPutFile("PYC Resource output file")
68 if not ok
: sys
.exit(0)
69 dst
= fss
.as_pathname()
72 fss
, ok
= macfs
.PromptGetFile("Main program:", "TEXT")
74 main
= fss
.as_pathname()
76 fsid
= py_resource
.create(dst
)
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
83 if location
[-4:] == '.pyc':
84 # Attempt corresponding .py
85 location
= location
[:-1]
86 if location
[-3:] != '.py':
87 print '*** skipping', location
89 id, name
= py_resource
.frompyfile(location
, name
, preload
=1)
90 print '%5d\t%s\t%s'%(id, name
, location
)
92 Res
.CloseResFile(fsid
)