Skip to content
Merged
82 changes: 72 additions & 10 deletions crates/pixi_cli/src/workspace/export/conda_explicit_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub struct Args {
#[arg(long, default_value = "false")]
pub ignore_source_errors: bool,

/// Emit sha256 hashes instead of md5. Installing the result requires conda >=24.9 or mamba >=2.1.
#[arg(long, default_value = "false")]
pub sha256: bool,

#[clap(flatten)]
pub lock_file_update_config: LockFileUpdateConfig,

Expand All @@ -63,18 +67,26 @@ pub struct Args {
fn build_explicit_spec<'a>(
platform: &Subdir,
conda_packages: impl IntoIterator<Item = &'a RepoDataRecord>,
sha256: bool,
) -> miette::Result<ExplicitEnvironmentSpec> {
let mut packages = Vec::new();

for cp in conda_packages {
let prec = &cp.package_record;
let hash = prec.md5.ok_or(miette::miette!(
"Package {} does not contain an md5 hash",
prec.name.as_normalized()
))?;
let (hash, kind) = if sha256 {
(prec.sha256.map(hex::encode), "a sha256")
} else {
(prec.md5.map(hex::encode), "an md5")
};
let hash = hash.ok_or_else(|| {
miette::miette!(
"package {} does not contain {kind} hash",
prec.name.as_normalized()
)
})?;

let mut url = cp.url.clone();
url.set_fragment(Some(&hex::encode(hash)));
url.set_fragment(Some(&hash));

packages.push(ExplicitEnvironmentEntry {
url: url.to_owned(),
Expand Down Expand Up @@ -124,6 +136,7 @@ fn render_env_platform(
env: &Environment,
platform: LockedPlatform<'_>,
ignore_pypi_errors: bool,
sha256: bool,
) -> miette::Result<()> {
// Rich platforms (platforms carrying system-requirements) are named
// independently of their conda subdir, and several of them can share one
Expand Down Expand Up @@ -178,7 +191,7 @@ fn render_env_platform(

let repodata = PackageRecord::sort_topologically(repodata);

let ees = build_explicit_spec(&subdir, &repodata)?;
let ees = build_explicit_spec(&subdir, &repodata, sha256)?;

tracing::info!("Creating conda explicit spec for env: {env_name} platform: {platform_name}");
let target = output_dir
Expand Down Expand Up @@ -289,6 +302,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
&env,
plat,
args.ignore_pypi_errors,
args.sha256,
)?;
}

Expand All @@ -297,7 +311,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {

#[cfg(test)]
mod tests {
use std::path::Path;
use std::{collections::HashMap, path::Path};

use rattler_lock::LockFile;
use tempfile::tempdir;
Expand All @@ -318,10 +332,11 @@ mod tests {
// example contains pypi dependencies so should fail if `ignore_pypi_errors` is
// false.
assert!(
render_env_platform(output_dir.path(), env_name, &env, platform, false)
render_env_platform(output_dir.path(), env_name, &env, platform, false, false)
.is_err()
);
render_env_platform(output_dir.path(), env_name, &env, platform, true).unwrap();
render_env_platform(output_dir.path(), env_name, &env, platform, true, false)
.unwrap();

let file_path = output_dir
.path()
Expand All @@ -334,6 +349,52 @@ mod tests {
}
}

#[test]
fn test_render_conda_explicit_spec_sha256() {
let path = Path::new(env!("CARGO_WORKSPACE_DIR"))
.join("tests/data/mock-projects/test-project-export/pixi.lock");
let lock_file = LockFile::from_path(&path).unwrap();

let output_dir = tempdir().unwrap();

for (env_name, env) in lock_file.environments() {
for platform in env.platforms() {
let platform_name = platform.name();
render_env_platform(output_dir.path(), env_name, &env, platform, true, true)
.unwrap();

let expected: HashMap<String, String> = env
.packages(platform)
.unwrap()
.filter_map(|package| match package {
LockedPackage::Conda(CondaPackageData::Binary(p)) => {
let record = RepoDataRecord::try_from(*p.clone()).unwrap();
let sha256 = hex::encode(record.package_record.sha256.unwrap());
Some((record.url.to_string(), sha256))
}
_ => None,
})
.collect();

let file_path = output_dir
.path()
.join(format!("{env_name}_{platform_name}_conda_spec.txt"));
let mut seen = 0;
for line in fs_err::read_to_string(file_path).unwrap().lines() {
let Some((url, hash)) = line.split_once('#') else {
continue;
};
if !url.contains("://") {
continue;
}
assert_eq!(expected.get(url), Some(&hash.to_string()), "{line}");
seen += 1;
}
assert_eq!(seen, expected.len());
}
}
}

/// Loads the rich-platform fixture, which declares five platforms across
/// three subdirs, two of which (`osx-arm64` and `linux-64`) are covered by
/// more than one platform.
Expand All @@ -352,7 +413,8 @@ mod tests {
for (env_name, env) in lock_file.environments() {
for platform in env.platforms() {
let platform_name = platform.name();
render_env_platform(output_dir.path(), env_name, &env, platform, true).unwrap();
render_env_platform(output_dir.path(), env_name, &env, platform, true, false)
.unwrap();

let file_path = output_dir
.path()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pixi workspace export conda-explicit-spec [OPTIONS] <OUTPUT_DIR>
- <a id="arg---ignore-source-errors" href="#arg---ignore-source-errors">`--ignore-source-errors`</a>
: Source dependencies are not supported in the conda explicit spec file
<br>**default**: `false`
- <a id="arg---sha256" href="#arg---sha256">`--sha256`</a>
: Emit sha256 hashes instead of md5. Installing the result requires conda >=24.9 or mamba >=2.1
<br>**default**: `false`

## Config Options
- <a id="arg---no-config" href="#arg---no-config">`--no-config`</a>
Expand Down
5 changes: 5 additions & 0 deletions tests/integration_python/test_main_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,11 @@ def check_invariants(command_name: str) -> None:
[str(tmp_pixi_workspace / "export_test")],
"pixi workspace export conda-explicit-spec",
),
(
["workspace", "export", "conda-explicit-spec", "--sha256"],
[str(tmp_pixi_workspace / "export_test_sha256")],
"pixi workspace export conda-explicit-spec --sha256",
),
(
["workspace", "exclude-newer", "set"],
["7d"],
Expand Down
Loading