]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/parser.py
[views] Restructure profile view, simplify bundle access
[patchwork] / apps / patchwork / parser.py
index ecc1d4b0f1e5ce962f4d37a3afcea23867769d90..abec782f4f7c75cf74d7a83ef73b07f680addbdd 100644 (file)
 
 
 import re
-import hashlib
+
+try:
+    import hashlib
+    sha1_hash = hashlib.sha1
+except ImportError:
+    import sha
+    sha1_hash = sha.sha
 
 _hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
 _filename_re = re.compile('^(---|\+\+\+) (\S+)')
@@ -152,21 +158,21 @@ def parse_patch(text):
 
     return (patchbuf, commentbuf)
 
-def patch_hash(str):
+def hash_patch(str):
+    # normalise spaces
     str = str.replace('\r', '')
     str = str.strip() + '\n'
-    lines = str.split('\n')
 
     prefixes = ['-', '+', ' ']
-    hash = hashlib.sha1()
+    hash = sha1_hash()
 
     for line in str.split('\n'):
 
         if len(line) <= 0:
             continue
 
-       hunk_match = _hunk_re.match(line)
-       filename_match = _filename_re.match(line)
+        hunk_match = _hunk_re.match(line)
+        filename_match = _filename_re.match(line)
 
         if filename_match:
             # normalise -p1 top-directories
@@ -178,9 +184,8 @@ def patch_hash(str):
 
             line = filename_match.group(1) + ' ' + filename
 
-            
-       elif hunk_match:
-            # remove line numbers
+        elif hunk_match:
+            # remove line numbers, but leave line counts
             def fn(x):
                 if not x:
                     return 1
@@ -189,18 +194,42 @@ def patch_hash(str):
             line = '@@ -%d +%d @@' % tuple(line_nos)
 
         elif line[0] in prefixes:
+            # if we have a +, - or context line, leave as-is
             pass
 
         else:
+            # other lines are ignored
             continue
 
-        hash.update(line + '\n')
+        hash.update(line.encode('utf-8') + '\n')
+
+    return hash
+
+
+def main(args):
+    from optparse import OptionParser
+
+    parser = OptionParser()
+    parser.add_option('-p', '--patch', action = 'store_true',
+            dest = 'print_patch', help = 'print parsed patch')
+    parser.add_option('-c', '--comment', action = 'store_true',
+            dest = 'print_comment', help = 'print parsed comment')
+    parser.add_option('-#', '--hash', action = 'store_true',
+            dest = 'print_hash', help = 'print patch hash')
+
+    (options, args) = parser.parse_args()
+
+    (patch, comment) = parse_patch(sys.stdin.read())
+
+    if options.print_hash and patch:
+        print hash_patch(patch).hexdigest()
+
+    if options.print_patch and patch:
+        print "Patch: ------\n" + patch
+
+    if options.print_comment and comment:
+        print "Comment: ----\n" + comment
 
 if __name__ == '__main__':
     import sys
-#    (patch, comment) = parse_patch(sys.stdin.read())
-#    if patch:
-#        print "Patch: ------\n" + patch
-#    if comment:
-#        print "Comment: ----\n" + comment
-    normalise_patch_content(sys.stdin.read())
+    sys.exit(main(sys.argv))