root/nodebox/branches/try-qt/site.py

Revision 36, 3.4 kB (checked in by fdb, 2 years ago)

NodeBox?1.0rc8 build script

Line 
1"""
2Append module search paths for third-party packages to sys.path.
3
4This is stripped down and customized for use in py2app applications
5"""
6
7import sys
8# os is actually in the zip, so we need to do this here.
9# we can't call it python24.zip because zlib is not a built-in module (!)
10_libdir = '/lib/python' + sys.version[:3]
11_parent = '/'.join(__file__.split('/')[:-1])
12if not _parent.endswith(_libdir):
13    _parent += _libdir
14sys.path.append(_parent + '/site-packages.zip')
15sys.path.insert(0, _parent + '/lib-dynload')
16import os
17
18
19def makepath(*paths):
20    dir = os.path.abspath(os.path.join(*paths))
21    return dir, os.path.normcase(dir)
22
23for m in sys.modules.values():
24    f = getattr(m, '__file__', None)
25    if isinstance(f, basestring) and os.path.exists(f):
26        m.__file__ = os.path.abspath(m.__file__)
27del m
28
29# This ensures that the initial path provided by the interpreter contains
30# only absolute pathnames, even if we're running from the build directory.
31L = []
32_dirs_in_sys_path = {}
33dir = dircase = None  # sys.path may be empty at this point
34for dir in sys.path:
35    # Filter out duplicate paths (on case-insensitive file systems also
36    # if they only differ in case); turn relative paths into absolute
37    # paths.
38    dir, dircase = makepath(dir)
39    if not dircase in _dirs_in_sys_path:
40        L.append(dir)
41        _dirs_in_sys_path[dircase] = 1
42sys.path[:] = L
43del dir, dircase, L
44_dirs_in_sys_path = None
45
46def _init_pathinfo():
47    global _dirs_in_sys_path
48    _dirs_in_sys_path = d = {}
49    for dir in sys.path:
50        if dir and not os.path.isdir(dir):
51            continue
52        dir, dircase = makepath(dir)
53        d[dircase] = 1
54
55def addsitedir(sitedir):
56    global _dirs_in_sys_path
57    if _dirs_in_sys_path is None:
58        _init_pathinfo()
59        reset = 1
60    else:
61        reset = 0
62    sitedir, sitedircase = makepath(sitedir)
63    if not sitedircase in _dirs_in_sys_path:
64        sys.path.append(sitedir)        # Add path component
65    try:
66        names = os.listdir(sitedir)
67    except os.error:
68        return
69    names.sort()
70    for name in names:
71        if name[-4:] == os.extsep + "pth":
72            addpackage(sitedir, name)
73    if reset:
74        _dirs_in_sys_path = None
75
76def addpackage(sitedir, name):
77    global _dirs_in_sys_path
78    if _dirs_in_sys_path is None:
79        _init_pathinfo()
80        reset = 1
81    else:
82        reset = 0
83    fullname = os.path.join(sitedir, name)
84    try:
85        f = open(fullname)
86    except IOError:
87        return
88    while 1:
89        dir = f.readline()
90        if not dir:
91            break
92        if dir[0] == '#':
93            continue
94        if dir.startswith("import"):
95            exec dir
96            continue
97        if dir[-1] == '\n':
98            dir = dir[:-1]
99        dir, dircase = makepath(sitedir, dir)
100        if not dircase in _dirs_in_sys_path and os.path.exists(dir):
101            sys.path.append(dir)
102            _dirs_in_sys_path[dircase] = 1
103    if reset:
104        _dirs_in_sys_path = None
105
106
107#sys.setdefaultencoding('utf-8')
108
109#
110# Run custom site specific code, if available.
111#
112try:
113    import sitecustomize
114except ImportError:
115    pass
116
117#
118# Remove sys.setdefaultencoding() so that users cannot change the
119# encoding after initialization.  The test for presence is needed when
120# this module is run as a script, because this code is executed twice.
121#
122if hasattr(sys, "setdefaultencoding"):
123    del sys.setdefaultencoding
Note: See TracBrowser for help on using the browser.