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

@ -21,7 +21,7 @@ required-features = ["cli"]
workspace = true
[dependencies]
platform-host = { path = "../platform-host" }
platform-tags = { path = "../platform-tags" }
pypi-types = { path = "../pypi-types" }
uv-cache = { path = "../uv-cache" }
uv-fs = { path = "../uv-fs" }

View file

@ -1,9 +1,9 @@
use std::io;
use std::path::Path;
use platform_tags::PlatformError;
use thiserror::Error;
use platform_host::PlatformError;
use uv_interpreter::{Interpreter, PythonEnvironment};
pub use crate::bare::create_bare_venv;

View file

@ -11,7 +11,6 @@ use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, EnvFilter};
use platform_host::Platform;
use uv_cache::Cache;
use uv_interpreter::{find_default_python, find_requested_python};
use uv_virtualenv::{create_bare_venv, Prompt};
@ -30,18 +29,17 @@ struct Cli {
fn run() -> Result<(), uv_virtualenv::Error> {
let cli = Cli::parse();
let location = cli.path.unwrap_or(PathBuf::from(".venv"));
let platform = Platform::current()?;
let cache = if let Some(project_dirs) = ProjectDirs::from("", "", "uv-virtualenv") {
Cache::from_path(project_dirs.cache_dir())?
} else {
Cache::from_path(".cache")?
};
let interpreter = if let Some(python_request) = &cli.python {
find_requested_python(python_request, &platform, &cache)?.ok_or(
find_requested_python(python_request, &cache)?.ok_or(
uv_interpreter::Error::NoSuchPython(python_request.to_string()),
)?
} else {
find_default_python(&platform, &cache)?
find_default_python(&cache)?
};
create_bare_venv(
&location,