normalize-package-index 0.3.2

Package index ingestion from distro and language registries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! DNF package index fetcher (Fedora/RHEL).
//!
//! Fetches package metadata from Fedora repositories.
//!
//! ## API Strategy
//! - **fetch**: `mdapi.fedoraproject.org/{release}/pkg/{name}` - Fedora MDAPI JSON
//! - **fetch_versions**: Queries all configured releases
//! - **search**: `apps.fedoraproject.org/packages/fcomm_connector/xapian/query/search_packages`
//! - **fetch_all**: Queries srcpkg list from all configured releases
//!
//! ## Multi-repo Support
//! ```rust,ignore
//! use normalize_packages::index::dnf::{Dnf, DnfRepo};
//!
//! // All repos (default)
//! let all = Dnf::all();
//!
//! // Current stable releases only
//! let stable = Dnf::stable();
//!
//! // Rawhide only
//! let rawhide = Dnf::rawhide();
//!
//! // Custom selection
//! let custom = Dnf::with_repos(&[DnfRepo::Fedora41, DnfRepo::Rawhide]);
//! ```

use super::{IndexError, PackageIndex, PackageMeta, VersionMeta};
use crate::cache;
use rayon::prelude::*;
use std::collections::HashMap;
use std::time::Duration;

/// Cache TTL for package lists (1 hour).
const CACHE_TTL: Duration = Duration::from_secs(60 * 60);

/// Available DNF repositories.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DnfRepo {
    // === Fedora Stable ===
    /// Fedora 41 (current)
    Fedora41,
    /// Fedora 40
    Fedora40,
    /// Fedora 39
    Fedora39,

    // === Fedora Development ===
    /// Fedora Rawhide (development)
    Rawhide,

    // === EPEL (Enterprise Linux) ===
    /// EPEL 9 (RHEL 9 / CentOS Stream 9)
    Epel9,
    /// EPEL 8 (RHEL 8 / CentOS Stream 8)
    Epel8,
}

impl DnfRepo {
    /// Get the release name used in mdapi URLs.
    fn release(&self) -> &'static str {
        match self {
            Self::Fedora41 => "f41",
            Self::Fedora40 => "f40",
            Self::Fedora39 => "f39",
            Self::Rawhide => "rawhide",
            Self::Epel9 => "epel9",
            Self::Epel8 => "epel8",
        }
    }

    /// Get the repository name for tagging.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Fedora41 => "fedora-41",
            Self::Fedora40 => "fedora-40",
            Self::Fedora39 => "fedora-39",
            Self::Rawhide => "rawhide",
            Self::Epel9 => "epel-9",
            Self::Epel8 => "epel-8",
        }
    }

    /// All available repositories.
    pub fn all() -> &'static [DnfRepo] {
        &[
            Self::Fedora41,
            Self::Fedora40,
            Self::Fedora39,
            Self::Rawhide,
            Self::Epel9,
            Self::Epel8,
        ]
    }

    /// Current stable Fedora releases.
    pub fn stable() -> &'static [DnfRepo] {
        &[Self::Fedora41, Self::Fedora40]
    }

    /// Rawhide only.
    pub fn rawhide() -> &'static [DnfRepo] {
        &[Self::Rawhide]
    }

    /// EPEL repositories only.
    pub fn epel() -> &'static [DnfRepo] {
        &[Self::Epel9, Self::Epel8]
    }

    /// All Fedora releases (no EPEL).
    pub fn fedora() -> &'static [DnfRepo] {
        &[
            Self::Fedora41,
            Self::Fedora40,
            Self::Fedora39,
            Self::Rawhide,
        ]
    }
}

/// DNF package index fetcher with configurable repositories.
pub struct Dnf {
    repos: Vec<DnfRepo>,
}

impl Dnf {
    /// Fedora packages API (fcomm_connector).
    const FEDORA_API: &'static str = "https://apps.fedoraproject.org/packages/fcomm_connector";

    /// mdapi for package metadata.
    const MDAPI: &'static str = "https://mdapi.fedoraproject.org";

    /// Create a fetcher with all repositories.
    pub fn all() -> Self {
        Self {
            repos: DnfRepo::all().to_vec(),
        }
    }

