Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pip/_internal/locations/base.py: 56%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

39 statements  

1from __future__ import annotations 

2 

3import functools 

4import os 

5import site 

6import sys 

7import sysconfig 

8 

9from pip._internal.exceptions import InstallationError 

10from pip._internal.utils import appdirs 

11from pip._internal.utils.virtualenv import running_under_virtualenv 

12 

13# Application Directories 

14USER_CACHE_DIR = appdirs.user_cache_dir("pip") 

15 

16# FIXME doesn't account for venv linked to global site-packages 

17site_packages: str = sysconfig.get_path("purelib") 

18 

19 

20def get_major_minor_version() -> str: 

21 """ 

22 Return the major-minor version of the current Python as a string, e.g. 

23 "3.7" or "3.10". 

24 """ 

25 return "{}.{}".format(*sys.version_info) 

26 

27 

28def change_root(new_root: str, pathname: str) -> str: 

29 """Return 'pathname' with 'new_root' prepended. 

30 

31 If 'pathname' is relative, this is equivalent to os.path.join(new_root, pathname). 

32 Otherwise, it requires making 'pathname' relative and then joining the 

33 two, which is tricky on DOS/Windows and Mac OS. 

34 

35 This is borrowed from Python's standard library's distutils module. 

36 """ 

37 if os.name == "posix": 

38 if not os.path.isabs(pathname): 

39 return os.path.join(new_root, pathname) 

40 else: 

41 return os.path.join(new_root, pathname[1:]) 

42 

43 elif os.name == "nt": 

44 (drive, path) = os.path.splitdrive(pathname) 

45 if path[0] == "\\": 

46 path = path[1:] 

47 return os.path.join(new_root, path) 

48 

49 else: 

50 raise InstallationError( 

51 f"Unknown platform: {os.name}\n" 

52 "Can not change root path prefix on unknown platform." 

53 ) 

54 

55 

56def get_src_prefix() -> str: 

57 if running_under_virtualenv(): 

58 src_prefix = os.path.join(sys.prefix, "src") 

59 else: 

60 # FIXME: keep src in cwd for now (it is not a temporary folder) 

61 try: 

62 src_prefix = os.path.join(os.getcwd(), "src") 

63 except OSError: 

64 # In case the current working directory has been renamed or deleted 

65 sys.exit("The folder you are executing pip from can no longer be found.") 

66 

67 # under macOS + virtualenv sys.prefix is not properly resolved 

68 # it is something like /path/to/python/bin/.. 

69 return os.path.abspath(src_prefix) 

70 

71 

72try: 

73 # Use getusersitepackages if this is present, as it ensures that the 

74 # value is initialised properly. 

75 user_site: str | None = site.getusersitepackages() 

76except AttributeError: 

77 user_site = site.USER_SITE 

78 

79 

80@functools.cache 

81def is_osx_framework() -> bool: 

82 return bool(sysconfig.get_config_var("PYTHONFRAMEWORK"))