Struct WheelBuilder

Source
pub struct WheelBuilder { /* private fields */ }
Expand description

Define and build a Python wheel from raw components.

Python wheels are glorified zip files with some special files annotating the Python component therein.

§Wheel Level Parameters

Wheels are defined by a distribution (e.g. a Python package name), a version, a compatibility tag, and an optional build tag.

The compatibility tag defines the Python, ABI, and platform compatibility of the wheel. See PEP 425 for an overview of the components of the compatibility tag and their potential values.

Our default compatibility tag value is py3-none-any. This is appropriate for a wheel containing pure Python code that is compatible with Python 3. If your wheel has binary executables or extension modules, you will want to update the compatibility tag to reflect the appropriate binary compatibility.

§.dist-info/WHEEL File

Wheel archives must have a WHEEL file describing the wheel itself.

This file is an email header like MIME document with various well-defined fields.

By default, we will automatically derive a minimal WHEEL file based on parameters passed into Self::new and defaults.

If you want to provide your own WHEEL file, simply define its content by adding a custom file through Self::add_file_dist_info.

§.dist-info/METADATA File

Wheel archives must have a METADATA file describing the thing being distributed.

This file is an email header like MIME document with various well-defined fields.

By default, we will automatically derive a minimal METADATA file based on builder state.

If you want to provide your own METADATA file, simply define its content by adding a custom file through Self::add_file_dist_info.

§Adding Files

Files in wheels go in 1 of 3 locations:

  1. The .dist-info/ directory (added via Self::add_file_dist_info).
  2. Special .data/<location>/ directories (added via Self::add_file_data).
  3. Everywhere else (added via Self::add_file).

Files in .dist-info/ describe the wheel itself and the entity being distributed.

Files in .data/<location>/ are moved to the indicated <location> when the wheel is installed. <location> here is the name of a Python installation directory, such as purelib (pure Python modules and bytecode), platlib (platform-specific / binary Python extension modules and other binaries), scripts (executable scripts), and more.

Files in all other locations in the archive are not treated specially and are extracted directly to purelib or platlib, depending on the value of Root-Is-Purelib.

§Building Wheels

Once you have modified settings and registered files, it is time to create your wheel.

If you want to materialize a .whl file with the proper file name, call Self::write_wheel_into_directory.

If you want to just materialize the zip content of the wheel, call Self::write_wheel_data.

If you want to obtain a collection of all the files that constitute the wheel before zip file generation, call Self::build_file_manifest.

To obtain the name of the .whl file given current settings, call Self::wheel_file_name.

Wheel zip archive content is deterministic for the same builder instance. For separate builder instances, content can be made identical by calling Self::set_modified_time to set the modified time and using identical input settings/files. (The modified time of files in zip files defaults to the time when the builder instance was created, which is obviously not deterministic.)

§Validation

This type generally performs little to no validation of input data. It is up to the caller to supply settings and content that constitutes a well-formed wheel.

Supplementary tools like auditwheel can be useful for validating the content of wheels.

Implementations§

Source§

impl WheelBuilder

Source

pub fn new(distribution: impl ToString, version: impl ToString) -> Self

Create a new instance with a package name and version.

Source

pub fn build_tag(&self) -> Option<&str>

Obtain the build tag for this wheel.

Source

pub fn set_build_tag(&mut self, v: impl ToString)

Set the build tag for this wheel.

Source

pub fn tag(&self) -> String

Obtain the compatibility tag.

Source

pub fn set_tag(&mut self, tag: impl ToString) -> Result<()>

Set the compatibility tag from a value.

Source

pub fn python_tag(&self) -> &str

Obtain the Python component of the compatibility tag.

Source

pub fn set_python_tag(&mut self, v: impl ToString)

Set the Python component of the compatibility tag.

Source

pub fn abi_tag(&self) -> &str

Obtain the ABI component of the compatibility tag.

Source

pub fn set_abi_tag(&mut self, v: impl ToString)

Set the ABI component of the compatibility tag.

Source

pub fn platform_tag(&self) -> &str

Obtain the platform component of the compatibility tag.

Source

pub fn set_platform_tag(&mut self, v: impl ToString)

Set the platform component of the compatibility tag.

Source

pub fn generator(&self) -> &str

Obtain the Generator value for the WHEEL file.

Source

pub fn set_generator(&mut self, v: impl ToString)

Set the Generator value for the WHEEL file.

Source

pub fn root_is_purelib(&self) -> bool

Obtain the Root-Is-Purelib value.

Source

pub fn set_root_is_purelib(&mut self, v: bool)

Set the value for Root-Is-Purelib.

If true, the wheel archive is extracted directly into purelib. If false, it is extracted to platlib.

Source

pub fn modified_time(&self) -> OffsetDateTime

Obtain the modified time for files in the wheel archive.

Source

pub fn set_modified_time(&mut self, v: OffsetDateTime)

Set the modified time for files in the wheel archive.

Source

pub fn add_file( &mut self, path: impl AsRef<Path>, file: impl Into<FileEntry>, ) -> Result<()>

Add a file to the wheel at the given path.

No validation of the path is performed.

Source

pub fn add_file_dist_info( &mut self, path: impl AsRef<Path>, file: impl Into<FileEntry>, ) -> Result<()>

Add a file to the .dist-info/ directory.

Attempts to add the RECORD file will work. However, the content will be ignored and regenerated as part of wheel building.

Source

pub fn add_file_data( &mut self, destination: impl ToString, path: impl AsRef<Path>, file: impl Into<FileEntry>, ) -> Result<()>

Add a file to a .data/<destination>/ directory.

destination is the name of a well-known Python installation directory. e.g. {purelib, platlib, headers, scripts, data}. When the wheel is installed, files in these .data/<destination>/ directories are moved to the corresponding path location within the targeted environment.

No validation of the destination values is performed.

Source

pub fn derive_record_file(&self, manifest: &FileManifest) -> Result<String>

Derive the content of a .dist-info/RECORD file in a wheel.

This iterates the contents of a FileManifest and derives digests and other metadata and assembles it into the appropriate format.

Source

pub fn wheel_file_name(&self) -> String

Obtain the file name for this wheel, as currently configured.

The file name of a wheel is of the form {distribution}-{version}(-{build tag})?-{python tag}-{abi tag}-{platform tag}.whl, per PEP 427. Each component is escaped with a regular expression.

Source

pub fn build_file_manifest(&self) -> Result<FileManifest>

Obtain a FileManifest holding the contents of the built wheel.

This function does most of the work to construct the built wheel. It will derive special files like .dist-info/WHEEL and .dist-info/RECORD and join them with files already registered in the builder.

Source

pub fn write_wheel_data(&self, writer: &mut (impl Write + Seek)) -> Result<()>

Writes the contents of a wheel file to a writable destination.

Wheels are zip files. So this function effectively materializes a zip file to the specified writer.

Source

pub fn write_wheel_into_directory( &self, directory: impl AsRef<Path>, ) -> Result<PathBuf>

Write the wheel file into a given directory, which must exist.

Returns the path of the written wheel file on success.

The wheel file isn’t created until after wheel content generation. So the only scenario in which the file would exist but not have appropriate content is if some kind of I/O error occurred.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.