Skip to content

Commit

Permalink
Pass install mirrors to other fetching commands
Browse files Browse the repository at this point in the history
  • Loading branch information
owenbrooks committed Nov 3, 2024
1 parent f831b50 commit 61c55fe
Show file tree
Hide file tree
Showing 21 changed files with 365 additions and 76 deletions.
4 changes: 2 additions & 2 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3937,15 +3937,15 @@ pub struct PythonInstallArgs {
///
/// Distributions can be read from a local directory by using the `file://` URL scheme.
#[arg(long, env = EnvVars::UV_PYTHON_INSTALL_MIRROR)]
pub python_install_mirror: Option<String>,
pub mirror: Option<String>,

/// Set the URL to use as the source for downloading PyPy installations.
///
/// The provided URL will replace `https://downloads.python.org/pypy` in, e.g., `https://downloads.python.org/pypy/pypy3.8-v7.3.7-osx64.tar.bz2`.
///
/// Distributions can be read from a local directory by using the `file://` URL scheme.
#[arg(long, env = EnvVars::UV_PYPY_INSTALL_MIRROR)]
pub pypy_install_mirror: Option<String>,
pub pypy_mirror: Option<String>,

/// Reinstall the requested Python version, if it's already installed.
///
Expand Down
18 changes: 14 additions & 4 deletions crates/uv-python/src/installation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use tracing::{debug, info};
use uv_cache::Cache;
use uv_client::BaseClientBuilder;
use uv_pep440::{Prerelease, Version};
use uv_static::EnvVars;

use crate::discovery::{
find_best_python_installation, find_python_installation, EnvironmentPreference, PythonRequest,
Expand Down Expand Up @@ -87,6 +86,8 @@ impl PythonInstallation {
client_builder: &BaseClientBuilder<'a>,
cache: &Cache,
reporter: Option<&dyn Reporter>,
python_install_mirror: Option<String>,
pypy_install_mirror: Option<String>,
) -> Result<Self, Error> {
let request = request.unwrap_or_else(|| &PythonRequest::Default);

Expand All @@ -101,7 +102,16 @@ impl PythonInstallation {
{
if let Some(request) = PythonDownloadRequest::from_request(request) {
debug!("Requested Python not found, checking for available download...");
match Self::fetch(request.fill()?, client_builder, cache, reporter).await {
match Self::fetch(
request.fill()?,
client_builder,
cache,
reporter,
python_install_mirror,
pypy_install_mirror,
)
.await
{
Ok(installation) => Ok(installation),
Err(Error::Download(downloads::Error::NoDownloadFound(_))) => {
Err(Error::MissingPython(err))
Expand All @@ -122,6 +132,8 @@ impl PythonInstallation {
client_builder: &BaseClientBuilder<'a>,
cache: &Cache,
reporter: Option<&dyn Reporter>,
python_install_mirror: Option<String>,
pypy_install_mirror: Option<String>,
) -> Result<Self, Error> {
let installations = ManagedPythonInstallations::from_settings()?.init()?;
let installations_dir = installations.root();
Expand All @@ -130,8 +142,6 @@ impl PythonInstallation {

let download = ManagedPythonDownload::from_request(&request)?;
let client = client_builder.build();
let python_install_mirror = std::env::var(EnvVars::UV_PYTHON_INSTALL_MIRROR).ok();
let pypy_install_mirror = std::env::var(EnvVars::UV_PYPY_INSTALL_MIRROR).ok();

info!("Fetching requested Python...");
let result = download
Expand Down
103 changes: 68 additions & 35 deletions crates/uv-settings/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use uv_pep508::Requirement;
use uv_pypi_types::{SupportedEnvironments, VerbatimParsedUrl};
use uv_python::{PythonDownloads, PythonPreference, PythonVersion};
use uv_resolver::{AnnotationStyle, ExcludeNewer, PrereleaseMode, ResolutionMode};
use uv_static::EnvVars;

/// A `pyproject.toml` with an (optional) `[tool.uv]` section.
#[allow(dead_code)]
Expand Down Expand Up @@ -41,6 +42,9 @@ pub struct Options {
#[serde(flatten)]
pub top_level: ResolverInstallerOptions,

#[serde(flatten)]
pub install_mirrors: InstallMirrorOptions,

#[serde(flatten)]
pub publish: PublishOptions,

Expand Down Expand Up @@ -224,37 +228,6 @@ pub struct GlobalOptions {
"#
)]
pub concurrent_installs: Option<NonZeroUsize>,
/// Mirror URL for downloading managed Python installations.
///
/// By default, managed Python installations are downloaded from [`python-build-standalone`](https://github.com/indygreg/python-build-standalone).
/// This variable can be set to a mirror URL to use a different source for Python installations.
/// The provided URL will replace `https://github.com/indygreg/python-build-standalone/releases/download` in, e.g., `https://github.com/indygreg/python-build-standalone/releases/download/20240713/cpython-3.12.4%2B20240713-aarch64-apple-darwin-install_only.tar.gz`.
///
/// Distributions can be read from a local directory by using the `file://` URL scheme.
#[option(
default = "None",
value_type = "str",
example = r#"
python-install-mirror = "https://github.com/indygreg/python-build-standalone/releases/download"
"#
)]
pub python_install_mirror: Option<String>,
/// Mirror URL to use for downloading managed PyPy installations.
///
/// By default, managed PyPy installations are downloaded from [downloads.python.org](https://downloads.python.org/).
/// This variable can be set to a mirror URL to use a different source for PyPy installations.
/// The provided URL will replace `https://downloads.python.org/pypy` in, e.g., `https://downloads.python.org/pypy/pypy3.8-v7.3.7-osx64.tar.bz2`.
///
/// Distributions can be read from a
/// local directory by using the `file://` URL scheme.
#[option(
default = "None",
value_type = "str",
example = r#"
pypy-install-mirror = "https://downloads.python.org/pypy"
"#
)]
pub pypy_install_mirror: Option<String>,
}

/// Settings relevant to all installer operations.
Expand Down Expand Up @@ -684,6 +657,61 @@ pub struct ResolverInstallerOptions {
pub no_binary_package: Option<Vec<PackageName>>,
}

/// Shared settings, relevant to all operations that might create managed python installations.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, CombineOptions, OptionsMetadata)]
#[serde(rename_all = "kebab-case")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct InstallMirrorOptions {
/// Mirror URL for downloading managed Python installations.
///
/// By default, managed Python installations are downloaded from [`python-build-standalone`](https://github.com/indygreg/python-build-standalone).
/// This variable can be set to a mirror URL to use a different source for Python installations.
/// The provided URL will replace `https://github.com/indygreg/python-build-standalone/releases/download` in, e.g., `https://github.com/indygreg/python-build-standalone/releases/download/20240713/cpython-3.12.4%2B20240713-aarch64-apple-darwin-install_only.tar.gz`.
///
/// Distributions can be read from a local directory by using the `file://` URL scheme.
#[option(
default = "None",
value_type = "str",
example = r#"
python-install-mirror = "https://github.com/indygreg/python-build-standalone/releases/download"
"#
)]
pub python_install_mirror: Option<String>,
/// Mirror URL to use for downloading managed PyPy installations.
///
/// By default, managed PyPy installations are downloaded from [downloads.python.org](https://downloads.python.org/).
/// This variable can be set to a mirror URL to use a different source for PyPy installations.
/// The provided URL will replace `https://downloads.python.org/pypy` in, e.g., `https://downloads.python.org/pypy/pypy3.8-v7.3.7-osx64.tar.bz2`.
///
/// Distributions can be read from a
/// local directory by using the `file://` URL scheme.
#[option(
default = "None",
value_type = "str",
example = r#"
pypy-install-mirror = "https://downloads.python.org/pypy"
"#
)]
pub pypy_install_mirror: Option<String>,
}

impl Default for InstallMirrorOptions {
fn default() -> Self {
InstallMirrorOptions::resolve(None, None)
}
}

impl InstallMirrorOptions {
pub fn resolve(python_mirror: Option<String>, pypy_mirror: Option<String>) -> Self {
let python_mirror_env = std::env::var(EnvVars::UV_PYTHON_INSTALL_MIRROR).ok();
let pypy_mirror_env = std::env::var(EnvVars::UV_PYPY_INSTALL_MIRROR).ok();
InstallMirrorOptions {
python_install_mirror: python_mirror_env.or(python_mirror),
pypy_install_mirror: pypy_mirror_env.or(pypy_mirror),
}
}
}

/// Settings that are specific to the `uv pip` command-line interface.
///
/// These values will be ignored when running commands outside the `uv pip` namespace (e.g.,
Expand Down Expand Up @@ -1540,8 +1568,6 @@ pub struct OptionsWire {
preview: Option<bool>,
python_preference: Option<PythonPreference>,
python_downloads: Option<PythonDownloads>,
python_install_mirror: Option<String>,
pypy_install_mirror: Option<String>,
concurrent_downloads: Option<NonZeroUsize>,
concurrent_builds: Option<NonZeroUsize>,
concurrent_installs: Option<NonZeroUsize>,
Expand Down Expand Up @@ -1575,6 +1601,11 @@ pub struct OptionsWire {
no_binary: Option<bool>,
no_binary_package: Option<Vec<PackageName>>,

// #[serde(flatten)]
// install_mirror: InstallMirrorOptions,
python_install_mirror: Option<String>,
pypy_install_mirror: Option<String>,

// #[serde(flatten)]
// publish: PublishOptions
publish_url: Option<Url>,
Expand Down Expand Up @@ -1670,8 +1701,6 @@ impl From<OptionsWire> for Options {
preview,
python_preference,
python_downloads,
python_install_mirror,
pypy_install_mirror,
concurrent_downloads,
concurrent_builds,
concurrent_installs,
Expand Down Expand Up @@ -1713,6 +1742,10 @@ impl From<OptionsWire> for Options {
override_dependencies,
constraint_dependencies,
environments,
install_mirrors: InstallMirrorOptions::resolve(
python_install_mirror,
pypy_install_mirror,
),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/uv/src/commands/build_frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use uv_python::{
};
use uv_requirements::RequirementsSource;
use uv_resolver::{ExcludeNewer, FlatIndex, RequiresPython};
use uv_settings::InstallMirrorOptions;
use uv_types::{BuildContext, BuildIsolation, HashStrategy};
use uv_workspace::{DiscoveryOptions, Workspace, WorkspaceError};

Expand All @@ -51,6 +52,7 @@ pub(crate) async fn build_frontend(
build_constraints: Vec<RequirementsSource>,
hash_checking: Option<HashCheckingMode>,
python: Option<String>,
install_mirrors: InstallMirrorOptions,
settings: ResolverSettings,
no_config: bool,
python_preference: PythonPreference,
Expand All @@ -73,6 +75,7 @@ pub(crate) async fn build_frontend(
&build_constraints,
hash_checking,
python.as_deref(),
install_mirrors,
settings.as_ref(),
no_config,
python_preference,
Expand Down Expand Up @@ -113,6 +116,7 @@ async fn build_impl(
build_constraints: &[RequirementsSource],
hash_checking: Option<HashCheckingMode>,
python_request: Option<&str>,
install_mirrors: InstallMirrorOptions,
settings: ResolverSettingsRef<'_>,
no_config: bool,
python_preference: PythonPreference,
Expand Down Expand Up @@ -247,6 +251,7 @@ async fn build_impl(
source.clone(),
output_dir,
python_request,
install_mirrors.clone(),
no_config,
workspace.as_ref(),
python_preference,
Expand Down Expand Up @@ -342,6 +347,7 @@ async fn build_package(
source: AnnotatedSource<'_>,
output_dir: Option<&Path>,
python_request: Option<&str>,
install_mirrors: InstallMirrorOptions,
no_config: bool,
workspace: Result<&Workspace, &WorkspaceError>,
python_preference: PythonPreference,
Expand Down Expand Up @@ -417,6 +423,8 @@ async fn build_package(
client_builder,
cache,
Some(&PythonDownloadReporter::single(printer)),
install_mirrors.python_install_mirror,
install_mirrors.pypy_install_mirror,
)
.await?
.into_interpreter();
Expand Down
6 changes: 6 additions & 0 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use uv_python::{
use uv_requirements::{NamedRequirementsResolver, RequirementsSource, RequirementsSpecification};
use uv_resolver::{FlatIndex, InstallTarget};
use uv_scripts::Pep723Script;
use uv_settings::InstallMirrorOptions;
use uv_types::{BuildIsolation, HashStrategy};
use uv_warnings::warn_user_once;
use uv_workspace::pyproject::{DependencyType, Source, SourceError};
Expand Down Expand Up @@ -67,6 +68,7 @@ pub(crate) async fn add(
extras: Vec<ExtraName>,
package: Option<PackageName>,
python: Option<String>,
install_mirrors: InstallMirrorOptions,
settings: ResolverInstallerSettings,
script: Option<PathBuf>,
python_preference: PythonPreference,
Expand Down Expand Up @@ -133,6 +135,7 @@ pub(crate) async fn add(
} else {
let requires_python = script_python_requirement(
python.as_deref(),
install_mirrors.clone(),
project_dir,
false,
python_preference,
Expand Down Expand Up @@ -176,6 +179,8 @@ pub(crate) async fn add(
&client_builder,
cache,
Some(&reporter),
install_mirrors.python_install_mirror,
install_mirrors.pypy_install_mirror,
)
.await?
.into_interpreter();
Expand Down Expand Up @@ -213,6 +218,7 @@ pub(crate) async fn add(
let venv = project::get_or_init_environment(
project.workspace(),
python.as_deref().map(PythonRequest::parse),
install_mirrors.clone(),
python_preference,
python_downloads,
connectivity,
Expand Down
3 changes: 3 additions & 0 deletions crates/uv/src/commands/project/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::{Context, Result};
use itertools::Itertools;
use owo_colors::OwoColorize;
use std::path::{Path, PathBuf};
use uv_settings::InstallMirrorOptions;

use uv_cache::Cache;
use uv_client::Connectivity;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub(crate) async fn export(
frozen: bool,
include_header: bool,
python: Option<String>,
install_mirrors: InstallMirrorOptions,
settings: ResolverSettings,
python_preference: PythonPreference,
python_downloads: PythonDownloads,
Expand Down Expand Up @@ -105,6 +107,7 @@ pub(crate) async fn export(
native_tls,
cache,
printer,
install_mirrors,
)
.await?
.into_interpreter();
Expand Down
Loading

0 comments on commit 61c55fe

Please sign in to comment.