uv/crates/uv-dev/src/compile.rs
Charlie Marsh ac84f5aedc
Move preview features into a dedicated crate (#15482)
## Summary

This is causing some cyclic dependencies issues for me, because these
can be used in virtually _any_ crate (like `uv-install-wheel`), which
then means that all of `uv-configuration` becomes a dependency, etc. I
think this should be a leaf crate so that we can safely depend on it
anywhere.
2025-08-24 09:55:30 -04:00

46 lines
1.2 KiB
Rust

use std::path::PathBuf;
use clap::Parser;
use tracing::info;
use uv_cache::{Cache, CacheArgs};
use uv_configuration::Concurrency;
use uv_preview::Preview;
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonPreference, PythonRequest};
#[derive(Parser)]
pub(crate) struct CompileArgs {
/// Compile all `.py` in this or any subdirectory to bytecode
root: PathBuf,
python: Option<PathBuf>,
#[command(flatten)]
cache_args: CacheArgs,
}
pub(crate) async fn compile(args: CompileArgs) -> anyhow::Result<()> {
let cache = Cache::try_from(args.cache_args)?.init()?;
let interpreter = if let Some(python) = args.python {
python
} else {
let interpreter = PythonEnvironment::find(
&PythonRequest::default(),
EnvironmentPreference::OnlyVirtual,
PythonPreference::default(),
&cache,
Preview::default(),
)?
.into_interpreter();
interpreter.sys_executable().to_path_buf()
};
let files = uv_installer::compile_tree(
&fs_err::canonicalize(args.root)?,
&interpreter,
&Concurrency::default(),
cache.root(),
)
.await?;
info!("Compiled {files} files");
Ok(())
}