X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fparser.py;h=24631b739822fde096f3f748343410be8ec2be3d;hb=7fd7d0bbe4e3fce405e02e9e630ef70023fdd43e;hp=021dbcdc36ae59bc382ad4cdba458be310f3495e;hpb=05d26756d7e7f254d5631a0649aeac5fd3a58ca8;p=patchwork diff --git a/apps/patchwork/parser.py b/apps/patchwork/parser.py index 021dbcd..24631b7 100644 --- a/apps/patchwork/parser.py +++ b/apps/patchwork/parser.py @@ -21,6 +21,7 @@ import re + try: import hashlib sha1_hash = hashlib.sha1 @@ -62,8 +63,7 @@ def parse_patch(text): lc = (0, 0) hunk = 0 - - for line in text.split('\n'): + for line in text.decode('utf-8').split('\n'): line += '\n' if state == 0: @@ -132,6 +132,9 @@ def parse_patch(text): lc[0] -= 1 elif line.startswith('+'): lc[1] -= 1 + elif line.startswith('\ No newline at end of file'): + # Special case: Not included as part of the hunk's line count + pass else: lc[0] -= 1 lc[1] -= 1 @@ -157,10 +160,10 @@ 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 = sha1_hash() @@ -183,9 +186,8 @@ def patch_hash(str): line = filename_match.group(1) + ' ' + filename - elif hunk_match: - # remove line numbers + # remove line numbers, but leave line counts def fn(x): if not x: return 1 @@ -194,18 +196,45 @@ 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() + + # decode from (assumed) UTF-8 + content = sys.stdin.read().decode('utf-8') + + (patch, comment) = parse_patch(content) + + 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))