Skip to main content

provenant/parsers/
conda.rs

1// SPDX-FileCopyrightText: Provenant contributors
2// SPDX-License-Identifier: Apache-2.0
3
4//! Parser for Conda/Anaconda package manifest files.
5//!
6//! Extracts package metadata and dependencies from Conda ecosystem manifest files
7//! supporting both recipe definitions and environment specifications.
8//!
9//! # Supported Formats
10//! - meta.yaml (Conda recipe metadata with Jinja2 templating support)
11//! - conda.yaml/environment.yml (Conda environment dependency specifications)
12//!
13//! # Key Features
14//! - YAML parsing for environment files
15//! - Dependency extraction from dependencies and build_requirements sections
16//! - Channel specification and platform detection
17//! - Version constraint parsing for Conda version specifiers
18//! - Package URL (purl) generation for conda packages
19//! - Limited meta.yaml support (note: Jinja2 templating not fully resolved)
20//!
21//! # Implementation Notes
22//! - Uses YAML parsing via `yaml_serde`
23//! - meta.yaml: Jinja2 templates not evaluated (use rendered YAML if available)
24//! - environment.yml: Full dependency specification support
25//! - Graceful error handling with `warn!()` logs
26//!
27//! # References
28//! - <https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html>
29//! - <https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html>
30
31use std::collections::HashMap;
32use std::path::Path;
33
34use crate::parser_warn as warn;
35use crate::parsers::utils::{MAX_ITERATION_COUNT, read_file_to_string, truncate_field};
36use regex::Regex;
37use yaml_serde::Value;
38
39use crate::models::{DatasourceId, Dependency, PackageData, PackageType, Sha256Digest};
40
41use super::PackageParser;
42use super::license_normalization::{
43    DeclaredLicenseMatchMetadata, build_declared_license_data_from_pair,
44    normalize_spdx_declared_license,
45};
46
47fn default_package_data(datasource_id: Option<DatasourceId>) -> PackageData {
48    PackageData {
49        package_type: Some(CondaMetaYamlParser::PACKAGE_TYPE),
50        datasource_id,
51        ..Default::default()
52    }
53}
54
55fn is_conda_recipe_yaml_path(path: &Path) -> bool {
56    let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
57        return false;
58    };
59    if name != "recipe.yaml" && name != "recipe.yml" {
60        return false;
61    }
62    path.parent()
63        .and_then(|parent| parent.file_name())
64        .and_then(|name| name.to_str())
65        .is_some_and(|name| name == "recipe")
66}
67
68/// Build a PURL (Package URL) for Conda or PyPI packages
69pub(crate) fn build_purl(
70    package_type: &str,
71    namespace: Option<&str>,
72    name: &str,
73    version: Option<&str>,
74    _qualifiers: Option<&str>,
75    _subpath: Option<&str>,
76    _extras: Option<&str>,
77) -> Option<String> {
78    let purl = match package_type {
79        "conda" => {
80            if let Some(ns) = namespace {
81                match version {
82                    Some(v) => format!("pkg:conda/{}/{}@{}", ns, name, v),
83                    None => format!("pkg:conda/{}/{}", ns, name),
84                }
85            } else {
86                match version {
87                    Some(v) => format!("pkg:conda/{}@{}", name, v),
88                    None => format!("pkg:conda/{}", name),
89                }
90            }
91        }
92        "pypi" => match version {
93            Some(v) => format!("pkg:pypi/{}@{}", name, v),
94            None => format!("pkg:pypi/{}", name),
95        },
96        _ => format!("pkg:{}/{}", package_type, name),
97    };
98    Some(purl)
99}
100
101fn build_conda_package_purl(name: Option<&str>, version: Option<&str>) -> Option<String> {
102    let name = name?;
103    build_purl("conda", None, name, version, None, None, None)
104}
105
106fn yaml_value_to_string(value: &Value) -> Option<String> {
107    match value {
108        Value::String(s) => Some(truncate_field(s.clone())),
109        Value::Number(n) => Some(truncate_field(n.to_string())),
110        Value::Bool(b) => Some(truncate_field(b.to_string())),
111        _ => None,
112    }
113}
114
115fn extract_jinja_statement(trimmed_line: &str) -> Option<&str> {
116    if !trimmed_line.starts_with("{%") {
117        return None;
118    }
119
120    let end = trimmed_line.find("%}")?;
121    Some(trimmed_line[2..end].trim())
122}
123
124fn extract_conda_requirement_name(req: &str) -> Option<String> {
125    let req = req.trim();
126    if req.is_empty() {
127        return None;
128    }
129
130    let req_without_ns = req.rsplit_once("::").map(|(_, rest)| rest).unwrap_or(req);
131
132    let name = req_without_ns
133        .split_whitespace()
134        .next()
135        .unwrap_or(req_without_ns)
136        .split(['=', '<', '>', '!', '~'])
137        .next()
138        .unwrap_or(req_without_ns)
139        .trim();
140
141    if name.is_empty() {
142        None
143    } else {
144        Some(truncate_field(name.to_string()))
145    }
146}
147
148/// Conda recipe manifest (meta.yaml) parser.
149///
150/// Extracts package metadata and dependencies from Conda recipe files, which
151/// define how to build a Conda package. Handles Jinja2 templating used in
152/// recipe files for variable substitution.
153pub struct CondaMetaYamlParser;
154
155impl PackageParser for CondaMetaYamlParser {
156    const PACKAGE_TYPE: PackageType = PackageType::Conda;
157
158    fn is_match(path: &Path) -> bool {
159        // Match */meta.yaml following Python reference logic
160        path.file_name()
161            .is_some_and(|name| name == "meta.yaml" || name == "meta.yml")
162            || is_conda_recipe_yaml_path(path)
163    }
164
165    fn extract_packages(path: &Path) -> Vec<PackageData> {
166        let contents = match read_file_to_string(path, None) {
167            Ok(c) => c,
168            Err(e) => {
169                warn!("Failed to read {}: {}", path.display(), e);
170                return vec![default_package_data(Some(DatasourceId::CondaMetaYaml))];
171            }
172        };
173
174        if is_conda_recipe_yaml_path(path) {
175            let yaml: Value = match yaml_serde::from_str(&contents) {
176                Ok(y) => y,
177                Err(e) => {
178                    warn!("Failed to parse YAML in {}: {}", path.display(), e);
179                    return vec![default_package_data(Some(DatasourceId::CondaMetaYaml))];
180                }
181            };
182
183            if !looks_like_conda_recipe_yaml(&yaml) {
184                return Vec::new();
185            }
186
187            return vec![parse_conda_recipe_yaml(&yaml)];
188        }
189
190        // Extract Jinja2 variables and apply crude substitution
191        let variables = extract_jinja2_variables(&contents);
192        let processed_yaml = apply_jinja2_substitutions(&contents, &variables);
193
194        // Parse YAML after Jinja2 processing
195        let yaml: Value = match yaml_serde::from_str(&processed_yaml) {
196            Ok(y) => y,
197            Err(e) => {
198                warn!("Failed to parse YAML in {}: {}", path.display(), e);
199                return vec![default_package_data(Some(DatasourceId::CondaMetaYaml))];
200            }
201        };
202
203        let package_element = yaml.get("package").and_then(|v| v.as_mapping());
204        let name = package_element
205            .and_then(|p| p.get("name"))
206            .and_then(yaml_value_to_string);
207
208        let version = package_element
209            .and_then(|p| p.get("version"))
210            .and_then(yaml_value_to_string);
211
212        let source = yaml.get("source").and_then(|v| v.as_mapping());
213        let download_url = source
214            .and_then(|s| s.get("url"))
215            .and_then(|v| v.as_str())
216            .map(|s| truncate_field(s.to_string()));
217
218        let sha256 = source
219            .and_then(|s| s.get("sha256"))
220            .and_then(|v| v.as_str())
221            .and_then(|s| Sha256Digest::from_hex(s).ok());
222
223        let about = yaml.get("about").and_then(|v| v.as_mapping());
224        let homepage_url = about
225            .and_then(|a| a.get("home"))
226            .and_then(|v| v.as_str())
227            .map(|s| truncate_field(s.to_string()));
228
229        let extracted_license_statement = about
230            .and_then(|a| a.get("license"))
231            .and_then(|v| v.as_str())
232            .map(|s| truncate_field(s.to_string()));
233        let (declared_license_expression, declared_license_expression_spdx, license_detections) =
234            normalize_conda_declared_license(extracted_license_statement.as_deref());
235
236        let description = about
237            .and_then(|a| a.get("summary"))
238            .and_then(|v| v.as_str())
239            .map(|s| truncate_field(s.to_string()));
240
241        let vcs_url = about
242            .and_then(|a| a.get("dev_url"))
243            .and_then(|v| v.as_str())
244            .map(|s| truncate_field(s.to_string()));
245        let license_file = about
246            .and_then(|a| a.get("license_file"))
247            .and_then(|v| v.as_str())
248            .map(str::trim)
249            .filter(|value| !value.is_empty())
250            .map(|s| truncate_field(s.to_string()));
251
252        // Extract dependencies from requirements sections
253        let mut dependencies = Vec::new();
254        let mut extra_data: HashMap<String, serde_json::Value> = HashMap::new();
255
256        if let Some(requirements) = yaml.get("requirements").and_then(|v| v.as_mapping()) {
257            for (scope_key, reqs_value) in requirements {
258                let scope = scope_key.as_str().unwrap_or("unknown");
259                if let Some(reqs) = reqs_value.as_sequence() {
260                    for req in reqs.iter().take(MAX_ITERATION_COUNT) {
261                        if let Some(req_str) = req.as_str()
262                            && let Some(dep) = parse_conda_requirement(req_str, scope)
263                        {
264                            // Filter out pip/python from dependencies, add to extra_data
265                            if extract_conda_requirement_name(req_str)
266                                .is_some_and(|n| n == "pip" || n == "python")
267                            {
268                                if let Some(arr) = extra_data
269                                    .entry(scope.to_string())
270                                    .or_insert_with(|| serde_json::Value::Array(vec![]))
271                                    .as_array_mut()
272                                {
273                                    arr.push(serde_json::Value::String(truncate_field(
274                                        req_str.to_string(),
275                                    )))
276                                }
277                            } else {
278                                dependencies.push(dep);
279                            }
280                        }
281                    }
282                }
283            }
284        }
285
286        let mut pkg = default_package_data(Some(DatasourceId::CondaMetaYaml));
287        pkg.package_type = Some(Self::PACKAGE_TYPE);
288        pkg.datasource_id = Some(DatasourceId::CondaMetaYaml);
289        pkg.name = name;
290        pkg.version = version;
291        pkg.purl = build_conda_package_purl(pkg.name.as_deref(), pkg.version.as_deref());
292        pkg.download_url = download_url;
293        pkg.homepage_url = homepage_url;
294        pkg.declared_license_expression = declared_license_expression.map(truncate_field);
295        pkg.declared_license_expression_spdx = declared_license_expression_spdx.map(truncate_field);
296        pkg.license_detections = license_detections;
297        pkg.extracted_license_statement = extracted_license_statement.map(truncate_field);
298        pkg.description = description;
299        pkg.vcs_url = vcs_url;
300        pkg.sha256 = sha256;
301        pkg.dependencies = dependencies;
302        if let Some(license_file) = license_file {
303            extra_data.insert(
304                "license_file".to_string(),
305                serde_json::Value::String(license_file),
306            );
307        }
308        if !extra_data.is_empty() {
309            pkg.extra_data = Some(extra_data);
310        }
311        vec![pkg]
312    }
313}
314
315fn looks_like_conda_recipe_yaml(yaml: &Value) -> bool {
316    yaml.get("schema_version")
317        .and_then(|value| value.as_u64())
318        .is_some_and(|value| value == 1)
319        && (yaml
320            .get("package")
321            .and_then(|value| value.as_mapping())
322            .is_some()
323            || yaml
324                .get("recipe")
325                .and_then(|value| value.as_mapping())
326                .is_some())
327}
328
329fn parse_conda_recipe_yaml(yaml: &Value) -> PackageData {
330    let context = extract_recipe_yaml_context(yaml);
331    let package = yaml
332        .get("package")
333        .or_else(|| yaml.get("recipe"))
334        .and_then(|value| value.as_mapping());
335    let source = yaml.get("source").and_then(|value| value.as_mapping());
336    let about = yaml.get("about").and_then(|value| value.as_mapping());
337
338    let name = package
339        .and_then(|pkg| pkg.get("name"))
340        .and_then(|value| recipe_yaml_value_to_string(value, &context));
341    let version = package
342        .and_then(|pkg| pkg.get("version"))
343        .and_then(|value| recipe_yaml_value_to_string(value, &context));
344
345    let download_url = source
346        .and_then(|src| src.get("url"))
347        .and_then(|value| recipe_yaml_value_to_string(value, &context));
348    let sha256 = source
349        .and_then(|src| src.get("sha256"))
350        .and_then(|value| recipe_yaml_value_to_string(value, &context))
351        .and_then(|value| Sha256Digest::from_hex(&value).ok());
352
353    let extracted_license_statement = about
354        .and_then(|section| section.get("license"))
355        .and_then(|value| recipe_yaml_value_to_string(value, &context));
356    let (declared_license_expression, declared_license_expression_spdx, license_detections) =
357        normalize_conda_declared_license(extracted_license_statement.as_deref());
358
359    let description = about
360        .and_then(|section| section.get("summary"))
361        .and_then(|value| recipe_yaml_value_to_string(value, &context));
362    let homepage_url = about
363        .and_then(|section| section.get("homepage").or_else(|| section.get("home")))
364        .and_then(|value| recipe_yaml_value_to_string(value, &context));
365    let vcs_url = about
366        .and_then(|section| {
367            section
368                .get("repository")
369                .or_else(|| section.get("dev_url"))
370                .or_else(|| section.get("repository_url"))
371        })
372        .and_then(|value| recipe_yaml_value_to_string(value, &context));
373    let documentation_url = about
374        .and_then(|section| section.get("documentation"))
375        .and_then(|value| recipe_yaml_value_to_string(value, &context));
376    let license_file = about
377        .and_then(|section| section.get("license_file"))
378        .and_then(|value| recipe_yaml_value_to_string(value, &context));
379
380    let mut dependencies = Vec::new();
381    let mut extra_data: HashMap<String, serde_json::Value> = HashMap::new();
382    if let Some(requirements) = yaml
383        .get("requirements")
384        .and_then(|value| value.as_mapping())
385    {
386        for (scope_key, reqs_value) in requirements {
387            let Some(scope) = scope_key.as_str() else {
388                continue;
389            };
390            let recipe_requirements = extract_recipe_yaml_requirement_strings(reqs_value, &context);
391            if recipe_requirements.is_empty() {
392                continue;
393            }
394
395            for req in &recipe_requirements {
396                if extract_conda_requirement_name(req)
397                    .is_some_and(|name| name == "pip" || name == "python")
398                {
399                    if let Some(arr) = extra_data
400                        .entry(scope.to_string())
401                        .or_insert_with(|| serde_json::Value::Array(vec![]))
402                        .as_array_mut()
403                    {
404                        arr.push(serde_json::Value::String(truncate_field(req.clone())));
405                    }
406                    continue;
407                }
408
409                if let Some(dep) = parse_conda_requirement(req, scope) {
410                    dependencies.push(dep);
411                }
412            }
413        }
414    }
415
416    if let Some(documentation_url) = documentation_url {
417        extra_data.insert(
418            "documentation".to_string(),
419            serde_json::Value::String(documentation_url),
420        );
421    }
422    if let Some(license_file) = license_file {
423        extra_data.insert(
424            "license_file".to_string(),
425            serde_json::Value::String(license_file),
426        );
427    }
428    extra_data.insert("schema_version".to_string(), serde_json::json!(1));
429
430    let mut pkg = default_package_data(Some(DatasourceId::CondaMetaYaml));
431    pkg.package_type = Some(CondaMetaYamlParser::PACKAGE_TYPE);
432    pkg.datasource_id = Some(DatasourceId::CondaMetaYaml);
433    pkg.name = name;
434    pkg.version = version;
435    pkg.purl = build_conda_package_purl(pkg.name.as_deref(), pkg.version.as_deref());
436    pkg.download_url = download_url;
437    pkg.homepage_url = homepage_url;
438    pkg.declared_license_expression = declared_license_expression.map(truncate_field);
439    pkg.declared_license_expression_spdx = declared_license_expression_spdx.map(truncate_field);
440    pkg.license_detections = license_detections;
441    pkg.extracted_license_statement = extracted_license_statement.map(truncate_field);
442    pkg.description = description;
443    pkg.vcs_url = vcs_url;
444    pkg.sha256 = sha256;
445    pkg.dependencies = dependencies;
446    pkg.extra_data = Some(extra_data);
447    pkg
448}
449
450fn extract_recipe_yaml_context(yaml: &Value) -> HashMap<String, String> {
451    let mut context = HashMap::new();
452    let Some(context_mapping) = yaml.get("context").and_then(|value| value.as_mapping()) else {
453        return context;
454    };
455
456    for (key, value) in context_mapping {
457        let Some(key) = key.as_str() else {
458            continue;
459        };
460        if let Some(value) = yaml_value_to_string(value) {
461            context.insert(truncate_field(key.to_string()), truncate_field(value));
462        }
463    }
464
465    context
466}
467
468fn recipe_yaml_value_to_string(value: &Value, context: &HashMap<String, String>) -> Option<String> {
469    let value = yaml_value_to_string(value)?;
470    Some(resolve_recipe_yaml_expressions(&value, context))
471}
472
473fn resolve_recipe_yaml_expressions(value: &str, context: &HashMap<String, String>) -> String {
474    let Some(re) = Regex::new(r#"\$\{\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}\}"#).ok() else {
475        return truncate_field(value.to_string());
476    };
477
478    let resolved = re.replace_all(value, |caps: &regex::Captures| {
479        context
480            .get(&caps[1])
481            .cloned()
482            .unwrap_or_else(|| caps[0].to_string())
483    });
484    truncate_field(resolved.into_owned())
485}
486
487fn extract_recipe_yaml_requirement_strings(
488    value: &Value,
489    context: &HashMap<String, String>,
490) -> Vec<String> {
491    let mut requirements = Vec::new();
492    collect_recipe_yaml_requirement_strings(value, context, &mut requirements);
493    requirements
494}
495
496fn collect_recipe_yaml_requirement_strings(
497    value: &Value,
498    context: &HashMap<String, String>,
499    requirements: &mut Vec<String>,
500) {
501    if let Some(req) = value.as_str() {
502        let resolved = resolve_recipe_yaml_expressions(req, context);
503        if should_keep_recipe_yaml_requirement(&resolved) {
504            requirements.push(resolved);
505        }
506        return;
507    }
508
509    if let Some(items) = value.as_sequence() {
510        for item in items.iter().take(MAX_ITERATION_COUNT) {
511            collect_recipe_yaml_requirement_strings(item, context, requirements);
512        }
513        return;
514    }
515
516    if let Some(mapping) = value.as_mapping() {
517        if let Some(then_value) = mapping.get("then") {
518            collect_recipe_yaml_requirement_strings(then_value, context, requirements);
519        }
520        if let Some(else_value) = mapping.get("else") {
521            collect_recipe_yaml_requirement_strings(else_value, context, requirements);
522        }
523    }
524}
525
526fn should_keep_recipe_yaml_requirement(req: &str) -> bool {
527    let trimmed = req.trim();
528    if trimmed.is_empty() {
529        return false;
530    }
531
532    !(trimmed.contains("${{")
533        || trimmed.contains("compiler('")
534        || trimmed.contains("compiler(\"")
535        || trimmed.contains("pin_subpackage(")
536        || trimmed.contains("pin_compatible(")
537        || trimmed.contains("stdlib('")
538        || trimmed.contains("stdlib(\""))
539}
540
541fn normalize_conda_declared_license(
542    statement: Option<&str>,
543) -> (
544    Option<String>,
545    Option<String>,
546    Vec<crate::models::LicenseDetection>,
547) {
548    match statement.map(str::trim).filter(|value| !value.is_empty()) {
549        Some("Apache Software") => build_declared_license_data_from_pair(
550            "apache-2.0",
551            "Apache-2.0",
552            DeclaredLicenseMatchMetadata::single_line("Apache Software"),
553        ),
554        Some("BSD-3-Clause") => build_declared_license_data_from_pair(
555            "bsd-new",
556            "BSD-3-Clause",
557            DeclaredLicenseMatchMetadata::single_line("BSD-3-Clause"),
558        ),
559        other => normalize_spdx_declared_license(other),
560    }
561}
562
563/// Conda environment file (environment.yml, conda.yaml) parser.
564///
565/// Extracts dependencies from Conda environment files used to define reproducible
566/// environments. Supports both Conda and pip dependencies, with channel specifications.
567pub struct CondaEnvironmentYmlParser;
568
569impl PackageParser for CondaEnvironmentYmlParser {
570    const PACKAGE_TYPE: PackageType = PackageType::Conda;
571
572    fn is_match(path: &Path) -> bool {
573        // Python reference: path_patterns = ('*conda*.yaml', '*env*.yaml', '*environment*.yaml')
574        if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
575            let lower = name.to_lowercase();
576            if matches!(lower.as_str(), "meta.yaml" | "meta.yml" | "recipe.yaml") {
577                return false;
578            }
579            let has_condaish_name =
580                lower.contains("conda") || lower.contains("env") || lower.contains("environment");
581            let has_condaish_ancestor = path
582                .ancestors()
583                .skip(1)
584                .filter_map(|ancestor| ancestor.file_name().and_then(|name| name.to_str()))
585                .any(|ancestor| ancestor.to_ascii_lowercase().contains("conda"));
586            (has_condaish_name || has_condaish_ancestor)
587                && (lower.ends_with(".yaml") || lower.ends_with(".yml"))
588        } else {
589            false
590        }
591    }
592
593    fn extract_packages(path: &Path) -> Vec<PackageData> {
594        let contents = match read_file_to_string(path, None) {
595            Ok(c) => c,
596            Err(e) => {
597                warn!("Failed to read {}: {}", path.display(), e);
598                return vec![default_package_data(Some(DatasourceId::CondaYaml))];
599            }
600        };
601
602        let yaml: Value = match yaml_serde::from_str(&contents) {
603            Ok(y) => y,
604            Err(e) => {
605                warn!("Failed to parse YAML in {}: {}", path.display(), e);
606                return vec![default_package_data(Some(DatasourceId::CondaYaml))];
607            }
608        };
609
610        if !looks_like_conda_environment_yaml(&yaml) {
611            return Vec::new();
612        }
613
614        let name = yaml
615            .get("name")
616            .and_then(|v| v.as_str())
617            .map(|s| truncate_field(s.to_string()));
618
619        let dependencies = extract_environment_dependencies(&yaml);
620
621        let mut extra_data = HashMap::new();
622        if let Some(channels) = yaml.get("channels").and_then(|v| v.as_sequence()) {
623            let channels_vec: Vec<String> = channels
624                .iter()
625                .filter_map(|c| c.as_str().map(|s| truncate_field(s.to_string())))
626                .collect();
627            if !channels_vec.is_empty() {
628                extra_data.insert("channels".to_string(), serde_json::json!(channels_vec));
629            }
630        }
631
632        // Environment files are private (not published packages)
633        let mut pkg = default_package_data(Some(DatasourceId::CondaYaml));
634        pkg.package_type = Some(Self::PACKAGE_TYPE);
635        pkg.datasource_id = Some(DatasourceId::CondaYaml);
636        pkg.name = name;
637        pkg.purl = build_conda_package_purl(pkg.name.as_deref(), pkg.version.as_deref());
638        pkg.primary_language = Some(truncate_field("Python".to_string()));
639        pkg.dependencies = dependencies;
640        pkg.is_private = true;
641        if !extra_data.is_empty() {
642            pkg.extra_data = Some(extra_data);
643        }
644        vec![pkg]
645    }
646}
647
648fn looks_like_conda_environment_yaml(yaml: &Value) -> bool {
649    let has_dependencies = yaml
650        .get("dependencies")
651        .and_then(|value| value.as_sequence())
652        .is_some_and(|items| !items.is_empty());
653    let has_channels = yaml
654        .get("channels")
655        .and_then(|value| value.as_sequence())
656        .is_some_and(|items| !items.is_empty());
657    let has_prefix = yaml
658        .get("prefix")
659        .and_then(|value| value.as_str())
660        .is_some_and(|value| !value.trim().is_empty());
661
662    has_dependencies || has_channels || has_prefix
663}
664
665/// Extract Jinja2-style variables from a Conda meta.yaml
666///
667/// For example, lines like `{% set version = "0.45.0" %}` and
668/// `{% set sha256 = "abc123..." %}` are captured as variables.
669pub fn extract_jinja2_variables(content: &str) -> HashMap<String, String> {
670    let mut variables = HashMap::new();
671
672    for line in content.lines().take(MAX_ITERATION_COUNT) {
673        let trimmed = line.trim();
674        if let Some(inner) = extract_jinja_statement(trimmed)
675            && let Some(inner) = inner.strip_prefix("set").map(str::trim)
676            && let Some((key, value)) = inner.split_once('=')
677        {
678            let key = key.trim();
679            let value = value.trim().trim_matches('"').trim_matches('\'');
680            variables.insert(
681                truncate_field(key.to_string()),
682                truncate_field(value.to_string()),
683            );
684        }
685    }
686
687    variables
688}
689
690/// Apply Jinja2 variable substitutions to YAML content
691///
692/// Supports:
693/// - `{{ variable }}` - Simple substitution
694/// - `{{ variable|lower }}` - Lowercase filter
695pub fn apply_jinja2_substitutions(content: &str, variables: &HashMap<String, String>) -> String {
696    let mut result = Vec::new();
697
698    for line in content.lines() {
699        let trimmed = line.trim();
700
701        if extract_jinja_statement(trimmed).is_some() {
702            continue;
703        }
704
705        let mut processed_line = line.to_string();
706
707        // Apply variable substitutions
708        if line.contains("{{") && line.contains("}}") {
709            for (var_name, var_value) in variables {
710                // Handle |lower filter
711                let pattern_lower = format!("{{{{ {}|lower }}}}", var_name);
712                if processed_line.contains(&pattern_lower) {
713                    processed_line =
714                        processed_line.replace(&pattern_lower, &var_value.to_lowercase());
715                }
716
717                // Handle normal substitution
718                let pattern_normal = format!("{{{{ {} }}}}", var_name);
719                processed_line = processed_line.replace(&pattern_normal, var_value);
720            }
721        }
722
723        // Skip lines with unresolved Jinja2 templates (complex expressions we can't handle)
724        if processed_line.contains("{{") {
725            continue;
726        }
727
728        result.push(processed_line);
729    }
730
731    quote_plain_numeric_version_scalars(&result.join("\n"))
732}
733
734fn quote_plain_numeric_version_scalars(content: &str) -> String {
735    let Some(version_re) =
736        Regex::new(r#"^(\s*(?:-\s*)?version:\s*)([0-9]+(?:\.[0-9]+)+)(\s*)$"#).ok()
737    else {
738        return content.to_string();
739    };
740
741    content
742        .lines()
743        .map(|line| {
744            version_re
745                .replace(line, |caps: &regex::Captures| {
746                    format!(r#"{}"{}"{}"#, &caps[1], &caps[2], &caps[3])
747                })
748                .into_owned()
749        })
750        .collect::<Vec<_>>()
751        .join("\n")
752}
753
754/// Parse a Conda requirement string into a Dependency
755///
756/// Format examples:
757/// - `mccortex ==1.0` - Pinned version with space before operator
758/// - `python >=3.6` - Version constraint
759/// - `conda-forge::numpy=1.15.4` - Namespace and pinned version (no space)
760/// - `bwa` - No version specified
761pub fn parse_conda_requirement(req: &str, scope: &str) -> Option<Dependency> {
762    let req = req.trim();
763
764    // Handle namespace prefix (conda-forge::package)
765    let (namespace, channel_url, req_without_ns) = parse_conda_channel_prefix(req);
766
767    // Split on first space to separate name from version constraint
768    let (name_part, version_constraint) =
769        if let Some((name, constraint)) = req_without_ns.split_once(' ') {
770            (name.trim(), Some(constraint.trim()))
771        } else {
772            (req_without_ns, None)
773        };
774
775    // Check for pinned version with `=` (no space): package=1.0
776    let (name, version, is_pinned, extracted_requirement) = if name_part.contains('=') {
777        let parts: Vec<&str> = name_part.splitn(2, '=').collect();
778        let n = parts[0].trim();
779        let v = if parts.len() > 1 {
780            let parsed = parts[1].trim();
781            if parsed.is_empty() {
782                None
783            } else {
784                Some(truncate_field(parsed.to_string()))
785            }
786        } else {
787            None
788        };
789        let req = v
790            .as_ref()
791            .map(|ver| format!("={}", ver))
792            .unwrap_or_default();
793        (n, v, true, Some(truncate_field(req)))
794    } else if let Some(constraint) = version_constraint {
795        let version_opt = if constraint.starts_with("==") {
796            Some(truncate_field(
797                constraint.trim_start_matches("==").trim().to_string(),
798            ))
799        } else {
800            None
801        };
802        (
803            name_part.trim(),
804            version_opt,
805            false,
806            Some(truncate_field(constraint.to_string())),
807        )
808    } else {
809        (name_part.trim(), None, false, Some(String::new()))
810    };
811
812    // Build PURL
813    let purl = build_purl(
814        "conda",
815        namespace,
816        name,
817        version.as_deref(),
818        None,
819        None,
820        None,
821    );
822
823    // Determine is_runtime and is_optional based on scope
824    let (is_runtime, is_optional) = match scope {
825        "run" => (true, false),
826        _ => (false, true), // build, host, test are all optional
827    };
828
829    let mut extra_data = HashMap::new();
830    if let Some(namespace) = namespace {
831        extra_data.insert(
832            "channel".to_string(),
833            serde_json::json!(truncate_field(namespace.to_string())),
834        );
835    }
836    if let Some(channel_url) = channel_url {
837        extra_data.insert(
838            "channel_url".to_string(),
839            serde_json::json!(truncate_field(channel_url.to_string())),
840        );
841    }
842
843    Some(Dependency {
844        purl,
845        extracted_requirement,
846        scope: Some(truncate_field(scope.to_string())),
847        is_runtime: Some(is_runtime),
848        is_optional: Some(is_optional),
849        is_pinned: Some(is_pinned),
850        is_direct: Some(true),
851        resolved_package: None,
852        extra_data: (!extra_data.is_empty()).then_some(extra_data),
853    })
854}
855
856fn extract_environment_dependencies(yaml: &Value) -> Vec<Dependency> {
857    let dependencies = match yaml.get("dependencies").and_then(|v| v.as_sequence()) {
858        Some(d) => d,
859        None => return Vec::new(),
860    };
861
862    let mut deps = Vec::new();
863    for dep_value in dependencies.iter().take(MAX_ITERATION_COUNT) {
864        if let Some(dep_str) = dep_value.as_str() {
865            if let Some(dep) = parse_environment_string_dependency(dep_str) {
866                deps.push(dep);
867            }
868        } else if let Some(pip_deps) = dep_value.get("pip").and_then(|v| v.as_sequence()) {
869            deps.extend(extract_pip_dependencies(pip_deps));
870        }
871    }
872    deps
873}
874
875fn parse_environment_string_dependency(dep_str: &str) -> Option<Dependency> {
876    let (namespace, channel_url, dep_without_ns) = parse_conda_channel_prefix(dep_str);
877    create_conda_dependency(namespace, channel_url, dep_without_ns, "dependencies")
878}
879
880fn parse_conda_exact_requirement(req_no_space: &str) -> (Option<String>, Option<String>) {
881    let exact = req_no_space
882        .strip_prefix("==")
883        .or_else(|| req_no_space.strip_prefix('='));
884
885    let Some(exact) = exact else {
886        return (None, None);
887    };
888
889    if exact.is_empty() {
890        return (None, None);
891    }
892
893    match exact.split_once('=') {
894        Some((version, build_string)) if !version.is_empty() => (
895            Some(truncate_field(version.to_string())),
896            (!build_string.is_empty()).then(|| truncate_field(build_string.to_string())),
897        ),
898        _ => (Some(truncate_field(exact.to_string())), None),
899    }
900}
901
902fn parse_conda_channel_prefix(dep_str: &str) -> (Option<&str>, Option<&str>, &str) {
903    if let Some((ns, rest)) = dep_str.rsplit_once("::") {
904        if ns.contains('/') || ns.contains(':') {
905            (None, Some(ns), rest)
906        } else {
907            (Some(ns), None, rest)
908        }
909    } else {
910        (None, None, dep_str)
911    }
912}
913
914fn create_conda_dependency(
915    namespace: Option<&str>,
916    channel_url: Option<&str>,
917    dep_without_ns: &str,
918    scope: &str,
919) -> Option<Dependency> {
920    let dep = dep_without_ns.trim();
921    let name_re = match Regex::new(r"^([A-Za-z0-9_.\-]+)") {
922        Ok(re) => re,
923        Err(_) => return None,
924    };
925
926    let caps = name_re.captures(dep)?;
927    let name_match = caps.get(1)?;
928    let name = name_match.as_str().trim();
929    let rest = dep[name_match.end()..].trim();
930
931    let (version, build_string, is_pinned, extracted_requirement) = if rest.is_empty() {
932        (None, None, false, Some(String::new()))
933    } else {
934        let req_no_space = rest.replace(' ', "");
935        let is_exact = req_no_space.starts_with("=") || req_no_space.starts_with("==");
936        let (parsed_version, parsed_build_string) = if is_exact {
937            parse_conda_exact_requirement(&req_no_space)
938        } else {
939            (None, None)
940        };
941
942        (
943            parsed_version,
944            parsed_build_string,
945            is_exact,
946            Some(truncate_field(rest.to_string())),
947        )
948    };
949
950    if name == "pip" || name == "python" {
951        return None;
952    }
953
954    let purl = build_purl(
955        "conda",
956        namespace,
957        name,
958        version.as_deref(),
959        None,
960        None,
961        None,
962    );
963    let mut extra_data = HashMap::new();
964    if let Some(namespace) = namespace {
965        extra_data.insert(
966            "channel".to_string(),
967            serde_json::json!(truncate_field(namespace.to_string())),
968        );
969    }
970    if let Some(channel_url) = channel_url {
971        extra_data.insert(
972            "channel_url".to_string(),
973            serde_json::json!(truncate_field(channel_url.to_string())),
974        );
975    }
976    if let Some(build_string) = build_string {
977        extra_data.insert("build_string".to_string(), serde_json::json!(build_string));
978    }
979
980    Some(Dependency {
981        purl,
982        extracted_requirement,
983        scope: Some(truncate_field(scope.to_string())),
984        is_runtime: Some(true),
985        is_optional: Some(false),
986        is_pinned: Some(is_pinned),
987        is_direct: Some(true),
988        resolved_package: None,
989        extra_data: (!extra_data.is_empty()).then_some(extra_data),
990    })
991}
992
993fn extract_pip_dependencies(pip_deps: &[Value]) -> Vec<Dependency> {
994    pip_deps
995        .iter()
996        .take(MAX_ITERATION_COUNT)
997        .filter_map(|pip_dep| {
998            if let Some(pip_req_str) = pip_dep.as_str()
999                && let Ok(parsed_req) = pip_req_str.parse::<pep508_rs::Requirement>()
1000            {
1001                create_pip_dependency(parsed_req, "dependencies", Some(pip_req_str))
1002            } else {
1003                None
1004            }
1005        })
1006        .collect()
1007}
1008
1009fn create_pip_dependency(
1010    parsed_req: pep508_rs::Requirement,
1011    scope: &str,
1012    raw_requirement: Option<&str>,
1013) -> Option<Dependency> {
1014    let name = truncate_field(parsed_req.name.to_string());
1015
1016    if name == "pip" || name == "python" {
1017        return None;
1018    }
1019
1020    let specs = parsed_req.version_or_url.as_ref().map(|v| match v {
1021        pep508_rs::VersionOrUrl::VersionSpecifier(spec) => truncate_field(spec.to_string()),
1022        pep508_rs::VersionOrUrl::Url(url) => truncate_field(url.to_string()),
1023    });
1024
1025    let extracted_requirement = if let Some(raw) = raw_requirement {
1026        let raw = raw.trim();
1027        let suffix = raw.strip_prefix(&name).unwrap_or(raw).trim().to_string();
1028        Some(truncate_field(suffix))
1029    } else {
1030        Some(truncate_field(specs.clone().unwrap_or_default()))
1031    };
1032
1033    let version = specs.as_ref().and_then(|spec_str| {
1034        if spec_str.starts_with("==") {
1035            Some(truncate_field(
1036                spec_str.trim_start_matches("==").to_string(),
1037            ))
1038        } else {
1039            None
1040        }
1041    });
1042
1043    let is_pinned = specs.as_ref().map(|s| s.contains("==")).unwrap_or(false);
1044    let purl = build_purl("pypi", None, &name, version.as_deref(), None, None, None);
1045
1046    Some(Dependency {
1047        purl,
1048        extracted_requirement,
1049        scope: Some(truncate_field(scope.to_string())),
1050        is_runtime: Some(true),
1051        is_optional: Some(false),
1052        is_pinned: Some(is_pinned),
1053        is_direct: Some(true),
1054        resolved_package: None,
1055        extra_data: None,
1056    })
1057}
1058
1059crate::register_parser!(
1060    "Conda package manifest and environment file",
1061    &[
1062        "**/meta.yaml",
1063        "**/meta.yml",
1064        "**/recipe/recipe.yaml",
1065        "**/recipe/recipe.yml",
1066        "**/environment.yml",
1067        "**/environment.yaml",
1068        "**/env.yaml",
1069        "**/env.yml",
1070        "**/conda.yaml",
1071        "**/conda.yml",
1072        "**/*conda*.yaml",
1073        "**/*conda*.yml",
1074        "**/*env*.yaml",
1075        "**/*env*.yml",
1076        "**/*environment*.yaml",
1077        "**/*environment*.yml"
1078    ],
1079    "conda",
1080    "Python",
1081    Some("https://docs.conda.io/"),
1082);