Commits

Rodrigo Bistolfi  committed d416249

Install media finding refactoring

  • Participants
  • Parent commits 76f1255

Comments (0)

Files changed (1)

File vinstall/backend/installmedia.py

 from utils import mount, mountiso, umount
 
 import os
-import time
-import sys
 import ConfigParser
 from itertools import chain
 
         """Find install media in devices and partitions
 
         """
-        dev_mountpoint = "/tmp/DEVMNT"
-        iso_mountpoint = "/tmp/ISOMNT"
-        try:
-            os.mkdir(dev_mountpoint)
-            os.mkdir(iso_mountpoint)
-        except OSError as e:
-            if e.errno == 17:
-                pass
-            else:
-                raise e
-        cdroms = ( media.Partition(i) for i in media.list_cdroms() )
-        partitions = media.Partition.all()
-        #XXX refactor for DRY
-        for cdrom in cdroms:
-            needs_umount = False
-            if not cdrom.is_mounted():
-                cdrom.mount(dev_mountpoint)
-                needs_umount = True
-            if is_install_media(cdrom.mountpoint):
-                config = parse_config(cdrom.mountpoint)
-                yield cls(cdrom.device_path, config)
-            if needs_umount:
-                cdrom.umount()
-        for partition in partitions:
-            if not partition.is_mounted():
-                partition.mount(partition.mountpoint)
-                needs_umount = True
-            if is_install_media(partition.mountpoint):
-                config = parse_config(partition.mountpoint)
-                yield cls(partition.mountpoint, config)
-            else:
-                for f in os.listdir(partition.mountpoint):
-                    if os.path.isfile(os.path.join(partition.mountpoint, f)) and f.endswith(".iso"):
-                        isopath = os.path.join(partition.mountpoint, f)
-                        mountiso(isopath, iso_mountpoint)
-                        if is_install_media(iso_mountpoint):
-                            config = parse_config(iso_mountpoint)
-                            yield cls(isopath, config)
-                        umount(iso_mountpoint)
-            if needs_umount:
-                partition.umount()
+        finders = [ CdRomInstallMediaFinder(), PartitionInstallMediaFinder() ]
+        return chain(*[i.find() for i in finders])
 
     def __init__(self, path, config):
         """Expose install media base dir and the config object
         return self.path.endswith("iso")
 
 
+class InstallMediaFinder(object):
+    "Interface for media finder objects"
+
+    def find(self):
+        "Find install media. Return an iterable containing InstallMedia objects"
+
+
+class CdRomInstallMediaFinder(InstallMediaFinder):
+    "Find install media in cdrom devices"
+
+    def __init__(self):
+        self.devices = (media.Partition(i) for i in media.list_cdroms())
+        self.mountpoint = "/tmp/DEVMNT"
+        create_mountpoint(self.mountpoint)
+
+    def find(self):
+        "Find install media in cdrom devices"
+        for cdrom in self.devices:
+            needs_umount = False
+            if not cdrom.is_mounted():
+                cdrom.mount(self.mountpoint)
+                needs_umount = True
+            if is_install_media(cdrom.mountpoint):
+                config = parse_config(cdrom.mountpoint)
+                yield InstallMedia(cdrom.device_path, config)
+            if needs_umount:
+                cdrom.umount()
+
+
+class PartitionInstallMediaFinder(InstallMediaFinder):
+    "Find install media in existing partitions"
+
+    def __init__(self):
+        self.partitions = media.Partition.all()
+
+    def find(self):
+        for partition in self.partitions:
+            needs_umount = False
+            if not partition.is_mounted():
+                partition.mount(partition.mountpoint)
+                needs_umount = True
+            if is_install_media(partition.mountpoint):
+                config = parse_config(partition.mountpoint)
+                yield InstallMedia(partition.mountpoint, config)
+            else:
+                isofinder = ISOInstallMediaFinder(partition.mountpoint)
+                for im in isofinder.find():
+                    yield im
+            if needs_umount:
+                partition.umount()
+
+
+class ISOInstallMediaFinder(InstallMediaFinder):
+    "Find install media in ISO files"
+
+    def __init__(self,root_dir):
+        self.root_dir = root_dir
+        self.mountpoint = "/tmp/ISOMNT"
+        create_mountpoint(self.mountpoint)
+
+    def find(self):
+        for f in os.listdir(self.root_dir):
+            if os.path.isfile(os.path.join(self.root_dir, f)) and f.endswith(".iso"):
+                isopath = os.path.join(self.root_dir, f)
+                mountiso(isopath, self.mountpoint)
+                if is_install_media(self.mountpoint):
+                    config = parse_config(self.mountpoint)
+                    yield InstallMedia(isopath, config)
+                umount(self.mountpoint)
+
+
 def parse_config(mnt):
     """ Reads VINSTALL.INI
 
     config = "veclinux/VINSTALL.INI"
     return os.path.exists(os.path.join(mnt,config))
 
+
+def create_mountpoint(location):
+    "Create mountpoint if it doesnt exist"
+    try:
+        os.mkdir(location)
+    except OSError as e:
+        if e.errno == 17:
+            pass
+        else:
+            raise e