Skip to content

Commit

Permalink
ENH: remove the need for a working directory
Browse files Browse the repository at this point in the history
Currently a Project has an associated a temporary working directory
containing the build directory (unless the user specifies another one
via configuration options) and the install directory for the meson
project.  The code can be simplified assigning to the project a build
directory and always using a temporary install directory.

Note that currently all files are copied into the Python wheel from
the source or build directory, thus running meson install into a
destination directory is not that useful. This will be fixes in later
commits.
  • Loading branch information
dnicolodi committed Jul 15, 2023
1 parent 0e20851 commit b79c87b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 64 deletions.
83 changes: 35 additions & 48 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,30 +489,33 @@ def _wheel_write_metadata(self, whl: mesonpy._wheelfile.WheelFile) -> None:
def build(self, directory: Path) -> pathlib.Path:
# ensure project is built
self._project.build()
# install the project
self._project.install()

wheel_file = pathlib.Path(directory, f'{self.name}.whl')
with mesonpy._wheelfile.WheelFile(wheel_file, 'w') as whl:
self._wheel_write_metadata(whl)
# install project in temporary destination directory
with tempfile.TemporaryDirectory() as destdir:
self._project.install(destdir)

with mesonpy._util.cli_counter(sum(len(x) for x in self._wheel_files.values())) as counter:
wheel_file = pathlib.Path(directory, f'{self.name}.whl')

root = 'purelib' if self.is_pure else 'platlib'
with mesonpy._wheelfile.WheelFile(wheel_file, 'w') as whl:
self._wheel_write_metadata(whl)

for path, entries in self._wheel_files.items():
for dst, src in entries:
counter.update(src)
with mesonpy._util.cli_counter(sum(len(x) for x in self._wheel_files.values())) as counter:

if path == root:
pass
elif path == 'mesonpy-libs':
# custom installation path for bundled libraries
dst = pathlib.Path(f'.{self._project.name}.mesonpy.libs', dst)
else:
dst = pathlib.Path(self.data_dir, path, dst)
root = 'purelib' if self.is_pure else 'platlib'

for path, entries in self._wheel_files.items():
for dst, src in entries:
counter.update(src)

self._install_path(whl, src, dst)
if path == root:
pass
elif path == 'mesonpy-libs':
# custom installation path for bundled libraries
dst = pathlib.Path(f'.{self._project.name}.mesonpy.libs', dst)
else:
dst = pathlib.Path(self.data_dir, path, dst)

self._install_path(whl, src, dst)

return wheel_file

