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