(
cls,
isolated_sys_path, # type: IsolatedSysPath
inherit_path, # type: InheritPath.Value
)
| 377 | |
| 378 | @classmethod |
| 379 | def minimum_sys_path( |
| 380 | cls, |
| 381 | isolated_sys_path, # type: IsolatedSysPath |
| 382 | inherit_path, # type: InheritPath.Value |
| 383 | ): |
| 384 | # type: (...) -> Tuple[List[str], Mapping[str, Any]] |
| 385 | scrub_paths = OrderedSet() # type: OrderedSet[str] |
| 386 | site_paths = OrderedSet() # type: OrderedSet[str] |
| 387 | user_site_paths = OrderedSet() # type: OrderedSet[str] |
| 388 | |
| 389 | def all_paths(path): |
| 390 | # type: (Optional[str]) -> Iterable[str] |
| 391 | if path is None: |
| 392 | return () |
| 393 | return path, os.path.realpath(path) |
| 394 | |
| 395 | for path_element in sys.path: |
| 396 | if path_element not in isolated_sys_path: |
| 397 | TRACER.log("Tainted path element: %s" % path_element) |
| 398 | site_paths.update(all_paths(path_element)) |
| 399 | else: |
| 400 | TRACER.log("Not a tainted path element: %s" % path_element, V=2) |
| 401 | |
| 402 | user_site_paths.update(all_paths(USER_SITE)) |
| 403 | |
| 404 | if inherit_path == InheritPath.FALSE: |
| 405 | scrub_paths = OrderedSet(site_paths) |
| 406 | scrub_paths.update(user_site_paths) |
| 407 | for path in user_site_paths: |
| 408 | TRACER.log("Scrubbing from user site: %s" % path) |
| 409 | for path in site_paths: |
| 410 | TRACER.log("Scrubbing from site-packages: %s" % path) |
| 411 | |
| 412 | scrubbed_sys_path = list(OrderedSet(sys.path) - scrub_paths) |
| 413 | |
| 414 | pythonpath = cls.unstash_pythonpath() |
| 415 | if pythonpath is not None: |
| 416 | original_pythonpath = pythonpath.split(os.pathsep) |
| 417 | user_pythonpath = list(OrderedSet(original_pythonpath) - set(sys.path)) |
| 418 | if original_pythonpath == user_pythonpath: |
| 419 | TRACER.log("Unstashed PYTHONPATH of %s" % pythonpath, V=2) |
| 420 | else: |
| 421 | TRACER.log( |
| 422 | "Extracted user PYTHONPATH of %s from unstashed PYTHONPATH of %s" |
| 423 | % (os.pathsep.join(user_pythonpath), pythonpath), |
| 424 | V=2, |
| 425 | ) |
| 426 | |
| 427 | if inherit_path == InheritPath.FALSE: |
| 428 | for path in user_pythonpath: |
| 429 | TRACER.log("Scrubbing user PYTHONPATH element: %s" % path) |
| 430 | elif inherit_path == InheritPath.PREFER: |
| 431 | TRACER.log("Prepending user PYTHONPATH: %s" % os.pathsep.join(user_pythonpath)) |
| 432 | scrubbed_sys_path = user_pythonpath + scrubbed_sys_path |
| 433 | elif inherit_path == InheritPath.FALLBACK: |
| 434 | TRACER.log("Appending user PYTHONPATH: %s" % os.pathsep.join(user_pythonpath)) |
| 435 | scrubbed_sys_path = scrubbed_sys_path + user_pythonpath |
| 436 |
no test coverage detected