def project_wheel_metadata( srcdir: build.PathType, isolated: bool = True, ) -> 'importlib_metadata.PackageMetadata': """ Return the wheel metadata for a project. Uses the ``prepare_metadata_for_build_wheel`` hook if available, otherwise ``build_wheel``. :param srcdir: Project source directory :param isolated: Whether or not to run invoke the backend in the current environment or to create an isolated one and invoke it there. """ builder = build.ProjectBuilder( os.fspath(srcdir), runner=pep517.quiet_subprocess_runner, ) if not isolated: return _project_wheel_metadata(builder) with build.env.IsolatedEnvBuilder() as env: builder.python_executable = env.executable builder.scripts_dir = env.scripts_dir env.install(builder.build_system_requires) env.install(builder.get_requires_for_build('wheel')) return _project_wheel_metadata(builder)
def test_isolated_environment_install(mocker): with build.env.IsolatedEnvironment.for_current() as env: mocker.patch('subprocess.check_call') env.install([]) subprocess.check_call.assert_not_called() env.install(['some', 'requirements']) if sys.version_info[:2] != (3, 5): subprocess.check_call.assert_called() args = subprocess.check_call.call_args[0][0] assert args[:7] == [env.executable, '-m', 'pip', 'install', '--prefix', env.path, '-r']
def _build_v2_module(self, build_path: str, output_directory: str) -> str: """ Build v2 module using PEP517 package builder. """ try: with build.env.IsolatedEnvBuilder() as env: distribution = "wheel" builder = build.ProjectBuilder( srcdir=build_path, python_executable=env.executable, scripts_dir=env.scripts_dir) env.install(builder.build_system_requires) env.install( builder.get_requires_for_build(distribution=distribution)) return builder.build(distribution=distribution, output_directory=output_directory) except Exception: raise ModuleBuildFailedError(msg="Module build failed")
def test_isolated_environment_install(mocker): with build.env.IsolatedEnvBuilder() as env: mocker.patch('subprocess.check_call') env.install([]) subprocess.check_call.assert_not_called() env.install(['some', 'requirements']) if sys.version_info[:2] != (3, 5): subprocess.check_call.assert_called() args = subprocess.check_call.call_args[0][0][:-1] assert args == [ env.executable, '-{}m'.format('E' if sys.version_info[0] == 2 else 'I'), 'pip', 'install', '--no-warn-script-location', '-r', ]
def test_isolated_environment_install(mocker): with build.env.IsolatedEnvBuilder() as env: mocker.patch('build.env._subprocess') env.install([]) build.env._subprocess.assert_not_called() env.install(['some', 'requirements']) build.env._subprocess.assert_called() args = build.env._subprocess.call_args[0][0][:-1] assert args == [ env.executable, '-Im', 'pip', 'install', '--use-pep517', '--no-warn-script-location', '-r', ]
def test_isolated_env_log(mocker, caplog, package_test_flit): mocker.patch('build.env._subprocess') caplog.set_level(logging.DEBUG) builder = build.env.IsolatedEnvBuilder() builder.log('something') with builder as env: env.install(['something']) assert [ (record.levelname, record.message) for record in caplog.records ] == [ ('INFO', 'something'), ('INFO', 'Creating venv isolated environment...'), ('INFO', 'Installing packages in isolated environment... (something)'), ] if sys.version_info >= (3, 8): # stacklevel assert [(record.lineno) for record in caplog.records] == [105, 107, 198]
def test_isolated_environment(mocker): with build.env.IsolatedEnvironment() as env: if os.name != 'nt': assert os.environ['PATH'] == os.path.join(env._path, 'bin') assert os.environ['PYTHONHOME'] == env._path for path in ('purelib', 'platlib'): assert sysconfig.get_path( path) not in os.environ['PYTHONPATH'].split(os.pathsep) mocker.patch('subprocess.check_call') env.install([]) subprocess.check_call.assert_not_called() env.install(['some', 'requirements']) if sys.version_info[:2] != (3, 5): subprocess.check_call.assert_called() assert subprocess.check_call.call_args[0][0] == [ sys.executable, '-m', 'pip', 'install', '--ignore-installed', '--prefix', env._path, 'some', 'requirements' ]