Skip to content

Commit ae94263

Browse files
committed
more logs
1 parent afef5ae commit ae94263

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

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.3"
1+
__version__ = "0.0.4"

hatch_build_scripts/plugin.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import pathspec
1414
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
1515

16-
logger = logging.getLogger(__name__)
16+
log = logging.getLogger(__name__)
17+
log_level = logging.getLevelName(os.getenv("HATCH_BUILD_SCRIPTS_LOG_LEVEL", "INFO"))
18+
log.setLevel(log_level)
1719

1820

1921
class BuildScriptsHook(BuildHookInterface):
@@ -31,10 +33,11 @@ def initialize(
3133
for script in all_scripts:
3234
if script.clean_out_dir:
3335
out_dir = Path(self.root, script.out_dir)
34-
logger.info(f"Cleaning {out_dir}")
36+
log.debug(f"Cleaning {out_dir}")
3537
shutil.rmtree(out_dir, ignore_errors=True)
3638
elif script.clean_artifacts:
3739
for out_file in script.out_files(self.root):
40+
log.debug(f"Cleaning {out_file}")
3841
out_file.unlink(missing_ok=True)
3942

4043
for script in all_scripts:
@@ -43,16 +46,20 @@ def initialize(
4346
out_dir.mkdir(parents=True, exist_ok=True)
4447

4548
for cmd in script.commands:
49+
log.info(f"Running command: {cmd}")
4650
run(cmd, cwd=str(work_dir), check=True, shell=True) # noqa: S602
4751

48-
logger.info(f"Copying artifacts to {out_dir}")
49-
for artifact_file in script.artifact_files():
50-
src_file = work_dir / artifact_file
51-
out_file = out_dir / artifact_file
52+
log.info(f"Copying artifacts to {out_dir}")
53+
for work_file in script.work_files(self.root, relative=True):
54+
src_file = work_dir / work_file
55+
out_file = out_dir / work_file
56+
log.debug(f"Copying {src_file} to {out_file}")
5257
if src_file not in created:
5358
out_file.parent.mkdir(parents=True, exist_ok=True)
5459
shutil.copyfile(src_file, out_file)
5560
created.add(out_file)
61+
else:
62+
log.debug(f"Skipping {src_file} - already exists")
5663

5764
build_data["artifacts"].append(str(out_dir.relative_to(self.root)))
5865

@@ -92,28 +99,24 @@ def __post_init__(self) -> None:
9299
self.out_dir = conv_path(self.out_dir)
93100
self.work_dir = conv_path(self.work_dir)
94101

95-
def work_files(self, root: str | Path) -> Sequence[Path]:
96-
"""Get the files that will be used by the script."""
97-
work_dir = Path(root, self.work_dir)
98-
if not work_dir.exists():
102+
def work_files(self, root: str | Path, *, relative: bool = False) -> Sequence[Path]:
103+
"""Get files in the work directory that match the artifacts spec."""
104+
abs_dir = Path(root, self.work_dir)
105+
if not abs_dir.exists():
99106
return []
100107
return [
101-
Path(root, self.work_dir, f)
102-
for f in self.artifacts_spec.match_tree(work_dir)
108+
Path(f) if relative else abs_dir / f
109+
for f in self.artifacts_spec.match_tree(abs_dir)
103110
]
104111

105-
def out_files(self, root: str | Path) -> Sequence[Path]:
106-
"""Get the files that will be created by the script."""
107-
out_dir = Path(root, self.out_dir)
108-
if not out_dir.exists():
112+
def out_files(self, root: str | Path, *, relative: bool = False) -> Sequence[Path]:
113+
"""Get files in the output directory that match the artifacts spec."""
114+
abs_dir = Path(root, self.out_dir)
115+
if not abs_dir.exists():
109116
return []
110117
return [
111-
Path(root, self.out_dir, f) for f in self.artifacts_spec.match_tree(out_dir)
112-
]
113-
114-
def artifact_files(self) -> Sequence[Path]:
115-
return [
116-
Path(conv_path(p)) for p in self.artifacts_spec.match_tree(self.work_dir)
118+
Path(f) if relative else abs_dir / f
119+
for f in self.artifacts_spec.match_tree(abs_dir)
117120
]
118121

119122
@cached_property

tests/test_plugin.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44

55

66
def test_plugin(tmpdir):
7-
(tmpdir / "some-dir").mkdir()
8-
(tmpdir / "another-dir").mkdir()
7+
tmp_lib_dir = tmpdir / "lib"
8+
tmp_lib_dir.mkdir()
99

10-
some_dir_out = tmpdir / "some-dir-out"
10+
(tmp_lib_dir / "some-dir").mkdir()
11+
(tmp_lib_dir / "another-dir").mkdir()
12+
13+
some_dir_out = tmp_lib_dir / "some-dir-out"
1114
some_dir_out.mkdir()
1215
# we expect that this file will not be cleaned
1316
(some_dir_out / "module.py").write_text('print("hello")', "utf-8")
1417
# we expect that this file will be cleaned
1518
(some_dir_out / "f3.txt").write_text("this should be cleaned", "utf-8")
1619

17-
another_dir_out = tmpdir / "another-dir-out"
20+
another_dir_out = tmp_lib_dir / "another-dir-out"
1821
another_dir_out.mkdir()
1922
# we expect that this file will be cleaned
2023
(another_dir_out / "module.py").write_text('print("hello")', "utf-8")
2124

2225
proj = create_project(
23-
tmpdir,
26+
tmp_lib_dir,
2427
[
2528
OneScriptConfig(
2629
out_dir="fake",

0 commit comments

Comments
 (0)