Merge "Unify utils.py and util.py"

This commit is contained in:
Zuul 2018-09-07 13:03:25 +00:00 committed by Gerrit Code Review
commit ef4e680e2d
3 changed files with 20 additions and 48 deletions

View File

@ -21,7 +21,7 @@ from placement import context
from placement import exception
from placement import policy
from placement.tests.unit import policy_fixture
from placement import utils
from placement import util
CONF = cfg.CONF
@ -41,7 +41,7 @@ class PlacementPolicyTestCase(testtools.TestCase):
authorizations against a fake rule between updates to the physical
policy file.
"""
with utils.tempdir() as tmpdir:
with util.tempdir() as tmpdir:
tmpfilename = os.path.join(tmpdir, 'placement-policy.yaml')
self.conf.set_default(

View File

@ -11,8 +11,11 @@
# under the License.
"""Utility methods for placement API."""
import contextlib
import functools
import re
import shutil
import tempfile
import jsonschema
from oslo_config import cfg
@ -693,3 +696,18 @@ def ensure_consumer(ctx, consumer_uuid, project_id, user_id,
# No worries, another thread created this user already
consumer = consumer_obj.Consumer.get_by_uuid(ctx, consumer_uuid)
return consumer, created_new_consumer
@contextlib.contextmanager
def tempdir(**kwargs):
argdict = kwargs.copy()
if 'dir' not in argdict:
argdict['dir'] = CONF.tempdir
tmpdir = tempfile.mkdtemp(**argdict)
try:
yield tmpdir
finally:
try:
shutil.rmtree(tmpdir)
except OSError as e:
LOG.error('Could not remove tmpdir: %s', e)

View File

@ -1,46 +0,0 @@
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# Copyright 2011 Justin Santa Barbara
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Utilities and helper functions."""
import contextlib
import shutil
import tempfile
from oslo_log import log as logging
import placement.conf
CONF = placement.conf.CONF
LOG = logging.getLogger(__name__)
@contextlib.contextmanager
def tempdir(**kwargs):
argdict = kwargs.copy()
if 'dir' not in argdict:
argdict['dir'] = CONF.tempdir
tmpdir = tempfile.mkdtemp(**argdict)
try:
yield tmpdir
finally:
try:
shutil.rmtree(tmpdir)
except OSError as e:
LOG.error('Could not remove tmpdir: %s', e)