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 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 7 | In earlier versions of Python (up to 1.5a3), scripts or modules that |
| 8 | needed to use site-specific modules would place ``import site'' |
| 9 | somewhere near the top of their code. Because of the automatic |
| 10 | import, this is no longer necessary (but code that does it still |
| 11 | works). |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 13 | This will append site-specific paths to the module search path. On |
Nick Coghlan | f2b16f3 | 2006-06-12 08:23:02 +0000 | [diff] [blame] | 14 | Unix (including Mac OSX), it starts with sys.prefix and |
| 15 | sys.exec_prefix (if different) and appends |
| 16 | lib/python<version>/site-packages as well as lib/site-python. |
| 17 | On other platforms (such as Windows), it tries each of the |
Nick Coghlan | 3fb55ca | 2006-06-12 08:19:37 +0000 | [diff] [blame] | 18 | prefixes directly, as well as with lib/site-packages appended. The |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 19 | resulting directories, if they exist, are appended to sys.path, and |
| 20 | also inspected for path configuration files. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 22 | A path configuration file is a file whose name has the form |
| 23 | <package>.pth; its contents are additional directories (one per line) |
| 24 | to be added to sys.path. Non-existing directories (or |
| 25 | non-directories) are never added to sys.path; no directory is added to |
| 26 | sys.path more than once. Blank lines and lines beginning with |
Guido van Rossum | facf24b | 2001-12-17 16:07:06 +0000 | [diff] [blame] | 27 | '#' are skipped. Lines starting with 'import' are executed. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 29 | For example, suppose sys.prefix and sys.exec_prefix are set to |
Neal Norwitz | 6e48256 | 2006-08-15 04:59:30 +0000 | [diff] [blame] | 30 | /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] | 31 | with three subdirectories, foo, bar and spam, and two path |
| 32 | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
| 33 | following: |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 34 | |
| 35 | # foo package configuration |
| 36 | foo |
| 37 | bar |
| 38 | bletch |
| 39 | |
| 40 | and bar.pth contains: |
| 41 | |
| 42 | # bar package configuration |
| 43 | bar |
| 44 | |
| 45 | Then the following directories are added to sys.path, in this order: |
| 46 | |
Neal Norwitz | 6e48256 | 2006-08-15 04:59:30 +0000 | [diff] [blame] | 47 | /usr/local/lib/python2.5/site-packages/bar |
| 48 | /usr/local/lib/python2.5/site-packages/foo |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 49 | |
| 50 | Note that bletch is omitted because it doesn't exist; bar precedes foo |
| 51 | because bar.pth comes alphabetically before foo.pth; and spam is |
| 52 | omitted because it is not mentioned in either path configuration file. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 53 | |
| 54 | 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] | 55 | named sitecustomize, which can perform arbitrary additional |
| 56 | site-specific customizations. If this import fails with an |
| 57 | ImportError exception, it is silently ignored. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 59 | """ |
| 60 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 61 | import sys |
| 62 | import os |
| 63 | import __builtin__ |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 64 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 65 | # Prefixes for site-packages; add additional prefixes like /usr/local here |
| 66 | PREFIXES = [sys.prefix, sys.exec_prefix] |
| 67 | # Enable per user site-packages directory |
| 68 | # set it to False to disable the feature or True to force the feature |
| 69 | ENABLE_USER_SITE = None |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 70 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 71 | # for distutils.commands.install |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 72 | # These values are initialized by the getuserbase() and getusersitepackages() |
| 73 | # functions, through the main() function when Python starts. |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 74 | USER_SITE = None |
| 75 | USER_BASE = None |
| 76 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 77 | |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 78 | def makepath(*paths): |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 79 | dir = os.path.abspath(os.path.join(*paths)) |
| 80 | return dir, os.path.normcase(dir) |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 81 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 82 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 83 | def abs__file__(): |
| 84 | """Set all module' __file__ attribute to an absolute path""" |
| 85 | for m in sys.modules.values(): |
Neal Norwitz | 0c46985 | 2006-04-11 07:21:20 +0000 | [diff] [blame] | 86 | if hasattr(m, '__loader__'): |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 87 | continue # don't mess with a PEP 302-supplied __file__ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 88 | try: |
| 89 | m.__file__ = os.path.abspath(m.__file__) |
| 90 | except AttributeError: |
| 91 | continue |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 92 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 93 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 94 | def removeduppaths(): |
| 95 | """ Remove duplicate entries from sys.path along with making them |
| 96 | absolute""" |
| 97 | # This ensures that the initial path provided by the interpreter contains |
| 98 | # only absolute pathnames, even if we're running from the build directory. |
| 99 | L = [] |
| 100 | known_paths = set() |
| 101 | for dir in sys.path: |
| 102 | # Filter out duplicate paths (on case-insensitive file systems also |
| 103 | # if they only differ in case); turn relative paths into absolute |
| 104 | # paths. |
| 105 | dir, dircase = makepath(dir) |
| 106 | if not dircase in known_paths: |
| 107 | L.append(dir) |
| 108 | known_paths.add(dircase) |
| 109 | sys.path[:] = L |
| 110 | return known_paths |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 111 | |
Fred Drake | e80c0d3 | 2002-07-25 20:13:03 +0000 | [diff] [blame] | 112 | # XXX This should not be part of site.py, since it is needed even when |
| 113 | # using the -S option for Python. See http://www.python.org/sf/586680 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 114 | def addbuilddir(): |
| 115 | """Append ./build/lib.<platform> in case we're running in the build dir |
| 116 | (especially for Guido :-)""" |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame^] | 117 | from sysconfig import get_platform |
Jeremy Hylton | 6d58bf6 | 2003-07-18 17:45:33 +0000 | [diff] [blame] | 118 | s = "build/lib.%s-%.3s" % (get_platform(), sys.version) |
Georg Brandl | f00b38e | 2008-01-21 21:19:07 +0000 | [diff] [blame] | 119 | if hasattr(sys, 'gettotalrefcount'): |
| 120 | s += '-pydebug' |
Guido van Rossum | 48eb9cd | 2001-01-19 21:54:59 +0000 | [diff] [blame] | 121 | s = os.path.join(os.path.dirname(sys.path[-1]), s) |
| 122 | sys.path.append(s) |
Guido van Rossum | 48eb9cd | 2001-01-19 21:54:59 +0000 | [diff] [blame] | 123 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 124 | |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 125 | def _init_pathinfo(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 126 | """Return a set containing all existing directory entries from sys.path""" |
| 127 | d = set() |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 128 | for dir in sys.path: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 129 | try: |
| 130 | if os.path.isdir(dir): |
| 131 | dir, dircase = makepath(dir) |
| 132 | d.add(dircase) |
| 133 | except TypeError: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 134 | continue |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 135 | return d |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 136 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 137 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 138 | def addpackage(sitedir, name, known_paths): |
Georg Brandl | 8d76cca | 2007-05-19 18:09:26 +0000 | [diff] [blame] | 139 | """Process a .pth file within the site-packages directory: |
| 140 | For each line in the file, either combine it with sitedir to a path |
| 141 | and add that to known_paths, or execute it if it starts with 'import '. |
| 142 | """ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 143 | if known_paths is None: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 144 | _init_pathinfo() |
| 145 | reset = 1 |
| 146 | else: |
| 147 | reset = 0 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 148 | fullname = os.path.join(sitedir, name) |
| 149 | try: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 150 | f = open(fullname, "rU") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 151 | except IOError: |
| 152 | return |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 153 | with f: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 154 | for line in f: |
| 155 | if line.startswith("#"): |
| 156 | continue |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 157 | if line.startswith(("import ", "import\t")): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 158 | exec line |
| 159 | continue |
| 160 | line = line.rstrip() |
| 161 | dir, dircase = makepath(sitedir, line) |
| 162 | if not dircase in known_paths and os.path.exists(dir): |
| 163 | sys.path.append(dir) |
| 164 | known_paths.add(dircase) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 165 | if reset: |
| 166 | known_paths = None |
| 167 | return known_paths |
| 168 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 169 | |
Brett Cannon | 12f8c4d | 2004-07-09 23:38:18 +0000 | [diff] [blame] | 170 | def addsitedir(sitedir, known_paths=None): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 171 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
| 172 | 'sitedir'""" |
| 173 | if known_paths is None: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 174 | known_paths = _init_pathinfo() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 175 | reset = 1 |
| 176 | else: |
| 177 | reset = 0 |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 178 | sitedir, sitedircase = makepath(sitedir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 179 | if not sitedircase in known_paths: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 180 | sys.path.append(sitedir) # Add path component |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 181 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 182 | names = os.listdir(sitedir) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 183 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 184 | return |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 185 | dotpth = os.extsep + "pth" |
| 186 | names = [name for name in names if name.endswith(dotpth)] |
| 187 | for name in sorted(names): |
| 188 | addpackage(sitedir, name, known_paths) |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 189 | if reset: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 190 | known_paths = None |
| 191 | return known_paths |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 192 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 193 | |
| 194 | def check_enableusersite(): |
| 195 | """Check if user site directory is safe for inclusion |
| 196 | |
Andrew M. Kuchling | 5217d5d | 2008-05-10 17:36:24 +0000 | [diff] [blame] | 197 | The function tests for the command line flag (including environment var), |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 198 | process uid/gid equal to effective uid/gid. |
| 199 | |
| 200 | None: Disabled for security reasons |
| 201 | False: Disabled by user (command line option) |
| 202 | True: Safe and enabled |
| 203 | """ |
| 204 | if sys.flags.no_user_site: |
| 205 | return False |
| 206 | |
| 207 | if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
| 208 | # check process uid == effective uid |
| 209 | if os.geteuid() != os.getuid(): |
| 210 | return None |
| 211 | if hasattr(os, "getgid") and hasattr(os, "getegid"): |
| 212 | # check process gid == effective gid |
| 213 | if os.getegid() != os.getgid(): |
| 214 | return None |
| 215 | |
| 216 | return True |
| 217 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 218 | def getuserbase(): |
| 219 | """Returns the `user base` directory path. |
| 220 | |
| 221 | The `user base` directory can be used to store data. If the global |
| 222 | variable ``USER_BASE`` is not initialized yet, this function will also set |
| 223 | it. |
| 224 | """ |
| 225 | global USER_BASE |
| 226 | if USER_BASE is not None: |
| 227 | return USER_BASE |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame^] | 228 | from sysconfig import get_config_var |
| 229 | USER_BASE = get_config_var('userbase') |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 230 | return USER_BASE |
| 231 | |
| 232 | def getusersitepackages(): |
| 233 | """Returns the user-specific site-packages directory path. |
| 234 | |
| 235 | If the global variable ``USER_SITE`` is not initialized yet, this |
| 236 | function will also set it. |
| 237 | """ |
| 238 | global USER_SITE |
| 239 | user_base = getuserbase() # this will also set USER_BASE |
| 240 | |
| 241 | if USER_SITE is not None: |
| 242 | return USER_SITE |
| 243 | |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame^] | 244 | from sysconfig import get_path |
| 245 | import os |
| 246 | USER_SITE = get_path('purelib', '%s_user' % os.name) |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 247 | return USER_SITE |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 248 | |
| 249 | def addusersitepackages(known_paths): |
| 250 | """Add a per user site-package to sys.path |
| 251 | |
| 252 | Each user has its own python directory with site-packages in the |
| 253 | home directory. |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 254 | """ |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 255 | # get the per user site-package path |
| 256 | # this call will also make sure USER_BASE and USER_SITE are set |
| 257 | user_site = getusersitepackages() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 258 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 259 | if ENABLE_USER_SITE and os.path.isdir(user_site): |
| 260 | addsitedir(user_site, known_paths) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 261 | return known_paths |
| 262 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 263 | def getsitepackages(): |
| 264 | """Returns a list containing all global site-packages directories |
| 265 | (and possibly site-python). |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 266 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 267 | For each directory present in the global ``PREFIXES``, this function |
| 268 | will find its `site-packages` subdirectory depending on the system |
| 269 | environment, and will return a list of full paths. |
| 270 | """ |
| 271 | sitepackages = [] |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 272 | seen = [] |
| 273 | |
| 274 | for prefix in PREFIXES: |
| 275 | if not prefix or prefix in seen: |
| 276 | continue |
| 277 | seen.append(prefix) |
| 278 | |
| 279 | if sys.platform in ('os2emx', 'riscos'): |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 280 | sitepackages.append(os.path.join(prefix, "Lib", "site-packages")) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 281 | elif os.sep == '/': |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 282 | sitepackages.append(os.path.join(prefix, "lib", |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 283 | "python" + sys.version[:3], |
| 284 | "site-packages")) |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 285 | sitepackages.append(os.path.join(prefix, "lib", "site-python")) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 286 | else: |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 287 | sitepackages.append(prefix) |
| 288 | sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 289 | if sys.platform == "darwin": |
| 290 | # for framework builds *only* we add the standard Apple |
Ronald Oussoren | e0154ed | 2009-03-30 23:10:35 +0000 | [diff] [blame] | 291 | # locations. |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 292 | if 'Python.framework' in prefix: |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 293 | sitepackages.append( |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 294 | os.path.expanduser( |
| 295 | os.path.join("~", "Library", "Python", |
| 296 | sys.version[:3], "site-packages"))) |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 297 | sitepackages.append( |
Ronald Oussoren | e0154ed | 2009-03-30 23:10:35 +0000 | [diff] [blame] | 298 | os.path.join("/Library", "Python", |
| 299 | sys.version[:3], "site-packages")) |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 300 | return sitepackages |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 301 | |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 302 | def addsitepackages(known_paths): |
| 303 | """Add site-packages (and possibly site-python) to sys.path""" |
| 304 | for sitedir in getsitepackages(): |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 305 | if os.path.isdir(sitedir): |
| 306 | addsitedir(sitedir, known_paths) |
| 307 | |
| 308 | return known_paths |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 309 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 310 | def setBEGINLIBPATH(): |
| 311 | """The OS/2 EMX port has optional extension modules that do double duty |
| 312 | as DLLs (and must use the .DLL file extension) for other extensions. |
| 313 | The library search path needs to be amended so these will be found |
| 314 | during module import. Use BEGINLIBPATH so that these are at the start |
| 315 | of the library search path. |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 316 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 317 | """ |
Andrew MacIntyre | 2e8a6e0 | 2003-12-02 12:27:25 +0000 | [diff] [blame] | 318 | dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload") |
| 319 | libpath = os.environ['BEGINLIBPATH'].split(';') |
| 320 | if libpath[-1]: |
| 321 | libpath.append(dllpath) |
| 322 | else: |
| 323 | libpath[-1] = dllpath |
| 324 | os.environ['BEGINLIBPATH'] = ';'.join(libpath) |
| 325 | |
| 326 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 327 | def setquit(): |
| 328 | """Define new built-ins 'quit' and 'exit'. |
| 329 | These are simply strings that display a hint on how to exit. |
Guido van Rossum | d89fa0c | 1998-08-07 18:01:14 +0000 | [diff] [blame] | 330 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 331 | """ |
| 332 | if os.sep == ':': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 333 | eof = 'Cmd-Q' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 334 | elif os.sep == '\\': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 335 | eof = 'Ctrl-Z plus Return' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 336 | else: |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 337 | eof = 'Ctrl-D (i.e. EOF)' |
Tim Peters | 88ca467 | 2006-03-10 23:39:56 +0000 | [diff] [blame] | 338 | |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 339 | class Quitter(object): |
| 340 | def __init__(self, name): |
| 341 | self.name = name |
| 342 | def __repr__(self): |
| 343 | return 'Use %s() or %s to exit' % (self.name, eof) |
| 344 | def __call__(self, code=None): |
Kurt B. Kaiser | d112bc7 | 2006-08-16 05:01:42 +0000 | [diff] [blame] | 345 | # Shells like IDLE catch the SystemExit, but listen when their |
| 346 | # stdin wrapper is closed. |
| 347 | try: |
| 348 | sys.stdin.close() |
| 349 | except: |
| 350 | pass |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 351 | raise SystemExit(code) |
| 352 | __builtin__.quit = Quitter('quit') |
| 353 | __builtin__.exit = Quitter('exit') |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 354 | |
| 355 | |
| 356 | class _Printer(object): |
| 357 | """interactive prompt objects for printing the license text, a list of |
| 358 | contributors and the copyright notice.""" |
| 359 | |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 360 | MAXLINES = 23 |
| 361 | |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 362 | def __init__(self, name, data, files=(), dirs=()): |
| 363 | self.__name = name |
| 364 | self.__data = data |
| 365 | self.__files = files |
| 366 | self.__dirs = dirs |
| 367 | self.__lines = None |
| 368 | |
| 369 | def __setup(self): |
| 370 | if self.__lines: |
| 371 | return |
| 372 | data = None |
| 373 | for dir in self.__dirs: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 374 | for filename in self.__files: |
| 375 | filename = os.path.join(dir, filename) |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 376 | try: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 377 | fp = file(filename, "rU") |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 378 | data = fp.read() |
| 379 | fp.close() |
| 380 | break |
| 381 | except IOError: |
| 382 | pass |
| 383 | if data: |
| 384 | break |
| 385 | if not data: |
| 386 | data = self.__data |
| 387 | self.__lines = data.split('\n') |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 388 | self.__linecnt = len(self.__lines) |
| 389 | |
| 390 | def __repr__(self): |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 391 | self.__setup() |
| 392 | if len(self.__lines) <= self.MAXLINES: |
| 393 | return "\n".join(self.__lines) |
| 394 | else: |
| 395 | return "Type %s() to see the full %s text" % ((self.__name,)*2) |
| 396 | |
| 397 | def __call__(self): |
| 398 | self.__setup() |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 399 | prompt = 'Hit Return for more, or q (and Return) to quit: ' |
| 400 | lineno = 0 |
| 401 | while 1: |
| 402 | try: |
| 403 | for i in range(lineno, lineno + self.MAXLINES): |
| 404 | print self.__lines[i] |
| 405 | except IndexError: |
| 406 | break |
| 407 | else: |
| 408 | lineno += self.MAXLINES |
| 409 | key = None |
| 410 | while key is None: |
| 411 | key = raw_input(prompt) |
| 412 | if key not in ('', 'q'): |
| 413 | key = None |
| 414 | if key == 'q': |
| 415 | break |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 416 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 417 | def setcopyright(): |
| 418 | """Set 'copyright' and 'credits' in __builtin__""" |
| 419 | __builtin__.copyright = _Printer("copyright", sys.copyright) |
| 420 | if sys.platform[:4] == 'java': |
| 421 | __builtin__.credits = _Printer( |
| 422 | "credits", |
| 423 | "Jython is maintained by the Jython developers (www.jython.org).") |
| 424 | else: |
| 425 | __builtin__.credits = _Printer("credits", """\ |
| 426 | Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
| 427 | for supporting Python development. See www.python.org for more information.""") |
| 428 | here = os.path.dirname(os.__file__) |
| 429 | __builtin__.license = _Printer( |
| 430 | "license", "See http://www.python.org/%.3s/license.html" % sys.version, |
| 431 | ["LICENSE.txt", "LICENSE"], |
| 432 | [os.path.join(here, os.pardir), here, os.curdir]) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 433 | |
| 434 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 435 | class _Helper(object): |
| 436 | """Define the built-in 'help'. |
| 437 | This is a wrapper around pydoc.help (with a twist). |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 438 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 439 | """ |
| 440 | |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 441 | def __repr__(self): |
| 442 | return "Type help() for interactive help, " \ |
| 443 | "or help(object) for help about object." |
| 444 | def __call__(self, *args, **kwds): |
| 445 | import pydoc |
| 446 | return pydoc.help(*args, **kwds) |
| 447 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 448 | def sethelper(): |
| 449 | __builtin__.help = _Helper() |
| 450 | |
| 451 | def aliasmbcs(): |
| 452 | """On Windows, some default encodings are not provided by Python, |
| 453 | while they are always available as "mbcs" in each locale. Make |
| 454 | them usable by aliasing to "mbcs" in such a case.""" |
| 455 | if sys.platform == 'win32': |
| 456 | import locale, codecs |
| 457 | enc = locale.getdefaultlocale()[1] |
| 458 | if enc.startswith('cp'): # "cp***" ? |
| 459 | try: |
| 460 | codecs.lookup(enc) |
| 461 | except LookupError: |
| 462 | import encodings |
| 463 | encodings._cache[enc] = encodings._unknown |
| 464 | encodings.aliases.aliases[enc] = 'mbcs' |
| 465 | |
| 466 | def setencoding(): |
| 467 | """Set the string encoding used by the Unicode implementation. The |
| 468 | default is 'ascii', but if you're willing to experiment, you can |
| 469 | change this.""" |
| 470 | encoding = "ascii" # Default value set by _PyUnicode_Init() |
| 471 | if 0: |
| 472 | # Enable to support locale aware default string encodings. |
| 473 | import locale |
| 474 | loc = locale.getdefaultlocale() |
| 475 | if loc[1]: |
| 476 | encoding = loc[1] |
| 477 | if 0: |
| 478 | # Enable to switch off string to Unicode coercion and implicit |
| 479 | # Unicode to string conversion. |
| 480 | encoding = "undefined" |
| 481 | if encoding != "ascii": |
| 482 | # On Non-Unicode builds this will raise an AttributeError... |
| 483 | sys.setdefaultencoding(encoding) # Needs Python Unicode build ! |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 484 | |
| 485 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 486 | def execsitecustomize(): |
| 487 | """Run custom site specific code, if available.""" |
| 488 | try: |
| 489 | import sitecustomize |
| 490 | except ImportError: |
| 491 | pass |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 492 | |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 493 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 494 | def execusercustomize(): |
| 495 | """Run custom user specific code, if available.""" |
| 496 | try: |
| 497 | import usercustomize |
| 498 | except ImportError: |
| 499 | pass |
| 500 | |
| 501 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 502 | def main(): |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 503 | global ENABLE_USER_SITE |
| 504 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 505 | abs__file__() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 506 | known_paths = removeduppaths() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 507 | if (os.name == "posix" and sys.path and |
| 508 | os.path.basename(sys.path[-1]) == "Modules"): |
| 509 | addbuilddir() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 510 | if ENABLE_USER_SITE is None: |
| 511 | ENABLE_USER_SITE = check_enableusersite() |
| 512 | known_paths = addusersitepackages(known_paths) |
| 513 | known_paths = addsitepackages(known_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 514 | if sys.platform == 'os2emx': |
| 515 | setBEGINLIBPATH() |
| 516 | setquit() |
| 517 | setcopyright() |
| 518 | sethelper() |
| 519 | aliasmbcs() |
| 520 | setencoding() |
| 521 | execsitecustomize() |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 522 | if ENABLE_USER_SITE: |
| 523 | execusercustomize() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 524 | # Remove sys.setdefaultencoding() so that users cannot change the |
| 525 | # encoding after initialization. The test for presence is needed when |
| 526 | # this module is run as a script, because this code is executed twice. |
| 527 | if hasattr(sys, "setdefaultencoding"): |
| 528 | del sys.setdefaultencoding |
Marc-André Lemburg | 990bbe9 | 2000-06-07 09:12:09 +0000 | [diff] [blame] | 529 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 530 | main() |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 531 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 532 | def _script(): |
| 533 | help = """\ |
| 534 | %s [--user-base] [--user-site] |
| 535 | |
| 536 | Without arguments print some useful information |
| 537 | With arguments print the value of USER_BASE and/or USER_SITE separated |
| 538 | by '%s'. |
| 539 | |
| 540 | Exit codes with --user-base or --user-site: |
| 541 | 0 - user site directory is enabled |
Christian Heimes | 17433d2 | 2008-05-09 12:19:09 +0000 | [diff] [blame] | 542 | 1 - user site directory is disabled by user |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 543 | 2 - uses site directory is disabled by super user |
| 544 | or for security reasons |
| 545 | >2 - unknown error |
| 546 | """ |
| 547 | args = sys.argv[1:] |
| 548 | if not args: |
| 549 | print "sys.path = [" |
| 550 | for dir in sys.path: |
| 551 | print " %r," % (dir,) |
| 552 | print "]" |
| 553 | print "USER_BASE: %r (%s)" % (USER_BASE, |
| 554 | "exists" if os.path.isdir(USER_BASE) else "doesn't exist") |
| 555 | print "USER_SITE: %r (%s)" % (USER_SITE, |
| 556 | "exists" if os.path.isdir(USER_SITE) else "doesn't exist") |
| 557 | print "ENABLE_USER_SITE: %r" % ENABLE_USER_SITE |
| 558 | sys.exit(0) |
| 559 | |
| 560 | buffer = [] |
| 561 | if '--user-base' in args: |
| 562 | buffer.append(USER_BASE) |
| 563 | if '--user-site' in args: |
| 564 | buffer.append(USER_SITE) |
| 565 | |
| 566 | if buffer: |
| 567 | print os.pathsep.join(buffer) |
| 568 | if ENABLE_USER_SITE: |
| 569 | sys.exit(0) |
| 570 | elif ENABLE_USER_SITE is False: |
| 571 | sys.exit(1) |
| 572 | elif ENABLE_USER_SITE is None: |
| 573 | sys.exit(2) |
| 574 | else: |
| 575 | sys.exit(3) |
| 576 | else: |
| 577 | import textwrap |
| 578 | print textwrap.dedent(help % (sys.argv[0], os.pathsep)) |
| 579 | sys.exit(10) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 580 | |
| 581 | if __name__ == '__main__': |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 582 | _script() |