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 |
Antoine Pitrou | 9e82b17 | 2014-06-12 19:41:30 -0400 | [diff] [blame] | 10 | lib/python<version>/site-packages. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 |
Antoine Pitrou | 9e82b17 | 2014-06-12 19:41:30 -0400 | [diff] [blame] | 18 | it is also checked for site-packages (sys.base_prefix and |
Vinay Sajip | abd344c | 2012-07-03 16:33:57 +0100 | [diff] [blame] | 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 |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 62 | completion for systems that support it. This can be overridden in |
Steve Dower | 313523c | 2016-09-17 12:22:41 -0700 | [diff] [blame] | 63 | sitecustomize, usercustomize or PYTHONSTARTUP. Starting Python in |
| 64 | isolated mode (-I) disables automatic readline configuration. |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 65 | |
| 66 | After these operations, an attempt is made to import a module |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 67 | named sitecustomize, which can perform arbitrary additional |
| 68 | site-specific customizations. If this import fails with an |
| 69 | ImportError exception, it is silently ignored. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 70 | """ |
| 71 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 72 | import sys |
| 73 | import os |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 74 | import builtins |
Antoine Pitrou | 853395b | 2013-08-06 22:56:40 +0200 | [diff] [blame] | 75 | import _sitebuiltins |
Miss Islington (bot) | 35202c7 | 2019-06-21 15:35:44 -0700 | [diff] [blame^] | 76 | import io |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 77 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 78 | # Prefixes for site-packages; add additional prefixes like /usr/local here |
| 79 | PREFIXES = [sys.prefix, sys.exec_prefix] |
| 80 | # Enable per user site-packages directory |
| 81 | # set it to False to disable the feature or True to force the feature |
| 82 | ENABLE_USER_SITE = None |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 83 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 84 | # for distutils.commands.install |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 85 | # These values are initialized by the getuserbase() and getusersitepackages() |
| 86 | # functions, through the main() function when Python starts. |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 87 | USER_SITE = None |
| 88 | USER_BASE = None |
| 89 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 90 | |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 91 | def makepath(*paths): |
Victor Stinner | b103a93 | 2010-10-12 22:23:23 +0000 | [diff] [blame] | 92 | dir = os.path.join(*paths) |
| 93 | try: |
| 94 | dir = os.path.abspath(dir) |
| 95 | except OSError: |
| 96 | pass |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 97 | return dir, os.path.normcase(dir) |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 98 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 99 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 100 | def abs_paths(): |
| 101 | """Set all module __file__ and __cached__ attributes to an absolute path""" |
Guido van Rossum | 7ac9d40 | 2007-05-18 00:24:43 +0000 | [diff] [blame] | 102 | for m in set(sys.modules.values()): |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 103 | if (getattr(getattr(m, '__loader__', None), '__module__', None) not in |
| 104 | ('_frozen_importlib', '_frozen_importlib_external')): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 105 | continue # don't mess with a PEP 302-supplied __file__ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 106 | try: |
| 107 | m.__file__ = os.path.abspath(m.__file__) |
Steve Weber | 2487f30 | 2018-06-10 20:49:34 -0400 | [diff] [blame] | 108 | except (AttributeError, OSError, TypeError): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 109 | pass |
| 110 | try: |
| 111 | m.__cached__ = os.path.abspath(m.__cached__) |
Steve Weber | 2487f30 | 2018-06-10 20:49:34 -0400 | [diff] [blame] | 112 | except (AttributeError, OSError, TypeError): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 113 | pass |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 114 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 115 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 116 | def removeduppaths(): |
| 117 | """ Remove duplicate entries from sys.path along with making them |
| 118 | absolute""" |
| 119 | # This ensures that the initial path provided by the interpreter contains |
| 120 | # only absolute pathnames, even if we're running from the build directory. |
| 121 | L = [] |
| 122 | known_paths = set() |
| 123 | for dir in sys.path: |
| 124 | # Filter out duplicate paths (on case-insensitive file systems also |
| 125 | # if they only differ in case); turn relative paths into absolute |
| 126 | # paths. |
| 127 | dir, dircase = makepath(dir) |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 128 | if dircase not in known_paths: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 129 | L.append(dir) |
| 130 | known_paths.add(dircase) |
| 131 | sys.path[:] = L |
| 132 | return known_paths |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 133 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 134 | |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 135 | def _init_pathinfo(): |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 136 | """Return a set containing all existing file system items from sys.path.""" |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 137 | d = set() |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 138 | for item in sys.path: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 139 | try: |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 140 | if os.path.exists(item): |
| 141 | _, itemcase = makepath(item) |
| 142 | d.add(itemcase) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 143 | except TypeError: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 144 | continue |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 145 | return d |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 146 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 147 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 148 | def addpackage(sitedir, name, known_paths): |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 149 | """Process a .pth file within the site-packages directory: |
| 150 | For each line in the file, either combine it with sitedir to a path |
| 151 | and add that to known_paths, or execute it if it starts with 'import '. |
| 152 | """ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 153 | if known_paths is None: |
Brett Cannon | 13252b8 | 2013-01-25 13:57:16 -0500 | [diff] [blame] | 154 | known_paths = _init_pathinfo() |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 155 | reset = True |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 156 | else: |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 157 | reset = False |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 158 | fullname = os.path.join(sitedir, name) |
| 159 | try: |
Miss Islington (bot) | 35202c7 | 2019-06-21 15:35:44 -0700 | [diff] [blame^] | 160 | f = io.TextIOWrapper(io.open_code(fullname)) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 161 | except OSError: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 162 | return |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 163 | with f: |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 164 | for n, line in enumerate(f): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 165 | if line.startswith("#"): |
| 166 | continue |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 167 | try: |
| 168 | if line.startswith(("import ", "import\t")): |
| 169 | exec(line) |
| 170 | continue |
| 171 | line = line.rstrip() |
| 172 | dir, dircase = makepath(sitedir, line) |
| 173 | if not dircase in known_paths and os.path.exists(dir): |
| 174 | sys.path.append(dir) |
| 175 | known_paths.add(dircase) |
Florent Xicluna | 54540ec | 2011-11-04 08:29:17 +0100 | [diff] [blame] | 176 | except Exception: |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 177 | print("Error processing line {:d} of {}:\n".format(n+1, fullname), |
| 178 | file=sys.stderr) |
Victor Stinner | 6553211 | 2012-02-21 22:10:16 +0100 | [diff] [blame] | 179 | import traceback |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 180 | for record in traceback.format_exception(*sys.exc_info()): |
| 181 | for line in record.splitlines(): |
| 182 | print(' '+line, file=sys.stderr) |
| 183 | print("\nRemainder of file ignored", file=sys.stderr) |
| 184 | break |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 185 | if reset: |
| 186 | known_paths = None |
| 187 | return known_paths |
| 188 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 189 | |
Brett Cannon | 12f8c4d | 2004-07-09 23:38:18 +0000 | [diff] [blame] | 190 | def addsitedir(sitedir, known_paths=None): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 191 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
| 192 | 'sitedir'""" |
| 193 | if known_paths is None: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 194 | known_paths = _init_pathinfo() |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 195 | reset = True |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 196 | else: |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 197 | reset = False |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 198 | sitedir, sitedircase = makepath(sitedir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 199 | if not sitedircase in known_paths: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 200 | sys.path.append(sitedir) # Add path component |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 201 | known_paths.add(sitedircase) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 202 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 203 | names = os.listdir(sitedir) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 204 | except OSError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 205 | return |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 206 | names = [name for name in names if name.endswith(".pth")] |
| 207 | for name in sorted(names): |
| 208 | addpackage(sitedir, name, known_paths) |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 209 | if reset: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 210 | known_paths = None |
| 211 | return known_paths |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 212 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 213 | |
| 214 | def check_enableusersite(): |
| 215 | """Check if user site directory is safe for inclusion |
| 216 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 217 | The function tests for the command line flag (including environment var), |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 218 | process uid/gid equal to effective uid/gid. |
| 219 | |
| 220 | None: Disabled for security reasons |
| 221 | False: Disabled by user (command line option) |
| 222 | True: Safe and enabled |
| 223 | """ |
| 224 | if sys.flags.no_user_site: |
| 225 | return False |
| 226 | |
| 227 | if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
| 228 | # check process uid == effective uid |
| 229 | if os.geteuid() != os.getuid(): |
| 230 | return None |
| 231 | if hasattr(os, "getgid") and hasattr(os, "getegid"): |
| 232 | # check process gid == effective gid |
| 233 | if os.getegid() != os.getgid(): |
| 234 | return None |
| 235 | |
| 236 | return True |
| 237 | |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 238 | |
| 239 | # NOTE: sysconfig and it's dependencies are relatively large but site module |
| 240 | # needs very limited part of them. |
| 241 | # To speedup startup time, we have copy of them. |
| 242 | # |
| 243 | # See https://bugs.python.org/issue29585 |
| 244 | |
| 245 | # Copy of sysconfig._getuserbase() |
| 246 | def _getuserbase(): |
| 247 | env_base = os.environ.get("PYTHONUSERBASE", None) |
| 248 | if env_base: |
| 249 | return env_base |
| 250 | |
| 251 | def joinuser(*args): |
| 252 | return os.path.expanduser(os.path.join(*args)) |
| 253 | |
| 254 | if os.name == "nt": |
| 255 | base = os.environ.get("APPDATA") or "~" |
| 256 | return joinuser(base, "Python") |
| 257 | |
| 258 | if sys.platform == "darwin" and sys._framework: |
| 259 | return joinuser("~", "Library", sys._framework, |
| 260 | "%d.%d" % sys.version_info[:2]) |
| 261 | |
| 262 | return joinuser("~", ".local") |
| 263 | |
| 264 | |
| 265 | # Same to sysconfig.get_path('purelib', os.name+'_user') |
| 266 | def _get_path(userbase): |
| 267 | version = sys.version_info |
| 268 | |
| 269 | if os.name == 'nt': |
Victor Stinner | b01c574 | 2017-06-28 18:34:42 +0200 | [diff] [blame] | 270 | return f'{userbase}\\Python{version[0]}{version[1]}\\site-packages' |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 271 | |
| 272 | if sys.platform == 'darwin' and sys._framework: |
| 273 | return f'{userbase}/lib/python/site-packages' |
| 274 | |
| 275 | return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages' |
| 276 | |
| 277 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 278 | def getuserbase(): |
| 279 | """Returns the `user base` directory path. |
| 280 | |
| 281 | The `user base` directory can be used to store data. If the global |
| 282 | variable ``USER_BASE`` is not initialized yet, this function will also set |
| 283 | it. |
| 284 | """ |
| 285 | global USER_BASE |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 286 | if USER_BASE is None: |
| 287 | USER_BASE = _getuserbase() |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 288 | return USER_BASE |
| 289 | |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 290 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 291 | def getusersitepackages(): |
| 292 | """Returns the user-specific site-packages directory path. |
| 293 | |
| 294 | If the global variable ``USER_SITE`` is not initialized yet, this |
| 295 | function will also set it. |
| 296 | """ |
| 297 | global USER_SITE |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 298 | userbase = getuserbase() # this will also set USER_BASE |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 299 | |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 300 | if USER_SITE is None: |
| 301 | USER_SITE = _get_path(userbase) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 302 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 303 | return USER_SITE |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 304 | |
| 305 | def addusersitepackages(known_paths): |
| 306 | """Add a per user site-package to sys.path |
| 307 | |
| 308 | Each user has its own python directory with site-packages in the |
| 309 | home directory. |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 310 | """ |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 311 | # get the per user site-package path |
| 312 | # this call will also make sure USER_BASE and USER_SITE are set |
| 313 | user_site = getusersitepackages() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 314 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 315 | if ENABLE_USER_SITE and os.path.isdir(user_site): |
| 316 | addsitedir(user_site, known_paths) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 317 | return known_paths |
| 318 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 319 | def getsitepackages(prefixes=None): |
Antoine Pitrou | 9e82b17 | 2014-06-12 19:41:30 -0400 | [diff] [blame] | 320 | """Returns a list containing all global site-packages directories. |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 321 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 322 | For each directory present in ``prefixes`` (or the global ``PREFIXES``), |
| 323 | this function will find its `site-packages` subdirectory depending on the |
| 324 | system environment, and will return a list of full paths. |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 325 | """ |
| 326 | sitepackages = [] |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 327 | seen = set() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 328 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 329 | if prefixes is None: |
| 330 | prefixes = PREFIXES |
| 331 | |
| 332 | for prefix in prefixes: |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 333 | if not prefix or prefix in seen: |
| 334 | continue |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 335 | seen.add(prefix) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 336 | |
Christian Heimes | de0b962 | 2012-11-19 00:59:39 +0100 | [diff] [blame] | 337 | if os.sep == '/': |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 338 | sitepackages.append(os.path.join(prefix, "lib", |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 339 | "python%d.%d" % sys.version_info[:2], |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 340 | "site-packages")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 341 | else: |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 342 | sitepackages.append(prefix) |
| 343 | sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 344 | return sitepackages |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 345 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 346 | def addsitepackages(known_paths, prefixes=None): |
Antoine Pitrou | 9e82b17 | 2014-06-12 19:41:30 -0400 | [diff] [blame] | 347 | """Add site-packages to sys.path""" |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 348 | for sitedir in getsitepackages(prefixes): |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 349 | if os.path.isdir(sitedir): |
| 350 | addsitedir(sitedir, known_paths) |
| 351 | |
| 352 | return known_paths |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 353 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 354 | def setquit(): |
Brian Curtin | fb1d3c1 | 2010-04-12 23:33:42 +0000 | [diff] [blame] | 355 | """Define new builtins 'quit' and 'exit'. |
| 356 | |
| 357 | These are objects which make the interpreter exit when called. |
| 358 | 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] | 359 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 360 | """ |
Ned Deily | c6ef503 | 2016-10-01 21:12:16 -0400 | [diff] [blame] | 361 | if os.sep == '\\': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 362 | eof = 'Ctrl-Z plus Return' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 363 | else: |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 364 | eof = 'Ctrl-D (i.e. EOF)' |
Tim Peters | 88ca467 | 2006-03-10 23:39:56 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | 853395b | 2013-08-06 22:56:40 +0200 | [diff] [blame] | 366 | builtins.quit = _sitebuiltins.Quitter('quit', eof) |
| 367 | builtins.exit = _sitebuiltins.Quitter('exit', eof) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 368 | |
| 369 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 370 | def setcopyright(): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 371 | """Set 'copyright' and 'credits' in builtins""" |
Antoine Pitrou | 853395b | 2013-08-06 22:56:40 +0200 | [diff] [blame] | 372 | builtins.copyright = _sitebuiltins._Printer("copyright", sys.copyright) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 373 | if sys.platform[:4] == 'java': |
Antoine Pitrou | 853395b | 2013-08-06 22:56:40 +0200 | [diff] [blame] | 374 | builtins.credits = _sitebuiltins._Printer( |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 375 | "credits", |
Antoine Pitrou | f93c7b8 | 2013-08-01 19:46:04 +0200 | [diff] [blame] | 376 | "Jython is maintained by the Jython developers (www.jython.org).") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 377 | else: |
Antoine Pitrou | 853395b | 2013-08-06 22:56:40 +0200 | [diff] [blame] | 378 | builtins.credits = _sitebuiltins._Printer("credits", """\ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 379 | Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
Antoine Pitrou | f93c7b8 | 2013-08-01 19:46:04 +0200 | [diff] [blame] | 380 | for supporting Python development. See www.python.org for more information.""") |
Martin v. Löwis | c00d39e | 2014-03-30 21:07:25 +0200 | [diff] [blame] | 381 | files, dirs = [], [] |
| 382 | # Not all modules are required to have a __file__ attribute. See |
| 383 | # PEP 420 for more details. |
| 384 | if hasattr(os, '__file__'): |
| 385 | here = os.path.dirname(os.__file__) |
| 386 | files.extend(["LICENSE.txt", "LICENSE"]) |
| 387 | dirs.extend([os.path.join(here, os.pardir), here, os.curdir]) |
Antoine Pitrou | 853395b | 2013-08-06 22:56:40 +0200 | [diff] [blame] | 388 | builtins.license = _sitebuiltins._Printer( |
R David Murray | 692ee9e | 2013-09-14 13:31:44 -0400 | [diff] [blame] | 389 | "license", |
Benjamin Peterson | d40f136 | 2015-02-01 20:17:22 -0500 | [diff] [blame] | 390 | "See https://www.python.org/psf/license/", |
Martin v. Löwis | c00d39e | 2014-03-30 21:07:25 +0200 | [diff] [blame] | 391 | files, dirs) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 392 | |
| 393 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 394 | def sethelper(): |
Antoine Pitrou | 853395b | 2013-08-06 22:56:40 +0200 | [diff] [blame] | 395 | builtins.help = _sitebuiltins._Helper() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 397 | def enablerlcompleter(): |
| 398 | """Enable default readline configuration on interactive prompts, by |
| 399 | registering a sys.__interactivehook__. |
| 400 | |
| 401 | If the readline module can be imported, the hook will set the Tab key |
| 402 | as completion key and register ~/.python_history as history file. |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 403 | This can be overridden in the sitecustomize or usercustomize module, |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 404 | or in a PYTHONSTARTUP file. |
| 405 | """ |
| 406 | def register_readline(): |
| 407 | import atexit |
| 408 | try: |
| 409 | import readline |
| 410 | import rlcompleter |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 411 | except ImportError: |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 412 | return |
| 413 | |
| 414 | # Reading the initialization (config) file may not be enough to set a |
R David Murray | 4a04301 | 2013-09-06 13:08:08 -0400 | [diff] [blame] | 415 | # completion key, so we set one first and then read the file. |
| 416 | readline_doc = getattr(readline, '__doc__', '') |
| 417 | if readline_doc is not None and 'libedit' in readline_doc: |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 418 | readline.parse_and_bind('bind ^I rl_complete') |
| 419 | else: |
| 420 | readline.parse_and_bind('tab: complete') |
Mark Dickinson | 9d35133 | 2013-05-06 15:39:31 +0200 | [diff] [blame] | 421 | |
| 422 | try: |
| 423 | readline.read_init_file() |
| 424 | except OSError: |
| 425 | # An OSError here could have many causes, but the most likely one |
| 426 | # is that there's no .inputrc file (or .editrc file in the case of |
| 427 | # Mac OS X + libedit) in the expected location. In that case, we |
| 428 | # want to ignore the exception. |
| 429 | pass |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 430 | |
Jason R. Coombs | 4d91490 | 2014-01-28 09:06:58 -0500 | [diff] [blame] | 431 | if readline.get_current_history_length() == 0: |
Antoine Pitrou | 5d23e6d | 2013-09-29 22:18:38 +0200 | [diff] [blame] | 432 | # If no history was loaded, default to .python_history. |
| 433 | # The guard is necessary to avoid doubling history size at |
| 434 | # each interpreter exit when readline was already configured |
| 435 | # through a PYTHONSTARTUP hook, see: |
| 436 | # http://bugs.python.org/issue5845#msg198636 |
| 437 | history = os.path.join(os.path.expanduser('~'), |
| 438 | '.python_history') |
| 439 | try: |
| 440 | readline.read_history_file(history) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 441 | except OSError: |
Antoine Pitrou | 5d23e6d | 2013-09-29 22:18:38 +0200 | [diff] [blame] | 442 | pass |
Anthony Sottile | b249966 | 2018-08-06 01:28:19 -0700 | [diff] [blame] | 443 | |
| 444 | def write_history(): |
| 445 | try: |
| 446 | readline.write_history_file(history) |
| 447 | except (FileNotFoundError, PermissionError): |
| 448 | # home directory does not exist or is not writable |
| 449 | # https://bugs.python.org/issue19891 |
| 450 | pass |
| 451 | |
| 452 | atexit.register(write_history) |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 453 | |
| 454 | sys.__interactivehook__ = register_readline |
| 455 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 456 | def venv(known_paths): |
| 457 | global PREFIXES, ENABLE_USER_SITE |
| 458 | |
| 459 | env = os.environ |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 460 | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env: |
Steve Dower | a8474d0 | 2019-02-03 23:19:38 -0800 | [diff] [blame] | 461 | executable = sys._base_executable = os.environ['__PYVENV_LAUNCHER__'] |
| 462 | elif sys.platform == 'win32' and '__PYVENV_LAUNCHER__' in env: |
| 463 | executable = sys.executable |
| 464 | import _winapi |
| 465 | sys._base_executable = _winapi.GetModuleFileName(0) |
| 466 | # bpo-35873: Clear the environment variable to avoid it being |
| 467 | # inherited by child processes. |
| 468 | del os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 469 | else: |
| 470 | executable = sys.executable |
Vinay Sajip | 27e4b60 | 2012-11-23 19:16:49 +0000 | [diff] [blame] | 471 | exe_dir, _ = os.path.split(os.path.abspath(executable)) |
| 472 | site_prefix = os.path.dirname(exe_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 473 | sys._home = None |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 474 | conf_basename = 'pyvenv.cfg' |
| 475 | candidate_confs = [ |
| 476 | conffile for conffile in ( |
Vinay Sajip | 27e4b60 | 2012-11-23 19:16:49 +0000 | [diff] [blame] | 477 | os.path.join(exe_dir, conf_basename), |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 478 | os.path.join(site_prefix, conf_basename) |
| 479 | ) |
| 480 | if os.path.isfile(conffile) |
| 481 | ] |
| 482 | |
| 483 | if candidate_confs: |
| 484 | virtual_conf = candidate_confs[0] |
| 485 | system_site = "true" |
Vinay Sajip | f223c53 | 2015-10-01 11:27:00 +0100 | [diff] [blame] | 486 | # Issue 25185: Use UTF-8, as that's what the venv module uses when |
| 487 | # writing the file. |
| 488 | with open(virtual_conf, encoding='utf-8') as f: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 489 | for line in f: |
Serhiy Storchaka | 727ba7c | 2016-11-08 20:17:35 +0200 | [diff] [blame] | 490 | if '=' in line: |
| 491 | key, _, value = line.partition('=') |
| 492 | key = key.strip().lower() |
| 493 | value = value.strip() |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 494 | if key == 'include-system-site-packages': |
| 495 | system_site = value.lower() |
| 496 | elif key == 'home': |
| 497 | sys._home = value |
| 498 | |
| 499 | sys.prefix = sys.exec_prefix = site_prefix |
| 500 | |
| 501 | # Doing this here ensures venv takes precedence over user-site |
| 502 | addsitepackages(known_paths, [sys.prefix]) |
| 503 | |
| 504 | # addsitepackages will process site_prefix again if its in PREFIXES, |
| 505 | # but that's ok; known_paths will prevent anything being added twice |
| 506 | if system_site == "true": |
| 507 | PREFIXES.insert(0, sys.prefix) |
| 508 | else: |
| 509 | PREFIXES = [sys.prefix] |
| 510 | ENABLE_USER_SITE = False |
| 511 | |
| 512 | return known_paths |
| 513 | |
| 514 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 515 | def execsitecustomize(): |
| 516 | """Run custom site specific code, if available.""" |
| 517 | try: |
Victor Stinner | e3560a7 | 2016-01-22 12:22:07 +0100 | [diff] [blame] | 518 | try: |
| 519 | import sitecustomize |
| 520 | except ImportError as exc: |
| 521 | if exc.name == 'sitecustomize': |
| 522 | pass |
| 523 | else: |
| 524 | raise |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 525 | except Exception as err: |
Steve Dower | 313523c | 2016-09-17 12:22:41 -0700 | [diff] [blame] | 526 | if sys.flags.verbose: |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 527 | sys.excepthook(*sys.exc_info()) |
| 528 | else: |
| 529 | sys.stderr.write( |
| 530 | "Error in sitecustomize; set PYTHONVERBOSE for traceback:\n" |
| 531 | "%s: %s\n" % |
| 532 | (err.__class__.__name__, err)) |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 533 | |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 534 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 535 | def execusercustomize(): |
| 536 | """Run custom user specific code, if available.""" |
| 537 | try: |
Victor Stinner | e3560a7 | 2016-01-22 12:22:07 +0100 | [diff] [blame] | 538 | try: |
| 539 | import usercustomize |
| 540 | except ImportError as exc: |
| 541 | if exc.name == 'usercustomize': |
| 542 | pass |
| 543 | else: |
| 544 | raise |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 545 | except Exception as err: |
Steve Dower | 313523c | 2016-09-17 12:22:41 -0700 | [diff] [blame] | 546 | if sys.flags.verbose: |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 547 | sys.excepthook(*sys.exc_info()) |
| 548 | else: |
| 549 | sys.stderr.write( |
| 550 | "Error in usercustomize; set PYTHONVERBOSE for traceback:\n" |
| 551 | "%s: %s\n" % |
| 552 | (err.__class__.__name__, err)) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 553 | |
| 554 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 555 | def main(): |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 556 | """Add standard site-specific directories to the module search path. |
| 557 | |
| 558 | This function is called automatically when this module is imported, |
| 559 | unless the python interpreter was started with the -S flag. |
| 560 | """ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 561 | global ENABLE_USER_SITE |
| 562 | |
INADA Naoki | 2e4e011 | 2017-03-15 00:52:19 +0900 | [diff] [blame] | 563 | orig_path = sys.path[:] |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 564 | known_paths = removeduppaths() |
INADA Naoki | 2e4e011 | 2017-03-15 00:52:19 +0900 | [diff] [blame] | 565 | if orig_path != sys.path: |
| 566 | # removeduppaths() might make sys.path absolute. |
| 567 | # fix __file__ and __cached__ of already imported modules too. |
| 568 | abs_paths() |
| 569 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 570 | known_paths = venv(known_paths) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 571 | if ENABLE_USER_SITE is None: |
| 572 | ENABLE_USER_SITE = check_enableusersite() |
| 573 | known_paths = addusersitepackages(known_paths) |
| 574 | known_paths = addsitepackages(known_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 575 | setquit() |
| 576 | setcopyright() |
| 577 | sethelper() |
Steve Dower | 313523c | 2016-09-17 12:22:41 -0700 | [diff] [blame] | 578 | if not sys.flags.isolated: |
| 579 | enablerlcompleter() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 580 | execsitecustomize() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 581 | if ENABLE_USER_SITE: |
| 582 | execusercustomize() |
Marc-André Lemburg | 990bbe9 | 2000-06-07 09:12:09 +0000 | [diff] [blame] | 583 | |
Steve Dower | 313523c | 2016-09-17 12:22:41 -0700 | [diff] [blame] | 584 | # Prevent extending of sys.path when python was started with -S and |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 585 | # site is imported later. |
| 586 | if not sys.flags.no_site: |
| 587 | main() |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 588 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 589 | def _script(): |
| 590 | help = """\ |
| 591 | %s [--user-base] [--user-site] |
| 592 | |
| 593 | Without arguments print some useful information |
| 594 | With arguments print the value of USER_BASE and/or USER_SITE separated |
| 595 | by '%s'. |
| 596 | |
| 597 | Exit codes with --user-base or --user-site: |
| 598 | 0 - user site directory is enabled |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 599 | 1 - user site directory is disabled by user |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 600 | 2 - uses site directory is disabled by super user |
| 601 | or for security reasons |
| 602 | >2 - unknown error |
| 603 | """ |
| 604 | args = sys.argv[1:] |
| 605 | if not args: |
Meador Inge | 9a7a811 | 2013-04-13 20:29:49 -0500 | [diff] [blame] | 606 | user_base = getuserbase() |
| 607 | user_site = getusersitepackages() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 608 | print("sys.path = [") |
| 609 | for dir in sys.path: |
| 610 | print(" %r," % (dir,)) |
| 611 | print("]") |
Meador Inge | 9a7a811 | 2013-04-13 20:29:49 -0500 | [diff] [blame] | 612 | print("USER_BASE: %r (%s)" % (user_base, |
| 613 | "exists" if os.path.isdir(user_base) else "doesn't exist")) |
| 614 | print("USER_SITE: %r (%s)" % (user_site, |
| 615 | "exists" if os.path.isdir(user_site) else "doesn't exist")) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 616 | print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE) |
| 617 | sys.exit(0) |
| 618 | |
| 619 | buffer = [] |
| 620 | if '--user-base' in args: |
| 621 | buffer.append(USER_BASE) |
| 622 | if '--user-site' in args: |
| 623 | buffer.append(USER_SITE) |
| 624 | |
| 625 | if buffer: |
| 626 | print(os.pathsep.join(buffer)) |
| 627 | if ENABLE_USER_SITE: |
| 628 | sys.exit(0) |
| 629 | elif ENABLE_USER_SITE is False: |
| 630 | sys.exit(1) |
| 631 | elif ENABLE_USER_SITE is None: |
| 632 | sys.exit(2) |
| 633 | else: |
| 634 | sys.exit(3) |
| 635 | else: |
| 636 | import textwrap |
| 637 | print(textwrap.dedent(help % (sys.argv[0], os.pathsep))) |
| 638 | sys.exit(10) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 639 | |
| 640 | if __name__ == '__main__': |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 641 | _script() |