[Distutils] Fwd: eggs and py2exe

Kevin Dangoor dangoor at gmail.com
Fri Oct 21 15:10:13 CEST 2005


Phillip suggested that this would be a good addition to the py2exe
wiki, but it appears that the py2exe wiki is closed off. This message
is my recipe for using py2exe (and py2app with basically the same
code) with eggs.

Kevin

---------- Forwarded message ----------
From: Kevin Dangoor <dangoor at gmail.com>
Date: Oct 20, 2005 9:20 PM
Subject: Re: [TurboGears] TurboGears and py2exe
To: turbogears at googlegroups.com


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)

As I mentioned in that thread from August, this is ugly and it would
be nice to fix... the only plus to going this route over fixing it
properly was that this route works for both py2exe and py2app. Fixing
it properly would've required me to dive into both of those packages.

Kevin

--
Kevin Dangoor
Author of the Zesty News RSS newsreader

email: kid at blazingthings.com
company: http://www.BlazingThings.com
blog: http://www.BlueSkyOnMars.com


--
Kevin Dangoor
Author of the Zesty News RSS newsreader

email: kid at blazingthings.com
company: http://www.BlazingThings.com
blog: http://www.BlueSkyOnMars.com


More information about the Distutils-SIG mailing list