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')
55 REFile
= re
.compile('^(.*)\\.(.*?)$')
57 for f
in moduleFiles
+ [mainFile
]:
59 fin
= open(f
+ 'c', 'r')
60 name
= os
.path
.basename(f
)
61 m
= REFile
.match(name
)
62 fout
.write('addModule(' + repr(m
.group(1)) + ', ' + repr(fin
.read()) + ', ' + repr(findDescr('pyc')) + ')\n')
64 m
= REFile
.match(mainFile
)
65 fout
.write('import ' + m
.group(1) + '\n')