# Path: hardware/cmdb.py
# LOG = logging.getLogger('hardware.cmdb')
# class CmdbError(Exception):
# def cmdb_filename(cfg_dir, name):
# def load_cmdb(cfg_dir, name):
# def save_cmdb(cfg_dir, name, cmdb):
# def update_cmdb(cmdb, var, pref, forced_find):
#     def update_entry(entry, cmdb, idx):
# 
# Path: hardware/matcher.py
# LOG = logging.getLogger('hardware.matcher')
# _FUNC_REGEXP = re.compile(r'^([^(]+)'          # function name
#                           r'\(\s*([^,]+)'      # first argument
#                           r'(?:\s*,\s*(.+))?'  # remaining optional arguments
#                           r'\)$')              # last parenthesis
# def _adder(array, index, value):
# def _appender(array, index, value):
# def _range(elt, minval, maxval):
# def _gt(left, right):
# def _ge(left, right):
# def _lt(left, right):
# def _le(left, right):
# def _not(_, right):
# def _and(_, left, right):
# def _or(_, left, right):
# def _network(left, right):
# def _regexp(left, right):
# def _in(elt, *lst):
# def _call_func(func, implicit, res):
# def _extract_result(implicit, expr):
# def match_spec(spec, lines, arr, adder=_adder):
# def match_all(lines, specs, arr, arr2, debug=False, level=0):
# def match_multiple(lines, spec, arr):
# def generate_filename_and_macs(items):


import errno
import logging
import os
import pprint
import time
from hardware import cmdb
from hardware import matcher
        '''Lock a file and return a file descriptor.

Need to call unlock to release the lock.
        '''
        self._validate_lockname()
        count = 0
        while True:
            try:
                self._lock_fd = os.open(self._lockname,
                                        os.O_CREAT | os.O_EXCL | os.O_RDWR)
                break
            except OSError as xcpt:
                if xcpt.errno != errno.EEXIST:
                    raise
                if count % 30 == 0:
                    LOG.debug('waiting for lock %s' % self._lockname)
                time.sleep(1)
                count += 1
        return self._lock_fd

    def unlock(self):
        'Called after the lock function to release a lock.'
        if self._lock_fd:
            os.close(self._lock_fd)
            os.unlink(self._lockname)
            self._lock_fd = None

    @staticmethod
    def _get_value(lines, spec, key):
        info = {}

# Path: hardware/cmdb.py
# LOG = logging.getLogger('hardware.cmdb')
# class CmdbError(Exception):
# def cmdb_filename(cfg_dir, name):
# def load_cmdb(cfg_dir, name):
# def save_cmdb(cfg_dir, name, cmdb):
# def update_cmdb(cmdb, var, pref, forced_find):
#     def update_entry(entry, cmdb, idx):
# 
# Path: hardware/matcher.py
# LOG = logging.getLogger('hardware.matcher')
# _FUNC_REGEXP = re.compile(r'^([^(]+)'          # function name
#                           r'\(\s*([^,]+)'      # first argument
#                           r'(?:\s*,\s*(.+))?'  # remaining optional arguments
#                           r'\)$')              # last parenthesis
# def _adder(array, index, value):
# def _appender(array, index, value):
# def _range(elt, minval, maxval):
# def _gt(left, right):
# def _ge(left, right):
# def _lt(left, right):
# def _le(left, right):
# def _not(_, right):
# def _and(_, left, right):
# def _or(_, left, right):
# def _network(left, right):
# def _regexp(left, right):
# def _in(elt, *lst):
# def _call_func(func, implicit, res):
# def _extract_result(implicit, expr):
# def match_spec(spec, lines, arr, adder=_adder):
# def match_all(lines, specs, arr, arr2, debug=False, level=0):
# def match_multiple(lines, spec, arr):
# def generate_filename_and_macs(items):


# Path: hardware/state.py
import errno
import logging
import os
import pprint
import time
from hardware import cmdb
from hardware import matcher

        '''Lock a file and return a file descriptor.

Need to call unlock to release the lock.
        '''
        self._validate_lockname()
        count = 0
        while True:
            try:
                self._lock_fd = os.open(self._lockname,
                                        os.O_CREAT | os.O_EXCL | os.O_RDWR)
                break
            except OSError as xcpt:
                if xcpt.errno != errno.EEXIST:
                    raise
                if count % 30 == 0:
                    LOG.debug('waiting for lock %s' % self._lockname)
                time.sleep(1)
                count += 1
        return self._lock_fd

    def unlock(self):
        'Called after the lock function to release a lock.'
        if self._lock_fd:
            os.close(self._lock_fd)
            os.unlink(self._lockname)
            self._lock_fd = None

    @staticmethod
    def _get_value(lines, spec, key):
        info = {}

        if (matcher.match_spec(spec, lines, info)