Shahar Havivi has uploaded a new change for review.

Change subject: v2v: ova should support zip and extracted directory formats
......................................................................

v2v: ova should support zip and extracted directory formats

Change-Id: I494b88709c4c23fd39690b589eff1134e74f81ba
Bug-Url: https://bugzilla.redhat.com/1277879
Signed-off-by: Shahar Havivi <shah...@redhat.com>
---
M vdsm/v2v.py
1 file changed, 49 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/29/48129/1

diff --git a/vdsm/v2v.py b/vdsm/v2v.py
index 997979e..8419f47 100644
--- a/vdsm/v2v.py
+++ b/vdsm/v2v.py
@@ -33,6 +33,7 @@
 import signal
 import threading
 import xml.etree.ElementTree as ET
+import zipfile
 
 import libvirt
 
@@ -63,6 +64,12 @@
 ImportProgress = namedtuple('ImportProgress',
                             ['current_disk', 'disk_count', 'description'])
 DiskProgress = namedtuple('DiskProgress', ['progress'])
+
+
+class OVA_FORMAT:
+    DIR = 'dir'
+    TAR = 'tar'
+    ZIP = 'zip'
 
 
 class STATUS:
@@ -733,7 +740,49 @@
         params['networks'].append(i)
 
 
+def _get_ova_file_type(ova_path):
+    if os.path.isdir(ova_path):
+        return OVA_FORMAT.DIR
+    elif zipfile.is_zipfile(ova_path):
+        return OVA_FORMAT.ZIP
+    return OVA_FORMAT.TAR
+
+
 def _read_ovf_from_ova(ova_path):
+    '''
+       virt-v2v support ova in tar, zip formats as well as
+       extracted directory
+    '''
+    ova_format = _get_ova_file_type(ova_path)
+    if ova_format == OVA_FORMAT.DIR:
+        return _read_ovf_from_ova_dir(ova_path)
+    elif ova_format == OVA_FORMAT.TAR:
+        return _read_ovf_from_tar_ova(ova_path)
+    else:
+        return _read_ovf_from_zip_ova(ova_path)
+
+
+def _read_ovf_from_ova_dir(ova_path):
+    files = os.listdir(ova_path)
+    for f in files:
+        if '.ovf' in f.lower():
+            f = open(os.path.join(ova_path, f), 'r')
+            with closing(f):
+                return ''.join(f.readlines())
+    raise ClientError('OVA directory %s does not contain ovf file' % ova_path)
+
+
+def _read_ovf_from_zip_ova(ova_path):
+    fh = file(ova_path, 'rb')
+    with closing(fh):
+        zf = zipfile.ZipFile(fh)
+        for name in zf.namelist():
+            if '.ovf' in name:
+                return zf.read(name)
+    raise ClientError('OVA does not contains file with .ovf suffix')
+
+
+def _read_ovf_from_tar_ova(ova_path):
     # FIXME: change to tarfile package when support --to-stdout
     cmd = ['/usr/bin/tar', 'xf', ova_path, '*.ovf', '--to-stdout']
     rc, output, error = execCmd(cmd)


-- 
To view, visit https://gerrit.ovirt.org/48129
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I494b88709c4c23fd39690b589eff1134e74f81ba
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Shahar Havivi <shav...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to