Skip to content

Commit

Permalink
Automatically figure out where to write GIT_COMMIT
Browse files Browse the repository at this point in the history
In most cases, we will want to put GIT_COMMIT in a directory with
the same name as the project (with hyphens replaced by underscores.)
Put GIT_COMMIT there by default. Also rename _edit_git_commit() to
_write_git_commit(), and don't create a backup file with the old
GIT_COMMIT.
  • Loading branch information
KyleFromNVIDIA committed May 9, 2024
1 parent 97a1950 commit b8562fe
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 59 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ In cases where more dynamic customization is sensible, suitable environment vari

Any option without a default is required.

| Option | Definition | Type | Default | Supports dynamic modification |
|-----------------------|---------------------------------------------------------------------|----------------|---------------------|-------------------------------|
| `build-backend` | The wrapped build backend (e.g. `setuptools.build_meta`) | string | | N |
| `commit-file` | The file in which to write the git commit hash | string | "" (No file) | N |
| `dependencies-file` | The path to the `dependencies.yaml` file to use | string | "dependencies.yaml" | Y |
| `matrix-entry` | A `;`-separated list of `=`-delimited key/value pairs | string | "" | Y |
| `require-cuda` | If false, builds will succeed even if nvcc is not available | bool | true | Y |
| `requires` | List of build requirements (in addition to `build-system.requires`) | list[str] | [] | N |
| Option | Definition | Type | Default | Supports dynamic modification |
|-----------------------|---------------------------------------------------------------------|----------------|-----------------------------|-------------------------------|
| `build-backend` | The wrapped build backend (e.g. `setuptools.build_meta`) | string | | N |
| `commit-file` | The file in which to write the git commit hash | string | "<project_name>/GIT_COMMIT" | N |
| `dependencies-file` | The path to the `dependencies.yaml` file to use | string | "dependencies.yaml" | Y |
| `matrix-entry` | A `;`-separated list of `=`-delimited key/value pairs | string | "" | Y |
| `require-cuda` | If false, builds will succeed even if nvcc is not available | bool | true | Y |
| `requires` | List of build requirements (in addition to `build-system.requires`) | list[str] | [] | N |


## Outstanding questions
Expand Down
42 changes: 19 additions & 23 deletions rapids_build_backend/impls.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,25 @@ def _get_git_commit() -> Union[str, None]:


@contextmanager
def _edit_git_commit(config):
def _write_git_commit(config, project_name):
"""
Temporarily modify the git commit of the package being built.
This is useful for projects that want to embed the current git commit in the package
at build time.
"""
commit_file = config.commit_file
if commit_file == "":
commit_file = os.path.join(project_name.replace("-", "_"), "GIT_COMMIT")
commit = _get_git_commit()

if commit_file != "" and commit is not None:
bkp_commit_file = os.path.join(
os.path.dirname(commit_file),
f".{os.path.basename(commit_file)}.rapids-build-backend.bak",
)
if commit_file is not None and commit is not None:
with open(commit_file, "w") as f:
f.write(f"{commit}\n")
try:
try:
shutil.move(commit_file, bkp_commit_file)
except FileNotFoundError:
bkp_commit_file = None

with open(commit_file, "w") as f:
f.write(f"{commit}\n")

yield
finally:
# Restore by moving rather than writing to avoid any formatting changes.
if bkp_commit_file:
shutil.move(bkp_commit_file, commit_file)
else:
os.unlink(commit_file)
os.unlink(commit_file)
else:
yield

Expand Down Expand Up @@ -227,7 +215,9 @@ def _edit_pyproject(config):
# (the actual build backend, which conditionally imports these functions).
def get_requires_for_build_wheel(config_settings):
config = Config(config_settings=config_settings)
with _edit_pyproject(config), _edit_git_commit(config):
pyproject = utils._get_pyproject()
project_name = pyproject["project"]["name"]
with _edit_pyproject(config), _write_git_commit(config, project_name):
# Reload the config for a possibly updated tool.rapids-build-backend.requires
reloaded_config = Config(config_settings=config_settings)
requires = list(reloaded_config.requires)
Expand All @@ -243,7 +233,9 @@ def get_requires_for_build_wheel(config_settings):

