<commit_before>import json
import os
import shutil
import subprocess
import sys
import time
import tempfile
import unittest

from contextlib import closing
from StringIO import StringIO

from volttron.platform import aip
from volttron.platform.control import server
from volttron.platform import packaging

try:
    from volttron.restricted import (auth, certs)
except ImportError:
    auth = None
    certs = None
    

# from volttron.platform.control import (CTL_STATUS,
#                                        CTL_INSTALL,
#                                        CTL_STATUS,
#                                        CTL_START,
#                                        CTL_STOP)

#All paths relative to proj-dir/volttron
# INST_EXEC = "install"
# REM_EXEC = "remove-executable"
# LOAD_AGENT = "load-agent"
# UNLOAD_AGENT = "unload-agent"
# LIST_AGENTS = "list-agents"
# STOP_AGENT = "stop-agent"
# START_AGENT = "start-agent"
# BUILD_AGENT = "volttron/scripts/build-agent.sh"

#Filenames for the config files which are created during setup and then
#passed on the command line
TMP_PLATFORM_CONFIG_FILENAME = "test-config.ini"
TMP_SMAP_CONFIG_FILENAME = "test-smap.ini"

#Used to fill in TWISTED_CONFIG template
TEST_CONFIG_FILE = 'base-platform-test.json'

PLATFORM_CONFIG_UNRESTRICTED = """
no-resource-monitor
no-verify
no-mobility

control-socket = {tmpdir}/run/control
"""


PLATFORM_CONFIG_RESTRICTED = """
mobility-address = {mobility-address}
"""



TWISTED_CONFIG = """
[report 0]
ReportDeliveryLocation = {smap-uri}/add/{smap-key}

[/]
type = Collection
Metadata/SourceName = {smap-source}
uuid = {smap-uuid}

[/datalogger]
type = volttron.drivers.data_logger.DataLogger
interval = 1

"""

UNRESTRICTED = 0
VERIFY_ONLY = 1
RESOURCE_CHECK_ONLY = 2
RESTRICTED = 3

MODES = (UNRESTRICTED, VERIFY_ONLY, RESOURCE_CHECK_ONLY, RESTRICTED)

rel_path = './'

VSTART = os.path.join(rel_path, "env/bin/volttron")
VCTRL = os.path.join(rel_path, "env/bin/volttron-ctl")
print VSTART

class BasePlatformTest(unittest.TestCase):

    def setUp(self): 
        self.tmpdir = tempfile.mkdtemp()
        self.wheelhouse = '/'.join((self.tmpdir, 'wheelhouse'))
        os.makedirs(self.wheelhouse)
        os.environ['VOLTTRON_HOME'] = self.tmpdir
        
        opts = type('Options', (), {'verify_agents': False, 'volttron_home': self.tmpdir})()
        self.test_aip = aip.AIPplatform(opts)
        self.test_aip.setup()
        
        
    def setup_connector(self):
        path = os.path.expandvars('$VOLTTRON_HOME/run/control')
        
        tries = 0
        max_tries = 5
        while(not os.path.exists(path) and tries < max_tries):
            time.sleep(5)
            tries += 1
            
        self.conn= server.ControlConnector(path)
    
    def startup_platform(self, platform_config, volttron_home=None, use_twistd = True, mode=UNRESTRICTED):
        
        
        
        try:
            config = json.loads(open(platform_config, 'r').read())
        except Exception as e:
            sys.stderr.write (str(e))
            self.fail("Could not load configuration file for tests", e)
        
#         self.tmpdir = tempfile.mkdtemp()<commit_msg>fixed a fail issue.<commit_after>import json
import os
import shutil
import subprocess
import sys
import time
import tempfile
import unittest

from contextlib import closing
from StringIO import StringIO

from volttron.platform import aip
from volttron.platform.control import server
from volttron.platform import packaging

try:
    from volttron.restricted import (auth, certs)
except ImportError:
    auth = None
    certs = None
    

# from volttron.platform.control import (CTL_STATUS,
#                                        CTL_INSTALL,
#                                        CTL_STATUS,
#                                        CTL_START,
#                                        CTL_STOP)

#All paths relative to proj-dir/volttron
# INST_EXEC = "install"
# REM_EXEC = "remove-executable"
# LOAD_AGENT = "load-agent"
# UNLOAD_AGENT = "unload-agent"
# LIST_AGENTS = "list-agents"
# STOP_AGENT = "stop-agent"
# START_AGENT = "start-agent"
# BUILD_AGENT = "volttron/scripts/build-agent.sh"

#Filenames for the config files which are created during setup and then
#passed on the command line
TMP_PLATFORM_CONFIG_FILENAME = "test-config.ini"
TMP_SMAP_CONFIG_FILENAME = "test-smap.ini"

#Used to fill in TWISTED_CONFIG template
TEST_CONFIG_FILE = 'base-platform-test.json'

PLATFORM_CONFIG_UNRESTRICTED = """
no-resource-monitor
no-verify
no-mobility

control-socket = {tmpdir}/run/control
"""


PLATFORM_CONFIG_RESTRICTED = """
mobility-address = {mobility-address}
"""



TWISTED_CONFIG = """
[report 0]
ReportDeliveryLocation = {smap-uri}/add/{smap-key}

[/]
type = Collection
Metadata/SourceName = {smap-source}
uuid = {smap-uuid}

[/datalogger]
type = volttron.drivers.data_logger.DataLogger
interval = 1

"""

UNRESTRICTED = 0
VERIFY_ONLY = 1
RESOURCE_CHECK_ONLY = 2
RESTRICTED = 3

MODES = (UNRESTRICTED, VERIFY_ONLY, RESOURCE_CHECK_ONLY, RESTRICTED)

rel_path = './'

VSTART = os.path.join(rel_path, "env/bin/volttron")
VCTRL = os.path.join(rel_path, "env/bin/volttron-ctl")
print VSTART

class BasePlatformTest(unittest.TestCase):

    def setUp(self): 
        self.tmpdir = tempfile.mkdtemp()
        self.wheelhouse = '/'.join((self.tmpdir, 'wheelhouse'))
        os.makedirs(self.wheelhouse)
        os.environ['VOLTTRON_HOME'] = self.tmpdir
        
        opts = type('Options', (), {'verify_agents': False, 'volttron_home': self.tmpdir})()
        self.test_aip = aip.AIPplatform(opts)
        self.test_aip.setup()
        
        
    def setup_connector(self):
        path = os.path.expandvars('$VOLTTRON_HOME/run/control')
        
        tries = 0
        max_tries = 5
        while(not os.path.exists(path) and tries < max_tries):
            time.sleep(5)
            tries += 1
            
        self.conn= server.ControlConnector(path)
    
    def startup_platform(self, platform_config, volttron_home=None, use_twistd = True, mode=UNRESTRICTED):
        
        
        
        try:
            config = json.loads(open(platform_config, 'r').read())
        except Exception as e:
            sys.stderr.write (str(e))
            self.fail("Could not load configuration file for tests {}".format(e))
        
#         self.tmpdir = tempfile.mkdtemp()
{'fraction_non_alphanumeric': 0.10680677540777918, 'fraction_numerical': 0.003136762860727729, 'mean_word_length': 2.7162004662004664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6778327', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2032, 'n_words': 440}