Merge branch 'maint-0.4.7'
[tor.git] / scripts / maint / checkManpageAlpha.py
blob70421c2fd112b0a8dbe91da6240ce6a795f081c0
1 #!/usr/bin/python
3 import difflib
4 import re
5 import sys
7 # Assume we only use the "== Section Name" section title syntax
8 sectionheader_re = re.compile(r'^==+\s(.*)\s*$')
10 # Assume we only use the "[[ItemName]]" anchor syntax
11 anchor_re = re.compile(r'^\[\[([^]]+)\]\]')
13 class Reader(object):
14 def __init__(self):
15 self.d = {}
16 # Initial state is to gather section headers
17 self.getline = self._getsec
18 self.section = None
20 def _getsec(self, line):
21 """Read a section header
23 Prepare to gather anchors from subsequent lines. Don't change
24 state if the line isn't a section header.
25 """
26 m = sectionheader_re.match(line)
27 if not m:
28 return
29 self.anchors = anchors = []
30 self.d[m.group(1)] = anchors
31 self.getline = self._getanchor
33 def _getanchor(self, line):
34 """Read an anchor for an item definition
36 Append the anchor names to the list of items in the current
37 section.
38 """
39 m = anchor_re.match(line)
40 if not m:
41 return self._getsec(line)
42 self.anchors.append(m.group(1))
44 def diffsort(self, key):
45 """Unified diff of unsorted and sorted item lists
46 """
47 # Append newlines because difflib works better with them
48 a = [s + '\n' for s in self.d[key]]
49 b = sorted(a, key=str.lower)
50 return difflib.unified_diff(a, b, fromfile=key+' unsorted',
51 tofile=key+' sorted')
53 def main():
54 """Diff unsorted and sorted lists of option names in a manpage
56 Use the file named by the first argument, or standard input if
57 there is none.
58 """
59 try:
60 fname = sys.argv[1]
61 f = open(fname, 'r')
62 except IndexError:
63 f = sys.stdin
65 reader = Reader()
66 for line in f:
67 reader.getline(line)
68 for key in sorted(reader.d.keys(), key=str.lower):
69 sys.stdout.writelines(reader.diffsort(key))
71 if __name__ == '__main__':
72 main()