def get_requires_for_build_sdist(config_settings):
config = Config(config_settings=config_settings)
with _edit_pyproject(config), _edit_git_commit(config):
pyproject = utils._get_pyproject()
project_name = pyproject["project"]["name"]
with _edit_pyproject(config), _write_git_commit(config, project_name):
# Reload the config for a possibly updated tool.rapids-build-backend.requires
reloaded_config = Config(config_settings=config_settings)
requires = list(reloaded_config.requires)
Expand Down Expand Up @@ -275,15 +267,19 @@ def get_requires_for_build_editable(config_settings):

def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
config = Config(config_settings=config_settings)
with _edit_pyproject(config), _edit_git_commit(config):
pyproject = utils._get_pyproject()
project_name = pyproject["project"]["name"]
with _edit_pyproject(config), _write_git_commit(config, project_name):
return _get_backend(config.build_backend).build_wheel(
wheel_directory, config_settings, metadata_directory
)


def build_sdist(sdist_directory, config_settings=None):
config = Config(config_settings=config_settings)
with _edit_pyproject(config), _edit_git_commit(config):
pyproject = utils._get_pyproject()
project_name = pyproject["project"]["name"]
with _edit_pyproject(config), _write_git_commit(config, project_name):
return _get_backend(config.build_backend).build_sdist(
sdist_directory, config_settings
)
Expand Down
59 changes: 33 additions & 26 deletions tests/test_impls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import pytest

from rapids_build_backend.impls import (
_edit_git_commit,
_edit_pyproject,
_get_cuda_suffix,
_write_git_commit,
)


Expand All @@ -26,39 +26,46 @@ def set_cwd(cwd):


@pytest.mark.parametrize(
"initial_contents",
("project_name", "directory", "commit_file_config", "expected_commit_file"),
[
"def456\n",
"",
None,
("test-project", "test_project", "", "test_project/GIT_COMMIT"),
(
"test-project",
"_test_project",
"_test_project/GIT_COMMIT",
"_test_project/GIT_COMMIT",
),
("test-project", None, None, None),
],
)
@patch("rapids_build_backend.impls._get_git_commit", Mock(return_value="abc123"))
def test_edit_git_commit(initial_contents):
def check_initial_contents(filename):
if initial_contents is not None:
with open(filename) as f:
assert f.read() == initial_contents
else:
assert not os.path.exists(filename)

with tempfile.TemporaryDirectory() as d:
commit_file = os.path.join(d, "commit-file")
bkp_commit_file = os.path.join(d, ".commit-file.rapids-build-backend.bak")
if initial_contents is not None:
with open(commit_file, "w") as f:
f.write(initial_contents)
def test_write_git_commit(
tmp_path, project_name, directory, commit_file_config, expected_commit_file
):
with set_cwd(tmp_path):
if directory:
os.mkdir(directory)

config = Mock(
commit_file=commit_file,
commit_file=commit_file_config,
)
with _edit_git_commit(config):
with open(commit_file) as f:
assert f.read() == "abc123\n"
check_initial_contents(bkp_commit_file)
with _write_git_commit(config, project_name):
if expected_commit_file:
with open(expected_commit_file) as f:
assert f.read() == "abc123\n"
else:
assert list(os.walk(".")) == [
(".", [], []),
]

assert not os.path.exists(bkp_commit_file)
check_initial_contents(commit_file)
if directory:
assert list(os.walk(directory)) == [
(directory, [], []),
]
os.rmdir(directory)
assert list(os.walk(".")) == [
(".", [], []),
]


@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_simple_setuptools(tmp_path, env, nvcc_version):
}

package_dir = tmp_path / "pkg"
os.makedirs(package_dir)
os.makedirs(package_dir / "simple_setuptools")

generate_from_template(package_dir, "dependencies.yaml", template_args)
generate_from_template(package_dir, "pyproject.toml", template_args)
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_simple_scikit_build_core(tmp_path, env, nvcc_version):
}

package_dir = tmp_path / "pkg"
os.makedirs(package_dir)
os.makedirs(package_dir / "simple_scikit_build_core")

generate_from_template(package_dir, "dependencies.yaml", template_args)
generate_from_template(package_dir, "pyproject.toml", template_args)
Expand Down

0 comments on commit b8562fe

Please sign in to comment.