<commit_before>#!/usr/bin/python
#
# Copyright (c)2011 the Boeing Company.
#
# author: <NAME> <<EMAIL>>
#
# HIP automated testing script, for use as a 'make check' target.
# This script requires that CORE be installed, see:
#     http://code.google.com/p/coreemu/
#


HIP_CFG_CACHE="~/.hiptest"
HIP_CFG_DIR="/usr/local/etc/hip"

import optparse, sys, os, datetime, time, shutil, re
from core import pycore
from core.misc import ipaddr
from core.misc.utils import check_call, mutecheck_call
from core.misc.ipaddr import *

# node list (count from 1)
nodes = [None]


class HipSession(pycore.Session):
    def buildknownhosts(self):
        ''' Collect HITs from HipNodes into a known_host_identities.xml file,
            and deploy that file to all nodes.
        '''
        cfgbase = os.path.expanduser(HIP_CFG_CACHE)
        knownhosts = os.path.join(cfgbase, "known_host_identities.xml")
        kh = open(knownhosts, "w")
        kh.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
        kh.write("<known_host_identities>\n")
        # collect
        for n in self.objs():
            if not isinstance(n, HipNode):
                continue
            pub = os.path.join(n.nodedir, "%s_host_identities.pub.xml" % n.name)
            f = open(pub, "r")
            publines = f.readlines()
            publines.insert(-2, "    <addr>%s</addr>\n" % n.getpreferredaddr())
            kh.write("".join(publines[3:-1]))
            f.close()
        kh.write("</known_host_identities>\n")
        kh.close()
        # deploy
        for n in self.objs():
            if not isinstance(n, HipNode):
                continue           
            dst = os.path.join(cfgbase, n.name, "known_host_identities.xml")
            shutil.copy(knownhosts, dst) 




class HipNode(pycore.nodes.LxcNode):
    def __init__(self, *args, **kwds):
        super(HipNode, self).__init__(*args, **kwds)
        self.hip_path = None
        self.addtap(name="hip0")

    def addtap(self, name):
        ''' Install a TAP device into the namespace for use by the HIP daemon.
        '''
        localname = "%s.%s" % (self.name, name)
        mutecheck_call(["tunctl", "-t", localname])
        tap = pycore.nodes.TunTap(node=self, name=name, localname=localname)
        tap.install()

    def hitgen(self):
        ''' Generate a HIP config for the given node. First check for cached
        config under HIP_CFG_CACHE.
        '''<commit_msg>more gracefully handle missing tunctl utility<commit_after>#!/usr/bin/python
#
# Copyright (c)2011 the Boeing Company.
#
# author: <NAME> <<EMAIL>>
#
# HIP automated testing script, for use as a 'make check' target.
# This script requires that CORE be installed, see:
#     http://code.google.com/p/coreemu/
#


HIP_CFG_CACHE="~/.hiptest"
HIP_CFG_DIR="/usr/local/etc/hip"

import optparse, sys, os, datetime, time, shutil, re
from core import pycore
from core.misc import ipaddr
from core.misc.utils import check_call, mutecheck_call
from core.misc.ipaddr import *

# node list (count from 1)
nodes = [None]


class HipSession(pycore.Session):
    def buildknownhosts(self):
        ''' Collect HITs from HipNodes into a known_host_identities.xml file,
            and deploy that file to all nodes.
        '''
        cfgbase = os.path.expanduser(HIP_CFG_CACHE)
        knownhosts = os.path.join(cfgbase, "known_host_identities.xml")
        kh = open(knownhosts, "w")
        kh.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
        kh.write("<known_host_identities>\n")
        # collect
        for n in self.objs():
            if not isinstance(n, HipNode):
                continue
            pub = os.path.join(n.nodedir, "%s_host_identities.pub.xml" % n.name)
            f = open(pub, "r")
            publines = f.readlines()
            publines.insert(-2, "    <addr>%s</addr>\n" % n.getpreferredaddr())
            kh.write("".join(publines[3:-1]))
            f.close()
        kh.write("</known_host_identities>\n")
        kh.close()
        # deploy
        for n in self.objs():
            if not isinstance(n, HipNode):
                continue           
            dst = os.path.join(cfgbase, n.name, "known_host_identities.xml")
            shutil.copy(knownhosts, dst) 




class HipNode(pycore.nodes.LxcNode):
    def __init__(self, *args, **kwds):
        super(HipNode, self).__init__(*args, **kwds)
        self.hip_path = None
        self.addtap(name="hip0")

    def addtap(self, name):
        ''' Install a TAP device into the namespace for use by the HIP daemon.
        '''
        localname = "%s.%s" % (self.name, name)
        try:
            mutecheck_call(["tunctl", "-t", localname])
        except OSError, e:
            print "error creating TUN/TAP device '%s':\n\t%s" % (localname, e)
            print "make sure the 'tunctl' utility is installed"
            sys.exit(1)
        tap = pycore.nodes.TunTap(node=self, name=name, localname=localname)
        tap.install()

    def hitgen(self):
        ''' Generate a HIP config for the given node. First check for cached
        config under HIP_CFG_CACHE.
        '''
{'fraction_non_alphanumeric': 0.1333854879718365, 'fraction_numerical': 0.004889497359671426, 'mean_word_length': 2.3556430446194225, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 2, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5188284', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1648, 'n_words': 410}