Skip to content

Commit

Permalink
Working combination script
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjarni R. Einarsson committed Jun 21, 2011
1 parent 4cf1edf commit 8264d33
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions combine.py
@@ -1,31 +1,65 @@
#!/usr/bin/python
#
# This is a poor-man's executable builder, for embedding dependencies into
# our pagekite.py file until we have proper packaging.
#
import os, sys

pydata = { }
data = { }
order = [ ]
for filename in sys.argv[1:]:
fd = open(filename, 'r')
bn = os.path.basename(filename).replace('.py', '')
if filename.endswith('/__init__.py'):
bn = os.path.basename(os.path.dirname(filename))
else:
bn = os.path.basename(filename).replace('.py', '')

order.append(bn)
data[bn] = [l.replace('\n', '').replace('\r', '') for l in fd.readlines()]

fd = open(filename, 'r')
lines = [l.replace('\n', '').replace('\r', '') for l in fd.readlines()]
fd.close()

print """#!/usr/bin/python'
# This is a compilation of multiple Python files. Search for the string
# 'MODULE' to examine individual parts.
if filename.endswith('.py') or filename.endswith('.pyw'):
pydata[bn] = lines
else:
data[bn] = lines

print """#!/usr/bin/python
#
# NOTE: This is a compilation of multiple Python files.
# See below for details on individual segments.
#
import sys, imp
"""

first = order.pop(0)
for mn in order:
print '##[ MODULE: %s ]%s' % (mn, '#' * (65-len(mn)))
print
print 'class %s(object):' % mn
for line in data[mn]:
print ' %s' % line
if mn in pydata:
what = 'MODULE'
ddict = pydata
else:
what = 'FILE'
ddict = data

print '%s' % '#' * 79
print
if mn != order[-1] and what == 'MODULE':
print 'sys.modules["%s"] = imp.new_module("%s")' % (mn, mn)
print 'exec """'
for line in ddict[mn]:
print '%s' % line.replace('\\', '\\\\').replace('"', '\\"')
print '""" in sys.modules["%s"].__dict__' % mn

elif what == 'FILE':
print '__DATA_%s = """' % mn.replace('.', '_').upper()
for line in ddict[mn]:
print '%s' % line.replace('\\', '\\\\').replace('"', '\\"')
print '"""'

print '##[ MAIN: %s ]%s' % (first, '#' * (67-len(first)))
print
for line in data[first]:
print '%s' % line
else:
for line in ddict[mn]:
print '%s' % line

print

0 comments on commit 8264d33

Please sign in to comment.