From 7828a2841813bab96d1ab6f7cbc5729e2d9c21d2 Mon Sep 17 00:00:00 2001 From: asmith26 Date: Mon, 24 Jun 2024 13:32:31 +0100 Subject: [PATCH] Add ability to specify option to generate hashes within pyproject.toml (#1129) Please note: I haven't written any Rust before, but just wanted this feature hence thought I'd try submitting a PR for this. I've essentially tried to mimic the code regarding the `--with-sources` feature. I'm not sure how to compile and test locally (so welcome any info on this - and if helpful I would be happy to test this locally and get back to confirm if this works). All thoughts/help/feedback etc. very welcome, and no worries if this feature is not useful. - Adds ability to specify option to generate hashes via the pyproject.toml (similar to https://rye.astral.sh/guide/pyproject/#toolryelock-with-sources). - Updates doc regarding the `--generate-hashes` option too. - Added [`+++ 0.35.0`](https://github.com/astral-sh/rye/pull/1129/files#diff-8ea7c706930a01df775defe815afe2478a41699e0ac0b99d7b069c0fe0d9f45fR65) to the doc in case it gets merged for this version, but feel free to amend as appropriate. Thanks! --- docs/guide/commands/lock.md | 2 ++ docs/guide/commands/sync.md | 2 ++ docs/guide/pyproject.md | 13 +++++++++++++ rye/src/pyproject.rs | 23 ++++++++++++++++++++++- rye/src/sync.rs | 5 +++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/guide/commands/lock.md b/docs/guide/commands/lock.md index 7fe3506541..079f69363d 100644 --- a/docs/guide/commands/lock.md +++ b/docs/guide/commands/lock.md @@ -30,6 +30,8 @@ Done! * `--all-features`: Enables all features +* `--generate-hashes`: Set to true to lock with hashes in the lockfile + * `--with-sources`: Set to true to lock with sources in the lockfile * `--pyproject `: Use this pyproject.toml file diff --git a/docs/guide/commands/sync.md b/docs/guide/commands/sync.md index 2a1ad45921..df85869325 100644 --- a/docs/guide/commands/sync.md +++ b/docs/guide/commands/sync.md @@ -53,6 +53,8 @@ To exit the sub shell run `exit`. * `--all-features`: Enables all features +* `--generate-hashes`: Set to true to lock with hashes in the lockfile + * `--with-sources`: Set to true to lock with sources in the lockfile * `--pyproject `: Use this pyproject.toml file diff --git a/docs/guide/pyproject.md b/docs/guide/pyproject.md index 34db616b37..44317384a4 100644 --- a/docs/guide/pyproject.md +++ b/docs/guide/pyproject.md @@ -60,6 +60,19 @@ pulled in as indirect dependencies. These are added here automatically with `ry excluded-dependencies = ["cffi"] ``` +## `tool.rye.generate-hashes` + ++++ 0.35.0 + +When this flag is enabled all `lock` and `sync` operations in the project or workspace +operate as if `--generate-hashes` is passed. This means that all dependencies in all +lock files will include a hash. + +```toml +[tool.rye] +generate-hashes = true +``` + ## `tool.rye.lock-with-sources` +++ 0.18.0 diff --git a/rye/src/pyproject.rs b/rye/src/pyproject.rs index bf32b073f4..da8b847487 100644 --- a/rye/src/pyproject.rs +++ b/rye/src/pyproject.rs @@ -535,6 +535,11 @@ impl Workspace { is_rye_managed(&self.doc) } + /// Should requirements.txt based locking include generating hashes? + pub fn generate_hashes(&self) -> bool { + generate_hashes(&self.doc) + } + /// Should requirements.txt based locking include a find-links reference? pub fn lock_with_sources(&self) -> bool { lock_with_sources(&self.doc) @@ -1006,7 +1011,15 @@ impl PyProject { .unwrap_or(false) } - /// Should requirements.txt based locking include a find-links reference? + /// Should requirements.txt-based locking include generating hashes? + pub fn generate_hashes(&self) -> bool { + match self.workspace { + Some(ref workspace) => workspace.generate_hashes(), + None => generate_hashes(&self.doc), + } + } + + /// Should requirements.txt-based locking include a find-links reference? pub fn lock_with_sources(&self) -> bool { match self.workspace { Some(ref workspace) => workspace.lock_with_sources(), @@ -1280,6 +1293,14 @@ fn is_rye_managed(doc: &DocumentMut) -> bool { .unwrap_or(false) } +fn generate_hashes(doc: &DocumentMut) -> bool { + doc.get("tool") + .and_then(|x| x.get("rye")) + .and_then(|x| x.get("generate-hashes")) + .and_then(|x| x.as_bool()) + .unwrap_or(false) +} + fn lock_with_sources(doc: &DocumentMut) -> bool { doc.get("tool") .and_then(|x| x.get("rye")) diff --git a/rye/src/sync.rs b/rye/src/sync.rs index 5ab816a616..589680e775 100644 --- a/rye/src/sync.rs +++ b/rye/src/sync.rs @@ -105,6 +105,11 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> { bail!("cannot sync or generate lockfile: package needs 'pyproject.toml'"); } + // Turn on generate_hashes if the project demands it. + if pyproject.generate_hashes() { + cmd.lock_options.generate_hashes = true; + } + // Turn on locking with sources if the project demands it. if pyproject.lock_with_sources() { cmd.lock_options.with_sources = true;