<commit_before>########
# Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

__author__ = 'idanmo'

import unittest
import shutil
import tempfile
from os import path
import subprocess
import logging
import os
import sys
import cosmo
import time
import threading
import re
import requests

root = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s [%(levelname)s] %(message)s', datefmt='%H:%M:%S')
ch.setFormatter(formatter)
root.addHandler(ch)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class ManagerRestProcess(object):

    def __init__(self, port, workflow_service_base_uri):
        self.process = None
        self.port = port
        self.workflow_service_base_uri = workflow_service_base_uri

    def start(self, timeout=10):
        endtime = time.time() + timeout

        manager_rest_command = [
            sys.executable,
            self.locate_manager_rest(),
            '--port', self.port,
            '--workflow_service_base_uri', self.workflow_service_base_uri
        ]

        logger.info('Starting manager-rest with: {0}'.format(manager_rest_command))

        self.process = subprocess.Popen(manager_rest_command)
        started = False
        attempt = 1
        while not started and time.time() < endtime:
            time.sleep(1)
            logger.info('Testing connection to manager rest service. (Attempt: {0}/{1})'.format(attempt, timeout))
            attempt += 1
            started = self.started()
        if not started:
            raise RuntimeError('Failed opening connection to manager rest service')

    def started(self):
        try:
            requests.get('http://localhost:8100/blueprints')
            return True
        except BaseException:
            return False

    def close(self):
        if not self.process is None:
            self.process.terminate()

    def locate_manager_rest(self):
        # start with current location
        manager_rest_location = path.abspath(__file__)
        # get to cosmo-manager
        for i in range(3):
            manager_rest_location = path.dirname(manager_rest_location)
        # build way into manager_rest
        return path.join(manager_rest_location, 'manager-rest/manager_rest/server.py')

class RuoteServiceProcess(object):

    JRUBY_VERSION = '1.7.3'

    def __init__(self, port=8101):
        self._pid = None
        self._port = port
        self._use_rvm = self._verify_ruby_environment()

    def _get_installed_ruby_packages(self):
        pass

    def _verify_ruby_environment(self):
        """ Verifies there's a valid JRuby environment.
        RuntimeError is raised if not, otherwise returns a boolean value which indicates
        whether RVM should be used for changing the current ruby environment before starting the service.
        """
        command = ['ruby', '--version']
        try:
            if self.JRUBY_VERSION in subprocess.check_output(command):
                return False
        except subprocess.CalledProcessError:
            pass

        command = ['rvm', 'list']
        jruby_version = "jruby-{0}".format(self.JRUBY_VERSION)
        try:
            if jruby_version in subprocess.check_output(command):
                return True
        except subprocess.CalledProcessError:
            pass

        raise RuntimeError("Invalid ruby environment [required -> JRuby {0}]".format(self.JRUBY_VERSION))

    def _verify_service_responsiveness(self):
        import urllib2
        service_url = "http://localhost:{0}".format(self._port)
        up = False
        try:
            res = urllib2.urlopen(service_url)
            up = res.code == 200
        except BaseException:
            pass
        if not up:
            raise RuntimeError("Ruote service is not responding @ {0}".format(service_url))

    def _verify_service_started(self, timeout=30):
        pid_pattern = ".*WEBrick::HTTPServer#start:\spid=(\d*)"
        deadline = time.time() + timeout
        while time.time() < deadline:
            line = self._process.stdout.readline().rstrip()
            if line != '':
                match = re.match(pid_pattern, line)
                if match:
                    self._pid = int(match.group(1))
                    break
        if self._pid is None:
            raise RuntimeError("Failed to start ruote service within a {0} seconds timeout".format(timeout))

    def start(self):
        startup_script_path = path.realpath(path.join(path.dirname(__file__), '..'))
        script = path.join(startup_script_path, 'run_ruote_service.sh')
        command = [script, str(self._use_rvm).lower(), str(self._port)]

        logger.info("Starting Ruote service")<commit_msg>CLOUDIFY-2222 show output from rest service when it starts<commit_after>########
# Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

__author__ = 'idanmo'