    /// Create a fetcher with stable Fedora releases.
    pub fn stable() -> Self {
        Self {
            repos: DnfRepo::stable().to_vec(),
        }
    }

    /// Create a fetcher with rawhide only.
    pub fn rawhide() -> Self {
        Self {
            repos: DnfRepo::rawhide().to_vec(),
        }
    }

    /// Create a fetcher with EPEL repositories only.
    pub fn epel() -> Self {
        Self {
            repos: DnfRepo::epel().to_vec(),
        }
    }

    /// Create a fetcher with all Fedora releases (no EPEL).
    pub fn fedora() -> Self {
        Self {
            repos: DnfRepo::fedora().to_vec(),
        }
    }

    /// Create a fetcher with custom repository selection.
    pub fn with_repos(repos: &[DnfRepo]) -> Self {
        Self {
            repos: repos.to_vec(),
        }
    }

    /// Fetch package info from a specific release.
    fn fetch_from_release(
        release: &str,
        name: &str,
        repo: DnfRepo,
    ) -> Result<PackageMeta, IndexError> {
        let url = format!("{}/{}/pkg/{}", Self::MDAPI, release, name);
        let response: serde_json::Value = ureq::get(&url)
            .set("Accept", "application/json")
            .call()?
            .into_json()?;

        if response.get("output").is_some() && response["output"].as_str() == Some("notok") {
            return Err(IndexError::NotFound(name.to_string()));
        }

        let mut extra = HashMap::new();

        // Extract dependencies from requires
        if let Some(requires) = response["requires"].as_array() {
            let deps: Vec<serde_json::Value> = requires
                .iter()
                .filter_map(|r| r["name"].as_str())
                .filter(|name| {
                    // Filter out internal deps
                    !name.contains("()") && !name.starts_with("rtld") && !name.contains(".so")
                })
                .map(|name| serde_json::Value::String(name.to_string()))
                .collect();
            if !deps.is_empty() {
                extra.insert("depends".to_string(), serde_json::Value::Array(deps));
            }
        }

        // Extract provides (virtual packages and shared libraries)
        if let Some(provides) = response["provides"].as_array() {
            let parsed_provides: Vec<serde_json::Value> = provides
                .iter()
                .filter_map(|p| p["name"].as_str())
                .map(|name| serde_json::Value::String(name.to_string()))
                .collect();
            if !parsed_provides.is_empty() {
                extra.insert(
                    "provides".to_string(),
                    serde_json::Value::Array(parsed_provides),
                );
            }
        }

        // Extract arch
        if let Some(arch) = response["arch"].as_str() {
            extra.insert(
                "arch".to_string(),
                serde_json::Value::String(arch.to_string()),
            );
        }

        // Tag with source repo
        extra.insert(
            "source_repo".to_string(),
            serde_json::Value::String(repo.name().to_string()),
        );

        Ok(PackageMeta {
            name: response["name"].as_str().unwrap_or(name).to_string(),
            version: format!(
                "{}-{}",
                response["version"].as_str().unwrap_or("unknown"),
                response["release"].as_str().unwrap_or("1")
            ),
            description: response["summary"].as_str().map(String::from),
            homepage: response["url"].as_str().map(String::from),
            repository: response["url"]
                .as_str()
                .filter(|u| u.contains("github.com") || u.contains("gitlab.com"))
                .map(String::from),
            license: response["license"].as_str().map(String::from),
            binaries: Vec::new(),
            keywords: Vec::new(),
            maintainers: Vec::new(),
            published: None,
            downloads: None,
            archive_url: None,
            checksum: None,
            extra,
        })
    }

    /// Load source package list from a release.
    fn load_repo(repo: DnfRepo) -> Result<Vec<PackageMeta>, IndexError> {
        let release = repo.release();
        let url = format!("{}/{}/srcpkgs", Self::MDAPI, release);

        let (data, _was_cached) =
            cache::fetch_with_cache("dnf", &format!("srcpkgs-{}", repo.name()), &url, CACHE_TTL)
                .map_err(IndexError::Network)?;

        let names: Vec<String> =
            serde_json::from_slice(&data).map_err(|e| IndexError::Parse(e.to_string()))?;

        // For fetch_all, we just return basic package info
        // Getting full details for each would be too slow
        Ok(names
            .into_iter()
            .map(|name| {
                let mut extra = HashMap::new();
                extra.insert(
                    "source_repo".to_string(),
                    serde_json::Value::String(repo.name().to_string()),
                );

                PackageMeta {
                    name,
                    version: String::new(), // Would need individual API call
                    description: None,
                    homepage: None,
                    repository: None,
                    license: None,
                    binaries: Vec::new(),
                    keywords: Vec::new(),
                    maintainers: Vec::new(),
                    published: None,
                    downloads: None,
                    archive_url: None,
                    checksum: None,
                    extra,
                }
            })
            .collect())
    }

