From c66b46facc87348f6fd3287c0300c395a911702f Mon Sep 17 00:00:00 2001 From: "sk7.park" Date: Tue, 5 Jul 2022 19:45:28 +0900 Subject: [PATCH] 1. Add diff type - commits 2. Fix variable error in image diff Change-Id: I91975f5a69f081302404b7411a7a1abae2e540a6 --- snapdiff/__init__.py | 52 +++++++++--- snapdiff/image.py | 4 +- snapdiff/templates/commit_diff.csv | 36 ++++++++ snapdiff/templates/commit_diff.html | 124 ++++++++++++++++++++++++++++ 4 files changed, 203 insertions(+), 13 deletions(-) create mode 100644 snapdiff/templates/commit_diff.csv create mode 100644 snapdiff/templates/commit_diff.html diff --git a/snapdiff/__init__.py b/snapdiff/__init__.py index f0b28e7..b68ee4a 100644 --- a/snapdiff/__init__.py +++ b/snapdiff/__init__.py @@ -68,6 +68,8 @@ def diff_to_json(old_url, new_url, **kwargs): old, new = Repo(old_url).packages, Repo(new_url).packages elif style == 'image': old, new = packages(old_url), packages(new_url) + elif style == 'commit': + old, new = Repo(old_url).packages, Repo(new_url).packages else: return @@ -104,10 +106,23 @@ def diff_to_json(old_url, new_url, **kwargs): data = [] for idx in range(1, len(sp), 2): l = sp[idx].split("<$^=->") - data.append({"commit_id": escape_html(l[0]), - "committer": escape_html(l[1]), - "commit_date": escape_html(l[2]), - "commit_log": {'html':escape_html_newline(escape_html(l[3])), 'raw':escape_html(l[3])}}) + if style != 'commit': + data.append({"commit_id": escape_html(l[0]), + "committer": escape_html(l[1]), + "commit_date": escape_html(l[2]), + "commit_log": {'html':escape_html_newline(escape_html(l[3])), 'raw':escape_html(l[3])}}) + else: + committer = escape_html(l[1]) + found = re.search('<(.+?)>', committer) + if found: + committer = found.group(1) + committer = committer.split('@')[0] + # if (committer.find('<') != -1) and (committer.find('>') != -1): + # committer = committer.split('<')[-1].split('>')[0].replace('@samsung.com','') + data.append({"commit_id": escape_html(l[0])[:7], + "committer": committer, + "commit_date": escape_html(l[2]), + "commit_log": {'html':escape_html_newline(escape_html(l[3])), 'raw':escape_html(l[3])}}) f.close() return data @@ -174,6 +189,7 @@ def diff_to_json(old_url, new_url, **kwargs): 'removed': [], 'modified': [], 'rebuilded': [], + 'count': {'added' : 0, 'removed': 0, 'modified': 0, 'rebuilded': 0}, }} #----------------------------------------------------------------------------- added_p = {} @@ -202,13 +218,13 @@ def diff_to_json(old_url, new_url, **kwargs): p = {'oldpkg': None, 'newpkg': { - 'name': ','.join(ps), + 'name': ','.join(ps) if style != 'commit' else _pp.name, 'version': { 'epoch': _p.version.epoch, 'rel': _p.version.rel, 'ver': _p.version.ver, }, - 'git_path': _git_path, + 'git_path': _git_path.split(':/')[-1], 'commit_id': _commit_id }, 'codediff': _codediff, @@ -242,13 +258,13 @@ def diff_to_json(old_url, new_url, **kwargs): obj['diff']['removed'].append( {'oldpkg': { - 'name': ','.join(ps), + 'name': ','.join(ps) if style != 'commit' else _pp.name, 'version': { 'epoch': _p.version.epoch, 'rel': _p.version.rel, 'ver': _p.version.ver, }, - 'git_path': _git_path, + 'git_path': _git_path.split(':/')[-1], 'commit_id': _commit_id }, 'newpkg': None, @@ -285,7 +301,7 @@ def diff_to_json(old_url, new_url, **kwargs): for _old, _new in modified_p[_git_path][_version]: ps.append('.'.join([_old.name,_old.arch])) - names = ','.join(ps) + names = ','.join(ps) if style != 'commit' else _old.name obj['diff']['modified'].append( {'oldpkg': { 'name': names, @@ -294,7 +310,7 @@ def diff_to_json(old_url, new_url, **kwargs): 'rel': _old.version.rel, 'ver': _old.version.ver, }, - 'git_path': _old_git_path, + 'git_path': _old_git_path.split(':/')[-1], 'commit_id': _old_commit_id }, 'newpkg':{ @@ -304,7 +320,7 @@ def diff_to_json(old_url, new_url, **kwargs): 'rel': _new.version.rel, 'ver': _new.version.ver, }, - 'git_path': _new_git_path, + 'git_path': _new_git_path.split(':/')[-1], 'commit_id': _new_commit_id }, 'codediff': _codediff, @@ -364,6 +380,13 @@ def diff_to_json(old_url, new_url, **kwargs): 'codediff': _codediff, }) + + idxs = ['added', 'removed', 'modified', 'rebuilded'] + for idx in idxs: + for item in obj['diff'][idx]: + if 'commit_log' in item: + obj['diff']['count'][idx] += len(item['commit_log']) + #----------------------------------------------------------------------------- #obj = {style: {'old': old_url, 'new': new_url}, #'diff': { @@ -491,6 +514,8 @@ def diff_to_csv(old_url, new_url, **kwargs): return output_html('diff.csv', **context) elif style == 'image': return output_html('image_diff.csv', **context) + elif style == 'commit': + return output_html('commit_diff.csv', **context) def diff_to_html(old_url, new_url, **kwargs): """Output diffs' html format. @@ -519,10 +544,13 @@ def diff_to_html(old_url, new_url, **kwargs): 'filename': diff_name, 'gitweb': data['gitweb'] } + if style == 'repo': return output_html('diff.html', **context) elif style == 'image': return output_html('image_diff.html', **context) + elif style == 'commit': + return output_html('commit_diff.html', **context) def diff_to_dist(old_url, new_url, dist_path, **kwargs): """Create a dist-dir of diffs, contains html and css. @@ -559,7 +587,7 @@ def diff_to_dist(old_url, new_url, dist_path, **kwargs): fname.write(output) output = diff_to_csv(old_url, new_url, style=kwargs.get('style'), diff_name=diff_name) - with io.open(os.path.join(dist_path, '%s.csv' % diff_name), 'w', encoding='utf-8', errors='ignore') as fname: + with io.open(os.path.join(dist_path, '%s.csv' % diff_name), 'w', encoding='utf-8-sig', errors='ignore') as fname: fname.write(output) return dist_path diff --git a/snapdiff/image.py b/snapdiff/image.py index 0219e75..ccc8ad7 100644 --- a/snapdiff/image.py +++ b/snapdiff/image.py @@ -19,7 +19,7 @@ def _get_file(url, suffix='.ks'): lines = response.content.split('\n') for line in lines: if suffix in line: - target = line.split('*')[-1] + target = line.split('*')[-1].split(' ')[-1] f_response = requests.get(url + '/' + target) if f_response.status_code == 200: # use StringIO to keep wrapped lines @@ -153,9 +153,11 @@ def packages(url): if line.strip(): name_arch, version, vcs = line.strip().split() name = name_arch.split('.')[0] + arch = name_arch.split('.')[1] setattr(packages_info, name, []) package_info = JsonDict() package_info.name = name + package_info.arch = arch package_info.version = JsonDict() ver, rel = version.split('-') package_info.version.epoch = 0 # we can't get epoch, so give 0. diff --git a/snapdiff/templates/commit_diff.csv b/snapdiff/templates/commit_diff.csv new file mode 100644 index 0000000..de65774 --- /dev/null +++ b/snapdiff/templates/commit_diff.csv @@ -0,0 +1,36 @@ +Difference between {{old['name']}} and {{new['name']}} + +Added Commits: {{ diff['count']['added'] }} +Removed Packages: {{ diff['removed']|count }} +Modified Commits: {{ diff['count']['modified'] }} + +Added Packages,{% if diff['added'] %} +Package,Git Path,CommitId,Committer,CommitDate,CommitLog + {%- for item in diff['added'] -%} + {%- for commit_item in item['commit_log'] %} +"{{ item['newpkg']['name'] }}","{{ item['newpkg']['git_path'] }}","{{commit_item['commit_id']}}","{{commit_item['committer']}}","{{commit_item['commit_date']}}","{{commit_item['commit_log']['raw']}}" + {%- endfor -%} + {%- endfor -%} +{%- endif %} + +Removed Packages,{% if diff['removed'] %} +Package,Version,Git Path,CommitId + {%- for item in diff['removed'] %} +"{{ item['oldpkg']['name'] }}","{{ item['oldpkg']['version']['ver'] }}-{{item['oldpkg']['version']['rel']}}","{{ item['oldpkg']['git_path'] }}","{{ item['oldpkg']['commit_id'] }}" + {%- endfor -%} +{%- endif %} + +Modified Packages,{% if diff['modified'] %} +Package,Git Path,CommitId,Committer,CommitDate,CommitLog + {%- for item in diff['modified'] -%} + {%- if item['oldpkg']['git_path'] != item['newpkg']['git_path'] -%} + {%- set git_path = item['oldpkg']['git_path'] + "-" + item['newpkg']['git_path'] -%} + {%- else -%} + {%- set git_path = item['oldpkg']['git_path'] -%} + {%- endif %} + {%- for commit_item in item['commit_log'] %} +"{{ item['oldpkg']['name'] }}","{{ git_path }}","{{commit_item['commit_id']}}","{{commit_item['committer']}}","{{commit_item['commit_date']}}","{{commit_item['commit_log']['raw']}}" + {%- endfor -%} + {%- endfor -%} +{%- endif %} + diff --git a/snapdiff/templates/commit_diff.html b/snapdiff/templates/commit_diff.html new file mode 100644 index 0000000..40d25eb --- /dev/null +++ b/snapdiff/templates/commit_diff.html @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + +
+
+ +
+
+

