[Kimchi-devel] [PATCH 4/6] Move ISO path validation to IsoImage()

Aline Manera alinefm at linux.vnet.ibm.com
Mon Jan 6 15:05:53 UTC 2014


From: Aline Manera <alinefm at br.ibm.com>

That way we will have a single point to check ISO path validation.
And the user doesn't need to care about it as IsoImage() will check if the ISO
path is local, remote or invalid.

Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 src/kimchi/isoinfo.py |   42 ++++++++++++++++--------------------------
 src/kimchi/utils.py   |   12 ++++++++++++
 2 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/src/kimchi/isoinfo.py b/src/kimchi/isoinfo.py
index 7d919a0..f93ff0e 100644
--- a/src/kimchi/isoinfo.py
+++ b/src/kimchi/isoinfo.py
@@ -29,7 +29,7 @@ import urllib2
 
 
 from kimchi.exception import IsoFormatError
-from kimchi.utils import kimchi_log
+from kimchi.utils import check_url_path, kimchi_log
 
 iso_dir = [
     ##
@@ -134,13 +134,22 @@ class IsoImage(object):
     EL_TORITO_VALIDATION_ENTRY = struct.Struct("=BBH24sHBB")
     EL_TORITO_BOOT_ENTRY = struct.Struct("=BBHBBHL20x")
 
-    def __init__(self, path, remote = False):
+    def __init__(self, path):
         self.path = path
+        self.remote = self._is_iso_remote()
         self.volume_id = None
         self.bootable = False
-        self.remote = remote
         self._scan()
 
+    def _is_iso_remote(self):
+        if os.path.isfile(self.path):
+            return False
+
+        if check_url_path(self.path):
+            return True
+
+        raise IsoFormatError('ISO %s does not exist' % self.path)
+
     def _unpack(self, s, data):
         return s.unpack(data[:s.size])
 
@@ -246,9 +255,9 @@ class Matcher(object):
         return self.lastmatch.group(num)
 
 
-def _probe_iso(fname, remote = False):
+def _probe_iso(fname):
     try:
-        iso = IsoImage(fname, remote)
+        iso = IsoImage(fname)
     except Exception, e:
         kimchi_log.warning("probe_iso: Error processing ISO image: %s\n%s" %
                            (fname, e))
@@ -302,35 +311,16 @@ def probe_iso(status_helper, params):
                     update_result(iso, ret)
                 except:
                     continue
-    elif os.path.isfile(loc):
-        ret = _probe_iso(loc, False)
-        update_result(loc, ret)
     else:
-        ret = _probe_iso(loc, True)
+        ret = _probe_iso(loc)
         update_result(loc, ret)
 
     if status_helper != None:
         status_helper('', True)
 
-def _check_url_path(path):
-    try:
-        code = urllib2.urlopen(path).getcode()
-        if code != 200:
-            return False
-    except (urllib2.HTTPError, ValueError):
-        return False
-
-    return True
 
 def probe_one(iso):
-    if os.path.isfile(iso):
-        remote = False
-    elif _check_url_path(iso):
-        remote = True
-    else:
-        raise IsoFormatError('ISO %s does not exist' % iso)
-
-    return _probe_iso(iso, remote)
+    return _probe_iso(iso)
 
 
 if __name__ == '__main__':
diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
index b6d84fd..af245c6 100644
--- a/src/kimchi/utils.py
+++ b/src/kimchi/utils.py
@@ -23,6 +23,7 @@
 
 import cherrypy
 import os
+import urllib2
 
 
 from cherrypy.lib.reprconf import Parser
@@ -84,3 +85,14 @@ def import_class(class_path):
 
 def import_module(module_name):
     return __import__(module_name, globals(), locals(), [''])
+
+
+def check_url_path(path):
+    try:
+        code = urllib2.urlopen(path).getcode()
+        if code != 200:
+            return False
+    except (urllib2.URLError, ValueError):
+        return False
+
+    return True
-- 
1.7.10.4




More information about the Kimchi-devel mailing list