provenant-cli 0.0.33

Rust-based ScanCode-compatible scanner for licenses, package metadata, SBOMs, and provenance data.
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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

//! Parser for RPM .spec files.
//!
//! Extracts package metadata from RPM specfiles, which define how RPM packages
//! are built. This is a beyond-parity implementation - the Python version is
//! a complete stub with "TODO: implement me!!" comments.
//!
//! # Supported Formats
//! - *.spec (RPM specfiles)
//!
//! # Key Features
//! - Preamble tag extraction (Name, Version, Release, Summary, License, etc.)
//! - Dependency extraction (BuildRequires, Requires, Provides)
//! - %description section parsing
//! - Basic macro expansion (%{name}, %{version}, %{release})
//! - %define and %global macro definitions
//! - Conditional macro handling (%{?dist})
//! - Multi-line dependency lists (comma-separated)
//! - Scoped Requires (Requires(post), Requires(preun), etc.)
//!
//! # Implementation Notes
//! - Parses only the preamble (before %prep, %build, etc. sections)
//! - Tags are case-insensitive per RPM spec format
//! - Simple macro expansion for common patterns
//! - BuildRequires dependencies have is_runtime=false, scope="build"
//! - Runtime Requires dependencies have is_runtime=true, scope="runtime"
//! - datasource_id is "rpm_specfile"

use std::collections::HashMap;
use std::path::Path;
use std::sync::LazyLock;

use crate::parser_warn as warn;
use packageurl::PackageUrl;
use regex::Regex;

use crate::models::{DatasourceId, Dependency, PackageData, PackageType, Party};
use crate::parsers::utils::{
    MAX_ITERATION_COUNT, read_file_to_string, split_name_email, truncate_field,
};

use super::PackageParser;

static RE_CONDITIONAL_MACRO: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"%\{\?[^}]+\}").expect("valid regex: %{?...} pattern is a compile-time constant")
});

const PACKAGE_TYPE: PackageType = PackageType::Rpm;

/// Parser for RPM specfiles
pub struct RpmSpecfileParser;

impl PackageParser for RpmSpecfileParser {
    const PACKAGE_TYPE: PackageType = PACKAGE_TYPE;

    fn is_match(path: &Path) -> bool {
        path.extension()
            .and_then(|e| e.to_str())
            .is_some_and(|ext| ext.eq_ignore_ascii_case("spec"))
    }

    fn extract_packages(path: &Path) -> Vec<PackageData> {
        let content = match read_file_to_string(path, None) {
            Ok(c) => c,
            Err(e) => {
                warn!("Failed to read RPM specfile {:?}: {}", path, e);
                return vec![PackageData {
                    package_type: Some(PACKAGE_TYPE),
                    datasource_id: Some(DatasourceId::RpmSpecfile),
                    ..Default::default()
                }];
            }
        };

        vec![parse_specfile(&content)]
    }
}