Expand Down Expand Up @@ -628,16 +631,13 @@ class Project():
def __init__(
self,
source_dir: Path,
working_dir: Path,
build_dir: Optional[Path] = None,
build_dir: Path,
meson_args: Optional[MesonArgs] = None,
editable_verbose: bool = False,
) -> None:
self._source_dir = pathlib.Path(source_dir).absolute()
self._working_dir = pathlib.Path(working_dir).absolute()
self._build_dir = pathlib.Path(build_dir).absolute() if build_dir else (self._working_dir / 'build')
self._build_dir = pathlib.Path(build_dir).absolute()
self._editable_verbose = editable_verbose
self._install_dir = self._working_dir / 'install'
self._meson_native_file = self._build_dir / 'meson-python-native-file.ini'
self._meson_cross_file = self._build_dir / 'meson-python-cross-file.ini'
self._meson_args: MesonArgs = collections.defaultdict(list)
Expand All @@ -651,7 +651,6 @@ def __init__(

# make sure the build dir exists
self._build_dir.mkdir(exist_ok=True, parents=True)
self._install_dir.mkdir(exist_ok=True, parents=True)

# setuptools-like ARCHFLAGS environment variable support
if sysconfig.get_platform().startswith('macosx-'):
Expand Down Expand Up @@ -805,24 +804,11 @@ def build(self) -> None:
"""Build the Meson project."""
self._run(self._build_command)

def install(self) -> None:
def install(self, destdir: Path) -> None:
"""Install the Meson project."""
destdir = os.fspath(self._install_dir)
destdir = os.fspath(destdir)
self._run(['meson', 'install', '--quiet', '--no-rebuild', '--destdir', destdir, *self._meson_args['install']])

@classmethod
@contextlib.contextmanager
def with_temp_working_dir(
cls,
source_dir: Path = os.path.curdir,
build_dir: Optional[Path] = None,
meson_args: Optional[MesonArgs] = None,
editable_verbose: bool = False,
) -> Iterator[Project]:
"""Creates a project instance pointing to a temporary working directory."""
with tempfile.TemporaryDirectory(prefix='.mesonpy-', dir=os.fspath(source_dir)) as tmpdir:
yield cls(source_dir, tmpdir, build_dir, meson_args, editable_verbose)

@functools.lru_cache()
def _info(self, name: str) -> Dict[str, Any]:
"""Read info from meson-info directory."""
Expand Down Expand Up @@ -974,18 +960,19 @@ def editable(self, directory: Path) -> pathlib.Path:


@contextlib.contextmanager
def _project(config_settings: Optional[Dict[Any, Any]]) -> Iterator[Project]:
def _project(config_settings: Optional[Dict[Any, Any]] = None) -> Iterator[Project]:
"""Create the project given the given config settings."""

settings = _validate_config_settings(config_settings or {})
meson_args = {name: settings.get(f'{name}-args', []) for name in _MESON_ARGS_KEYS}

with Project.with_temp_working_dir(
build_dir=settings.get('builddir'),
meson_args=typing.cast(MesonArgs, meson_args),
editable_verbose=bool(settings.get('editable-verbose'))
) as project:
yield project
meson_args = typing.cast(MesonArgs, {name: settings.get(f'{name}-args', []) for name in _MESON_ARGS_KEYS})
source_dir = os.path.curdir
build_dir = settings.get('builddir')
editable_verbose = bool(settings.get('editable-verbose'))

with contextlib.ExitStack() as ctx:
if build_dir is None:
build_dir = ctx.enter_context(tempfile.TemporaryDirectory(prefix='.mesonpy-', dir=source_dir))
yield Project(source_dir, build_dir, meson_args, editable_verbose)


def _parse_version_string(string: str) -> Tuple[int, ...]:
Expand Down
13 changes: 6 additions & 7 deletions tests/test_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ def test_collect(package_complex):
def test_mesonpy_meta_finder(package_complex, tmp_path):
# build a package in a temporary directory
mesonpy.Project(package_complex, tmp_path)
build_path = tmp_path / 'build'

# point the meta finder to the build directory
finder = _editable.MesonpyMetaFinder({'complex'}, os.fspath(build_path), ['ninja'])
finder = _editable.MesonpyMetaFinder({'complex'}, os.fspath(tmp_path), ['ninja'])

# check repr
assert repr(finder) == f'MesonpyMetaFinder({str(build_path)!r})'
assert repr(finder) == f'MesonpyMetaFinder({str(tmp_path)!r})'

# verify that we can look up a pure module in the source directory
spec = finder.find_spec('complex')
Expand All @@ -79,7 +78,7 @@ def test_mesonpy_meta_finder(package_complex, tmp_path):
spec = finder.find_spec('complex.test')
assert spec.name == 'complex.test'
assert isinstance(spec.loader, _editable.ExtensionFileLoader)
assert spec.origin == os.fspath(build_path / f'test{EXT_SUFFIX}')
assert spec.origin == os.fspath(tmp_path / f'test{EXT_SUFFIX}')

try:
# install the finder in the meta path
Expand All @@ -89,7 +88,7 @@ def test_mesonpy_meta_finder(package_complex, tmp_path):
assert complex.__spec__.origin == os.fspath(package_complex / 'complex/__init__.py')
assert complex.__file__ == os.fspath(package_complex / 'complex/__init__.py')
import complex.test
assert complex.test.__spec__.origin == os.fspath(build_path / f'test{EXT_SUFFIX}')
assert complex.test.__spec__.origin == os.fspath(tmp_path / f'test{EXT_SUFFIX}')
assert complex.test.answer() == 42
import complex.namespace.foo
assert complex.namespace.foo.__spec__.origin == os.fspath(package_complex / 'complex/namespace/foo.py')
Expand Down Expand Up @@ -128,7 +127,7 @@ def test_resources(tmp_path):
mesonpy.Project(package_path, tmp_path)

# point the meta finder to the build directory
finder = _editable.MesonpyMetaFinder({'simple'}, os.fspath(tmp_path / 'build'), ['ninja'])
finder = _editable.MesonpyMetaFinder({'simple'}, os.fspath(tmp_path), ['ninja'])

# verify that we can look up resources
spec = finder.find_spec('simple')
Expand All @@ -147,7 +146,7 @@ def test_importlib_resources(tmp_path):
mesonpy.Project(package_path, tmp_path)

# point the meta finder to the build directory
finder = _editable.MesonpyMetaFinder({'simple'}, os.fspath(tmp_path / 'build'), ['ninja'])
finder = _editable.MesonpyMetaFinder({'simple'}, os.fspath(tmp_path), ['ninja'])

try:
# install the finder in the meta path
Expand Down
2 changes: 1 addition & 1 deletion tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def test_ndebug(package_purelib_and_platlib, tmp_path, args, expected):
# compile a C source file (the trailing ^ is used to
# specify the target that is the first output of the rule
# containing the specified source file).
['ninja', '-C', os.fspath(project._build_dir), '-t', 'commands', '../../plat.c^'],
['ninja', '-C', os.fspath(project._build_dir), '-t', 'commands', '../plat.c^'],
stdout=subprocess.PIPE, check=True).stdout
assert (b'-DNDEBUG' in command) == expected
12 changes: 6 additions & 6 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
]
)
def test_name(package):
with chdir(package_dir / package), mesonpy.Project.with_temp_working_dir() as project:
with chdir(package_dir / package), mesonpy._project() as project:
assert project.name == package.replace('-', '_')


