Skip to content

Commit

Permalink
Add basic uv sync and uv lock commands (#3436)
Browse files Browse the repository at this point in the history
## Summary

These aren't intended for production use; instead, I'm just trying to
frame out the overall data flows and code-sharing for these commands. We
now have `uv sync` (sync the environment to match the lockfile, without
refreshing or resolving) and `uv lock` (generate the lockfile). Both
_require_ a virtual environment to exist (something we should change).
`uv sync`, `uv run`, and `uv lock` all share code for the underlying
subroutines (resolution and installation), so the commands themselves
are relatively small (~100 lines) and mostly consist of reading
arguments and such.

`uv lock` and `uv sync` don't actually really work yet, because we have
no way to include the project itself in the lockfile (that's a TODO in
the lockfile implementation).

Closes #3432.
  • Loading branch information
charliermarsh authored May 8, 2024
1 parent 0228b15 commit 76a3ceb
Show file tree
Hide file tree
Showing 13 changed files with 1,118 additions and 760 deletions.
3 changes: 2 additions & 1 deletion crates/uv-types/src/builds.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use uv_interpreter::PythonEnvironment;

/// Whether to enforce build isolation when building source distributions.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Default, Copy, Clone)]
pub enum BuildIsolation<'a> {
#[default]
Isolated,
Shared(&'a PythonEnvironment),
}
Expand Down
3 changes: 2 additions & 1 deletion crates/uv-types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use pep508_rs::MarkerEnvironment;
use pypi_types::{HashDigest, HashError};
use uv_normalize::PackageName;

#[derive(Debug, Clone)]
#[derive(Debug, Default, Clone)]
pub enum HashStrategy {
/// No hash policy is specified.
#[default]
None,
/// Hashes should be generated (specifically, a SHA-256 hash), but not validated.
Generate,
Expand Down
55 changes: 55 additions & 0 deletions crates/uv/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ pub(crate) enum Commands {
/// Clear the cache, removing all entries or those linked to specific packages.
#[command(hide = true)]
Clean(CleanArgs),
/// Run a command in the project environment.
#[clap(hide = true)]
Run(RunArgs),
/// Sync the project's dependencies with the environment.
#[clap(hide = true)]
Sync(SyncArgs),
/// Resolve the project requirements into a lockfile.
#[clap(hide = true)]
Lock(LockArgs),
/// Display uv's version
Version {
#[arg(long, value_enum, default_value = "text")]
Expand Down Expand Up @@ -1852,6 +1859,54 @@ pub(crate) struct RunArgs {
pub(crate) python: Option<String>,
}

#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct SyncArgs {
/// 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
/// directory, falling back to searching for a Python executable in `PATH`. The `--python`
/// option allows you to specify a different interpreter.
///
/// Supported formats:
/// - `3.10` looks for an installed Python 3.10 using `py --list-paths` on Windows, or
/// `python3.10` on Linux and macOS.
/// - `python3.10` or `python.exe` looks for a binary with the given name in `PATH`.
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
#[arg(
long,
short,
env = "UV_PYTHON",
verbatim_doc_comment,
group = "discovery"
)]
pub(crate) python: Option<String>,
}

#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct LockArgs {
/// 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
/// directory, falling back to searching for a Python executable in `PATH`. The `--python`
/// option allows you to specify a different interpreter.
///
/// Supported formats:
/// - `3.10` looks for an installed Python 3.10 using `py --list-paths` on Windows, or
/// `python3.10` on Linux and macOS.
/// - `python3.10` or `python.exe` looks for a binary with the given name in `PATH`.
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
#[arg(
long,
short,
env = "UV_PYTHON",
verbatim_doc_comment,
group = "discovery"
)]
pub(crate) python: Option<String>,
}

#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
struct AddArgs {
Expand Down
6 changes: 4 additions & 2 deletions crates/uv/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub(crate) use pip_list::pip_list;
pub(crate) use pip_show::pip_show;
pub(crate) use pip_sync::pip_sync;
pub(crate) use pip_uninstall::pip_uninstall;
pub(crate) use run::run;
#[cfg(feature = "self-update")]
pub(crate) use self_update::self_update;
use uv_cache::Cache;
Expand All @@ -26,6 +25,9 @@ use uv_interpreter::PythonEnvironment;
use uv_normalize::PackageName;
pub(crate) use venv::venv;
pub(crate) use version::version;
pub(crate) use workspace::lock::lock;
pub(crate) use workspace::run::run;
pub(crate) use workspace::sync::sync;

use crate::printer::Printer;

Expand All @@ -41,11 +43,11 @@ mod pip_show;
mod pip_sync;
mod pip_uninstall;
mod reporters;
mod run;
#[cfg(feature = "self-update")]
mod self_update;
mod venv;
mod version;
mod workspace;

#[derive(Copy, Clone)]
pub(crate) enum ExitStatus {
Expand Down
Loading

0 comments on commit 76a3ceb

Please sign in to comment.