Files
uv/crates/uv-dev/src/build.rs
T

95 lines
2.8 KiB
Rust
Raw Normal View History

2023-11-01 16:46:37 +01:00
use std::env;
use std::path::PathBuf;
use anyhow::{Context, Result};
2023-10-16 14:43:31 +02:00
use clap::Parser;
use fs_err as fs;
2023-11-01 16:46:37 +01:00
use distribution_types::IndexLocations;
use platform_host::Platform;
2024-02-15 11:19:46 -06:00
use uv_build::{SourceBuild, SourceBuildContext};
use uv_cache::{Cache, CacheArgs};
use uv_client::{FlatIndex, RegistryClientBuilder};
use uv_dispatch::BuildDispatch;
use uv_installer::NoBinary;
use uv_interpreter::Virtualenv;
use uv_resolver::InMemoryIndex;
use uv_traits::{BuildContext, BuildKind, ConfigSettings, InFlight, NoBuild, SetupPyStrategy};
2023-10-16 14:43:31 +02:00
#[derive(Parser)]
2023-11-01 16:46:37 +01:00
pub(crate) struct BuildArgs {
2023-10-16 14:43:31 +02:00
/// Base python in a way that can be found with `which`
/// TODO(konstin): Also use proper python parsing here
#[clap(short, long)]
python: Option<PathBuf>,
/// Directory to story the built wheel in
#[clap(short, long)]
wheels: Option<PathBuf>,
/// The source distribution to build, either a directory or a source archive.
2023-10-16 14:43:31 +02:00
sdist: PathBuf,
/// The subdirectory to build within the source distribution.
subdirectory: Option<PathBuf>,
/// You can edit the python sources of an editable install and the changes will be used without
/// the need to reinstall it.
#[clap(short, long)]
editable: bool,
2023-11-16 21:49:48 +01:00
#[command(flatten)]
cache_args: CacheArgs,
2023-10-16 14:43:31 +02:00
}
2023-10-26 11:17:22 +02:00
/// Build a source distribution to a wheel
pub(crate) async fn build(args: BuildArgs) -> Result<PathBuf> {
2023-10-16 14:43:31 +02:00
let wheel_dir = if let Some(wheel_dir) = args.wheels {
fs::create_dir_all(&wheel_dir).context("Invalid wheel directory")?;
wheel_dir
} else {
env::current_dir()?
};
let build_kind = if args.editable {
BuildKind::Editable
} else {
BuildKind::Wheel
};
2023-10-16 14:43:31 +02:00
let cache = Cache::try_from(args.cache_args)?;
let platform = Platform::current()?;
let venv = Virtualenv::from_env(platform, &cache)?;
2023-12-18 11:43:03 -05:00
let client = RegistryClientBuilder::new(cache.clone()).build();
let index_urls = IndexLocations::default();
2024-01-15 11:02:02 -05:00
let flat_index = FlatIndex::default();
let index = InMemoryIndex::default();
2024-01-09 20:27:06 -05:00
let setup_py = SetupPyStrategy::default();
let in_flight = InFlight::default();
let config_settings = ConfigSettings::default();
let build_dispatch = BuildDispatch::new(
2023-12-18 11:43:03 -05:00
&client,
&cache,
venv.interpreter(),
&index_urls,
2024-01-15 11:02:02 -05:00
&flat_index,
&index,
&in_flight,
2023-12-18 09:42:58 -05:00
venv.python_executable(),
2024-01-09 20:27:06 -05:00
setup_py,
&config_settings,
&NoBuild::None,
&NoBinary::None,
);
let builder = SourceBuild::setup(
&args.sdist,
args.subdirectory.as_deref(),
build_dispatch.interpreter(),
&build_dispatch,
SourceBuildContext::default(),
2023-12-12 15:56:15 -05:00
args.sdist.display().to_string(),
2024-01-09 20:27:06 -05:00
setup_py,
config_settings.clone(),
build_kind,
)
.await?;
Ok(wheel_dir.join(builder.build(&wheel_dir).await?))
2023-10-16 14:43:31 +02:00
}