Add local path support
authorLingchao Xin <lingchaox.xin@intel.com>
Fri, 29 Nov 2013 09:19:58 +0000 (17:19 +0800)
committerLingchao Xin <lingchaox.xin@intel.com>
Fri, 29 Nov 2013 09:29:35 +0000 (17:29 +0800)
Now snapdiff supports a local repo parsing

Change-Id: Ifde1bdc28f8c0ea6d30fc0a75ec4843ffbf6b66f

snapdiff/repo.py

index 8e0a59fc067f9131eedec0e37baf22e435b03668..6721a594f1c61bdda54c17dc3abc101cacde3e00 100644 (file)
@@ -3,6 +3,7 @@ from .render import output_html
 
 import gzip
 import json
+import mimetypes
 import os
 import requests
 from StringIO import StringIO
@@ -24,20 +25,34 @@ def _get_primary_md(url):
         if item.type == 'primary':
             href = item.location.href
             if href:
-                return _download(url + href)
+                return _download(url + '/' + href)
 
     raise RepoError('Get repo: {0} primary metadata failed ...'.format(url))
 
 def _download(url):
     """Return given url's content"""
-    response = requests.get(url)
-
-    if response.status_code == 200:
-        if response.headers['content-type'] == 'application/x-gzip':
-            gzf = gzip.GzipFile(fileobj=StringIO(response.content))
-            return gzf.read()
-        elif response.headers['content-type'] == 'text/xml':
-            return response.content
+    # a very simple judgement :)
+    if url.startswith('http') or url.startswith('https'):
+        response = requests.get(url)
+
+        if response.status_code == 200:
+            if response.headers['content-type'] == 'application/x-gzip':
+                gzf = gzip.GzipFile(fileobj=StringIO(response.content))
+                return gzf.read()
+            elif response.headers['content-type'] == 'text/xml':
+                return response.content
+    else:
+        # if url is a local path
+        _type = mimetypes.guess_type(url)
+        try:
+            if _type[0] == 'text/xml' and _type[1] == 'gzip':
+                with gzip.open(url) as gzf:
+                    return gzf.read()
+            elif _type[0] == 'text/xml' and _type[1] is None:
+                with open(url, 'rb') as xml:
+                    return xml.read()
+        except IOError, err:
+            print(err)
 
 
 class Repo(object):