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