mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-02-25 18:55:27 -06:00
urlfetcher: Use python style tempfiles
Simplifies things WRT writing file contents
This commit is contained in:
parent
cda3e381e6
commit
b7beb0edb0
@ -70,29 +70,27 @@ class _URLFetcher(object):
|
|||||||
ret += "/"
|
ret += "/"
|
||||||
return ret + filename
|
return ret + filename
|
||||||
|
|
||||||
def _saveTemp(self, fileobj, prefix):
|
def _saveTemp(self, urlobj, prefix):
|
||||||
"""
|
"""
|
||||||
Save the fileobj contents to a temporary file, and return
|
Save the fileobj contents to a temporary file, and return
|
||||||
the filename
|
the filename
|
||||||
"""
|
"""
|
||||||
prefix = "virtinst-" + prefix
|
prefix = "virtinst-" + prefix
|
||||||
if "VIRTINST_TEST_SUITE" in os.environ:
|
if "VIRTINST_TEST_SUITE" in os.environ:
|
||||||
fn = os.path.join("/tmp", prefix)
|
filename = os.path.join("/tmp", prefix)
|
||||||
fd = os.open(fn, os.O_RDWR | os.O_CREAT, 0640)
|
fileobj = file(filename, "w+b")
|
||||||
else:
|
else:
|
||||||
(fd, fn) = tempfile.mkstemp(prefix=prefix,
|
fileobj = tempfile.NamedTemporaryFile(
|
||||||
dir=self.scratchdir)
|
dir=self.scratchdir, prefix=prefix, delete=False)
|
||||||
|
filename = fileobj.name
|
||||||
|
|
||||||
block_size = 16384
|
block_size = 16384
|
||||||
try:
|
while 1:
|
||||||
while 1:
|
buff = urlobj.read(block_size)
|
||||||
buff = fileobj.read(block_size)
|
if not buff:
|
||||||
if not buff:
|
break
|
||||||
break
|
fileobj.write(buff)
|
||||||
os.write(fd, buff)
|
return filename
|
||||||
finally:
|
|
||||||
os.close(fd)
|
|
||||||
return fn
|
|
||||||
|
|
||||||
|
|
||||||
##############
|
##############
|
||||||
@ -122,28 +120,21 @@ class _URLFetcher(object):
|
|||||||
Grab the passed filename from self.location and save it to
|
Grab the passed filename from self.location and save it to
|
||||||
a temporary file, returning the temp filename
|
a temporary file, returning the temp filename
|
||||||
"""
|
"""
|
||||||
|
url = self._make_full_url(filename)
|
||||||
|
base = os.path.basename(filename)
|
||||||
|
logging.debug("Fetching URI: %s", url)
|
||||||
|
|
||||||
f = None
|
|
||||||
try:
|
try:
|
||||||
path = self._make_full_url(filename)
|
urlobj = grabber.urlopen(url,
|
||||||
base = os.path.basename(filename)
|
progress_obj=self.meter,
|
||||||
logging.debug("Fetching URI: %s", path)
|
text=_("Retrieving file %s...") % base)
|
||||||
|
except Exception, e:
|
||||||
try:
|
raise ValueError(_("Couldn't acquire file %s: %s") %
|
||||||
f = grabber.urlopen(path,
|
(url, str(e)))
|
||||||
progress_obj=self.meter,
|
|
||||||
text=_("Retrieving file %s...") % base)
|
|
||||||
except Exception, e:
|
|
||||||
raise ValueError(_("Couldn't acquire file %s: %s") %
|
|
||||||
(path, str(e)))
|
|
||||||
|
|
||||||
tmpname = self._saveTemp(f, prefix=base + ".")
|
|
||||||
logging.debug("Saved file to " + tmpname)
|
|
||||||
return tmpname
|
|
||||||
finally:
|
|
||||||
if f:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
tmpname = self._saveTemp(urlobj, prefix=base + ".")
|
||||||
|
logging.debug("Saved file to " + tmpname)
|
||||||
|
return tmpname
|
||||||
|
|
||||||
|
|
||||||
class _HTTPURLFetcher(_URLFetcher):
|
class _HTTPURLFetcher(_URLFetcher):
|
||||||
|
Loading…
Reference in New Issue
Block a user