| 1 | """ |
|---|
| 2 | Append module search paths for third-party packages to sys.path. |
|---|
| 3 | |
|---|
| 4 | This is stripped down and customized for use in py2app applications |
|---|
| 5 | """ |
|---|
| 6 | |
|---|
| 7 | import 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]) |
|---|
| 12 | if not _parent.endswith(_libdir): |
|---|
| 13 | _parent += _libdir |
|---|
| 14 | sys.path.append(_parent + '/site-packages.zip') |
|---|
| 15 | sys.path.insert(0, _parent + '/lib-dynload') |
|---|
| 16 | import os |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | def makepath(*paths): |
|---|
| 20 | dir = os.path.abspath(os.path.join(*paths)) |
|---|
| 21 | return dir, os.path.normcase(dir) |
|---|
| 22 | |
|---|
| 23 | for 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__) |
|---|
| 27 | del 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. |
|---|
| 31 | L = [] |
|---|
| 32 | _dirs_in_sys_path = {} |
|---|
| 33 | dir = dircase = None # sys.path may be empty at this point |
|---|
| 34 | for 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 |
|---|
| 42 | sys.path[:] = L |
|---|
| 43 | del dir, dircase, L |
|---|
| 44 | _dirs_in_sys_path = None |
|---|
| 45 | |
|---|
| 46 | def _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 | |
|---|
| 55 | def 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 | |
|---|
| 76 | def 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 | # |
|---|
| 112 | try: |
|---|
| 113 | import sitecustomize |
|---|
| 114 | except 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 | # |
|---|
| 122 | if hasattr(sys, "setdefaultencoding"): |
|---|
| 123 | del sys.setdefaultencoding |
|---|