Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flit backend #128

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jupyter_packaging/build_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
build_sdist as orig_build_sdist,
build_wheel as orig_build_wheel,
)
import tomlkit
import tomli

from jupyter_packaging.setupbase import __version__

Expand Down Expand Up @@ -55,7 +55,7 @@ def _get_build_func():
pyproject = Path("pyproject.toml")
if not pyproject.exists():
return
data = tomlkit.loads(pyproject.read_text(encoding="utf-8"))
data = tomli.loads(pyproject.read_text(encoding="utf-8"))
if "tool" not in data:
return
if "jupyter-packaging" not in data["tool"]:
Expand Down Expand Up @@ -100,7 +100,7 @@ def _ensure_targets():
pyproject = Path("pyproject.toml")
if not pyproject.exists():
return
data = tomlkit.loads(pyproject.read_text(encoding="utf-8"))
data = tomli.loads(pyproject.read_text(encoding="utf-8"))
if "tool" not in data:
return
if "jupyter-packaging" not in data["tool"]:
Expand Down
130 changes: 130 additions & 0 deletions jupyter_packaging/build_flit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import importlib
from pathlib import Path
import sys

from flit.buildapi import (
get_requires_for_build_wheel,
get_requires_for_build_sdist,
prepare_metadata_for_build_wheel,
build_sdist as orig_build_sdist,
build_wheel as orig_build_wheel,
build_editable as orig_build_editable,
)
import tomli


VERSION = "0.11.1"

# PEP 517 specifies that the CWD will always be the source tree
pyproj_toml = Path("pyproject.toml")


def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
"""Build a wheel with an optional pre-build step."""
builder = _get_build_func()
if builder:
builder()
_replace_keys()
val = orig_build_wheel(
wheel_directory,
config_settings=config_settings,
metadata_directory=metadata_directory,
)
_ensure_targets()
return val


def build_sdist(sdist_directory, config_settings=None):
"""Build an sdist with an optional pre-build step."""
builder = _get_build_func()
if builder:
builder()
_replace_keys()
val = orig_build_sdist(sdist_directory, config_settings=config_settings)
_ensure_targets()
return val


def build_editable(wheel_directory, config_settings=None, metadata_directory=None):
"""Build in editable mode pre-build step."""
builder = _get_build_func("editable-build") or _get_build_func("build")
if builder:
builder()
_replace_keys()
val = orig_build_editable(
wheel_directory,
config_settings=config_settings,
metadata_directory=metadata_directory,
)
_ensure_targets()
return val


def _get_build_func(prefix="build"):
pyproject = Path("pyproject.toml")
if not pyproject.exists():
return
target = prefix + "er"

data = tomli.loads(pyproject.read_text(encoding="utf-8"))
if "tool" not in data:
return
if "jupyter-packaging" not in data["tool"]:
return
if target not in data["tool"]["jupyter-packaging"]:
return
section = data["tool"]["jupyter-packaging"]

if "factory" not in section[target]:
raise ValueError(f"Missing `factory` specifier for {target}")

factory_data = section[target]["factory"]
mod_name, _, factory_name = factory_data.rpartition(".")

if "options" in section and "skip-if-exists" in section["options"]:
skip_if_exists = section["options"]["skip-if-exists"]
if all(Path(path).exists() for path in skip_if_exists):
return None

# If the module fails to import, try importing as a local script
try:
mod = importlib.import_module(mod_name)
except ImportError:
try:
sys.path.insert(0, str(Path.cwd()))
mod = importlib.import_module(mod_name)
finally:
sys.path.pop(0)

factory = getattr(mod, factory_name)
kwargs = section.get(f"{prefix}-args", {})
return factory(**kwargs)