Expand All @@ -42,21 +42,21 @@ def test_name(package):
]
)
def test_version(package):
with chdir(package_dir / package), mesonpy.Project.with_temp_working_dir() as project:
with chdir(package_dir / package), mesonpy._project() as project:
assert project.version == '1.0.0'


def test_unsupported_dynamic(package_unsupported_dynamic):
with pytest.raises(mesonpy.MesonBuilderError, match='Unsupported dynamic fields: "dependencies"'):
with mesonpy.Project.with_temp_working_dir():
with mesonpy._project():
pass


def test_unsupported_python_version(package_unsupported_python_version):
with pytest.raises(mesonpy.MesonBuilderError, match=(
f'Unsupported Python version {platform.python_version()}, expected ==1.0.0'
)):
with mesonpy.Project.with_temp_working_dir():
with mesonpy._project():
pass


Expand Down Expand Up @@ -205,7 +205,7 @@ def test_invalid_build_dir(package_pure, tmp_path, mocker):
meson.reset_mock()

# corrupting the build direcory setup is run again
tmp_path.joinpath('build/meson-private/coredata.dat').unlink()
tmp_path.joinpath('meson-private/coredata.dat').unlink()
project = mesonpy.Project(package_pure, tmp_path)
assert len(meson.call_args_list) == 1
assert meson.call_args_list[0].args[1][1] == 'setup'
Expand All @@ -214,7 +214,7 @@ def test_invalid_build_dir(package_pure, tmp_path, mocker):
meson.reset_mock()

# removing the build directory things should still work
shutil.rmtree(tmp_path.joinpath('build'))
shutil.rmtree(tmp_path)
project = mesonpy.Project(package_pure, tmp_path)
assert len(meson.call_args_list) == 1
assert meson.call_args_list[0].args[1][1] == 'setup'
Expand Down
4 changes: 2 additions & 2 deletions tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_entrypoints(wheel_full_metadata):


def test_top_level_modules(package_module_types):
with mesonpy.Project.with_temp_working_dir() as project:
with mesonpy._project() as project:
assert set(project._wheel_builder.top_level_modules) == {
'file',
'package',
Expand All @@ -239,7 +239,7 @@ def test_top_level_modules(package_module_types):

def test_purelib_platlib_split(package_purelib_platlib_split, tmp_path):
with pytest.raises(mesonpy.BuildError, match='The purelib-platlib-split package is split'):
with mesonpy.Project.with_temp_working_dir() as project:
with mesonpy._project() as project:
project.wheel(tmp_path)


Expand Down

0 comments on commit b79c87b

Please sign in to comment.