File OvfSet-init.patch of Package open-ovf
Index: open-ovf-0.1/py/ovf/OvfSet.py
===================================================================
--- open-ovf-0.1.orig/py/ovf/OvfSet.py
+++ open-ovf-0.1/py/ovf/OvfSet.py
@@ -29,7 +29,7 @@ class OvfSet(object):
archive or as a directory layout
"""
- def __init__(self, path=None, mode="r"):
+ def __init__(self, path=None, mode="r", format=FORMAT_DIR):
"""
Initialize object from path in read/write mode
@@ -48,8 +48,8 @@ class OvfSet(object):
#: the package name of this object (the name of .ovf without extension)
self.name = None
- self.ovfFile = None #: The OvfFile object
- self.archiveFormat = FORMAT_DIR #: The archive type of this (default save type)
+ self.ovfFile = None #: The OvfFile object
+ self.archiveFormat = format #: The archive type of this (default save type)
self.archivePath = None #: The write path of the archive
self.archiveSavePath = None #: The Save Path for the archive (differs from archivePath for tar)
self.__tmpdir__ = None #: the temporary dir if tar (cleaned up in __del__)
@@ -60,7 +60,7 @@ class OvfSet(object):
self.certificate = None
if path != None:
- self.initializeFromPath(path, mode)
+ self.initializeFromPath(path)
def __del__(self):
"""
@@ -89,70 +89,21 @@ class OvfSet(object):
mode = property(_getMode, _setMode)
- def initializeFromPath(self, path, mode="r"):
+ def initializeFromPath(self, path):
"""
initialize object from the file or path given in path
@raise IOError: The cases are as follow
- - I{B{Case 1:}} The path provided in the parameters is not
- valid.
- - I{B{Case 2:}} The mode parameter has a value of r and the path
- already exist.
- - I{B{Case 3:}} Unsafe Tar file
- - I{B{Case 4:}} The tar file cannot be found.
+ - I{B{Case 1:}} Invalid archive format
+ - I{B{Case 2:}} Unsafe Tar file
+ - I{B{Case 3:}} The tar file cannot be found.
@type path: String
@param path: a path to a file to open
- @type mode: string
- @param mode: mode for open, either 'r' or 'w'
"""
-
- exists = True
- if os.path.isdir(path):
- self.archiveFormat = FORMAT_DIR
- elif os.path.isfile(path):
- if tarfile.is_tarfile(path):
- self.archiveFormat = FORMAT_TAR
- else:
- # this file is not a tar file, assume that this is a .ovf
- self.archiveFormat = FORMAT_DIR
- elif os.path.exists(path):
- raise IOError("unsupported file type for " + path)
- else:
- exists = False
- if mode == "r":
- raise IOError("cannot open for read " + path)
- if path.endswith("/") or path.endswith("\\"):
- self.archiveFormat = FORMAT_DIR
- elif path.endswith(".ovf") or path.endswith(".OVF"):
- self.archiveFormat = FORMAT_DIR
- else:
- self.archiveFormat = FORMAT_TAR
-
- if exists == True and self.archiveFormat == FORMAT_TAR:
- # Here, for now, we make a temporary copy
- tmpdir = os.path.dirname(os.path.abspath(path))
- if os.environ.has_key("TMPDIR"): tmpdir = None
- tmpd = tempfile.mkdtemp(dir=tmpdir)
- self.__tmpdir__ = tmpd
- tf = tarfile.open(path, "r")
- ti = tf.next()
- while ti is not None:
- #TODO: need to do safe extraction here this
- # on windows need to protect c://
- # also, check that .ovf is first file
- # suggestion[ejcasler]: change ".." to "../"
- # make absolute refs begin with tempdir path
- if ti.name.find("..") != -1 or ti.name.startswith("/"):
- raise IOError("Unsafe Tar file" + path)
- tf.extract(ti, tmpd)
- ti = tf.next()
- self.archivePath = tmpd
- self.archiveSavePath = path
- elif exists == True and self.archiveFormat == FORMAT_DIR:
- # for existing, if it is a file path=dirname(path)
+ if self.archiveFormat == FORMAT_DIR:
if os.path.isfile(path):
self.archivePath = os.path.dirname(path)
# dirname returns "" rather than "." for "filename"
@@ -162,46 +113,53 @@ class OvfSet(object):
else:
self.archivePath = path
self.archiveSavePath = self.archivePath
- elif exists == False and self.archiveFormat == FORMAT_TAR:
- # for non-existant file, this is the file (.ova)
- self.archivePath = path
- self.archiveSavePath = self.archivePath
- self.setName(os.path.basename(path)[0:(len(os.path.basename(path))-4)])
- elif exists == False and self.archiveFormat == FORMAT_DIR:
- # for non-existant dir, this is a dir (not .ovf)
- self.archivePath = path
- self.archiveSavePath = self.archivePath
+ elif self.archiveFormat == FORMAT_TAR:
+ if os.path.isfile(path):
+ # Here, for now, we make a temporary copy
+ tmpdir = os.path.dirname(os.path.abspath(path))
+ if os.environ.has_key("TMPDIR"): tmpdir = None
+ tmpd = tempfile.mkdtemp(dir=tmpdir)
+ self.__tmpdir__ = tmpd
+ tf = tarfile.open(path, "r")
+ for ti in tf.getmembers():
+ #TODO: need to do safe extraction here this
+ # on windows need to protect c://
+ # also, check that .ovf is first file
+ # suggestion[ejcasler]: change ".." to "../"
+ # make absolute refs begin with tempdir path
+ if ti.name.find("..") != -1 or ti.name.startswith("/"):
+ raise IOError("Unsafe Tar file" + path)
+ tf.extract(ti, tmpd)
+ tf.close()
+ self.archivePath = tmpd
+ self.archiveSavePath = path
+ else:
+ # for non-existant file, this is the file (.ova)
+ (p, f) = os.path.split(os.path.abspath(path))
+ self.archivePath = p
+ self.archiveSavePath = self.archivePath
+ self.setName((f.rsplit(".ova", 1))[0])
+
else:
- raise IOError("shouldn't be here")
+ raise IOError("Invalid archive format")
- if ( not os.path.isfile(path) and self.archiveFormat == FORMAT_DIR and
- exists == True ) or self.__tmpdir__ != None:
- name = False
+ if self.name == None:
for curFile in os.listdir(self.archivePath):
if curFile.endswith(".ovf"):
- if name != False:
- return False
- # set name to filename without .ovf
- name = curFile[0:(len(curFile)-4)]
- if name == False and mode == "r":
- raise IOError("no ovf file in " + path + "(" + self.archivePath + ")")
- elif name:
- self.setName(name)
-
- # now self.archivePath, self.archiveSavePath and self.archiveFormat should
- # be set. self.name should be set if possible.
- # now, self.archivePath/self.name + ".ovf" should have the ovf file
-
- if self.name != None:
- basepath = os.path.join(self.archivePath, self.name)
- self.ovfFile = OvfFile.OvfFile(basepath + ".ovf")
- if os.path.isfile(basepath + ".mf"):
- # we have a manifest
- self.manifest = basepath + ".mf"
-
- if os.path.isfile(basepath + ".cert"):
- # we have a certificate
- self.certificate = basepath + ".cert"
+ self.name = curFile[0:(len(curFile)-4)]
+
+ # ovf file name = self.archivePath + / + self.name + .ovf
+ basepath = os.path.join(self.archivePath, self.name)
+ self.ovfFile = OvfFile.OvfFile(basepath + ".ovf")
+
+ # Similarly, aux file name = self.archive + / + self.name + .ext
+ if os.path.isfile(basepath + ".mf"):
+ # we have a manifest
+ self.manifest = basepath + ".mf"
+
+ if os.path.isfile(basepath + ".cert"):
+ # we have a certificate
+ self.certificate = basepath + ".cert"
def toString(self):
"""Overrides toString for OvfSet"""
@@ -285,7 +243,7 @@ class OvfSet(object):
os.unlink(ovfName)
ovfname = None
except:
- if ovfname != None:
+ if ovfName != None:
os.unlink(ovfName)
raise