Use checkboxes to select files.
[hgct.git] / buildMain.py
blob9698cca10b5f921cd320dc855fba075faf302796
1 #!/usr/bin/env python
3 import sys, os.path, re, imp, py_compile
5 if len(sys.argv) < 3:
6 print "Usage: <out-file> <main-file> [<modules>...]"
7 sys.exit(1)
9 outFile = sys.argv[1]
10 mainFile = sys.argv[2]
11 moduleFiles = sys.argv[3:]
13 preamble = """#!/usr/bin/env python
15 import tempfile, sys, imp, __builtin__
17 modules = {}
19 class Module:
20 pass
22 def addModule(name, contents, descr):
23 m = Module()
24 m.descr = descr
25 m.contents = contents
26 modules[name] = m
28 origImport = __import__
29 def MyImport(name, globals=None, locals=None, fromlist=None):
30 try:
31 return sys.modules[name]
32 except KeyError:
33 pass
35 if modules.has_key(name):
36 f = tempfile.TemporaryFile()
37 f.write(modules[name].contents)
38 f.seek(0)
39 ret = imp.load_module(name, f, '', modules[name].descr)
40 f.close()
41 return ret
42 else:
43 return origImport(name, globals, locals, fromlist)
45 __builtin__.__import__ = MyImport
46 """
48 def findDescr(suffix):
49 for x in imp.get_suffixes():
50 if x[0] == '.' + suffix:
51 return x
53 fout = open(outFile, 'w')
54 fout.write(preamble)
55 REFile = re.compile('^(.*)\\.(.*?)$')
57 for f in moduleFiles + [mainFile]:
58 py_compile.compile(f)
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')
66 fout.close()