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 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 16 | If a file named "pyvenv.cfg" exists one directory above sys.executable, |
| 17 | sys.prefix and sys.exec_prefix are set to that directory and |
Vinay Sajip | abd344c | 2012-07-03 16:33:57 +0100 | [diff] [blame] | 18 | it is also checked for site-packages and site-python (sys.base_prefix and |
| 19 | sys.base_exec_prefix will always be the "real" prefixes of the Python |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 20 | installation). If "pyvenv.cfg" (a bootstrap configuration file) contains |
| 21 | the key "include-system-site-packages" set to anything other than "false" |
| 22 | (case-insensitive), the system-level prefixes will still also be |
| 23 | searched for site-packages; otherwise they won't. |
| 24 | |
| 25 | All of the resulting site-specific directories, if they exist, are |
| 26 | appended to sys.path, and also inspected for path configuration |
| 27 | files. |
| 28 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 29 | A path configuration file is a file whose name has the form |
| 30 | <package>.pth; its contents are additional directories (one per line) |
| 31 | to be added to sys.path. Non-existing directories (or |
| 32 | non-directories) are never added to sys.path; no directory is added to |
| 33 | sys.path more than once. Blank lines and lines beginning with |
Guido van Rossum | facf24b | 2001-12-17 16:07:06 +0000 | [diff] [blame] | 34 | '#' are skipped. Lines starting with 'import' are executed. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 36 | For example, suppose sys.prefix and sys.exec_prefix are set to |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 37 | /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] | 38 | with three subdirectories, foo, bar and spam, and two path |
| 39 | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
| 40 | following: |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 41 | |
| 42 | # foo package configuration |
| 43 | foo |
| 44 | bar |
| 45 | bletch |
| 46 | |
| 47 | and bar.pth contains: |
| 48 | |
| 49 | # bar package configuration |
| 50 | bar |
| 51 | |
| 52 | Then the following directories are added to sys.path, in this order: |
| 53 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 54 | /usr/local/lib/python2.5/site-packages/bar |
| 55 | /usr/local/lib/python2.5/site-packages/foo |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 56 | |
| 57 | Note that bletch is omitted because it doesn't exist; bar precedes foo |
| 58 | because bar.pth comes alphabetically before foo.pth; and spam is |
| 59 | omitted because it is not mentioned in either path configuration file. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame^] | 61 | The readline module is also automatically configured to enable |
| 62 | completion for systems that support it. This can be overriden in |
| 63 | sitecustomize, usercustomize or PYTHONSTARTUP. |
| 64 | |
| 65 | After these operations, an attempt is made to import a module |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 66 | named sitecustomize, which can perform arbitrary additional |
| 67 | site-specific customizations. If this import fails with an |
| 68 | ImportError exception, it is silently ignored. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 69 | """ |
| 70 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 71 | import sys |
| 72 | import os |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 73 | import re |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 74 | import builtins |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 75 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 76 | # Prefixes for site-packages; add additional prefixes like /usr/local here |
| 77 | PREFIXES = [sys.prefix, sys.exec_prefix] |
| 78 | # Enable per user site-packages directory |
| 79 | # set it to False to disable the feature or True to force the feature |
| 80 | ENABLE_USER_SITE = None |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 81 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 82 | # for distutils.commands.install |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 83 | # These values are initialized by the getuserbase() and getusersitepackages() |
| 84 | # functions, through the main() function when Python starts. |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 85 | USER_SITE = None |
| 86 | USER_BASE = None |
| 87 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 88 | |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 89 | def makepath(*paths): |
Victor Stinner | b103a93 | 2010-10-12 22:23:23 +0000 | [diff] [blame] | 90 | dir = os.path.join(*paths) |
| 91 | try: |
| 92 | dir = os.path.abspath(dir) |
| 93 | except OSError: |
| 94 | pass |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 95 | return dir, os.path.normcase(dir) |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 96 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 97 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 98 | def abs_paths(): |
| 99 | """Set all module __file__ and __cached__ attributes to an absolute path""" |
Guido van Rossum | 7ac9d40 | 2007-05-18 00:24:43 +0000 | [diff] [blame] | 100 | for m in set(sys.modules.values()): |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 101 | if (getattr(getattr(m, '__loader__', None), '__module__', None) != |
| 102 | '_frozen_importlib'): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 103 | continue # don't mess with a PEP 302-supplied __file__ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 104 | try: |
| 105 | m.__file__ = os.path.abspath(m.__file__) |
Victor Stinner | b103a93 | 2010-10-12 22:23:23 +0000 | [diff] [blame] | 106 | except (AttributeError, OSError): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 107 | pass |
| 108 | try: |
| 109 | m.__cached__ = os.path.abspath(m.__cached__) |
Victor Stinner | b103a93 | 2010-10-12 22:23:23 +0000 | [diff] [blame] | 110 | except (AttributeError, OSError): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 111 | pass |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 112 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 113 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 114 | def removeduppaths(): |
| 115 | """ Remove duplicate entries from sys.path along with making them |
| 116 | absolute""" |
| 117 | # This ensures that the initial path provided by the interpreter contains |
| 118 | # only absolute pathnames, even if we're running from the build directory. |
| 119 | L = [] |
| 120 | known_paths = set() |
| 121 | for dir in sys.path: |
| 122 | # Filter out duplicate paths (on case-insensitive file systems also |
| 123 | # if they only differ in case); turn relative paths into absolute |
| 124 | # paths. |
| 125 | dir, dircase = makepath(dir) |
| 126 | if not dircase in known_paths: |
| 127 | L.append(dir) |
| 128 | known_paths.add(dircase) |
| 129 | sys.path[:] = L |
| 130 | return known_paths |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 131 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 132 | |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 133 | def _init_pathinfo(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 134 | """Return a set containing all existing directory entries from sys.path""" |
| 135 | d = set() |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 136 | for dir in sys.path: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 137 | try: |
| 138 | if os.path.isdir(dir): |
| 139 | dir, dircase = makepath(dir) |
| 140 | d.add(dircase) |
| 141 | except TypeError: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 142 | continue |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 143 | return d |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 144 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 145 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 146 | def addpackage(sitedir, name, known_paths): |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 147 | """Process a .pth file within the site-packages directory: |
| 148 | For each line in the file, either combine it with sitedir to a path |
| 149 | and add that to known_paths, or execute it if it starts with 'import '. |
| 150 | """ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 151 | if known_paths is None: |
Brett Cannon | 13252b8 | 2013-01-25 13:57:16 -0500 | [diff] [blame] | 152 | known_paths = _init_pathinfo() |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 153 | reset = 1 |
| 154 | else: |
| 155 | reset = 0 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 156 | fullname = os.path.join(sitedir, name) |
| 157 | try: |
Victor Stinner | 4e86d5b | 2011-05-04 13:55:36 +0200 | [diff] [blame] | 158 | f = open(fullname, "r") |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 159 | except OSError: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 160 | return |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 161 | with f: |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 162 | for n, line in enumerate(f): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 163 | if line.startswith("#"): |
| 164 | continue |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 165 | try: |
| 166 | if line.startswith(("import ", "import\t")): |
| 167 | exec(line) |
| 168 | continue |
| 169 | line = line.rstrip() |
| 170 | dir, dircase = makepath(sitedir, line) |
| 171 | if not dircase in known_paths and os.path.exists(dir): |
| 172 | sys.path.append(dir) |
| 173 | known_paths.add(dircase) |
Florent Xicluna | 54540ec | 2011-11-04 08:29:17 +0100 | [diff] [blame] | 174 | except Exception: |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 175 | print("Error processing line {:d} of {}:\n".format(n+1, fullname), |
| 176 | file=sys.stderr) |
Victor Stinner | 6553211 | 2012-02-21 22:10:16 +0100 | [diff] [blame] | 177 | import traceback |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 178 | for record in traceback.format_exception(*sys.exc_info()): |
| 179 | for line in record.splitlines(): |
| 180 | print(' '+line, file=sys.stderr) |
| 181 | print("\nRemainder of file ignored", file=sys.stderr) |
| 182 | break |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 183 | if reset: |
| 184 | known_paths = None |
| 185 | return known_paths |
| 186 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 187 | |
Brett Cannon | 12f8c4d | 2004-07-09 23:38:18 +0000 | [diff] [blame] | 188 | def addsitedir(sitedir, known_paths=None): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 189 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
| 190 | 'sitedir'""" |
| 191 | if known_paths is None: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 192 | known_paths = _init_pathinfo() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 193 | reset = 1 |
| 194 | else: |
| 195 | reset = 0 |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 196 | sitedir, sitedircase = makepath(sitedir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 197 | if not sitedircase in known_paths: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 198 | sys.path.append(sitedir) # Add path component |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 199 | known_paths.add(sitedircase) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 200 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | names = os.listdir(sitedir) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 202 | except OSError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 203 | return |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 204 | names = [name for name in names if name.endswith(".pth")] |
| 205 | for name in sorted(names): |
| 206 | addpackage(sitedir, name, known_paths) |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 207 | if reset: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 208 | known_paths = None |
| 209 | return known_paths |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 210 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 211 | |
| 212 | def check_enableusersite(): |
| 213 | """Check if user site directory is safe for inclusion |
| 214 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 215 | The function tests for the command line flag (including environment var), |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 216 | process uid/gid equal to effective uid/gid. |
| 217 | |
| 218 | None: Disabled for security reasons |
| 219 | False: Disabled by user (command line option) |
| 220 | True: Safe and enabled |
| 221 | """ |
| 222 | if sys.flags.no_user_site: |
| 223 | return False |
| 224 | |
| 225 | if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
| 226 | # check process uid == effective uid |
| 227 | if os.geteuid() != os.getuid(): |
| 228 | return None |
| 229 | if hasattr(os, "getgid") and hasattr(os, "getegid"): |
| 230 | # check process gid == effective gid |
| 231 | if os.getegid() != os.getgid(): |
| 232 | return None |
| 233 | |
| 234 | return True |
| 235 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 236 | def getuserbase(): |
| 237 | """Returns the `user base` directory path. |
| 238 | |
| 239 | The `user base` directory can be used to store data. If the global |
| 240 | variable ``USER_BASE`` is not initialized yet, this function will also set |
| 241 | it. |
| 242 | """ |
| 243 | global USER_BASE |
| 244 | if USER_BASE is not None: |
| 245 | return USER_BASE |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 246 | from sysconfig import get_config_var |
| 247 | USER_BASE = get_config_var('userbase') |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 248 | return USER_BASE |
| 249 | |
| 250 | def getusersitepackages(): |
| 251 | """Returns the user-specific site-packages directory path. |
| 252 | |
| 253 | If the global variable ``USER_SITE`` is not initialized yet, this |
| 254 | function will also set it. |
| 255 | """ |
| 256 | global USER_SITE |
| 257 | user_base = getuserbase() # this will also set USER_BASE |
| 258 | |
| 259 | if USER_SITE is not None: |
| 260 | return USER_SITE |
| 261 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 262 | from sysconfig import get_path |
Ronald Oussoren | 4cda46a | 2010-05-08 10:49:43 +0000 | [diff] [blame] | 263 | |
| 264 | if sys.platform == 'darwin': |
| 265 | from sysconfig import get_config_var |
| 266 | if get_config_var('PYTHONFRAMEWORK'): |
| 267 | USER_SITE = get_path('purelib', 'osx_framework_user') |
| 268 | return USER_SITE |
| 269 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 270 | USER_SITE = get_path('purelib', '%s_user' % os.name) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 271 | return USER_SITE |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 272 | |
| 273 | def addusersitepackages(known_paths): |
| 274 | """Add a per user site-package to sys.path |
| 275 | |
| 276 | Each user has its own python directory with site-packages in the |
| 277 | home directory. |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 278 | """ |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 279 | # get the per user site-package path |
| 280 | # this call will also make sure USER_BASE and USER_SITE are set |
| 281 | user_site = getusersitepackages() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 282 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 283 | if ENABLE_USER_SITE and os.path.isdir(user_site): |
| 284 | addsitedir(user_site, known_paths) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 285 | return known_paths |
| 286 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 287 | def getsitepackages(prefixes=None): |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 288 | """Returns a list containing all global site-packages directories |
| 289 | (and possibly site-python). |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 290 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 291 | For each directory present in ``prefixes`` (or the global ``PREFIXES``), |
| 292 | this function will find its `site-packages` subdirectory depending on the |
| 293 | system environment, and will return a list of full paths. |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 294 | """ |
| 295 | sitepackages = [] |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 296 | seen = set() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 297 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 298 | if prefixes is None: |
| 299 | prefixes = PREFIXES |
| 300 | |
| 301 | for prefix in prefixes: |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 302 | if not prefix or prefix in seen: |
| 303 | continue |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 304 | seen.add(prefix) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 305 | |
Christian Heimes | de0b962 | 2012-11-19 00:59:39 +0100 | [diff] [blame] | 306 | if os.sep == '/': |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 307 | sitepackages.append(os.path.join(prefix, "lib", |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 308 | "python" + sys.version[:3], |
| 309 | "site-packages")) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 310 | sitepackages.append(os.path.join(prefix, "lib", "site-python")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 311 | else: |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 312 | sitepackages.append(prefix) |
| 313 | sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 314 | if sys.platform == "darwin": |
| 315 | # for framework builds *only* we add the standard Apple |
Ronald Oussoren | fa1fcd1 | 2009-03-30 23:16:10 +0000 | [diff] [blame] | 316 | # locations. |
Ronald Oussoren | 4cda46a | 2010-05-08 10:49:43 +0000 | [diff] [blame] | 317 | from sysconfig import get_config_var |
| 318 | framework = get_config_var("PYTHONFRAMEWORK") |
Ronald Oussoren | bda4672 | 2010-08-01 09:02:50 +0000 | [diff] [blame] | 319 | if framework: |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 320 | sitepackages.append( |
Ronald Oussoren | 4cda46a | 2010-05-08 10:49:43 +0000 | [diff] [blame] | 321 | os.path.join("/Library", framework, |
Ronald Oussoren | fa1fcd1 | 2009-03-30 23:16:10 +0000 | [diff] [blame] | 322 | sys.version[:3], "site-packages")) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 323 | return sitepackages |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 324 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 325 | def addsitepackages(known_paths, prefixes=None): |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 326 | """Add site-packages (and possibly site-python) to sys.path""" |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 327 | for sitedir in getsitepackages(prefixes): |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 328 | if os.path.isdir(sitedir): |
| 329 | addsitedir(sitedir, known_paths) |
| 330 | |
| 331 | return known_paths |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 332 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 333 | def setquit(): |
Brian Curtin | fb1d3c1 | 2010-04-12 23:33:42 +0000 | [diff] [blame] | 334 | """Define new builtins 'quit' and 'exit'. |
| 335 | |
| 336 | These are objects which make the interpreter exit when called. |
| 337 | 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] | 338 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 339 | """ |
| 340 | if os.sep == ':': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 341 | eof = 'Cmd-Q' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 342 | elif os.sep == '\\': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 343 | eof = 'Ctrl-Z plus Return' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 344 | else: |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 345 | eof = 'Ctrl-D (i.e. EOF)' |
Tim Peters | 88ca467 | 2006-03-10 23:39:56 +0000 | [diff] [blame] | 346 | |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 347 | class Quitter(object): |
| 348 | def __init__(self, name): |
| 349 | self.name = name |
| 350 | def __repr__(self): |
| 351 | return 'Use %s() or %s to exit' % (self.name, eof) |
| 352 | def __call__(self, code=None): |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 353 | # Shells like IDLE catch the SystemExit, but listen when their |
| 354 | # stdin wrapper is closed. |
| 355 | try: |
Roger Serwy | 1eafd10 | 2013-04-11 19:16:44 -0500 | [diff] [blame] | 356 | sys.stdin.close() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 357 | except: |
| 358 | pass |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 359 | raise SystemExit(code) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 360 | builtins.quit = Quitter('quit') |
| 361 | builtins.exit = Quitter('exit') |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 362 | |
| 363 | |
| 364 | class _Printer(object): |
| 365 | """interactive prompt objects for printing the license text, a list of |
| 366 | contributors and the copyright notice.""" |
| 367 | |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 368 | MAXLINES = 23 |
| 369 | |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 370 | def __init__(self, name, data, files=(), dirs=()): |
| 371 | self.__name = name |
| 372 | self.__data = data |
| 373 | self.__files = files |
| 374 | self.__dirs = dirs |
| 375 | self.__lines = None |
| 376 | |
| 377 | def __setup(self): |
| 378 | if self.__lines: |
| 379 | return |
| 380 | data = None |
| 381 | for dir in self.__dirs: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 382 | for filename in self.__files: |
| 383 | filename = os.path.join(dir, filename) |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 384 | try: |
Giampaolo Rodola' | 2f50aaf | 2013-02-12 02:04:27 +0100 | [diff] [blame] | 385 | with open(filename, "r") as fp: |
| 386 | data = fp.read() |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 387 | break |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 388 | except OSError: |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 389 | pass |
| 390 | if data: |
| 391 | break |
| 392 | if not data: |
| 393 | data = self.__data |
| 394 | self.__lines = data.split('\n') |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 395 | self.__linecnt = len(self.__lines) |
| 396 | |
| 397 | def __repr__(self): |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 398 | self.__setup() |
| 399 | if len(self.__lines) <= self.MAXLINES: |
| 400 | return "\n".join(self.__lines) |
| 401 | else: |
| 402 | return "Type %s() to see the full %s text" % ((self.__name,)*2) |
| 403 | |
| 404 | def __call__(self): |
| 405 | self.__setup() |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 406 | prompt = 'Hit Return for more, or q (and Return) to quit: ' |
| 407 | lineno = 0 |
| 408 | while 1: |
| 409 | try: |
| 410 | for i in range(lineno, lineno + self.MAXLINES): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 411 | print(self.__lines[i]) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 412 | except IndexError: |
| 413 | break |
| 414 | else: |
| 415 | lineno += self.MAXLINES |
| 416 | key = None |
| 417 | while key is None: |
Guido van Rossum | 704b34d | 2007-12-20 18:42:56 +0000 | [diff] [blame] | 418 | key = input(prompt) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 419 | if key not in ('', 'q'): |
| 420 | key = None |
| 421 | if key == 'q': |
| 422 | break |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 423 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 424 | def setcopyright(): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 425 | """Set 'copyright' and 'credits' in builtins""" |
| 426 | builtins.copyright = _Printer("copyright", sys.copyright) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 427 | if sys.platform[:4] == 'java': |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 428 | builtins.credits = _Printer( |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 429 | "credits", |
| 430 | "Jython is maintained by the Jython developers (www.jython.org).") |
| 431 | else: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 432 | builtins.credits = _Printer("credits", """\ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 433 | Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
| 434 | for supporting Python development. See www.python.org for more information.""") |
| 435 | here = os.path.dirname(os.__file__) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 436 | builtins.license = _Printer( |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 437 | "license", "See http://www.python.org/%.3s/license.html" % sys.version, |
| 438 | ["LICENSE.txt", "LICENSE"], |
| 439 | [os.path.join(here, os.pardir), here, os.curdir]) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 440 | |
| 441 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 442 | class _Helper(object): |
Brian Curtin | fb1d3c1 | 2010-04-12 23:33:42 +0000 | [diff] [blame] | 443 | """Define the builtin 'help'. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 444 | This is a wrapper around pydoc.help (with a twist). |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 445 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 446 | """ |
| 447 | |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 448 | def __repr__(self): |
| 449 | return "Type help() for interactive help, " \ |
| 450 | "or help(object) for help about object." |
| 451 | def __call__(self, *args, **kwds): |
| 452 | import pydoc |
| 453 | return pydoc.help(*args, **kwds) |
| 454 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 455 | def sethelper(): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 456 | builtins.help = _Helper() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 457 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame^] | 458 | def enablerlcompleter(): |
| 459 | """Enable default readline configuration on interactive prompts, by |
| 460 | registering a sys.__interactivehook__. |
| 461 | |
| 462 | If the readline module can be imported, the hook will set the Tab key |
| 463 | as completion key and register ~/.python_history as history file. |
| 464 | This can be overriden in the sitecustomize or usercustomize module, |
| 465 | or in a PYTHONSTARTUP file. |
| 466 | """ |
| 467 | def register_readline(): |
| 468 | import atexit |
| 469 | try: |
| 470 | import readline |
| 471 | import rlcompleter |
| 472 | except ImportError: |
| 473 | return |
| 474 | |
| 475 | # Reading the initialization (config) file may not be enough to set a |
| 476 | # completion key, so we set one first and then read the file |
| 477 | if 'libedit' in getattr(readline, '__doc__', ''): |
| 478 | readline.parse_and_bind('bind ^I rl_complete') |
| 479 | else: |
| 480 | readline.parse_and_bind('tab: complete') |
| 481 | readline.read_init_file() |
| 482 | |
| 483 | history = os.path.join(os.path.expanduser('~'), '.python_history') |
| 484 | try: |
| 485 | readline.read_history_file(history) |
| 486 | except IOError: |
| 487 | pass |
| 488 | atexit.register(readline.write_history_file, history) |
| 489 | |
| 490 | sys.__interactivehook__ = register_readline |
| 491 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 492 | def aliasmbcs(): |
| 493 | """On Windows, some default encodings are not provided by Python, |
| 494 | while they are always available as "mbcs" in each locale. Make |
| 495 | them usable by aliasing to "mbcs" in such a case.""" |
| 496 | if sys.platform == 'win32': |
| 497 | import locale, codecs |
| 498 | enc = locale.getdefaultlocale()[1] |
| 499 | if enc.startswith('cp'): # "cp***" ? |
| 500 | try: |
| 501 | codecs.lookup(enc) |
| 502 | except LookupError: |
| 503 | import encodings |
| 504 | encodings._cache[enc] = encodings._unknown |
| 505 | encodings.aliases.aliases[enc] = 'mbcs' |
| 506 | |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 507 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 508 | CONFIG_LINE = re.compile(r'^(?P<key>(\w|[-_])+)\s*=\s*(?P<value>.*)\s*$') |
| 509 | |
| 510 | def venv(known_paths): |
| 511 | global PREFIXES, ENABLE_USER_SITE |
| 512 | |
| 513 | env = os.environ |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 514 | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env: |
| 515 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 516 | else: |
| 517 | executable = sys.executable |
Vinay Sajip | 27e4b60 | 2012-11-23 19:16:49 +0000 | [diff] [blame] | 518 | exe_dir, _ = os.path.split(os.path.abspath(executable)) |
| 519 | site_prefix = os.path.dirname(exe_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 520 | sys._home = None |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 521 | conf_basename = 'pyvenv.cfg' |
| 522 | candidate_confs = [ |
| 523 | conffile for conffile in ( |
Vinay Sajip | 27e4b60 | 2012-11-23 19:16:49 +0000 | [diff] [blame] | 524 | os.path.join(exe_dir, conf_basename), |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 525 | os.path.join(site_prefix, conf_basename) |
| 526 | ) |
| 527 | if os.path.isfile(conffile) |
| 528 | ] |
| 529 | |
| 530 | if candidate_confs: |
| 531 | virtual_conf = candidate_confs[0] |
| 532 | system_site = "true" |
| 533 | with open(virtual_conf) as f: |
| 534 | for line in f: |
| 535 | line = line.strip() |
| 536 | m = CONFIG_LINE.match(line) |
| 537 | if m: |
| 538 | d = m.groupdict() |
| 539 | key, value = d['key'].lower(), d['value'] |
| 540 | if key == 'include-system-site-packages': |
| 541 | system_site = value.lower() |
| 542 | elif key == 'home': |
| 543 | sys._home = value |
| 544 | |
| 545 | sys.prefix = sys.exec_prefix = site_prefix |
| 546 | |
| 547 | # Doing this here ensures venv takes precedence over user-site |
| 548 | addsitepackages(known_paths, [sys.prefix]) |
| 549 | |
| 550 | # addsitepackages will process site_prefix again if its in PREFIXES, |
| 551 | # but that's ok; known_paths will prevent anything being added twice |
| 552 | if system_site == "true": |
| 553 | PREFIXES.insert(0, sys.prefix) |
| 554 | else: |
| 555 | PREFIXES = [sys.prefix] |
| 556 | ENABLE_USER_SITE = False |
| 557 | |
| 558 | return known_paths |
| 559 | |
| 560 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 561 | def execsitecustomize(): |
| 562 | """Run custom site specific code, if available.""" |
| 563 | try: |
| 564 | import sitecustomize |
| 565 | except ImportError: |
| 566 | pass |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 567 | except Exception as err: |
Guido van Rossum | c6fe983 | 2006-08-19 00:10:28 +0000 | [diff] [blame] | 568 | if os.environ.get("PYTHONVERBOSE"): |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 569 | sys.excepthook(*sys.exc_info()) |
| 570 | else: |
| 571 | sys.stderr.write( |
| 572 | "Error in sitecustomize; set PYTHONVERBOSE for traceback:\n" |
| 573 | "%s: %s\n" % |
| 574 | (err.__class__.__name__, err)) |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 575 | |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 576 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 577 | def execusercustomize(): |
| 578 | """Run custom user specific code, if available.""" |
| 579 | try: |
| 580 | import usercustomize |
| 581 | except ImportError: |
| 582 | pass |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 583 | except Exception as err: |
| 584 | if os.environ.get("PYTHONVERBOSE"): |
| 585 | sys.excepthook(*sys.exc_info()) |
| 586 | else: |
| 587 | sys.stderr.write( |
| 588 | "Error in usercustomize; set PYTHONVERBOSE for traceback:\n" |
| 589 | "%s: %s\n" % |
| 590 | (err.__class__.__name__, err)) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 591 | |
| 592 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 593 | def main(): |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 594 | """Add standard site-specific directories to the module search path. |
| 595 | |
| 596 | This function is called automatically when this module is imported, |
| 597 | unless the python interpreter was started with the -S flag. |
| 598 | """ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 599 | global ENABLE_USER_SITE |
| 600 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 601 | abs_paths() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 602 | known_paths = removeduppaths() |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 603 | known_paths = venv(known_paths) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 604 | if ENABLE_USER_SITE is None: |
| 605 | ENABLE_USER_SITE = check_enableusersite() |
| 606 | known_paths = addusersitepackages(known_paths) |
| 607 | known_paths = addsitepackages(known_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 608 | setquit() |
| 609 | setcopyright() |
| 610 | sethelper() |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame^] | 611 | enablerlcompleter() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 612 | aliasmbcs() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 613 | execsitecustomize() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 614 | if ENABLE_USER_SITE: |
| 615 | execusercustomize() |
Marc-André Lemburg | 990bbe9 | 2000-06-07 09:12:09 +0000 | [diff] [blame] | 616 | |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 617 | # Prevent edition of sys.path when python was started with -S and |
| 618 | # site is imported later. |
| 619 | if not sys.flags.no_site: |
| 620 | main() |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 621 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 622 | def _script(): |
| 623 | help = """\ |
| 624 | %s [--user-base] [--user-site] |
| 625 | |
| 626 | Without arguments print some useful information |
| 627 | With arguments print the value of USER_BASE and/or USER_SITE separated |
| 628 | by '%s'. |
| 629 | |
| 630 | Exit codes with --user-base or --user-site: |
| 631 | 0 - user site directory is enabled |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 632 | 1 - user site directory is disabled by user |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 633 | 2 - uses site directory is disabled by super user |
| 634 | or for security reasons |
| 635 | >2 - unknown error |
| 636 | """ |
| 637 | args = sys.argv[1:] |
| 638 | if not args: |
Meador Inge | 9a7a811 | 2013-04-13 20:29:49 -0500 | [diff] [blame] | 639 | user_base = getuserbase() |
| 640 | user_site = getusersitepackages() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 641 | print("sys.path = [") |
| 642 | for dir in sys.path: |
| 643 | print(" %r," % (dir,)) |
| 644 | print("]") |
Meador Inge | 9a7a811 | 2013-04-13 20:29:49 -0500 | [diff] [blame] | 645 | print("USER_BASE: %r (%s)" % (user_base, |
| 646 | "exists" if os.path.isdir(user_base) else "doesn't exist")) |
| 647 | print("USER_SITE: %r (%s)" % (user_site, |
| 648 | "exists" if os.path.isdir(user_site) else "doesn't exist")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 649 | print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE) |
| 650 | sys.exit(0) |
| 651 | |
| 652 | buffer = [] |
| 653 | if '--user-base' in args: |
| 654 | buffer.append(USER_BASE) |
| 655 | if '--user-site' in args: |
| 656 | buffer.append(USER_SITE) |
| 657 | |
| 658 | if buffer: |
| 659 | print(os.pathsep.join(buffer)) |
| 660 | if ENABLE_USER_SITE: |
| 661 | sys.exit(0) |
| 662 | elif ENABLE_USER_SITE is False: |
| 663 | sys.exit(1) |
| 664 | elif ENABLE_USER_SITE is None: |
| 665 | sys.exit(2) |
| 666 | else: |
| 667 | sys.exit(3) |
| 668 | else: |
| 669 | import textwrap |
| 670 | print(textwrap.dedent(help % (sys.argv[0], os.pathsep))) |
| 671 | sys.exit(10) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 672 | |
| 673 | if __name__ == '__main__': |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 674 | _script() |