"""
This script just puts together a list of files that it thinks are python libraries.
It puts them together in such a way that py2exe thinks we're trying to import them all.
setup.py py2exe
Will then bundle all of them together.
The executable it generates will actually just run the filename it is passed as the first argument.
This means that if you build this executable on varying versions of python (2.2, 2.3, 2.4)
you can test programs in these different environments.
(You can test your scrip[t for backward compatibility with these versions of python)
Another use, is as a way of running any python script on a machine that doesn't have python installed.
At the moment we look in 'Lib' and 'lib-tk' directory of our pythonpath - looking for files that look like python modules.
It would be easy to tweak to add extra modules.
Set incdirs to None to search every directory in sys.path.
(This will include 'site-packages' and result in a bigger distribution).
With help from Bruno Thoorens.
"""
import os
import sys
#incdirs = ['lib', 'lib-tk'] # directories we want to include. Set this value to None to search the whole of sys.path
incdirs = None
win32exc = True # there is a win32\lib directory - I don't want to include this in the search
outfile = 'testenv.py'
excludelist = ['pty', 'test', ]
##############################
libdirs=[]
for p in sys.path:
p1,p2=os.path.split(p)
if win32exc and 'win32' in p1.lower():
continue
# print p1,p2 # if you want to see what is p1 and p2 ;o)
if not incdirs or p2.lower() in incdirs:
libdirs.append(p)
print 'Directories we are searching : '
print libdirs
print
envfile = ['if False:']
for libdir in libdirs:
for entry in os.listdir(libdir):
if os.path.isfile(libdir + '\\' + entry):
entry = entry[:entry.rfind('.')]
if (' import ' + entry) in stdlib or '-' in entry or '.' in entry:
continue
if entry.startswith('_') or entry in excludelist:
continue
try:
__import__(entry) # we are actually attempting the import, expensive but checks we only include modules we can actually import
except:
pass
else:
envfile.append(' import ' + entry)
print 'Found %s modules to add.' % (len(envfile)-1)
thecode = '\n'.join(envfile) + """
import sys
import os
import imp
def main_is_frozen():
return (hasattr(sys, "frozen") or # new py2exe
hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")) # tools/freeze
def get_main_dir():
if main_is_frozen():
return os.path.abspath(os.path.dirname(sys.executable))
return os.path.abspath(os.path.dirname(sys.argv[0]))
if len(sys.argv) == 1:
print 'No filename specified'
print 'Usage : testenv *args'
sys.exit(1)
filename = sys.argv[1]
if not os.path.isfile(filename):
print 'Specified file, "%s", doesn\\'t exist' % filename
filename = os.path.abspath(filename)
sys.path.append(os.path.abspath('.'))
sys.path.append(get_main_dir())
sys.path.append(os.path.dirname(filename))
__file__ = filename
sys.argv = [filename] + sys.argv[2:]
execfile(filename)
"""
handle = file(outfile, 'w')
handle.write(thecode)
handle.close()
print 'Done'