]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/parser.py
Move patchparser to patchwork.parser
[patchwork] / apps / patchwork / parser.py
index bb6fce022a98a4bb61b734ac2a5189878e1c6491..16d1de4c6f8e03fdbed64231098d86a2774781e4 100644 (file)
 
 
 import re
-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+)')
 
 def parse_patch(text):
     patchbuf = ''
@@ -62,6 +53,7 @@ def parse_patch(text):
     lc = (0, 0)
     hunk = 0
 
+    hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
 
     for line in text.split('\n'):
         line += '\n'
@@ -99,7 +91,7 @@ def parse_patch(text):
                 buf = ''
 
         elif state == 3:
-            match = _hunk_re.match(line)
+            match = hunk_re.match(line)
             if match:
 
                 def fn(x):
@@ -157,55 +149,10 @@ def parse_patch(text):
 
     return (patchbuf, commentbuf)
 
-def patch_hash(str):
-    str = str.replace('\r', '')
-    str = str.strip() + '\n'
-    lines = str.split('\n')
-
-    prefixes = ['-', '+', ' ']
-    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)
-
-        if filename_match:
-            # normalise -p1 top-directories
-            if filename_match.group(1) == '---':
-                filename = 'a/'
-            else:
-                filename = 'b/'
-            filename += '/'.join(filename_match.group(2).split('/')[1:])
-
-            line = filename_match.group(1) + ' ' + filename
-
-            
-       elif hunk_match:
-            # remove line numbers
-            def fn(x):
-                if not x:
-                    return 1
-                return int(x)
-            line_nos = map(fn, hunk_match.groups())
-            line = '@@ -%d +%d @@' % tuple(line_nos)
-
-        elif line[0] in prefixes:
-            pass
-
-        else:
-            continue
-
-        hash.update(line + '\n')
-
 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())
+    (patch, comment) = parse_patch(sys.stdin.read())
+    if patch:
+        print "Patch: ------\n" + patch
+    if comment:
+        print "Comment: ----\n" + comment