[et-mgmt-tools] [PATCH 06 of 11] Format detection for .vmx
john.levon at sun.com
john.levon at sun.com
Mon Jul 7 22:51:36 UTC 2008
# HG changeset patch
# User john.levon at sun.com
# Date 1215470433 25200
# Node ID a8f5d5be978f0237f41c35275936863c2787a73b
# Parent 4528daf7a40728f906e1843b8368198ce9d438c5
Format detection for .vmx
Auto-detect vmx format.
Signed-off-by: John Levon <john.levon at sun.com>
diff --git a/virt-convert b/virt-convert
--- a/virt-convert
+++ b/virt-convert
@@ -34,6 +34,7 @@
# use qemu-img info to verify the disk type
# add some unit tests
# input/output/detection for all formats
+# add --formats?
#
import sys
@@ -62,8 +63,7 @@ def parse_args():
opts.add_option("-d", "--debug", action="store_true", dest="debug",
help=("Print debugging information"))
opts.add_option("-i", "--input-format", action="store",
- dest="input_format", default="vmx",
- help=("Input format, e.g. 'vmx'"))
+ dest="input_format", help=("Input format, e.g. 'vmx'"))
opts.add_option("-o", "--output-format", action="store",
dest="output_format", default="virt-image",
help=("Output format, e.g. 'virt-image'"))
@@ -78,41 +78,45 @@ def parse_args():
if len(args) > 2:
opts.error(("Too many arguments provided"))
+ if len(args) == 1:
+ options.output_file = None
+ options.output_dir = None
+ if os.path.isdir(args[0]):
+ options.output_dir = args[0]
+ elif os.path.isdir(args[1]) or args[1].endswith("/"):
+ options.output_file = None
+ options.output_dir = args[1]
+ else:
+ options.output_file = args[1]
+ options.output_dir = os.path.dirname(os.path.realpath(args[1]))
+
+ if options.output_format not in vmconfig.formats():
+ opts.error(("Unknown output format \"%s\"" % options.output_format))
+ if options.output_format not in vmconfig.output_formats():
+ opts.error(("No output handler for format \"%s\""
+ % options.output_format))
+
+ if not options.input_format:
+ try:
+ (args[0], options.input_format) = vmconfig.find_input(args[0])
+ except StandardError, e:
+ opts.error("Couldn't determine input format for \"%s\": %s" %
+ (args[0], e.message))
+ sys.exit(1)
+
if options.input_format not in vmconfig.formats():
opts.error(("Unknown input format \"%s\"" % options.input_format))
if options.input_format not in vmconfig.input_formats():
opts.error(("No input handler for format \"%s\""
% options.input_format))
- if options.output_format not in vmconfig.formats():
- opts.error(("Unknown output format \"%s\"" % options.output_format))
- if options.output_format not in vmconfig.output_formats():
- opts.error(("No output handler for format \"%s\""
- % options.output_format))
-
if os.path.isdir(args[0]):
- vmx_files = [x for x in os.listdir(args[0]) if x.endswith(".vmx") ]
- if (len(vmx_files)) == 0:
- opts.error(("No VM definition file was found in %s" % args[0]))
- if (len(vmx_files)) > 1:
- opts.error(("Too many .vmx definitions found in %s" % args[0]))
- options.input_file = os.path.join(args[0], vmx_files[0])
+ (options.input_file, _) = vmconfig.find_input(args[0],
+ options.input_format)
options.input_dir = args[0]
else:
options.input_file = args[0]
options.input_dir = os.path.dirname(os.path.realpath(args[0]))
-
- if len(args) == 1:
- options.output_file = None
- options.output_dir = None
- if os.path.isdir(args[0]):
- options.output_dir = options.input_dir
- elif os.path.isdir(args[1]) or args[1].endswith("/"):
- options.output_file = None
- options.output_dir = args[1]
- else:
- options.output_file = args[1]
- options.output_dir = os.path.dirname(os.path.realpath(args[1]))
return options
@@ -155,19 +159,8 @@ def main():
options = parse_args()
cli.setupLogging("virt-convert", options.debug)
- try:
- inp = virtconv.vmconfig.find_parser_by_name(options.input_format)
- except:
- logging.error("No parser of format \"%s\" was found." %
- options.input_format)
- sys.exit(1)
-
- try:
- outp = virtconv.vmconfig.find_parser_by_name(options.output_format)
- except:
- logging.error("No parser of format \"%s\" was found." %
- options.output_format)
- sys.exit(1)
+ inp = vmconfig.parser_by_name(options.input_format)
+ outp = vmconfig.parser_by_name(options.output_format)
vmdef = None
diff --git a/virtconv/parsers/virtimage.py b/virtconv/parsers/virtimage.py
--- a/virtconv/parsers/virtimage.py
+++ b/virtconv/parsers/virtimage.py
@@ -78,6 +78,7 @@ class virtimage_parser(vmconfig.parser):
suffix = ".virt-image.xml"
can_import = False
can_export = True
+ can_identify = False
@staticmethod
def identify_file(input_file):
diff --git a/virtconv/parsers/virtinstance.py b/virtconv/parsers/virtinstance.py
--- a/virtconv/parsers/virtinstance.py
+++ b/virtconv/parsers/virtinstance.py
@@ -101,6 +101,7 @@ class virtinstance_parser(vmconfig.parse
suffix = ".virt-instance.xml"
can_import = False
can_export = True
+ can_identify = False
@staticmethod
def identify_file(input_file):
diff --git a/virtconv/parsers/vmx.py b/virtconv/parsers/vmx.py
--- a/virtconv/parsers/vmx.py
+++ b/virtconv/parsers/vmx.py
@@ -19,6 +19,7 @@
#
import virtconv.vmconfig as vmconfig
+import re
class vmx_parser(vmconfig.parser):
"""
@@ -31,13 +32,21 @@ class vmx_parser(vmconfig.parser):
suffix = ".vmx"
can_import = True
can_export = False
+ can_identify = True
@staticmethod
def identify_file(input_file):
"""
Return True if the given file is of this format.
"""
- raise NotImplementedError
+ infile = open(input_file, "r")
+ line = infile.readline()
+ infile.close()
+
+ # some .vmx files don't bother with the header
+ if re.match(r'^config.version\s+=', line):
+ return True
+ return re.match(r'^#!\s*/usr/bin/vm(ware|player)', line) is not None
@staticmethod
def import_file(input_file):
diff --git a/virtconv/vmconfig.py b/virtconv/vmconfig.py
--- a/virtconv/vmconfig.py
+++ b/virtconv/vmconfig.py
@@ -179,11 +179,13 @@ def register_parser(parser):
global _parsers
_parsers += [ parser ]
-def find_parser_by_name(name):
- """
- Return the parser of the given name
- """
- return [p for p in _parsers if p.name == name][0] or None
+def parser_by_name(name):
+ """
+ Return the parser of the given name.
+ """
+ parsers = [p for p in _parsers if p.name == name]
+ if len(parsers):
+ return parsers[0]
def find_parser_by_file(input_file):
"""
@@ -211,3 +213,28 @@ def output_formats():
Return a list of supported output formats.
"""
return [p.name for p in _parsers if p.can_export]
+
+def find_input(path, format = None):
+ """
+ Search for a configuration file automatically. If @format is given,
+ then only search using a matching format parser.
+ """
+
+ if os.path.isdir(path):
+ files = os.listdir(path)
+
+ for p in _parsers:
+ if not p.can_identify:
+ continue
+ if format and format != p.name:
+ continue
+
+ if os.path.isfile(path):
+ if p.identify_file(path):
+ return (path, p.name)
+ elif os.path.isdir(path):
+ for cfgfile in [ x for x in files if x.endswith(p.suffix) ]:
+ if p.identify_file(os.path.join(path, cfgfile)):
+ return (os.path.join(path, cfgfile), p.name)
+
+ raise StandardError("unknown format")
More information about the et-mgmt-tools
mailing list