import unittest
import shutil
import tempfile
from os import path
import subprocess
import logging
import os
import sys
import cosmo
import time
import threading
import re
import requests

root = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s [%(levelname)s] %(message)s', datefmt='%H:%M:%S')
ch.setFormatter(formatter)
root.addHandler(ch)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class ManagerRestProcess(object):

    def __init__(self, port, workflow_service_base_uri):
        self.process = None
        self.port = port
        self.workflow_service_base_uri = workflow_service_base_uri

    def start(self, timeout=10):
        endtime = time.time() + timeout

        manager_rest_command = [
            sys.executable,
            self.locate_manager_rest(),
            '--port', self.port,
            '--workflow_service_base_uri', self.workflow_service_base_uri
        ]

        logger.info('Starting manager-rest with: {0}'.format(manager_rest_command))

        self.process = subprocess.Popen(manager_rest_command)
        started = False
        attempt = 1
        while not started and time.time() < endtime:
            time.sleep(1)
            logger.info('Testing connection to manager rest service. (Attempt: {0}/{1})'.format(attempt, timeout))
            attempt += 1
            started = self.started()
        if not started:
            raise RuntimeError('Failed opening connection to manager rest service')

    def started(self):
        try:
            requests.get('http://localhost:8100/blueprints')
            return True
        except BaseException:
            return False

    def close(self):
        if not self.process is None:
            self.process.terminate()

    def locate_manager_rest(self):
        # start with current location
        manager_rest_location = path.abspath(__file__)
        # get to cosmo-manager
        for i in range(3):
            manager_rest_location = path.dirname(manager_rest_location)
        # build way into manager_rest
        return path.join(manager_rest_location, 'manager-rest/manager_rest/server.py')

class RuoteServiceProcess(object):

    JRUBY_VERSION = '1.7.3'

    def __init__(self, port=8101):
        self._pid = None
        self._port = port
        self._use_rvm = self._verify_ruby_environment()

    def _get_installed_ruby_packages(self):
        pass

    def _verify_ruby_environment(self):
        """ Verifies there's a valid JRuby environment.
        RuntimeError is raised if not, otherwise returns a boolean value which indicates
        whether RVM should be used for changing the current ruby environment before starting the service.
        """
        command = ['ruby', '--version']
        try:
            if self.JRUBY_VERSION in subprocess.check_output(command):
                return False
        except subprocess.CalledProcessError:
            pass

        command = ['rvm', 'list']
        jruby_version = "jruby-{0}".format(self.JRUBY_VERSION)
        try:
            if jruby_version in subprocess.check_output(command):
                return True
        except subprocess.CalledProcessError:
            pass

        raise RuntimeError("Invalid ruby environment [required -> JRuby {0}]".format(self.JRUBY_VERSION))

    def _verify_service_responsiveness(self):
        import urllib2
        service_url = "http://localhost:{0}".format(self._port)
        up = False
        try:
            res = urllib2.urlopen(service_url)
            up = res.code == 200
        except BaseException:
            pass
        if not up:
            raise RuntimeError("Ruote service is not responding @ {0}".format(service_url))

    def _verify_service_started(self, timeout=30):
        pid_pattern = ".*WEBrick::HTTPServer#start:\spid=(\d*)"
        deadline = time.time() + timeout
        while time.time() < deadline:
            line = self._process.stdout.readline().rstrip()
            if line != '':
                logger.info('[route] %s', line)
                match = re.match(pid_pattern, line)
                if match:
                    self._pid = int(match.group(1))
                    break
        if self._pid is None:
            raise RuntimeError("Failed to start ruote service within a {0} seconds timeout".format(timeout))

    def start(self):
        startup_script_path = path.realpath(path.join(path.dirname(__file__), '..'))
        script = path.join(startup_script_path, 'run_ruote_service.sh')
        command = [script, str(self._use_rvm).lower(), str(self._port)]

        logger.info("Starting Ruote service")
{'fraction_non_alphanumeric': 0.09567071474923519, 'fraction_numerical': 0.007972559562436267, 'mean_word_length': 2.5816733067729083, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '3486278', 'n_tokens_mistral': 3065, 'n_tokens_neox': 2917, 'n_words': 822}