mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-07 21:35:00 +00:00
Remove async
from some filesystem-only APIs (#146)
This commit is contained in:
parent
03101c6a5c
commit
bcd281eb1f
12 changed files with 32 additions and 34 deletions
|
@ -436,7 +436,7 @@ async fn resolve_and_install(
|
||||||
debug!("Installing {} build requirements", requirements.len());
|
debug!("Installing {} build requirements", requirements.len());
|
||||||
|
|
||||||
let local_index = if let Some(cache) = cache {
|
let local_index = if let Some(cache) = cache {
|
||||||
LocalIndex::from_directory(cache).await?
|
LocalIndex::try_from_directory(cache)?
|
||||||
} else {
|
} else {
|
||||||
LocalIndex::default()
|
LocalIndex::default()
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,7 +11,7 @@ use crate::commands::ExitStatus;
|
||||||
use crate::printer::Printer;
|
use crate::printer::Printer;
|
||||||
|
|
||||||
/// Enumerate the installed packages in the current environment.
|
/// Enumerate the installed packages in the current environment.
|
||||||
pub(crate) async fn freeze(cache: Option<&Path>, _printer: Printer) -> Result<ExitStatus> {
|
pub(crate) fn freeze(cache: Option<&Path>, _printer: Printer) -> Result<ExitStatus> {
|
||||||
// Detect the current Python interpreter.
|
// Detect the current Python interpreter.
|
||||||
let platform = Platform::current()?;
|
let platform = Platform::current()?;
|
||||||
let python = PythonExecutable::from_env(platform, cache)?;
|
let python = PythonExecutable::from_env(platform, cache)?;
|
||||||
|
@ -21,7 +21,7 @@ pub(crate) async fn freeze(cache: Option<&Path>, _printer: Printer) -> Result<Ex
|
||||||
);
|
);
|
||||||
|
|
||||||
// Build the installed index.
|
// Build the installed index.
|
||||||
let site_packages = SitePackages::from_executable(&python).await?;
|
let site_packages = SitePackages::try_from_executable(&python)?;
|
||||||
for (name, dist_info) in site_packages.iter() {
|
for (name, dist_info) in site_packages.iter() {
|
||||||
#[allow(clippy::print_stdout)]
|
#[allow(clippy::print_stdout)]
|
||||||
{
|
{
|
||||||
|
|
|
@ -69,7 +69,7 @@ pub(crate) async fn sync_requirements(
|
||||||
local,
|
local,
|
||||||
remote,
|
remote,
|
||||||
extraneous,
|
extraneous,
|
||||||
} = PartitionedRequirements::try_from_requirements(requirements, cache, &python).await?;
|
} = PartitionedRequirements::try_from_requirements(requirements, cache, &python)?;
|
||||||
|
|
||||||
// Nothing to do.
|
// Nothing to do.
|
||||||
if remote.is_empty() && local.is_empty() && extraneous.is_empty() {
|
if remote.is_empty() && local.is_empty() && extraneous.is_empty() {
|
||||||
|
@ -284,17 +284,17 @@ struct PartitionedRequirements {
|
||||||
impl PartitionedRequirements {
|
impl PartitionedRequirements {
|
||||||
/// Partition a set of requirements into those that should be linked from the cache, those that
|
/// Partition a set of requirements into those that should be linked from the cache, those that
|
||||||
/// need to be downloaded, and those that should be removed.
|
/// need to be downloaded, and those that should be removed.
|
||||||
pub(crate) async fn try_from_requirements(
|
pub(crate) fn try_from_requirements(
|
||||||
requirements: &[Requirement],
|
requirements: &[Requirement],
|
||||||
cache: Option<&Path>,
|
cache: Option<&Path>,
|
||||||
python: &PythonExecutable,
|
python: &PythonExecutable,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
// Index all the already-installed packages in site-packages.
|
// Index all the already-installed packages in site-packages.
|
||||||
let mut site_packages = SitePackages::from_executable(python).await?;
|
let mut site_packages = SitePackages::try_from_executable(python)?;
|
||||||
|
|
||||||
// Index all the already-downloaded wheels in the cache.
|
// Index all the already-downloaded wheels in the cache.
|
||||||
let local_index = if let Some(cache) = cache {
|
let local_index = if let Some(cache) = cache {
|
||||||
LocalIndex::from_directory(cache).await?
|
LocalIndex::try_from_directory(cache)?
|
||||||
} else {
|
} else {
|
||||||
LocalIndex::default()
|
LocalIndex::default()
|
||||||
};
|
};
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub(crate) async fn pip_uninstall(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Index the current `site-packages` directory.
|
// Index the current `site-packages` directory.
|
||||||
let site_packages = puffin_installer::SitePackages::from_executable(&python).await?;
|
let site_packages = puffin_installer::SitePackages::try_from_executable(&python)?;
|
||||||
|
|
||||||
// Sort and deduplicate the requirements.
|
// Sort and deduplicate the requirements.
|
||||||
let packages = {
|
let packages = {
|
||||||
|
|
|
@ -198,7 +198,6 @@ async fn main() -> ExitCode {
|
||||||
.filter(|_| !cli.no_cache),
|
.filter(|_| !cli.no_cache),
|
||||||
printer,
|
printer,
|
||||||
)
|
)
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
Commands::Venv(args) => commands::venv(&args.name, args.python.as_deref(), printer).await,
|
Commands::Venv(args) => commands::venv(&args.name, args.python.as_deref(), printer).await,
|
||||||
Commands::Add(args) => commands::add(&args.name, printer),
|
Commands::Add(args) => commands::add(&args.name, printer),
|
||||||
|
|
|
@ -26,4 +26,4 @@ tokio = { workspace = true }
|
||||||
tokio-util = { workspace = true }
|
tokio-util = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
url = { workspace = true }
|
url = { workspace = true }
|
||||||
zip = { workspace = true }
|
zip = { workspace = true }
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use fs_err::tokio as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
static WHEEL_CACHE: &str = "wheels-v0";
|
static WHEEL_CACHE: &str = "wheels-v0";
|
||||||
|
|
||||||
|
@ -23,13 +23,13 @@ impl WheelCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize the wheel cache.
|
/// Initialize the wheel cache.
|
||||||
pub(crate) async fn init(&self) -> std::io::Result<()> {
|
pub(crate) fn init(&self) -> std::io::Result<()> {
|
||||||
fs::create_dir_all(&self.root).await
|
fs::create_dir_all(&self.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a handle to the wheel cache directory.
|
/// Returns a handle to the wheel cache directory.
|
||||||
pub(crate) async fn read_dir(&self) -> std::io::Result<fs::ReadDir> {
|
pub(crate) fn read_dir(&self) -> std::io::Result<fs::ReadDir> {
|
||||||
fs::read_dir(&self.root).await
|
fs::read_dir(&self.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the cache root.
|
/// Returns the cache root.
|
||||||
|
|
|
@ -47,7 +47,7 @@ impl<'a> Downloader<'a> {
|
||||||
) -> Result<Vec<InMemoryDistribution>> {
|
) -> Result<Vec<InMemoryDistribution>> {
|
||||||
// Create the wheel cache subdirectory, if necessary.
|
// Create the wheel cache subdirectory, if necessary.
|
||||||
let wheel_cache = WheelCache::new(target);
|
let wheel_cache = WheelCache::new(target);
|
||||||
wheel_cache.init().await?;
|
wheel_cache.init()?;
|
||||||
|
|
||||||
// Phase 1: Fetch the wheels in parallel.
|
// Phase 1: Fetch the wheels in parallel.
|
||||||
let mut fetches = JoinSet::new();
|
let mut fetches = JoinSet::new();
|
||||||
|
|
|
@ -2,8 +2,8 @@ pub use distribution::{
|
||||||
CachedDistribution, Distribution, InstalledDistribution, RemoteDistribution,
|
CachedDistribution, Distribution, InstalledDistribution, RemoteDistribution,
|
||||||
};
|
};
|
||||||
pub use downloader::{Downloader, Reporter as DownloadReporter};
|
pub use downloader::{Downloader, Reporter as DownloadReporter};
|
||||||
pub use index::LocalIndex;
|
|
||||||
pub use installer::{Installer, Reporter as InstallReporter};
|
pub use installer::{Installer, Reporter as InstallReporter};
|
||||||
|
pub use local_index::LocalIndex;
|
||||||
pub use site_packages::SitePackages;
|
pub use site_packages::SitePackages;
|
||||||
pub use uninstall::uninstall;
|
pub use uninstall::uninstall;
|
||||||
pub use unzipper::{Reporter as UnzipReporter, Unzipper};
|
pub use unzipper::{Reporter as UnzipReporter, Unzipper};
|
||||||
|
@ -11,8 +11,8 @@ pub use unzipper::{Reporter as UnzipReporter, Unzipper};
|
||||||
mod cache;
|
mod cache;
|
||||||
mod distribution;
|
mod distribution;
|
||||||
mod downloader;
|
mod downloader;
|
||||||
mod index;
|
|
||||||
mod installer;
|
mod installer;
|
||||||
|
mod local_index;
|
||||||
mod site_packages;
|
mod site_packages;
|
||||||
mod uninstall;
|
mod uninstall;
|
||||||
mod unzipper;
|
mod unzipper;
|
||||||
|
|
|
@ -14,16 +14,17 @@ pub struct LocalIndex(HashMap<PackageName, CachedDistribution>);
|
||||||
|
|
||||||
impl LocalIndex {
|
impl LocalIndex {
|
||||||
/// Build an index of cached distributions from a directory.
|
/// Build an index of cached distributions from a directory.
|
||||||
pub async fn from_directory(path: &Path) -> Result<Self> {
|
pub fn try_from_directory(path: &Path) -> Result<Self> {
|
||||||
let mut index = HashMap::new();
|
let mut index = HashMap::new();
|
||||||
|
|
||||||
let cache = WheelCache::new(path);
|
let cache = WheelCache::new(path);
|
||||||
let Ok(mut dir) = cache.read_dir().await else {
|
let Ok(dir) = cache.read_dir() else {
|
||||||
return Ok(Self(index));
|
return Ok(Self(index));
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Some(entry) = dir.next_entry().await? {
|
for entry in dir {
|
||||||
if entry.file_type().await?.is_dir() {
|
let entry = entry?;
|
||||||
|
if entry.file_type()?.is_dir() {
|
||||||
if let Some(dist_info) = CachedDistribution::try_from_path(&entry.path())? {
|
if let Some(dist_info) = CachedDistribution::try_from_path(&entry.path())? {
|
||||||
index.insert(dist_info.name().clone(), dist_info);
|
index.insert(dist_info.name().clone(), dist_info);
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use fs_err::tokio as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
use puffin_interpreter::PythonExecutable;
|
use puffin_interpreter::PythonExecutable;
|
||||||
use puffin_package::package_name::PackageName;
|
use puffin_package::package_name::PackageName;
|
||||||
|
@ -13,12 +13,12 @@ pub struct SitePackages(BTreeMap<PackageName, InstalledDistribution>);
|
||||||
|
|
||||||
impl SitePackages {
|
impl SitePackages {
|
||||||
/// Build an index of installed packages from the given Python executable.
|
/// Build an index of installed packages from the given Python executable.
|
||||||
pub async fn from_executable(python: &PythonExecutable) -> Result<Self> {
|
pub fn try_from_executable(python: &PythonExecutable) -> Result<Self> {
|
||||||
let mut index = BTreeMap::new();
|
let mut index = BTreeMap::new();
|
||||||
|
|
||||||
let mut dir = fs::read_dir(python.site_packages()).await?;
|
for entry in fs::read_dir(python.site_packages())? {
|
||||||
while let Some(entry) = dir.next_entry().await? {
|
let entry = entry?;
|
||||||
if entry.file_type().await?.is_dir() {
|
if entry.file_type()?.is_dir() {
|
||||||
if let Some(dist_info) = InstalledDistribution::try_from_path(&entry.path())? {
|
if let Some(dist_info) = InstalledDistribution::try_from_path(&entry.path())? {
|
||||||
index.insert(dist_info.name().clone(), dist_info);
|
index.insert(dist_info.name().clone(), dist_info);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use fs_err::tokio as fs;
|
|
||||||
use fs_err::File;
|
|
||||||
use rayon::iter::ParallelBridge;
|
use rayon::iter::ParallelBridge;
|
||||||
use rayon::iter::ParallelIterator;
|
use rayon::iter::ParallelIterator;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
@ -38,7 +36,7 @@ impl Unzipper {
|
||||||
) -> Result<Vec<CachedDistribution>> {
|
) -> Result<Vec<CachedDistribution>> {
|
||||||
// Create the wheel cache subdirectory, if necessary.
|
// Create the wheel cache subdirectory, if necessary.
|
||||||
let wheel_cache = WheelCache::new(target);
|
let wheel_cache = WheelCache::new(target);
|
||||||
wheel_cache.init().await?;
|
wheel_cache.init()?;
|
||||||
|
|
||||||
let staging = tempfile::tempdir_in(wheel_cache.root())?;
|
let staging = tempfile::tempdir_in(wheel_cache.root())?;
|
||||||
|
|
||||||
|
@ -57,7 +55,7 @@ impl Unzipper {
|
||||||
.await??;
|
.await??;
|
||||||
|
|
||||||
// Write the unzipped wheel to the target directory.
|
// Write the unzipped wheel to the target directory.
|
||||||
fs::rename(
|
fs_err::tokio::rename(
|
||||||
staging.path().join(remote.id()),
|
staging.path().join(remote.id()),
|
||||||
wheel_cache.entry(&remote.id()),
|
wheel_cache.entry(&remote.id()),
|
||||||
)
|
)
|
||||||
|
@ -104,15 +102,15 @@ fn unzip_wheel(wheel: InMemoryDistribution, target: &Path) -> Result<()> {
|
||||||
// Create necessary parent directories.
|
// Create necessary parent directories.
|
||||||
let path = target.join(file_path);
|
let path = target.join(file_path);
|
||||||
if file.is_dir() {
|
if file.is_dir() {
|
||||||
std::fs::create_dir_all(path)?;
|
fs_err::create_dir_all(path)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if let Some(parent) = path.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
std::fs::create_dir_all(parent)?;
|
fs_err::create_dir_all(parent)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the file.
|
// Write the file.
|
||||||
let mut outfile = File::create(&path)?;
|
let mut outfile = fs_err::File::create(&path)?;
|
||||||
std::io::copy(&mut file, &mut outfile)?;
|
std::io::copy(&mut file, &mut outfile)?;
|
||||||
|
|
||||||
// Set permissions.
|
// Set permissions.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue