| 1 |
g> |
| 2 |
> |
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
from contextlib import contextmanager |
| 24 |
from pathlib import Path |
| 25 |
from typing import override |
| 26 |
from collections.abc import Iterator |
| 27 |
import logging |
| 28 |
import os.path as osp |
| 29 |
import shutil |
| 30 |
import sysconfig |
| 31 |
import tomllib |
| 32 |
|
| 33 |
try: |
| 34 |
from installer import install |
| 35 |
from installer.destinations import SchemeDictionaryDestination |
| 36 |
from installer.sources import WheelFile |
| 37 |
from installer.utils import parse_metadata_file |
| 38 |
except ModuleNotFoundError: |
| 39 |
SchemeDictionaryDestination = WheelFile = install = parse_metadata_file = None |
| 40 |
|
| 41 |
from dhpython.build.base import Base, shell_command, create_setuptools_parallel_cfg |
| 42 |
from dhpython.build.types import Context, Args |
| 43 |
from dhpython.tools import dpkg_architecture |
| 44 |
|
| 45 |
log = logging.getLogger('dhpython') |
| 46 |
|
| 47 |
|
| 48 |
def _build_backend() -> str | None: |
| 49 |
"""Retrieve the build-system from pyproject.toml, where possible""" |
| 50 |
try: |
| 51 |
with open('pyproject.toml', 'rb') as f: |
| 52 |
pyproject = tomllib.load(f) |
| 53 |
backend = pyproject.get('build-system', {}).get('build-backend') |
| 54 |
if backend is None: |
| 55 |
return None |
| 56 |
assert isinstance(backend, str) |
| 57 |
return backend |
| 58 |
except NameError: |
| 59 |
|
| 60 |
return None |
| 61 |
except FileNotFoundError: |
| 62 |
|
| 63 |
return None |
| 64 |
except tomllib.TOMLDecodeError as e: |
| 65 |
log.warning("pyproject.toml: TOMLDecodeError: %s", str(e)) |
| 66 |
return None |
| 67 |
|
| 68 |
|
| 69 |
class BuildSystem(Base): |
| 70 |
DESCRIPTION = 'Generic PEP517 build system' |
| 71 |
SUPPORTED_INTERPRETERS = {'python3', 'python{version}'} |
| 72 |
REQUIRED_FILES = ['pyproject.toml'] |
| 73 |
OPTIONAL_FILES = {} |
| 74 |
CLEAN_FILES = Base.CLEAN_FILES | {'build'} |
| 75 |
|
| 76 |
@override |
| 77 |
def detect(self, context: Context) -> int: |
| 78 |
"""Return certainty level that this plugin describes the right build |
| 79 |
system |
| 80 |
|
| 81 |
This method uses cls.{REQUIRED}_FILES (pyroject.toml) only; any |
| 82 |
other PEP517 compliant builder (such as the flit) builder should |
| 83 |
indicate higher specificity than this plugin. |
| 84 |
|
| 85 |
:return: 0 <= certainty <= 100 |
| 86 |
:rtype: int |
| 87 |
""" |
| 88 |
result = super().detect(context) |
| 89 |
|
| 90 |
result -= 20 |
| 91 |
|
| 92 |
if _build_backend(): |
| 93 |
result += 10 |
| 94 |
|
| 95 |
if result > 100: |
| 96 |
return 100 |
| 97 |
return result |
| 98 |
|
| 99 |
@override |
| 100 |
def clean(self, context: Context, args: Args) -> None: |
| 101 |
super().clean(context, args) |
| 102 |
if osp.exists(args.interpreter.binary()): |
| 103 |
log.debug("removing '%s' (and everything under it)", args.build_dir) |
| 104 |
if osp.isdir(args.build_dir): |
| 105 |
shutil.rmtree(args.build_dir) |
| 106 |
|
| 107 |
@override |
| 108 |
def configure(self, context: Context, args: Args) -> None: |
| 109 |
if install is None: |
| 110 |
raise Exception("PEP517 plugin dependencies are not available. " |
| 111 |
"Please Build-Depend on pybuild-plugin-pyproject.") |
| 112 |
|
| 113 |
|
| 114 |
@override |
| 115 |
def build(self, context: Context, args: Args) -> None: |
| 116 |
self.build_wheel(context, args) |
| 117 |
self.unpack_wheel(context, args) |
| 118 |
|
| 119 |
def _backend_config_settings(self, args: Args) -> list[str]: |
| 120 |
backend = _build_backend() |
| 121 |
if backend == "mesonpy": |
| 122 |
arch_data = dpkg_architecture() |
| 123 |
return [ |
| 124 |
|
| 125 |
|
| 126 |
f"build-dir={args.home_dir}/meson-build", |
| 127 |
"compile-args=--verbose", |
| 128 |
|
| 129 |
"setup-args=--wrap-mode=nodownload", |
| 130 |
"setup-args=--prefix=/usr", |
| 131 |
"setup-args=--sysconfdir=/etc", |
| 132 |
"setup-args=--localstatedir=/var", |
| 133 |
"setup-args=--libdir=lib/" + arch_data["DEB_HOST_MULTIARCH"], |
| 134 |
] |
| 135 |
return [] |
| 136 |
|
| 137 |
@shell_command |
| 138 |
@create_setuptools_parallel_cfg(backend=_build_backend) |
| 139 |
@override |
| 140 |
def build_wheel(self, context: Context, args: Args) -> str: |
| 141 |
""" build a wheel using the PEP517 builder defined by upstream """ |
| 142 |
log.info('Building wheel for %s with "build" module', args.interpreter) |
| 143 |
config_settings = self._backend_config_settings(args) |
| 144 |
context.ENV['FLIT_NO_NETWORK'] = '1' |
| 145 |
context.ENV['HOME'] = args.home_dir |
| 146 |
return ('{interpreter} -m build ' |
| 147 |
'--skip-dependency-check --no-isolation --wheel ' |
| 148 |
'--outdir ' + args.home_dir + ' ' + |
| 149 |
' '.join(('--config-setting ' + setting) |
| 150 |
for setting in config_settings) + |
| 151 |
' {args}' |
| 152 |
) |
| 153 |
|
| 154 |
@contextmanager |
| 155 |
def opened_wheel(self, context: Context, args: Args) -> Iterator[WheelFile]: |
| 156 |
wheel_fn = self.built_wheel(context, args) |
| 157 |
if not wheel_fn: |
| 158 |
raise AssertionError("Unable to locate a built wheel") |
| 159 |
wheel = Path(wheel_fn) |
| 160 |
if wheel.name.startswith('UNKNOWN'): |
| 161 |
raise Exception(f'UNKNOWN wheel found: {wheel.name}. Does ' |
| 162 |
'pyproject.toml specify a build-backend?') |
| 163 |
with WheelFile.open(wheel) as source: |
| 164 |
yield source |
| 165 |
|
| 166 |
def unpack_wheel(self, context: Context, args: Args) -> None: |
| 167 |
""" unpack the wheel into pybuild's normal """ |
| 168 |
log.info('Unpacking wheel built for %s with "installer" module', args.interpreter) |
| 169 |
extras = {} |
| 170 |
for extra in ('scripts', 'data', 'include'): |
| 171 |
path = Path(args.home_dir) / extra |
| 172 |
if path.exists(): |
| 173 |
log.warning('%s directory already exists, skipping unpack. ' |
| 174 |
'Is the Python package being built twice?', |
| 175 |
extra.title()) |
| 176 |
return |
| 177 |
extras[extra] = str(path) |
| 178 |
destination = SchemeDictionaryDestination( |
| 179 |
{ |
| 180 |
'platlib': args.build_dir, |
| 181 |
'purelib': args.build_dir, |
| 182 |
'scripts': extras["scripts"], |
| 183 |
'data': extras["data"], |
| 184 |
'headers': extras["include"], |
| 185 |
}, |
| 186 |
interpreter=args.interpreter.binary_dv, |
| 187 |
script_kind='posix', |
| 188 |
) |
| 189 |
|
| 190 |
with self.opened_wheel(context, args) as source: |
| 191 |
install( |
| 192 |
source=source, |
| 193 |
destination=destination, |
| 194 |
additional_metadata={}, |
| 195 |
) |
| 196 |
|
| 197 |
@override |
| 198 |
def install(self, context: Context, args: Args) -> None: |
| 199 |
log.info('Copying package built for %s to destdir', args.interpreter) |
| 200 |
try: |
| 201 |
paths = sysconfig.get_paths(scheme='deb_system') |
| 202 |
except KeyError: |
| 203 |
|
| 204 |
|
| 205 |
paths = sysconfig.get_paths(scheme='posix_prefix') |
| 206 |
|
| 207 |
|
| 208 |
for extra in ('data', 'scripts', 'include'): |
| 209 |
src_dir = Path(args.home_dir) / extra |
| 210 |
if not src_dir.exists(): |
| 211 |
continue |
| 212 |
if extra == 'include': |
| 213 |
with self.opened_wheel(context, args) as source: |
| 214 |
metadata = parse_metadata_file( |
| 215 |
source.read_dist_info("METADATA") |
| 216 |
) |
| 217 |
extra_path = osp.join( |
| 218 |
args.interpreter.include_dir, metadata["Name"] |
| 219 |
) |
| 220 |
else: |
| 221 |
extra_path = paths[extra] |
| 222 |
target_dir = args.destdir + extra_path |
| 223 |
log.debug('Copying %s directory contents from %s -> %s', |
| 224 |
extra, src_dir, target_dir) |
| 225 |
shutil.copytree( |
| 226 |
src_dir, |
| 227 |
target_dir, |
| 228 |
dirs_exist_ok=True, |
| 229 |
) |
| 230 |
|
| 231 |
|
| 232 |
module_dir = args.build_dir |
| 233 |
target_dir = args.destdir + args.install_dir |
| 234 |
log.debug('Copying module contents from %s -> %s', |
| 235 |
module_dir, target_dir) |
| 236 |
shutil.copytree( |
| 237 |
module_dir, |
| 238 |
target_dir, |
| 239 |
dirs_exist_ok=True, |
| 240 |
) |
| 241 |
|
| 242 |
@shell_command |
| 243 |
@override |
| 244 |
def test(self, context: Context, args: Args) -> str: |
| 245 |
scripts = Path(args.home_dir) / 'scripts' |
| 246 |
if scripts.exists(): |
| 247 |
context.ENV['PATH'] = f"{scripts}:{context.ENV['PATH']}" |
| 248 |
context.ENV['HOME'] = args.home_dir |
| 249 |
return super().test_cmd(context, args) |