Skip to content

Commit

Permalink
Allow to generate lockfiles with hashes when using uv. (#1070)
Browse files Browse the repository at this point in the history
This PR add the option `--generate-hashes` to both `rye sync` and `rye
lock`. It will generate lock files with hashes if using `uv`.
  • Loading branch information
mvaled authored May 10, 2024
1 parent 45b7bd3 commit 2f59957
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rye/src/cli/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub struct Args {
/// Use this pyproject.toml file
#[arg(long, value_name = "PYPROJECT_TOML")]
pyproject: Option<PathBuf>,
/// Set to true to lock with hashes in the lockfile.
#[arg(long)]
generate_hashes: bool,
}

pub fn execute(cmd: Args) -> Result<(), Error> {
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions rye/src/cli/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions rye/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Regex> =
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions rye/src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct UvCompileOptions {
pub no_deps: bool,
pub no_header: bool,
pub keyring_provider: KeyringProvider,
pub generate_hashes: bool,
}

impl UvCompileOptions {
Expand All @@ -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");
}
Expand Down Expand Up @@ -88,6 +93,7 @@ impl Default for UvCompileOptions {
upgrade: UvPackageUpgrade::Nothing,
no_deps: false,
no_header: false,
generate_hashes: false,
keyring_provider: KeyringProvider::Disabled,
}
}
Expand Down Expand Up @@ -330,13 +336,15 @@ impl Uv {
exclude_newer: Option<String>,
upgrade: UvPackageUpgrade,
keyring_provider: KeyringProvider,
generate_hashes: bool,
) -> Result<(), Error> {
let options = UvCompileOptions {
allow_prerelease,
exclude_newer,
upgrade,
no_deps: false,
no_header: true,
generate_hashes,
keyring_provider,
};

Expand Down Expand Up @@ -581,6 +589,7 @@ impl UvWithVenv {
upgrade: UvPackageUpgrade::Nothing,
no_deps: true,
no_header: true,
generate_hashes: false,
keyring_provider,
};

Expand Down
2 changes: 2 additions & 0 deletions rye/tests/test_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fn test_autosync_remember() {
# features: []
# all-features: true
# with-sources: true
# generate-hashes: false
--index-url https://pypi.org/simple/
Expand Down Expand Up @@ -246,6 +247,7 @@ fn test_autosync_remember() {
# features: []
# all-features: true
# with-sources: true
# generate-hashes: false
--index-url https://pypi.org/simple/
Expand Down

0 comments on commit 2f59957

Please sign in to comment.