summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorTaylor Yu <catalyst@torproject.org>2020-02-14 11:02:16 -0600
committerTaylor Yu <catalyst@torproject.org>2020-02-14 11:02:16 -0600
commit55f088bb2998e70cb88e17ce21c3541966ae94ad (patch)
tree6a2e1a077770ffdbc6dc79fffbee86dd163cf362 /scripts
parent98899f20ad22e97c03235cfe13ff1fe416a15509 (diff)
downloadtor-55f088bb2998e70cb88e17ce21c3541966ae94ad.tar.gz
tor-55f088bb2998e70cb88e17ce21c3541966ae94ad.zip
Script to check sorting of manpage entries
Add a script to help check the alphabetical ordering of option names in a manpage. Closes ticket 33339.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/maint/checkManpageAlpha.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/scripts/maint/checkManpageAlpha.py b/scripts/maint/checkManpageAlpha.py
new file mode 100755
index 0000000000..70421c2fd1
--- /dev/null
+++ b/scripts/maint/checkManpageAlpha.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+import difflib
+import re
+import sys
+
+# Assume we only use the "== Section Name" section title syntax
+sectionheader_re = re.compile(r'^==+\s(.*)\s*$')
+
+# Assume we only use the "[[ItemName]]" anchor syntax
+anchor_re = re.compile(r'^\[\[([^]]+)\]\]')
+
+class Reader(object):
+ def __init__(self):
+ self.d = {}
+ # Initial state is to gather section headers
+ self.getline = self._getsec
+ self.section = None
+
+ def _getsec(self, line):
+ """Read a section header
+
+ Prepare to gather anchors from subsequent lines. Don't change
+ state if the line isn't a section header.
+ """
+ m = sectionheader_re.match(line)
+ if not m:
+ return
+ self.anchors = anchors = []
+ self.d[m.group(1)] = anchors
+ self.getline = self._getanchor
+
+ def _getanchor(self, line):
+ """Read an anchor for an item definition
+
+ Append the anchor names to the list of items in the current
+ section.
+ """
+ m = anchor_re.match(line)
+ if not m:
+ return self._getsec(line)
+ self.anchors.append(m.group(1))
+
+ def diffsort(self, key):
+ """Unified diff of unsorted and sorted item lists
+ """
+ # Append newlines because difflib works better with them
+ a = [s + '\n' for s in self.d[key]]
+ b = sorted(a, key=str.lower)
+ return difflib.unified_diff(a, b, fromfile=key+' unsorted',
+ tofile=key+' sorted')
+
+def main():
+ """Diff unsorted and sorted lists of option names in a manpage
+
+ Use the file named by the first argument, or standard input if
+ there is none.
+ """
+ try:
+ fname = sys.argv[1]
+ f = open(fname, 'r')
+ except IndexError:
+ f = sys.stdin
+
+ reader = Reader()
+ for line in f:
+ reader.getline(line)
+ for key in sorted(reader.d.keys(), key=str.lower):
+ sys.stdout.writelines(reader.diffsort(key))
+
+if __name__ == '__main__':
+ main()