1313import pathspec
1414from 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
1921class 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
0 commit comments