def __init__()

in automation/tinc/main/ext/qautils/gppylib/gphostcache.py [0:0]


    def __init__(self, pool, interfacesToLookup, currentHostNameAnswersForInterfaces):
        self.__hostCache={}         # interface -> hostname

        # Read the .gphostcache file if it exists
        if os.path.isfile(CACHEFILE):
            try:
                for line in readAllLinesFromFile(CACHEFILE, stripLines=True, skipEmptyLines=True):
                    if line[0] == '#': # okay check because empty lines are skipped
                        continue

                    arr = line.split(':')
                    if len(arr) == 2 and len(arr[0]) > 0 and len(arr[1]) > 0:
                        (interface, hostname) = arr
                        self.__hostCache[interface.strip()]=hostname.strip()
            except Exception, e:
                logger.warn("Error reading file '%s': %s" % (CACHEFILE, str(e)))

        #
        # check to see which values are inconsistent with the cache and need lookup again
        #
        inconsistent = []
        for i in range(len(interfacesToLookup)):
            interface = interfacesToLookup[i]
            hostname = currentHostNameAnswersForInterfaces[i]

            # If we don't have this mapping yet set it, otherwise we simply
            # validate consistency.
            if interface not in self.__hostCache:
                self.__hostCache[interface] = hostname
            elif hostname is None:
                # external source did not have a tentative answer, the first
                # case above should have fired for the first entry on this
                # interface and will force us to lookup the hostname.  
                # Additional hits on the interface can be ignored.
                pass
            elif self.__hostCache[interface] is None:
                self.__hostCache[interface] = hostname
            elif self.__hostCache[interface] != hostname:
                logger.warn("inconsistent hostname '%s' for interface '%s' and expected hostname '%s'" % \
                            (self.__hostCache[interface], interface, hostname))
                inconsistent.append(interface)

        # Clear out any inconsistent hostnames to force a recheck.
        for i in inconsistent:
            self.__hostCache[i] = None

        # Lookup any hostnames that we don't have answers for:
        pending_cmds={}
        for interface in self.__hostCache:
            if self.__hostCache[interface] is None:
                logger.debug("hostname lookup for %s" % interface)
                cmd=unix.Hostname('host lookup', ctxt=base.REMOTE, remoteHost=interface)
                pool.addCommand(cmd)
                pending_cmds[interface] = cmd

        # Fetch the results out of the WorkerPool
        if len(pending_cmds) > 0:
            pool.join()

            for interface in pending_cmds:
                cmd = pending_cmds[interface]

                # Make sure the command completed successfully
                if cmd.get_results().rc != 0:
                    logger.warn("Failed to resolve hostname for %s" % interface)
                    continue

                self.__hostCache[interface] = cmd.get_hostname()

            pool.empty_completed_items()

        # Try to update the hostcache file if we executed any hostname commands
        if len(pending_cmds) > 0:
            try:
                fp = open(CACHEFILE, 'w')

                for interface in sorted(self.__hostCache.keys()):
                    hostname = self.__hostCache[interface]

                    # skip any dangling references we still have
                    if not hostname:
                        continue
                    fp.write("%s:%s\n" % (interface, hostname))

                fp.close()
            except Exception, e:
                logger.warn(str(e))
                logger.warn("Failed to write file '%s'" % CACHEFILE)