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 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 7 | This will append site-specific paths to the module search path. On |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | Unix (including Mac OSX), it starts with sys.prefix and |
| 9 | sys.exec_prefix (if different) and appends |
| 10 | lib/python<version>/site-packages as well as lib/site-python. |
| 11 | On other platforms (such as Windows), it tries each of the |
| 12 | prefixes directly, as well as with lib/site-packages appended. The |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 13 | resulting directories, if they exist, are appended to sys.path, and |
| 14 | also inspected for path configuration files. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 16 | A path configuration file is a file whose name has the form |
| 17 | <package>.pth; its contents are additional directories (one per line) |
| 18 | to be added to sys.path. Non-existing directories (or |
| 19 | non-directories) are never added to sys.path; no directory is added to |
| 20 | sys.path more than once. Blank lines and lines beginning with |
Guido van Rossum | facf24b | 2001-12-17 16:07:06 +0000 | [diff] [blame] | 21 | '#' are skipped. Lines starting with 'import' are executed. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 23 | For example, suppose sys.prefix and sys.exec_prefix are set to |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 24 | /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] | 25 | with three subdirectories, foo, bar and spam, and two path |
| 26 | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
| 27 | following: |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 28 | |
| 29 | # foo package configuration |
| 30 | foo |
| 31 | bar |
| 32 | bletch |
| 33 | |
| 34 | and bar.pth contains: |
| 35 | |
| 36 | # bar package configuration |
| 37 | bar |
| 38 | |
| 39 | Then the following directories are added to sys.path, in this order: |
| 40 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 41 | /usr/local/lib/python2.5/site-packages/bar |
| 42 | /usr/local/lib/python2.5/site-packages/foo |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 43 | |
| 44 | Note that bletch is omitted because it doesn't exist; bar precedes foo |
| 45 | because bar.pth comes alphabetically before foo.pth; and spam is |
| 46 | omitted because it is not mentioned in either path configuration file. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 47 | |
| 48 | 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] | 49 | named sitecustomize, which can perform arbitrary additional |
| 50 | site-specific customizations. If this import fails with an |
| 51 | ImportError exception, it is silently ignored. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 53 | """ |
| 54 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 55 | import sys |
| 56 | import os |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 57 | import builtins |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 58 | import traceback |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 59 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 60 | # Prefixes for site-packages; add additional prefixes like /usr/local here |
| 61 | PREFIXES = [sys.prefix, sys.exec_prefix] |
| 62 | # Enable per user site-packages directory |
| 63 | # set it to False to disable the feature or True to force the feature |
| 64 | ENABLE_USER_SITE = None |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 65 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 66 | # for distutils.commands.install |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 67 | # These values are initialized by the getuserbase() and getusersitepackages() |
| 68 | # functions, through the main() function when Python starts. |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 69 | USER_SITE = None |
| 70 | USER_BASE = None |
| 71 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 72 | |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 73 | def makepath(*paths): |
Victor Stinner | b103a93 | 2010-10-12 22:23:23 +0000 | [diff] [blame] | 74 | dir = os.path.join(*paths) |
| 75 | try: |
| 76 | dir = os.path.abspath(dir) |
| 77 | except OSError: |
| 78 | pass |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 79 | return dir, os.path.normcase(dir) |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 80 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 81 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 82 | def abs_paths(): |
| 83 | """Set all module __file__ and __cached__ attributes to an absolute path""" |
Guido van Rossum | 7ac9d40 | 2007-05-18 00:24:43 +0000 | [diff] [blame] | 84 | for m in set(sys.modules.values()): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 85 | if hasattr(m, '__loader__'): |
| 86 | continue # don't mess with a PEP 302-supplied __file__ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 87 | try: |
| 88 | m.__file__ = os.path.abspath(m.__file__) |
Victor Stinner | b103a93 | 2010-10-12 22:23:23 +0000 | [diff] [blame] | 89 | except (AttributeError, OSError): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 90 | pass |
| 91 | try: |
| 92 | m.__cached__ = os.path.abspath(m.__cached__) |
Victor Stinner | b103a93 | 2010-10-12 22:23:23 +0000 | [diff] [blame] | 93 | except (AttributeError, OSError): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 94 | pass |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 95 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 96 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 97 | def removeduppaths(): |
| 98 | """ Remove duplicate entries from sys.path along with making them |
| 99 | absolute""" |
| 100 | # This ensures that the initial path provided by the interpreter contains |
| 101 | # only absolute pathnames, even if we're running from the build directory. |
| 102 | L = [] |
| 103 | known_paths = set() |
| 104 | for dir in sys.path: |
| 105 | # Filter out duplicate paths (on case-insensitive file systems also |
| 106 | # if they only differ in case); turn relative paths into absolute |
| 107 | # paths. |
| 108 | dir, dircase = makepath(dir) |
| 109 | if not dircase in known_paths: |
| 110 | L.append(dir) |
| 111 | known_paths.add(dircase) |
| 112 | sys.path[:] = L |
| 113 | return known_paths |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 114 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 115 | |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 116 | def _init_pathinfo(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 117 | """Return a set containing all existing directory entries from sys.path""" |
| 118 | d = set() |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 119 | for dir in sys.path: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 120 | try: |
| 121 | if os.path.isdir(dir): |
| 122 | dir, dircase = makepath(dir) |
| 123 | d.add(dircase) |
| 124 | except TypeError: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 125 | continue |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 126 | return d |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 127 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 128 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 129 | def addpackage(sitedir, name, known_paths): |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 130 | """Process a .pth file within the site-packages directory: |
| 131 | For each line in the file, either combine it with sitedir to a path |
| 132 | and add that to known_paths, or execute it if it starts with 'import '. |
| 133 | """ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 134 | if known_paths is None: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 135 | _init_pathinfo() |
| 136 | reset = 1 |
| 137 | else: |
| 138 | reset = 0 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 139 | fullname = os.path.join(sitedir, name) |
| 140 | try: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 141 | f = open(fullname, "rU") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 142 | except IOError: |
| 143 | return |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 144 | with f: |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 145 | for n, line in enumerate(f): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 146 | if line.startswith("#"): |
| 147 | continue |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 148 | try: |
| 149 | if line.startswith(("import ", "import\t")): |
| 150 | exec(line) |
| 151 | continue |
| 152 | line = line.rstrip() |
| 153 | dir, dircase = makepath(sitedir, line) |
| 154 | if not dircase in known_paths and os.path.exists(dir): |
| 155 | sys.path.append(dir) |
| 156 | known_paths.add(dircase) |
| 157 | except Exception as err: |
| 158 | print("Error processing line {:d} of {}:\n".format(n+1, fullname), |
| 159 | file=sys.stderr) |
| 160 | for record in traceback.format_exception(*sys.exc_info()): |
| 161 | for line in record.splitlines(): |
| 162 | print(' '+line, file=sys.stderr) |
| 163 | print("\nRemainder of file ignored", file=sys.stderr) |
| 164 | break |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 165 | if reset: |
| 166 | known_paths = None |
| 167 | return known_paths |
| 168 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 169 | |
Brett Cannon | 12f8c4d | 2004-07-09 23:38:18 +0000 | [diff] [blame] | 170 | def addsitedir(sitedir, known_paths=None): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 171 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
| 172 | 'sitedir'""" |
| 173 | if known_paths is None: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 174 | known_paths = _init_pathinfo() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 175 | reset = 1 |
| 176 | else: |
| 177 | reset = 0 |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 178 | sitedir, sitedircase = makepath(sitedir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 179 | if not sitedircase in known_paths: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 180 | sys.path.append(sitedir) # Add path component |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 181 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 182 | names = os.listdir(sitedir) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 183 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 184 | return |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 185 | names = [name for name in names if name.endswith(".pth")] |
| 186 | for name in sorted(names): |
| 187 | addpackage(sitedir, name, known_paths) |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 188 | if reset: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 189 | known_paths = None |
| 190 | return known_paths |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 191 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 192 | |
| 193 | def check_enableusersite(): |
| 194 | """Check if user site directory is safe for inclusion |
| 195 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 196 | The function tests for the command line flag (including environment var), |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 197 | process uid/gid equal to effective uid/gid. |
| 198 | |
| 199 | None: Disabled for security reasons |
| 200 | False: Disabled by user (command line option) |
| 201 | True: Safe and enabled |
| 202 | """ |
| 203 | if sys.flags.no_user_site: |
| 204 | return False |
| 205 | |
| 206 | if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
| 207 | # check process uid == effective uid |
| 208 | if os.geteuid() != os.getuid(): |
| 209 | return None |
| 210 | if hasattr(os, "getgid") and hasattr(os, "getegid"): |
| 211 | # check process gid == effective gid |
| 212 | if os.getegid() != os.getgid(): |
| 213 | return None |
| 214 | |
| 215 | return True |
| 216 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 217 | def getuserbase(): |
| 218 | """Returns the `user base` directory path. |
| 219 | |
| 220 | The `user base` directory can be used to store data. If the global |
| 221 | variable ``USER_BASE`` is not initialized yet, this function will also set |
| 222 | it. |
| 223 | """ |
| 224 | global USER_BASE |
| 225 | if USER_BASE is not None: |
| 226 | return USER_BASE |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 227 | from sysconfig import get_config_var |
| 228 | USER_BASE = get_config_var('userbase') |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 229 | return USER_BASE |
| 230 | |
| 231 | def getusersitepackages(): |
| 232 | """Returns the user-specific site-packages directory path. |
| 233 | |
| 234 | If the global variable ``USER_SITE`` is not initialized yet, this |
| 235 | function will also set it. |
| 236 | """ |
| 237 | global USER_SITE |
| 238 | user_base = getuserbase() # this will also set USER_BASE |
| 239 | |
| 240 | if USER_SITE is not None: |
| 241 | return USER_SITE |
| 242 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 243 | from sysconfig import get_path |
| 244 | import os |
Ronald Oussoren | 4cda46a | 2010-05-08 10:49:43 +0000 | [diff] [blame] | 245 | |
| 246 | if sys.platform == 'darwin': |
| 247 | from sysconfig import get_config_var |
| 248 | if get_config_var('PYTHONFRAMEWORK'): |
| 249 | USER_SITE = get_path('purelib', 'osx_framework_user') |
| 250 | return USER_SITE |
| 251 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 252 | USER_SITE = get_path('purelib', '%s_user' % os.name) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 253 | return USER_SITE |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 254 | |
| 255 | def addusersitepackages(known_paths): |
| 256 | """Add a per user site-package to sys.path |
| 257 | |
| 258 | Each user has its own python directory with site-packages in the |
| 259 | home directory. |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 260 | """ |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 261 | # get the per user site-package path |
| 262 | # this call will also make sure USER_BASE and USER_SITE are set |
| 263 | user_site = getusersitepackages() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 264 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 265 | if ENABLE_USER_SITE and os.path.isdir(user_site): |
| 266 | addsitedir(user_site, known_paths) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 267 | return known_paths |
| 268 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 269 | def getsitepackages(): |
| 270 | """Returns a list containing all global site-packages directories |
| 271 | (and possibly site-python). |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 272 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 273 | For each directory present in the global ``PREFIXES``, this function |
| 274 | will find its `site-packages` subdirectory depending on the system |
| 275 | environment, and will return a list of full paths. |
| 276 | """ |
| 277 | sitepackages = [] |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 278 | seen = set() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 279 | |
| 280 | for prefix in PREFIXES: |
| 281 | if not prefix or prefix in seen: |
| 282 | continue |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 283 | seen.add(prefix) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 284 | |
| 285 | if sys.platform in ('os2emx', 'riscos'): |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 286 | sitepackages.append(os.path.join(prefix, "Lib", "site-packages")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 287 | elif os.sep == '/': |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 288 | sitepackages.append(os.path.join(prefix, "lib", |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 289 | "python" + sys.version[:3], |
| 290 | "site-packages")) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 291 | sitepackages.append(os.path.join(prefix, "lib", "site-python")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 292 | else: |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 293 | sitepackages.append(prefix) |
| 294 | sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 295 | if sys.platform == "darwin": |
| 296 | # for framework builds *only* we add the standard Apple |
Ronald Oussoren | fa1fcd1 | 2009-03-30 23:16:10 +0000 | [diff] [blame] | 297 | # locations. |
Ronald Oussoren | 4cda46a | 2010-05-08 10:49:43 +0000 | [diff] [blame] | 298 | from sysconfig import get_config_var |
| 299 | framework = get_config_var("PYTHONFRAMEWORK") |
Ronald Oussoren | bda4672 | 2010-08-01 09:02:50 +0000 | [diff] [blame] | 300 | if framework: |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 301 | sitepackages.append( |
Ronald Oussoren | 4cda46a | 2010-05-08 10:49:43 +0000 | [diff] [blame] | 302 | os.path.join("/Library", framework, |
Ronald Oussoren | fa1fcd1 | 2009-03-30 23:16:10 +0000 | [diff] [blame] | 303 | sys.version[:3], "site-packages")) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 304 | return sitepackages |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 305 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 306 | def addsitepackages(known_paths): |
| 307 | """Add site-packages (and possibly site-python) to sys.path""" |
| 308 | for sitedir in getsitepackages(): |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 309 | if os.path.isdir(sitedir): |
| 310 | addsitedir(sitedir, known_paths) |
| 311 | |
| 312 | return known_paths |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 313 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 314 | def setBEGINLIBPATH(): |
| 315 | """The OS/2 EMX port has optional extension modules that do double duty |
| 316 | as DLLs (and must use the .DLL file extension) for other extensions. |
| 317 | The library search path needs to be amended so these will be found |
| 318 | during module import. Use BEGINLIBPATH so that these are at the start |
| 319 | of the library search path. |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 320 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 321 | """ |
Andrew MacIntyre | 2e8a6e0 | 2003-12-02 12:27:25 +0000 | [diff] [blame] | 322 | dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload") |
| 323 | libpath = os.environ['BEGINLIBPATH'].split(';') |
| 324 | if libpath[-1]: |
| 325 | libpath.append(dllpath) |
| 326 | else: |
| 327 | libpath[-1] = dllpath |
| 328 | os.environ['BEGINLIBPATH'] = ';'.join(libpath) |
| 329 | |
| 330 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 331 | def setquit(): |
Brian Curtin | fb1d3c1 | 2010-04-12 23:33:42 +0000 | [diff] [blame] | 332 | """Define new builtins 'quit' and 'exit'. |
| 333 | |
| 334 | These are objects which make the interpreter exit when called. |
| 335 | 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] | 336 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 337 | """ |
| 338 | if os.sep == ':': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 339 | eof = 'Cmd-Q' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 340 | elif os.sep == '\\': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 341 | eof = 'Ctrl-Z plus Return' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 342 | else: |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 343 | eof = 'Ctrl-D (i.e. EOF)' |
Tim Peters | 88ca467 | 2006-03-10 23:39:56 +0000 | [diff] [blame] | 344 | |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 345 | class Quitter(object): |
| 346 | def __init__(self, name): |
| 347 | self.name = name |
| 348 | def __repr__(self): |
| 349 | return 'Use %s() or %s to exit' % (self.name, eof) |
| 350 | def __call__(self, code=None): |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 351 | # Shells like IDLE catch the SystemExit, but listen when their |
| 352 | # stdin wrapper is closed. |
| 353 | try: |
Christian Heimes | 862543a | 2007-12-31 03:07:24 +0000 | [diff] [blame] | 354 | fd = -1 |
| 355 | if hasattr(sys.stdin, "fileno"): |
| 356 | fd = sys.stdin.fileno() |
| 357 | if fd != 0: |
| 358 | # Don't close stdin if it wraps fd 0 |
| 359 | sys.stdin.close() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 360 | except: |
| 361 | pass |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 362 | raise SystemExit(code) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 363 | builtins.quit = Quitter('quit') |
| 364 | builtins.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: |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 388 | fp = open(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): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 415 | print(self.__lines[i]) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 416 | except IndexError: |
| 417 | break |
| 418 | else: |
| 419 | lineno += self.MAXLINES |
| 420 | key = None |
| 421 | while key is None: |
Guido van Rossum | 704b34d | 2007-12-20 18:42:56 +0000 | [diff] [blame] | 422 | key = input(prompt) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 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(): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 429 | """Set 'copyright' and 'credits' in builtins""" |
| 430 | builtins.copyright = _Printer("copyright", sys.copyright) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 431 | if sys.platform[:4] == 'java': |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 432 | builtins.credits = _Printer( |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 433 | "credits", |
| 434 | "Jython is maintained by the Jython developers (www.jython.org).") |
| 435 | else: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 436 | builtins.credits = _Printer("credits", """\ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 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__) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 440 | builtins.license = _Printer( |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 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 | fb1d3c1 | 2010-04-12 23:33:42 +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(): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 460 | builtins.help = _Helper() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 477 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 478 | def execsitecustomize(): |
| 479 | """Run custom site specific code, if available.""" |
| 480 | try: |
| 481 | import sitecustomize |
| 482 | except ImportError: |
| 483 | pass |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 484 | except Exception as err: |
Guido van Rossum | c6fe983 | 2006-08-19 00:10:28 +0000 | [diff] [blame] | 485 | if os.environ.get("PYTHONVERBOSE"): |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 486 | sys.excepthook(*sys.exc_info()) |
| 487 | else: |
| 488 | sys.stderr.write( |
| 489 | "Error in sitecustomize; set PYTHONVERBOSE for traceback:\n" |
| 490 | "%s: %s\n" % |
| 491 | (err.__class__.__name__, err)) |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 492 | |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 493 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 494 | def execusercustomize(): |
| 495 | """Run custom user specific code, if available.""" |
| 496 | try: |
| 497 | import usercustomize |
| 498 | except ImportError: |
| 499 | pass |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 500 | except Exception as err: |
| 501 | if os.environ.get("PYTHONVERBOSE"): |
| 502 | sys.excepthook(*sys.exc_info()) |
| 503 | else: |
| 504 | sys.stderr.write( |
| 505 | "Error in usercustomize; set PYTHONVERBOSE for traceback:\n" |
| 506 | "%s: %s\n" % |
| 507 | (err.__class__.__name__, err)) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 508 | |
| 509 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 510 | def main(): |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 511 | """Add standard site-specific directories to the module search path. |
| 512 | |
| 513 | This function is called automatically when this module is imported, |
| 514 | unless the python interpreter was started with the -S flag. |
| 515 | """ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 516 | global ENABLE_USER_SITE |
| 517 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 518 | abs_paths() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 519 | known_paths = removeduppaths() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 520 | if ENABLE_USER_SITE is None: |
| 521 | ENABLE_USER_SITE = check_enableusersite() |
| 522 | known_paths = addusersitepackages(known_paths) |
| 523 | known_paths = addsitepackages(known_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 524 | if sys.platform == 'os2emx': |
| 525 | setBEGINLIBPATH() |
| 526 | setquit() |
| 527 | setcopyright() |
| 528 | sethelper() |
| 529 | aliasmbcs() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 530 | execsitecustomize() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 531 | if ENABLE_USER_SITE: |
| 532 | execusercustomize() |
Marc-André Lemburg | 990bbe9 | 2000-06-07 09:12:09 +0000 | [diff] [blame] | 533 | |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 534 | # Prevent edition of sys.path when python was started with -S and |
| 535 | # site is imported later. |
| 536 | if not sys.flags.no_site: |
| 537 | main() |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 538 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 539 | def _script(): |
| 540 | help = """\ |
| 541 | %s [--user-base] [--user-site] |
| 542 | |
| 543 | Without arguments print some useful information |
| 544 | With arguments print the value of USER_BASE and/or USER_SITE separated |
| 545 | by '%s'. |
| 546 | |
| 547 | Exit codes with --user-base or --user-site: |
| 548 | 0 - user site directory is enabled |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 549 | 1 - user site directory is disabled by user |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 550 | 2 - uses site directory is disabled by super user |
| 551 | or for security reasons |
| 552 | >2 - unknown error |
| 553 | """ |
| 554 | args = sys.argv[1:] |
| 555 | if not args: |
| 556 | print("sys.path = [") |
| 557 | for dir in sys.path: |
| 558 | print(" %r," % (dir,)) |
| 559 | print("]") |
| 560 | print("USER_BASE: %r (%s)" % (USER_BASE, |
| 561 | "exists" if os.path.isdir(USER_BASE) else "doesn't exist")) |
| 562 | print("USER_SITE: %r (%s)" % (USER_SITE, |
| 563 | "exists" if os.path.isdir(USER_SITE) else "doesn't exist")) |
| 564 | print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE) |
| 565 | sys.exit(0) |
| 566 | |
| 567 | buffer = [] |
| 568 | if '--user-base' in args: |
| 569 | buffer.append(USER_BASE) |
| 570 | if '--user-site' in args: |
| 571 | buffer.append(USER_SITE) |
| 572 | |
| 573 | if buffer: |
| 574 | print(os.pathsep.join(buffer)) |
| 575 | if ENABLE_USER_SITE: |
| 576 | sys.exit(0) |
| 577 | elif ENABLE_USER_SITE is False: |
| 578 | sys.exit(1) |
| 579 | elif ENABLE_USER_SITE is None: |
| 580 | sys.exit(2) |
| 581 | else: |
| 582 | sys.exit(3) |
| 583 | else: |
| 584 | import textwrap |
| 585 | print(textwrap.dedent(help % (sys.argv[0], os.pathsep))) |
| 586 | sys.exit(10) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 587 | |
| 588 | if __name__ == '__main__': |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 589 | _script() |