@@ -66,6 +66,8 @@ pub enum Error {
66
66
Csv ( #[ from] csv:: Error ) ,
67
67
#[ error( "Expected a Python module with an `__init__.py` at: `{}`" , _0. user_display( ) ) ]
68
68
MissingModule ( PathBuf ) ,
69
+ #[ error( "Absolute module root is not allowed: `{}`" , _0. display( ) ) ]
70
+ AbsoluteModuleRoot ( PathBuf ) ,
69
71
#[ error( "Inconsistent metadata between prepare and build step: `{0}`" ) ]
70
72
InconsistentSteps ( & ' static str ) ,
71
73
#[ error( "Failed to write to {}" , _0. user_display( ) ) ]
@@ -292,11 +294,29 @@ fn write_hashed(
292
294
} )
293
295
}
294
296
297
+ /// TODO(konsti): Wire this up with actual settings and remove this struct.
298
+ ///
299
+ /// Which files to include in the wheel
300
+ pub struct WheelSettings {
301
+ /// The directory that contains the module directory, usually `src`, or an empty path when
302
+ /// using the flat layout over the src layout.
303
+ module_root : PathBuf ,
304
+ }
305
+
306
+ impl Default for WheelSettings {
307
+ fn default ( ) -> Self {
308
+ Self {
309
+ module_root : PathBuf :: from ( "src" ) ,
310
+ }
311
+ }
312
+ }
313
+
295
314
/// Build a wheel from the source tree and place it in the output directory.
296
315
pub fn build_wheel (
297
316
source_tree : & Path ,
298
317
wheel_dir : & Path ,
299
318
metadata_directory : Option < & Path > ,
319
+ wheel_settings : WheelSettings ,
300
320
uv_version : & str ,
301
321
) -> Result < WheelFilename , Error > {
302
322
let contents = fs_err:: read_to_string ( source_tree. join ( "pyproject.toml" ) ) ?;
@@ -319,7 +339,10 @@ pub fn build_wheel(
319
339
let mut wheel_writer = ZipDirectoryWriter :: new_wheel ( File :: create ( & wheel_path) ?) ;
320
340
321
341
debug ! ( "Adding content files to {}" , wheel_path. user_display( ) ) ;
322
- let strip_root = source_tree. join ( "src" ) ;
342
+ if wheel_settings. module_root . is_absolute ( ) {
343
+ return Err ( Error :: AbsoluteModuleRoot ( wheel_settings. module_root ) ) ;
344
+ }
345
+ let strip_root = source_tree. join ( wheel_settings. module_root ) ;
323
346
let module_root = strip_root. join ( pyproject_toml. name ( ) . as_dist_info_name ( ) . as_ref ( ) ) ;
324
347
if !module_root. join ( "__init__.py" ) . is_file ( ) {
325
348
return Err ( Error :: MissingModule ( module_root) ) ;
@@ -337,6 +360,9 @@ pub fn build_wheel(
337
360
let relative_path_str = relative_path
338
361
. to_str ( )
339
362
. ok_or_else ( || Error :: NotUtf8Path ( relative_path. to_path_buf ( ) ) ) ?;
363
+
364
+ debug ! ( "Adding to wheel: `{relative_path_str}`" ) ;
365
+
340
366
if entry. file_type ( ) . is_dir ( ) {
341
367
wheel_writer. write_directory ( relative_path_str) ?;
342
368
} else if entry. file_type ( ) . is_file ( ) {
@@ -514,7 +540,7 @@ pub fn build_source_dist(
514
540
continue ;
515
541
} ;
516
542
517
- add_source_dist_entry ( & mut tar, entry, & top_level, & source_dist_path, & relative) ?;
543
+ add_source_dist_entry ( & mut tar, & entry, & top_level, & source_dist_path, & relative) ?;
518
544
}
519
545
520
546
tar. finish ( )
@@ -526,7 +552,7 @@ pub fn build_source_dist(
526
552
/// Add a file or a directory to a source distribution.
527
553
fn add_source_dist_entry (
528
554
tar : & mut Builder < GzEncoder < File > > ,
529
- entry : DirEntry ,
555
+ entry : & DirEntry ,
530
556
top_level : & str ,
531
557
source_dist_path : & Path ,
532
558
relative : & Path ,
0 commit comments