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