Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install managed Python into the Windows Registry (PEP 514) #10634

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,10 @@ jobs:
- name: "Validate global Python install"
run: python ./scripts/check_system_python.py --uv ./uv.exe

# NB: Run this last, we are modifying the registry
- name: "Test PEP 514 registration"
run: python ./scripts/check_registry.py --uv ./uv.exe

system-test-windows-python-313:
timeout-minutes: 10
needs: build-binary-windows
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ which = { workspace = true }
procfs = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { workspace = true }
windows-registry = { workspace = true }
windows-result = { workspace = true }
windows-sys = { workspace = true }

[dev-dependencies]
anyhow = { version = "1.0.89" }
Expand Down
6 changes: 3 additions & 3 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ use crate::interpreter::Error as InterpreterError;
use crate::managed::ManagedPythonInstallations;
#[cfg(windows)]
use crate::microsoft_store::find_microsoft_store_pythons;
#[cfg(windows)]
use crate::py_launcher::{registry_pythons, WindowsPython};
use crate::virtualenv::Error as VirtualEnvError;
use crate::virtualenv::{
conda_environment_from_env, virtualenv_from_env, virtualenv_from_working_dir,
virtualenv_python_executable, CondaEnvironmentKind,
};
#[cfg(windows)]
use crate::windows_registry::{registry_pythons, WindowsPython};
use crate::{Interpreter, PythonVersion};

/// A request to find a Python installation.
Expand Down Expand Up @@ -323,7 +323,7 @@ fn python_executables_from_installed<'a>(
}
})
.inspect(|installation| debug!("Found managed installation `{installation}`"))
.map(|installation| (PythonSource::Managed, installation.executable())))
.map(|installation| (PythonSource::Managed, installation.executable(false))))
})
})
.flatten_ok();
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-python/src/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl ManagedPythonDownload {
.filter(|download| download.key.libc != Libc::Some(target_lexicon::Environment::Musl))
}

pub fn url(&self) -> &str {
pub fn url(&self) -> &'static str {
self.url
}

Expand All @@ -465,7 +465,7 @@ impl ManagedPythonDownload {
self.key.os()
}

pub fn sha256(&self) -> Option<&str> {
pub fn sha256(&self) -> Option<&'static str> {
self.sha256
}

Expand Down
11 changes: 8 additions & 3 deletions crates/uv-python/src/installation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl PythonInstallation {
DownloadResult::Fetched(path) => path,
};

let installed = ManagedPythonInstallation::new(path)?;
let installed = ManagedPythonInstallation::new(path, download);
installed.ensure_externally_managed()?;
installed.ensure_sysconfig_patched()?;
installed.ensure_canonical_executables()?;
Expand All @@ -171,7 +171,7 @@ impl PythonInstallation {

Ok(Self {
source: PythonSource::Managed,
interpreter: Interpreter::query(installed.executable(), cache)?,
interpreter: Interpreter::query(installed.executable(false), cache)?,
})
}

Expand Down Expand Up @@ -282,7 +282,7 @@ impl PythonInstallationKey {
}
}

