Remove python-losetup dependency
authorNikos Skalkotos <skalkoto@grnet.gr>
Fri, 9 Mar 2012 15:26:29 +0000 (17:26 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Fri, 9 Mar 2012 15:26:29 +0000 (17:26 +0200)
The package is buggy and we can use pbs to do the same thing.

image_creator/disk.py
image_creator/main.py
image_creator/util.py [new file with mode: 0644]
setup.py

index 6d0ad1f..809a1a3 100644 (file)
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 
 #!/usr/bin/env python
 
-import losetup
+from image_creator.util import get_command
+from clint.textui import progress
+
 import stat
 import os
 import tempfile
 import stat
 import os
 import tempfile
@@ -9,33 +11,14 @@ import re
 import sys
 import guestfs
 
 import sys
 import guestfs
 
-import pbs
-from pbs import dd
-from clint.textui import progress
-
 
 class DiskError(Exception):
     pass
 
 
 class DiskError(Exception):
     pass
 
-
-def find_sbin_command(command, exception):
-    search_paths = ['/usr/local/sbin', '/usr/sbin', '/sbin']
-    for fullpath in map(lambda x: "%s/%s" % (x, command), search_paths):
-        if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
-            return pbs.Command(fullpath)
-        continue
-    raise exception
-
-
-try:
-    from pbs import dmsetup
-except pbs.CommandNotFound as e:
-    dmsetup = find_sbin_command('dmsetup', e)
-
-try:
-    from pbs import blockdev
-except pbs.CommandNotFound as e:
-    blockdev = find_sbin_command('blockdev', e)
+dd = get_command('dd')
+dmsetup = get_command('dmsetup')
+losetup = get_command('losetup')
+blockdev = get_command('blockdev')
 
 
 class Disk(object):
 
 
 class Disk(object):
@@ -57,10 +40,10 @@ class Disk(object):
         self._cleanup_jobs.append((job, args))
 
     def _losetup(self, fname):
         self._cleanup_jobs.append((job, args))
 
     def _losetup(self, fname):
-        loop = losetup.find_unused_loop_device()
-        loop.mount(fname)
-        self._add_cleanup(loop.unmount)
-        return loop.device
+        loop = losetup('-f', '--show', fname)
+        loop = loop.strip() # remove the new-line char
+        self._add_cleanup(losetup, '-d', loop)
+        return loop
 
     def _dir_to_disk(self):
         raise NotImplementedError
 
     def _dir_to_disk(self):
         raise NotImplementedError
index 3bdb2e5..3b25bc5 100644 (file)
 from image_creator import get_os_class
 from image_creator import __version__ as version
 from image_creator.disk import Disk
 from image_creator import get_os_class
 from image_creator import __version__ as version
 from image_creator.disk import Disk
+from image_creator.util import get_command
+
 import sys
 import os
 import optparse
 import sys
 import os
 import optparse
-from pbs import dd
+
+dd = get_command('dd')
 
 
 class FatalError(Exception):
 
 
 class FatalError(Exception):
@@ -123,7 +126,6 @@ def main():
 
         size = options.shrink and dev.shrink() or dev.size()
         metadata['size'] = str(size // 2 ** 20)
 
         size = options.shrink and dev.shrink() or dev.size()
         metadata['size'] = str(size // 2 ** 20)
-
         dd('if=%s' % dev.device,
             'of=%s/%s.%s' % (options.outdir, options.name, 'diskdump'),
             'bs=4M', 'count=%d' % ((size + 1) // 2 ** 22))
         dd('if=%s' % dev.device,
             'of=%s/%s.%s' % (options.outdir, options.name, 'diskdump'),
             'bs=4M', 'count=%d' % ((size + 1) // 2 ** 22))
diff --git a/image_creator/util.py b/image_creator/util.py
new file mode 100644 (file)
index 0000000..63346c8
--- /dev/null
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+
+import pbs
+
+def get_command(command):
+    def find_sbin_command(command, exception):
+        search_paths = ['/usr/local/sbin', '/usr/sbin', '/sbin']
+        for fullpath in map(lambda x: "%s/%s" % (x, command), search_paths):
+            if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
+                return pbs.Command(fullpath)
+        raise exception
+
+    try:
+        return pbs.__getattr__(command)
+    except pbs.CommadNotFount as e:
+        return find_sbin_command(command, e)
index 345c8c3..3553e3e 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -47,7 +47,7 @@ setup(
     license='BSD',
     packages=['image_creator'],
     include_package_data=True,
     license='BSD',
     packages=['image_creator'],
     include_package_data=True,
-    install_requires=['losetup', 'pbs', 'clint'],
+    install_requires=['pbs', 'clint'],
     entry_points={
         'console_scripts': ['snf-image-creator = image_creator.main:main']
     }
     entry_points={
         'console_scripts': ['snf-image-creator = image_creator.main:main']
     }