~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to bin/generate-index.py

  • Committer: Selene Scriven
  • Date: 2015-09-14 19:23:29 UTC
  • mto: (153.1.18 tiny25)
  • mto: This revision was merged to the branch mainline in revision 156.
  • Revision ID: ubuntu@toykeeper.net-20150914192329-0ean5s8qpnnkdbub
updated to BLF-VLD 0.2

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
        for v2 in v.split(', '):
 
68
            if v2.strip() in ('', ):
 
69
                continue
 
70
            key = (k,v2)
 
71
            if key not in results:
 
72
                results[key] = []
 
73
            results[key].append(path)
 
74
 
 
75
    return (path, description)
 
76
 
 
77
def summarize(results, descriptions):
 
78
    lines = []
 
79
 
 
80
    # Show a one-liner for each project.
 
81
    lines.append('')
 
82
    lines.append('Summary of each project')
 
83
    lines.append('')
 
84
    descriptions.sort()
 
85
    for path, desc in descriptions:
 
86
        desc = desc.split('\n')[0]
 
87
        if not desc: continue
 
88
        lines.append('    %s:' % (path,))
 
89
        lines.append('        ' + desc)
 
90
        lines.append('')
 
91
 
 
92
    # Show the tags for each project, sorted by tag
 
93
    keys = results.keys()
 
94
    # TODO: move 'extras' section to bottom
 
95
    keys.sort()
 
96
    prev_lk = ''
 
97
    for lk,rk in keys:
 
98
        if lk != prev_lk:
 
99
            lines.append('')
 
100
            lines.append('%s' % (lk))
 
101
        prev_lk = lk
 
102
        lines.append('')
 
103
        lines.append('    %s:' % (rk))
 
104
        for v in results[(lk,rk)]:
 
105
            lines.append('        %s' % (v))
 
106
 
 
107
    return lines
 
108
 
 
109
if __name__ == "__main__":
 
110
    import sys
 
111
    main(sys.argv[1:])
 
112