Use sitecustomize.py to set python paths

This commit is contained in:
Yevgen Muntyan 2010-04-18 19:00:26 -07:00
parent 8b15b7a276
commit 93834677bb
3 changed files with 173 additions and 61 deletions

View File

@ -0,0 +1,154 @@
import os
import sys
import getopt
import shutil
import glob
import compileall
import zipfile
def remove_file(path, quiet=False):
if os.path.isdir(path):
if not quiet:
print ' removing directory %s' % (path,)
shutil.rmtree(path)
elif os.path.exists(path):
if not quiet:
print ' removing file %s' % (path,)
os.remove(path)
def remove_files(basedir, *args, **kwargs):
for g in args:
for path in glob.glob(os.path.join(basedir, g)):
remove_file(path, quiet=kwargs.get('quiet', False))
def trim(pythondir):
def make_path(*args):
return os.path.join(pythondir, os.path.join(*args))
def path_exists(relpath):
return os.path.exists(make_path(relpath))
if not os.path.isdir(pythondir):
print >> sys.stderr, "Not a directory: '%s'" % (pythondir,)
return 1
if not os.path.isfile(make_path('python.exe')) or \
not os.path.isdir(make_path('Lib')):
print >> sys.stderr, "Directory '%s' does not look like a " + \
"Win32 Python directory" % (pythondir,)
return 1
for d in ('Doc', 'include', 'libs', 'Scripts', 'share', 'tcl'):
remove_file(make_path(d))
remove_file(make_path('NEWS.txt'))
remove_files(pythondir, 'w9xpopen.exe', '*-wininst.log', 'Remove*.exe')
remove_file(make_path('Tools', 'pynche'))
for f in ('_ctypes_test.pyd', '_testcapi.pyd', '_tkinter.pyd',
'tcl[0-9]*.dll', 'tclpip[0-9]*.dll', 'tk[0-9]*.dll'):
remove_files(pythondir, os.path.join('DLLs', f))
for f in ('bsddb\\test', 'ctypes\\test', 'curses', 'distutils',
'email\\test', 'idlelib', 'json\\tests', 'lib2to3',
'lib-tk', 'pkgconfig', 'sqlite3\\test', 'test',
'__phello__.foo.py'):
remove_file(make_path('Lib', f))
for f in ('README.txt', '*.egg-info', '*\\*.egg-info', 'gtk-2.0\\codegen'):
remove_files(pythondir, os.path.join('Lib', 'site-packages', f))
for f in ('*.py[co]', '*\\*.py[co]', '*\\*\\*.py[co]', '*\\*\\*\\*.py[co]',
'*\\*\\*\\*\\*.py[co]', '*\\*\\*\\*\\*\\*.py[co]'):
remove_files(pythondir, f)
return 0
def makedist(srcdir, destdir):
if not os.path.isdir(srcdir):
print >> sys.stderr, "Not a directory: '%s'" % (srcdir,)
return 1
if os.path.exists(destdir):
print >> sys.stderr, "Directory '%s' already exists" % (destdir,)
return 1
def copyfiles(*args):
if len(args) < 2:
raise RuntimeError('oops')
for src in args[:-1]:
files = glob.glob(os.path.join(srcdir, src))
if not files:
raise RuntimeError("File '%s' does not exist in directory '%s'" % \
(src, srcdir))
for path in files:
print ' copying %s to %s' % (path, args[-1])
if os.path.isdir(path):
dest = os.path.join(destdir, args[-1], os.path.basename(path))
shutil.copytree(path, dest)
else:
shutil.copy(path, os.path.join(destdir, args[-1]))
dlls = glob.glob(os.path.join(srcdir, 'python[0-9][0-9].dll'))
if not dlls:
raise RuntimeError("No python dll in directory '%s'" % (srcdir,))
if len(dlls) > 1:
raise RuntimeError("Too many python dlls in directory '%s'" % (srcdir,))
pydll, ext = os.path.splitext(os.path.basename(dlls[0]))
assert ext == '.dll' and pydll.startswith('python')
os.mkdir(destdir)
os.mkdir(os.path.join(destdir, 'bin'))
os.mkdir(os.path.join(destdir, 'lib'))
os.mkdir(os.path.join(destdir, 'lib', 'python'))
copyfiles('*.exe', '*.dll', 'bin')
copyfiles('*.txt', 'lib\\python')
copyfiles('DLLs', 'Tools', 'lib\\python')
pylib_path = os.path.join(destdir, 'bin', 'Lib')
pyzip_path = os.path.join(destdir, 'bin', pydll + '.zip')
copyfiles('Lib', 'bin')
print ' moving bin\\Lib\\site-packages to lib\\python\\'
shutil.move(os.path.join(pylib_path, 'site-packages'),
os.path.join(destdir, 'lib\\python'))
shutil.copy(os.path.join(os.path.dirname(__file__), 'sitecustomize.py'),
pylib_path)
print " creating '%s'" % (pyzip_path,)
libzip = zipfile.PyZipFile(pyzip_path, 'w', zipfile.ZIP_DEFLATED)
libzip.writepy(pylib_path)
for d in glob.glob(os.path.join(pylib_path, '*')):
if os.path.isdir(d):
libzip.writepy(d)
libzip.close()
print ' compiling python files in lib\\python\\site-packages'
compileall.compile_dir(os.path.join(destdir, 'lib', 'python', 'site-packages'),
quiet=1)
remove_file(pylib_path)
def main(argv):
usage = '''\
usage: makepythondist.py makedist srcdir destdir
makepythondist.py trim srcdir'''
opts, args = getopt.getopt(argv[1:], "h", ["help"])
for opt, arg in opts:
if opt in ('-h', '--help'):
print >> sys.stderr, usage
return 1
else:
raise RuntimeError("oops")
job = None
if len(args) >= 1:
job = args[0]
if job == 'makedist':
if len(args) == 3:
return makedist(args[1], args[2])
elif job == 'trim':
if len(args) == 2:
return trim(args[1])
print >> sys.stderr, usage
return 1
if __name__ == '__main__':
sys.exit(main(sys.argv))