fn parse_specfile(content: &str) -> PackageData {
    let mut tags: HashMap<String, String> = HashMap::new();
    let mut macros: HashMap<String, String> = HashMap::new();
    let mut build_requires: Vec<String> = Vec::new();
    let mut requires: Vec<(String, Option<String>)> = Vec::new(); // (requirement, scope)
    let mut provides: Vec<String> = Vec::new();
    let mut description: Option<String> = None;

    let lines: Vec<&str> = content.lines().collect();
    let mut i = 0;
    let mut iterations: usize = 0;

    while i < lines.len() {
        iterations += 1;
        if iterations > MAX_ITERATION_COUNT {
            warn!(
                "RPM specfile preamble iteration limit ({}) exceeded",
                MAX_ITERATION_COUNT
            );
            break;
        }
        let line = lines[i].trim();

        // Stop at first section marker (%, but not %define/%global)
        if line.starts_with('%') && !line.starts_with("%define") && !line.starts_with("%global") {
            if is_conditional_preamble_directive(line) {
                i += 1;
                continue;
            }
            break;
        }

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            i += 1;
            continue;
        }

        // Parse %define and %global macros
        if let Some(stripped) = line
            .strip_prefix("%define")
            .or(line.strip_prefix("%global"))
        {
            let parts: Vec<&str> = stripped.trim().splitn(2, char::is_whitespace).collect();
            if parts.len() == 2 {
                macros.insert(
                    parts[0].to_string(),
                    truncate_field(parts[1].trim().to_string()),
                );
            }
            i += 1;
            continue;
        }

        // Parse Tag: Value lines
        if let Some(colon_pos) = line.find(':') {
            let tag = line[..colon_pos].trim().to_lowercase();
            let value = line[colon_pos + 1..].trim().to_string();

            match tag.as_str() {
                "buildrequires" => {
                    for dep in value.split(',').take(MAX_ITERATION_COUNT) {
                        let dep = dep.trim();
                        if !dep.is_empty() {
                            build_requires.push(dep.to_string());
                        }
                    }
                }
                t if t.starts_with("requires") => {
                    // Parse Requires, Requires(post), Requires(preun), etc.
                    let scope = if let Some(start) = t.find('(') {
                        if let Some(end) = t.find(')') {
                            Some(t[start + 1..end].to_string())
                        } else {
                            Some("runtime".to_string())
                        }
                    } else {
                        Some("runtime".to_string())
                    };

                    for dep in value.split(',').take(MAX_ITERATION_COUNT) {
                        let dep = dep.trim();
                        if !dep.is_empty() {
                            requires.push((dep.to_string(), scope.clone()));
                        }
                    }
                }
                "provides" => {
                    for prov in value.split(',').take(MAX_ITERATION_COUNT) {
                        let prov = prov.trim();
                        if !prov.is_empty() {
                            provides.push(prov.to_string());
                        }
                    }
                }
                _ => {
                    tags.insert(tag, value);
                }
            }
        }

        i += 1;
    }

    // Now parse %description section if present
    let mut desc_iterations: usize = 0;
    while i < lines.len() {
        desc_iterations += 1;
        if desc_iterations > MAX_ITERATION_COUNT {
            warn!(
                "RPM specfile description search iteration limit ({}) exceeded",
                MAX_ITERATION_COUNT
            );
            break;
        }
        let line = lines[i].trim();

        if line.starts_with("%description") {
            i += 1;
            let mut desc_lines = Vec::new();

            while i < lines.len() {
                desc_iterations += 1;
                if desc_iterations > MAX_ITERATION_COUNT {
                    warn!(
                        "RPM specfile description iteration limit ({}) exceeded",
                        MAX_ITERATION_COUNT
                    );
                    break;
                }
                let desc_line = lines[i];
                let trimmed = desc_line.trim();

                // Stop at next section
                if trimmed.starts_with('%') {
                    break;
                }

                // Don't include empty lines at start
                if !desc_lines.is_empty() || !trimmed.is_empty() {
                    desc_lines.push(desc_line);
                }

                i += 1;
            }

            // Trim trailing empty lines
            while desc_lines.last().is_some_and(|l| l.trim().is_empty()) {
                desc_lines.pop();
            }

            if !desc_lines.is_empty() {
                description = Some(desc_lines.join("\n"));
            }

            break;
        }

        i += 1;
    }

    // Extract basic metadata from tags
    let name = tags.get("name").cloned();
    let version = tags.get("version").cloned();
    let release = tags.get("release").cloned();

    // Store name and version in macros for expansion
    if let Some(ref n) = name {
        macros.insert("name".to_string(), n.clone());
    }
    if let Some(ref v) = version {
        macros.insert("version".to_string(), v.clone());
    }
    if let Some(ref r) = release {
        macros.insert("release".to_string(), r.clone());
    }

    // Expand macros in all tag values
    let mut expanded_tags: HashMap<String, String> = HashMap::new();
    for (tag, value) in tags.iter() {
        expanded_tags.insert(tag.clone(), truncate_field(expand_macros(value, &macros)));
    }

    // Get expanded values
    let name = expanded_tags.get("name").cloned();
    let version = expanded_tags.get("version").cloned();
    let release = expanded_tags.get("release").cloned();
    let summary = expanded_tags.get("summary").cloned();
    let license = expanded_tags.get("license").cloned();
    let url = expanded_tags.get("url").cloned();
    let group = expanded_tags.get("group").cloned();
    let epoch = expanded_tags.get("epoch").cloned();
    let packager = expanded_tags.get("packager").cloned();

    let download_url = expanded_tags
        .get("source")
        .or_else(|| expanded_tags.get("source0"))
        .cloned()
        .map(truncate_field);

    // Create parties
    let mut parties = Vec::new();
    if let Some(pkg) = packager {
        let (name_opt, email_opt) = split_name_email(&pkg);
        parties.push(Party {
            r#type: None,
            role: Some("packager".to_string()),
            name: name_opt,
            email: email_opt,
            url: None,
            organization: None,
            organization_url: None,
            timezone: None,
        });
    }

    // Create dependencies
    let mut dependencies = Vec::new();

    for dep_str in build_requires.into_iter().take(MAX_ITERATION_COUNT) {
        let dep_str = truncate_field(expand_macros(&dep_str, &macros));
        let dep_name = extract_dep_name(&dep_str);
        let purl = build_rpm_purl(&dep_name, None).map(truncate_field);

        dependencies.push(Dependency {
            purl,
            extracted_requirement: Some(dep_str),
            scope: Some("build".to_string()),
            is_runtime: Some(false),
            is_optional: Some(false),
            is_direct: Some(true),
            is_pinned: None,
            resolved_package: None,
            extra_data: None,
        });
    }

    for (dep_str, scope) in requires.into_iter().take(MAX_ITERATION_COUNT) {
        let dep_str = truncate_field(expand_macros(&dep_str, &macros));
        let dep_name = extract_dep_name(&dep_str);
        let purl = build_rpm_purl(&dep_name, None).map(truncate_field);

        dependencies.push(Dependency {
            purl,
            extracted_requirement: Some(dep_str),
            scope,
            is_runtime: Some(true),
            is_optional: Some(false),
            is_direct: Some(true),
            is_pinned: None,
            resolved_package: None,
            extra_data: None,
        });
    }

    // Build PURL
    let purl = name
        .as_ref()
        .and_then(|n| build_rpm_purl(n, version.as_deref()))
        .map(truncate_field);

    // Build extra_data for non-standard fields
    let mut extra_data = HashMap::new();
    if let Some(r) = release {
        extra_data.insert("release".to_string(), serde_json::Value::String(r));
    }
    if let Some(e) = epoch {
        extra_data.insert("epoch".to_string(), serde_json::Value::String(e));
    }
    if let Some(g) = group {
        extra_data.insert("group".to_string(), serde_json::Value::String(g));
    }
    if !provides.is_empty() {
        let provides_json: Vec<serde_json::Value> = provides
            .into_iter()
            .take(MAX_ITERATION_COUNT)
            .map(|prov| serde_json::Value::String(truncate_field(expand_macros(&prov, &macros))))
            .collect();
        extra_data.insert(
            "provides".to_string(),
            serde_json::Value::Array(provides_json),
        );
    }

    let extra_data_opt = if extra_data.is_empty() {
        None
    } else {
        Some(extra_data)
    };

    // Use %description if available, otherwise use Summary
    let description_text = description.map(truncate_field).or(summary);

    PackageData {
        datasource_id: Some(DatasourceId::RpmSpecfile),
        package_type: Some(PACKAGE_TYPE),
        namespace: None, // RPM namespace is optional
        name,
        version,
        description: description_text,
        homepage_url: url,
        download_url,
        extracted_license_statement: license,
        parties,
        dependencies,
        purl,
        extra_data: extra_data_opt,
        ..Default::default()
    }
}

