Merge "Sync filetuils from oslo-incubator"

This commit is contained in:
Jenkins 2013-09-09 13:27:59 +00:00 committed by Gerrit Code Review
commit b1a3b0af7e
2 changed files with 19 additions and 10 deletions

View File

@ -21,7 +21,7 @@ import errno
import os
from nova.openstack.common import excutils
from nova.openstack.common.gettextutils import _
from nova.openstack.common.gettextutils import _ # noqa
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)
@ -69,33 +69,34 @@ def read_cached_file(filename, force_reload=False):
return (reloaded, cache_info['data'])
def delete_if_exists(path):
def delete_if_exists(path, remove=os.unlink):
"""Delete a file, but ignore file not found error.
:param path: File to delete
:param remove: Optional function to remove passed path
"""
try:
os.unlink(path)
remove(path)
except OSError as e:
if e.errno == errno.ENOENT:
return
else:
if e.errno != errno.ENOENT:
raise
@contextlib.contextmanager
def remove_path_on_error(path):
def remove_path_on_error(path, remove=delete_if_exists):
"""Protect code that wants to operate on PATH atomically.
Any exception will cause PATH to be removed.
:param path: File to work with
:param remove: Optional function to remove passed path
"""
try:
yield
except Exception:
with excutils.save_and_reraise_exception():
delete_if_exists(path)
remove(path)
def file_open(*args, **kwargs):

View File

@ -20,6 +20,7 @@ import copy
import errno
import eventlet
import fixtures
import functools
import mox
import os
import re
@ -6063,7 +6064,7 @@ disk size: 4.4M''', ''))
def fake_unlink(path):
self.executes.append(('rm', path))
def fake_rm_on_errror(path):
def fake_rm_on_error(path, remove=None):
self.executes.append(('rm', '-f', path))
def fake_qemu_img_info(path):
@ -6090,7 +6091,14 @@ disk size: 4.4M''', ''))
self.stubs.Set(os, 'unlink', fake_unlink)
self.stubs.Set(images, 'fetch', lambda *_: None)
self.stubs.Set(images, 'qemu_img_info', fake_qemu_img_info)
self.stubs.Set(fileutils, 'delete_if_exists', fake_rm_on_errror)
self.stubs.Set(fileutils, 'delete_if_exists', fake_rm_on_error)
# Since the remove param of fileutils.remove_path_on_error()
# is initialized at load time, we must provide a wrapper
# that explicitly resets it to our fake delete_if_exists()
old_rm_path_on_error = fileutils.remove_path_on_error
f = functools.partial(old_rm_path_on_error, remove=fake_rm_on_error)
self.stubs.Set(fileutils, 'remove_path_on_error', f)
context = 'opaque context'
image_id = '4'