From 2f599570d3fc494d58125f3857c9c53de595b86b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=A1zquez=20Acosta?= Date: Fri, 10 May 2024 22:15:36 +0200 Subject: [PATCH] Allow to generate lockfiles with hashes when using uv. (#1070) This PR add the option `--generate-hashes` to both `rye sync` and `rye lock`. It will generate lock files with hashes if using `uv`. --- rye/src/cli/lock.rs | 4 ++++ rye/src/cli/sync.rs | 4 ++++ rye/src/lock.rs | 8 ++++++++ rye/src/uv.rs | 9 +++++++++ rye/tests/test_sync.rs | 2 ++ 5 files changed, 27 insertions(+) diff --git a/rye/src/cli/lock.rs b/rye/src/cli/lock.rs index dbc54cfaa2..ecbeef1316 100644 --- a/rye/src/cli/lock.rs +++ b/rye/src/cli/lock.rs @@ -43,6 +43,9 @@ pub struct Args { /// Use this pyproject.toml file #[arg(long, value_name = "PYPROJECT_TOML")] pyproject: Option, + /// Set to true to lock with hashes in the lockfile. + #[arg(long)] + generate_hashes: bool, } pub fn execute(cmd: Args) -> Result<(), Error> { @@ -58,6 +61,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> { all_features: cmd.all_features, with_sources: cmd.with_sources, reset: cmd.reset, + generate_hashes: cmd.generate_hashes, }, pyproject: cmd.pyproject, keyring_provider: cmd.keyring_provider, diff --git a/rye/src/cli/sync.rs b/rye/src/cli/sync.rs index 4e5fa22871..15b12bec76 100644 --- a/rye/src/cli/sync.rs +++ b/rye/src/cli/sync.rs @@ -52,6 +52,9 @@ pub struct Args { /// Do not reuse (reset) prior lock options. #[arg(long)] reset: bool, + /// Set to true to lock with hashes in the lockfile. + #[arg(long)] + generate_hashes: bool, } pub fn execute(cmd: Args) -> Result<(), Error> { @@ -74,6 +77,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> { all_features: cmd.all_features, with_sources: cmd.with_sources, reset: cmd.reset, + generate_hashes: cmd.generate_hashes, }, keyring_provider: cmd.keyring_provider, pyproject: cmd.pyproject, diff --git a/rye/src/lock.rs b/rye/src/lock.rs index 38e31f7168..d910401274 100644 --- a/rye/src/lock.rs +++ b/rye/src/lock.rs @@ -36,6 +36,7 @@ static REQUIREMENTS_HEADER: &str = r#"# generated by rye # features: {{ lock_options.features|tojson }} # all-features: {{ lock_options.all_features|tojson }} # with-sources: {{ lock_options.with_sources|tojson }} +# generate-hashes: {{ lock_options.generate_hashes|tojson }} "#; static PARAM_RE: Lazy = @@ -89,6 +90,8 @@ pub struct LockOptions { pub with_sources: bool, /// Do not reuse (reset) prior lock options. pub reset: bool, + /// Generate hashes in the lock file. + pub generate_hashes: bool, } impl LockOptions { @@ -430,6 +433,7 @@ fn generate_lockfile( env::var("__RYE_UV_EXCLUDE_NEWER").ok(), upgrade, keyring_provider, + lock_options.generate_hashes, )?; } else { if keyring_provider != KeyringProvider::Disabled { @@ -455,6 +459,10 @@ fn generate_lockfile( if lock_options.pre { cmd.arg("--pre"); } + if lock_options.generate_hashes { + cmd.arg("--generate-hashes"); + cmd.arg("--reuse-hashes"); + } cmd.arg(if output == CommandOutput::Verbose { "--verbose" diff --git a/rye/src/uv.rs b/rye/src/uv.rs index e4f4e6a1b3..303fe9a9d8 100644 --- a/rye/src/uv.rs +++ b/rye/src/uv.rs @@ -39,6 +39,7 @@ struct UvCompileOptions { pub no_deps: bool, pub no_header: bool, pub keyring_provider: KeyringProvider, + pub generate_hashes: bool, } impl UvCompileOptions { @@ -51,6 +52,10 @@ impl UvCompileOptions { cmd.arg("--no-deps"); } + if self.generate_hashes { + cmd.arg("--generate-hashes"); + } + if self.allow_prerelease { cmd.arg("--prerelease=allow"); } @@ -88,6 +93,7 @@ impl Default for UvCompileOptions { upgrade: UvPackageUpgrade::Nothing, no_deps: false, no_header: false, + generate_hashes: false, keyring_provider: KeyringProvider::Disabled, } } @@ -330,6 +336,7 @@ impl Uv { exclude_newer: Option, upgrade: UvPackageUpgrade, keyring_provider: KeyringProvider, + generate_hashes: bool, ) -> Result<(), Error> { let options = UvCompileOptions { allow_prerelease, @@ -337,6 +344,7 @@ impl Uv { upgrade, no_deps: false, no_header: true, + generate_hashes, keyring_provider, }; @@ -581,6 +589,7 @@ impl UvWithVenv { upgrade: UvPackageUpgrade::Nothing, no_deps: true, no_header: true, + generate_hashes: false, keyring_provider, }; diff --git a/rye/tests/test_sync.rs b/rye/tests/test_sync.rs index 92cadf625a..61bf9e768b 100644 --- a/rye/tests/test_sync.rs +++ b/rye/tests/test_sync.rs @@ -189,6 +189,7 @@ fn test_autosync_remember() { # features: [] # all-features: true # with-sources: true + # generate-hashes: false --index-url https://pypi.org/simple/ @@ -246,6 +247,7 @@ fn test_autosync_remember() { # features: [] # all-features: true # with-sources: true + # generate-hashes: false --index-url https://pypi.org/simple/