Author andy-chu
Recipients
Date 2007-06-19.03:40:37
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
The motivation for this is that distributing a zip file is a very light and easy way to distribute a python program with multiple packages/modules.  I have done this on Linux, Mac and Windows and it works very nicely -- except that you need a few extra files to bootstrap it: set PYTHONPATH to the zip file and run the main function.

With this small patch, you get rid of the need for extra files.  At the bottom is a demo on Linux.

On Windows, you can do a similar thing by making a file that is both a zip file and a batch file.  The batch file will pass %~f0 (itself) to the -z flag of the Python interpreter.

I ran this by Guido and he seemed to think it was a fine idea.  At Google, we have numerous platform-specific hacks in a program called "autopar" to solve this problem.

I have included the basic patch, but if you guys agree with this, I will add some tests and documentation.  And I think it might be useful to include something in the Tools/ directory to do what update_zip.sh does below (add a __zipmain__ module and a shebang/batch file header to a zip file, to make it executable)?

I think this may also help to fix a bug with eggs:

http://peak.telecommunity.com/DevCenter/setuptools#eggsecutable-scripts

IMPORTANT NOTE: Eggs with an "eggsecutable" header cannot be renamed, or invoked via symlinks. They must be invoked using their original filename, in order to ensure that, once running, pkg_resources will know what project and version is in use. The header script will check this and exit with an error if the .egg file has been renamed or is invoked via a symlink that changes its base name.



andychu testdata$ ls
__zipmain__.py  foo.py  foo.pyc  foo.zip  foo_exe.zip  header.txt  update_zip.sh


# The main program you're going to run in "development mode"

andychu testdata$ ./foo.py foo bar
argv: ['./foo.py', 'foo', 'bar']


# Same program, packaged into a zip file

andychu testdata$ ./foo_exe.zip foo bar
argv: ['-c', 'foo', 'bar']


# Contents of the zip file

andychu testdata$ unzip -l foo_exe.zip
Archive:  foo_exe.zip
warning [foo_exe.zip]:  51 extra bytes at beginning or within zipfile
  (attempting to process anyway)
  Length     Date   Time    Name
 --------    ----   ----    ----
      243  06-18-07 20:01   __zipmain__.py
      301  06-18-07 19:58   foo.py
 --------                   -------
      544                   2 files


# Demo script to build an executable zip file.

andychu testdata$ cat header.txt
#!/usr/local/google/clients/python/trunk/python -z


andychu testdata$ cat update_zip.sh
#!/bin/bash

# Make a regular zip file.
zip foo.zip foo.py __zipmain__.py

# Add a shebang line to it.
cat header.txt foo.zip > foo_exe.zip

# Make it executable.
chmod 750 foo_exe.zip
History
Date User Action Args
2007-08-23 15:58:47adminlinkissue1739468 messages
2007-08-23 15:58:47admincreate