summaryrefslogtreecommitdiff
path: root/doc/tools/findacks
blob: c13b00f0af50f9571f5b6051c6e62f141c3718df (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
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
#!/usr/bin/env python
"""Script to locate email addresses in the CVS logs."""
__version__ = '$Revision: 1.1.1.1 $'

import os
import re
import sys
import UserDict

import cvsinfo


class Acknowledgements(UserDict.UserDict):
    def add(self, email, name, path):
        d = self.data
        d.setdefault(email, {})[path] = name


def open_cvs_log(info, paths=None):
    cvsroot = info.get_cvsroot()
    cmd = "cvs -q -d%s log " % cvsroot
    if paths:
        cmd += " ".join(paths)
    return os.popen(cmd, "r")


email_rx = re.compile("<([a-z][-a-z0-9._]*@[-a-z0-9.]+)>", re.IGNORECASE)

def find_acks(f, acks):
    prev = ''
    filename = None
    MAGIC_WORDS = ('van', 'von')
    while 1:
        line = f.readline()
        if not line:
            break
        if line.startswith("Working file: "):
            filename = line.split(None, 2)[2].strip()
            prev = line
            continue
        m = email_rx.search(line)
        if m:
            words = prev.split() + line[:m.start()].split()
            L = []
            while words \
                  and (words[-1][0].isupper() or words[-1] in MAGIC_WORDS):
                L.insert(0, words.pop())
            name = " ".join(L)
            email = m.group(1).lower()
            acks.add(email, name, filename)
        prev = line


def load_cvs_log_acks(acks, args):
    repolist = cvsinfo.get_repository_list(args or [""])
    for info, paths in repolist:
        print >>sys.stderr, "Repository:", info.get_cvsroot()
        f = open_cvs_log(info, paths)
        find_acks(f, acks)
        f.close()


def load_tex_source_acks(acks, args):
    for path in args:
        path = path or os.curdir
        if os.path.isfile(path):
            read_acks_from_tex_file(acks, path)
        else:
            read_acks_from_tex_dir(acks, path)


def read_acks_from_tex_file(acks, path):
    f = open(path)
    while 1:
        line = f.readline()
        if not line:
            break
        if line.startswith(r"\sectionauthor{"):
            line = line[len(r"\sectionauthor"):]
            name, line = extract_tex_group(line)
            email, line = extract_tex_group(line)
            acks.add(email, name, path)


def read_acks_from_tex_dir(acks, path):
    stack = [path]
    while stack:
        p = stack.pop()
        for n in os.listdir(p):
            n = os.path.join(p, n)
            if os.path.isdir(n):
                stack.insert(0, n)
            elif os.path.normpath(n).endswith(".tex"):
                read_acks_from_tex_file(acks, n)


def extract_tex_group(s):
    c = 0
    for i in range(len(s)):
        if s[i] == '{':
            c += 1
        elif s[i] == '}':
            c -= 1
            if c == 0:
                return s[1:i], s[i+1:]


def print_acks(acks):
    first = 1
    for email, D in acks.items():
        if first:
            first = 0
        else:
            print
        L = D.items()
        L.sort()
        prefname = L[0][1]
        for file, name in L[1:]:
            if name != prefname:
                prefname = ""
                break
        if prefname:
            print prefname, "<%s>:" % email
        else:
            print email + ":"
        for file, name in L:
            if name == prefname:
                print "    " + file
            else:
                print "    %s (as %s)" % (file, name)


def print_ack_names(acks):
    names = []
    for email, D in acks.items():
        L = D.items()
        L.sort()
        prefname = L[0][1]
        for file, name in L[1:]:
            prefname = prefname or name
        names.append(prefname or email)
    def f(s1, s2):
        s1 = s1.lower()
        s2 = s2.lower()
        return cmp((s1.split()[-1], s1),
                   (s2.split()[-1], s2))
    names.sort(f)
    for name in names:
        print name


def main():
    args = sys.argv[1:]
    acks = Acknowledgements()
    load_cvs_log_acks(acks, args)
    load_tex_source_acks(acks, args)
    print_ack_names(acks)


if __name__ == "__main__":
    main()