Differences between revisions 2 and 3
Revision 2 as of 2005-10-27 17:03:24
Size: 1968
Editor: pcp05206170pcs
Comment:
Revision 3 as of 2005-10-27 17:03:51
Size: 1965
Editor: pcp05206170pcs
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
 1. unpack zipped eggs, because I believe py2exe chokes on them when
resolving dependencies
 1. unpack zipped eggs, because I believe py2exe chokes on them when resolving dependencies
Line 8: Line 7:
 3. add all of the files in the eggs to the data_files, so that the
eggs are installed along side the main exe
 3. add all of the files in the eggs to the data_files, so that the eggs are installed along side the main exe
Line 11: Line 9:
 5. generate a new library.zip that does not include anything in the
top level packages found in step 2
 5. generate a new library.zip that does not include anything in the top level packages found in step 2

This is an ugly hack that, I assume, will go away some day when py2exe is aware of eggs. The good thing about this hack is that the basic principles work with both py2exe and py2app.

This is cut from a couple spots in my script. The basic flow is:

  1. unpack zipped eggs, because I believe py2exe chokes on them when resolving dependencies
  2. keep track of the top level packages in the eggs
  3. add all of the files in the eggs to the data_files, so that the eggs are installed along side the main exe
  4. build the exe
  5. generate a new library.zip that does not include anything in the top level packages found in step 2

here's steps 1 and 2:

import pkg_resources
eggs = pkg_resources.require("TurboGears")

from setuptools.archive_util import unpack_archive

for egg in eggs:
   if os.path.isdir(egg.location):
       sys.path.insert(0, egg.location)
       continue
   unpack_archive(egg.location, eggdir)

eggpacks = set()
eggspth = open("build/eggs.pth", "w")
for egg in eggs:
   print egg
   eggspth.write(os.path.basename(egg.location))
   eggspth.write("\n")
   eggpacks.update(egg.get_metadata_lines("top_level.txt"))
eggspth.close()
eggpacks.remove("pkg_resources")

and here's step 5:

   import zipfile

   oldzipfile = "dist/exe/library.zip"
   newzipfile = "dist/exe/small-library.zip"

   oldzip = zipfile.ZipFile(oldzipfile, "r")
   newzip = zipfile.ZipFile(newzipfile, "w", zipfile.ZIP_STORED)

   for entry in oldzip.infolist():
       delim = entry.filename.find("/")
       if delim == -1:
           delim = entry.filename.find(".")
       if delim > -1:
           if entry.filename[0:delim] in eggpacks:
               print "Skipping %s, it's in the egg" % (entry.filename)
               continue
       newzip.writestr(entry, oldzip.read(entry.filename))
   newzip.close()
   oldzip.close()

   os.remove(oldzipfile)
   os.rename(newzipfile, oldzipfile)

ExeWithEggs (last edited 2023-11-21 06:30:53 by JimmyRetzlaff)