Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 1 | """Append module search paths for third-party packages to sys.path. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 3 | **************************************************************** |
| 4 | * This module is automatically imported during initialization. * |
| 5 | **************************************************************** |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 7 | In earlier versions of Python (up to 1.5a3), scripts or modules that |
| 8 | needed to use site-specific modules would place ``import site'' |
| 9 | somewhere near the top of their code. Because of the automatic |
| 10 | import, this is no longer necessary (but code that does it still |
| 11 | works). |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 13 | This will append site-specific paths to the module search path. On |
Nick Coghlan | f2b16f3 | 2006-06-12 08:23:02 +0000 | [diff] [blame] | 14 | Unix (including Mac OSX), it starts with sys.prefix and |
| 15 | sys.exec_prefix (if different) and appends |
| 16 | lib/python<version>/site-packages as well as lib/site-python. |
| 17 | On other platforms (such as Windows), it tries each of the |
Nick Coghlan | 3fb55ca | 2006-06-12 08:19:37 +0000 | [diff] [blame] | 18 | prefixes directly, as well as with lib/site-packages appended. The |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 19 | resulting directories, if they exist, are appended to sys.path, and |
| 20 | also inspected for path configuration files. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 22 | A path configuration file is a file whose name has the form |
| 23 | <package>.pth; its contents are additional directories (one per line) |
| 24 | to be added to sys.path. Non-existing directories (or |
| 25 | non-directories) are never added to sys.path; no directory is added to |
| 26 | sys.path more than once. Blank lines and lines beginning with |
Guido van Rossum | facf24b | 2001-12-17 16:07:06 +0000 | [diff] [blame] | 27 | '#' are skipped. Lines starting with 'import' are executed. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 29 | For example, suppose sys.prefix and sys.exec_prefix are set to |
Neal Norwitz | 6e48256 | 2006-08-15 04:59:30 +0000 | [diff] [blame] | 30 | /usr/local and there is a directory /usr/local/lib/python2.5/site-packages |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 31 | with three subdirectories, foo, bar and spam, and two path |
| 32 | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
| 33 | following: |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 34 | |
| 35 | # foo package configuration |
| 36 | foo |
| 37 | bar |
| 38 | bletch |
| 39 | |
| 40 | and bar.pth contains: |
| 41 | |
| 42 | # bar package configuration |
| 43 | bar |
| 44 | |
| 45 | Then the following directories are added to sys.path, in this order: |
| 46 | |
Neal Norwitz | 6e48256 | 2006-08-15 04:59:30 +0000 | [diff] [blame] | 47 | /usr/local/lib/python2.5/site-packages/bar |
| 48 | /usr/local/lib/python2.5/site-packages/foo |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 49 | |
| 50 | Note that bletch is omitted because it doesn't exist; bar precedes foo |
| 51 | because bar.pth comes alphabetically before foo.pth; and spam is |
| 52 | omitted because it is not mentioned in either path configuration file. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 53 | |
| 54 | After these path manipulations, an attempt is made to import a module |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 55 | named sitecustomize, which can perform arbitrary additional |
| 56 | site-specific customizations. If this import fails with an |
| 57 | ImportError exception, it is silently ignored. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 59 | """ |
| 60 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 61 | import sys |
| 62 | import os |
| 63 | import __builtin__ |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 64 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 65 | # Prefixes for site-packages; add additional prefixes like /usr/local here |
| 66 | PREFIXES = [sys.prefix, sys.exec_prefix] |
| 67 | # Enable per user site-packages directory |
| 68 | # set it to False to disable the feature or True to force the feature |
| 69 | ENABLE_USER_SITE = None |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 70 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 71 | # for distutils.commands.install |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 72 | # These values are initialized by the getuserbase() and getusersitepackages() |
| 73 | # functions, through the main() function when Python starts. |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 74 | USER_SITE = None |
| 75 | USER_BASE = None |
| 76 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 77 | |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 78 | def makepath(*paths): |
Victor Stinner | d2f6ae6 | 2010-10-12 22:53:51 +0000 | [diff] [blame] | 79 | dir = os.path.join(*paths) |
| 80 | try: |
| 81 | dir = os.path.abspath(dir) |
| 82 | except OSError: |
| 83 | pass |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 84 | return dir, os.path.normcase(dir) |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 85 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 86 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 87 | def abs__file__(): |
| 88 | """Set all module' __file__ attribute to an absolute path""" |
| 89 | for m in sys.modules.values(): |
Neal Norwitz | 0c46985 | 2006-04-11 07:21:20 +0000 | [diff] [blame] | 90 | if hasattr(m, '__loader__'): |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 91 | continue # don't mess with a PEP 302-supplied __file__ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 92 | try: |
| 93 | m.__file__ = os.path.abspath(m.__file__) |
Victor Stinner | d2f6ae6 | 2010-10-12 22:53:51 +0000 | [diff] [blame] | 94 | except (AttributeError, OSError): |
| 95 | pass |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 96 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 97 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 98 | def removeduppaths(): |
| 99 | """ Remove duplicate entries from sys.path along with making them |
| 100 | absolute""" |
| 101 | # This ensures that the initial path provided by the interpreter contains |
| 102 | # only absolute pathnames, even if we're running from the build directory. |
| 103 | L = [] |
| 104 | known_paths = set() |
| 105 | for dir in sys.path: |
| 106 | # Filter out duplicate paths (on case-insensitive file systems also |
| 107 | # if they only differ in case); turn relative paths into absolute |
| 108 | # paths. |
| 109 | dir, dircase = makepath(dir) |
| 110 | if not dircase in known_paths: |
| 111 | L.append(dir) |
| 112 | known_paths.add(dircase) |
| 113 | sys.path[:] = L |
| 114 | return known_paths |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 115 | |
Fred Drake | e80c0d3 | 2002-07-25 20:13:03 +0000 | [diff] [blame] | 116 | # XXX This should not be part of site.py, since it is needed even when |
| 117 | # using the -S option for Python. See http://www.python.org/sf/586680 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 118 | def addbuilddir(): |
| 119 | """Append ./build/lib.<platform> in case we're running in the build dir |
| 120 | (especially for Guido :-)""" |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame] | 121 | from sysconfig import get_platform |
Jeremy Hylton | 6d58bf6 | 2003-07-18 17:45:33 +0000 | [diff] [blame] | 122 | s = "build/lib.%s-%.3s" % (get_platform(), sys.version) |
Georg Brandl | f00b38e | 2008-01-21 21:19:07 +0000 | [diff] [blame] | 123 | if hasattr(sys, 'gettotalrefcount'): |
| 124 | s += '-pydebug' |
Florent Xicluna | 176cda1 | 2010-03-22 22:52:11 +0000 | [diff] [blame] | 125 | s = os.path.join(os.path.dirname(sys.path.pop()), s) |
Guido van Rossum | 48eb9cd | 2001-01-19 21:54:59 +0000 | [diff] [blame] | 126 | sys.path.append(s) |
Guido van Rossum | 48eb9cd | 2001-01-19 21:54:59 +0000 | [diff] [blame] | 127 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 128 | |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 129 | def _init_pathinfo(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 130 | """Return a set containing all existing directory entries from sys.path""" |
| 131 | d = set() |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 132 | for dir in sys.path: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 133 | try: |
| 134 | if os.path.isdir(dir): |
| 135 | dir, dircase = makepath(dir) |
| 136 | d.add(dircase) |
| 137 | except TypeError: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 138 | continue |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 139 | return d |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 140 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 141 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 142 | def addpackage(sitedir, name, known_paths): |
Georg Brandl | 8d76cca | 2007-05-19 18:09:26 +0000 | [diff] [blame] | 143 | """Process a .pth file within the site-packages directory: |
| 144 | For each line in the file, either combine it with sitedir to a path |
| 145 | and add that to known_paths, or execute it if it starts with 'import '. |
| 146 | """ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 147 | if known_paths is None: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 148 | _init_pathinfo() |
| 149 | reset = 1 |
| 150 | else: |
| 151 | reset = 0 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 152 | fullname = os.path.join(sitedir, name) |
| 153 | try: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 154 | f = open(fullname, "rU") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 155 | except IOError: |
| 156 | return |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 157 | with f: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 158 | for line in f: |
| 159 | if line.startswith("#"): |
| 160 | continue |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 161 | if line.startswith(("import ", "import\t")): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 162 | exec line |
| 163 | continue |
| 164 | line = line.rstrip() |
| 165 | dir, dircase = makepath(sitedir, line) |
| 166 | if not dircase in known_paths and os.path.exists(dir): |
| 167 | sys.path.append(dir) |
| 168 | known_paths.add(dircase) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 169 | if reset: |
| 170 | known_paths = None |
| 171 | return known_paths |
| 172 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 173 | |
Brett Cannon | 12f8c4d | 2004-07-09 23:38:18 +0000 | [diff] [blame] | 174 | def addsitedir(sitedir, known_paths=None): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 175 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
| 176 | 'sitedir'""" |
| 177 | if known_paths is None: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 178 | known_paths = _init_pathinfo() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 179 | reset = 1 |
| 180 | else: |
| 181 | reset = 0 |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 182 | sitedir, sitedircase = makepath(sitedir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 183 | if not sitedircase in known_paths: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 184 | sys.path.append(sitedir) # Add path component |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 185 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 186 | names = os.listdir(sitedir) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 187 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 188 | return |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 189 | dotpth = os.extsep + "pth" |
| 190 | names = [name for name in names if name.endswith(dotpth)] |
| 191 | for name in sorted(names): |
| 192 | addpackage(sitedir, name, known_paths) |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 193 | if reset: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 194 | known_paths = None |
| 195 | return known_paths |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 196 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 197 | |
| 198 | def check_enableusersite(): |
| 199 | """Check if user site directory is safe for inclusion |
| 200 | |
Andrew M. Kuchling | 5217d5d | 2008-05-10 17:36:24 +0000 | [diff] [blame] | 201 | The function tests for the command line flag (including environment var), |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 202 | process uid/gid equal to effective uid/gid. |
| 203 | |
| 204 | None: Disabled for security reasons |
| 205 | False: Disabled by user (command line option) |
| 206 | True: Safe and enabled |
| 207 | """ |
| 208 | if sys.flags.no_user_site: |
| 209 | return False |
| 210 | |
| 211 | if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
| 212 | # check process uid == effective uid |
| 213 | if os.geteuid() != os.getuid(): |
| 214 | return None |
| 215 | if hasattr(os, "getgid") and hasattr(os, "getegid"): |
| 216 | # check process gid == effective gid |
| 217 | if os.getegid() != os.getgid(): |
| 218 | return None |
| 219 | |
| 220 | return True |
| 221 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 222 | def getuserbase(): |
| 223 | """Returns the `user base` directory path. |
| 224 | |
| 225 | The `user base` directory can be used to store data. If the global |
| 226 | variable ``USER_BASE`` is not initialized yet, this function will also set |
| 227 | it. |
| 228 | """ |
| 229 | global USER_BASE |
| 230 | if USER_BASE is not None: |
| 231 | return USER_BASE |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame] | 232 | from sysconfig import get_config_var |
| 233 | USER_BASE = get_config_var('userbase') |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 234 | return USER_BASE |
| 235 | |
| 236 | def getusersitepackages(): |
| 237 | """Returns the user-specific site-packages directory path. |
| 238 | |
| 239 | If the global variable ``USER_SITE`` is not initialized yet, this |
| 240 | function will also set it. |
| 241 | """ |
| 242 | global USER_SITE |
| 243 | user_base = getuserbase() # this will also set USER_BASE |
| 244 | |
| 245 | if USER_SITE is not None: |
| 246 | return USER_SITE |
| 247 | |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame] | 248 | from sysconfig import get_path |
| 249 | import os |
Ronald Oussoren | 2f88bfd | 2010-05-08 10:29:06 +0000 | [diff] [blame] | 250 | |
| 251 | if sys.platform == 'darwin': |
| 252 | from sysconfig import get_config_var |
| 253 | if get_config_var('PYTHONFRAMEWORK'): |
| 254 | USER_SITE = get_path('purelib', 'osx_framework_user') |
| 255 | return USER_SITE |
| 256 | |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame] | 257 | USER_SITE = get_path('purelib', '%s_user' % os.name) |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 258 | return USER_SITE |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 259 | |
| 260 | def addusersitepackages(known_paths): |
| 261 | """Add a per user site-package to sys.path |
| 262 | |
| 263 | Each user has its own python directory with site-packages in the |
| 264 | home directory. |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 265 | """ |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 266 | # get the per user site-package path |
| 267 | # this call will also make sure USER_BASE and USER_SITE are set |
| 268 | user_site = getusersitepackages() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 269 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 270 | if ENABLE_USER_SITE and os.path.isdir(user_site): |
| 271 | addsitedir(user_site, known_paths) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 272 | return known_paths |
| 273 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 274 | def getsitepackages(): |
| 275 | """Returns a list containing all global site-packages directories |
| 276 | (and possibly site-python). |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 277 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 278 | For each directory present in the global ``PREFIXES``, this function |
| 279 | will find its `site-packages` subdirectory depending on the system |
| 280 | environment, and will return a list of full paths. |
| 281 | """ |
| 282 | sitepackages = [] |
Benjamin Peterson | 3b95934 | 2010-06-03 21:21:03 +0000 | [diff] [blame] | 283 | seen = set() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 284 | |
| 285 | for prefix in PREFIXES: |
| 286 | if not prefix or prefix in seen: |
| 287 | continue |
Benjamin Peterson | 3b95934 | 2010-06-03 21:21:03 +0000 | [diff] [blame] | 288 | seen.add(prefix) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 289 | |
| 290 | if sys.platform in ('os2emx', 'riscos'): |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 291 | sitepackages.append(os.path.join(prefix, "Lib", "site-packages")) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 292 | elif os.sep == '/': |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 293 | sitepackages.append(os.path.join(prefix, "lib", |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 294 | "python" + sys.version[:3], |
| 295 | "site-packages")) |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 296 | sitepackages.append(os.path.join(prefix, "lib", "site-python")) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 297 | else: |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 298 | sitepackages.append(prefix) |
| 299 | sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 300 | if sys.platform == "darwin": |
| 301 | # for framework builds *only* we add the standard Apple |
Ronald Oussoren | e0154ed | 2009-03-30 23:10:35 +0000 | [diff] [blame] | 302 | # locations. |
Ronald Oussoren | 2f88bfd | 2010-05-08 10:29:06 +0000 | [diff] [blame] | 303 | from sysconfig import get_config_var |
| 304 | framework = get_config_var("PYTHONFRAMEWORK") |
| 305 | if framework and "/%s.framework/"%(framework,) in prefix: |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 306 | sitepackages.append( |
Ronald Oussoren | 2f88bfd | 2010-05-08 10:29:06 +0000 | [diff] [blame] | 307 | os.path.join("/Library", framework, |
Ronald Oussoren | e0154ed | 2009-03-30 23:10:35 +0000 | [diff] [blame] | 308 | sys.version[:3], "site-packages")) |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 309 | return sitepackages |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 310 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 311 | def addsitepackages(known_paths): |
| 312 | """Add site-packages (and possibly site-python) to sys.path""" |
| 313 | for sitedir in getsitepackages(): |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 314 | if os.path.isdir(sitedir): |
| 315 | addsitedir(sitedir, known_paths) |
| 316 | |
| 317 | return known_paths |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 318 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 319 | def setBEGINLIBPATH(): |
| 320 | """The OS/2 EMX port has optional extension modules that do double duty |
| 321 | as DLLs (and must use the .DLL file extension) for other extensions. |
| 322 | The library search path needs to be amended so these will be found |
| 323 | during module import. Use BEGINLIBPATH so that these are at the start |
| 324 | of the library search path. |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 325 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 326 | """ |
Andrew MacIntyre | 2e8a6e0 | 2003-12-02 12:27:25 +0000 | [diff] [blame] | 327 | dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload") |
| 328 | libpath = os.environ['BEGINLIBPATH'].split(';') |
| 329 | if libpath[-1]: |
| 330 | libpath.append(dllpath) |
| 331 | else: |
| 332 | libpath[-1] = dllpath |
| 333 | os.environ['BEGINLIBPATH'] = ';'.join(libpath) |
| 334 | |
| 335 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 336 | def setquit(): |
Brian Curtin | bc96f32 | 2010-04-12 23:30:49 +0000 | [diff] [blame] | 337 | """Define new builtins 'quit' and 'exit'. |
| 338 | |
| 339 | These are objects which make the interpreter exit when called. |
| 340 | The repr of each object contains a hint at how it works. |
Guido van Rossum | d89fa0c | 1998-08-07 18:01:14 +0000 | [diff] [blame] | 341 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 342 | """ |
| 343 | if os.sep == ':': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 344 | eof = 'Cmd-Q' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 345 | elif os.sep == '\\': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 346 | eof = 'Ctrl-Z plus Return' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 347 | else: |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 348 | eof = 'Ctrl-D (i.e. EOF)' |
Tim Peters | 88ca467 | 2006-03-10 23:39:56 +0000 | [diff] [blame] | 349 | |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 350 | class Quitter(object): |
| 351 | def __init__(self, name): |
| 352 | self.name = name |
| 353 | def __repr__(self): |
| 354 | return 'Use %s() or %s to exit' % (self.name, eof) |
| 355 | def __call__(self, code=None): |
Kurt B. Kaiser | d112bc7 | 2006-08-16 05:01:42 +0000 | [diff] [blame] | 356 | # Shells like IDLE catch the SystemExit, but listen when their |
| 357 | # stdin wrapper is closed. |
| 358 | try: |
| 359 | sys.stdin.close() |
| 360 | except: |
| 361 | pass |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 362 | raise SystemExit(code) |
| 363 | __builtin__.quit = Quitter('quit') |
| 364 | __builtin__.exit = Quitter('exit') |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 365 | |
| 366 | |
| 367 | class _Printer(object): |
| 368 | """interactive prompt objects for printing the license text, a list of |
| 369 | contributors and the copyright notice.""" |
| 370 | |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 371 | MAXLINES = 23 |
| 372 | |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 373 | def __init__(self, name, data, files=(), dirs=()): |
| 374 | self.__name = name |
| 375 | self.__data = data |
| 376 | self.__files = files |
| 377 | self.__dirs = dirs |
| 378 | self.__lines = None |
| 379 | |
| 380 | def __setup(self): |
| 381 | if self.__lines: |
| 382 | return |
| 383 | data = None |
| 384 | for dir in self.__dirs: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 385 | for filename in self.__files: |
| 386 | filename = os.path.join(dir, filename) |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 387 | try: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 388 | fp = file(filename, "rU") |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 389 | data = fp.read() |
| 390 | fp.close() |
| 391 | break |
| 392 | except IOError: |
| 393 | pass |
| 394 | if data: |
| 395 | break |
| 396 | if not data: |
| 397 | data = self.__data |
| 398 | self.__lines = data.split('\n') |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 399 | self.__linecnt = len(self.__lines) |
| 400 | |
| 401 | def __repr__(self): |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 402 | self.__setup() |
| 403 | if len(self.__lines) <= self.MAXLINES: |
| 404 | return "\n".join(self.__lines) |
| 405 | else: |
| 406 | return "Type %s() to see the full %s text" % ((self.__name,)*2) |
| 407 | |
| 408 | def __call__(self): |
| 409 | self.__setup() |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 410 | prompt = 'Hit Return for more, or q (and Return) to quit: ' |
| 411 | lineno = 0 |
| 412 | while 1: |
| 413 | try: |
| 414 | for i in range(lineno, lineno + self.MAXLINES): |
| 415 | print self.__lines[i] |
| 416 | except IndexError: |
| 417 | break |
| 418 | else: |
| 419 | lineno += self.MAXLINES |
| 420 | key = None |
| 421 | while key is None: |
| 422 | key = raw_input(prompt) |
| 423 | if key not in ('', 'q'): |
| 424 | key = None |
| 425 | if key == 'q': |
| 426 | break |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 427 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 428 | def setcopyright(): |
| 429 | """Set 'copyright' and 'credits' in __builtin__""" |
| 430 | __builtin__.copyright = _Printer("copyright", sys.copyright) |
| 431 | if sys.platform[:4] == 'java': |
| 432 | __builtin__.credits = _Printer( |
| 433 | "credits", |
| 434 | "Jython is maintained by the Jython developers (www.jython.org).") |
| 435 | else: |
| 436 | __builtin__.credits = _Printer("credits", """\ |
| 437 | Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
| 438 | for supporting Python development. See www.python.org for more information.""") |
| 439 | here = os.path.dirname(os.__file__) |
| 440 | __builtin__.license = _Printer( |
| 441 | "license", "See http://www.python.org/%.3s/license.html" % sys.version, |
| 442 | ["LICENSE.txt", "LICENSE"], |
| 443 | [os.path.join(here, os.pardir), here, os.curdir]) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 444 | |
| 445 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 446 | class _Helper(object): |
Brian Curtin | bc96f32 | 2010-04-12 23:30:49 +0000 | [diff] [blame] | 447 | """Define the builtin 'help'. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 448 | This is a wrapper around pydoc.help (with a twist). |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 449 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 450 | """ |
| 451 | |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 452 | def __repr__(self): |
| 453 | return "Type help() for interactive help, " \ |
| 454 | "or help(object) for help about object." |
| 455 | def __call__(self, *args, **kwds): |
| 456 | import pydoc |
| 457 | return pydoc.help(*args, **kwds) |
| 458 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 459 | def sethelper(): |
| 460 | __builtin__.help = _Helper() |
| 461 | |
| 462 | def aliasmbcs(): |
| 463 | """On Windows, some default encodings are not provided by Python, |
| 464 | while they are always available as "mbcs" in each locale. Make |
| 465 | them usable by aliasing to "mbcs" in such a case.""" |
| 466 | if sys.platform == 'win32': |
| 467 | import locale, codecs |
| 468 | enc = locale.getdefaultlocale()[1] |
| 469 | if enc.startswith('cp'): # "cp***" ? |
| 470 | try: |
| 471 | codecs.lookup(enc) |
| 472 | except LookupError: |
| 473 | import encodings |
| 474 | encodings._cache[enc] = encodings._unknown |
| 475 | encodings.aliases.aliases[enc] = 'mbcs' |
| 476 | |
| 477 | def setencoding(): |
| 478 | """Set the string encoding used by the Unicode implementation. The |
| 479 | default is 'ascii', but if you're willing to experiment, you can |
| 480 | change this.""" |
| 481 | encoding = "ascii" # Default value set by _PyUnicode_Init() |
| 482 | if 0: |
| 483 | # Enable to support locale aware default string encodings. |
| 484 | import locale |
| 485 | loc = locale.getdefaultlocale() |
| 486 | if loc[1]: |
| 487 | encoding = loc[1] |
| 488 | if 0: |
| 489 | # Enable to switch off string to Unicode coercion and implicit |
| 490 | # Unicode to string conversion. |
| 491 | encoding = "undefined" |
| 492 | if encoding != "ascii": |
| 493 | # On Non-Unicode builds this will raise an AttributeError... |
| 494 | sys.setdefaultencoding(encoding) # Needs Python Unicode build ! |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 495 | |
| 496 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 497 | def execsitecustomize(): |
| 498 | """Run custom site specific code, if available.""" |
| 499 | try: |
| 500 | import sitecustomize |
| 501 | except ImportError: |
| 502 | pass |
Victor Stinner | 6664426 | 2010-03-10 22:30:19 +0000 | [diff] [blame] | 503 | except Exception: |
| 504 | if sys.flags.verbose: |
| 505 | sys.excepthook(*sys.exc_info()) |
| 506 | else: |
| 507 | print >>sys.stderr, \ |
| 508 | "'import sitecustomize' failed; use -v for traceback" |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 509 | |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 510 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 511 | def execusercustomize(): |
| 512 | """Run custom user specific code, if available.""" |
| 513 | try: |
| 514 | import usercustomize |
| 515 | except ImportError: |
| 516 | pass |
Victor Stinner | 6664426 | 2010-03-10 22:30:19 +0000 | [diff] [blame] | 517 | except Exception: |
| 518 | if sys.flags.verbose: |
| 519 | sys.excepthook(*sys.exc_info()) |
| 520 | else: |
| 521 | print>>sys.stderr, \ |
Victor Stinner | 3ec3200 | 2010-03-10 22:45:04 +0000 | [diff] [blame] | 522 | "'import usercustomize' failed; use -v for traceback" |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 523 | |
| 524 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 525 | def main(): |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 526 | global ENABLE_USER_SITE |
| 527 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 528 | abs__file__() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 529 | known_paths = removeduppaths() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 530 | if (os.name == "posix" and sys.path and |
| 531 | os.path.basename(sys.path[-1]) == "Modules"): |
| 532 | addbuilddir() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 533 | if ENABLE_USER_SITE is None: |
| 534 | ENABLE_USER_SITE = check_enableusersite() |
| 535 | known_paths = addusersitepackages(known_paths) |
| 536 | known_paths = addsitepackages(known_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 537 | if sys.platform == 'os2emx': |
| 538 | setBEGINLIBPATH() |
| 539 | setquit() |
| 540 | setcopyright() |
| 541 | sethelper() |
| 542 | aliasmbcs() |
| 543 | setencoding() |
| 544 | execsitecustomize() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 545 | if ENABLE_USER_SITE: |
| 546 | execusercustomize() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 547 | # Remove sys.setdefaultencoding() so that users cannot change the |
| 548 | # encoding after initialization. The test for presence is needed when |
| 549 | # this module is run as a script, because this code is executed twice. |
| 550 | if hasattr(sys, "setdefaultencoding"): |
| 551 | del sys.setdefaultencoding |
Marc-André Lemburg | 990bbe9 | 2000-06-07 09:12:09 +0000 | [diff] [blame] | 552 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 553 | main() |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 554 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 555 | def _script(): |
| 556 | help = """\ |
| 557 | %s [--user-base] [--user-site] |
| 558 | |
| 559 | Without arguments print some useful information |
| 560 | With arguments print the value of USER_BASE and/or USER_SITE separated |
| 561 | by '%s'. |
| 562 | |
| 563 | Exit codes with --user-base or --user-site: |
| 564 | 0 - user site directory is enabled |
Christian Heimes | 17433d2 | 2008-05-09 12:19:09 +0000 | [diff] [blame] | 565 | 1 - user site directory is disabled by user |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 566 | 2 - uses site directory is disabled by super user |
| 567 | or for security reasons |
| 568 | >2 - unknown error |
| 569 | """ |
| 570 | args = sys.argv[1:] |
| 571 | if not args: |
| 572 | print "sys.path = [" |
| 573 | for dir in sys.path: |
| 574 | print " %r," % (dir,) |
| 575 | print "]" |
| 576 | print "USER_BASE: %r (%s)" % (USER_BASE, |
| 577 | "exists" if os.path.isdir(USER_BASE) else "doesn't exist") |
| 578 | print "USER_SITE: %r (%s)" % (USER_SITE, |
| 579 | "exists" if os.path.isdir(USER_SITE) else "doesn't exist") |
| 580 | print "ENABLE_USER_SITE: %r" % ENABLE_USER_SITE |
| 581 | sys.exit(0) |
| 582 | |
| 583 | buffer = [] |
| 584 | if '--user-base' in args: |
| 585 | buffer.append(USER_BASE) |
| 586 | if '--user-site' in args: |
| 587 | buffer.append(USER_SITE) |
| 588 | |
| 589 | if buffer: |
| 590 | print os.pathsep.join(buffer) |
| 591 | if ENABLE_USER_SITE: |
| 592 | sys.exit(0) |
| 593 | elif ENABLE_USER_SITE is False: |
| 594 | sys.exit(1) |
| 595 | elif ENABLE_USER_SITE is None: |
| 596 | sys.exit(2) |
| 597 | else: |
| 598 | sys.exit(3) |
| 599 | else: |
| 600 | import textwrap |
| 601 | print textwrap.dedent(help % (sys.argv[0], os.pathsep)) |
| 602 | sys.exit(10) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 603 | |
| 604 | if __name__ == '__main__': |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 605 | _script() |