def _ensure_targets():
pyproject = Path("pyproject.toml")
if not pyproject.exists():
return
data = tomli.loads(pyproject.read_text(encoding="utf-8"))
if "tool" not in data:
return
if "jupyter-packaging" not in data["tool"]:
return
section = data["tool"]["jupyter-packaging"]
if "options" in section and "ensured-targets" in section["options"]:
targets = section["options"]["ensured-targets"]
missing = [t for t in targets if not Path(t).exists()]
if missing:
raise ValueError(("missing files: %s" % missing))


def _replace_keys():
if not pyproj_toml.exists():
return
data = pyproj_toml.read_text(encoding="utf-8")
before = "[tool.jupyter-packaging.external-data]"
after = "[tool.flit.external-data]"
data = data.replace(before, after)
pyproj_toml.write_text(data, encoding="utf-8")
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ packages = find:
python_requires = >=3.7
install_requires =
packaging
tomlkit
flit
tomli>=1.0
setuptools>=60.2.0
wheel
deprecation
Expand Down
194 changes: 194 additions & 0 deletions tests/test_build_flit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import os
from pathlib import Path
import sys
from subprocess import check_call
from unittest.mock import patch

import pytest

from jupyter_packaging.build_flit import build_wheel, build_sdist, build_editable


TOML_CONTENT = """
[project]
name = "foo"
version = "0.1.0"
description = "foo package"

[tool.jupyter-packaging.builder]
factory = "foo.main"

[tool.jupyter-packaging.build-args]
fizz = "buzz"
"""

DATA_CONTENT = """
[tool.jupyter-packaging.external-data]
directory = "data"
"""

FOO_CONTENT = r"""
from pathlib import Path
def main(fizz=None):
Path('foo.txt').write_text(f'fizz={fizz}', encoding='utf-8')
"""

BAD_CONTENT = """
[tool.jupyter-packaging.builder]
bar = "foo.main"
"""

ENSURED_CONTENT = """
[tool.jupyter-packaging.options]
ensured-targets = ["foo.txt"]
"""

SKIP_IF_EXISTS = """
[tool.jupyter-packaging.options]
skip-if-exists = ["foo.txt"]
"""


def test_build_wheel_no_toml(tmp_path):
os.chdir(tmp_path)
orig_wheel = patch("jupyter_packaging.build_flit.orig_build_wheel")
build_wheel(tmp_path)
orig_wheel.assert_called_with(
tmp_path, config_settings=None, metadata_directory=None
)


def test_build_wheel(tmp_path, mocker):
os.chdir(tmp_path)
tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
tmp_path.joinpath("pyproject.toml").write_text(
TOML_CONTENT + DATA_CONTENT + ENSURED_CONTENT, encoding="utf-8"
)
orig_wheel = mocker.patch("jupyter_packaging.build_flit.orig_build_wheel")
build_wheel(tmp_path)
orig_wheel.assert_called_with(
tmp_path, config_settings=None, metadata_directory=None
)
data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
assert data == "fizz=buzz"

content = TOML_CONTENT.replace("buzz", "fizz") + SKIP_IF_EXISTS
tmp_path.joinpath("pyproject.toml").write_text(content, encoding="utf-8")
build_wheel(tmp_path)
data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
assert data == "fizz=buzz"


def test_build_wheel_bad_toml(tmp_path, mocker):
os.chdir(tmp_path)
tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
tmp_path.joinpath("pyproject.toml").write_text(BAD_CONTENT, encoding="utf-8")
orig_wheel = mocker.patch("jupyter_packaging.build_flit.orig_build_wheel")
with pytest.raises(ValueError):
build_wheel(tmp_path)
orig_wheel.assert_not_called()


def test_build_wheel_no_toml(tmp_path, mocker):
os.chdir(tmp_path)
orig_wheel = mocker.patch("jupyter_packaging.build_flit.orig_build_wheel")
build_wheel(tmp_path)
orig_wheel.assert_called_with(
tmp_path, config_settings=None, metadata_directory=None
)


