provenant-cli 0.1.3

Independent Rust scanner for ScanCode-compatible workflows, 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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;
use std::io::{self, Write};

use tera::{Context, Tera};

use crate::models::FileType;
use crate::output_schema::Output;

use super::shared::{io_other, sorted_files};

pub(crate) fn write_html_report(output: &Output, writer: &mut dyn Write) -> io::Result<()> {
    let mut license_copyright_rows = Vec::<BTreeMap<String, String>>::new();
    let mut file_rows = Vec::<BTreeMap<String, String>>::new();
    let mut holder_rows = Vec::<BTreeMap<String, String>>::new();
    let mut author_rows = Vec::<BTreeMap<String, String>>::new();
    let mut email_rows = Vec::<BTreeMap<String, String>>::new();
    let mut url_rows = Vec::<BTreeMap<String, String>>::new();
    let mut package_rows = Vec::<BTreeMap<String, String>>::new();

    for file in sorted_files(&output.files) {
        let mut file_row = BTreeMap::new();
        file_row.insert("path".to_string(), file.path.clone());
        file_row.insert(
            "type".to_string(),
            match file.file_type {
                FileType::File => "file",
                FileType::Directory => "directory",
            }
            .to_string(),
        );
        file_row.insert("name".to_string(), file.name.clone());
        file_row.insert("extension".to_string(), file.extension.clone());
        file_row.insert("size".to_string(), file.size.to_string());
        file_row.insert(
            "sha1".to_string(),
            file.sha1.clone().unwrap_or_else(|| "None".to_string()),
        );
        file_row.insert(
            "md5".to_string(),
            file.md5.clone().unwrap_or_else(|| "None".to_string()),
        );
        file_row.insert("files_count".to_string(), String::new());
        file_row.insert("mime_type".to_string(), html_opt(file.mime_type.as_deref()));
        file_row.insert(
            "file_type".to_string(),
            html_opt(file.file_type_label.as_deref()),
        );
        file_row.insert(
            "programming_language".to_string(),
            html_opt(file.programming_language.as_deref()),
        );
        file_row.insert(
            "is_binary".to_string(),
            html_bool(file.is_binary.unwrap_or(false)).to_string(),
        );
        file_row.insert(
            "is_text".to_string(),
            html_bool(file.is_text.unwrap_or(false)).to_string(),
        );
        file_row.insert(
            "is_archive".to_string(),
            html_bool(file.is_archive.unwrap_or(false)).to_string(),
        );
        file_row.insert(
            "is_media".to_string(),
            html_bool(file.is_media.unwrap_or(false)).to_string(),
        );
        file_row.insert(
            "is_source".to_string(),
            html_bool(file.is_source.unwrap_or(false)).to_string(),
        );
        file_row.insert(
            "is_script".to_string(),
            html_bool(file.is_script.unwrap_or(false)).to_string(),
        );
        file_rows.push(file_row);

        for c in &file.copyrights {
            let mut row = BTreeMap::new();
            row.insert("path".to_string(), file.path.clone());
            row.insert("start".to_string(), c.start_line.to_string());
            row.insert("end".to_string(), c.end_line.to_string());
            row.insert("what".to_string(), "copyright".to_string());
            row.insert("value".to_string(), c.copyright.clone());
            license_copyright_rows.push(row);
        }
        for detection in &file.license_detections {
            for m in &detection.matches {
                let mut row = BTreeMap::new();
                row.insert("path".to_string(), file.path.clone());
                row.insert("start".to_string(), m.start_line.to_string());
                row.insert("end".to_string(), m.end_line.to_string());
                row.insert("what".to_string(), "license".to_string());
                row.insert("value".to_string(), detection.license_expression.clone());
                license_copyright_rows.push(row);
            }
        }

        for h in &file.holders {
            let mut row = BTreeMap::new();
            row.insert("path".to_string(), file.path.clone());
            row.insert("holder".to_string(), h.holder.clone());
            row.insert("start".to_string(), h.start_line.to_string());
            row.insert("end".to_string(), h.end_line.to_string());
            holder_rows.push(row);
        }
        for a in &file.authors {
            let mut row = BTreeMap::new();
            row.insert("path".to_string(), file.path.clone());
            row.insert("author".to_string(), a.author.clone());
            row.insert("start".to_string(), a.start_line.to_string());
            row.insert("end".to_string(), a.end_line.to_string());
            author_rows.push(row);
        }
        for e in &file.emails {
            let mut row = BTreeMap::new();
            row.insert("path".to_string(), file.path.clone());
            row.insert("email".to_string(), e.email.clone());
            row.insert("start".to_string(), e.start_line.to_string());
            row.insert("end".to_string(), e.end_line.to_string());
            email_rows.push(row);
        }
        for u in &file.urls {
            let mut row = BTreeMap::new();
            row.insert("path".to_string(), file.path.clone());
            row.insert("url".to_string(), u.url.clone());
            row.insert("start".to_string(), u.start_line.to_string());
            row.insert("end".to_string(), u.end_line.to_string());
            url_rows.push(row);
        }

        for package_data in &file.package_data {
            if package_data.package_type.is_none()
                && package_data.name.is_none()
                && package_data.version.is_none()
                && package_data.primary_language.is_none()
            {
                continue;
            }

            let mut row = BTreeMap::new();
            row.insert("path".to_string(), file.path.clone());
            let package_type = serde_json::to_value(package_data.package_type)
                .ok()
                .and_then(|v| v.as_str().map(str::to_string));
            row.insert(
                "type".to_string(),
                package_type.unwrap_or_else(|| "None".to_string()),
            );
            row.insert(
                "packaging".to_string(),
                html_opt(package_data.namespace.as_deref()),
            );
            row.insert(
                "primary_language".to_string(),
                html_opt(package_data.primary_language.as_deref()),
            );
            package_rows.push(row);
        }
    }

    license_copyright_rows.sort_by(|a, b| {
        a["path"]
            .cmp(&b["path"])
            .then(a["start"].cmp(&b["start"]))
            .then(a["what"].cmp(&b["what"]))
    });
    holder_rows.sort_by(|a, b| a["path"].cmp(&b["path"]).then(a["start"].cmp(&b["start"])));
    author_rows.sort_by(|a, b| a["path"].cmp(&b["path"]).then(a["start"].cmp(&b["start"])));
    email_rows.sort_by(|a, b| a["path"].cmp(&b["path"]).then(a["start"].cmp(&b["start"])));
    url_rows.sort_by(|a, b| a["path"].cmp(&b["path"]).then(a["start"].cmp(&b["start"])));
    package_rows.sort_by(|a, b| a["path"].cmp(&b["path"]).then(a["type"].cmp(&b["type"])));

    let mut context = Context::new();
    context.insert("license_copyright_rows", &license_copyright_rows);
    context.insert("file_rows", &file_rows);
    context.insert("holder_rows", &holder_rows);
    context.insert("author_rows", &author_rows);
    context.insert("email_rows", &email_rows);
    context.insert("url_rows", &url_rows);
    context.insert("package_rows", &package_rows);

    let rendered = Tera::one_off(HTML_REPORT_TEMPLATE, &context, false).map_err(io_other)?;
    writer.write_all(rendered.as_bytes())
}

