Remove async from some filesystem-only APIs (#146)

This commit is contained in:
Charlie Marsh 2023-10-19 21:08:51 -04:00 committed by GitHub
parent 03101c6a5c
commit bcd281eb1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 32 additions and 34 deletions

View file

@ -436,7 +436,7 @@ async fn resolve_and_install(
debug!("Installing {} build requirements", requirements.len());
let local_index = if let Some(cache) = cache {
LocalIndex::from_directory(cache).await?
LocalIndex::try_from_directory(cache)?
} else {
LocalIndex::default()
};

View file

@ -11,7 +11,7 @@ use crate::commands::ExitStatus;
use crate::printer::Printer;
/// 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.
let platform = Platform::current()?;
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.
let site_packages = SitePackages::from_executable(&python).await?;
let site_packages = SitePackages::try_from_executable(&python)?;
for (name, dist_info) in site_packages.iter() {
#[allow(clippy::print_stdout)]
{

View file

@ -69,7 +69,7 @@ pub(crate) async fn sync_requirements(
local,
remote,
extraneous,
} = PartitionedRequirements::try_from_requirements(requirements, cache, &python).await?;
} = PartitionedRequirements::try_from_requirements(requirements, cache, &python)?;
// Nothing to do.
if remote.is_empty() && local.is_empty() && extraneous.is_empty() {
@ -284,17 +284,17 @@ struct PartitionedRequirements {
impl PartitionedRequirements {
/// 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.
pub(crate) async fn try_from_requirements(
pub(crate) fn try_from_requirements(
requirements: &[Requirement],
cache: Option<&Path>,
python: &PythonExecutable,
) -> Result<Self> {
// 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.
let local_index = if let Some(cache) = cache {
LocalIndex::from_directory(cache).await?
LocalIndex::try_from_directory(cache)?
} else {
LocalIndex::default()
};

View file

@ -39,7 +39,7 @@ pub(crate) async fn pip_uninstall(
);
// 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.
let packages = {

View file

@ -198,7 +198,6 @@ async fn main() -> ExitCode {
.filter(|_| !cli.no_cache),
printer,
)
.await
}
Commands::Venv(args) => commands::venv(&args.name, args.python.as_deref(), printer).await,
Commands::Add(args) => commands::add(&args.name, printer),

View file

@ -26,4 +26,4 @@ tokio = { workspace = true }
tokio-util = { workspace = true }
tracing = { workspace = true }
url = { workspace = true }
zip = { workspace = true }
zip = { workspace = true }

View file

@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};
use fs_err::tokio as fs;
use fs_err as fs;
static WHEEL_CACHE: &str = "wheels-v0";
@ -23,13 +23,13 @@ impl WheelCache {
}
/// Initialize the wheel cache.
pub(crate) async fn init(&self) -> std::io::Result<()> {
fs::create_dir_all(&self.root).await
pub(crate) fn init(&self) -> std::io::Result<()> {
fs::create_dir_all(&self.root)
}
/// Returns a handle to the wheel cache directory.
pub(crate) async fn read_dir(&self) -> std::io::Result<fs::ReadDir> {
fs::read_dir(&self.root).await
pub(crate) fn read_dir(&self) -> std::io::Result<fs::ReadDir> {
fs::read_dir(&self.root)
}
/// Returns the cache root.

View file

@ -47,7 +47,7 @@ impl<'a> Downloader<'a> {
) -> Result<Vec<InMemoryDistribution>> {
// Create the wheel cache subdirectory, if necessary.
let wheel_cache = WheelCache::new(target);
wheel_cache.init().await?;
wheel_cache.init()?;
// Phase 1: Fetch the wheels in parallel.
let mut fetches = JoinSet::new();

View file

@ -2,8 +2,8 @@ pub use distribution::{
CachedDistribution, Distribution, InstalledDistribution, RemoteDistribution,
};
pub use downloader::{Downloader, Reporter as DownloadReporter};
pub use index::LocalIndex;
pub use installer::{Installer, Reporter as InstallReporter};
pub use local_index::LocalIndex;
pub use site_packages::SitePackages;
pub use uninstall::uninstall;
pub use unzipper::{Reporter as UnzipReporter, Unzipper};
@ -11,8 +11,8 @@ pub use unzipper::{Reporter as UnzipReporter, Unzipper};
mod cache;
mod distribution;
mod downloader;
mod index;
mod installer;
mod local_index;
mod site_packages;
mod uninstall;
mod unzipper;

View file

@ -14,16 +14,17 @@ pub struct LocalIndex(HashMap<PackageName, CachedDistribution>);
impl LocalIndex {
/// 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 cache = WheelCache::new(path);
let Ok(mut dir) = cache.read_dir().await else {
let Ok(dir) = cache.read_dir() else {
return Ok(Self(index));
};
while let Some(entry) = dir.next_entry().await? {
if entry.file_type().await?.is_dir() {
for entry in dir {
let entry = entry?;
if entry.file_type()?.is_dir() {
if let Some(dist_info) = CachedDistribution::try_from_path(&entry.path())? {
index.insert(dist_info.name().clone(), dist_info);
}

View file

@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use anyhow::Result;
use fs_err::tokio as fs;
use fs_err as fs;
use puffin_interpreter::PythonExecutable;
use puffin_package::package_name::PackageName;
@ -13,12 +13,12 @@ pub struct SitePackages(BTreeMap<PackageName, InstalledDistribution>);
impl SitePackages {
/// 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 dir = fs::read_dir(python.site_packages()).await?;
while let Some(entry) = dir.next_entry().await? {
if entry.file_type().await?.is_dir() {
for entry in fs::read_dir(python.site_packages())? {
let entry = entry?;
if entry.file_type()?.is_dir() {
if let Some(dist_info) = InstalledDistribution::try_from_path(&entry.path())? {
index.insert(dist_info.name().clone(), dist_info);
}

View file

@ -1,8 +1,6 @@
use std::path::Path;
use anyhow::Result;
use fs_err::tokio as fs;
use fs_err::File;
use rayon::iter::ParallelBridge;
use rayon::iter::ParallelIterator;
use tracing::debug;
@ -38,7 +36,7 @@ impl Unzipper {
) -> Result<Vec<CachedDistribution>> {
// Create the wheel cache subdirectory, if necessary.
let wheel_cache = WheelCache::new(target);
wheel_cache.init().await?;
wheel_cache.init()?;
let staging = tempfile::tempdir_in(wheel_cache.root())?;
@ -57,7 +55,7 @@ impl Unzipper {
.await??;
// Write the unzipped wheel to the target directory.
fs::rename(
fs_err::tokio::rename(
staging.path().join(remote.id()),
wheel_cache.entry(&remote.id()),
)
@ -104,15 +102,15 @@ fn unzip_wheel(wheel: InMemoryDistribution, target: &Path) -> Result<()> {
// Create necessary parent directories.
let path = target.join(file_path);
if file.is_dir() {
std::fs::create_dir_all(path)?;
fs_err::create_dir_all(path)?;
return Ok(());
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
fs_err::create_dir_all(parent)?;
}
// Write the file.
let mut outfile = File::create(&path)?;
let mut outfile = fs_err::File::create(&path)?;
std::io::copy(&mut file, &mut outfile)?;
// Set permissions.