makeExe.py

Peter Hansen peter at engcorp.com
Tue Jan 27 17:12:07 EST 2004


Premshree Pillai wrote:
> 
> Peter wrote:
> > if you're interested
> > in integrating the
> > changes, and maintaining it, I'll forward our own
> > script which you can
> > pull apart and reuse as required...
> 
> I'd be interested in that.

Attached below.  Use for any purpose, no warranty. :-)

(The "batchutil" at the end stuff is not really necessary, but is 
handy for us to put Python code inside ".bat" files without having
to hardcode the actual path anywhere.  Basically it finds the .bat
file wherever it is in the path and executes it as a Python file.
I should probably post that sometime too....)

-Peter

------------------start of py2exe.bat-------------------
@goto dos

# setup.py
from distutils.core import setup
import py2exe
import sys
import os
import getopt
from StringIO import StringIO

def usage():
    print 'Usage: py2exe [-i path] mainmodule.py'
    sys.exit(0)


if __name__ == '__main__':
    opts, args = getopt.getopt(sys.argv[1:], 'h?qi:')

    quiet = False
    for opt,val in opts:
        if opt in ['-?', '-h']:
            usage()

        elif opt in ['-q']:
            quiet = True

        elif opt in ['-i']:
            abspath = os.path.normpath(os.path.abspath(val))
            if abspath not in sys.path:
                if not quiet:
                    print 'Appending to sys.path: %s' % abspath
                sys.path.append(abspath)

    if len(args) != 1:
        usage()

    mainScript = args[0]
    if mainScript.endswith('.py'):
        # get part before the .py
        baseName = os.path.splitext(mainScript)[0]

        # fake command line arguments
        sys.argv[1:] = ['py2exe']

        # install redirector for output
        if quiet:
            print 'Running...'

            sys.stdout = StringIO()
            sys.stderr = StringIO()

        try:
            result = setup(
                name=baseName,
                scripts=[mainScript]
                )

        finally:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__

        # quick hack which I hope checks that the run succeeded, at least partially
        # result should be an instance of distutils.dist.Distribution which has a have_run attribute
        if hasattr(result, 'have_run'):
            print '\nDone.  Look in dist/%s folder for the executable(s).' % baseName

    else:
        usage()



'''
:dos
@echo off
set argv=%0
:getargs
set argv=%argv% %1
shift
if not %1.==. goto getargs
call python -c "import batchutil; batchutil.runme()" %argv%
rem '''
---------------end of file-----------------------------------



More information about the Python-list mailing list