fn html_opt(value: Option<&str>) -> String {
    value.unwrap_or("None").to_string()
}

fn html_bool(value: bool) -> &'static str {
    if value { "True" } else { "False" }
}

const HTML_REPORT_TEMPLATE: &str = r#"<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Provenant HTML Report</title>
    <style type="text/css">
      table {
        border-collapse: collapse;
        border: 1px solid gray;
        margin-bottom: 20px;
      }
      td {
        padding: 5px 5px;
        border-style: solid;
        border-width: 1px;
        overflow: hidden;
      }
      th {
        padding: 10px 5px;
        border-style: solid;
        border-width: 1px;
        overflow: hidden;
        border-color: gray;
        color: #fff;
        background-color: #5e81b7;
      }
      tr:nth-child(even) {
        background-color: #ffffff;
      }
      tr:nth-child(odd) {
        background-color: #f9f9f9;
      }
      tr:hover {
        background-color: #eeeeee;
      }
      * {
        font-family: Helvetica, Arial, sans-serif;
        font-weight: normal;
        font-size: 12px;
      }
    </style>
  </head>
  <body>
    <p>Scanned with Provenant</p>

    <table>
      <caption>Copyrights and Licenses Information</caption>
      <thead>
        <tr>
          <th>path</th>
          <th>start</th>
          <th>end</th>
          <th>what</th>
          <th>value</th>
        </tr>
      </thead>
      <tbody>
        {% for row in license_copyright_rows %}
        <tr>
          <td>{{ row.path }}</td>
          <td>{{ row.start }}</td>
          <td>{{ row.end }}</td>
          <td>{{ row.what }}</td>
          <td>{{ row.value }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>

    <table>
      <caption>File Information</caption>
      <thead>
        <tr>
          <th>path</th>
          <th>type</th>
          <th>name</th>
          <th>extension</th>
          <th>size</th>
          <th>sha1</th>
          <th>md5</th>
          <th>files_count</th>
          <th>mime_type</th>
          <th>file_type</th>
          <th>programming_language</th>
          <th>is_binary</th>
          <th>is_text</th>
          <th>is_archive</th>
          <th>is_media</th>
          <th>is_source</th>
          <th>is_script</th>
        </tr>
      </thead>
      <tbody>
        {% for row in file_rows %}
        <tr>
          <td>{{ row.path }}</td>
          <td>{{ row.type }}</td>
          <td>{{ row.name }}</td>
          <td>{{ row.extension }}</td>
          <td>{{ row.size }}</td>
          <td>{{ row.sha1 }}</td>
          <td>{{ row.md5 }}</td>
          <td>{{ row.files_count }}</td>
          <td>{{ row.mime_type }}</td>
          <td>{{ row.file_type }}</td>
          <td>{{ row.programming_language }}</td>
          <td>{{ row.is_binary }}</td>
          <td>{{ row.is_text }}</td>
          <td>{{ row.is_archive }}</td>
          <td>{{ row.is_media }}</td>
          <td>{{ row.is_source }}</td>
          <td>{{ row.is_script }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>

    <table>
      <caption>Holders</caption>
      <thead>
        <tr>
          <th>path</th>
          <th>holder</th>
          <th>start</th>
          <th>end</th>
        </tr>
      </thead>
      <tbody>
        {% for row in holder_rows %}
        <tr>
          <td>{{ row.path }}</td>
          <td>{{ row.holder }}</td>
          <td>{{ row.start }}</td>
          <td>{{ row.end }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>

    <table>
      <caption>Authors</caption>
      <thead>
        <tr>
          <th>path</th>
          <th>Author</th>
          <th>start</th>
          <th>end</th>
        </tr>
      </thead>
      <tbody>
        {% for row in author_rows %}
        <tr>
          <td>{{ row.path }}</td>
          <td>{{ row.author }}</td>
          <td>{{ row.start }}</td>
          <td>{{ row.end }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>

    <table>
      <caption>Emails</caption>
      <thead>
        <tr>
          <th>path</th>
          <th>email</th>
          <th>start</th>
          <th>end</th>
        </tr>
      </thead>
      <tbody>
        {% for row in email_rows %}
        <tr>
          <td>{{ row.path }}</td>
          <td>{{ row.email }}</td>
          <td>{{ row.start }}</td>
          <td>{{ row.end }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>

    <table>
      <caption>Urls</caption>
      <thead>
        <tr>
          <th>path</th>
          <th>url</th>
          <th>start</th>
          <th>end</th>
        </tr>
      </thead>
      <tbody>
        {% for row in url_rows %}
        <tr>
          <td>{{ row.path }}</td>
          <td>{{ row.url }}</td>
          <td>{{ row.start }}</td>
          <td>{{ row.end }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>

    <table>
      <caption>Package Information</caption>
      <thead>
        <tr>
          <th>path</th>
          <th>type</th>
          <th>packaging</th>
          <th>primary_language</th>
        </tr>
      </thead>
      <tbody>
        {% for row in package_rows %}
        <tr>
          <td>{{ row.path }}</td>
          <td>{{ row.type }}</td>
          <td>{{ row.packaging }}</td>
          <td>{{ row.primary_language }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </body>
  <footer>
    <p>
      Generated with Provenant and provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
      ANY KIND, either express or implied. No content created from Provenant should be considered or
      used as legal advice. Consult an attorney for legal advice. Provenant is a free software code
      scanning tool. Visit
      <a href="https://github.com/mstykow/provenant/">https://github.com/mstykow/provenant/</a>
      for support and download.
    </p>
  </footer>
</html>
"#;