3 import sys
, os
.path
, re
, imp
, py_compile
6 print "Usage: <out-file> <main-file> [<modules>...]"
10 mainFile
= sys
.argv
[2]
11 moduleFiles
= sys
.argv
[3:]
13 preamble
= """#!/usr/bin/env python
15 import tempfile, sys, imp, __builtin__
22 def addModule(name, contents, descr):
28 origImport = __import__
29 def MyImport(name, globals=None, locals=None, fromlist=None):
31 return sys.modules[name]
35 if modules.has_key(name):
36 f = tempfile.TemporaryFile()
37 f.write(modules[name].contents)
39 ret = imp.load_module(name, f, '', modules[name].descr)
43 return origImport(name, globals, locals, fromlist)
45 __builtin__.__import__ = MyImport
48 def findDescr(suffix
):
49 for x
in imp
.get_suffixes():
50 if x
[0] == '.' + suffix
:
53 fout
= open(outFile
, 'w')
58 fin
= open(f
+ 'c', 'r')
59 name
= os
.path
.basename(f
)
60 m
= re
.match('^(.*)\\.(.*?)$', name
)
61 fout
.write('addModule(' + repr(m
.group(1)) + ', ' + repr(fin
.read()) + ', ' + repr(findDescr('pyc')) + ')\n')
63 fout
.write(open(mainFile
, 'r').read())