| 1 | n/a | """Append module search paths for third-party packages to sys.path. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | **************************************************************** |
|---|
| 4 | n/a | * This module is automatically imported during initialization. * |
|---|
| 5 | n/a | **************************************************************** |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | This will append site-specific paths to the module search path. On |
|---|
| 8 | n/a | Unix (including Mac OSX), it starts with sys.prefix and |
|---|
| 9 | n/a | sys.exec_prefix (if different) and appends |
|---|
| 10 | n/a | lib/python<version>/site-packages. |
|---|
| 11 | n/a | On other platforms (such as Windows), it tries each of the |
|---|
| 12 | n/a | prefixes directly, as well as with lib/site-packages appended. The |
|---|
| 13 | n/a | resulting directories, if they exist, are appended to sys.path, and |
|---|
| 14 | n/a | also inspected for path configuration files. |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | If a file named "pyvenv.cfg" exists one directory above sys.executable, |
|---|
| 17 | n/a | sys.prefix and sys.exec_prefix are set to that directory and |
|---|
| 18 | n/a | it is also checked for site-packages (sys.base_prefix and |
|---|
| 19 | n/a | sys.base_exec_prefix will always be the "real" prefixes of the Python |
|---|
| 20 | n/a | installation). If "pyvenv.cfg" (a bootstrap configuration file) contains |
|---|
| 21 | n/a | the key "include-system-site-packages" set to anything other than "false" |
|---|
| 22 | n/a | (case-insensitive), the system-level prefixes will still also be |
|---|
| 23 | n/a | searched for site-packages; otherwise they won't. |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | All of the resulting site-specific directories, if they exist, are |
|---|
| 26 | n/a | appended to sys.path, and also inspected for path configuration |
|---|
| 27 | n/a | files. |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | A path configuration file is a file whose name has the form |
|---|
| 30 | n/a | <package>.pth; its contents are additional directories (one per line) |
|---|
| 31 | n/a | to be added to sys.path. Non-existing directories (or |
|---|
| 32 | n/a | non-directories) are never added to sys.path; no directory is added to |
|---|
| 33 | n/a | sys.path more than once. Blank lines and lines beginning with |
|---|
| 34 | n/a | '#' are skipped. Lines starting with 'import' are executed. |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | For example, suppose sys.prefix and sys.exec_prefix are set to |
|---|
| 37 | n/a | /usr/local and there is a directory /usr/local/lib/python2.5/site-packages |
|---|
| 38 | n/a | with three subdirectories, foo, bar and spam, and two path |
|---|
| 39 | n/a | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
|---|
| 40 | n/a | following: |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | # foo package configuration |
|---|
| 43 | n/a | foo |
|---|
| 44 | n/a | bar |
|---|
| 45 | n/a | bletch |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | and bar.pth contains: |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | # bar package configuration |
|---|
| 50 | n/a | bar |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | Then the following directories are added to sys.path, in this order: |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | /usr/local/lib/python2.5/site-packages/bar |
|---|
| 55 | n/a | /usr/local/lib/python2.5/site-packages/foo |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | Note that bletch is omitted because it doesn't exist; bar precedes foo |
|---|
| 58 | n/a | because bar.pth comes alphabetically before foo.pth; and spam is |
|---|
| 59 | n/a | omitted because it is not mentioned in either path configuration file. |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | The readline module is also automatically configured to enable |
|---|
| 62 | n/a | completion for systems that support it. This can be overridden in |
|---|
| 63 | n/a | sitecustomize, usercustomize or PYTHONSTARTUP. Starting Python in |
|---|
| 64 | n/a | isolated mode (-I) disables automatic readline configuration. |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | After these operations, an attempt is made to import a module |
|---|
| 67 | n/a | named sitecustomize, which can perform arbitrary additional |
|---|
| 68 | n/a | site-specific customizations. If this import fails with an |
|---|
| 69 | n/a | ImportError exception, it is silently ignored. |
|---|
| 70 | n/a | """ |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | import sys |
|---|
| 73 | n/a | import os |
|---|
| 74 | n/a | import builtins |
|---|
| 75 | n/a | import _sitebuiltins |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | # Prefixes for site-packages; add additional prefixes like /usr/local here |
|---|
| 78 | n/a | PREFIXES = [sys.prefix, sys.exec_prefix] |
|---|
| 79 | n/a | # Enable per user site-packages directory |
|---|
| 80 | n/a | # set it to False to disable the feature or True to force the feature |
|---|
| 81 | n/a | ENABLE_USER_SITE = None |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | # for distutils.commands.install |
|---|
| 84 | n/a | # These values are initialized by the getuserbase() and getusersitepackages() |
|---|
| 85 | n/a | # functions, through the main() function when Python starts. |
|---|
| 86 | n/a | USER_SITE = None |
|---|
| 87 | n/a | USER_BASE = None |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | def makepath(*paths): |
|---|
| 91 | n/a | dir = os.path.join(*paths) |
|---|
| 92 | n/a | try: |
|---|
| 93 | n/a | dir = os.path.abspath(dir) |
|---|
| 94 | n/a | except OSError: |
|---|
| 95 | n/a | pass |
|---|
| 96 | n/a | return dir, os.path.normcase(dir) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | def abs_paths(): |
|---|
| 100 | n/a | """Set all module __file__ and __cached__ attributes to an absolute path""" |
|---|
| 101 | n/a | for m in set(sys.modules.values()): |
|---|
| 102 | n/a | if (getattr(getattr(m, '__loader__', None), '__module__', None) not in |
|---|
| 103 | n/a | ('_frozen_importlib', '_frozen_importlib_external')): |
|---|
| 104 | n/a | continue # don't mess with a PEP 302-supplied __file__ |
|---|
| 105 | n/a | try: |
|---|
| 106 | n/a | m.__file__ = os.path.abspath(m.__file__) |
|---|
| 107 | n/a | except (AttributeError, OSError): |
|---|
| 108 | n/a | pass |
|---|
| 109 | n/a | try: |
|---|
| 110 | n/a | m.__cached__ = os.path.abspath(m.__cached__) |
|---|
| 111 | n/a | except (AttributeError, OSError): |
|---|
| 112 | n/a | pass |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | def removeduppaths(): |
|---|
| 116 | n/a | """ Remove duplicate entries from sys.path along with making them |
|---|
| 117 | n/a | absolute""" |
|---|
| 118 | n/a | # This ensures that the initial path provided by the interpreter contains |
|---|
| 119 | n/a | # only absolute pathnames, even if we're running from the build directory. |
|---|
| 120 | n/a | L = [] |
|---|
| 121 | n/a | known_paths = set() |
|---|
| 122 | n/a | for dir in sys.path: |
|---|
| 123 | n/a | # Filter out duplicate paths (on case-insensitive file systems also |
|---|
| 124 | n/a | # if they only differ in case); turn relative paths into absolute |
|---|
| 125 | n/a | # paths. |
|---|
| 126 | n/a | dir, dircase = makepath(dir) |
|---|
| 127 | n/a | if not dircase in known_paths: |
|---|
| 128 | n/a | L.append(dir) |
|---|
| 129 | n/a | known_paths.add(dircase) |
|---|
| 130 | n/a | sys.path[:] = L |
|---|
| 131 | n/a | return known_paths |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def _init_pathinfo(): |
|---|
| 135 | n/a | """Return a set containing all existing file system items from sys.path.""" |
|---|
| 136 | n/a | d = set() |
|---|
| 137 | n/a | for item in sys.path: |
|---|
| 138 | n/a | try: |
|---|
| 139 | n/a | if os.path.exists(item): |
|---|
| 140 | n/a | _, itemcase = makepath(item) |
|---|
| 141 | n/a | d.add(itemcase) |
|---|
| 142 | n/a | except TypeError: |
|---|
| 143 | n/a | continue |
|---|
| 144 | n/a | return d |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def addpackage(sitedir, name, known_paths): |
|---|
| 148 | n/a | """Process a .pth file within the site-packages directory: |
|---|
| 149 | n/a | For each line in the file, either combine it with sitedir to a path |
|---|
| 150 | n/a | and add that to known_paths, or execute it if it starts with 'import '. |
|---|
| 151 | n/a | """ |
|---|
| 152 | n/a | if known_paths is None: |
|---|
| 153 | n/a | known_paths = _init_pathinfo() |
|---|
| 154 | n/a | reset = True |
|---|
| 155 | n/a | else: |
|---|
| 156 | n/a | reset = False |
|---|
| 157 | n/a | fullname = os.path.join(sitedir, name) |
|---|
| 158 | n/a | try: |
|---|
| 159 | n/a | f = open(fullname, "r") |
|---|
| 160 | n/a | except OSError: |
|---|
| 161 | n/a | return |
|---|
| 162 | n/a | with f: |
|---|
| 163 | n/a | for n, line in enumerate(f): |
|---|
| 164 | n/a | if line.startswith("#"): |
|---|
| 165 | n/a | continue |
|---|
| 166 | n/a | try: |
|---|
| 167 | n/a | if line.startswith(("import ", "import\t")): |
|---|
| 168 | n/a | exec(line) |
|---|
| 169 | n/a | continue |
|---|
| 170 | n/a | line = line.rstrip() |
|---|
| 171 | n/a | dir, dircase = makepath(sitedir, line) |
|---|
| 172 | n/a | if not dircase in known_paths and os.path.exists(dir): |
|---|
| 173 | n/a | sys.path.append(dir) |
|---|
| 174 | n/a | known_paths.add(dircase) |
|---|
| 175 | n/a | except Exception: |
|---|
| 176 | n/a | print("Error processing line {:d} of {}:\n".format(n+1, fullname), |
|---|
| 177 | n/a | file=sys.stderr) |
|---|
| 178 | n/a | import traceback |
|---|
| 179 | n/a | for record in traceback.format_exception(*sys.exc_info()): |
|---|
| 180 | n/a | for line in record.splitlines(): |
|---|
| 181 | n/a | print(' '+line, file=sys.stderr) |
|---|
| 182 | n/a | print("\nRemainder of file ignored", file=sys.stderr) |
|---|
| 183 | n/a | break |
|---|
| 184 | n/a | if reset: |
|---|
| 185 | n/a | known_paths = None |
|---|
| 186 | n/a | return known_paths |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | def addsitedir(sitedir, known_paths=None): |
|---|
| 190 | n/a | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
|---|
| 191 | n/a | 'sitedir'""" |
|---|
| 192 | n/a | if known_paths is None: |
|---|
| 193 | n/a | known_paths = _init_pathinfo() |
|---|
| 194 | n/a | reset = True |
|---|
| 195 | n/a | else: |
|---|
| 196 | n/a | reset = False |
|---|
| 197 | n/a | sitedir, sitedircase = makepath(sitedir) |
|---|
| 198 | n/a | if not sitedircase in known_paths: |
|---|
| 199 | n/a | sys.path.append(sitedir) # Add path component |
|---|
| 200 | n/a | known_paths.add(sitedircase) |
|---|
| 201 | n/a | try: |
|---|
| 202 | n/a | names = os.listdir(sitedir) |
|---|
| 203 | n/a | except OSError: |
|---|
| 204 | n/a | return |
|---|
| 205 | n/a | names = [name for name in names if name.endswith(".pth")] |
|---|
| 206 | n/a | for name in sorted(names): |
|---|
| 207 | n/a | addpackage(sitedir, name, known_paths) |
|---|
| 208 | n/a | if reset: |
|---|
| 209 | n/a | known_paths = None |
|---|
| 210 | n/a | return known_paths |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | def check_enableusersite(): |
|---|
| 214 | n/a | """Check if user site directory is safe for inclusion |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | The function tests for the command line flag (including environment var), |
|---|
| 217 | n/a | process uid/gid equal to effective uid/gid. |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | None: Disabled for security reasons |
|---|
| 220 | n/a | False: Disabled by user (command line option) |
|---|
| 221 | n/a | True: Safe and enabled |
|---|
| 222 | n/a | """ |
|---|
| 223 | n/a | if sys.flags.no_user_site: |
|---|
| 224 | n/a | return False |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
|---|
| 227 | n/a | # check process uid == effective uid |
|---|
| 228 | n/a | if os.geteuid() != os.getuid(): |
|---|
| 229 | n/a | return None |
|---|
| 230 | n/a | if hasattr(os, "getgid") and hasattr(os, "getegid"): |
|---|
| 231 | n/a | # check process gid == effective gid |
|---|
| 232 | n/a | if os.getegid() != os.getgid(): |
|---|
| 233 | n/a | return None |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | return True |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | def getuserbase(): |
|---|
| 238 | n/a | """Returns the `user base` directory path. |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | The `user base` directory can be used to store data. If the global |
|---|
| 241 | n/a | variable ``USER_BASE`` is not initialized yet, this function will also set |
|---|
| 242 | n/a | it. |
|---|
| 243 | n/a | """ |
|---|
| 244 | n/a | global USER_BASE |
|---|
| 245 | n/a | if USER_BASE is not None: |
|---|
| 246 | n/a | return USER_BASE |
|---|
| 247 | n/a | from sysconfig import get_config_var |
|---|
| 248 | n/a | USER_BASE = get_config_var('userbase') |
|---|
| 249 | n/a | return USER_BASE |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | def getusersitepackages(): |
|---|
| 252 | n/a | """Returns the user-specific site-packages directory path. |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | If the global variable ``USER_SITE`` is not initialized yet, this |
|---|
| 255 | n/a | function will also set it. |
|---|
| 256 | n/a | """ |
|---|
| 257 | n/a | global USER_SITE |
|---|
| 258 | n/a | user_base = getuserbase() # this will also set USER_BASE |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | if USER_SITE is not None: |
|---|
| 261 | n/a | return USER_SITE |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | from sysconfig import get_path |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | if sys.platform == 'darwin': |
|---|
| 266 | n/a | from sysconfig import get_config_var |
|---|
| 267 | n/a | if get_config_var('PYTHONFRAMEWORK'): |
|---|
| 268 | n/a | USER_SITE = get_path('purelib', 'osx_framework_user') |
|---|
| 269 | n/a | return USER_SITE |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | USER_SITE = get_path('purelib', '%s_user' % os.name) |
|---|
| 272 | n/a | return USER_SITE |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | def addusersitepackages(known_paths): |
|---|
| 275 | n/a | """Add a per user site-package to sys.path |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | Each user has its own python directory with site-packages in the |
|---|
| 278 | n/a | home directory. |
|---|
| 279 | n/a | """ |
|---|
| 280 | n/a | # get the per user site-package path |
|---|
| 281 | n/a | # this call will also make sure USER_BASE and USER_SITE are set |
|---|
| 282 | n/a | user_site = getusersitepackages() |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | if ENABLE_USER_SITE and os.path.isdir(user_site): |
|---|
| 285 | n/a | addsitedir(user_site, known_paths) |
|---|
| 286 | n/a | return known_paths |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | def getsitepackages(prefixes=None): |
|---|
| 289 | n/a | """Returns a list containing all global site-packages directories. |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | For each directory present in ``prefixes`` (or the global ``PREFIXES``), |
|---|
| 292 | n/a | this function will find its `site-packages` subdirectory depending on the |
|---|
| 293 | n/a | system environment, and will return a list of full paths. |
|---|
| 294 | n/a | """ |
|---|
| 295 | n/a | sitepackages = [] |
|---|
| 296 | n/a | seen = set() |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | if prefixes is None: |
|---|
| 299 | n/a | prefixes = PREFIXES |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | for prefix in prefixes: |
|---|
| 302 | n/a | if not prefix or prefix in seen: |
|---|
| 303 | n/a | continue |
|---|
| 304 | n/a | seen.add(prefix) |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | if os.sep == '/': |
|---|
| 307 | n/a | sitepackages.append(os.path.join(prefix, "lib", |
|---|
| 308 | n/a | "python%d.%d" % sys.version_info[:2], |
|---|
| 309 | n/a | "site-packages")) |
|---|
| 310 | n/a | else: |
|---|
| 311 | n/a | sitepackages.append(prefix) |
|---|
| 312 | n/a | sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
|---|
| 313 | n/a | if sys.platform == "darwin": |
|---|
| 314 | n/a | # for framework builds *only* we add the standard Apple |
|---|
| 315 | n/a | # locations. |
|---|
| 316 | n/a | from sysconfig import get_config_var |
|---|
| 317 | n/a | framework = get_config_var("PYTHONFRAMEWORK") |
|---|
| 318 | n/a | if framework: |
|---|
| 319 | n/a | sitepackages.append( |
|---|
| 320 | n/a | os.path.join("/Library", framework, |
|---|
| 321 | n/a | '%d.%d' % sys.version_info[:2], "site-packages")) |
|---|
| 322 | n/a | return sitepackages |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | def addsitepackages(known_paths, prefixes=None): |
|---|
| 325 | n/a | """Add site-packages to sys.path""" |
|---|
| 326 | n/a | for sitedir in getsitepackages(prefixes): |
|---|
| 327 | n/a | if os.path.isdir(sitedir): |
|---|
| 328 | n/a | addsitedir(sitedir, known_paths) |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | return known_paths |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | def setquit(): |
|---|
| 333 | n/a | """Define new builtins 'quit' and 'exit'. |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | These are objects which make the interpreter exit when called. |
|---|
| 336 | n/a | The repr of each object contains a hint at how it works. |
|---|
| 337 | n/a | |
|---|
| 338 | n/a | """ |
|---|
| 339 | n/a | if os.sep == '\\': |
|---|
| 340 | n/a | eof = 'Ctrl-Z plus Return' |
|---|
| 341 | n/a | else: |
|---|
| 342 | n/a | eof = 'Ctrl-D (i.e. EOF)' |
|---|
| 343 | n/a | |
|---|
| 344 | n/a | builtins.quit = _sitebuiltins.Quitter('quit', eof) |
|---|
| 345 | n/a | builtins.exit = _sitebuiltins.Quitter('exit', eof) |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | def setcopyright(): |
|---|
| 349 | n/a | """Set 'copyright' and 'credits' in builtins""" |
|---|
| 350 | n/a | builtins.copyright = _sitebuiltins._Printer("copyright", sys.copyright) |
|---|
| 351 | n/a | if sys.platform[:4] == 'java': |
|---|
| 352 | n/a | builtins.credits = _sitebuiltins._Printer( |
|---|
| 353 | n/a | "credits", |
|---|
| 354 | n/a | "Jython is maintained by the Jython developers (www.jython.org).") |
|---|
| 355 | n/a | else: |
|---|
| 356 | n/a | builtins.credits = _sitebuiltins._Printer("credits", """\ |
|---|
| 357 | n/a | Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
|---|
| 358 | n/a | for supporting Python development. See www.python.org for more information.""") |
|---|
| 359 | n/a | files, dirs = [], [] |
|---|
| 360 | n/a | # Not all modules are required to have a __file__ attribute. See |
|---|
| 361 | n/a | # PEP 420 for more details. |
|---|
| 362 | n/a | if hasattr(os, '__file__'): |
|---|
| 363 | n/a | here = os.path.dirname(os.__file__) |
|---|
| 364 | n/a | files.extend(["LICENSE.txt", "LICENSE"]) |
|---|
| 365 | n/a | dirs.extend([os.path.join(here, os.pardir), here, os.curdir]) |
|---|
| 366 | n/a | builtins.license = _sitebuiltins._Printer( |
|---|
| 367 | n/a | "license", |
|---|
| 368 | n/a | "See https://www.python.org/psf/license/", |
|---|
| 369 | n/a | files, dirs) |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | def sethelper(): |
|---|
| 373 | n/a | builtins.help = _sitebuiltins._Helper() |
|---|
| 374 | n/a | |
|---|
| 375 | n/a | def enablerlcompleter(): |
|---|
| 376 | n/a | """Enable default readline configuration on interactive prompts, by |
|---|
| 377 | n/a | registering a sys.__interactivehook__. |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | If the readline module can be imported, the hook will set the Tab key |
|---|
| 380 | n/a | as completion key and register ~/.python_history as history file. |
|---|
| 381 | n/a | This can be overridden in the sitecustomize or usercustomize module, |
|---|
| 382 | n/a | or in a PYTHONSTARTUP file. |
|---|
| 383 | n/a | """ |
|---|
| 384 | n/a | def register_readline(): |
|---|
| 385 | n/a | import atexit |
|---|
| 386 | n/a | try: |
|---|
| 387 | n/a | import readline |
|---|
| 388 | n/a | import rlcompleter |
|---|
| 389 | n/a | except ImportError: |
|---|
| 390 | n/a | return |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | # Reading the initialization (config) file may not be enough to set a |
|---|
| 393 | n/a | # completion key, so we set one first and then read the file. |
|---|
| 394 | n/a | readline_doc = getattr(readline, '__doc__', '') |
|---|
| 395 | n/a | if readline_doc is not None and 'libedit' in readline_doc: |
|---|
| 396 | n/a | readline.parse_and_bind('bind ^I rl_complete') |
|---|
| 397 | n/a | else: |
|---|
| 398 | n/a | readline.parse_and_bind('tab: complete') |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | try: |
|---|
| 401 | n/a | readline.read_init_file() |
|---|
| 402 | n/a | except OSError: |
|---|
| 403 | n/a | # An OSError here could have many causes, but the most likely one |
|---|
| 404 | n/a | # is that there's no .inputrc file (or .editrc file in the case of |
|---|
| 405 | n/a | # Mac OS X + libedit) in the expected location. In that case, we |
|---|
| 406 | n/a | # want to ignore the exception. |
|---|
| 407 | n/a | pass |
|---|
| 408 | n/a | |
|---|
| 409 | n/a | if readline.get_current_history_length() == 0: |
|---|
| 410 | n/a | # If no history was loaded, default to .python_history. |
|---|
| 411 | n/a | # The guard is necessary to avoid doubling history size at |
|---|
| 412 | n/a | # each interpreter exit when readline was already configured |
|---|
| 413 | n/a | # through a PYTHONSTARTUP hook, see: |
|---|
| 414 | n/a | # http://bugs.python.org/issue5845#msg198636 |
|---|
| 415 | n/a | history = os.path.join(os.path.expanduser('~'), |
|---|
| 416 | n/a | '.python_history') |
|---|
| 417 | n/a | try: |
|---|
| 418 | n/a | readline.read_history_file(history) |
|---|
| 419 | n/a | except IOError: |
|---|
| 420 | n/a | pass |
|---|
| 421 | n/a | atexit.register(readline.write_history_file, history) |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | sys.__interactivehook__ = register_readline |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | def venv(known_paths): |
|---|
| 426 | n/a | global PREFIXES, ENABLE_USER_SITE |
|---|
| 427 | n/a | |
|---|
| 428 | n/a | env = os.environ |
|---|
| 429 | n/a | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env: |
|---|
| 430 | n/a | executable = os.environ['__PYVENV_LAUNCHER__'] |
|---|
| 431 | n/a | else: |
|---|
| 432 | n/a | executable = sys.executable |
|---|
| 433 | n/a | exe_dir, _ = os.path.split(os.path.abspath(executable)) |
|---|
| 434 | n/a | site_prefix = os.path.dirname(exe_dir) |
|---|
| 435 | n/a | sys._home = None |
|---|
| 436 | n/a | conf_basename = 'pyvenv.cfg' |
|---|
| 437 | n/a | candidate_confs = [ |
|---|
| 438 | n/a | conffile for conffile in ( |
|---|
| 439 | n/a | os.path.join(exe_dir, conf_basename), |
|---|
| 440 | n/a | os.path.join(site_prefix, conf_basename) |
|---|
| 441 | n/a | ) |
|---|
| 442 | n/a | if os.path.isfile(conffile) |
|---|
| 443 | n/a | ] |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | if candidate_confs: |
|---|
| 446 | n/a | virtual_conf = candidate_confs[0] |
|---|
| 447 | n/a | system_site = "true" |
|---|
| 448 | n/a | # Issue 25185: Use UTF-8, as that's what the venv module uses when |
|---|
| 449 | n/a | # writing the file. |
|---|
| 450 | n/a | with open(virtual_conf, encoding='utf-8') as f: |
|---|
| 451 | n/a | for line in f: |
|---|
| 452 | n/a | if '=' in line: |
|---|
| 453 | n/a | key, _, value = line.partition('=') |
|---|
| 454 | n/a | key = key.strip().lower() |
|---|
| 455 | n/a | value = value.strip() |
|---|
| 456 | n/a | if key == 'include-system-site-packages': |
|---|
| 457 | n/a | system_site = value.lower() |
|---|
| 458 | n/a | elif key == 'home': |
|---|
| 459 | n/a | sys._home = value |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | sys.prefix = sys.exec_prefix = site_prefix |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | # Doing this here ensures venv takes precedence over user-site |
|---|
| 464 | n/a | addsitepackages(known_paths, [sys.prefix]) |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | # addsitepackages will process site_prefix again if its in PREFIXES, |
|---|
| 467 | n/a | # but that's ok; known_paths will prevent anything being added twice |
|---|
| 468 | n/a | if system_site == "true": |
|---|
| 469 | n/a | PREFIXES.insert(0, sys.prefix) |
|---|
| 470 | n/a | else: |
|---|
| 471 | n/a | PREFIXES = [sys.prefix] |
|---|
| 472 | n/a | ENABLE_USER_SITE = False |
|---|
| 473 | n/a | |
|---|
| 474 | n/a | return known_paths |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | def execsitecustomize(): |
|---|
| 478 | n/a | """Run custom site specific code, if available.""" |
|---|
| 479 | n/a | try: |
|---|
| 480 | n/a | try: |
|---|
| 481 | n/a | import sitecustomize |
|---|
| 482 | n/a | except ImportError as exc: |
|---|
| 483 | n/a | if exc.name == 'sitecustomize': |
|---|
| 484 | n/a | pass |
|---|
| 485 | n/a | else: |
|---|
| 486 | n/a | raise |
|---|
| 487 | n/a | except Exception as err: |
|---|
| 488 | n/a | if sys.flags.verbose: |
|---|
| 489 | n/a | sys.excepthook(*sys.exc_info()) |
|---|
| 490 | n/a | else: |
|---|
| 491 | n/a | sys.stderr.write( |
|---|
| 492 | n/a | "Error in sitecustomize; set PYTHONVERBOSE for traceback:\n" |
|---|
| 493 | n/a | "%s: %s\n" % |
|---|
| 494 | n/a | (err.__class__.__name__, err)) |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | |
|---|
| 497 | n/a | def execusercustomize(): |
|---|
| 498 | n/a | """Run custom user specific code, if available.""" |
|---|
| 499 | n/a | try: |
|---|
| 500 | n/a | try: |
|---|
| 501 | n/a | import usercustomize |
|---|
| 502 | n/a | except ImportError as exc: |
|---|
| 503 | n/a | if exc.name == 'usercustomize': |
|---|
| 504 | n/a | pass |
|---|
| 505 | n/a | else: |
|---|
| 506 | n/a | raise |
|---|
| 507 | n/a | except Exception as err: |
|---|
| 508 | n/a | if sys.flags.verbose: |
|---|
| 509 | n/a | sys.excepthook(*sys.exc_info()) |
|---|
| 510 | n/a | else: |
|---|
| 511 | n/a | sys.stderr.write( |
|---|
| 512 | n/a | "Error in usercustomize; set PYTHONVERBOSE for traceback:\n" |
|---|
| 513 | n/a | "%s: %s\n" % |
|---|
| 514 | n/a | (err.__class__.__name__, err)) |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | |
|---|
| 517 | n/a | def main(): |
|---|
| 518 | n/a | """Add standard site-specific directories to the module search path. |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | This function is called automatically when this module is imported, |
|---|
| 521 | n/a | unless the python interpreter was started with the -S flag. |
|---|
| 522 | n/a | """ |
|---|
| 523 | n/a | global ENABLE_USER_SITE |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | abs_paths() |
|---|
| 526 | n/a | known_paths = removeduppaths() |
|---|
| 527 | n/a | known_paths = venv(known_paths) |
|---|
| 528 | n/a | if ENABLE_USER_SITE is None: |
|---|
| 529 | n/a | ENABLE_USER_SITE = check_enableusersite() |
|---|
| 530 | n/a | known_paths = addusersitepackages(known_paths) |
|---|
| 531 | n/a | known_paths = addsitepackages(known_paths) |
|---|
| 532 | n/a | setquit() |
|---|
| 533 | n/a | setcopyright() |
|---|
| 534 | n/a | sethelper() |
|---|
| 535 | n/a | if not sys.flags.isolated: |
|---|
| 536 | n/a | enablerlcompleter() |
|---|
| 537 | n/a | execsitecustomize() |
|---|
| 538 | n/a | if ENABLE_USER_SITE: |
|---|
| 539 | n/a | execusercustomize() |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | # Prevent extending of sys.path when python was started with -S and |
|---|
| 542 | n/a | # site is imported later. |
|---|
| 543 | n/a | if not sys.flags.no_site: |
|---|
| 544 | n/a | main() |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | def _script(): |
|---|
| 547 | n/a | help = """\ |
|---|
| 548 | n/a | %s [--user-base] [--user-site] |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | Without arguments print some useful information |
|---|
| 551 | n/a | With arguments print the value of USER_BASE and/or USER_SITE separated |
|---|
| 552 | n/a | by '%s'. |
|---|
| 553 | n/a | |
|---|
| 554 | n/a | Exit codes with --user-base or --user-site: |
|---|
| 555 | n/a | 0 - user site directory is enabled |
|---|
| 556 | n/a | 1 - user site directory is disabled by user |
|---|
| 557 | n/a | 2 - uses site directory is disabled by super user |
|---|
| 558 | n/a | or for security reasons |
|---|
| 559 | n/a | >2 - unknown error |
|---|
| 560 | n/a | """ |
|---|
| 561 | n/a | args = sys.argv[1:] |
|---|
| 562 | n/a | if not args: |
|---|
| 563 | n/a | user_base = getuserbase() |
|---|
| 564 | n/a | user_site = getusersitepackages() |
|---|
| 565 | n/a | print("sys.path = [") |
|---|
| 566 | n/a | for dir in sys.path: |
|---|
| 567 | n/a | print(" %r," % (dir,)) |
|---|
| 568 | n/a | print("]") |
|---|
| 569 | n/a | print("USER_BASE: %r (%s)" % (user_base, |
|---|
| 570 | n/a | "exists" if os.path.isdir(user_base) else "doesn't exist")) |
|---|
| 571 | n/a | print("USER_SITE: %r (%s)" % (user_site, |
|---|
| 572 | n/a | "exists" if os.path.isdir(user_site) else "doesn't exist")) |
|---|
| 573 | n/a | print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE) |
|---|
| 574 | n/a | sys.exit(0) |
|---|
| 575 | n/a | |
|---|
| 576 | n/a | buffer = [] |
|---|
| 577 | n/a | if '--user-base' in args: |
|---|
| 578 | n/a | buffer.append(USER_BASE) |
|---|
| 579 | n/a | if '--user-site' in args: |
|---|
| 580 | n/a | buffer.append(USER_SITE) |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | if buffer: |
|---|
| 583 | n/a | print(os.pathsep.join(buffer)) |
|---|
| 584 | n/a | if ENABLE_USER_SITE: |
|---|
| 585 | n/a | sys.exit(0) |
|---|
| 586 | n/a | elif ENABLE_USER_SITE is False: |
|---|
| 587 | n/a | sys.exit(1) |
|---|
| 588 | n/a | elif ENABLE_USER_SITE is None: |
|---|
| 589 | n/a | sys.exit(2) |
|---|
| 590 | n/a | else: |
|---|
| 591 | n/a | sys.exit(3) |
|---|
| 592 | n/a | else: |
|---|
| 593 | n/a | import textwrap |
|---|
| 594 | n/a | print(textwrap.dedent(help % (sys.argv[0], os.pathsep))) |
|---|
| 595 | n/a | sys.exit(10) |
|---|
| 596 | n/a | |
|---|
| 597 | n/a | if __name__ == '__main__': |
|---|
| 598 | n/a | _script() |
|---|