Snapdiff is used to generate image and repo diffs. You can use it as a module:
>>> import snapdiff
- >>> snapdiff.diff_to_JSON('old url', 'new url', style='repo')
- >>> snapdiff.diff_to_HTML('old url', 'new url', style='image')
+ >>> snapdiff.diff_to_json('old url', 'new url', style='repo')
+ >>> snapdiff.diff_to_html('old url', 'new url', style='image')
or use it directly:
snap-diff [-h] [--json] [-t] old new
__title__ = 'python-snapdiff'
__version__ = '0.1'
+from itertools import izip_longest
import json
import os
import shutil
from .repo import Repo
-def diff_to_JSON(old_url, new_url, style='repo'):
+def diff_to_json(old_url, new_url, style='repo'):
"""Output diffs' json format"""
if not old_url or not new_url:
package_names = set(old.__dict__.keys() + new.__dict__.keys())
def _pair_old_new():
+ """Generate old and new pkg pair"""
for name in package_names:
if old[name] is None:
for pkg in new[name]:
yield (old_pkg, new_pkg)
old[name].remove(old_pkg)
new[name].remove(new_pkg)
- for pair in map(None, old[name], new[name]):
+ for pair in izip_longest(old[name], new[name]):
yield pair
for old_pkg, new_pkg in _pair_old_new():
if old_pkg is None:
return json.dumps(obj, indent=4)
-def diff_to_HTML(old_url, new_url, style='repo'):
+def diff_to_html(old_url, new_url, style='repo'):
"""Output diffs' html format"""
- json_obj = diff_to_JSON(old_url, new_url, style)
+ json_obj = diff_to_json(old_url, new_url, style)
if json_obj is None:
return
context = {
'old': {'url': old_url,
- 'name': re.search('\w*_\d{8}\.\d*', old_url).group(0)},
+ 'name': re.search(r'\w*_\d{8}\.\d*', old_url).group(0)},
'new': {'url': new_url,
- 'name': re.search('\w*_\d{8}\.\d*', new_url).group(0)},
+ 'name': re.search(r'\w*_\d{8}\.\d*', new_url).group(0)},
'diff': data['diff'],
}
if style == 'repo':
- return output_html('diff.html', **context)
+ return output_html('diff.html', **context) # pylint: disable=W0142
elif style == 'image':
- return output_html('image_diff.html', **context)
+ return output_html('image_diff.html', **context) # pylint: disable=W0142
def diff_to_dist(old_url, new_url, dist_path, style='repo', diff_name='diff'):
"""create a dist-dir of diffs, contains html and css"""
static_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'static')
- output_html = diff_to_HTML(old_url, new_url, style)
+ output = diff_to_html(old_url, new_url, style)
if not os.path.exists(dist_path):
os.makedirs(dist_path)
- for root, dirs, files in os.walk(static_dir):
+ for root, _dirs, files in os.walk(static_dir):
for filename in files:
if filename.endswith('.css'):
if not os.path.exists(os.path.join(dist_path, 'css')):
shutil.copy(os.path.join(root, filename),
os.path.join(dist_path, 'img', filename))
- with open(os.path.join(dist_path, '%s.html' % diff_name), 'w') as fp:
- fp.write(output_html)
+ with open(os.path.join(dist_path, '%s.html' % diff_name), 'w') as fname:
+ fname.write(output)
return dist_path
+
+"""This module handles image related"""
+
import difflib
import glob
import os
def _get_file(url, suffix='.ks'):
+ """Return (name, content) of ks file"""
if url.startswith('http') or url.startswith('https'):
response = requests.get(url + '/MD5SUMS')
if response.status_code == 200:
return hunks
def _parse_sections(stream):
+ """Parse ks diff sections"""
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)
return sections
def ks_diff(old, new):
+ """Return a ks file diff dict"""
ks_old, ks_new = _get_file(old), _get_file(new)
diff_list = []
- stream = difflib.unified_diff(ks_old[1], ks_new[1], fromfile=ks_old[0], tofile=ks_new[0])
+ 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):
+ """Return an image packages info dict"""
content = _get_file(url, suffix='.packages')
packages_info = JsonDict()
+
+"""This module renders html templates"""
+
import os
from jinja2 import Environment, FileSystemLoader
+
+"""This module handles repo related"""
+
import gzip
import mimetypes
from StringIO import StringIO
@property
def packages(self):
+ """Return a packages info dict"""
packages_info = JsonDict()
for package in self._primary.package:
+
+"""This module contains usefull tools used frequently"""
+
import re
import xml.sax.handler
def xml2obj(src):
+ # pylint: disable=C0301, C0111, R0903, R0912, W0212, C0103, W0231
"""
A simple function to converts XML data into native Python object,
from http://goo.gl/Ymc6Nl, Licensed under the PSF License.
#!/usr/bin/env python
+"""snap-diff is an extra tool used to test python-snapdiff functions or
+provide as a command line tool"""
+
import argparse
import sys
def main(argv):
+ """The main body"""
description = 'Diff two repos with different urls'
parser = argparse.ArgumentParser(description=description)
parser.add_argument(dest='old', help='old repo')
help="which diff you want(default is 'repo')")
group = parser.add_mutually_exclusive_group()
group.add_argument('--json', help='output json diff', action='store_true')
- group.add_argument('-d', dest='directory', help="output html diff into the directory")
+ group.add_argument('-d', dest='directory', \
+ help='output html diff into directory')
parser.add_argument('-n', dest='name', \
help="give a name to diff html(effective to '-d' option only)")
args = parser.parse_args(argv)
if args.directory and args.name:
- snapdiff.diff_to_dist(args.old, args.new, args.directory, style=args.type, diff_name=args.name)
+ snapdiff.diff_to_dist(args.old, args.new, args.directory, \
+ style=args.type, diff_name=args.name)
elif args.directory:
- snapdiff.diff_to_dist(args.old, args.new, args.directory, style=args.type)
+ snapdiff.diff_to_dist(args.old, args.new, args.directory, \
+ style=args.type)
elif args.json:
- print snapdiff.diff_to_JSON(args.old, args.new, style=args.type)
+ print snapdiff.diff_to_json(args.old, args.new, style=args.type)
else:
- print snapdiff.diff_to_HTML(args.old, args.new, style=args.type)
+ print snapdiff.diff_to_html(args.old, args.new, style=args.type)
if __name__ == '__main__':
try: