Skip to content

Commit

Permalink
Pass universal flag down to uv (#1186)
Browse files Browse the repository at this point in the history
Partially addresses #1056

Allows parsing the new `--universal` flag down to uv when calling `lock`
or `sync`, also adds it as an option to the pyrpoject.toml

Tested locally for `lock` and `sync` with command line args and in the
pyproject, seems to work.
  • Loading branch information
lbowenwest authored Jul 2, 2024
1 parent b88f1ec commit 11fc495
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docs/guide/pyproject.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ pulled in as indirect dependencies. These are added here automatically with `ry
excluded-dependencies = ["cffi"]
```

## `tool.rye.universal`

+++ 0.36.0

When this flag is enabled all `lock` and `sync` operations in the project or workspace
operate as if `--universal` is passed. This means that the dependency resolver will
attempt to generate a resolution that's valid on all platforms, operating systems, and
architectures, rather than a resolution that's specific to the current platform.

```toml
[tool.rye]
universal = true
```

## `tool.rye.generate-hashes`

+++ 0.35.0
Expand Down
6 changes: 5 additions & 1 deletion rye/src/cli/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ pub struct Args {
/// Set to true to lock with hashes in the lockfile.
#[arg(long)]
generate_hashes: bool,
/// Use universal lock files.
#[arg(long)]
universal: bool,
/// Reset prior lock options.
#[arg(long)]
reset: bool,
/// Use this pyproject.toml file
/// Use this pyproject.toml file.
#[arg(long, value_name = "PYPROJECT_TOML")]
pyproject: Option<PathBuf>,
}
Expand All @@ -62,6 +65,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
with_sources: cmd.with_sources,
reset: cmd.reset,
generate_hashes: cmd.generate_hashes,
universal: cmd.universal,
},
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 @@ -55,6 +55,9 @@ pub struct Args {
/// Do not reuse (reset) prior lock options.
#[arg(long)]
reset: bool,
/// Use universal lock files
#[arg(long)]
universal: bool,
}

pub fn execute(cmd: Args) -> Result<(), Error> {
Expand All @@ -78,6 +81,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
with_sources: cmd.with_sources,
reset: cmd.reset,
generate_hashes: cmd.generate_hashes,
universal: cmd.universal,
},
keyring_provider: cmd.keyring_provider,
pyproject: cmd.pyproject,
Expand Down
10 changes: 8 additions & 2 deletions rye/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ static REQUIREMENTS_HEADER: &str = r#"# generated by rye
# all-features: {{ lock_options.all_features|tojson }}
# with-sources: {{ lock_options.with_sources|tojson }}
# generate-hashes: {{ lock_options.generate_hashes|tojson }}
# universal: {{ lock_options.universal|tojson }}
"#;
static PARAM_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^# (pre|features|all-features|with-sources):\s*(.*?)$").unwrap());
static PARAM_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^# (pre|features|all-features|with-sources|universal):\s*(.*?)$").unwrap()
});

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LockMode {
Expand Down Expand Up @@ -103,6 +105,8 @@ pub struct LockOptions {
pub reset: bool,
/// Generate hashes in the lock file.
pub generate_hashes: bool,
/// Use universal lock files.
pub universal: bool,
}

impl LockOptions {
Expand Down Expand Up @@ -141,6 +145,7 @@ impl LockOptions {
"with-sources" => {
rv.with_sources = rv.with_sources || serde_json::from_str(value)?
}
"universal" => rv.universal = rv.universal || serde_json::from_str(value)?,
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -445,6 +450,7 @@ fn generate_lockfile(
upgrade,
keyring_provider,
lock_options.generate_hashes,
lock_options.universal,
)?;
} else {
if keyring_provider != KeyringProvider::Disabled {
Expand Down
21 changes: 21 additions & 0 deletions rye/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,11 @@ impl Workspace {
generate_hashes(&self.doc)
}

/// Should requirements.txt based locking be universal
pub fn universal(&self) -> bool {
universal(&self.doc)
}

/// Should requirements.txt based locking include a find-links reference?
pub fn lock_with_sources(&self) -> bool {
lock_with_sources(&self.doc)
Expand Down Expand Up @@ -1019,6 +1024,14 @@ impl PyProject {
}
}

/// Should requirements.txt-based locking be universal?
pub fn universal(&self) -> bool {
match self.workspace {
Some(ref workspace) => workspace.universal(),
None => universal(&self.doc),
}
}

/// Should requirements.txt-based locking include a find-links reference?
pub fn lock_with_sources(&self) -> bool {
match self.workspace {
Expand Down Expand Up @@ -1302,6 +1315,14 @@ fn generate_hashes(doc: &DocumentMut) -> bool {
.unwrap_or(false)
}

fn universal(doc: &DocumentMut) -> bool {
doc.get("tool")
.and_then(|x| x.get("rye"))
.and_then(|x| x.get("universal"))
.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"))
Expand Down
5 changes: 5 additions & 0 deletions rye/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {
cmd.lock_options.generate_hashes = true;
}

// Turn on universal locking if the project demands it.
if pyproject.universal() {
cmd.lock_options.universal = true;
}

// Turn on locking with sources if the project demands it.
if pyproject.lock_with_sources() {
cmd.lock_options.with_sources = true;
Expand Down
9 changes: 9 additions & 0 deletions rye/src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct UvCompileOptions {
pub no_header: bool,
pub keyring_provider: KeyringProvider,
pub generate_hashes: bool,
pub universal: bool,
}

impl UvCompileOptions {
Expand All @@ -65,6 +66,10 @@ impl UvCompileOptions {
cmd.arg("--exclude-newer").arg(dt);
}

if self.universal {
cmd.arg("--universal");
}

match self.upgrade {
UvPackageUpgrade::All => {
cmd.arg("--upgrade");
Expand All @@ -91,6 +96,7 @@ impl Default for UvCompileOptions {
no_header: false,
generate_hashes: false,
keyring_provider: KeyringProvider::Disabled,
universal: false,
}
}
}
Expand Down Expand Up @@ -350,6 +356,7 @@ impl Uv {
upgrade: UvPackageUpgrade,
keyring_provider: KeyringProvider,
generate_hashes: bool,
universal: bool,
) -> Result<(), Error> {
let options = UvCompileOptions {
allow_prerelease,
Expand All @@ -359,6 +366,7 @@ impl Uv {
no_header: true,
generate_hashes,
keyring_provider,
universal,
};

let mut cmd = self.cmd();
Expand Down Expand Up @@ -608,6 +616,7 @@ impl UvWithVenv {
no_header: true,
generate_hashes: false,
keyring_provider,
universal: false,
};

cmd.arg("pip").arg("compile");
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() {
# all-features: true
# with-sources: true
# generate-hashes: false
# universal: false
--index-url https://pypi.org/simple/
Expand Down Expand Up @@ -246,6 +247,7 @@ fn test_autosync_remember() {
# all-features: true
# with-sources: true
# generate-hashes: false
# universal: false
--index-url https://pypi.org/simple/
Expand Down

0 comments on commit 11fc495

Please sign in to comment.