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

Add offline support to uv tool run and uv run #3676

Merged
merged 1 commit into from
May 21, 2024
Merged
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
3 changes: 2 additions & 1 deletion crates/uv-client/src/registry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,10 @@ impl MediaType {
}
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum Connectivity {
/// Allow access to the network.
#[default]
Online,

/// Do not allow access to the network.
Expand Down
14 changes: 14 additions & 0 deletions crates/uv/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,13 @@ pub(crate) struct RunArgs {
#[arg(long)]
pub(crate) with: Vec<String>,

/// Run offline, i.e., without accessing the network.
#[arg(global = true, long, overrides_with("no_offline"))]
pub(crate) offline: bool,

#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,

/// The Python interpreter to use to build the run environment.
///
/// By default, `uv` uses the virtual environment in the current working directory or any parent
Expand Down Expand Up @@ -1954,4 +1961,11 @@ pub(crate) struct ToolRunArgs {
group = "discovery"
)]
pub(crate) python: Option<String>,

/// Run offline, i.e., without accessing the network.
#[arg(global = true, long, overrides_with("no_offline"))]
pub(crate) offline: bool,

#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
}
6 changes: 4 additions & 2 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use platform_tags::Tags;
use pypi_types::Yanked;
use tracing::debug;
use uv_cache::Cache;
use uv_client::{BaseClientBuilder, RegistryClient, RegistryClientBuilder};
use uv_client::{BaseClientBuilder, Connectivity, RegistryClient, RegistryClientBuilder};
use uv_configuration::{
Concurrency, ConfigSettings, Constraints, NoBinary, NoBuild, Overrides, PreviewMode, Reinstall,
SetupPyStrategy,
Expand Down Expand Up @@ -468,11 +468,12 @@ pub(crate) async fn update_environment(
venv: PythonEnvironment,
requirements: &[RequirementsSource],
preview: PreviewMode,
connectivity: Connectivity,
cache: &Cache,
printer: Printer,
) -> Result<PythonEnvironment> {
// TODO(zanieb): Support client configuration
let client_builder = BaseClientBuilder::default();
let client_builder = BaseClientBuilder::default().connectivity(connectivity);

// Read all requirements from the provided sources.
// TODO(zanieb): Consider allowing constraints and extras
Expand Down Expand Up @@ -524,6 +525,7 @@ pub(crate) async fn update_environment(
// Initialize the registry client.
// TODO(zanieb): Support client options e.g. offline, tls, etc.
let client = RegistryClientBuilder::new(cache.clone())
.connectivity(connectivity)
.markers(markers)
.platform(venv.interpreter().platform())
.build();
Expand Down
18 changes: 15 additions & 3 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::process::Command;
use tracing::debug;

use uv_cache::Cache;
use uv_client::Connectivity;
use uv_configuration::PreviewMode;
use uv_interpreter::{PythonEnvironment, SystemPython};
use uv_requirements::{ProjectWorkspace, RequirementsSource};
Expand All @@ -25,6 +26,7 @@ pub(crate) async fn run(
python: Option<String>,
isolated: bool,
preview: PreviewMode,
connectivity: Connectivity,
cache: &Cache,
printer: Printer,
) -> Result<ExitStatus> {
Expand Down Expand Up @@ -60,8 +62,15 @@ pub(crate) async fn run(

// Install the project requirements.
Some(
project::update_environment(venv, &project.requirements(), preview, cache, printer)
.await?,
project::update_environment(
venv,
&project.requirements(),
preview,
connectivity,
cache,
printer,
)
.await?,
)
};

Expand Down Expand Up @@ -101,7 +110,10 @@ pub(crate) async fn run(
)?;

// Install the ephemeral requirements.
Some(project::update_environment(venv, &requirements, preview, cache, printer).await?)
Some(
project::update_environment(venv, &requirements, preview, connectivity, cache, printer)
.await?,
)
};

// Construct the command
Expand Down
4 changes: 3 additions & 1 deletion crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::process::Command;
use tracing::debug;

use uv_cache::Cache;
use uv_client::Connectivity;
use uv_configuration::PreviewMode;
use uv_interpreter::{PythonEnvironment, SystemPython};
use uv_requirements::RequirementsSource;
Expand All @@ -25,6 +26,7 @@ pub(crate) async fn run(
python: Option<String>,
_isolated: bool,
preview: PreviewMode,
connectivity: Connectivity,
cache: &Cache,
printer: Printer,
) -> Result<ExitStatus> {
Expand Down Expand Up @@ -65,7 +67,7 @@ pub(crate) async fn run(

// Install the ephemeral requirements.
let ephemeral_env =
Some(update_environment(venv, &requirements, preview, cache, printer).await?);
Some(update_environment(venv, &requirements, preview, connectivity, cache, printer).await?);

// TODO(zanieb): Determine the command via the package entry points
let command = target;
Expand Down
8 changes: 8 additions & 0 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use owo_colors::OwoColorize;
use tracing::instrument;

use uv_cache::Cache;
use uv_client::Connectivity;
use uv_requirements::RequirementsSource;
use uv_workspace::Combine;

Expand Down Expand Up @@ -565,6 +566,7 @@ async fn run() -> Result<ExitStatus> {
args.python,
globals.isolated,
globals.preview,
args.connectivity,
&cache,
printer,
)
Expand Down Expand Up @@ -603,12 +605,18 @@ async fn run() -> Result<ExitStatus> {
Commands::Tool(ToolNamespace {
command: ToolCommand::Run(args),
}) => {
let connectivity = if args.offline {
Connectivity::Offline
} else {
Connectivity::Online
};
commands::run_tool(
args.target,
args.args,
args.python,
globals.isolated,
globals.preview,
connectivity,
&cache,
printer,
)
Expand Down
16 changes: 16 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ pub(crate) struct RunSettings {
pub(crate) args: Vec<OsString>,
pub(crate) with: Vec<String>,
pub(crate) python: Option<String>,

// Shared settings.
// TODO(zanieb): should be moved to a global setting
pub(crate) connectivity: Connectivity,
}

impl RunSettings {
Expand All @@ -105,6 +109,8 @@ impl RunSettings {
args,
with,
python,
offline,
no_offline,
} = args;

Self {
Expand All @@ -113,6 +119,16 @@ impl RunSettings {
args,
with,
python,
// Shared settings
connectivity: flag(offline, no_offline)
.map(|offline| {
if offline {
Connectivity::Offline
} else {
Connectivity::Online
}
})
.unwrap_or_default(),
}
}
}
Expand Down
Loading