Skip to content

Instantly share code, notes, and snippets.

@enolfc
Created October 30, 2015 11:12
Show Gist options
  • Save enolfc/101c65d6464999cbcbf4 to your computer and use it in GitHub Desktop.
Save enolfc/101c65d6464999cbcbf4 to your computer and use it in GitHub Desktop.
Get OVF disk
from six.moves.urllib import parse
from lxml import etree
SPECS = {
'http://www.vmware.com/interfaces/specifications/vmdk.html': 'vmdk',
'https://people.gnome.org/~markmc/qcow-image-format.html': 'qcow',
}
def get_ovf_disk(ovf):
tree = etree.parse("file.ovf")
root = tree.getroot()
ovf_ns = root.nsmap['ovf']
id_attr = '{%s}id' % ovf_ns
href_attr = '{%s}href' % ovf_ns
files = {f.get(id_attr): f.get(href_attr) for f in
root.findall('ovf:References/ovf:File', root.nsmap)}
# we do not care about more than one disk
disk = root.find('ovf:DiskSection/ovf:Disk', root.nsmap)
if disk is not None:
format_attr = '{%s}format' % ovf_ns
fileref_attr = '{%s}fileRef' % ovf_ns
ovf_format = disk.get(format_attr)
if not ovf_format:
raise Exception("Expecting some format!")
(format_url, _) = parse.urldefrag(ovf_format)
try:
disk_format = SPECS[format_url]
except KeyError:
raise Exception("Unknown format!")
try:
disk_file = files[disk.get(fileref_attr)]
except KeyError:
raise Exception("Unknown disk!")
return (disk_format, disk_file)
return None, None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment