Move architecture and operating system probing to Python (#2381)

The architecture of uv does not necessarily match that of the python
interpreter (#2326). In cross compiling/testing scenarios the operating
system can also mismatch. To solve this, we move arch and os detection
to python, vendoring the relevant pypa/packaging code, preventing
mismatches between what the python interpreter was compiled for and what
uv was compiled for.

To make the scripts more manageable, they are now a directory in a
tempdir and we run them with `python -m` . I've simplified the
pypa/packaging code since we're still building the tags in rust. A
`Platform` is now instantiated by querying the python interpreter for
its platform. The pypa/packaging files are copied verbatim for easier
updates except a `lru_cache()` python 3.7 backport.

Error handling is done by a `"result": "success|error"` field that allow
passing error details to rust:

```console
$ uv venv --no-cache
  × Can't use Python at `/home/konsti/projects/uv/.venv/bin/python3`
  ╰─▶ Unknown operation system `linux`
```

I've used the [maturin sysconfig
collection](855f6d2cb1/sysconfig)
as reference. I'm unsure how to test these changes across the wide
variety of platforms.

Fixes #2326
This commit is contained in:
konsti 2024-03-13 12:51:14 +01:00 committed by GitHub
parent e0ac5b4e84
commit 7964bfbb2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
50 changed files with 1603 additions and 1473 deletions

View file

@ -6,7 +6,6 @@ use clap::Parser;
use fs_err as fs;
use distribution_types::IndexLocations;
use platform_host::Platform;
use rustc_hash::FxHashMap;
use uv_build::{SourceBuild, SourceBuildContext};
use uv_cache::{Cache, CacheArgs};
@ -56,8 +55,7 @@ pub(crate) async fn build(args: BuildArgs) -> Result<PathBuf> {
let cache = Cache::try_from(args.cache_args)?;
let platform = Platform::current()?;
let venv = PythonEnvironment::from_virtualenv(platform, &cache)?;
let venv = PythonEnvironment::from_virtualenv(&cache)?;
let client = RegistryClientBuilder::new(cache.clone()).build();
let index_urls = IndexLocations::default();
let flat_index = FlatIndex::default();

View file

@ -1,7 +1,6 @@
use std::path::PathBuf;
use clap::Parser;
use platform_host::Platform;
use tracing::info;
use uv_cache::{Cache, CacheArgs};
use uv_interpreter::PythonEnvironment;
@ -21,8 +20,7 @@ pub(crate) async fn compile(args: CompileArgs) -> anyhow::Result<()> {
let interpreter = if let Some(python) = args.python {
python
} else {
let platform = Platform::current()?;
let venv = PythonEnvironment::from_virtualenv(platform, &cache)?;
let venv = PythonEnvironment::from_virtualenv(&cache)?;
venv.python_executable().to_path_buf()
};

View file

@ -14,7 +14,6 @@ use distribution_types::{
};
use install_wheel_rs::linker::LinkMode;
use pep508_rs::Requirement;
use platform_host::Platform;
use platform_tags::Tags;
use uv_cache::{Cache, CacheArgs};
use uv_client::{FlatIndex, RegistryClient, RegistryClientBuilder};
@ -55,8 +54,7 @@ pub(crate) async fn install_many(args: InstallManyArgs) -> Result<()> {
info!("Got {} requirements", requirements.len());
let cache = Cache::try_from(args.cache_args)?;
let platform = Platform::current()?;
let venv = PythonEnvironment::from_virtualenv(platform, &cache)?;
let venv = PythonEnvironment::from_virtualenv(&cache)?;
let client = RegistryClientBuilder::new(cache.clone()).build();
let index_locations = IndexLocations::default();
let flat_index = FlatIndex::default();

View file

@ -11,7 +11,6 @@ use petgraph::dot::{Config as DotConfig, Dot};
use distribution_types::{FlatIndexLocation, IndexLocations, IndexUrl, Resolution};
use pep508_rs::Requirement;
use platform_host::Platform;
use uv_cache::{Cache, CacheArgs};
use uv_client::{FlatIndex, FlatIndexClient, RegistryClientBuilder};
use uv_dispatch::BuildDispatch;
@ -54,8 +53,7 @@ pub(crate) struct ResolveCliArgs {
pub(crate) async fn resolve_cli(args: ResolveCliArgs) -> Result<()> {
let cache = Cache::try_from(args.cache_args)?;
let platform = Platform::current()?;
let venv = PythonEnvironment::from_virtualenv(platform, &cache)?;
let venv = PythonEnvironment::from_virtualenv(&cache)?;
let index_locations =
IndexLocations::new(args.index_url, args.extra_index_url, args.find_links, false);
let client = RegistryClientBuilder::new(cache.clone())

View file

@ -13,7 +13,6 @@ use tracing_indicatif::span_ext::IndicatifSpanExt;
use distribution_types::IndexLocations;
use pep440_rs::{Version, VersionSpecifier, VersionSpecifiers};
use pep508_rs::{Requirement, VersionOrUrl};
use platform_host::Platform;
use uv_cache::{Cache, CacheArgs};
use uv_client::{FlatIndex, OwnedArchive, RegistryClient, RegistryClientBuilder};
use uv_dispatch::BuildDispatch;
@ -72,8 +71,7 @@ pub(crate) async fn resolve_many(args: ResolveManyArgs) -> Result<()> {
};
let total = requirements.len();
let platform = Platform::current()?;
let venv = PythonEnvironment::from_virtualenv(platform, &cache)?;
let venv = PythonEnvironment::from_virtualenv(&cache)?;
let in_flight = InFlight::default();
let client = RegistryClientBuilder::new(cache.clone()).build();