def test_build_editable(tmp_path, mocker):
os.chdir(tmp_path)
tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
tmp_path.joinpath("pyproject.toml").write_text(
TOML_CONTENT + ENSURED_CONTENT, encoding="utf-8"
)
orig_wheel = mocker.patch("jupyter_packaging.build_flit.orig_build_wheel")
build_wheel(tmp_path)
orig_wheel.assert_called_with(
tmp_path, config_settings=None, metadata_directory=None
)
data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
assert data == "fizz=buzz"

content = TOML_CONTENT.replace("buzz", "fizz") + SKIP_IF_EXISTS
tmp_path.joinpath("pyproject.toml").write_text(content, encoding="utf-8")
build_editable(tmp_path)
data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
assert data == "fizz=buzz"


def test_build_sdist(tmp_path, mocker):
os.chdir(tmp_path)
tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
tmp_path.joinpath("pyproject.toml").write_text(
TOML_CONTENT + ENSURED_CONTENT, encoding="utf-8"
)
orig_sdist = mocker.patch("jupyter_packaging.build_flit.orig_build_sdist")
build_sdist(tmp_path)
orig_sdist.assert_called_with(tmp_path, config_settings=None)
data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
assert data == "fizz=buzz"


def test_build_sdist_bad_toml(tmp_path, mocker):
os.chdir(tmp_path)
tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
tmp_path.joinpath("pyproject.toml").write_text(BAD_CONTENT, encoding="utf-8")
orig_sdist = mocker.patch("jupyter_packaging.build_flit.orig_build_sdist")
with pytest.raises(ValueError):
build_sdist(tmp_path)
orig_sdist.assert_not_called()


def test_build_sdist_no_toml(tmp_path, mocker):
os.chdir(tmp_path)
orig_sdist = mocker.patch("jupyter_packaging.build_flit.orig_build_sdist")
build_sdist(tmp_path)
orig_sdist.assert_called_with(tmp_path, config_settings=None)


def test_build_package(make_package):
package_dir = make_package()
pyproject = package_dir / "pyproject.toml"
text = pyproject.read_text(encoding="utf-8")
text = text.replace("setuptools.build_meta", "jupyter_packaging.build_flit")
text += TOML_CONTENT
text += DATA_CONTENT
data_dir = package_dir / "data/etc/jupyter/jupyter_server_config.d"
data_dir.mkdir(parents=True)
data_file = data_dir / "jupyter_server_foo.json"
data_file.write_text(
'{"ServerApp": {"jpserver_extensions": {"jupyter_server_foo": true}}}"',
encoding="utf-8",
)
pyproject.write_text(text, encoding="utf-8")
package_dir.joinpath("foo.py").write_text(FOO_CONTENT, encoding="utf-8")
check_call([sys.executable, "-m", "build"], cwd=package_dir)
data = package_dir.joinpath("foo.txt").read_text(encoding="utf-8")
assert data == "fizz=buzz"


def test_develop_package(make_package):
package_dir = make_package()
pyproject = package_dir / "pyproject.toml"
text = pyproject.read_text(encoding="utf-8")
text = text.replace("setuptools.build_meta", "jupyter_packaging.build_flit")
text += TOML_CONTENT.replace(".build", ".editable-build")
text += DATA_CONTENT
data_dir = package_dir / "data/etc/jupyter/jupyter_server_config.d"
data_dir.mkdir(parents=True)
data_file = data_dir / "jupyter_server_foo.json"
data_file.write_text(
'{"ServerApp": {"jpserver_extensions": {"jupyter_server_foo": true}}}"',
encoding="utf-8",
)
pyproject.write_text(text, encoding="utf-8")
package_dir.joinpath("foo.py").write_text(FOO_CONTENT, encoding="utf-8")
check_call([sys.executable, "-m", "pip", "install", "-e", "."], cwd=package_dir)
target = (
Path(sys.base_prefix)
/ "etc/jupyter/jupyter_server_config.d/jupyter_server_foo.json"
)
assert target.exists()