    /// Load packages from all configured repositories in parallel.
    fn load_packages(&self) -> Result<Vec<PackageMeta>, IndexError> {
        let results: Vec<_> = self
            .repos
            .par_iter()
            .map(|&repo| Self::load_repo(repo))
            .collect();

        let mut packages = Vec::new();
        for result in results {
            match result {
                Ok(pkgs) => packages.extend(pkgs),
                Err(e) => {
                    tracing::warn!("failed to load DNF repo: {}", e);
                }
            }
        }

        Ok(packages)
    }
}

impl PackageIndex for Dnf {
    fn ecosystem(&self) -> &'static str {
        "dnf"
    }

    fn display_name(&self) -> &'static str {
        "DNF (Fedora/RHEL)"
    }

    fn fetch(&self, name: &str) -> Result<PackageMeta, IndexError> {
        // Try each configured repo until we find the package
        for repo in &self.repos {
            match Self::fetch_from_release(repo.release(), name, *repo) {
                Ok(pkg) => return Ok(pkg),
                Err(IndexError::NotFound(_)) => continue,
                Err(e) => return Err(e),
            }
        }

        Err(IndexError::NotFound(name.to_string()))
    }

    fn fetch_versions(&self, name: &str) -> Result<Vec<VersionMeta>, IndexError> {
        // Query all configured releases in parallel
        let results: Vec<_> = self
            .repos
            .par_iter()
            .filter_map(|repo| Self::fetch_from_release(repo.release(), name, *repo).ok())
            .collect();

        if results.is_empty() {
            return Err(IndexError::NotFound(name.to_string()));
        }

        Ok(results
            .into_iter()
            .map(|pkg| VersionMeta {
                version: pkg.version,
                released: None,
                yanked: false,
            })
            .collect())
    }

    fn supports_fetch_all(&self) -> bool {
        true
    }

    fn fetch_all(&self) -> Result<Vec<PackageMeta>, IndexError> {
        self.load_packages()
    }

    fn search(&self, query: &str) -> Result<Vec<PackageMeta>, IndexError> {
        // Use the fcomm_connector API with JSON in URL
        let search_json = serde_json::json!({
            "filters": {"search": query},
            "rows_per_page": 50,
            "start_row": 0
        });

        let url = format!(
            "{}/xapian/query/search_packages/{}",
            Self::FEDORA_API,
            urlencoding::encode(&search_json.to_string())
        );

        let response: serde_json::Value = ureq::get(&url)
            .set("Accept", "application/json")
            .call()?
            .into_json()?;

        let rows = response["rows"]
            .as_array()
            .ok_or_else(|| IndexError::Parse("missing rows".into()))?;

        Ok(rows
            .iter()
            .filter_map(|pkg| {
                Some(PackageMeta {
                    name: pkg["name"].as_str()?.to_string(),
                    version: pkg["version"].as_str().unwrap_or("unknown").to_string(),
                    description: pkg["summary"].as_str().map(String::from),
                    homepage: pkg["upstream_url"].as_str().map(String::from),
                    repository: None,
                    license: pkg["license"].as_str().map(String::from),
                    binaries: Vec::new(),
                    keywords: Vec::new(),
                    maintainers: Vec::new(),
                    published: None,
                    downloads: None,
                    archive_url: None,
                    checksum: None,
                    extra: Default::default(),
                })
            })
            .collect())
    }
}

mod urlencoding {
    pub fn encode(s: &str) -> String {
        let mut result = String::with_capacity(s.len() * 3);
        for c in s.chars() {
            match c {
                'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' | '~' => result.push(c),
                _ => {
                    for b in c.to_string().bytes() {
                        result.push_str(&format!("%{:02X}", b));
                    }
                }
            }
        }
        result
    }
}