paperwork: fix installing translations (#370379)
[NixPkgs.git] / pkgs / applications / networking / browsers / firefox / mozbz-1935621-attachment-9442305.patch
blobbbf9b2fa103650c71872a2735014603bd87ca9b4
1 diff --git a/python/mach/mach/site.py b/python/mach/mach/site.py
2 --- a/python/mach/mach/site.py
3 +++ b/python/mach/mach/site.py
4 @@ -15,10 +15,11 @@
5 import site
6 import subprocess
7 import sys
8 import sysconfig
9 import tempfile
10 +import warnings
11 from contextlib import contextmanager
12 from pathlib import Path
13 from typing import Callable, Optional
15 from mach.requirements import (
16 @@ -817,37 +818,79 @@
18 class PythonVirtualenv:
19 """Calculates paths of interest for general python virtual environments"""
21 def __init__(self, prefix):
22 - if _is_windows:
23 - self.bin_path = os.path.join(prefix, "Scripts")
24 - self.python_path = os.path.join(self.bin_path, "python.exe")
25 - else:
26 - self.bin_path = os.path.join(prefix, "bin")
27 - self.python_path = os.path.join(self.bin_path, "python")
28 self.prefix = os.path.realpath(prefix)
29 + self.paths = self._get_sysconfig_paths(self.prefix)
31 - @functools.lru_cache(maxsize=None)
32 - def resolve_sysconfig_packages_path(self, sysconfig_path):
33 - # macOS uses a different default sysconfig scheme based on whether it's using the
34 - # system Python or running in a virtualenv.
35 - # Manually define the scheme (following the implementation in
36 - # "sysconfig._get_default_scheme()") so that we're always following the
37 - # code path for a virtualenv directory structure.
38 - if os.name == "posix":
39 - scheme = "posix_prefix"
40 - else:
41 - scheme = os.name
42 + # Name of the Python executable to use in virtual environments.
43 + # An executable with the same name as sys.executable might not exist in
44 + # virtual environments. An executable with 'python' as the steam —
45 + # without version numbers or ABI flags — will always be present in
46 + # virtual environments, so we use that.
47 + python_exe_name = "python" + sysconfig.get_config_var("EXE")
49 + self.bin_path = self.paths["scripts"]
50 + self.python_path = os.path.join(self.bin_path, python_exe_name)
52 - sysconfig_paths = sysconfig.get_paths(scheme)
53 - data_path = Path(sysconfig_paths["data"])
54 - path = Path(sysconfig_paths[sysconfig_path])
55 - relative_path = path.relative_to(data_path)
56 + @staticmethod
57 + def _get_sysconfig_paths(prefix):
58 + """Calculate the sysconfig paths of a virtual environment in the given prefix.
60 - # Path to virtualenv's "site-packages" directory for provided sysconfig path
61 - return os.path.normpath(os.path.normcase(Path(self.prefix) / relative_path))
62 + The virtual environment MUST be using the same Python distribution as us.
63 + """
64 + # Determine the sysconfig scheme used in virtual environments
65 + if "venv" in sysconfig.get_scheme_names():
66 + # A 'venv' scheme was added in Python 3.11 to allow users to
67 + # calculate the paths for a virtual environment, since the default
68 + # scheme may not always be the same as used on virtual environments.
69 + # Some common examples are the system Python distributed by macOS,
70 + # Debian, and Fedora.
71 + # For more information, see https://github.com/python/cpython/issues/89576
72 + venv_scheme = "venv"
73 + elif os.name == "nt":
74 + # We know that before the 'venv' scheme was added, on Windows,
75 + # the 'nt' scheme was used in virtual environments.
76 + venv_scheme = "nt"
77 + elif os.name == "posix":
78 + # We know that before the 'venv' scheme was added, on POSIX,
79 + # the 'posix_prefix' scheme was used in virtual environments.
80 + venv_scheme = "posix_prefix"
81 + else:
82 + # This should never happen with upstream Python, as the 'venv'
83 + # scheme should always be available on >=3.11, and no other
84 + # platforms are supported by the upstream on older Python versions.
85 + #
86 + # Since the 'venv' scheme isn't available, and we have no knowledge
87 + # of this platform/distribution, fallback to the default scheme.
88 + #
89 + # Hitting this will likely be the result of running a custom Python
90 + # distribution targetting a platform that is not supported by the
91 + # upstream.
92 + # In this case, unless the Python vendor patched the Python
93 + # distribution in such a way as the default scheme may not always be
94 + # the same scheme, using the default scheme should be correct.
95 + # If the vendor did patch Python as such, to work around this issue,
96 + # I would recommend them to define a 'venv' scheme that matches
97 + # the layout used on virtual environments in their Python distribution.
98 + # (rec. signed Filipe Laíns — upstream sysconfig maintainer)
99 + venv_scheme = sysconfig.get_default_scheme()
100 + warnings.warn(
101 + f"Unknown platform '{os.name}', using the default install scheme '{venv_scheme}'. "
102 + "If this is incorrect, please ask your Python vendor to add a 'venv' sysconfig scheme "
103 + "(see https://github.com/python/cpython/issues/89576, or check the code comment).",
104 + stacklevel=2,
106 + # Build the sysconfig config_vars dictionary for the virtual environment.
107 + venv_vars = sysconfig.get_config_vars().copy()
108 + venv_vars["base"] = venv_vars["platbase"] = prefix
109 + # Get sysconfig paths for the virtual environment.
110 + return sysconfig.get_paths(venv_scheme, vars=venv_vars)
112 + def resolve_sysconfig_packages_path(self, sysconfig_path):
113 + return self.paths[sysconfig_path]
115 def site_packages_dirs(self):
116 dirs = []
117 if sys.platform.startswith("win"):
118 dirs.append(os.path.normpath(os.path.normcase(self.prefix)))