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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

//! Copyright detection orchestrator.
//!
//! Runs the full detection pipeline: text → numbered lines → candidate groups
//! → tokens → parse tree → walk tree → refine → filter junk → detections.
//!
//! The grammar currently builds lower-level structures (Name, Company,
//! YrRange, etc.) but does not yet produce top-level COPYRIGHT/AUTHOR tree
//! nodes. This detector handles both cases:
//! - If the grammar produces COPYRIGHT/AUTHOR nodes, use them directly.
//! - Otherwise, scan the flat node sequence for COPY/AUTH tokens and
//!   collect spans heuristically.

use std::borrow::Cow;
use std::sync::LazyLock;
use std::time::{Duration, Instant};

use regex::Regex;

use super::candidates::collect_candidate_lines;
use super::detector_input_normalization::{
    maybe_expand_copyrighted_by_href_urls, normalize_split_angle_bracket_urls,
};
use super::lexer::get_tokens;
use super::line_tracking::{LineNumberIndex, PreparedLineCache};
use super::parser::{parse, parse_with_deadline};
#[cfg(test)]
use super::refiner::refine_copyright;
use super::types::{
    AuthorDetection, CopyrightDetection, HolderDetection, ParseNode, PosTag, TreeLabel,
};

const NON_COPYRIGHT_LABELS: &[TreeLabel] = &[];
const NON_HOLDER_LABELS: &[TreeLabel] = &[TreeLabel::YrRange, TreeLabel::YrAnd];
const NON_HOLDER_LABELS_MINI: &[TreeLabel] = &[TreeLabel::YrRange, TreeLabel::YrAnd];

const NON_HOLDER_POS_TAGS: &[PosTag] = &[
    PosTag::Copy,
    PosTag::Yr,
    PosTag::YrPlus,
    PosTag::BareYr,
    PosTag::Email,
    PosTag::Url,
    PosTag::Holder,
    PosTag::Is,
    PosTag::Held,
];

const NON_HOLDER_POS_TAGS_MINI: &[PosTag] = &[
    PosTag::Copy,
    PosTag::Yr,
    PosTag::YrPlus,
    PosTag::BareYr,
    PosTag::Is,
    PosTag::Held,
];

const NON_AUTHOR_POS_TAGS: &[PosTag] = &[
    PosTag::Copy,
    PosTag::Yr,
    PosTag::YrPlus,
    PosTag::BareYr,
    PosTag::Auth,
    PosTag::Auth2,
    PosTag::Auths,
    PosTag::AuthDot,
    PosTag::Contributors,
    PosTag::Commit,
    PosTag::SpdxContrib,
    PosTag::Holder,
    PosTag::Is,
    PosTag::Held,
];

const NON_COPYRIGHT_POS_TAGS: &[PosTag] = &[];

static NOT_COPYRIGHTED_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?i)\bnot\s+copyrighted\b").unwrap());

#[derive(Default)]
struct TreeAnalysis {
    has_copy_like_token: bool,
    has_authorish_boundary_token: bool,
    has_year_token: bool,
    single_line: Option<usize>,
    is_single_line_group: bool,
}

fn analyze_tree(nodes: &[ParseNode]) -> TreeAnalysis {
    fn visit(node: &ParseNode, analysis: &mut TreeAnalysis) {
        match node {
            ParseNode::Leaf(token) => {
                analysis.has_copy_like_token |=
                    matches!(token.tag, PosTag::Copy | PosTag::SpdxContrib);
                analysis.has_authorish_boundary_token |= matches!(
                    token.tag,
                    PosTag::Auths | PosTag::AuthDot | PosTag::Contributors | PosTag::Commit
                );
                analysis.has_year_token |= token_utils::is_year_like_token(token);

                let line = token.start_line.get();
                match analysis.single_line {
                    Some(existing) if existing != line => analysis.is_single_line_group = false,
                    None => analysis.single_line = Some(line),
                    _ => {}
                }
            }
            ParseNode::Tree { children, .. } => {
                for child in children {
                    visit(child, analysis);
                }
            }
        }
    }

    let mut analysis = TreeAnalysis {
        is_single_line_group: true,
        ..TreeAnalysis::default()
    };

    for node in nodes {
        visit(node, &mut analysis);
    }

    analysis
}

