# Path: virtman/openstack/common/fileutils.py
# LOG = logging.getLogger(__name__)
# _FILE_CACHE = {}
# def ensure_tree(path):
# def read_cached_file(filename, force_reload=False):
# def delete_if_exists(path, remove=os.unlink):
# def remove_path_on_error(path, remove=delete_if_exists):
# def file_open(*args, **kwargs):
# def write_to_tempfile(content, path=None, suffix='', prefix='tmp'):


import contextlib
import errno
import functools
import os
import shutil
import subprocess
import sys
import tempfile
import threading
import time
import weakref
    import msvcrt
    import fcntl
from oslo.config import cfg
from virtman.openstack.common import fileutils
from virtman.openstack.common.gettextutils import _  # noqa
from virtman.openstack.common import local
from virtman.openstack.common import log as logging
    :param lock_path: The lock_path keyword argument is used to specify a
    special location for external lock files to live. If nothing is set, then
    CONF.lock_path is used as a default.
    """
    with _semaphores_lock:
        try:
            sem = _semaphores[name]
        except KeyError:
            sem = threading.Semaphore()
            _semaphores[name] = sem

    with sem:
        LOG.debug(_('Got semaphore "%(lock)s"'), {'lock': name})

        # NOTE(mikal): I know this looks odd
        if not hasattr(local.strong_store, 'locks_held'):
            local.strong_store.locks_held = []
        local.strong_store.locks_held.append(name)

        try:
            if external and not CONF.disable_process_locking:
                LOG.debug(_('Attempting to grab file lock "%(lock)s"'),
                          {'lock': name})

                # We need a copy of lock_path because it is non-local
                local_lock_path = lock_path or CONF.lock_path
                if not local_lock_path:
                    raise cfg.RequiredOptError('lock_path')

                if not os.path.exists(local_lock_path):

# Path: virtman/openstack/common/fileutils.py
# LOG = logging.getLogger(__name__)
# _FILE_CACHE = {}
# def ensure_tree(path):
# def read_cached_file(filename, force_reload=False):
# def delete_if_exists(path, remove=os.unlink):
# def remove_path_on_error(path, remove=delete_if_exists):
# def file_open(*args, **kwargs):
# def write_to_tempfile(content, path=None, suffix='', prefix='tmp'):


# Path: virtman/openstack/common/lockutils.py
import contextlib
import errno
import functools
import os
import shutil
import subprocess
import sys
import tempfile
import threading
import time
import weakref
    import msvcrt
    import fcntl
from oslo.config import cfg
from virtman.openstack.common import fileutils
from virtman.openstack.common.gettextutils import _  # noqa
from virtman.openstack.common import local
from virtman.openstack.common import log as logging

    :param lock_path: The lock_path keyword argument is used to specify a
    special location for external lock files to live. If nothing is set, then
    CONF.lock_path is used as a default.
    """
    with _semaphores_lock:
        try:
            sem = _semaphores[name]
        except KeyError:
            sem = threading.Semaphore()
            _semaphores[name] = sem

    with sem:
        LOG.debug(_('Got semaphore "%(lock)s"'), {'lock': name})

        # NOTE(mikal): I know this looks odd
        if not hasattr(local.strong_store, 'locks_held'):
            local.strong_store.locks_held = []
        local.strong_store.locks_held.append(name)

        try:
            if external and not CONF.disable_process_locking:
                LOG.debug(_('Attempting to grab file lock "%(lock)s"'),
                          {'lock': name})

                # We need a copy of lock_path because it is non-local
                local_lock_path = lock_path or CONF.lock_path
                if not local_lock_path:
                    raise cfg.RequiredOptError('lock_path')

                if not os.path.exists(local_lock_path):

                    fileutils.ensure_tree(local_lock_path)