Jakub Wilk avatar Jakub Wilk committed 2e46683

Initial import.

Comments (0)

Files changed (1)

+#!/usr/bin/python3
+
+import argparse
+import re
+import sys
+
+extract_comments = re.compile(br'''
+^ [ ]* /[*] [ ] "([^"]+)":([0-9]+) $
+( .*? )
+^ [ ][*]/ $
+''', re.VERBOSE | re.MULTILINE | re.DOTALL).findall
+
+parse_marker = re.compile(br'''
+^ ( .*? ) \s+ [#] [  ] <<<<<<<<<<<<<< $
+''', re.VERBOSE).match
+
+class RecoveryError(Exception):
+    pass
+
+def main():
+    stdout = sys.stdout.buffer
+    ap = argparse.ArgumentParser()
+    ap.add_argument('source')
+    ap.add_argument('file', nargs='?')
+    options = ap.parse_args()
+    with open(options.source, 'rb') as file:
+        contents = file.read()
+    comments = extract_comments(contents)
+    if options.file is None:
+        files = frozenset(c[0].decode('ASCII') for c in comments)
+        for file in sorted(list(files)):
+            print(file)
+    else:
+        file_b = options.file.encode('ASCII')
+        code = {}
+        for comment in comments:
+            if comment[0] != file_b:
+                continue
+            n = int(comment[1])
+            k = None
+            local_code = []
+            for m, line in enumerate(comment[2].splitlines()):
+                if not line.startswith(b' * '):
+                    continue
+                line = line[3:]
+                match = parse_marker(line)
+                if match is not None:
+                    line = match.group(1)
+                    k = m
+                local_code += [line]
+            if k is None:
+                raise RecoveryError
+            for m, line in enumerate(local_code, n - k):
+                if m < 0:
+                    raise RecoveryError
+                try:
+                    if code[m] != line:
+                        raise RecoveryError
+                except KeyError:
+                    code[m] = line
+        i = 0
+        while code:
+            line = code.pop(i, b'# <uncython?>')
+            stdout.write(line)
+            stdout.write(b'\n')
+            i += 1
+
+if __name__ == '__main__':
+    main()
+
+# vim:ts=4 sw=4 et
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.