~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to bin/generate-index.py

  • Committer: Selene Scriven
  • Date: 2019-05-24 00:00:21 UTC
  • mto: (483.1.1 fsm)
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: bzr@toykeeper.net-20190524000021-2f8tp4zvfe9aas7f
added GXB172 firmware from loneoceans (tiny841 boost driver)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""Generate an "INDEX" file based on the content of "meta" files
 
4
in all sub-directories.  The index summarizes what tags are relevant
 
5
to which projects, to help people find relevant projects.
 
6
"""
 
7
 
 
8
import os, os.path
 
9
import email
 
10
from pprint import pprint
 
11
 
 
12
outfile = 'INDEX'
 
13
 
 
14
def main(args):
 
15
    results = dict()
 
16
    descriptions = []
 
17
    for root, dirs, files in os.walk('.'):
 
18
        #print 'Scanning "%s"...' % (root)
 
19
        # ignore hidden dirs like '.bzr' and '.git'
 
20
        dirs.sort()
 
21
        for d in dirs:
 
22
            #print 'Dir %s' % (d)
 
23
            if d.startswith('.'):
 
24
                dirs.remove(d)
 
25
                #print 'Removing %s' % (d)
 
26
        if root == '.':
 
27
            continue
 
28
        if 'meta' in files:
 
29
            path = os.path.join(root, 'meta')
 
30
            descriptions.append(parse(path, results))
 
31
        else:
 
32
            # Warn if there's no meta in a leaf dir
 
33
            if not dirs:
 
34
                print 'No "meta" file in %s' % (root)
 
35
 
 
36
    lines = [
 
37
            'This file lists the tags used by projects in this repository, and a list',
 
38
            'of projects associated with each tag.  The purpose is to help people find',
 
39
            'projects relevant to their needs, such as hardware or desired features.',
 
40
            '',
 
41
            'Do not edit this file.  Edit the "meta" files in sub-directories instead,',
 
42
            'and run generate-index.py to rebuild this file.',
 
43
            ]
 
44
    lines.extend(summarize(results, descriptions))
 
45
    lines.append('')
 
46
    #print '=========='
 
47
    #print '\n'.join(lines)
 
48
    fp = open(outfile, 'w')
 
49
    fp.write('\n'.join(lines))
 
50
    fp.close()
 
51
    print 'Wrote %i lines to %s' % (len(lines), outfile)
 
52
 
 
53
def parse(path, results):
 
54
    #print 'Parsing "%s"...' % (path)
 
55
    msg = email.message_from_file(open(path))
 
56
    #pprint(msg.items())
 
57
    description = ''
 
58
 
 
59
    path = os.path.dirname(path)
 
60
    if path.startswith('./'):
 
61
        path = path[2:]
 
62
 
 
63
    for k,v in msg.items():
 
64
        if k.strip() in ('', 'Description', ):
 
65
            description = v
 
66
            continue
 
67
        v = v.replace('\n', '')
 
68
        v = v.replace('  ', ' ')
 
69
        for v2 in v.split(', '):
 
70
            if v2.strip() in ('', ):
 
71
                continue
 
72
            key = (k,v2.strip())
 
73
            if key not in results:
 
74
                results[key] = []
 
75
            results[key].append(path)
 
76
 
 
77
    return (path, description)
 
78
 
 
79
def summarize(results, descriptions):
 
80
    lines = []
 
81
 
 
82
    # Show a one-liner for each project.
 
83
    lines.append('')
 
84
    lines.append('Summary of each project')
 
85
    lines.append('')
 
86
    descriptions.sort()
 
87
    for path, desc in descriptions:
 
88
        desc = desc.split('\n')[0]
 
89
        if not desc: continue
 
90
        lines.append('    %s:' % (path,))
 
91
        lines.append('        ' + desc)
 
92
        lines.append('')
 
93
 
 
94
    # Show the tags for each project, sorted by tag
 
95
    keys = results.keys()
 
96
    # TODO: move 'extras' section to bottom
 
97
    keys.sort()
 
98
    prev_lk = ''
 
99
    for lk,rk in keys:
 
100
        if lk != prev_lk:
 
101
            lines.append('')
 
102
            lines.append('%s' % (lk))
 
103
        prev_lk = lk
 
104
        lines.append('')
 
105
        lines.append('    %s:' % (rk))
 
106
        for v in results[(lk,rk)]:
 
107
            lines.append('        %s' % (v))
 
108
 
 
109
    return lines
 
110
 
 
111
if __name__ == "__main__":
 
112
    import sys
 
113
    main(sys.argv[1:])
 
114