Changeset 388 for python/vendor/current/Lib/site.py
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Lib/site.py
r2 r388 62 62 import os 63 63 import __builtin__ 64 import traceback 64 65 65 66 # Prefixes for site-packages; add additional prefixes like /usr/local here … … 68 69 # set it to False to disable the feature or True to force the feature 69 70 ENABLE_USER_SITE = None 71 70 72 # for distutils.commands.install 73 # These values are initialized by the getuserbase() and getusersitepackages() 74 # functions, through the main() function when Python starts. 71 75 USER_SITE = None 72 76 USER_BASE = None … … 74 78 75 79 def makepath(*paths): 76 dir = os.path.abspath(os.path.join(*paths)) 80 dir = os.path.join(*paths) 81 try: 82 dir = os.path.abspath(dir) 83 except OSError: 84 pass 77 85 return dir, os.path.normcase(dir) 78 86 … … 85 93 try: 86 94 m.__file__ = os.path.abspath(m.__file__) 87 except AttributeError:88 continue95 except (AttributeError, OSError): 96 pass 89 97 90 98 … … 107 115 return known_paths 108 116 109 # XXX This should not be part of site.py, since it is needed even when110 # using the -S option for Python. See http://www.python.org/sf/586680111 def addbuilddir():112 """Append ./build/lib.<platform> in case we're running in the build dir113 (especially for Guido :-)"""114 from distutils.util import get_platform115 s = "build/lib.%s-%.3s" % (get_platform(), sys.version)116 if hasattr(sys, 'gettotalrefcount'):117 s += '-pydebug'118 s = os.path.join(os.path.dirname(sys.path[-1]), s)119 sys.path.append(s)120 121 117 122 118 def _init_pathinfo(): … … 149 145 return 150 146 with f: 151 for line in f:147 for n, line in enumerate(f): 152 148 if line.startswith("#"): 153 149 continue 154 if line.startswith(("import ", "import\t")): 155 exec line 156 continue 157 line = line.rstrip() 158 dir, dircase = makepath(sitedir, line) 159 if not dircase in known_paths and os.path.exists(dir): 160 sys.path.append(dir) 161 known_paths.add(dircase) 150 try: 151 if line.startswith(("import ", "import\t")): 152 exec line 153 continue 154 line = line.rstrip() 155 dir, dircase = makepath(sitedir, line) 156 if not dircase in known_paths and os.path.exists(dir): 157 sys.path.append(dir) 158 known_paths.add(dircase) 159 except Exception as err: 160 print >>sys.stderr, "Error processing line {:d} of {}:\n".format( 161 n+1, fullname) 162 for record in traceback.format_exception(*sys.exc_info()): 163 for line in record.splitlines(): 164 print >>sys.stderr, ' '+line 165 print >>sys.stderr, "\nRemainder of file ignored" 166 break 162 167 if reset: 163 168 known_paths = None … … 213 218 return True 214 219 220 def getuserbase(): 221 """Returns the `user base` directory path. 222 223 The `user base` directory can be used to store data. If the global 224 variable ``USER_BASE`` is not initialized yet, this function will also set 225 it. 226 """ 227 global USER_BASE 228 if USER_BASE is not None: 229 return USER_BASE 230 from sysconfig import get_config_var 231 USER_BASE = get_config_var('userbase') 232 return USER_BASE 233 234 def getusersitepackages(): 235 """Returns the user-specific site-packages directory path. 236 237 If the global variable ``USER_SITE`` is not initialized yet, this 238 function will also set it. 239 """ 240 global USER_SITE 241 user_base = getuserbase() # this will also set USER_BASE 242 243 if USER_SITE is not None: 244 return USER_SITE 245 246 from sysconfig import get_path 247 import os 248 249 if sys.platform == 'darwin': 250 from sysconfig import get_config_var 251 if get_config_var('PYTHONFRAMEWORK'): 252 USER_SITE = get_path('purelib', 'osx_framework_user') 253 return USER_SITE 254 255 USER_SITE = get_path('purelib', '%s_user' % os.name) 256 return USER_SITE 215 257 216 258 def addusersitepackages(known_paths): … … 219 261 Each user has its own python directory with site-packages in the 220 262 home directory. 221 222 USER_BASE is the root directory for all Python versions 223 224 USER_SITE is the user specific site-packages directory 225 226 USER_SITE/.. can be used for data. 227 """ 228 global USER_BASE, USER_SITE, ENABLE_USER_SITE 229 env_base = os.environ.get("PYTHONUSERBASE", None) 230 231 def joinuser(*args): 232 return os.path.expanduser(os.path.join(*args)) 233 234 #if sys.platform in ('os2emx', 'riscos'): 235 # # Don't know what to put here 236 # USER_BASE = '' 237 # USER_SITE = '' 238 if os.name == "nt": 239 base = os.environ.get("APPDATA") or "~" 240 USER_BASE = env_base if env_base else joinuser(base, "Python") 241 USER_SITE = os.path.join(USER_BASE, 242 "Python" + sys.version[0] + sys.version[2], 243 "site-packages") 244 else: 245 USER_BASE = env_base if env_base else joinuser("~", ".local") 246 USER_SITE = os.path.join(USER_BASE, "lib", 247 "python" + sys.version[:3], 248 "site-packages") 249 250 if ENABLE_USER_SITE and os.path.isdir(USER_SITE): 251 addsitedir(USER_SITE, known_paths) 263 """ 264 # get the per user site-package path 265 # this call will also make sure USER_BASE and USER_SITE are set 266 user_site = getusersitepackages() 267 268 if ENABLE_USER_SITE and os.path.isdir(user_site): 269 addsitedir(user_site, known_paths) 252 270 return known_paths 253 271 254 255 def addsitepackages(known_paths): 256 """Add site-packages (and possibly site-python) to sys.path""" 257 sitedirs = [] 258 seen = [] 272 def getsitepackages(): 273 """Returns a list containing all global site-packages directories 274 (and possibly site-python). 275 276 For each directory present in the global ``PREFIXES``, this function 277 will find its `site-packages` subdirectory depending on the system 278 environment, and will return a list of full paths. 279 """ 280 sitepackages = [] 281 seen = set() 259 282 260 283 for prefix in PREFIXES: 261 284 if not prefix or prefix in seen: 262 285 continue 263 seen.a ppend(prefix)286 seen.add(prefix) 264 287 265 288 if sys.platform in ('os2emx', 'riscos'): 266 site dirs.append(os.path.join(prefix, "Lib", "site-packages"))289 sitepackages.append(os.path.join(prefix, "Lib", "site-packages")) 267 290 elif os.sep == '/': 268 site dirs.append(os.path.join(prefix, "lib",291 sitepackages.append(os.path.join(prefix, "lib", 269 292 "python" + sys.version[:3], 270 293 "site-packages")) 271 site dirs.append(os.path.join(prefix, "lib", "site-python"))294 sitepackages.append(os.path.join(prefix, "lib", "site-python")) 272 295 else: 273 sitedirs.append(prefix) 274 sitedirs.append(os.path.join(prefix, "lib", "site-packages")) 275 296 sitepackages.append(prefix) 297 sitepackages.append(os.path.join(prefix, "lib", "site-packages")) 276 298 if sys.platform == "darwin": 277 299 # for framework builds *only* we add the standard Apple 278 # locations. Currently only per-user, but /Library and 279 # /Network/Library could be added too 280 if 'Python.framework' in prefix: 281 sitedirs.append( 282 os.path.expanduser( 283 os.path.join("~", "Library", "Python", 284 sys.version[:3], "site-packages"))) 285 286 for sitedir in sitedirs: 300 # locations. 301 from sysconfig import get_config_var 302 framework = get_config_var("PYTHONFRAMEWORK") 303 if framework: 304 sitepackages.append( 305 os.path.join("/Library", framework, 306 sys.version[:3], "site-packages")) 307 return sitepackages 308 309 def addsitepackages(known_paths): 310 """Add site-packages (and possibly site-python) to sys.path""" 311 for sitedir in getsitepackages(): 287 312 if os.path.isdir(sitedir): 288 313 addsitedir(sitedir, known_paths) 289 314 290 315 return known_paths 291 292 316 293 317 def setBEGINLIBPATH(): … … 309 333 310 334 def setquit(): 311 """Define new built-ins 'quit' and 'exit'. 312 These are simply strings that display a hint on how to exit. 335 """Define new builtins 'quit' and 'exit'. 336 337 These are objects which make the interpreter exit when called. 338 The repr of each object contains a hint at how it works. 313 339 314 340 """ … … 417 443 418 444 class _Helper(object): 419 """Define the built -in 'help'.445 """Define the builtin 'help'. 420 446 This is a wrapper around pydoc.help (with a twist). 421 447 … … 473 499 except ImportError: 474 500 pass 501 except Exception: 502 if sys.flags.verbose: 503 sys.excepthook(*sys.exc_info()) 504 else: 505 print >>sys.stderr, \ 506 "'import sitecustomize' failed; use -v for traceback" 475 507 476 508 … … 481 513 except ImportError: 482 514 pass 515 except Exception: 516 if sys.flags.verbose: 517 sys.excepthook(*sys.exc_info()) 518 else: 519 print>>sys.stderr, \ 520 "'import usercustomize' failed; use -v for traceback" 483 521 484 522 … … 488 526 abs__file__() 489 527 known_paths = removeduppaths() 490 if (os.name == "posix" and sys.path and491 os.path.basename(sys.path[-1]) == "Modules"):492 addbuilddir()493 528 if ENABLE_USER_SITE is None: 494 529 ENABLE_USER_SITE = check_enableusersite()
Note:
See TracChangeset
for help on using the changeset viewer.