diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2008-09-09 17:26:38 +1000 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2008-09-09 17:26:38 +1000 |
commit | 0521d8b2e26242fa3a161df50b3f61b9a05ae0dd (patch) | |
tree | 8cb2d309152c8cac7423263249aaff95c9db269d /apps/patchwork/parser.py | |
parent | 4ede11b48f3b056d655a2e4a74d3627292bfbb9f (diff) | |
download | patchwork-0521d8b2e26242fa3a161df50b3f61b9a05ae0dd.tar.bz2 patchwork-0521d8b2e26242fa3a161df50b3f61b9a05ae0dd.tar.xz |
Hook-up hashing infrastructure
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'apps/patchwork/parser.py')
-rw-r--r-- | apps/patchwork/parser.py | 61 |
1 files changed, 59 insertions, 2 deletions
diff --git a/apps/patchwork/parser.py b/apps/patchwork/parser.py index 16d1de4..8ed36cc 100644 --- a/apps/patchwork/parser.py +++ b/apps/patchwork/parser.py @@ -22,6 +22,16 @@ 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 = '' commentbuf = '' @@ -53,7 +63,6 @@ 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' @@ -91,7 +100,7 @@ def parse_patch(text): buf = '' elif state == 3: - match = hunk_re.match(line) + match = _hunk_re.match(line) if match: def fn(x): @@ -149,10 +158,58 @@ def parse_patch(text): return (patchbuf, commentbuf) +def hash_patch(str): + # normalise spaces + str = str.replace('\r', '') + str = str.strip() + '\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, but leave line counts + 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: + # if we have a +, - or context line, leave as-is + pass + + else: + # other lines are ignored + continue + + hash.update(line.encode('utf-8') + '\n') + + return hash + if __name__ == '__main__': import sys (patch, comment) = parse_patch(sys.stdin.read()) if patch: print "Patch: ------\n" + patch + print "hash: %s" % hash_patch(patch).hexdigest() if comment: print "Comment: ----\n" + comment |