Add write_to_tempfile back to fileutils
This was recently removed during graduation since I had stated that only keystone was using the function. I was incorrect. It seems that ceilometer and sahara are also using the function. We should add it back so those projects can remove fileutils from their oslo incubator project. Change-Id: I661dd222da6386a7dbcf854958a63e59b13e9ba4
This commit is contained in:
parent
e94d1ccaf8
commit
36d599f477
@ -18,6 +18,7 @@ import errno
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import stat
|
import stat
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from oslo_utils import excutils
|
from oslo_utils import excutils
|
||||||
|
|
||||||
@ -69,3 +70,30 @@ def remove_path_on_error(path, remove=delete_if_exists):
|
|||||||
except Exception:
|
except Exception:
|
||||||
with excutils.save_and_reraise_exception():
|
with excutils.save_and_reraise_exception():
|
||||||
remove(path)
|
remove(path)
|
||||||
|
|
||||||
|
|
||||||
|
def write_to_tempfile(content, path=None, suffix='', prefix='tmp'):
|
||||||
|
"""Create temporary file or use existing file.
|
||||||
|
|
||||||
|
This util is needed for creating temporary file with
|
||||||
|
specified content, suffix and prefix. If path is not None,
|
||||||
|
it will be used for writing content. If the path doesn't
|
||||||
|
exist it'll be created.
|
||||||
|
|
||||||
|
:param content: content for temporary file.
|
||||||
|
:param path: same as parameter 'dir' for mkstemp
|
||||||
|
:param suffix: same as parameter 'suffix' for mkstemp
|
||||||
|
:param prefix: same as parameter 'prefix' for mkstemp
|
||||||
|
|
||||||
|
For example: it can be used in database tests for creating
|
||||||
|
configuration files.
|
||||||
|
"""
|
||||||
|
if path:
|
||||||
|
ensure_tree(path)
|
||||||
|
|
||||||
|
(fd, path) = tempfile.mkstemp(suffix=suffix, dir=path, prefix=prefix)
|
||||||
|
try:
|
||||||
|
os.write(fd, content)
|
||||||
|
finally:
|
||||||
|
os.close(fd)
|
||||||
|
return path
|
||||||
|
@ -20,6 +20,7 @@ import stat
|
|||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from oslotest import base as test_base
|
from oslotest import base as test_base
|
||||||
|
import six
|
||||||
|
|
||||||
from oslo_utils import fileutils
|
from oslo_utils import fileutils
|
||||||
|
|
||||||
@ -115,3 +116,73 @@ class RemovePathOnError(test_base.BaseTestCase):
|
|||||||
raise Exception
|
raise Exception
|
||||||
except Exception:
|
except Exception:
|
||||||
self.assertFalse(os.path.exists(tmpdir))
|
self.assertFalse(os.path.exists(tmpdir))
|
||||||
|
|
||||||
|
|
||||||
|
class WriteToTempfileTestCase(test_base.BaseTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
super(WriteToTempfileTestCase, self).setUp()
|
||||||
|
self.content = 'testing123'.encode('ascii')
|
||||||
|
|
||||||
|
def check_file_content(self, path):
|
||||||
|
with open(path, 'r') as fd:
|
||||||
|
ans = fd.read()
|
||||||
|
self.assertEqual(self.content, six.b(ans))
|
||||||
|
|
||||||
|
def test_file_without_path_and_suffix(self):
|
||||||
|
res = fileutils.write_to_tempfile(self.content)
|
||||||
|
self.assertTrue(os.path.exists(res))
|
||||||
|
|
||||||
|
(basepath, tmpfile) = os.path.split(res)
|
||||||
|
self.assertTrue(basepath.startswith(tempfile.gettempdir()))
|
||||||
|
self.assertTrue(tmpfile.startswith('tmp'))
|
||||||
|
|
||||||
|
self.check_file_content(res)
|
||||||
|
|
||||||
|
def test_file_with_not_existing_path(self):
|
||||||
|
path = '/tmp/testing/test1'
|
||||||
|
res = fileutils.write_to_tempfile(self.content, path=path)
|
||||||
|
self.assertTrue(os.path.exists(res))
|
||||||
|
(basepath, tmpfile) = os.path.split(res)
|
||||||
|
self.assertEqual(basepath, path)
|
||||||
|
self.assertTrue(tmpfile.startswith('tmp'))
|
||||||
|
|
||||||
|
self.check_file_content(res)
|
||||||
|
shutil.rmtree('/tmp/testing')
|
||||||
|
|
||||||
|
def test_file_with_not_default_suffix(self):
|
||||||
|
suffix = '.conf'
|
||||||
|
res = fileutils.write_to_tempfile(self.content, suffix=suffix)
|
||||||
|
self.assertTrue(os.path.exists(res))
|
||||||
|
|
||||||
|
(basepath, tmpfile) = os.path.split(res)
|
||||||
|
self.assertTrue(basepath.startswith(tempfile.gettempdir()))
|
||||||
|
self.assertTrue(tmpfile.startswith('tmp'))
|
||||||
|
self.assertTrue(tmpfile.endswith('.conf'))
|
||||||
|
|
||||||
|
self.check_file_content(res)
|
||||||
|
|
||||||
|
def test_file_with_not_existing_path_and_not_default_suffix(self):
|
||||||
|
suffix = '.txt'
|
||||||
|
path = '/tmp/testing/test2'
|
||||||
|
res = fileutils.write_to_tempfile(self.content,
|
||||||
|
path=path,
|
||||||
|
suffix=suffix)
|
||||||
|
self.assertTrue(os.path.exists(res))
|
||||||
|
(basepath, tmpfile) = os.path.split(res)
|
||||||
|
self.assertTrue(tmpfile.startswith('tmp'))
|
||||||
|
self.assertEqual(basepath, path)
|
||||||
|
self.assertTrue(tmpfile.endswith(suffix))
|
||||||
|
|
||||||
|
self.check_file_content(res)
|
||||||
|
shutil.rmtree('/tmp/testing')
|
||||||
|
|
||||||
|
def test_file_with_not_default_prefix(self):
|
||||||
|
prefix = 'test'
|
||||||
|
res = fileutils.write_to_tempfile(self.content, prefix=prefix)
|
||||||
|
self.assertTrue(os.path.exists(res))
|
||||||
|
|
||||||
|
(basepath, tmpfile) = os.path.split(res)
|
||||||
|
self.assertTrue(tmpfile.startswith(prefix))
|
||||||
|
self.assertTrue(basepath.startswith(tempfile.gettempdir()))
|
||||||
|
|
||||||
|
self.check_file_content(res)
|
||||||
|
Loading…
Reference in New Issue
Block a user