Skip to content

Commit

Permalink
Pass keyring-provider to uv sync and install (#1183)
Browse files Browse the repository at this point in the history
When calling `uv pip {install,sync}`, the `keyring-provider` argument
doesn't get passed, even though `uv` supports these arguments for both.

We already have the functionality implemented for `uv pip compile`, so
just extend the support to these.

Tested locally for both commands, seems to work fine.
  • Loading branch information
emarsden-iso committed Jun 30, 2024
1 parent 03d535d commit c60682f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
5 changes: 5 additions & 0 deletions rye/src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use pep508_rs::Requirement;
use crate::cli::add::ReqExtras;
use crate::config::Config;
use crate::installer::{install, resolve_local_requirement};
use crate::lock::KeyringProvider;
use crate::sources::py::PythonVersionRequest;
use crate::utils::CommandOutput;

Expand All @@ -29,6 +30,9 @@ pub struct Args {
/// Force install the package even if it's already there.
#[arg(short, long)]
force: bool,
/// Attempt to use `keyring` for authentication for index URLs.
#[arg(long, value_enum, default_value_t)]
keyring_provider: KeyringProvider,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
Expand Down Expand Up @@ -74,6 +78,7 @@ pub fn execute(mut cmd: Args) -> Result<(), Error> {
&cmd.include_dep,
&extra_requirements,
output,
cmd.keyring_provider,
)?;
Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions rye/src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use url::Url;
use crate::bootstrap::{ensure_self_venv, fetch, FetchOptions};
use crate::config::Config;
use crate::consts::VENV_BIN;
use crate::lock::KeyringProvider;
use crate::platform::get_app_dir;
use crate::pyproject::{normalize_package_name, read_venv_marker, ExpandedSources};
use crate::sources::py::PythonVersionRequest;
Expand Down Expand Up @@ -109,6 +110,7 @@ pub fn install(
include_deps: &[String],
extra_requirements: &[Requirement],
output: CommandOutput,
keyring_provider: KeyringProvider,
) -> Result<(), Error> {
let config = Config::current();
let sources = ExpandedSources::from_sources(&config.sources()?)?;
Expand Down Expand Up @@ -154,6 +156,7 @@ pub fn install(
importlib_workaround: py_ver.major == 3 && py_ver.minor == 7,
extras: extra_requirements.to_vec(),
refresh: force,
keyring_provider,
},
);
if result.is_err() {
Expand Down
11 changes: 11 additions & 0 deletions rye/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ pub enum KeyringProvider {
Subprocess,
}

impl KeyringProvider {
pub fn add_as_pip_args(self, cmd: &mut Command) {
match self {
KeyringProvider::Disabled => {}
KeyringProvider::Subprocess => {
cmd.arg("--keyring-provider").arg("subprocess");
}
}
}
}

/// Controls how locking should work.
#[derive(Debug, Clone, Default, Serialize)]
pub struct LockOptions {
Expand Down
7 changes: 5 additions & 2 deletions rye/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::utils::{
get_venv_python_bin, set_proxy_variables, symlink_dir, update_venv_sync_marker, CommandOutput,
IoPathContext,
};
use crate::uv::UvBuilder;
use crate::uv::{UvBuilder, UvSyncOptions};

/// Controls the sync mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
Expand Down Expand Up @@ -257,14 +257,17 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {
let tempdir = tempdir()?;
let py_path = get_venv_python_bin(&venv);
if Config::current().use_uv() {
let uv_options = UvSyncOptions {
keyring_provider: cmd.keyring_provider,
};
UvBuilder::new()
.with_output(output.quieter())
.with_workdir(&pyproject.workspace_path())
.with_sources(sources)
.ensure_exists()?
.venv(&venv, &py_path, &py_ver, None)?
.with_output(output)
.sync(&target_lockfile)?;
.sync(&target_lockfile, uv_options)?;
} else {
let mut pip_sync_cmd = Command::new(get_pip_sync(&py_ver, output)?);
let root = pyproject.workspace_path();
Expand Down
31 changes: 24 additions & 7 deletions rye/src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct UvInstallOptions {
pub importlib_workaround: bool,
pub extras: Vec<Requirement>,
pub refresh: bool,
pub keyring_provider: KeyringProvider,
}

pub enum UvPackageUpgrade {
Expand Down Expand Up @@ -76,12 +77,7 @@ impl UvCompileOptions {
UvPackageUpgrade::Nothing => {}
}

match self.keyring_provider {
KeyringProvider::Disabled => {}
KeyringProvider::Subprocess => {
cmd.arg("--keyring-provider").arg("subprocess");
}
}
self.keyring_provider.add_as_pip_args(cmd);
}
}

Expand All @@ -99,6 +95,23 @@ impl Default for UvCompileOptions {
}
}

pub struct UvSyncOptions {
pub keyring_provider: KeyringProvider,
}

impl UvSyncOptions {
pub fn add_as_pip_args(self, cmd: &mut Command) {
self.keyring_provider.add_as_pip_args(cmd);
}
}

impl Default for UvSyncOptions {
fn default() -> Self {
Self {
keyring_provider: KeyringProvider::Disabled,
}
}
}
pub struct UvBuilder {
workdir: Option<PathBuf>,
sources: Option<ExpandedSources>,
Expand Down Expand Up @@ -502,6 +515,8 @@ impl UvWithVenv {
cmd.arg("--refresh");
}

options.keyring_provider.add_as_pip_args(&mut cmd);

self.uv.sources.add_as_pip_args(&mut cmd);

cmd.arg("--").arg(requirement.to_string());
Expand Down Expand Up @@ -537,10 +552,12 @@ impl UvWithVenv {
}

/// Syncs the venv
pub fn sync(&self, lockfile: &Path) -> Result<(), Error> {
pub fn sync(&self, lockfile: &Path, options: UvSyncOptions) -> Result<(), Error> {
let mut cmd = self.venv_cmd();
cmd.arg("pip").arg("sync");

options.add_as_pip_args(&mut cmd);

self.uv.sources.add_as_pip_args(&mut cmd);

let status = cmd
Expand Down

0 comments on commit c60682f

Please sign in to comment.