/// Returns a tuple of (copyrights, holders, authors).
pub fn detect_copyrights_from_text(
    content: &str,
) -> (
    Vec<CopyrightDetection>,
    Vec<HolderDetection>,
    Vec<AuthorDetection>,
) {
    detect_copyrights_from_text_with_deadline(content, None)
}

pub fn detect_copyrights_from_text_with_deadline(
    content: &str,
    max_runtime: Option<Duration>,
) -> (
    Vec<CopyrightDetection>,
    Vec<HolderDetection>,
    Vec<AuthorDetection>,
) {
    let mut copyrights = Vec::new();
    let mut holders = Vec::new();
    let mut authors = Vec::new();
    let deadline = max_runtime.and_then(|d| Instant::now().checked_add(d));

    if content.is_empty() {
        return (copyrights, holders, authors);
    }

    let normalized = normalize_split_angle_bracket_urls(content);
    let expanded = maybe_expand_copyrighted_by_href_urls(normalized.as_ref());
    let did_expand_href = matches!(expanded, Cow::Owned(_));
    let content = expanded.as_ref();
    let line_number_index = LineNumberIndex::new(content);

    let allow_not_copyrighted_prefix = NOT_COPYRIGHTED_RE.find_iter(content).count() == 1;

    let raw_lines: Vec<&str> = content.lines().collect();
    let prepared_cache = PreparedLineCache::new(&raw_lines);

    if raw_lines.is_empty() {
        return (copyrights, holders, authors);
    }

    let groups =
        collect_candidate_lines(raw_lines.iter().enumerate().map(|(i, line)| (i + 1, *line)));

    let mut seen = seen_text::SeenTextSets::from_existing(&copyrights, &holders, &authors);

    for group in &groups {
        if deadline_exceeded(deadline) {
            break;
        }

        if group.is_empty() {
            continue;
        }

        let tokens = get_tokens(group);
        if tokens.is_empty() {
            continue;
        }

        let tree = if deadline.is_some() {
            parse_with_deadline(tokens, deadline)
        } else {
            parse(tokens)
        };

        let has_top_level_nodes = tree.iter().any(|n| {
            matches!(
                n.label(),
                Some(TreeLabel::Copyright) | Some(TreeLabel::Copyright2) | Some(TreeLabel::Author)
            )
        });
        let analysis = analyze_tree(&tree);

        if has_top_level_nodes {
            let (new_c, new_h, new_a) =
                extract_from_tree_nodes(&tree, allow_not_copyrighted_prefix);
            seen.register_copyrights(&new_c);
            seen.register_holders(&new_h);
            seen.register_authors(&new_a);
            let copyrights_before = copyrights.len();
            copyrights.extend(new_c);
            holders.extend(new_h);
            authors.extend(new_a);

            if let Some(det) = extract_original_author_additional_contributors(&tree)
                && seen.authors.insert(det.author.clone())
            {
                authors.push(det);
            }

            if copyrights.len() == copyrights_before
                && analysis.has_copy_like_token
                && analysis.has_authorish_boundary_token
                && analysis.is_single_line_group
            {
                let (new_c, new_h) = extract_bare_copyrights(&tree);
                seen.register_copyrights(&new_c);
                seen.register_holders(&new_h);
                copyrights.extend(new_c);
                holders.extend(new_h);
                let (new_c, new_h) =
                    extract_copyrights_from_spans(&tree, allow_not_copyrighted_prefix);
                seen.register_copyrights(&new_c);
                seen.register_holders(&new_h);
                copyrights.extend(new_c);
                holders.extend(new_h);
            }

            if copyrights.len() == copyrights_before
                && analysis.has_copy_like_token
                && analysis.has_year_token
            {
                let (new_c, new_h) =
                    extract_copyrights_from_spans(&tree, allow_not_copyrighted_prefix);
                seen.register_copyrights(&new_c);
                seen.register_holders(&new_h);
                copyrights.extend(new_c);
                holders.extend(new_h);
            }
        } else {
            let (new_c, new_h) = extract_bare_copyrights(&tree);
            seen.register_copyrights(&new_c);
            seen.register_holders(&new_h);
            copyrights.extend(new_c);
            holders.extend(new_h);
            let (new_c, new_h, new_a) = extract_from_spans(&tree, allow_not_copyrighted_prefix);
            seen.register_copyrights(&new_c);
            seen.register_holders(&new_h);
            seen.register_authors(&new_a);
            copyrights.extend(new_c);
            holders.extend(new_h);
            authors.extend(new_a);
            let mut new_a = extract_orphaned_by_authors(&tree);
            seen.dedup_new_authors(&mut new_a, 0);
            authors.extend(new_a);

            if let Some(det) = extract_original_author_additional_contributors(&tree)
                && seen.authors.insert(det.author.clone())
            {
                authors.push(det);
            }
        }

        // Run after each group is processed so it can fix authors detected
        // through any extraction path.
        fix_truncated_contributors_authors(&tree, &mut authors);
        seen.rebuild_authors_from(&authors);
        let (mut new_c, mut new_h) = extract_holder_is_name(&tree);
        seen.dedup_new_copyrights(&mut new_c, 0);
        seen.dedup_new_holders(&mut new_h, 0);
        copyrights.extend(new_c);
        holders.extend(new_h);
        apply_written_by_for_markers(group, &mut copyrights, &mut holders);
        extend_multiline_copyright_c_year_holder_continuations(
            group,
            &mut copyrights,
            &mut holders,
        );
        extend_multiline_copyright_c_no_year_names(group, &mut copyrights[..], &mut holders[..]);
        extend_authors_see_url_copyrights(group, &mut copyrights[..], &mut holders[..]);
        extend_leading_dash_suffixes(group, &mut copyrights[..], &mut holders[..]);
        extend_dash_obfuscated_email_suffixes(&raw_lines, group, &mut copyrights[..], &holders[..]);
        extend_trailing_copy_year_suffixes(&raw_lines, group, &mut copyrights[..]);
        extend_w3c_registered_org_list_suffixes(group, &mut copyrights[..], &mut holders[..]);
        extend_software_in_the_public_interest_holder(group, &mut copyrights, &mut holders);
    }

    let mut fallback = fallback_year_only_copyrights(&groups);
    seen.dedup_new_copyrights(&mut fallback, 0);
    fallback.retain(|det| {
        !copyrights.iter().any(|c| {
            c.copyright
                .to_ascii_lowercase()
                .contains(&det.copyright.to_ascii_lowercase())
        })
    });
    copyrights.extend(fallback);

    if deadline_exceeded(deadline) {
        refine_final_copyrights(&mut copyrights);
        postprocess_transforms::refine_final_authors(&mut authors);
        dedupe_exact_span_copyrights(&mut copyrights);
        dedupe_exact_span_holders(&mut holders);
        dedupe_exact_span_authors(&mut authors);
        return (copyrights, holders, authors);
    }

    let prepared_lines = prepared_cache.materialize();

    phases::run_phase_primary_extractions(
        content,
        &groups,
        &line_number_index,
        &prepared_lines,
        &mut copyrights,
        &mut holders,
        &mut seen,
    );

    phases::run_phase_postprocess(
        content,
        &raw_lines,
        &prepared_lines,
        did_expand_href,
        &mut copyrights,
        &mut holders,
        &mut authors,
        &mut seen,
    );

    refine_final_copyrights(&mut copyrights);
    postprocess_transforms::refine_final_authors(&mut authors);
    postprocess_transforms::drop_trademark_boilerplate_multiline_extensions(
        &raw_lines,
        &mut copyrights,
        &mut holders,
    );
    postprocess_transforms::drop_same_span_license_tail_variants(&mut copyrights, &mut holders);
    postprocess_transforms::drop_shadowed_bare_c_from_year_fragments(&mut copyrights, &mut holders);
    drop_path_fragment_holders_from_bare_c_code_lines(&raw_lines, &copyrights, &mut holders);
    drop_scan_only_holders_from_copyright_scan_lines(&raw_lines, &copyrights, &mut holders);

    for group in &groups {
        extend_dash_obfuscated_email_suffixes(&raw_lines, group, &mut copyrights[..], &holders[..]);
    }
    restore_linux_foundation_copyrights_from_raw_lines(&raw_lines, &mut copyrights);
    copyrights.retain(|c| {
        let trimmed = c.copyright.trim();
        !trimmed.eq_ignore_ascii_case("copyright holders")
            && !trimmed.eq_ignore_ascii_case("the above copyright holders")
    });

    holders.extend(add_missing_holders_for_bare_c_name_year_suffixes(
        &copyrights,
    ));
    holders
        .extend(postprocess_transforms::add_missing_holders_for_iso_date_copyrights(&copyrights));

    dedupe_exact_span_holders(&mut holders);

    dedupe_exact_span_copyrights(&mut copyrights);
    dedupe_exact_span_holders(&mut holders);
    dedupe_exact_span_authors(&mut authors);

    (copyrights, holders, authors)
}

