Skip to content

Commit 70410ba

Browse files
authored
Add a parameter flag to clean up artifacts after build (#11)
* Add clean_artifacts_after_build script config parameter * Bump version * README.md doc additions for new `clean_artifacts_after_build` parameter * Added clean_artifacts_after_build=True scripts to test case
1 parent 4e0fcab commit 70410ba

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ an array of scripts to the `tool.hatch.build.hooks.build-scripts.scripts` key in
2929
| `work_dir` | `"."` | The directory to run the commands in. All artifact patterns are relative to this directory. |
3030
| `clean_artifacts` | `true` | Whether to clean files from the `out_dir` that match the artifact patterns before running the commands. |
3131
| `clean_out_dir` | `false` | Whether to clean the `out_dir` before running the commands. |
32+
| `clean_artifacts_after_build` | `false` | Whether to clean the files from the `out_dir` that were generated by the scripts. |
3233

3334
In practice this looks like:
3435

@@ -56,3 +57,4 @@ You can configure script defaults for scripts by adding a `[tool.hatch.build.hoo
5657
| `work_dir` | `"."` | The directory to run the commands in. All artifact patterns are relative to this directory. |
5758
| `clean_artifacts` | `true` | Whether to clean files from the `out_dir` that match the artifact patterns before running the commands. |
5859
| `clean_out_dir` | `false` | Whether to clean the `out_dir` before running the commands. |
60+
| `clean_artifacts_after_build` | `false` | Whether to clean the files from the `out_dir` that were generated by the scripts. |

hatch_build_scripts/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.4"
1+
__version__ = "0.0.5"

hatch_build_scripts/hooks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Register hooks for the plugin."""
2+
23
from hatchling.plugin import hookimpl
34

45
from hatch_build_scripts.plugin import BuildScriptsHook

hatch_build_scripts/plugin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ def initialize(
6565

6666
build_data["artifacts"].append(str(out_dir.relative_to(self.root)))
6767

68+
def finalize(
69+
self,
70+
version: str, # noqa: ARG002
71+
build_data: dict[str, Any], # noqa: ARG002
72+
artifact_path: str, # noqa: ARG002
73+
) -> None:
74+
all_scripts = load_scripts(self.config)
75+
76+
for script in all_scripts:
77+
if not script.clean_artifacts_after_build:
78+
continue
79+
80+
for out_file in script.out_files(self.root):
81+
log.debug(f"After build, cleaning {out_file}")
82+
out_file.unlink(missing_ok=True)
83+
6884

6985
def load_scripts(config: dict[str, Any]) -> Sequence[OneScriptConfig]:
7086
script_defaults = dataclass_defaults(OneScriptConfig)
@@ -97,6 +113,9 @@ class OneScriptConfig:
97113
clean_out_dir: bool = False
98114
"""Whether to clean the output directory before running the scripts"""
99115

116+
clean_artifacts_after_build: bool = False
117+
"""Whether to clean the build directory after running the scripts"""
118+
100119
def __post_init__(self) -> None:
101120
self.out_dir = conv_path(self.out_dir)
102121
self.work_dir = conv_path(self.work_dir)

tests/test_plugin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def test_plugin(tmpdir):
99

1010
(tmp_lib_dir / "some-dir").mkdir()
1111
(tmp_lib_dir / "another-dir").mkdir()
12+
(tmp_lib_dir / "yet-another-dir").mkdir()
1213

1314
some_dir_out = tmp_lib_dir / "some-dir-out"
1415
some_dir_out.mkdir()
@@ -50,6 +51,17 @@ def test_plugin(tmpdir):
5051
artifacts=["*.txt"],
5152
clean_out_dir=True,
5253
),
54+
OneScriptConfig(
55+
out_dir="yet-another-dir-out",
56+
work_dir="yet-another-dir",
57+
commands=[
58+
"echo 'hello world' > f1.txt",
59+
"echo 'hello world' > f2.txt",
60+
],
61+
artifacts=["*.txt"],
62+
clean_out_dir=True,
63+
clean_artifacts_after_build=True,
64+
),
5365
],
5466
)
5567

@@ -66,3 +78,9 @@ def test_plugin(tmpdir):
6678

6779
assert not (extract_dir / "some-dir-out" / "f3.txt").exists()
6880
assert not (extract_dir / "another-dir-out" / "module.py").exists()
81+
82+
# we expect that this file still exists in the source
83+
assert (tmp_lib_dir / "fake" / "fake.txt").exists()
84+
# we expect that this file was cleaned in the source but exists in wheel
85+
assert (extract_dir / "yet-another-dir-out" / "f1.txt").exists()
86+
assert not (tmp_lib_dir / "yet-another-dir-out" / "f1.txt").exists()

0 commit comments

Comments
 (0)