|
| 1 | +use std::{ |
| 2 | + env, |
| 3 | + path::{Component, Path}, |
| 4 | + str::FromStr, |
| 5 | +}; |
| 6 | + |
| 7 | +use dunce::canonicalize; |
| 8 | +use pixi_spec::PathSpec; |
| 9 | +use rattler_conda_types::{MatchSpec, PackageName, ParseStrictness, package::ArchiveIdentifier}; |
| 10 | + |
| 11 | +/// Represents either a regular conda MatchSpec or a filesystem path to a conda artifact. |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub enum MatchSpecOrPath { |
| 14 | + MatchSpec(Box<MatchSpec>), |
| 15 | + Path(PathSpec), |
| 16 | +} |
| 17 | + |
| 18 | +impl MatchSpecOrPath { |
| 19 | + pub fn as_match_spec(&self) -> Option<&MatchSpec> { |
| 20 | + if let Self::MatchSpec(spec) = self { |
| 21 | + Some(spec.as_ref()) |
| 22 | + } else { |
| 23 | + None |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + pub fn is_path(&self) -> bool { |
| 28 | + matches!(self, Self::Path(_)) |
| 29 | + } |
| 30 | + |
| 31 | + pub fn display_name(&self) -> Option<String> { |
| 32 | + match self { |
| 33 | + Self::MatchSpec(spec) => spec |
| 34 | + .name |
| 35 | + .as_ref() |
| 36 | + .map(|name| name.as_normalized().to_string()), |
| 37 | + Self::Path(path_spec) => path_spec |
| 38 | + .path |
| 39 | + .file_name() |
| 40 | + .map(|fname| fname.to_string()) |
| 41 | + .or_else(|| Some(path_spec.path.as_str().to_string())), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Convert into a MatchSpec suitable for execution, turning paths into file URLs. |
| 46 | + pub fn into_exec_match_spec(self) -> Result<MatchSpec, String> { |
| 47 | + match self { |
| 48 | + Self::MatchSpec(spec) => Ok(*spec), |
| 49 | + Self::Path(path_spec) => path_spec_to_match_spec(path_spec), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns the underlying PathSpec, if any. |
| 54 | + pub fn into_path_spec(self) -> Result<PathSpec, String> { |
| 55 | + match self { |
| 56 | + Self::Path(path) => Ok(path), |
| 57 | + Self::MatchSpec(_) => Err("expected a path dependency".into()), |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl FromStr for MatchSpecOrPath { |
| 63 | + type Err = String; |
| 64 | + |
| 65 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 66 | + // Check if this is a URL pointing to a conda package |
| 67 | + // Rattler's MatchSpec parser doesn't recognize URLs with schemes, so we handle them here |
| 68 | + if let Ok(url) = url::Url::parse(value) { |
| 69 | + if let Some(archive) = ArchiveIdentifier::try_from_url(&url) { |
| 70 | + // This is a URL to a conda package |
| 71 | + let name = PackageName::try_from(archive.name) |
| 72 | + .map_err(|e| format!("invalid package name: {e}"))?; |
| 73 | + |
| 74 | + return Ok(Self::MatchSpec(Box::new(MatchSpec { |
| 75 | + name: Some(name), |
| 76 | + url: Some(url), |
| 77 | + ..MatchSpec::default() |
| 78 | + }))); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + match MatchSpec::from_str(value, ParseStrictness::Lenient) { |
| 83 | + Ok(spec) => Ok(Self::MatchSpec(Box::new(spec))), |
| 84 | + Err(parse_err) => { |
| 85 | + if looks_like_path(value) { |
| 86 | + let path_spec = build_path_spec(value)?; |
| 87 | + Ok(Self::Path(path_spec)) |
| 88 | + } else { |
| 89 | + Err(parse_err.to_string()) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +fn build_path_spec(value: &str) -> Result<PathSpec, String> { |
| 97 | + let provided = Path::new(value); |
| 98 | + let joined = if provided.is_absolute() { |
| 99 | + provided.to_path_buf() |
| 100 | + } else { |
| 101 | + let cwd = env::current_dir() |
| 102 | + .map_err(|err| format!("failed to determine current directory: {err}"))?; |
| 103 | + cwd.join(provided) |
| 104 | + }; |
| 105 | + |
| 106 | + // Use canonical path when available to avoid duplicate cache keys, but fall back silently. |
| 107 | + let absolute = canonicalize(&joined).unwrap_or(joined); |
| 108 | + let path_str = absolute |
| 109 | + .to_str() |
| 110 | + .ok_or_else(|| format!("path '{}' is not valid UTF-8", absolute.display()))?; |
| 111 | + |
| 112 | + Ok(PathSpec::new(path_str.to_string())) |
| 113 | +} |
| 114 | + |
| 115 | +fn looks_like_path(value: &str) -> bool { |
| 116 | + if value.is_empty() { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + if value.contains("::") { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + let path = Path::new(value); |
| 125 | + if path.is_absolute() { |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + let mut components = path.components(); |
| 130 | + let Some(first) = components.next() else { |
| 131 | + return false; |
| 132 | + }; |
| 133 | + |
| 134 | + let starts_with_dot = matches!(first, Component::CurDir | Component::ParentDir); |
| 135 | + let has_multiple_components = components.next().is_some(); |
| 136 | + let looks_like_archive = value.ends_with(".conda") || value.ends_with(".tar.bz2"); |
| 137 | + |
| 138 | + starts_with_dot |
| 139 | + || has_multiple_components |
| 140 | + || value.contains(std::path::MAIN_SEPARATOR) |
| 141 | + || value.contains('/') |
| 142 | + || value.contains('\\') |
| 143 | + || looks_like_archive |
| 144 | +} |
| 145 | + |
| 146 | +fn path_spec_to_match_spec(path_spec: PathSpec) -> Result<MatchSpec, String> { |
| 147 | + let path = Path::new(path_spec.path.as_str()); |
| 148 | + |
| 149 | + // Invariant for if we ever change stuff around |
| 150 | + debug_assert!( |
| 151 | + path.is_absolute(), |
| 152 | + "path_spec_to_match_spec expects absolute paths" |
| 153 | + ); |
| 154 | + |
| 155 | + let url = url::Url::from_file_path(path) |
| 156 | + .map_err(|_| format!("failed to convert '{}' into a file:// url", path.display()))?; |
| 157 | + |
| 158 | + // Extract package name from the archive |
| 159 | + let archive = ArchiveIdentifier::try_from_url(&url) |
| 160 | + .ok_or_else(|| format!("failed to parse package archive from '{url}'"))?; |
| 161 | + |
| 162 | + let name = |
| 163 | + PackageName::try_from(archive.name).map_err(|e| format!("invalid package name: {e}"))?; |
| 164 | + |
| 165 | + Ok(MatchSpec { |
| 166 | + name: Some(name), |
| 167 | + url: Some(url), |
| 168 | + ..MatchSpec::default() |
| 169 | + }) |
| 170 | +} |
| 171 | + |
| 172 | +#[cfg(test)] |
| 173 | +mod tests { |
| 174 | + use super::*; |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn detects_relative_like_inputs() { |
| 178 | + assert!(looks_like_path("./pkg/file.conda")); |
| 179 | + assert!(looks_like_path("pkg/file.conda")); |
| 180 | + assert!(looks_like_path("file.tar.bz2")); |
| 181 | + assert!(looks_like_path("file.conda")); |
| 182 | + assert!(!looks_like_path("python>=3.12")); |
| 183 | + assert!(!looks_like_path("conda-forge::python")); |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn parses_https_url() { |
| 188 | + let result = MatchSpecOrPath::from_str( |
| 189 | + "https://conda.anaconda.org/conda-forge/noarch/tzdata-2024b-hc8b5060_0.conda", |
| 190 | + ); |
| 191 | + assert!(result.is_ok(), "Failed to parse HTTPS URL: {result:?}"); |
| 192 | + let spec_or_path = result.unwrap(); |
| 193 | + match spec_or_path { |
| 194 | + MatchSpecOrPath::MatchSpec(spec) => { |
| 195 | + assert_eq!( |
| 196 | + spec.name.as_ref().map(|n| n.as_normalized()), |
| 197 | + Some("tzdata") |
| 198 | + ); |
| 199 | + assert!(spec.url.is_some()); |
| 200 | + } |
| 201 | + _ => panic!("Expected MatchSpec, got Path"), |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + #[test] |
| 206 | + fn parses_file_url() { |
| 207 | + let result = MatchSpecOrPath::from_str("file:///tmp/test-package-1.0.0-h123_0.conda"); |
| 208 | + assert!(result.is_ok()); |
| 209 | + let spec_or_path = result.unwrap(); |
| 210 | + match spec_or_path { |
| 211 | + MatchSpecOrPath::MatchSpec(spec) => { |
| 212 | + assert_eq!( |
| 213 | + spec.name.as_ref().map(|n| n.as_normalized()), |
| 214 | + Some("test-package") |
| 215 | + ); |
| 216 | + assert!(spec.url.is_some()); |
| 217 | + assert_eq!(spec.url.as_ref().unwrap().scheme(), "file"); |
| 218 | + } |
| 219 | + _ => panic!("Expected MatchSpec, got Path"), |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments