opexxx

ova2free.py

Feb 7th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Copyright (c) 2011 Daniel Poelzleithner <poelzleithner @ b1-systems.de>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. #
  9. #   * Redistributions of source code must retain the above copyright notice,
  10. #     this list of conditions and the following disclaimer.
  11. #   * Redistributions in binary form must reproduce the above copyright notice,
  12. #     this list of conditions and the following disclaimer in the documentation
  13. #     and/or other materials provided with the distribution.
  14. #   * Neither the name of Pioneers of the Inevitable, Songbird, nor the names
  15. #     of its contributors may be used to endorse or promote products derived
  16. #     from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  22. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  
  29.  
  30. from optparse import OptionParser
  31. import subprocess
  32. import os, os.path
  33. import sys
  34.  
  35. VERSION = "0.1"
  36.  
  37. usage = "usage: %prog [options] ofafile\nConvert applience and virtual images into useful ones"
  38. parser = OptionParser(usage=usage, version=VERSION)
  39. parser.add_option("-f", "--format", dest="format",
  40.                   help="convert info given format (default: qcow2)", default="qcow2")
  41.  
  42. (options, args) = parser.parse_args()
  43.  
  44.  
  45. def convert_img(path, target_format="qcow2"):
  46.     """
  47.    convert img to target format
  48.    """
  49.     dir = os.path.dirname(path)
  50.     basename = os.path.basename(path)
  51.     base,ext = os.path.splitext(basename)
  52.    
  53.     print "convert %s into useful format" %path
  54.  
  55.     # generate itermediet vdi
  56.     if ext.lower() != ".vdi":
  57.         vdiout = os.path.join(dir, base + ".vdi")
  58.         if os.path.exists(vdiout):
  59.             print "file exists, skipping"
  60.         else:
  61.             print "converting to VDI (intermediat): %s" %vdiout
  62.             subprocess.check_call(["VBoxManage", "clonehd", path, "--format", "VDI", vdiout])
  63.  
  64.     if target_format.lower() != "vdi":
  65.         target_out = os.path.join(dir, base + "." + target_format)
  66.         print "converting to target format: %s" %target_out
  67.         subprocess.check_call(["qemu-img", "convert", "-f", "vdi", vdiout, "-O", target_format, target_out])
  68.         return target_out
  69.     else:
  70.         return vdiout
  71.  
  72. def unpack_ofa(path):
  73.     nd = os.path.join(os.getcwd(), os.path.dirname(path), os.path.splitext(os.path.basename(path))[0])
  74.     print "create target directory", nd
  75.     rpath = os.path.join(os.getcwd(), path)
  76.     if not os.path.exists(nd):
  77.         os.mkdir(nd)
  78.     print "unpack applience"
  79.     subprocess.check_call(["tar", "xvf", rpath], cwd=nd)
  80.     files = os.listdir(nd)
  81.     for file in files:
  82.         x = os.path.splitext(file)
  83.         if len(x) < 2: continue
  84.         if x[-1].lower() == '.vmdk':
  85.             convert_img(os.path.join(nd, file), options.format)
  86.  
  87.  
  88. def main():
  89.     if len(args) != 1:
  90.         parser.print_usage()
  91.         sys.exit(1)
  92.    
  93.     try:
  94.        ext = os.path.splitext(args[0])[1]
  95.     except IndexError:
  96.         print "can't determine type of image"
  97.         sys.exit(2)
  98.  
  99.     print ext
  100.     if ext == ".ofa" or ext == ".ova":
  101.        unpack_ofa(args[0])
  102.  
  103.     elif ext == ".vdi":
  104.        convert_img(args[0], options.format)
  105.  
  106.     else:
  107.         print "don't know this image type"
  108.         sys.exit(3)
  109.        
  110.  
  111.    
  112.  
  113.  
  114. if __name__ == '__main__':
  115.     main()
Add Comment
Please, Sign In to add comment