fn is_conditional_preamble_directive(line: &str) -> bool {
    [
        "%if", "%ifarch", "%ifnarch", "%ifos", "%ifnos", "%elif", "%else", "%endif",
    ]
    .iter()
    .any(|directive| line.starts_with(directive))
}

/// Expands simple macros in a string (%{name}, %{version}, %{release}, %{?dist})
fn expand_macros(s: &str, macros: &HashMap<String, String>) -> String {
    let mut result = s.to_string();

    result = RE_CONDITIONAL_MACRO.replace_all(&result, "").to_string();

    // Expand simple macros %{macro}
    for (key, value) in macros {
        let pattern = format!("%{{{}}}", key);
        result = result.replace(&pattern, value);
    }

    result = RE_CONDITIONAL_MACRO.replace_all(&result, "").to_string();

    result
}

/// Extracts the package name from a dependency string (removes version constraints)
fn extract_dep_name(dep: &str) -> String {
    let parts: Vec<&str> = dep.split(&['>', '<', '='][..]).map(|s| s.trim()).collect();

    truncate_field(parts[0].to_string())
}

/// Builds a package URL for RPM packages
fn build_rpm_purl(name: &str, version: Option<&str>) -> Option<String> {
    let mut purl = PackageUrl::new(PACKAGE_TYPE.as_str(), name).ok()?;

    if let Some(ver) = version {
        purl.with_version(ver).ok()?;
    }

    Some(purl.to_string())
}

crate::register_parser!(
    "RPM specfile",
    &["**/*.spec"],
    "rpm",
    "",
    Some("https://rpm-software-management.github.io/rpm/manual/spec.html"),
);