Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | """Append module search paths for third-party packages to sys.path. |
michael@0 | 2 | |
michael@0 | 3 | **************************************************************** |
michael@0 | 4 | * This module is automatically imported during initialization. * |
michael@0 | 5 | **************************************************************** |
michael@0 | 6 | |
michael@0 | 7 | In earlier versions of Python (up to 1.5a3), scripts or modules that |
michael@0 | 8 | needed to use site-specific modules would place ``import site'' |
michael@0 | 9 | somewhere near the top of their code. Because of the automatic |
michael@0 | 10 | import, this is no longer necessary (but code that does it still |
michael@0 | 11 | works). |
michael@0 | 12 | |
michael@0 | 13 | This will append site-specific paths to the module search path. On |
michael@0 | 14 | Unix, it starts with sys.prefix and sys.exec_prefix (if different) and |
michael@0 | 15 | appends lib/python<version>/site-packages as well as lib/site-python. |
michael@0 | 16 | It also supports the Debian convention of |
michael@0 | 17 | lib/python<version>/dist-packages. On other platforms (mainly Mac and |
michael@0 | 18 | Windows), it uses just sys.prefix (and sys.exec_prefix, if different, |
michael@0 | 19 | but this is unlikely). The resulting directories, if they exist, are |
michael@0 | 20 | appended to sys.path, and also inspected for path configuration files. |
michael@0 | 21 | |
michael@0 | 22 | FOR DEBIAN, this sys.path is augmented with directories in /usr/local. |
michael@0 | 23 | Local addons go into /usr/local/lib/python<version>/site-packages |
michael@0 | 24 | (resp. /usr/local/lib/site-python), Debian addons install into |
michael@0 | 25 | /usr/{lib,share}/python<version>/dist-packages. |
michael@0 | 26 | |
michael@0 | 27 | A path configuration file is a file whose name has the form |
michael@0 | 28 | <package>.pth; its contents are additional directories (one per line) |
michael@0 | 29 | to be added to sys.path. Non-existing directories (or |
michael@0 | 30 | non-directories) are never added to sys.path; no directory is added to |
michael@0 | 31 | sys.path more than once. Blank lines and lines beginning with |
michael@0 | 32 | '#' are skipped. Lines starting with 'import' are executed. |
michael@0 | 33 | |
michael@0 | 34 | For example, suppose sys.prefix and sys.exec_prefix are set to |
michael@0 | 35 | /usr/local and there is a directory /usr/local/lib/python2.X/site-packages |
michael@0 | 36 | with three subdirectories, foo, bar and spam, and two path |
michael@0 | 37 | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
michael@0 | 38 | following: |
michael@0 | 39 | |
michael@0 | 40 | # foo package configuration |
michael@0 | 41 | foo |
michael@0 | 42 | bar |
michael@0 | 43 | bletch |
michael@0 | 44 | |
michael@0 | 45 | and bar.pth contains: |
michael@0 | 46 | |
michael@0 | 47 | # bar package configuration |
michael@0 | 48 | bar |
michael@0 | 49 | |
michael@0 | 50 | Then the following directories are added to sys.path, in this order: |
michael@0 | 51 | |
michael@0 | 52 | /usr/local/lib/python2.X/site-packages/bar |
michael@0 | 53 | /usr/local/lib/python2.X/site-packages/foo |
michael@0 | 54 | |
michael@0 | 55 | Note that bletch is omitted because it doesn't exist; bar precedes foo |
michael@0 | 56 | because bar.pth comes alphabetically before foo.pth; and spam is |
michael@0 | 57 | omitted because it is not mentioned in either path configuration file. |
michael@0 | 58 | |
michael@0 | 59 | After these path manipulations, an attempt is made to import a module |
michael@0 | 60 | named sitecustomize, which can perform arbitrary additional |
michael@0 | 61 | site-specific customizations. If this import fails with an |
michael@0 | 62 | ImportError exception, it is silently ignored. |
michael@0 | 63 | |
michael@0 | 64 | """ |
michael@0 | 65 | |
michael@0 | 66 | import sys |
michael@0 | 67 | import os |
michael@0 | 68 | try: |
michael@0 | 69 | import __builtin__ as builtins |
michael@0 | 70 | except ImportError: |
michael@0 | 71 | import builtins |
michael@0 | 72 | try: |
michael@0 | 73 | set |
michael@0 | 74 | except NameError: |
michael@0 | 75 | from sets import Set as set |
michael@0 | 76 | |
michael@0 | 77 | # Prefixes for site-packages; add additional prefixes like /usr/local here |
michael@0 | 78 | PREFIXES = [sys.prefix, sys.exec_prefix] |
michael@0 | 79 | # Enable per user site-packages directory |
michael@0 | 80 | # set it to False to disable the feature or True to force the feature |
michael@0 | 81 | ENABLE_USER_SITE = None |
michael@0 | 82 | # for distutils.commands.install |
michael@0 | 83 | USER_SITE = None |
michael@0 | 84 | USER_BASE = None |
michael@0 | 85 | |
michael@0 | 86 | _is_64bit = (getattr(sys, 'maxsize', None) or getattr(sys, 'maxint')) > 2**32 |
michael@0 | 87 | _is_pypy = hasattr(sys, 'pypy_version_info') |
michael@0 | 88 | _is_jython = sys.platform[:4] == 'java' |
michael@0 | 89 | if _is_jython: |
michael@0 | 90 | ModuleType = type(os) |
michael@0 | 91 | |
michael@0 | 92 | def makepath(*paths): |
michael@0 | 93 | dir = os.path.join(*paths) |
michael@0 | 94 | if _is_jython and (dir == '__classpath__' or |
michael@0 | 95 | dir.startswith('__pyclasspath__')): |
michael@0 | 96 | return dir, dir |
michael@0 | 97 | dir = os.path.abspath(dir) |
michael@0 | 98 | return dir, os.path.normcase(dir) |
michael@0 | 99 | |
michael@0 | 100 | def abs__file__(): |
michael@0 | 101 | """Set all module' __file__ attribute to an absolute path""" |
michael@0 | 102 | for m in sys.modules.values(): |
michael@0 | 103 | if ((_is_jython and not isinstance(m, ModuleType)) or |
michael@0 | 104 | hasattr(m, '__loader__')): |
michael@0 | 105 | # only modules need the abspath in Jython. and don't mess |
michael@0 | 106 | # with a PEP 302-supplied __file__ |
michael@0 | 107 | continue |
michael@0 | 108 | f = getattr(m, '__file__', None) |
michael@0 | 109 | if f is None: |
michael@0 | 110 | continue |
michael@0 | 111 | m.__file__ = os.path.abspath(f) |
michael@0 | 112 | |
michael@0 | 113 | def removeduppaths(): |
michael@0 | 114 | """ Remove duplicate entries from sys.path along with making them |
michael@0 | 115 | absolute""" |
michael@0 | 116 | # This ensures that the initial path provided by the interpreter contains |
michael@0 | 117 | # only absolute pathnames, even if we're running from the build directory. |
michael@0 | 118 | L = [] |
michael@0 | 119 | known_paths = set() |
michael@0 | 120 | for dir in sys.path: |
michael@0 | 121 | # Filter out duplicate paths (on case-insensitive file systems also |
michael@0 | 122 | # if they only differ in case); turn relative paths into absolute |
michael@0 | 123 | # paths. |
michael@0 | 124 | dir, dircase = makepath(dir) |
michael@0 | 125 | if not dircase in known_paths: |
michael@0 | 126 | L.append(dir) |
michael@0 | 127 | known_paths.add(dircase) |
michael@0 | 128 | sys.path[:] = L |
michael@0 | 129 | return known_paths |
michael@0 | 130 | |
michael@0 | 131 | # XXX This should not be part of site.py, since it is needed even when |
michael@0 | 132 | # using the -S option for Python. See http://www.python.org/sf/586680 |
michael@0 | 133 | def addbuilddir(): |
michael@0 | 134 | """Append ./build/lib.<platform> in case we're running in the build dir |
michael@0 | 135 | (especially for Guido :-)""" |
michael@0 | 136 | from distutils.util import get_platform |
michael@0 | 137 | s = "build/lib.%s-%.3s" % (get_platform(), sys.version) |
michael@0 | 138 | if hasattr(sys, 'gettotalrefcount'): |
michael@0 | 139 | s += '-pydebug' |
michael@0 | 140 | s = os.path.join(os.path.dirname(sys.path[-1]), s) |
michael@0 | 141 | sys.path.append(s) |
michael@0 | 142 | |
michael@0 | 143 | def _init_pathinfo(): |
michael@0 | 144 | """Return a set containing all existing directory entries from sys.path""" |
michael@0 | 145 | d = set() |
michael@0 | 146 | for dir in sys.path: |
michael@0 | 147 | try: |
michael@0 | 148 | if os.path.isdir(dir): |
michael@0 | 149 | dir, dircase = makepath(dir) |
michael@0 | 150 | d.add(dircase) |
michael@0 | 151 | except TypeError: |
michael@0 | 152 | continue |
michael@0 | 153 | return d |
michael@0 | 154 | |
michael@0 | 155 | def addpackage(sitedir, name, known_paths): |
michael@0 | 156 | """Add a new path to known_paths by combining sitedir and 'name' or execute |
michael@0 | 157 | sitedir if it starts with 'import'""" |
michael@0 | 158 | if known_paths is None: |
michael@0 | 159 | _init_pathinfo() |
michael@0 | 160 | reset = 1 |
michael@0 | 161 | else: |
michael@0 | 162 | reset = 0 |
michael@0 | 163 | fullname = os.path.join(sitedir, name) |
michael@0 | 164 | try: |
michael@0 | 165 | f = open(fullname, "rU") |
michael@0 | 166 | except IOError: |
michael@0 | 167 | return |
michael@0 | 168 | try: |
michael@0 | 169 | for line in f: |
michael@0 | 170 | if line.startswith("#"): |
michael@0 | 171 | continue |
michael@0 | 172 | if line.startswith("import"): |
michael@0 | 173 | exec(line) |
michael@0 | 174 | continue |
michael@0 | 175 | line = line.rstrip() |
michael@0 | 176 | dir, dircase = makepath(sitedir, line) |
michael@0 | 177 | if not dircase in known_paths and os.path.exists(dir): |
michael@0 | 178 | sys.path.append(dir) |
michael@0 | 179 | known_paths.add(dircase) |
michael@0 | 180 | finally: |
michael@0 | 181 | f.close() |
michael@0 | 182 | if reset: |
michael@0 | 183 | known_paths = None |
michael@0 | 184 | return known_paths |
michael@0 | 185 | |
michael@0 | 186 | def addsitedir(sitedir, known_paths=None): |
michael@0 | 187 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
michael@0 | 188 | 'sitedir'""" |
michael@0 | 189 | if known_paths is None: |
michael@0 | 190 | known_paths = _init_pathinfo() |
michael@0 | 191 | reset = 1 |
michael@0 | 192 | else: |
michael@0 | 193 | reset = 0 |
michael@0 | 194 | sitedir, sitedircase = makepath(sitedir) |
michael@0 | 195 | if not sitedircase in known_paths: |
michael@0 | 196 | sys.path.append(sitedir) # Add path component |
michael@0 | 197 | try: |
michael@0 | 198 | names = os.listdir(sitedir) |
michael@0 | 199 | except os.error: |
michael@0 | 200 | return |
michael@0 | 201 | names.sort() |
michael@0 | 202 | for name in names: |
michael@0 | 203 | if name.endswith(os.extsep + "pth"): |
michael@0 | 204 | addpackage(sitedir, name, known_paths) |
michael@0 | 205 | if reset: |
michael@0 | 206 | known_paths = None |
michael@0 | 207 | return known_paths |
michael@0 | 208 | |
michael@0 | 209 | def addsitepackages(known_paths, sys_prefix=sys.prefix, exec_prefix=sys.exec_prefix): |
michael@0 | 210 | """Add site-packages (and possibly site-python) to sys.path""" |
michael@0 | 211 | prefixes = [os.path.join(sys_prefix, "local"), sys_prefix] |
michael@0 | 212 | if exec_prefix != sys_prefix: |
michael@0 | 213 | prefixes.append(os.path.join(exec_prefix, "local")) |
michael@0 | 214 | |
michael@0 | 215 | for prefix in prefixes: |
michael@0 | 216 | if prefix: |
michael@0 | 217 | if sys.platform in ('os2emx', 'riscos') or _is_jython: |
michael@0 | 218 | sitedirs = [os.path.join(prefix, "Lib", "site-packages")] |
michael@0 | 219 | elif _is_pypy: |
michael@0 | 220 | sitedirs = [os.path.join(prefix, 'site-packages')] |
michael@0 | 221 | elif sys.platform == 'darwin' and prefix == sys_prefix: |
michael@0 | 222 | |
michael@0 | 223 | if prefix.startswith("/System/Library/Frameworks/"): # Apple's Python |
michael@0 | 224 | |
michael@0 | 225 | sitedirs = [os.path.join("/Library/Python", sys.version[:3], "site-packages"), |
michael@0 | 226 | os.path.join(prefix, "Extras", "lib", "python")] |
michael@0 | 227 | |
michael@0 | 228 | else: # any other Python distros on OSX work this way |
michael@0 | 229 | sitedirs = [os.path.join(prefix, "lib", |
michael@0 | 230 | "python" + sys.version[:3], "site-packages")] |
michael@0 | 231 | |
michael@0 | 232 | elif os.sep == '/': |
michael@0 | 233 | sitedirs = [os.path.join(prefix, |
michael@0 | 234 | "lib", |
michael@0 | 235 | "python" + sys.version[:3], |
michael@0 | 236 | "site-packages"), |
michael@0 | 237 | os.path.join(prefix, "lib", "site-python"), |
michael@0 | 238 | os.path.join(prefix, "python" + sys.version[:3], "lib-dynload")] |
michael@0 | 239 | lib64_dir = os.path.join(prefix, "lib64", "python" + sys.version[:3], "site-packages") |
michael@0 | 240 | if (os.path.exists(lib64_dir) and |
michael@0 | 241 | os.path.realpath(lib64_dir) not in [os.path.realpath(p) for p in sitedirs]): |
michael@0 | 242 | if _is_64bit: |
michael@0 | 243 | sitedirs.insert(0, lib64_dir) |
michael@0 | 244 | else: |
michael@0 | 245 | sitedirs.append(lib64_dir) |
michael@0 | 246 | try: |
michael@0 | 247 | # sys.getobjects only available in --with-pydebug build |
michael@0 | 248 | sys.getobjects |
michael@0 | 249 | sitedirs.insert(0, os.path.join(sitedirs[0], 'debug')) |
michael@0 | 250 | except AttributeError: |
michael@0 | 251 | pass |
michael@0 | 252 | # Debian-specific dist-packages directories: |
michael@0 | 253 | if sys.version[0] == '2': |
michael@0 | 254 | sitedirs.append(os.path.join(prefix, "lib", |
michael@0 | 255 | "python" + sys.version[:3], |
michael@0 | 256 | "dist-packages")) |
michael@0 | 257 | else: |
michael@0 | 258 | sitedirs.append(os.path.join(prefix, "lib", |
michael@0 | 259 | "python" + sys.version[0], |
michael@0 | 260 | "dist-packages")) |
michael@0 | 261 | sitedirs.append(os.path.join(prefix, "local/lib", |
michael@0 | 262 | "python" + sys.version[:3], |
michael@0 | 263 | "dist-packages")) |
michael@0 | 264 | sitedirs.append(os.path.join(prefix, "lib", "dist-python")) |
michael@0 | 265 | else: |
michael@0 | 266 | sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")] |
michael@0 | 267 | if sys.platform == 'darwin': |
michael@0 | 268 | # for framework builds *only* we add the standard Apple |
michael@0 | 269 | # locations. Currently only per-user, but /Library and |
michael@0 | 270 | # /Network/Library could be added too |
michael@0 | 271 | if 'Python.framework' in prefix: |
michael@0 | 272 | home = os.environ.get('HOME') |
michael@0 | 273 | if home: |
michael@0 | 274 | sitedirs.append( |
michael@0 | 275 | os.path.join(home, |
michael@0 | 276 | 'Library', |
michael@0 | 277 | 'Python', |
michael@0 | 278 | sys.version[:3], |
michael@0 | 279 | 'site-packages')) |
michael@0 | 280 | for sitedir in sitedirs: |
michael@0 | 281 | if os.path.isdir(sitedir): |
michael@0 | 282 | addsitedir(sitedir, known_paths) |
michael@0 | 283 | return None |
michael@0 | 284 | |
michael@0 | 285 | def check_enableusersite(): |
michael@0 | 286 | """Check if user site directory is safe for inclusion |
michael@0 | 287 | |
michael@0 | 288 | The function tests for the command line flag (including environment var), |
michael@0 | 289 | process uid/gid equal to effective uid/gid. |
michael@0 | 290 | |
michael@0 | 291 | None: Disabled for security reasons |
michael@0 | 292 | False: Disabled by user (command line option) |
michael@0 | 293 | True: Safe and enabled |
michael@0 | 294 | """ |
michael@0 | 295 | if hasattr(sys, 'flags') and getattr(sys.flags, 'no_user_site', False): |
michael@0 | 296 | return False |
michael@0 | 297 | |
michael@0 | 298 | if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
michael@0 | 299 | # check process uid == effective uid |
michael@0 | 300 | if os.geteuid() != os.getuid(): |
michael@0 | 301 | return None |
michael@0 | 302 | if hasattr(os, "getgid") and hasattr(os, "getegid"): |
michael@0 | 303 | # check process gid == effective gid |
michael@0 | 304 | if os.getegid() != os.getgid(): |
michael@0 | 305 | return None |
michael@0 | 306 | |
michael@0 | 307 | return True |
michael@0 | 308 | |
michael@0 | 309 | def addusersitepackages(known_paths): |
michael@0 | 310 | """Add a per user site-package to sys.path |
michael@0 | 311 | |
michael@0 | 312 | Each user has its own python directory with site-packages in the |
michael@0 | 313 | home directory. |
michael@0 | 314 | |
michael@0 | 315 | USER_BASE is the root directory for all Python versions |
michael@0 | 316 | |
michael@0 | 317 | USER_SITE is the user specific site-packages directory |
michael@0 | 318 | |
michael@0 | 319 | USER_SITE/.. can be used for data. |
michael@0 | 320 | """ |
michael@0 | 321 | global USER_BASE, USER_SITE, ENABLE_USER_SITE |
michael@0 | 322 | env_base = os.environ.get("PYTHONUSERBASE", None) |
michael@0 | 323 | |
michael@0 | 324 | def joinuser(*args): |
michael@0 | 325 | return os.path.expanduser(os.path.join(*args)) |
michael@0 | 326 | |
michael@0 | 327 | #if sys.platform in ('os2emx', 'riscos'): |
michael@0 | 328 | # # Don't know what to put here |
michael@0 | 329 | # USER_BASE = '' |
michael@0 | 330 | # USER_SITE = '' |
michael@0 | 331 | if os.name == "nt": |
michael@0 | 332 | base = os.environ.get("APPDATA") or "~" |
michael@0 | 333 | if env_base: |
michael@0 | 334 | USER_BASE = env_base |
michael@0 | 335 | else: |
michael@0 | 336 | USER_BASE = joinuser(base, "Python") |
michael@0 | 337 | USER_SITE = os.path.join(USER_BASE, |
michael@0 | 338 | "Python" + sys.version[0] + sys.version[2], |
michael@0 | 339 | "site-packages") |
michael@0 | 340 | else: |
michael@0 | 341 | if env_base: |
michael@0 | 342 | USER_BASE = env_base |
michael@0 | 343 | else: |
michael@0 | 344 | USER_BASE = joinuser("~", ".local") |
michael@0 | 345 | USER_SITE = os.path.join(USER_BASE, "lib", |
michael@0 | 346 | "python" + sys.version[:3], |
michael@0 | 347 | "site-packages") |
michael@0 | 348 | |
michael@0 | 349 | if ENABLE_USER_SITE and os.path.isdir(USER_SITE): |
michael@0 | 350 | addsitedir(USER_SITE, known_paths) |
michael@0 | 351 | if ENABLE_USER_SITE: |
michael@0 | 352 | for dist_libdir in ("lib", "local/lib"): |
michael@0 | 353 | user_site = os.path.join(USER_BASE, dist_libdir, |
michael@0 | 354 | "python" + sys.version[:3], |
michael@0 | 355 | "dist-packages") |
michael@0 | 356 | if os.path.isdir(user_site): |
michael@0 | 357 | addsitedir(user_site, known_paths) |
michael@0 | 358 | return known_paths |
michael@0 | 359 | |
michael@0 | 360 | |
michael@0 | 361 | |
michael@0 | 362 | def setBEGINLIBPATH(): |
michael@0 | 363 | """The OS/2 EMX port has optional extension modules that do double duty |
michael@0 | 364 | as DLLs (and must use the .DLL file extension) for other extensions. |
michael@0 | 365 | The library search path needs to be amended so these will be found |
michael@0 | 366 | during module import. Use BEGINLIBPATH so that these are at the start |
michael@0 | 367 | of the library search path. |
michael@0 | 368 | |
michael@0 | 369 | """ |
michael@0 | 370 | dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload") |
michael@0 | 371 | libpath = os.environ['BEGINLIBPATH'].split(';') |
michael@0 | 372 | if libpath[-1]: |
michael@0 | 373 | libpath.append(dllpath) |
michael@0 | 374 | else: |
michael@0 | 375 | libpath[-1] = dllpath |
michael@0 | 376 | os.environ['BEGINLIBPATH'] = ';'.join(libpath) |
michael@0 | 377 | |
michael@0 | 378 | |
michael@0 | 379 | def setquit(): |
michael@0 | 380 | """Define new built-ins 'quit' and 'exit'. |
michael@0 | 381 | These are simply strings that display a hint on how to exit. |
michael@0 | 382 | |
michael@0 | 383 | """ |
michael@0 | 384 | if os.sep == ':': |
michael@0 | 385 | eof = 'Cmd-Q' |
michael@0 | 386 | elif os.sep == '\\': |
michael@0 | 387 | eof = 'Ctrl-Z plus Return' |
michael@0 | 388 | else: |
michael@0 | 389 | eof = 'Ctrl-D (i.e. EOF)' |
michael@0 | 390 | |
michael@0 | 391 | class Quitter(object): |
michael@0 | 392 | def __init__(self, name): |
michael@0 | 393 | self.name = name |
michael@0 | 394 | def __repr__(self): |
michael@0 | 395 | return 'Use %s() or %s to exit' % (self.name, eof) |
michael@0 | 396 | def __call__(self, code=None): |
michael@0 | 397 | # Shells like IDLE catch the SystemExit, but listen when their |
michael@0 | 398 | # stdin wrapper is closed. |
michael@0 | 399 | try: |
michael@0 | 400 | sys.stdin.close() |
michael@0 | 401 | except: |
michael@0 | 402 | pass |
michael@0 | 403 | raise SystemExit(code) |
michael@0 | 404 | builtins.quit = Quitter('quit') |
michael@0 | 405 | builtins.exit = Quitter('exit') |
michael@0 | 406 | |
michael@0 | 407 | |
michael@0 | 408 | class _Printer(object): |
michael@0 | 409 | """interactive prompt objects for printing the license text, a list of |
michael@0 | 410 | contributors and the copyright notice.""" |
michael@0 | 411 | |
michael@0 | 412 | MAXLINES = 23 |
michael@0 | 413 | |
michael@0 | 414 | def __init__(self, name, data, files=(), dirs=()): |
michael@0 | 415 | self.__name = name |
michael@0 | 416 | self.__data = data |
michael@0 | 417 | self.__files = files |
michael@0 | 418 | self.__dirs = dirs |
michael@0 | 419 | self.__lines = None |
michael@0 | 420 | |
michael@0 | 421 | def __setup(self): |
michael@0 | 422 | if self.__lines: |
michael@0 | 423 | return |
michael@0 | 424 | data = None |
michael@0 | 425 | for dir in self.__dirs: |
michael@0 | 426 | for filename in self.__files: |
michael@0 | 427 | filename = os.path.join(dir, filename) |
michael@0 | 428 | try: |
michael@0 | 429 | fp = open(filename, "rU") |
michael@0 | 430 | data = fp.read() |
michael@0 | 431 | fp.close() |
michael@0 | 432 | break |
michael@0 | 433 | except IOError: |
michael@0 | 434 | pass |
michael@0 | 435 | if data: |
michael@0 | 436 | break |
michael@0 | 437 | if not data: |
michael@0 | 438 | data = self.__data |
michael@0 | 439 | self.__lines = data.split('\n') |
michael@0 | 440 | self.__linecnt = len(self.__lines) |
michael@0 | 441 | |
michael@0 | 442 | def __repr__(self): |
michael@0 | 443 | self.__setup() |
michael@0 | 444 | if len(self.__lines) <= self.MAXLINES: |
michael@0 | 445 | return "\n".join(self.__lines) |
michael@0 | 446 | else: |
michael@0 | 447 | return "Type %s() to see the full %s text" % ((self.__name,)*2) |
michael@0 | 448 | |
michael@0 | 449 | def __call__(self): |
michael@0 | 450 | self.__setup() |
michael@0 | 451 | prompt = 'Hit Return for more, or q (and Return) to quit: ' |
michael@0 | 452 | lineno = 0 |
michael@0 | 453 | while 1: |
michael@0 | 454 | try: |
michael@0 | 455 | for i in range(lineno, lineno + self.MAXLINES): |
michael@0 | 456 | print(self.__lines[i]) |
michael@0 | 457 | except IndexError: |
michael@0 | 458 | break |
michael@0 | 459 | else: |
michael@0 | 460 | lineno += self.MAXLINES |
michael@0 | 461 | key = None |
michael@0 | 462 | while key is None: |
michael@0 | 463 | try: |
michael@0 | 464 | key = raw_input(prompt) |
michael@0 | 465 | except NameError: |
michael@0 | 466 | key = input(prompt) |
michael@0 | 467 | if key not in ('', 'q'): |
michael@0 | 468 | key = None |
michael@0 | 469 | if key == 'q': |
michael@0 | 470 | break |
michael@0 | 471 | |
michael@0 | 472 | def setcopyright(): |
michael@0 | 473 | """Set 'copyright' and 'credits' in __builtin__""" |
michael@0 | 474 | builtins.copyright = _Printer("copyright", sys.copyright) |
michael@0 | 475 | if _is_jython: |
michael@0 | 476 | builtins.credits = _Printer( |
michael@0 | 477 | "credits", |
michael@0 | 478 | "Jython is maintained by the Jython developers (www.jython.org).") |
michael@0 | 479 | elif _is_pypy: |
michael@0 | 480 | builtins.credits = _Printer( |
michael@0 | 481 | "credits", |
michael@0 | 482 | "PyPy is maintained by the PyPy developers: http://pypy.org/") |
michael@0 | 483 | else: |
michael@0 | 484 | builtins.credits = _Printer("credits", """\ |
michael@0 | 485 | Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
michael@0 | 486 | for supporting Python development. See www.python.org for more information.""") |
michael@0 | 487 | here = os.path.dirname(os.__file__) |
michael@0 | 488 | builtins.license = _Printer( |
michael@0 | 489 | "license", "See http://www.python.org/%.3s/license.html" % sys.version, |
michael@0 | 490 | ["LICENSE.txt", "LICENSE"], |
michael@0 | 491 | [os.path.join(here, os.pardir), here, os.curdir]) |
michael@0 | 492 | |
michael@0 | 493 | |
michael@0 | 494 | class _Helper(object): |
michael@0 | 495 | """Define the built-in 'help'. |
michael@0 | 496 | This is a wrapper around pydoc.help (with a twist). |
michael@0 | 497 | |
michael@0 | 498 | """ |
michael@0 | 499 | |
michael@0 | 500 | def __repr__(self): |
michael@0 | 501 | return "Type help() for interactive help, " \ |
michael@0 | 502 | "or help(object) for help about object." |
michael@0 | 503 | def __call__(self, *args, **kwds): |
michael@0 | 504 | import pydoc |
michael@0 | 505 | return pydoc.help(*args, **kwds) |
michael@0 | 506 | |
michael@0 | 507 | def sethelper(): |
michael@0 | 508 | builtins.help = _Helper() |
michael@0 | 509 | |
michael@0 | 510 | def aliasmbcs(): |
michael@0 | 511 | """On Windows, some default encodings are not provided by Python, |
michael@0 | 512 | while they are always available as "mbcs" in each locale. Make |
michael@0 | 513 | them usable by aliasing to "mbcs" in such a case.""" |
michael@0 | 514 | if sys.platform == 'win32': |
michael@0 | 515 | import locale, codecs |
michael@0 | 516 | enc = locale.getdefaultlocale()[1] |
michael@0 | 517 | if enc.startswith('cp'): # "cp***" ? |
michael@0 | 518 | try: |
michael@0 | 519 | codecs.lookup(enc) |
michael@0 | 520 | except LookupError: |
michael@0 | 521 | import encodings |
michael@0 | 522 | encodings._cache[enc] = encodings._unknown |
michael@0 | 523 | encodings.aliases.aliases[enc] = 'mbcs' |
michael@0 | 524 | |
michael@0 | 525 | def setencoding(): |
michael@0 | 526 | """Set the string encoding used by the Unicode implementation. The |
michael@0 | 527 | default is 'ascii', but if you're willing to experiment, you can |
michael@0 | 528 | change this.""" |
michael@0 | 529 | encoding = "ascii" # Default value set by _PyUnicode_Init() |
michael@0 | 530 | if 0: |
michael@0 | 531 | # Enable to support locale aware default string encodings. |
michael@0 | 532 | import locale |
michael@0 | 533 | loc = locale.getdefaultlocale() |
michael@0 | 534 | if loc[1]: |
michael@0 | 535 | encoding = loc[1] |
michael@0 | 536 | if 0: |
michael@0 | 537 | # Enable to switch off string to Unicode coercion and implicit |
michael@0 | 538 | # Unicode to string conversion. |
michael@0 | 539 | encoding = "undefined" |
michael@0 | 540 | if encoding != "ascii": |
michael@0 | 541 | # On Non-Unicode builds this will raise an AttributeError... |
michael@0 | 542 | sys.setdefaultencoding(encoding) # Needs Python Unicode build ! |
michael@0 | 543 | |
michael@0 | 544 | |
michael@0 | 545 | def execsitecustomize(): |
michael@0 | 546 | """Run custom site specific code, if available.""" |
michael@0 | 547 | try: |
michael@0 | 548 | import sitecustomize |
michael@0 | 549 | except ImportError: |
michael@0 | 550 | pass |
michael@0 | 551 | |
michael@0 | 552 | def virtual_install_main_packages(): |
michael@0 | 553 | f = open(os.path.join(os.path.dirname(__file__), 'orig-prefix.txt')) |
michael@0 | 554 | sys.real_prefix = f.read().strip() |
michael@0 | 555 | f.close() |
michael@0 | 556 | pos = 2 |
michael@0 | 557 | hardcoded_relative_dirs = [] |
michael@0 | 558 | if sys.path[0] == '': |
michael@0 | 559 | pos += 1 |
michael@0 | 560 | if _is_jython: |
michael@0 | 561 | paths = [os.path.join(sys.real_prefix, 'Lib')] |
michael@0 | 562 | elif _is_pypy: |
michael@0 | 563 | if sys.version_info > (3, 2): |
michael@0 | 564 | cpyver = '%d' % sys.version_info[0] |
michael@0 | 565 | elif sys.pypy_version_info >= (1, 5): |
michael@0 | 566 | cpyver = '%d.%d' % sys.version_info[:2] |
michael@0 | 567 | else: |
michael@0 | 568 | cpyver = '%d.%d.%d' % sys.version_info[:3] |
michael@0 | 569 | paths = [os.path.join(sys.real_prefix, 'lib_pypy'), |
michael@0 | 570 | os.path.join(sys.real_prefix, 'lib-python', cpyver)] |
michael@0 | 571 | if sys.pypy_version_info < (1, 9): |
michael@0 | 572 | paths.insert(1, os.path.join(sys.real_prefix, |
michael@0 | 573 | 'lib-python', 'modified-%s' % cpyver)) |
michael@0 | 574 | hardcoded_relative_dirs = paths[:] # for the special 'darwin' case below |
michael@0 | 575 | # |
michael@0 | 576 | # This is hardcoded in the Python executable, but relative to sys.prefix: |
michael@0 | 577 | for path in paths[:]: |
michael@0 | 578 | plat_path = os.path.join(path, 'plat-%s' % sys.platform) |
michael@0 | 579 | if os.path.exists(plat_path): |
michael@0 | 580 | paths.append(plat_path) |
michael@0 | 581 | elif sys.platform == 'win32': |
michael@0 | 582 | paths = [os.path.join(sys.real_prefix, 'Lib'), os.path.join(sys.real_prefix, 'DLLs')] |
michael@0 | 583 | else: |
michael@0 | 584 | paths = [os.path.join(sys.real_prefix, 'lib', 'python'+sys.version[:3])] |
michael@0 | 585 | hardcoded_relative_dirs = paths[:] # for the special 'darwin' case below |
michael@0 | 586 | lib64_path = os.path.join(sys.real_prefix, 'lib64', 'python'+sys.version[:3]) |
michael@0 | 587 | if os.path.exists(lib64_path): |
michael@0 | 588 | if _is_64bit: |
michael@0 | 589 | paths.insert(0, lib64_path) |
michael@0 | 590 | else: |
michael@0 | 591 | paths.append(lib64_path) |
michael@0 | 592 | # This is hardcoded in the Python executable, but relative to |
michael@0 | 593 | # sys.prefix. Debian change: we need to add the multiarch triplet |
michael@0 | 594 | # here, which is where the real stuff lives. As per PEP 421, in |
michael@0 | 595 | # Python 3.3+, this lives in sys.implementation, while in Python 2.7 |
michael@0 | 596 | # it lives in sys. |
michael@0 | 597 | try: |
michael@0 | 598 | arch = getattr(sys, 'implementation', sys)._multiarch |
michael@0 | 599 | except AttributeError: |
michael@0 | 600 | # This is a non-multiarch aware Python. Fallback to the old way. |
michael@0 | 601 | arch = sys.platform |
michael@0 | 602 | plat_path = os.path.join(sys.real_prefix, 'lib', |
michael@0 | 603 | 'python'+sys.version[:3], |
michael@0 | 604 | 'plat-%s' % arch) |
michael@0 | 605 | if os.path.exists(plat_path): |
michael@0 | 606 | paths.append(plat_path) |
michael@0 | 607 | # This is hardcoded in the Python executable, but |
michael@0 | 608 | # relative to sys.prefix, so we have to fix up: |
michael@0 | 609 | for path in list(paths): |
michael@0 | 610 | tk_dir = os.path.join(path, 'lib-tk') |
michael@0 | 611 | if os.path.exists(tk_dir): |
michael@0 | 612 | paths.append(tk_dir) |
michael@0 | 613 | |
michael@0 | 614 | # These are hardcoded in the Apple's Python executable, |
michael@0 | 615 | # but relative to sys.prefix, so we have to fix them up: |
michael@0 | 616 | if sys.platform == 'darwin': |
michael@0 | 617 | hardcoded_paths = [os.path.join(relative_dir, module) |
michael@0 | 618 | for relative_dir in hardcoded_relative_dirs |
michael@0 | 619 | for module in ('plat-darwin', 'plat-mac', 'plat-mac/lib-scriptpackages')] |
michael@0 | 620 | |
michael@0 | 621 | for path in hardcoded_paths: |
michael@0 | 622 | if os.path.exists(path): |
michael@0 | 623 | paths.append(path) |
michael@0 | 624 | |
michael@0 | 625 | sys.path.extend(paths) |
michael@0 | 626 | |
michael@0 | 627 | def force_global_eggs_after_local_site_packages(): |
michael@0 | 628 | """ |
michael@0 | 629 | Force easy_installed eggs in the global environment to get placed |
michael@0 | 630 | in sys.path after all packages inside the virtualenv. This |
michael@0 | 631 | maintains the "least surprise" result that packages in the |
michael@0 | 632 | virtualenv always mask global packages, never the other way |
michael@0 | 633 | around. |
michael@0 | 634 | |
michael@0 | 635 | """ |
michael@0 | 636 | egginsert = getattr(sys, '__egginsert', 0) |
michael@0 | 637 | for i, path in enumerate(sys.path): |
michael@0 | 638 | if i > egginsert and path.startswith(sys.prefix): |
michael@0 | 639 | egginsert = i |
michael@0 | 640 | sys.__egginsert = egginsert + 1 |
michael@0 | 641 | |
michael@0 | 642 | def virtual_addsitepackages(known_paths): |
michael@0 | 643 | force_global_eggs_after_local_site_packages() |
michael@0 | 644 | return addsitepackages(known_paths, sys_prefix=sys.real_prefix) |
michael@0 | 645 | |
michael@0 | 646 | def fixclasspath(): |
michael@0 | 647 | """Adjust the special classpath sys.path entries for Jython. These |
michael@0 | 648 | entries should follow the base virtualenv lib directories. |
michael@0 | 649 | """ |
michael@0 | 650 | paths = [] |
michael@0 | 651 | classpaths = [] |
michael@0 | 652 | for path in sys.path: |
michael@0 | 653 | if path == '__classpath__' or path.startswith('__pyclasspath__'): |
michael@0 | 654 | classpaths.append(path) |
michael@0 | 655 | else: |
michael@0 | 656 | paths.append(path) |
michael@0 | 657 | sys.path = paths |
michael@0 | 658 | sys.path.extend(classpaths) |
michael@0 | 659 | |
michael@0 | 660 | def execusercustomize(): |
michael@0 | 661 | """Run custom user specific code, if available.""" |
michael@0 | 662 | try: |
michael@0 | 663 | import usercustomize |
michael@0 | 664 | except ImportError: |
michael@0 | 665 | pass |
michael@0 | 666 | |
michael@0 | 667 | |
michael@0 | 668 | def main(): |
michael@0 | 669 | global ENABLE_USER_SITE |
michael@0 | 670 | virtual_install_main_packages() |
michael@0 | 671 | abs__file__() |
michael@0 | 672 | paths_in_sys = removeduppaths() |
michael@0 | 673 | if (os.name == "posix" and sys.path and |
michael@0 | 674 | os.path.basename(sys.path[-1]) == "Modules"): |
michael@0 | 675 | addbuilddir() |
michael@0 | 676 | if _is_jython: |
michael@0 | 677 | fixclasspath() |
michael@0 | 678 | GLOBAL_SITE_PACKAGES = not os.path.exists(os.path.join(os.path.dirname(__file__), 'no-global-site-packages.txt')) |
michael@0 | 679 | if not GLOBAL_SITE_PACKAGES: |
michael@0 | 680 | ENABLE_USER_SITE = False |
michael@0 | 681 | if ENABLE_USER_SITE is None: |
michael@0 | 682 | ENABLE_USER_SITE = check_enableusersite() |
michael@0 | 683 | paths_in_sys = addsitepackages(paths_in_sys) |
michael@0 | 684 | paths_in_sys = addusersitepackages(paths_in_sys) |
michael@0 | 685 | if GLOBAL_SITE_PACKAGES: |
michael@0 | 686 | paths_in_sys = virtual_addsitepackages(paths_in_sys) |
michael@0 | 687 | if sys.platform == 'os2emx': |
michael@0 | 688 | setBEGINLIBPATH() |
michael@0 | 689 | setquit() |
michael@0 | 690 | setcopyright() |
michael@0 | 691 | sethelper() |
michael@0 | 692 | aliasmbcs() |
michael@0 | 693 | setencoding() |
michael@0 | 694 | execsitecustomize() |
michael@0 | 695 | if ENABLE_USER_SITE: |
michael@0 | 696 | execusercustomize() |
michael@0 | 697 | # Remove sys.setdefaultencoding() so that users cannot change the |
michael@0 | 698 | # encoding after initialization. The test for presence is needed when |
michael@0 | 699 | # this module is run as a script, because this code is executed twice. |
michael@0 | 700 | if hasattr(sys, "setdefaultencoding"): |
michael@0 | 701 | del sys.setdefaultencoding |
michael@0 | 702 | |
michael@0 | 703 | main() |
michael@0 | 704 | |
michael@0 | 705 | def _script(): |
michael@0 | 706 | help = """\ |
michael@0 | 707 | %s [--user-base] [--user-site] |
michael@0 | 708 | |
michael@0 | 709 | Without arguments print some useful information |
michael@0 | 710 | With arguments print the value of USER_BASE and/or USER_SITE separated |
michael@0 | 711 | by '%s'. |
michael@0 | 712 | |
michael@0 | 713 | Exit codes with --user-base or --user-site: |
michael@0 | 714 | 0 - user site directory is enabled |
michael@0 | 715 | 1 - user site directory is disabled by user |
michael@0 | 716 | 2 - uses site directory is disabled by super user |
michael@0 | 717 | or for security reasons |
michael@0 | 718 | >2 - unknown error |
michael@0 | 719 | """ |
michael@0 | 720 | args = sys.argv[1:] |
michael@0 | 721 | if not args: |
michael@0 | 722 | print("sys.path = [") |
michael@0 | 723 | for dir in sys.path: |
michael@0 | 724 | print(" %r," % (dir,)) |
michael@0 | 725 | print("]") |
michael@0 | 726 | def exists(path): |
michael@0 | 727 | if os.path.isdir(path): |
michael@0 | 728 | return "exists" |
michael@0 | 729 | else: |
michael@0 | 730 | return "doesn't exist" |
michael@0 | 731 | print("USER_BASE: %r (%s)" % (USER_BASE, exists(USER_BASE))) |
michael@0 | 732 | print("USER_SITE: %r (%s)" % (USER_SITE, exists(USER_BASE))) |
michael@0 | 733 | print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE) |
michael@0 | 734 | sys.exit(0) |
michael@0 | 735 | |
michael@0 | 736 | buffer = [] |
michael@0 | 737 | if '--user-base' in args: |
michael@0 | 738 | buffer.append(USER_BASE) |
michael@0 | 739 | if '--user-site' in args: |
michael@0 | 740 | buffer.append(USER_SITE) |
michael@0 | 741 | |
michael@0 | 742 | if buffer: |
michael@0 | 743 | print(os.pathsep.join(buffer)) |
michael@0 | 744 | if ENABLE_USER_SITE: |
michael@0 | 745 | sys.exit(0) |
michael@0 | 746 | elif ENABLE_USER_SITE is False: |
michael@0 | 747 | sys.exit(1) |
michael@0 | 748 | elif ENABLE_USER_SITE is None: |
michael@0 | 749 | sys.exit(2) |
michael@0 | 750 | else: |
michael@0 | 751 | sys.exit(3) |
michael@0 | 752 | else: |
michael@0 | 753 | import textwrap |
michael@0 | 754 | print(textwrap.dedent(help % (sys.argv[0], os.pathsep))) |
michael@0 | 755 | sys.exit(10) |
michael@0 | 756 | |
michael@0 | 757 | if __name__ == '__main__': |
michael@0 | 758 | _script() |