Add ks_diff of image diff
authorLingchao Xin <lingchaox.xin@intel.com>
Mon, 2 Dec 2013 10:30:00 +0000 (18:30 +0800)
committerLingchao Xin <lingchaox.xin@intel.com>
Tue, 3 Dec 2013 03:03:49 +0000 (11:03 +0800)
Support both remote and local path diff

Change-Id: I5c93663b0340b373e03e35ce83da067e03b152be

setup.py
snapdiff/__init__.py
snapdiff/image.py [new file with mode: 0644]

index e4aaaa498d9056a7f84d733e8ce7435ac92aa387..b2b682db9c4083db44fb73bf1236b3ea4e7b9394 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -16,7 +16,8 @@ setup(name=snapdiff.__title__,
       scripts=['tools/repo-diff',],
       packages=['snapdiff',],
       include_package_data=True,
-      install_requires=['Jinja2>=2.6', 'requests>=2.0.1'],
+      install_requires=['Jinja2>=2.6', 'requests>=2.0.1',
+          'beautifulsoup4>=4.3.2'],
       zip_safe=False,
 )
 
index afe3d8ef1bf134ea1f87d54a067b747a7c22d171..9e206795fa74f83cad205c13d558fed20896ec4e 100644 (file)
@@ -15,4 +15,5 @@ __title__ = 'python-snapdiff'
 __version__ = '0.1'
 
 from .repo import *
+from .image import *
 
diff --git a/snapdiff/image.py b/snapdiff/image.py
new file mode 100644 (file)
index 0000000..1488aa9
--- /dev/null
@@ -0,0 +1,40 @@
+from bs4 import BeautifulSoup
+import difflib
+import glob
+import os
+import requests
+from StringIO import StringIO
+
+
+def _get_ks(url):
+    if url.startswith('http') or url.startswith('https'):
+        response = requests.get(url)
+        bs = BeautifulSoup(response.content)
+        tags = bs.findAll('a')
+        for tag in tags:
+            if '.ks' in tag.get('href'):
+                ks = tag.get('href') # get ks
+                ks_response = requests.get(os.path.join(url, ks))
+                if ks_response.status_code == 200:
+                    # use StringIO to keep wrapped lines
+                    return ks, StringIO(ks_response.content).readlines()
+                break
+    else:
+        kss = glob.glob(os.path.join(url, '*.ks'))
+        if kss:
+            ks = kss[0]
+            try:
+                with open(ks, 'rb') as _ks:
+                    return ks, _ks.readlines()
+            except IOError, err:
+                print err
+
+    return '', ''
+
+def ks_diff(old, new):
+    ks_old, ks_new = _get_ks(old), _get_ks(new)
+
+    for line in difflib.unified_diff(ks_old[1], ks_new[1], fromfile=ks_old[0],
+            tofile=ks_new[0]):
+        yield line
+