| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import os, shutil, sys, glob |
|---|
| 4 | import __builtin__ |
|---|
| 5 | from cStringIO import StringIO |
|---|
| 6 | from optparse import OptionParser |
|---|
| 7 | |
|---|
| 8 | __builtin__._ = str |
|---|
| 9 | |
|---|
| 10 | plugin_count = 0 |
|---|
| 11 | |
|---|
| 12 | lastfake = False |
|---|
| 13 | |
|---|
| 14 | def entry(filename, out=None, copythese=None, fake=False, pyc=False): |
|---|
| 15 | print "Processing filename %s" % filename |
|---|
| 16 | if filename.endswith(".py") and 'EGG-INFO' not in filename: |
|---|
| 17 | if copythese is not None and not fake: |
|---|
| 18 | copythese.append(filename) |
|---|
| 19 | if not filename.endswith("__init__.py"): |
|---|
| 20 | module = filename[:-3].replace('/', '.').replace('\\', '.') |
|---|
| 21 | if out: |
|---|
| 22 | if fake: |
|---|
| 23 | global lastfake |
|---|
| 24 | if not lastfake: |
|---|
| 25 | out.write("if False: # fake the import so py2exe will include the file\n") |
|---|
| 26 | lastfake = True |
|---|
| 27 | out.write(" import %s\n" % (module)) |
|---|
| 28 | else: |
|---|
| 29 | global lastfake |
|---|
| 30 | lastfake = False |
|---|
| 31 | global plugin_count |
|---|
| 32 | name = module.split('.')[-1] |
|---|
| 33 | |
|---|
| 34 | # Don't print any __init__ modules in the splash screen |
|---|
| 35 | if name != "__init__": |
|---|
| 36 | plugin_count += 1 |
|---|
| 37 | out.write("app.gaugeCallback('%s')\n" % name) |
|---|
| 38 | print "importing %s" % module |
|---|
| 39 | out.write("try:\n import %s\nexcept:\n pass\n" % (module)) |
|---|
| 40 | |
|---|
| 41 | def process(path, out=None, copythese=None, fake=False, pyc=False): |
|---|
| 42 | files = glob.glob('%s/*' % path) |
|---|
| 43 | for path in files: |
|---|
| 44 | if os.path.isdir(path): |
|---|
| 45 | process(path, out, fake=fake, pyc=pyc) |
|---|
| 46 | else: |
|---|
| 47 | entry(path, out, copythese, fake, pyc) |
|---|
| 48 | |
|---|
| 49 | if __name__ == "__main__": |
|---|
| 50 | usage="usage: %prog [-s dir] [-o file]" |
|---|
| 51 | parser=OptionParser(usage=usage) |
|---|
| 52 | parser.add_option("-i", action="store", dest="input", |
|---|
| 53 | default="peppy", help="base input directory") |
|---|
| 54 | parser.add_option("-d", action="store", dest="importdir", |
|---|
| 55 | default="builtins", help="import directory within base directory") |
|---|
| 56 | parser.add_option("-o", action="store", dest="output", |
|---|
| 57 | default="peppy/py2exe_plugins.py", help="output filename") |
|---|
| 58 | parser.add_option("-e", action="store", dest="eggs", |
|---|
| 59 | default="", help="process unpacked eggs") |
|---|
| 60 | (options, args) = parser.parse_args() |
|---|
| 61 | |
|---|
| 62 | out = StringIO() |
|---|
| 63 | out.write("# Automatically generated, and only used when creating a py2exe distribution\n") |
|---|
| 64 | |
|---|
| 65 | os.chdir(options.input) |
|---|
| 66 | savepath = os.getcwd() |
|---|
| 67 | destdir = os.path.join(savepath, options.importdir) |
|---|
| 68 | print destdir |
|---|
| 69 | |
|---|
| 70 | process(options.importdir, out) |
|---|
| 71 | # Need to fake the importing of all the library modules and the editra style |
|---|
| 72 | # definition files so py2exe will include them |
|---|
| 73 | process('peppy/lib', out, fake=True) |
|---|
| 74 | process('peppy/editra/syntax', out, fake=True) |
|---|
| 75 | |
|---|
| 76 | if options.eggs: |
|---|
| 77 | process(options.eggs, out, pyc=True) |
|---|
| 78 | |
|---|
| 79 | filename = os.path.join(savepath, options.output) |
|---|
| 80 | fh = open(filename, 'w') |
|---|
| 81 | fh.write("import wx\napp = wx.GetApp()\n") |
|---|
| 82 | fh.write(out.getvalue()) |
|---|
| 83 | fh.close() |
|---|
| 84 | |
|---|
| 85 | countname = filename.replace(".py", "_count.py") |
|---|
| 86 | fh = open(countname, 'w') |
|---|
| 87 | fh.write("count = %d\n" % plugin_count) |
|---|
| 88 | fh.close() |
|---|