13
13
import pathspec
14
14
from hatchling .builders .hooks .plugin .interface import BuildHookInterface
15
15
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 )
17
19
18
20
19
21
class BuildScriptsHook (BuildHookInterface ):
@@ -31,10 +33,11 @@ def initialize(
31
33
for script in all_scripts :
32
34
if script .clean_out_dir :
33
35
out_dir = Path (self .root , script .out_dir )
34
- logger . info (f"Cleaning { out_dir } " )
36
+ log . debug (f"Cleaning { out_dir } " )
35
37
shutil .rmtree (out_dir , ignore_errors = True )
36
38
elif script .clean_artifacts :
37
39
for out_file in script .out_files (self .root ):
40
+ log .debug (f"Cleaning { out_file } " )
38
41
out_file .unlink (missing_ok = True )
39
42
40
43
for script in all_scripts :
@@ -43,16 +46,20 @@ def initialize(
43
46
out_dir .mkdir (parents = True , exist_ok = True )
44
47
45
48
for cmd in script .commands :
49
+ log .info (f"Running command: { cmd } " )
46
50
run (cmd , cwd = str (work_dir ), check = True , shell = True ) # noqa: S602
47
51
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 } " )
52
57
if src_file not in created :
53
58
out_file .parent .mkdir (parents = True , exist_ok = True )
54
59
shutil .copyfile (src_file , out_file )
55
60
created .add (out_file )
61
+ else :
62
+ log .debug (f"Skipping { src_file } - already exists" )
56
63
57
64
build_data ["artifacts" ].append (str (out_dir .relative_to (self .root )))
58
65
@@ -92,28 +99,24 @@ def __post_init__(self) -> None:
92
99
self .out_dir = conv_path (self .out_dir )
93
100
self .work_dir = conv_path (self .work_dir )
94
101
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 ():
99
106
return []
100
107
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 )
103
110
]
104
111
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 ():
109
116
return []
110
117
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 )
117
120
]
118
121
119
122
@cached_property
0 commit comments