aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/cve-generate-chartdata
blob: dbbbe8280d72751a3e387e5f452a49153bb6d526 (plain)
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
#!/usr/bin/env python3
import json, os.path, collections
import sys
import argparse
import subprocess
import tempfile
from datetime import datetime, date, timedelta

args = argparse.ArgumentParser(description="Generate CVE count data files")
args.add_argument("-j", "--json", help="JSON data file to use")
args.add_argument("-r", "--resultsdir", help="results directory to parse")
args = args.parse_args()

try:
    with open(args.json) as f:
        counts = json.load(f)
except FileNotFoundError:
    # if the file does not exist, start with an empty database.
    counts = {}

lastyear = {}

#
# Write CVE counts by day
#
def round_to_day(val):
    return int((datetime.fromtimestamp(int(val)).date() - date(1970, 1, 1)).total_seconds())

a_year_ago = (datetime.now() - timedelta(days=365) - datetime(1970, 1, 1)).total_seconds()

for branch in os.listdir(args.resultsdir):
    branchdir = os.path.join(args.resultsdir, branch)
    for f in os.listdir(branchdir):
        ts = f.split(".")[0]
        rounded_ts = str(round_to_day(ts))
        if rounded_ts not in counts:
            counts[rounded_ts] = {}
        if branch not in counts[rounded_ts]:
            cvereport = os.path.join(branchdir, f)
            with open(cvereport) as report:
                reportdata = json.load(report)
            count = 0
            seen = []
            for package in reportdata['package']:
                if branch in ['dunfell', 'kirkstone', 'langdale'] and package['name'] in ['linux-yocto']:
                    continue
                for issue in package['issue']:
                    if issue['status'] == "Unpatched" and issue['id'] not in seen:
                        count = count + 1
                        seen.append(issue['id'])
            print("Adding count %s for branch %s from file %s (ts %s)" % (count, branch, cvereport, rounded_ts))
            counts[rounded_ts][branch] = str(count)

for c in counts:
    if int(c) > a_year_ago:
        lastyear[c] = counts[c]

with open(args.json, "w") as f:
    json.dump(counts, f, sort_keys=True, indent="\t")

with open(args.json.replace(".json", "-lastyear.json") , "w") as f:
    json.dump(lastyear, f, sort_keys=True, indent="\t")