+ Difference and Changelogs Overview +

+ Go back...
+ csv format...
+

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

+

Highlights

+ + + + {% set git_web = gitweb %} + {% if diff['added'] %} + + + + + {% for item in diff['added'] %} + {% for commit_item in item['commit_log'] %} + + + + + + + + + {% endfor %} + {% endfor %} + + {% endif %} + + {% if diff['removed'] %} + + + + + {% for item in diff['removed'] %} + + + + + + + {% endfor %} + + {% endif %} + + {% if diff['modified'] %} + + + + + {% for item in diff['modified'] %} + {% for commit_item in item['commit_log'] %} + + + + + + + + + {% endfor %} + {% endfor %} + + {% endif %} +

Added Packages

PackageGit PathCommitIdCommitterCommitDateCommitLog
{{ item['newpkg']['name'] }}{{ item['newpkg']['git_path'] }}{{ commit_item['commit_id'] }}{{ commit_item['committer'] }}{{ commit_item['commit_date'] }}{{ commit_item['commit_log']['html'] }}

Removed Packages

PackageVersionGitPathCommitId
{{ item['oldpkg']['name'] }}{{ item['oldpkg']['version']['ver'] }}-{{item['oldpkg']['version']['rel']}}{{ item['oldpkg']['git_path'] }}{{ item['oldpkg']['commit_id'] }}

Modified Packages

PackageGit PathCommitIdCommitterCommitDateCommitLog
{{ item['oldpkg']['name'] }} + + {{ commit_item['commit_id'] }}{{ commit_item['committer'] }}{{ commit_item['commit_date'] }}{{ commit_item['commit_log']['html'] }}
+ + + + -- 2.34.1