Skip to content

Commit

Permalink
Add offline support to uv tool run and uv run
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed May 20, 2024
1 parent 68e4a18 commit 7cfb1a6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
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,
}
25 changes: 19 additions & 6 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::debug;
use distribution_types::{IndexLocations, Resolution};
use install_wheel_rs::linker::LinkMode;
use uv_cache::Cache;
use uv_client::{BaseClientBuilder, RegistryClientBuilder};
use uv_client::{BaseClientBuilder, Connectivity, RegistryClientBuilder};
use uv_configuration::{
Concurrency, ConfigSettings, NoBinary, NoBuild, PreviewMode, SetupPyStrategy,
};
Expand All @@ -35,6 +35,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 @@ -73,7 +74,17 @@ pub(crate) async fn run(
let venv = project::init(&project, cache, printer)?;

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

// If necessary, create an environment for the ephemeral requirements.
Expand Down Expand Up @@ -111,7 +122,7 @@ pub(crate) async fn run(
)?;

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

// Construct the command
Expand Down Expand Up @@ -189,11 +200,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 requirements_client = BaseClientBuilder::default().connectivity(connectivity);

// Read all requirements from the provided sources.
// TODO(zanieb): Consider allowing constraints and extras
Expand All @@ -203,7 +215,7 @@ pub(crate) async fn update_environment(
&[],
&[],
&ExtrasSpecification::None,
&client_builder,
&requirements_client,
preview,
)
.await?;
Expand Down Expand Up @@ -243,9 +255,10 @@ pub(crate) async fn update_environment(
let markers = venv.interpreter().markers();

// Initialize the registry client.
// TODO(zanieb): Support client options e.g. offline, tls, etc.
// TODO(zanieb): Support client options e.g. tls, etc.
let client = RegistryClientBuilder::new(cache.clone())
.markers(markers)
.connectivity(connectivity)
.platform(venv.interpreter().platform())
.build();

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;
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 @@ -64,7 +66,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 @@ -564,6 +565,7 @@ async fn run() -> Result<ExitStatus> {
args.python,
globals.isolated,
globals.preview,
args.connectivity,
&cache,
printer,
)
Expand Down Expand Up @@ -602,12 +604,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

0 comments on commit 7cfb1a6

Please sign in to comment.