View File

@ -0,0 +1,19 @@
import os
import sys
__all__ = []
try:
# it is supposed to be a zip file
filename = __file__
mydir = os.path.dirname(filename)
if mydir.lower().endswith('.zip'):
mydir = os.path.dirname(mydir)
if os.path.basename(mydir).lower() == 'bin':
libdir = os.path.join(os.path.dirname(mydir), 'lib', 'python')
sys.path.insert(1, os.path.join(libdir, 'site-packages', 'gtk-2.0'))
sys.path.insert(1, os.path.join(libdir, 'site-packages'))
sys.path.insert(1, os.path.join(libdir, 'DLLs'))
except:
# oh well...
pass

View File

@ -456,63 +456,6 @@ moo_python_api_py_arg_parse_tuple (MooPyObject *args,
}
#ifdef __WIN32__
static gboolean
sys_path_add_dir (const char *dir)
{
PyObject *path;
PyObject *add;
int result;
g_return_val_if_fail (dir != NULL, FALSE);
path = PySys_GetObject ("path");
if (!path)
{
PyErr_Print ();
return FALSE;
}
if (!PyList_Check (path))
{
g_critical ("sys.path is not a list");
return FALSE;
}
add = PyString_FromString (dir);
result = PyList_Insert(path, 1, add);
Py_DECREF (add);
if (result != 0)
{
PyErr_Print ();
return FALSE;
}
return TRUE;
}
static void
add_win32_dirs (void)
{
char *top = g_win32_get_package_installation_directory_of_module (NULL);
char *dir1 = g_build_filename (top, "lib", "python", "DLLs", NULL);
char *dir2 = g_build_filename (top, "lib", "python", "site-packages", NULL);
char *dir3 = g_build_filename (top, "lib", "python", "site-packages", "gtk-2.0", NULL);
sys_path_add_dir (dir3);
sys_path_add_dir (dir2);
sys_path_add_dir (dir1);
g_free (dir3);
g_free (dir2);
g_free (dir1);
g_free (top);
}
#endif /* __WIN32__ */
static gboolean
moo_python_api_init (void)
{
@ -577,10 +520,6 @@ moo_python_api_init (void)
_moo_py_init_print_funcs ();
}
#ifdef __WIN32__
add_win32_dirs ();
#endif
api.py_none = (gpointer) Py_None;
#ifndef MOO_DEBUG