. Defaults to now.    returns:  2 tuple of datetimes (begin, end)
              The begin timestamp of this audit period is the same as the
              end of the previous.
    """
    if not unit:
        unit = CONF.instance_usage_audit_period
    offset = 0
    if '@' in unit:
        unit, offset = unit.split("@", 1)
        offset = int(offset)
    if before is not None:
        rightnow = before
    else:
        rightnow = timeutils.utcnow()
    if unit not in ('month', 'day', 'year', 'hour'):
        raise ValueError('Time period must be hour, day, month or year')
    if unit == 'month':
        if offset == 0:
            offset = 1
        end = datetime.datetime(day=offset,
                                month=rightnow.month,
                                year=rightnow.year)
        if end >= rightnow:
            year = rightnow.year
            if 1 >= rightnow.month:
                year -= 1
                month = 12 + (rightnow.month - 1)
            else:
                month = rightnow.month - 1
            end = datetime.datetime(day=offset,
                                    month=month,
                                    year=year)
        year = end.year
        if 1 >= end.month:
            year -= 1
            month = 12 + (end.month - 1)
        else:
            month = end.month - 1
        begin = datetime.datetime(day=offset, month=month, year=year)
    elif unit == 'year':
        if offset == 0:
            offset = 1
        end = datetime.datetime(day=1, month=offset, year=rightnow.year)
        if end >= rightnow:
            end = datetime.datetime(day=1,
                                    month=offset,
                                    year=rightnow.year - 1)
            begin = datetime.datetime(day=1,
                                      month=offset,
                                      year=rightnow.year - 2)
        else:
            begin = datetime.datetime(day=1,
                                      month=offset,
                                      year=rightnow.year - 1)
    elif unit == 'day':
        end = datetime.datetime(hour=offset,
                               day=rightnow.day,
                               month=rightnow.month,
                               year=rightnow.year)
        if end >= rightnow:
            end = end - datetime.timedelta(days=1)
        begin = end - datetime.timedelta(days=1)
    elif unit == 'hour':
        end = rightnow.replace(minute=offset, second=0, microsecond=0)
        if end >= rightnow:
            end = end - datetime.timedelta(hours=1)
        begin = end - datetime.timedelta(hours=1)
    return (begin, end)
**** CubicPower OpenStack Study ****
def generate_password(length=None, symbolgroups=DEFAULT_PASSWORD_SYMBOLS):
    """Generate a random password from the supplied symbol groups.
    At least one symbol from each group will be included. Unpredictable
    results if length is less than the number of symbol groups.
    Believed to be reasonably secure (with a reasonable password length!)
    """
    if length is None:
        length = CONF.password_length
    r = random.SystemRandom()
    # NOTE(jerdfelt): Some password policies require at least one character
    # from each group of symbols, so start off with one random character
    # from each symbol group
    password = [r.choice(s) for s in symbolgroups]
    # If length < len(symbolgroups), the leading characters will only
    # be from the first length groups. Try our best to not be predictable
    # by shuffling and then truncating.
    r.shuffle(password)
    password = password[:length]
    length -= len(password)
    # then fill with random characters from all symbol groups
    symbols = ''.join(symbolgroups)
    password.extend([r.choice(symbols) for _i in xrange(length)])
    # finally shuffle to ensure first x characters aren't from a
    # predictable group
    r.shuffle(password)
    return ''.join(password)
**** CubicPower OpenStack Study ****
def get_my_ipv4_address():
    """Run ip route/addr commands to figure out the best ipv4
    """
    LOCALHOST = '127.0.0.1'
    try:
        out = execute('ip', '-f', 'inet', '-o', 'route', 'show')
        # Find the default route
        regex_default = ('default\s*via\s*'
                         '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
                         '\s*dev\s*(\w*)\s*')
        default_routes = re.findall(regex_default, out[0])
        if not default_routes:
            return LOCALHOST
        gateway, iface = default_routes[0]
        # Find the right subnet for the gateway/interface for
        # the default route
        route = ('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})'
              '\s*dev\s*(\w*)\s*')
        for match in re.finditer(route, out[0]):
            subnet = netaddr.IPNetwork(match.group(1) + "/" + match.group(2))
            if (match.group(3) == iface and
                    netaddr.IPAddress(gateway) in subnet):
                try:
                    return _get_ipv4_address_for_interface(iface)
                except exception.NovaException:
                    pass
    except Exception as ex:
        LOG.error(_("Couldn't get IPv4 : %(ex)s") % {'ex': ex})
    return LOCALHOST
**** CubicPower OpenStack Study ****
def _get_ipv4_address_for_interface(iface):
    """Run ip addr show for an interface and grab its ipv4 addresses
    """
    try:
        out = execute('ip', '-f', 'inet', '-o', 'addr', 'show', iface)
        regexp_address = re.compile('inet\s*'
                                    '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
        address = [m.group(1) for m in regexp_address.finditer(out[0])
                   if m.group(1) != '127.0.0.1']
        if address:
            return address[0]
        else:
            msg = _('IPv4 address is not found.: %s') % out[0]
            raise exception.NovaException(msg)
    except Exception as ex:
        msg = _("Couldn't get IPv4 of %(interface)s"
                " : %(ex)s") % {'interface': iface, 'ex': ex}
        LOG.error(msg)
        raise exception.NovaException(msg)
**** CubicPower OpenStack Study ****
def get_my_linklocal(interface):
    try:
        if_str = execute('ip', '-f', 'inet6', '-o', 'addr', 'show', interface)
        condition = '\s+inet6\s+([0-9a-f:]+)/\d+\s+scope\s+link'
        links = [re.search(condition, x) for x in if_str[0].split('\n')]
        address = [w.group(1) for w in links if w is not None]
        if address[0] is not None:
            return address[0]
        else:
            msg = _('Link Local address is not found.:%s') % if_str
            raise exception.NovaException(msg)
    except Exception as ex:
        msg = _("Couldn't get Link Local IP of %(interface)s"
                " :%(ex)s") % {'interface': interface, 'ex': ex}
        raise exception.NovaException(msg)
**** CubicPower OpenStack Study ****
class LazyPluggable(object):
    """A pluggable backend loaded lazily based on some value."""
    
**** CubicPower OpenStack Study ****
    def __init__(self, pivot, config_group=None, **backends):
        self.__backends = backends
        self.__pivot = pivot
        self.__backend = None
        self.__config_group = config_group
**** CubicPower OpenStack Study ****
    def __get_backend(self):
        if not self.__backend:
            if self.__config_group is None:
                backend_name = CONF[self.__pivot]
            else:
                backend_name = CONF[self.__config_group][self.__pivot]
            if backend_name not in self.__backends:
                msg = _('Invalid backend: %s') % backend_name
                raise exception.NovaException(msg)
            backend = self.__backends[backend_name]
            if isinstance(backend, tuple):
                name = backend[0]
                fromlist = backend[1]
            else:
                name = backend
                fromlist = backend
            self.__backend = __import__(name, None, None, fromlist)
        return self.__backend
**** CubicPower OpenStack Study ****
    def __getattr__(self, key):
        backend = self.__get_backend()
        return getattr(backend, key)
def xhtml_escape(value):
    """Escapes a string so it is valid within XML or XHTML.
    """
    return saxutils.escape(value, {'"': '"', "'": '''})
def utf8(value):
    """Try to turn a string into utf-8 if possible.
    Code is directly from the utf8 function in
    http://github.com/facebook/tornado/blob/master/tornado/escape.py
    """
    if isinstance(value, unicode):
        return value.encode('utf-8')
    elif isinstance(value, gettextutils.Message):
        return unicode(value).encode('utf-8')
    assert isinstance(value, str)
    return value
def check_isinstance(obj, cls):
    """Checks that obj is of type cls, and lets PyLint infer types."""
    if isinstance(obj, cls):
        return obj
    raise Exception(_('Expected object of type: %s') % (str(cls)))
def parse_server_string(server_str):
    """Parses the given server_string and returns a list of host and port.
    If it's not a combination of host part and port, the port element
    is a null string. If the input is invalid expression, return a null
    list.
    """
    try:
        # First of all, exclude pure IPv6 address (w/o port).
        if netaddr.valid_ipv6(server_str):
            return (server_str, '')
        # Next, check if this is IPv6 address with a port number combination.
        if server_str.find("]:") != -1:
            (address, port) = server_str.replace('[', '', 1).split(']:')
            return (address, port)
        # Third, check if this is a combination of an address and a port
        if server_str.find(':') == -1:
            return (server_str, '')
        # This must be a combination of an address and a port
        (address, port) = server_str.split(':')
        return (address, port)
    except Exception:
        LOG.error(_('Invalid server_string: %s'), server_str)
        return ('', '')
def is_int_like(val):
    """Check if a value looks like an int."""
    try:
        return str(int(val)) == str(val)
    except Exception:
        return False
def is_valid_ipv4(address):
    """Verify that address represents a valid IPv4 address."""
    try:
        return netaddr.valid_ipv4(address)
    except Exception:
        return False
def is_valid_ipv6(address):
    try:
        return netaddr.valid_ipv6(address)
    except Exception:
        return False
def is_valid_ip_address(address):
    return is_valid_ipv4(address) or is_valid_ipv6(address)
def is_valid_ipv6_cidr(address):
    try:
        str(netaddr.IPNetwork(address, version=6).cidr)
        return True
    except Exception:
        return False
def get_shortened_ipv6(address):
    addr = netaddr.IPAddress(address, version=6)
    return str(addr.ipv6())
def get_shortened_ipv6_cidr(address):
    net = netaddr.IPNetwork(address, version=6)
    return str(net.cidr)
def is_valid_cidr(address):
    """Check if address is valid
    The provided address can be a IPv6 or a IPv4
    CIDR address.
    """
    try:
        # Validate the correct CIDR Address
        netaddr.IPNetwork(address)
    except netaddr.core.AddrFormatError:
        return False
    except UnboundLocalError:
        # NOTE(MotoKen): work around bug in netaddr 0.7.5 (see detail in
        # https://github.com/drkjam/netaddr/issues/2)
        return False
    # Prior validation partially verify /xx part
    # Verify it here
    ip_segment = address.split('/')
    if (len(ip_segment) <= 1 or
            ip_segment[1] == ''):
        return False
    return True
def get_ip_version(network):
    """Returns the IP version of a network (IPv4 or IPv6).
    Raises AddrFormatError if invalid network.
    """
    if netaddr.IPNetwork(network).version == 6:
        return "IPv6"
    elif netaddr.IPNetwork(network).version == 4:
        return "IPv4"
def monkey_patch():
    """If the CONF.monkey_patch set as True,
    this function patches a decorator
    for all functions in specified modules.
    You can set decorators for each modules
    using CONF.monkey_patch_modules.
    The format is "Module path:Decorator function".
    Example:
      'nova.api.ec2.cloud:nova.notifications.notify_decorator'
    Parameters of the decorator is as follows.
    (See nova.notifications.notify_decorator)
    name - name of the function
    function - object of the function
    """
    # If CONF.monkey_patch is not True, this function do nothing.
    if not CONF.monkey_patch:
        return
    # Get list of modules and decorators
    for module_and_decorator in CONF.monkey_patch_modules:
        module, decorator_name = module_and_decorator.split(':')
        # import decorator function
        decorator = importutils.import_class(decorator_name)
        __import__(module)
        # Retrieve module information using pyclbr
        module_data = pyclbr.readmodule_ex(module)
        for key in module_data.keys():
            # set the decorator for the class methods
            if isinstance(module_data[key], pyclbr.Class):
                clz = importutils.import_class("%s.%s" % (module, key))
                for method, func in inspect.getmembers(clz, inspect.ismethod):
                    setattr(clz, method,
                        decorator("%s.%s.%s" % (module, key, method), func))
            # set the decorator for the function
            if isinstance(module_data[key], pyclbr.Function):
                func = importutils.import_class("%s.%s" % (module, key))
                setattr(sys.modules[module], key,
                    decorator("%s.%s" % (module, key), func))
def convert_to_list_dict(lst, label):
    """Convert a value or list into a list of dicts."""
    if not lst:
        return None
    if not isinstance(lst, list):
        lst = [lst]
    return [{label: x} for x in lst]
def make_dev_path(dev, partition=None, base='/dev'):
    """Return a path to a particular device.
    >>> make_dev_path('xvdc')
    /dev/xvdc
    >>> make_dev_path('xvdc', 1)
    /dev/xvdc1
    """
    path = os.path.join(base, dev)
    if partition:
        path += str(partition)
    return path
def sanitize_hostname(hostname):
    """Return a hostname which conforms to RFC-952 and RFC-1123 specs."""
    if isinstance(hostname, unicode):
        hostname = hostname.encode('latin-1', 'ignore')
    hostname = re.sub('[ _]', '-', hostname)
    hostname = re.sub('[^\w.-]+', '', hostname)
    hostname = hostname.lower()
    hostname = hostname.strip('.-')
    return hostname
def read_cached_file(filename, cache_info, reload_func=None):
    """Read from a file if it has been modified.
    :param cache_info: dictionary to hold opaque cache.
    :param reload_func: optional function to be called with data when
                        file is reloaded due to a modification.
    :returns: data from file
    """
    mtime = os.path.getmtime(filename)
    if not cache_info or mtime != cache_info.get('mtime'):
        LOG.debug(_("Reloading cached file %s") % filename)
        with open(filename) as fap:
            cache_info['data'] = fap.read()
        cache_info['mtime'] = mtime
        if reload_func:
            reload_func(cache_info['data'])
    return cache_info['data']
@contextlib.contextmanager
def temporary_mutation(obj, **kwargs):
    """Temporarily set the attr on a particular object to a given value then
    revert when finished.
    One use of this is to temporarily set the read_deleted flag on a context
    object:
        with temporary_mutation(context, read_deleted="yes"):
            do_something_that_needed_deleted_objects()
    """
**** CubicPower OpenStack Study ****
def xhtml_escape(value):
    """Escapes a string so it is valid within XML or XHTML.
    """
    return saxutils.escape(value, {'"': '"', "'": '''})
**** CubicPower OpenStack Study ****
def utf8(value):
    """Try to turn a string into utf-8 if possible.
    Code is directly from the utf8 function in
    http://github.com/facebook/tornado/blob/master/tornado/escape.py
    """
    if isinstance(value, unicode):
        return value.encode('utf-8')
    elif isinstance(value, gettextutils.Message):
        return unicode(value).encode('utf-8')
    assert isinstance(value, str)
    return value
**** CubicPower OpenStack Study ****
def check_isinstance(obj, cls):
    """Checks that obj is of type cls, and lets PyLint infer types."""
    if isinstance(obj, cls):
        return obj
    raise Exception(_('Expected object of type: %s') % (str(cls)))
**** CubicPower OpenStack Study ****
def parse_server_string(server_str):
    """Parses the given server_string and returns a list of host and port.
    If it's not a combination of host part and port, the port element
    is a null string. If the input is invalid expression, return a null
    list.
    """
    try:
        # First of all, exclude pure IPv6 address (w/o port).
        if netaddr.valid_ipv6(server_str):
            return (server_str, '')
        # Next, check if this is IPv6 address with a port number combination.
        if server_str.find("]:") != -1:
            (address, port) = server_str.replace('[', '', 1).split(']:')
            return (address, port)
        # Third, check if this is a combination of an address and a port
        if server_str.find(':') == -1:
            return (server_str, '')
        # This must be a combination of an address and a port
        (address, port) = server_str.split(':')
        return (address, port)
    except Exception:
        LOG.error(_('Invalid server_string: %s'), server_str)
        return ('', '')
**** CubicPower OpenStack Study ****
def is_int_like(val):
    """Check if a value looks like an int."""
    try:
        return str(int(val)) == str(val)
    except Exception:
        return False
**** CubicPower OpenStack Study ****
def is_valid_ipv4(address):
    """Verify that address represents a valid IPv4 address."""
    try:
        return netaddr.valid_ipv4(address)
    except Exception:
        return False
**** CubicPower OpenStack Study ****
def is_valid_ipv6(address):
    try:
        return netaddr.valid_ipv6(address)
    except Exception:
        return False
**** CubicPower OpenStack Study ****
def is_valid_ip_address(address):
    return is_valid_ipv4(address) or is_valid_ipv6(address)
**** CubicPower OpenStack Study ****
def is_valid_ipv6_cidr(address):
    try:
        str(netaddr.IPNetwork(address, version=6).cidr)
        return True
    except Exception:
        return False
**** CubicPower OpenStack Study ****
def get_shortened_ipv6(address):
    addr = netaddr.IPAddress(address, version=6)
    return str(addr.ipv6())
**** CubicPower OpenStack Study ****
def get_shortened_ipv6_cidr(address):
    net = netaddr.IPNetwork(address, version=6)
    return str(net.cidr)
**** CubicPower OpenStack Study ****
def is_valid_cidr(address):
    """Check if address is valid
    The provided address can be a IPv6 or a IPv4
    CIDR address.
    """
    try:
        # Validate the correct CIDR Address
        netaddr.IPNetwork(address)
    except netaddr.core.AddrFormatError:
        return False
    except UnboundLocalError:
        # NOTE(MotoKen): work around bug in netaddr 0.7.5 (see detail in
        # https://github.com/drkjam/netaddr/issues/2)
        return False
    # Prior validation partially verify /xx part
    # Verify it here
    ip_segment = address.split('/')
    if (len(ip_segment) <= 1 or
            ip_segment[1] == ''):
        return False
    return True
**** CubicPower OpenStack Study ****
def get_ip_version(network):
    """Returns the IP version of a network (IPv4 or IPv6).
    Raises AddrFormatError if invalid network.
    """
    if netaddr.IPNetwork(network).version == 6:
        return "IPv6"
    elif netaddr.IPNetwork(network).version == 4:
        return "IPv4"
**** CubicPower OpenStack Study ****
def monkey_patch():
    """If the CONF.monkey_patch set as True,
    this function patches a decorator
    for all functions in specified modules.
    You can set decorators for each modules
    using CONF.monkey_patch_modules.
    The format is "Module path:Decorator function".
    Example:
      'nova.api.ec2.cloud:nova.notifications.notify_decorator'
    Parameters of the decorator is as follows.
    (See nova.notifications.notify_decorator)
    name - name of the function
    function - object of the function
    """
    # If CONF.monkey_patch is not True, this function do nothing.
    if not CONF.monkey_patch:
        return
    # Get list of modules and decorators
    for module_and_decorator in CONF.monkey_patch_modules:
        module, decorator_name = module_and_decorator.split(':')
        # import decorator function
        decorator = importutils.import_class(decorator_name)
        __import__(module)
        # Retrieve module information using pyclbr
        module_data = pyclbr.readmodule_ex(module)
        for key in module_data.keys():
            # set the decorator for the class methods
            if isinstance(module_data[key], pyclbr.Class):
                clz = importutils.import_class("%s.%s" % (module, key))
                for method, func in inspect.getmembers(clz, inspect.ismethod):
                    setattr(clz, method,
                        decorator("%s.%s.%s" % (module, key, method), func))
            # set the decorator for the function
            if isinstance(module_data[key], pyclbr.Function):
                func = importutils.import_class("%s.%s" % (module, key))
                setattr(sys.modules[module], key,
                    decorator("%s.%s" % (module, key), func))
**** CubicPower OpenStack Study ****
def convert_to_list_dict(lst, label):
    """Convert a value or list into a list of dicts."""
    if not lst:
        return None
    if not isinstance(lst, list):
        lst = [lst]
    return [{label: x} for x in lst]
**** CubicPower OpenStack Study ****
def make_dev_path(dev, partition=None, base='/dev'):
    """Return a path to a particular device.
    >>> make_dev_path('xvdc')
    /dev/xvdc
    >>> make_dev_path('xvdc', 1)
    /dev/xvdc1
    """
    path = os.path.join(base, dev)
    if partition:
        path += str(partition)
    return path
**** CubicPower OpenStack Study ****
def sanitize_hostname(hostname):
    """Return a hostname which conforms to RFC-952 and RFC-1123 specs."""
    if isinstance(hostname, unicode):
        hostname = hostname.encode('latin-1', 'ignore')
    hostname = re.sub('[ _]', '-', hostname)
    hostname = re.sub('[^\w.-]+', '', hostname)
    hostname = hostname.lower()
    hostname = hostname.strip('.-')
    return hostname
**** CubicPower OpenStack Study ****
def read_cached_file(filename, cache_info, reload_func=None):
    """Read from a file if it has been modified.
    :param cache_info: dictionary to hold opaque cache.
    :param reload_func: optional function to be called with data when
                        file is reloaded due to a modification.
    :returns: data from file
    """
    mtime = os.path.getmtime(filename)
    if not cache_info or mtime != cache_info.get('mtime'):
        LOG.debug(_("Reloading cached file %s") % filename)
        with open(filename) as fap:
            cache_info['data'] = fap.read()
        cache_info['mtime'] = mtime
        if reload_func:
            reload_func(cache_info['data'])
    return cache_info['data']
@contextlib.contextmanager
**** CubicPower OpenStack Study ****
def temporary_mutation(obj, **kwargs):
    """Temporarily set the attr on a particular object to a given value then
    revert when finished.
    One use of this is to temporarily set the read_deleted flag on a context
    object:
        with temporary_mutation(context, read_deleted="yes"):
            do_something_that_needed_deleted_objects()
    """
**** CubicPower OpenStack Study ****
    def is_dict_like(thing):
        return hasattr(thing, 'has_key')
**** CubicPower OpenStack Study ****
    def get(thing, attr, default):
        if is_dict_like(thing):
            return thing.get(attr, default)
        else:
            return getattr(thing, attr, default)
**** CubicPower OpenStack Study ****
    def set_value(thing, attr, val):
        if is_dict_like(thing):
            thing[attr] = val
        else:
            setattr(thing, attr, val)
**** CubicPower OpenStack Study ****
    def delete(thing, attr):
        if is_dict_like(thing):
            del thing[attr]
        else:
            delattr(thing, attr)
    NOT_PRESENT = object()
    old_values = {}
    for attr, new_value in kwargs.items():
        old_values[attr] = get(obj, attr, NOT_PRESENT)
        set_value(obj, attr, new_value)
    try:
        yield
    finally:
        for attr, old_value in old_values.items():
            if old_value is NOT_PRESENT:
                delete(obj, attr)
            else:
                set_value(obj, attr, old_value)
def generate_mac_address():
    """Generate an Ethernet MAC address."""
    # NOTE(vish): We would prefer to use 0xfe here to ensure that linux
    #             bridge mac addresses don't change, but it appears to
    #             conflict with libvirt, so we use the next highest octet
    #             that has the unicast and locally administered bits set
    #             properly: 0xfa.
    #             Discussion: https://bugs.launchpad.net/nova/+bug/921838
    mac = [0xfa, 0x16, 0x3e,
           random.randint(0x00, 0xff),
           random.randint(0x00, 0xff),
           random.randint(0x00, 0xff)]
    return ':'.join(map(lambda x: "%02x" % x, mac))
def read_file_as_root(file_path):
    """Secure helper to read file as root."""
    try:
        out, _err = execute('cat', file_path, run_as_root=True)
        return out
    except processutils.ProcessExecutionError:
        raise exception.FileNotFound(file_path=file_path)
@contextlib.contextmanager
def temporary_chown(path, owner_uid=None):
    """Temporarily chown a path.
    :param owner_uid: UID of temporary owner (defaults to current user)
    """
    if owner_uid is None:
        owner_uid = os.getuid()
    orig_uid = os.stat(path).st_uid
    if orig_uid != owner_uid:
        execute('chown', owner_uid, path, run_as_root=True)
    try:
        yield
    finally:
        if orig_uid != owner_uid:
            execute('chown', orig_uid, path, run_as_root=True)
def chown(path, owner_uid=None):
    """chown a path.
    :param owner_uid: UID of owner (defaults to current user)
    """
    if owner_uid is None:
        owner_uid = os.getuid()
    orig_uid = os.stat(path).st_uid
    if orig_uid != owner_uid:
        execute('chown', owner_uid, path, run_as_root=True)
@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'), str(e))
def walk_class_hierarchy(clazz, encountered=None):
    """Walk class hierarchy, yielding most derived classes first."""
    if not encountered:
        encountered = []
    for subclass in clazz.__subclasses__():
        if subclass not in encountered:
            encountered.append(subclass)
            # drill down to leaves first
            for subsubclass in walk_class_hierarchy(subclass, encountered):
                yield subsubclass
            yield subclass
**** CubicPower OpenStack Study ****
def generate_mac_address():
    """Generate an Ethernet MAC address."""
    # NOTE(vish): We would prefer to use 0xfe here to ensure that linux
    #             bridge mac addresses don't change, but it appears to
    #             conflict with libvirt, so we use the next highest octet
    #             that has the unicast and locally administered bits set
    #             properly: 0xfa.
    #             Discussion: https://bugs.launchpad.net/nova/+bug/921838
    mac = [0xfa, 0x16, 0x3e,
           random.randint(0x00, 0xff),
           random.randint(0x00, 0xff),
           random.randint(0x00, 0xff)]
    return ':'.join(map(lambda x: "%02x" % x, mac))
**** CubicPower OpenStack Study ****
def read_file_as_root(file_path):
    """Secure helper to read file as root."""
    try:
        out, _err = execute('cat', file_path, run_as_root=True)
        return out
    except processutils.ProcessExecutionError:
        raise exception.FileNotFound(file_path=file_path)
@contextlib.contextmanager
**** CubicPower OpenStack Study ****
def temporary_chown(path, owner_uid=None):
    """Temporarily chown a path.
    :param owner_uid: UID of temporary owner (defaults to current user)
    """
    if owner_uid is None:
        owner_uid = os.getuid()
    orig_uid = os.stat(path).st_uid
    if orig_uid != owner_uid:
        execute('chown', owner_uid, path, run_as_root=True)
    try:
        yield
    finally:
        if orig_uid != owner_uid:
            execute('chown', orig_uid, path, run_as_root=True)
**** CubicPower OpenStack Study ****
def chown(path, owner_uid=None):
    """chown a path.
    :param owner_uid: UID of owner (defaults to current user)
    """
    if owner_uid is None:
        owner_uid = os.getuid()
    orig_uid = os.stat(path).st_uid
    if orig_uid != owner_uid:
        execute('chown', owner_uid, path, run_as_root=True)
@contextlib.contextmanager
**** CubicPower OpenStack Study ****
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'), str(e))
**** CubicPower OpenStack Study ****
def walk_class_hierarchy(clazz, encountered=None):
    """Walk class hierarchy, yielding most derived classes first."""
    if not encountered:
        encountered = []
    for subclass in clazz.__subclasses__():
        if subclass not in encountered:
            encountered.append(subclass)
            # drill down to leaves first
            for subsubclass in walk_class_hierarchy(subclass, encountered):
                yield subsubclass
            yield subclass
**** CubicPower OpenStack Study ****
class UndoManager(object):
    """Provides a mechanism to facilitate rolling back a series of actions
    when an exception is raised.
    """
    
**** CubicPower OpenStack Study ****
    def __init__(self):
        self.undo_stack = []
**** CubicPower OpenStack Study ****
    def undo_with(self, undo_func):
        self.undo_stack.append(undo_func)
**** CubicPower OpenStack Study ****
    def _rollback(self):
        for undo_func in reversed(self.undo_stack):
            undo_func()
**** CubicPower OpenStack Study ****
    def rollback_and_reraise(self, msg=None, **kwargs):
        """Rollback a series of actions then re-raise the exception.
        .. note:: (sirp) This should only be called within an
                  exception handler.
        """
        with excutils.save_and_reraise_exception():
            if msg:
                LOG.exception(msg, **kwargs)
            self._rollback()
def mkfs(fs, path, label=None, run_as_root=False):
    """Format a file or block device
    :param fs: Filesystem type (examples include 'swap', 'ext3', 'ext4'
               'btrfs', etc.)
    :param path: Path to file or block device to format
    :param label: Volume label to use
    """
    if fs == 'swap':
        args = ['mkswap']
    else:
        args = ['mkfs', '-t', fs]
    #add -F to force no interactive execute on non-block device.
    if fs in ('ext3', 'ext4', 'ntfs'):
        args.extend(['-F'])
    if label:
        if fs in ('msdos', 'vfat'):
            label_opt = '-n'
        else:
            label_opt = '-L'
        args.extend([label_opt, label])
    args.append(path)
    execute(*args, run_as_root=run_as_root)
def last_bytes(file_like_object, num):
    """Return num bytes from the end of the file, and remaining byte count.
    :param file_like_object: The file to read
    :param num: The number of bytes to return
    :returns (data, remaining)
    """
    try:
        file_like_object.seek(-num, os.SEEK_END)
    except IOError as e:
        if e.errno == 22:
            file_like_object.seek(0, os.SEEK_SET)
        else:
            raise
    remaining = file_like_object.tell()
    return (file_like_object.read(), remaining)
def metadata_to_dict(metadata):
    result = {}
    for item in metadata:
        if not item.get('deleted'):
            result[item['key']] = item['value']
    return result
def dict_to_metadata(metadata):
    result = []
    for key, value in metadata.iteritems():
        result.append(dict(key=key, value=value))
    return result
def instance_meta(instance):
    if isinstance(instance['metadata'], dict):
        return instance['metadata']
    else:
        return metadata_to_dict(instance['metadata'])
def instance_sys_meta(instance):
    if not instance.get('system_metadata'):
        return {}
    if isinstance(instance['system_metadata'], dict):
        return instance['system_metadata']
    else:
        return metadata_to_dict(instance['system_metadata'])
def get_wrapped_function(function):
    """Get the method at the bottom of a stack of decorators."""
    if not hasattr(function, 'func_closure') or not function.func_closure:
        return function
**** CubicPower OpenStack Study ****
def mkfs(fs, path, label=None, run_as_root=False):
    """Format a file or block device
    :param fs: Filesystem type (examples include 'swap', 'ext3', 'ext4'
               'btrfs', etc.)
    :param path: Path to file or block device to format
    :param label: Volume label to use
    """
    if fs == 'swap':
        args = ['mkswap']
    else:
        args = ['mkfs', '-t', fs]
    #add -F to force no interactive execute on non-block device.
    if fs in ('ext3', 'ext4', 'ntfs'):
        args.extend(['-F'])
    if label:
        if fs in ('msdos', 'vfat'):
            label_opt = '-n'
        else:
            label_opt = '-L'
        args.extend([label_opt, label])
    args.append(path)
    execute(*args, run_as_root=run_as_root)
**** CubicPower OpenStack Study ****
def last_bytes(file_like_object, num):
    """Return num bytes from the end of the file, and remaining byte count.
    :param file_like_object: The file to read
    :param num: The number of bytes to return
    :returns (data, remaining)
    """
    try:
        file_like_object.seek(-num, os.SEEK_END)
    except IOError as e:
        if e.errno == 22:
            file_like_object.seek(0, os.SEEK_SET)
        else:
            raise
    remaining = file_like_object.tell()
    return (file_like_object.read(), remaining)
**** CubicPower OpenStack Study ****
def metadata_to_dict(metadata):
    result = {}
    for item in metadata:
        if not item.get('deleted'):
            result[item['key']] = item['value']
    return result
**** CubicPower OpenStack Study ****
def dict_to_metadata(metadata):
    result = []
    for key, value in metadata.iteritems():
        result.append(dict(key=key, value=value))
    return result
**** CubicPower OpenStack Study ****
def instance_meta(instance):
    if isinstance(instance['metadata'], dict):
        return instance['metadata']
    else:
        return metadata_to_dict(instance['metadata'])
**** CubicPower OpenStack Study ****
def instance_sys_meta(instance):
    if not instance.get('system_metadata'):
        return {}
    if isinstance(instance['system_metadata'], dict):
        return instance['system_metadata']
    else:
        return metadata_to_dict(instance['system_metadata'])
**** CubicPower OpenStack Study ****
def get_wrapped_function(function):
    """Get the method at the bottom of a stack of decorators."""
    if not hasattr(function, 'func_closure') or not function.func_closure:
        return function
**** CubicPower OpenStack Study ****
    def _get_wrapped_function(function):
        if not hasattr(function, 'func_closure') or not function.func_closure:
            return None
        for closure in function.func_closure:
            func = closure.cell_contents
            deeper_func = _get_wrapped_function(func)
            if deeper_func:
                return deeper_func
            elif hasattr(closure.cell_contents, '__call__'):
                return closure.cell_contents
    return _get_wrapped_function(function)
def expects_func_args(*args):
**** CubicPower OpenStack Study ****
def expects_func_args(*args):
**** CubicPower OpenStack Study ****
    def _decorator_checker(dec):
        @functools.wraps(dec)
        def _decorator(f):
            base_f = get_wrapped_function(f)
            arg_names, a, kw, _default = inspect.getargspec(base_f)
            if a or kw or set(args) <= set(arg_names):
                # NOTE (ndipanov): We can't really tell if correct stuff will
                # be passed if it's a function with *args or **kwargs so
                # we still carry on and hope for the best
                return dec(f)
            else:
                raise TypeError("Decorated function %(f_name)s does not "
                                "have the arguments expected by the "
                                "decorator %(d_name)s" %
                                {'f_name': base_f.__name__,
                                 'd_name': dec.__name__})
        return _decorator
    return _decorator_checker
**** CubicPower OpenStack Study ****
        def _decorator(f):
            base_f = get_wrapped_function(f)
            arg_names, a, kw, _default = inspect.getargspec(base_f)
            if a or kw or set(args) <= set(arg_names):
                # NOTE (ndipanov): We can't really tell if correct stuff will
                # be passed if it's a function with *args or **kwargs so
                # we still carry on and hope for the best
                return dec(f)
            else:
                raise TypeError("Decorated function %(f_name)s does not "
                                "have the arguments expected by the "
                                "decorator %(d_name)s" %
                                {'f_name': base_f.__name__,
                                 'd_name': dec.__name__})
        return _decorator
    return _decorator_checker
**** CubicPower OpenStack Study ****
class ExceptionHelper(object):
    """Class to wrap another and translate the ClientExceptions raised by its
    function calls to the actual ones.
    """
    
**** CubicPower OpenStack Study ****
    def __init__(self, target):
        self._target = target
**** CubicPower OpenStack Study ****
    def __getattr__(self, name):
        func = getattr(self._target, name)
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except messaging.ExpectedException as e:
                raise (e.exc_info[1], None, e.exc_info[2])
        return wrapper
def check_string_length(value, name, min_length=0, max_length=None):
    """Check the length of specified string
    :param value: the value of the string
    :param name: the name of the string
    :param min_length: the min_length of the string
    :param max_length: the max_length of the string
    """
    if not isinstance(value, six.string_types):
        msg = _("%s is not a string or unicode") % name
        raise exception.InvalidInput(message=msg)
    if len(value) < min_length:
        msg = _("%(name)s has a minimum character requirement of "
                "%(min_length)s.") % {'name': name, 'min_length': min_length}
        raise exception.InvalidInput(message=msg)
    if max_length and len(value) > max_length:
        msg = _("%(name)s has more than %(max_length)s "
                "characters.") % {'name': name, 'max_length': max_length}
        raise exception.InvalidInput(message=msg)
def validate_integer(value, name, min_value=None, max_value=None):
    """Make sure that value is a valid integer, potentially within range."""
    try:
        value = int(str(value))
    except (ValueError, UnicodeEncodeError):
        msg = _('%(value_name)s must be an integer')
        raise exception.InvalidInput(reason=(
            msg % {'value_name': name}))
    if min_value is not None:
        if value < min_value:
            msg = _('%(value_name)s must be >= %(min_value)d')
            raise exception.InvalidInput(
                reason=(msg % {'value_name': name,
                               'min_value': min_value}))
    if max_value is not None:
        if value > max_value:
            msg = _('%(value_name)s must be <= %(max_value)d')
            raise exception.InvalidInput(
                reason=(
                    msg % {'value_name': name,
                           'max_value': max_value})
            )
    return value
def spawn_n(func, *args, **kwargs):
    """Passthrough method for eventlet.spawn_n.
    This utility exists so that it can be stubbed for testing without
    interfering with the service spawns.
    """
    eventlet.spawn_n(func, *args, **kwargs)
def is_none_string(val):
    """Check if a string represents a None value.
    """
    if not isinstance(val, six.string_types):
        return False
    return val.lower() == 'none'
def convert_version_to_int(version):
    try:
        if isinstance(version, six.string_types):
            version = convert_version_to_tuple(version)
        if isinstance(version, tuple):
            return reduce(lambda x, y: (x * 1000) + y, version)
    except Exception:
        raise exception.NovaException(message="Hypervisor version invalid.")
def convert_version_to_str(version_int):
    version_numbers = []
    factor = 1000
    while version_int != 0:
        version_number = version_int - (version_int // factor * factor)
        version_numbers.insert(0, str(version_number))
        version_int = version_int / factor
    return reduce(lambda x, y: "%s.%s" % (x, y), version_numbers)
def convert_version_to_tuple(version_str):
    return tuple(int(part) for part in version_str.split('.'))
def is_neutron():
    global _IS_NEUTRON
    if _IS_NEUTRON is not None:
        return _IS_NEUTRON
    try:
        # compatibility with Folsom/Grizzly configs
        cls_name = CONF.network_api_class
        if cls_name == 'nova.network.quantumv2.api.API':
            cls_name = 'nova.network.neutronv2.api.API'
        from nova.network.neutronv2 import api as neutron_api
        _IS_NEUTRON = issubclass(importutils.import_class(cls_name),
                                 neutron_api.API)
    except ImportError:
        _IS_NEUTRON = False
    return _IS_NEUTRON
def reset_is_neutron():
    global _IS_NEUTRON
    _IS_NEUTRON = None
def is_auto_disk_config_disabled(auto_disk_config_raw):
    auto_disk_config_disabled = False
    if auto_disk_config_raw is not None:
        adc_lowered = auto_disk_config_raw.strip().lower()
        if adc_lowered == "disabled":
            auto_disk_config_disabled = True
    return auto_disk_config_disabled
def get_auto_disk_config_from_instance(instance=None, sys_meta=None):
    if sys_meta is None:
        sys_meta = instance_sys_meta(instance)
    return sys_meta.get("image_auto_disk_config")
def get_auto_disk_config_from_image_props(image_properties):
    return image_properties.get("auto_disk_config")
def get_system_metadata_from_image(image_meta, flavor=None):
    system_meta = {}
    prefix_format = SM_IMAGE_PROP_PREFIX + '%s'
    for key, value in image_meta.get('properties', {}).iteritems():
        new_value = unicode(value)[:255]
        system_meta[prefix_format % key] = new_value
    for key in SM_INHERITABLE_KEYS:
        value = image_meta.get(key)
        if key == 'min_disk' and flavor:
            if image_meta.get('disk_format') == 'vhd':
                value = flavor['root_gb']
            else:
                value = max(value, flavor['root_gb'])
        if value is None:
            continue
        system_meta[prefix_format % key] = value
    return system_meta
def get_image_from_system_metadata(system_meta):
    image_meta = {}
    properties = {}
    if not isinstance(system_meta, dict):
        system_meta = metadata_to_dict(system_meta)
    for key, value in system_meta.iteritems():
        if value is None:
            continue
        # NOTE(xqueralt): Not sure this has to inherit all the properties or
        # just the ones we need. Leaving it for now to keep the old behaviour.
        if key.startswith(SM_IMAGE_PROP_PREFIX):
            key = key[len(SM_IMAGE_PROP_PREFIX):]
        if key in SM_INHERITABLE_KEYS:
            image_meta[key] = value
        else:
            # Skip properties that are non-inheritable
            if key in CONF.non_inheritable_image_properties:
                continue
            properties[key] = value
    if properties:
        image_meta['properties'] = properties
    return image_meta
def get_hash_str(base_str):
    """returns string that represents hash of base_str (in hex format)."""
    return hashlib.md5(base_str).hexdigest()
def cpu_count():
    try:
        return multiprocessing.cpu_count()
    except NotImplementedError:
        return 1
**** CubicPower OpenStack Study ****
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except messaging.ExpectedException as e:
                raise (e.exc_info[1], None, e.exc_info[2])
        return wrapper
def check_string_length(value, name, min_length=0, max_length=None):
    """Check the length of specified string
    :param value: the value of the string
    :param name: the name of the string
    :param min_length: the min_length of the string
    :param max_length: the max_length of the string
    """
    if not isinstance(value, six.string_types):
        msg = _("%s is not a string or unicode") % name
        raise exception.InvalidInput(message=msg)
    if len(value) < min_length:
        msg = _("%(name)s has a minimum character requirement of "
                "%(min_length)s.") % {'name': name, 'min_length': min_length}
        raise exception.InvalidInput(message=msg)
    if max_length and len(value) > max_length:
        msg = _("%(name)s has more than %(max_length)s "
                "characters.") % {'name': name, 'max_length': max_length}
        raise exception.InvalidInput(message=msg)
def validate_integer(value, name, min_value=None, max_value=None):
    """Make sure that value is a valid integer, potentially within range."""
    try:
        value = int(str(value))
    except (ValueError, UnicodeEncodeError):
        msg = _('%(value_name)s must be an integer')
        raise exception.InvalidInput(reason=(
            msg % {'value_name': name}))
    if min_value is not None:
        if value < min_value:
            msg = _('%(value_name)s must be >= %(min_value)d')
            raise exception.InvalidInput(
                reason=(msg % {'value_name': name,
                               'min_value': min_value}))
    if max_value is not None:
        if value > max_value:
            msg = _('%(value_name)s must be <= %(max_value)d')
            raise exception.InvalidInput(
                reason=(
                    msg % {'value_name': name,
                           'max_value': max_value})
            )
    return value
def spawn_n(func, *args, **kwargs):
    """Passthrough method for eventlet.spawn_n.
    This utility exists so that it can be stubbed for testing without
    interfering with the service spawns.
    """
    eventlet.spawn_n(func, *args, **kwargs)
def is_none_string(val):
    """Check if a string represents a None value.
    """
    if not isinstance(val, six.string_types):
        return False
    return val.lower() == 'none'
def convert_version_to_int(version):
    try:
        if isinstance(version, six.string_types):
            version = convert_version_to_tuple(version)
        if isinstance(version, tuple):
            return reduce(lambda x, y: (x * 1000) + y, version)
    except Exception:
        raise exception.NovaException(message="Hypervisor version invalid.")
def convert_version_to_str(version_int):
    version_numbers = []
    factor = 1000
    while version_int != 0:
        version_number = version_int - (version_int // factor * factor)
        version_numbers.insert(0, str(version_number))
        version_int = version_int / factor
    return reduce(lambda x, y: "%s.%s" % (x, y), version_numbers)
def convert_version_to_tuple(version_str):
    return tuple(int(part) for part in version_str.split('.'))
def is_neutron():
    global _IS_NEUTRON
    if _IS_NEUTRON is not None:
        return _IS_NEUTRON
    try:
        # compatibility with Folsom/Grizzly configs
        cls_name = CONF.network_api_class
        if cls_name == 'nova.network.quantumv2.api.API':
            cls_name = 'nova.network.neutronv2.api.API'
        from nova.network.neutronv2 import api as neutron_api
        _IS_NEUTRON = issubclass(importutils.import_class(cls_name),
                                 neutron_api.API)
    except ImportError:
        _IS_NEUTRON = False
    return _IS_NEUTRON
def reset_is_neutron():
    global _IS_NEUTRON
    _IS_NEUTRON = None
def is_auto_disk_config_disabled(auto_disk_config_raw):
    auto_disk_config_disabled = False
    if auto_disk_config_raw is not None:
        adc_lowered = auto_disk_config_raw.strip().lower()
        if adc_lowered == "disabled":
            auto_disk_config_disabled = True
    return auto_disk_config_disabled
def get_auto_disk_config_from_instance(instance=None, sys_meta=None):
    if sys_meta is None:
        sys_meta = instance_sys_meta(instance)
    return sys_meta.get("image_auto_disk_config")
def get_auto_disk_config_from_image_props(image_properties):
    return image_properties.get("auto_disk_config")
def get_system_metadata_from_image(image_meta, flavor=None):
    system_meta = {}
    prefix_format = SM_IMAGE_PROP_PREFIX + '%s'
    for key, value in image_meta.get('properties', {}).iteritems():
        new_value = unicode(value)[:255]
        system_meta[prefix_format % key] = new_value
    for key in SM_INHERITABLE_KEYS:
        value = image_meta.get(key)
        if key == 'min_disk' and flavor:
            if image_meta.get('disk_format') == 'vhd':
                value = flavor['root_gb']
            else:
                value = max(value, flavor['root_gb'])
        if value is None:
            continue
        system_meta[prefix_format % key] = value
    return system_meta
def get_image_from_system_metadata(system_meta):
    image_meta = {}
    properties = {}
    if not isinstance(system_meta, dict):
        system_meta = metadata_to_dict(system_meta)
    for key, value in system_meta.iteritems():
        if value is None:
            continue
        # NOTE(xqueralt): Not sure this has to inherit all the properties or
        # just the ones we need. Leaving it for now to keep the old behaviour.
        if key.startswith(SM_IMAGE_PROP_PREFIX):
            key = key[len(SM_IMAGE_PROP_PREFIX):]
        if key in SM_INHERITABLE_KEYS:
            image_meta[key] = value
        else:
            # Skip properties that are non-inheritable
            if key in CONF.non_inheritable_image_properties:
                continue
            properties[key] = value
    if properties:
        image_meta['properties'] = properties
    return image_meta
def get_hash_str(base_str):
    """returns string that represents hash of base_str (in hex format)."""
    return hashlib.md5(base_str).hexdigest()
def cpu_count():
    try:
        return multiprocessing.cpu_count()
    except NotImplementedError:
        return 1