mod author_heuristics;
mod pattern_extract;
mod phases;
mod postprocess_transforms;
mod seen_text;
mod token_utils;
mod tree_walk;

use pattern_extract::{
    extend_software_in_the_public_interest_holder, fallback_year_only_copyrights,
};
use postprocess_transforms::{
    add_missing_holders_for_bare_c_name_year_suffixes, deadline_exceeded,
    dedupe_exact_span_authors, dedupe_exact_span_copyrights, dedupe_exact_span_holders,
    extend_authors_see_url_copyrights, extend_dash_obfuscated_email_suffixes,
    extend_leading_dash_suffixes, extend_multiline_copyright_c_no_year_names,
    extend_multiline_copyright_c_year_holder_continuations, extend_trailing_copy_year_suffixes,
    extend_w3c_registered_org_list_suffixes, refine_final_copyrights,
    restore_linux_foundation_copyrights_from_raw_lines,
};
pub(super) use token_utils::collect_all_leaves;
use token_utils::{
    apply_written_by_for_markers, drop_path_fragment_holders_from_bare_c_code_lines,
    drop_scan_only_holders_from_copyright_scan_lines,
    extract_original_author_additional_contributors,
};
use tree_walk::{
    extract_bare_copyrights, extract_copyrights_from_spans, extract_from_spans,
    extract_from_tree_nodes, extract_holder_is_name, extract_orphaned_by_authors,
    fix_truncated_contributors_authors,
};

#[cfg(test)]
mod tests;

#[cfg(test)]
#[path = "tests_false_positives.rs"]
mod tests_false_positives;

#[cfg(test)]
#[path = "tests_structured_metadata.rs"]
mod tests_structured_metadata;

#[cfg(test)]
#[path = "tests_author_pipeline.rs"]
mod tests_author_pipeline;

#[cfg(test)]
#[path = "tests_multiline_repairs.rs"]
mod tests_multiline_repairs;

#[cfg(test)]
#[path = "tests_parser_internals.rs"]
mod tests_parser_internals;

#[cfg(test)]
#[path = "tests_copyright_holder_pipeline.rs"]
mod tests_copyright_holder_pipeline;