pub fn new_from_version(
fn new_from_version(
implementation: LenientImplementationName,
version: &PythonVersion,
os: Os,
Expand Down Expand Up @@ -320,6 +320,11 @@ impl PythonInstallationKey {
.expect("Python installation keys must have valid Python versions")
}

/// The version in `x.y.z` format.
pub fn sys_version(&self) -> String {
format!("{}.{}.{}", self.major, self.minor, self.patch)
}

pub fn arch(&self) -> &Arch {
&self.arch
}
Expand Down
9 changes: 7 additions & 2 deletions crates/uv-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ mod microsoft_store;
pub mod platform;
mod pointer_size;
mod prefix;
#[cfg(windows)]
mod py_launcher;
mod python_version;
mod sysconfig;
mod target;
mod version_files;
mod virtualenv;
#[cfg(windows)]
pub mod windows_registry;

#[cfg(windows)]
pub(crate) const COMPANY_KEY: &str = "Astral";
#[cfg(windows)]
pub(crate) const COMPANY_DISPLAY_NAME: &str = "Astral Software Inc.";

#[cfg(not(test))]
pub(crate) fn current_dir() -> Result<std::path::PathBuf, std::io::Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-python/src/macos_dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Error {
};
warn_user!(
"Failed to patch the install name of the dynamic library for {}. This may cause issues when building Python native extensions.{}",
installation.executable().simplified_display(),
installation.executable(false).simplified_display(),
error
);
}
Expand Down
58 changes: 47 additions & 11 deletions crates/uv-python/src/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use uv_state::{StateBucket, StateStore};
use uv_static::EnvVars;
use uv_trampoline_builder::{windows_python_launcher, Launcher};

use crate::downloads::Error as DownloadError;
use crate::downloads::{Error as DownloadError, ManagedPythonDownload};
use crate::implementation::{
Error as ImplementationError, ImplementationName, LenientImplementationName,
};
Expand Down Expand Up @@ -229,7 +229,7 @@ impl ManagedPythonInstallations {
.unwrap_or(true)
})
.filter_map(|path| {
ManagedPythonInstallation::new(path)
ManagedPythonInstallation::from_path(path)
.inspect_err(|err| {
warn!("Ignoring malformed managed Python entry:\n {err}");
})
Expand Down Expand Up @@ -294,10 +294,27 @@ pub struct ManagedPythonInstallation {
path: PathBuf,
/// An install key for the Python version.
key: PythonInstallationKey,
/// The URL with the Python archive.
///
/// Empty when self was constructed from a path.
url: Option<&'static str>,
/// The SHA256 of the Python archive at the URL.
///
/// Empty when self was constructed from a path.
sha256: Option<&'static str>,
}

impl ManagedPythonInstallation {
pub fn new(path: PathBuf) -> Result<Self, Error> {
pub fn new(path: PathBuf, download: &ManagedPythonDownload) -> Self {
Self {
path,
key: download.key().clone(),
url: Some(download.url()),
sha256: download.sha256(),
}
}

pub(crate) fn from_path(path: PathBuf) -> Result<Self, Error> {
let key = PythonInstallationKey::from_str(
path.file_name()
.ok_or(Error::NameError("name is empty".to_string()))?
Expand All @@ -307,15 +324,23 @@ impl ManagedPythonInstallation {

let path = std::path::absolute(&path).map_err(|err| Error::AbsolutePath(path, err))?;

Ok(Self { path, key })
Ok(Self {
path,
key,
url: None,
sha256: None,
})
}

/// The path to this managed installation's Python executable.
///
/// If the installation has multiple execututables i.e., `python`, `python3`, etc., this will
/// return the _canonical_ executable name which the other names link to. On Unix, this is
/// `python{major}.{minor}{variant}` and on Windows, this is `python{exe}`.
pub fn executable(&self) -> PathBuf {
///
/// If windowed is true, `pythonw.exe` is selected over `python.exe` on windows, with no changes
/// on non-windows.
Gankra marked this conversation as resolved.
Show resolved Hide resolved
pub fn executable(&self, windowed: bool) -> PathBuf {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is adding this bool here just for this one use?

    install_path.set_value(
        "WindowedExecutablePath",
        &Value::from(&HSTRING::from(installation.executable(true).as_os_str())),
    )?;

It makes me a little sad to need to pass false everywhere. Unfortunately the logic here is fairly involved... hm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bailed on rewriting the entire logic here, not sure either.

let implementation = match self.implementation() {
ImplementationName::CPython => "python",
ImplementationName::PyPy => "pypy",
Expand All @@ -342,6 +367,9 @@ impl ManagedPythonInstallation {
// On Windows, the executable is just `python.exe` even for alternative variants
let variant = if cfg!(unix) {
self.key.variant.suffix()
} else if cfg!(windows) && windowed {
// Use windowed Python that doesn't open a terminal.
"w"
} else {
""
};
Expand Down Expand Up @@ -412,11 +440,11 @@ impl ManagedPythonInstallation {

pub fn satisfies(&self, request: &PythonRequest) -> bool {
match request {
PythonRequest::File(path) => self.executable() == *path,
PythonRequest::File(path) => self.executable(false) == *path,
PythonRequest::Default | PythonRequest::Any => true,
PythonRequest::Directory(path) => self.path() == *path,
PythonRequest::ExecutableName(name) => self
.executable()
.executable(false)
.file_name()
.is_some_and(|filename| filename.to_string_lossy() == *name),
PythonRequest::Implementation(implementation) => {
Expand All @@ -432,7 +460,7 @@ impl ManagedPythonInstallation {

/// Ensure the environment contains the canonical Python executable names.
pub fn ensure_canonical_executables(&self) -> Result<(), Error> {
let python = self.executable();
let python = self.executable(false);

let canonical_names = &["python"];

Expand Down Expand Up @@ -537,7 +565,7 @@ impl ManagedPythonInstallation {
///
/// If the file already exists at the target path, an error will be returned.
pub fn create_bin_link(&self, target: &Path) -> Result<(), Error> {
let python = self.executable();
let python = self.executable(false);

let bin = target.parent().ok_or(Error::NoExecutableDirectory)?;
fs_err::create_dir_all(bin).map_err(|err| Error::ExecutableDirectory {
Expand Down Expand Up @@ -583,15 +611,15 @@ impl ManagedPythonInstallation {
/// [`ManagedPythonInstallation::create_bin_link`].
pub fn is_bin_link(&self, path: &Path) -> bool {
if cfg!(unix) {
is_same_file(path, self.executable()).unwrap_or_default()
is_same_file(path, self.executable(false)).unwrap_or_default()
} else if cfg!(windows) {
let Some(launcher) = Launcher::try_from_path(path).unwrap_or_default() else {
return false;
};
if !matches!(launcher.kind, uv_trampoline_builder::LauncherKind::Python) {
return false;
}
launcher.python_path == self.executable()
launcher.python_path == self.executable(false)
} else {
unreachable!("Only Windows and Unix are supported")
}
Expand Down Expand Up @@ -625,6 +653,14 @@ impl ManagedPythonInstallation {
// Do not upgrade if the patch versions are the same
self.key.patch != other.key.patch
}

pub fn url(&self) -> Option<&'static str> {
self.url
}

pub fn sha256(&self) -> Option<&'static str> {
self.sha256
}
}

/// Generate a platform portion of a key from the environment.
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-python/src/microsoft_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!
//! Effectively a port of <https://github.com/python/cpython/blob/58ce131037ecb34d506a613f21993cde2056f628/PC/launcher2.c#L1744>

use crate::py_launcher::WindowsPython;
use crate::windows_registry::WindowsPython;
use crate::PythonVersion;
use itertools::Either;
use std::env;
Expand Down
4 changes: 4 additions & 0 deletions crates/uv-python/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl Arch {
variant: None,
}
}

pub fn family(&self) -> target_lexicon::Architecture {
self.family
}
}

impl Display for Libc {
Expand Down
Loading
Loading