summaryrefslogtreecommitdiffstats
path: root/src/build/env.py
blob: b4a90a9ee8a4c9a4a991d1dd377379f62060205c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
Creates and manages isolated build environments.
"""
import abc
import functools
import logging
import os
import platform
import shutil
import subprocess
import sys
import sysconfig
import tempfile
import warnings

from types import TracebackType
from typing import Callable, Collection, List, Optional, Tuple, Type

import build


try:
    import virtualenv
except ModuleNotFoundError:
    virtualenv = None


_logger = logging.getLogger(__name__)


class IsolatedEnv(metaclass=abc.ABCMeta):
    """Abstract base of isolated build environments, as required by the build project."""

    @property
    @abc.abstractmethod
    def executable(self) -> str:
        """The executable of the isolated build environment."""
        raise NotImplementedError

    @property
    @abc.abstractmethod
    def scripts_dir(self) -> str:
        """The scripts directory of the isolated build environment."""
        raise NotImplementedError

    @abc.abstractmethod
    def install(self, requirements: Collection[str]) -> None:
        """
        Install packages from PEP 508 requirements in the isolated build environment.

        :param requirements: PEP 508 requirements
        """
        raise NotImplementedError


@functools.lru_cache(maxsize=None)
def _should_use_virtualenv() -> bool:
    import packaging.requirements

    # virtualenv might be incompatible if it was installed separately
    # from build. This verifies that virtualenv and all of its
    # dependencies are installed as specified by build.
    return virtualenv is not None and not any(
        packaging.requirements.Requirement(d[1]).name == 'virtualenv'
        for d in build.check_dependency('build[virtualenv]')
        if len(d) > 1
    )


def _subprocess(cmd: List[str]) -> None:
    """Invoke subprocess and output stdout and stderr if it fails."""
    try:
        subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        print(e.output.decode(), end='', file=sys.stderr)
        raise e


class IsolatedEnvBuilder:
    """Builder object for isolated environments."""

    def __init__(self) -> None:
        self._path: Optional[str] = None

    def __enter__(self) -> IsolatedEnv:
        """
        Create an isolated build environment.

        :return: The isolated build environment
        """
        # Call ``realpath`` to prevent spurious warning from being emitted
        # that the venv location has changed on Windows. The username is
        # DOS-encoded in the output of tempfile - the location is the same
        # but the representation of it is different, which confuses venv.
        # Ref: https://bugs.python.org/issue46171
        self._path = os.path.realpath(tempfile.mkdtemp(prefix='build-env-'))
        try:
            # use virtualenv when available (as it's faster than venv)
            if _should_use_virtualenv():
                self.log('Creating virtualenv isolated environment...')
                executable, scripts_dir = _create_isolated_env_virtualenv(self._path)
            else:
                self.log('Creating venv isolated environment...')
                executable, scripts_dir = _create_isolated_env_venv(self._path)
            return _IsolatedEnvVenvPip(
                path=self._path,
                python_executable=executable,
                scripts_dir=scripts_dir,
                log=self.log,
            )
        except Exception:  # cleanup folder if creation fails
            self.__exit__(*sys.exc_info())
            raise

    def __exit__(
        self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
    ) -> None:
        """
        Delete the created isolated build environment.

        :param exc_type: The type of exception raised (if any)
        :param exc_val: The value of exception raised (if any)
        :param exc_tb: The traceback of exception raised (if any)
        """
        if self._path is not None and os.path.exists(self._path):  # in case the user already deleted skip remove
            shutil.rmtree(self._path)

    @staticmethod
    def log(message: str) -> None:
        """
        Prints message

        The default implementation uses the logging module but this function can be
        overwritten by users to have a different implementation.

        :param msg: Message to output
        """
        if sys.version_info >= (3, 8):
            _logger.log(logging.INFO, message, stacklevel=2)
        else:
            _logger.log(logging.INFO, message)


class _IsolatedEnvVenvPip(IsolatedEnv):
    """
    Isolated build environment context manager

    Non-standard paths injected directly to sys.path will still be passed to the environment.
    """

    def __init__(
        self,
        path: str,
        python_executable: str,
        scripts_dir: str,
        log: Callable[[str], None],
    ) -> None:
        """
        :param path: The path where the environment exists
        :param python_executable: The python executable within the environment
        :param log: Log function
        """
        self._path = path
        self._python_executable = python_executable
        self._scripts_dir = scripts_dir
        self._log = log

    @property
    def path(self) -> str:
        """The location of the isolated build environment."""
        return self._path

    @property
    def executable(self) -> str:
        """The python executable of the isolated build environment."""
        return self._python_executable

    @property
    def scripts_dir(self) -> str:
        return self._scripts_dir

    def install(self, requirements: Collection[str]) -> None:
        """
        Install packages from PEP 508 requirements in the isolated build environment.

        :param requirements: PEP 508 requirement specification to install

        :note: Passing non-PEP 508 strings will result in undefined behavior, you *should not* rely on it. It is
               merely an implementation detail, it may change any time without warning.
        """
        if not requirements:
            return

        self._log('Installing packages in isolated environment... ({})'.format(', '.join(sorted(requirements))))

        # pip does not honour environment markers in command line arguments
        # but it does for requirements from a file
        with tempfile.NamedTemporaryFile('w+', prefix='build-reqs-', suffix='.txt', delete=False) as req_file:
            req_file.write(os.linesep.join(requirements))
        try:
            cmd = [
                self.executable,
                '-Im',
                'pip',
                'install',
                '--use-pep517',
                '--no-warn-script-location',
                '-r',
                os.path.abspath(req_file.name),
            ]
            _subprocess(cmd)
        finally:
            os.unlink(req_file.name)


def _create_isolated_env_virtualenv(path: str) -> Tuple[str, str]:
    """
    We optionally can use the virtualenv package to provision a virtual environment.

    :param path: The path where to create the isolated build environment
    :return: The Python executable and script folder
    """
    cmd = [str(path), '--no-setuptools', '--no-wheel', '--activators', '']
    result = virtualenv.cli_run(cmd, setup_logging=False)
    executable = str(result.creator.exe)
    script_dir = str(result.creator.script_dir)
    return executable, script_dir


@functools.lru_cache(maxsize=None)
def _fs_supports_symlink() -> bool:
    """Return True if symlinks are supported"""
    # Using definition used by venv.main()
    if not sys.platform.startswith('win'):
        return True

    # Windows may support symlinks (setting in Windows 10)
    with tempfile.NamedTemporaryFile(prefix='build-symlink-') as tmp_file:
        dest = f'{tmp_file}-b'
        try:
            os.symlink(tmp_file.name, dest)
            os.unlink(dest)
            return True
        except (OSError, NotImplementedError, AttributeError):
            return False


def _create_isolated_env_venv(path: str) -> Tuple[str, str]:
    """
    On Python 3 we use the venv package from the standard library.

    :param path: The path where to create the isolated build environment
    :return: The Python executable and script folder
    """
    import venv

    import packaging.version

    if sys.version_info < (3, 8):
        import importlib_metadata as metadata
    else:
        from importlib import metadata

    symlinks = _fs_supports_symlink()
    try:
        with warnings.catch_warnings():
            if sys.version_info[:3] == (3, 11, 0):
                warnings.filterwarnings('ignore', 'check_home argument is deprecated and ignored.', DeprecationWarning)
            venv.EnvBuilder(with_pip=True, symlinks=symlinks).create(path)
    except subprocess.CalledProcessError as exc:
        raise build.FailedProcessError(exc, 'Failed to create venv. Maybe try installing virtualenv.') from None

    executable, script_dir, purelib = _find_executable_and_scripts(path)

    # Get the version of pip in the environment
    pip_distribution = next(iter(metadata.distributions(name='pip', path=[purelib])))  # type: ignore[no-untyped-call]
    current_pip_version = packaging.version.Version(pip_distribution.version)

    if platform.system() == 'Darwin' and int(platform.mac_ver()[0].split('.')[0]) >= 11:
        # macOS 11+ name scheme change requires 20.3. Intel macOS 11.0 can be told to report 10.16 for backwards
        # compatibility; but that also fixes earlier versions of pip so this is only needed for 11+.
        is_apple_silicon_python = platform.machine() != 'x86_64'
        minimum_pip_version = '21.0.1' if is_apple_silicon_python else '20.3.0'
    else:
        # PEP-517 and manylinux1 was first implemented in 19.1
        minimum_pip_version = '19.1.0'

    if current_pip_version < packaging.version.Version(minimum_pip_version):
        _subprocess([executable, '-m', 'pip', 'install', f'pip>={minimum_pip_version}'])

    # Avoid the setuptools from ensurepip to break the isolation
    _subprocess([executable, '-m', 'pip', 'uninstall', 'setuptools', '-y'])
    return executable, script_dir


def _find_executable_and_scripts(path: str) -> Tuple[str, str, str]:
    """
    Detect the Python executable and script folder of a virtual environment.

    :param path: The location of the virtual environment
    :return: The Python executable, script folder, and purelib folder
    """
    config_vars = sysconfig.get_config_vars().copy()  # globally cached, copy before altering it
    config_vars['base'] = path
    scheme_names = sysconfig.get_scheme_names()
    if 'venv' in scheme_names:
        # Python distributors with custom default installation scheme can set a
        # scheme that can't be used to expand the paths in a venv.
        # This can happen if build itself is not installed in a venv.
        # The distributors are encouraged to set a "venv" scheme to be used for this.
        # See https://bugs.python.org/issue45413
        # and https://github.com/pypa/virtualenv/issues/2208
        paths = sysconfig.get_paths(scheme='venv', vars=config_vars)
    elif 'posix_local' in scheme_names:
        # The Python that ships on Debian/Ubuntu varies the default scheme to
        # install to /usr/local
        # But it does not (yet) set the "venv" scheme.
        # If we're the Debian "posix_local" scheme is available, but "venv"
        # is not, we use "posix_prefix" instead which is venv-compatible there.
        paths = sysconfig.get_paths(scheme='posix_prefix', vars=config_vars)
    elif 'osx_framework_library' in scheme_names:
        # The Python that ships with the macOS developer tools varies the
        # default scheme depending on whether the ``sys.prefix`` is part of a framework.
        # But it does not (yet) set the "venv" scheme.
        # If the Apple-custom "osx_framework_library" scheme is available but "venv"
        # is not, we use "posix_prefix" instead which is venv-compatible there.
        paths = sysconfig.get_paths(scheme='posix_prefix', vars=config_vars)
    else:
        paths = sysconfig.get_paths(vars=config_vars)
    executable = os.path.join(paths['scripts'], 'python.exe' if sys.platform.startswith('win') else 'python')
    if not os.path.exists(executable):
        raise RuntimeError(f'Virtual environment creation failed, executable {executable} missing')

    return executable, paths['scripts'], paths['purelib']


__all__ = [
    'IsolatedEnvBuilder',
    'IsolatedEnv',
]