From: Shan Jin Date: Tue, 24 Dec 2013 05:28:34 +0000 (+0800) Subject: add image ks file diff feature X-Git-Tag: submit/devel/20190730.075508~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2c672a4532e7239b55d8cd79214f1b3c370d5b55;p=services%2Fpython-snapdiff.git add image ks file diff feature - add few methods to parse the diff info in image.py. - add new template to show the ks diff. Change-Id: I7d8b71a63a09088f4a8e756b4a8791b4e6088ac9 Fixes: #1567 --- diff --git a/snapdiff/__init__.py b/snapdiff/__init__.py index 84b1c04..aeb85cb 100644 --- a/snapdiff/__init__.py +++ b/snapdiff/__init__.py @@ -28,6 +28,7 @@ __version__ = '0.1' import json import os import shutil +import re from .image import ks_diff, packages from .render import output_html @@ -160,7 +161,7 @@ def diff_to_JSON(old_url, new_url, style='repo'): } if style == 'image': - obj['diff']['ks'] = ''.join([d for d in ks_diff(old_url, new_url)]) + obj['diff']['ks'] = ks_diff(old_url, new_url) return json.dumps(obj, indent=4) @@ -174,12 +175,17 @@ def diff_to_HTML(old_url, new_url, style='repo'): data = json.loads(json_obj) - context = {'old_url': old_url, - 'new_url': new_url, - 'diff': data['diff'], - } - - return output_html('diff.html', **context) + context = { + 'old': {'url': old_url, + 'name': re.search('\w*_\d{8}\.\d*', old_url).group(0)}, + 'new': {'url': new_url, + 'name': re.search('\w*_\d{8}\.\d*', new_url).group(0)}, + 'diff': data['diff'], + } + if style == 'repo': + return output_html('diff.html', **context) + elif style == 'image': + return output_html('image_diff.html', **context) def diff_to_dist(old_url, new_url, dist_path, style='repo'): """create a dist-dir of diffs, contains html and css""" diff --git a/snapdiff/image.py b/snapdiff/image.py index c22efc0..e52dbf1 100644 --- a/snapdiff/image.py +++ b/snapdiff/image.py @@ -33,12 +33,117 @@ def _get_file(url, suffix='.ks'): return '', '' +def _parse_range(line): + '''Start and Count''' + def parse_start_count(chars): + '''Count ommit when it's 1''' + start, count = chars[1:].split(',') + return int(start), int(count) + + _, delete, insert, _ = line.split() + return { + 'delete': parse_start_count(delete), + 'insert': parse_start_count(insert), + } + +def _parse_hunks(arr, range_): + '''Hunks''' + count = 0 + hunks = [] + left_start = range_['delete'][0] + right_start = range_['insert'][0] + for line in arr: + if line.startswith('+++') or line.startswith('---'): + count += 1 + continue + elif line.startswith(' '): + type_ = 'context' + hunk = {'type':'context', + 'left_num':left_start, + 'right_num':right_start, + 'text':line[1:-1]} + left_start += 1 + right_start += 1 + elif line.startswith('-'): + type_ = 'delete' + hunk = {} + for right_line in arr[count:]: + if right_line.startswith(' '): + hunk = {'type':'delete', + 'left_num':left_start, + 'left_text':line[1:-1]} + left_start += 1 + break + elif right_line.startswith('+'): + hunk = {'type':'change', + 'left_num':left_start, + 'right_num':right_start, + 'left_text':line[1:-1], + 'right_text':right_line[1:-1]} + arr.remove(right_line) + left_start += 1 + right_start += 1 + break + if not hunk: + hunk = {'type':'delete', + 'left_num':left_start, + 'left_text':line[1:-1]} + left_start += 1 + elif line.startswith('+'): + type_ = 'insert' + hunk = {'type':type_, + 'right_num':right_start, + 'right_text':line[1:-1]} + right_start += 1 + else: + break + hunks.append(hunk) + count += 1 + return hunks + +def _parse_sections(stream): + sections = [] + stack = [] + head_stack = '' + def parse_range_hunks(line): + print head_stack + range_ = _parse_range(head_stack) + hunks = _parse_hunks(stack, range_) + stack = [] + head_stack = line + sections.append({ + 'hunks': hunks + }) + for line in stream: + if not line.startswith('@@ '): + stack.append(line) + continue + if not head_stack: + head_stack = line + continue + range_ = _parse_range(head_stack) + hunks = _parse_hunks(stack, range_) + sections.append({ + 'hunks': hunks + }) + stack = [] + head_stack = line + if stack and head_stack: + range_ = _parse_range(head_stack) + hunks = _parse_hunks(stack, range_) + sections.append({ + 'hunks': hunks + }) + return sections + def ks_diff(old, new): ks_old, ks_new = _get_file(old), _get_file(new) + diff_list = [] - for line in difflib.unified_diff(ks_old[1], ks_new[1], fromfile=ks_old[0], - tofile=ks_new[0]): - yield line + stream = difflib.unified_diff(ks_old[1], ks_new[1], fromfile=ks_old[0], tofile=ks_new[0]) + for section in _parse_sections(stream): + diff_list.append(section) + return {'file_name': [ks_old[0], ks_new[0]], 'sections': diff_list} def packages(url): content = _get_file(url, suffix='.packages') diff --git a/snapdiff/static/css/diff.css b/snapdiff/static/css/diff.css new file mode 100644 index 0000000..c383798 --- /dev/null +++ b/snapdiff/static/css/diff.css @@ -0,0 +1,36 @@ +.ks_diff{ + border-bottom: 1px solid #B0BDCC; +} +ul { + padding: 0; + margin: 0; + list-style-type: none; +} +.ks_diff td { + border: none; + background-color: white; +} + +.ks_diff .code_insert { + background-color: #DDFFDD; +} +.ks_diff .code_delete { + background-color: #FFEEEE; +} +.ks_diff .content { + border-left: thin solid #B0BDCC; + border-right: thin solid #B0BDCC; +} +.ks_diff .nocontent { + background-color: #EEEEEE; +} +.modifLi { + list-style-type: disc; + padding-left:1em; +} +.tableTitle { + background-color: white; + border: none; + font-size: 1.3em; + padding-left: 0; +} diff --git a/snapdiff/templates/diff.html b/snapdiff/templates/diff.html index 708e6d6..4a62a6c 100644 --- a/snapdiff/templates/diff.html +++ b/snapdiff/templates/diff.html @@ -8,6 +8,7 @@ +
@@ -26,7 +27,7 @@ Go back...

Difference between - {{ old_url }} and {{ new_url }} + {{ old['name'] }} and {{ new['name'] }}

Highlights