aboutsummaryrefslogtreecommitdiff
path: root/scripts/cbc_repo2html
blob: 729ce4a2aca121d944a2e6ec6a3c0a924c798abf (plain) (blame)
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
#!/usr/bin/env python

import argparse
import json
import os


def table_from_dict(data):
    headers = ['subdir',
                'name',
                'version',
                'build',
                'build_number',
                'license',
                'depends',
                'md5',
                'sig',
                'size']

    html = '<table>'

    for header in headers:
        html += '<th>' + header.upper() + '</th>'

    for pkg_name, pkg_info in sorted(data.items()):
        html += '<tr>'
        html += '<tr>'
        for header in headers:
            if header not in pkg_info.keys():
                pkg_info[header] = '-'

            for key, value in pkg_info.items():
                if value is None or not value:
                    value = '-'
                if key == header:
                    if isinstance(value, list):
                        html += '<td>'
                        for record in sorted(value):
                            html += '<li>' + record + '</li>'
                        html += '</td>'
                    else:
                        html += '<td>' + str(value) + '</td>'

        html += '</tr>'
    html += '</tr>'
    html += '</table>'
    return html

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('repodata', action='store')
    args = parser.parse_args()

    filename = args.repodata

    if '.json' not in os.path.splitext(filename)[1]:
        print("JSON formatted file required.")
        exit(1)

    repodata = None
    with open(filename, 'r') as data:
        repodata = json.load(data)

    if repodata is None:
        print("Invalid JSON file.")
        exit(1)

    print('<html>')
    print('<head>')
    print('<title>Repository Contents</title>')
    print('</head>')
    print('<style>')
    print('''
    table {
        border-collapse: collapse;
        width: 100%;
    }
    
    th, td {
        width: 5%;
        padding: 8px;
        text-align: left;
        /*border-bottom: 1px solid #ddd;*/
        border: 1px solid #ddd;
    }
    ''')
    print('</style>')
    print('<body>')
    #for key, subdict in sorted(repodata['packages'].items()):
    #    print("{0:50s} {1:>40s} {2:>20d}kb".format(key, subdict['md5'], subdict['size']))
    print(table_from_dict(repodata['packages']))
    print('</body>')
    print('</html>')