@@ -18,23 +18,55 @@ class CustomBuildHook(BuildHookInterface):
1818
1919 def initialize (self , version : str , build_data : dict [str , Any ]) -> None :
2020 """Initialize the build hook and add scripts directory files."""
21- # Get the source directory for the package
22- source_dir = Path (self .root ) / "src" / "python_package_folder"
23- scripts_dir = source_dir / "scripts"
24-
2521 # Debug: Print to stderr so it shows in build output
2622 print (f"[DEBUG] Build hook called. Root: { self .root } " , file = sys .stderr )
27- print (f"[DEBUG] Scripts dir exists: { scripts_dir .exists ()} " , file = sys .stderr )
23+
24+ # Try multiple possible locations for the scripts directory
25+ # 1. Source layout: src/python_package_folder/scripts
26+ # 2. Sdist layout: python_package_folder/scripts (after extraction)
27+ # 3. Alternative sdist layout: scripts/ (if extracted differently)
28+ possible_scripts_dirs = [
29+ Path (self .root ) / "src" / "python_package_folder" / "scripts" ,
30+ Path (self .root ) / "python_package_folder" / "scripts" ,
31+ Path (self .root ) / "scripts" ,
32+ ]
33+
34+ scripts_dir = None
35+ for possible_dir in possible_scripts_dirs :
36+ if possible_dir .exists () and possible_dir .is_dir ():
37+ scripts_dir = possible_dir
38+ print (f"[DEBUG] Found scripts dir at: { scripts_dir } " , file = sys .stderr )
39+ break
40+
41+ if scripts_dir is None :
42+ print (f"[DEBUG] Scripts directory not found. Tried: { [str (d ) for d in possible_scripts_dirs ]} " , file = sys .stderr )
43+ return
2844
2945 # If scripts directory exists, include all files from it
3046 if scripts_dir .exists () and scripts_dir .is_dir ():
3147 # Add all files from scripts directory to force-include
3248 # This ensures they're included in the wheel at the correct location
3349 for script_file in scripts_dir .iterdir ():
3450 if script_file .is_file ():
35- # Calculate relative paths
36- source_path = script_file .relative_to (self .root )
37- # Target path inside the wheel package
51+ # Calculate relative paths from project root
52+ try :
53+ source_path = script_file .relative_to (self .root )
54+ except ValueError :
55+ # If relative_to fails, try to construct path manually
56+ # This can happen with sdist layouts
57+ if "python_package_folder" in str (script_file ):
58+ # Extract the part after python_package_folder
59+ parts = script_file .parts
60+ try :
61+ idx = parts .index ("python_package_folder" )
62+ source_path = Path (* parts [idx :])
63+ except (ValueError , IndexError ):
64+ # Fallback: use the filename
65+ source_path = Path ("python_package_folder" ) / "scripts" / script_file .name
66+ else :
67+ source_path = Path ("python_package_folder" ) / "scripts" / script_file .name
68+
69+ # Target path inside the wheel package (always the same)
3870 target_path = f"python_package_folder/scripts/{ script_file .name } "
3971
4072 print (f"[DEBUG] Adding { source_path } -> { target_path } " , file = sys .stderr )
@@ -46,8 +78,6 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
4678 build_data ["force_include" ][str (source_path )] = target_path
4779
4880 print (f"[DEBUG] force_include now has { len (build_data .get ('force_include' , {}))} entries" , file = sys .stderr )
49- else :
50- print (f"[DEBUG] Scripts directory not found at { scripts_dir } " , file = sys .stderr )
5181
5282
5383# Export the hook class (hatchling might need this)
0 commit comments