<commit_before># -*- coding: utf-8 -*-
"""
Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U

This file is part of fiware-orion-pep

fiware-orion-pep is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.

fiware-orion-pep is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with fiware-orion-pep.
If not, seehttp://www.gnu.org/licenses/.

For those usages not covered by the GNU Affero General Public License
please contact with::[<EMAIL>]
"""

__author__ = 'Jon'

import time
import subprocess
import os
import collections
import platform
from iotqautils.accessControl import AC
from iotqautils.idm_keystone import IdmUtils
from lettuce import world
from deploy_pep import *


def show_times(init_value):
    """
    shows the time duration of the entire test
    :param init_value: initial time
    """
    print "**************************************************************"
    print "Initial (date & time): " + str(init_value)
    print "Final   (date & time): " + str(time.strftime("%c"))
    print "**************************************************************"


def start_pep_app():
    """
    Depending of the method defined in properties" start PEP with the config defined
    :return:
    """
    if world.environment == 'docker':
        start_docker_pep(world.docker_ip, world.docker_user, world.docker_password,
                         world.docker_pep_user, world.docker_pep_password,
                         world.docker_pep_container, world.pep_path)
    if world.environment == 'remote':
        start_pep(world.pep_host_ip, world.pep_host_user, world.pep_host_password, pep_path=world.pep_path)


def start_mock(filename, ip, port):
    """
    Start the mock where the PEP will redirect the petition if pass the authentication
    :param filename:
    :param ip:
    :param port:
    :return:
    """
    path, fl = os.path.split(os.path.realpath(__file__))
    if platform.system() == 'Windows':
        path = path[0:path.rfind('\\')] + '\\tools\\mocks\\'
    elif platform.system() == 'Linux':
        path = path[0:path.rfind('/')] + '/tools/mocks/'
    else:
        raise NameError('The SO is not recognize, start the mock manually')
    # path += '\\mocks\\'
    DEVNULL = open(os.devnull, 'wb')
    return subprocess.Popen('python %s%s %s %s' % (path, filename, ip, port), stdout=DEVNULL, stderr=DEVNULL)


def stop_process(process):
    """
    Given a "subprocess" process, kill it and its children process in the correct SO
    :param process:
    :return:
    """
    if platform.system() == 'Windows':
        subprocess.Popen(['taskkill', '/F', '/T', '/PID', str(process.pid)])
    elif platform.system() == 'Linux':
        subprocess.Popen(['kill', '-9', str(process.pid)])
    else:
        raise NameError('The SO is not recognize, stop the process manually')


def start_proxy(ip_proxy, port_proxy, ip_destination, port_destination):
    """
    Start proxys in windows or linux
    :param ip_proxy:
    :param port_proxy:
    :param ip_destination:
    :param port_destination:
    :return:
    """
    path, fl = os.path.split(os.path.realpath(__file__))
    if platform.system() == 'Windows':
        path = path[0:path.rfind('\\')] + '\\tools\\mocks\\'
    elif platform.system() == 'Linux':
        path = path[0:path.rfind('/')] + '/tools/mocks/'
    else:
        raise NameError('The SO is not recognize, start the proxys manually')
    DEVNULL = open(os.devnull, 'wb')
    proxy_proc = subprocess.Popen(
        'python %sproxy.py %s %s %s %s' % (path, ip_proxy, port_proxy, ip_destination, port_destination),
        stdout=DEVNULL, stderr=DEVNULL)
    return proxy_proc


def initialize_keystone(platform, environment):
    """
    Initialize the keystone environment needed to the tests
    :param platform:
    :param environment:
    :return:<commit_msg>Fixed start proxys/mocks in Centos/Redhat<commit_after># -*- coding: utf-8 -*-
"""
Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U

This file is part of fiware-orion-pep

fiware-orion-pep is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.

fiware-orion-pep is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with fiware-orion-pep.
If not, seehttp://www.gnu.org/licenses/.

For those usages not covered by the GNU Affero General Public License
please contact with::[<EMAIL>]
"""

__author__ = 'Jon'

import time
import subprocess
import os
import collections
import platform
from iotqautils.accessControl import AC
from iotqautils.idm_keystone import IdmUtils
from lettuce import world
from deploy_pep import *


def show_times(init_value):
    """
    shows the time duration of the entire test
    :param init_value: initial time
    """
    print "**************************************************************"
    print "Initial (date & time): " + str(init_value)
    print "Final   (date & time): " + str(time.strftime("%c"))
    print "**************************************************************"


def start_pep_app():
    """
    Depending of the method defined in properties" start PEP with the config defined
    :return:
    """
    if world.environment == 'docker':
        start_docker_pep(world.docker_ip, world.docker_user, world.docker_password,
                         world.docker_pep_user, world.docker_pep_password,
                         world.docker_pep_container, world.pep_path)
    if world.environment == 'remote':
        start_pep(world.pep_host_ip, world.pep_host_user, world.pep_host_password, pep_path=world.pep_path)


def start_mock(filename, ip, port):
    """
    Start the mock where the PEP will redirect the petition if pass the authentication
    :param filename:
    :param ip:
    :param port:
    :return:
    """
    path, fl = os.path.split(os.path.realpath(__file__))
    if platform.system() == 'Windows':
        path = path[0:path.rfind('\\')] + '\\tools\\mocks\\'
    elif platform.system() == 'Linux':
        path = path[0:path.rfind('/')] + '/tools/mocks/'
    else:
        raise NameError('The SO is not recognize, start the mock manually')
    # path += '\\mocks\\'
    DEVNULL = open(os.devnull, 'wb')
    command = ('python %s%s %s %s' % (path, filename, ip, port)).split(' ')
    return subprocess.Popen(command, stdout=DEVNULL, stderr=DEVNULL)


def stop_process(process):
    """
    Given a "subprocess" process, kill it and its children process in the correct SO
    :param process:
    :return:
    """
    if platform.system() == 'Windows':
        subprocess.Popen(['taskkill', '/F', '/T', '/PID', str(process.pid)])
    elif platform.system() == 'Linux':
        subprocess.Popen(['kill', '-9', str(process.pid)])
    else:
        raise NameError('The SO is not recognize, stop the process manually')


def start_proxy(ip_proxy, port_proxy, ip_destination, port_destination):
    """
    Start proxys in windows or linux
    :param ip_proxy:
    :param port_proxy:
    :param ip_destination:
    :param port_destination:
    :return:
    """
    path, fl = os.path.split(os.path.realpath(__file__))
    if platform.system() == 'Windows':
        path = path[0:path.rfind('\\')] + '\\tools\\mocks\\'
    elif platform.system() == 'Linux':
        path = path[0:path.rfind('/')] + '/tools/mocks/'
    else:
        raise NameError('The SO is not recognize, start the proxys manually')
    DEVNULL = open(os.devnull, 'wb')
    command = ('python %sproxy.py %s %s %s %s' % (path, ip_proxy, port_proxy, ip_destination, port_destination)).split(' ')
    proxy_proc = subprocess.Popen(command, stdout=DEVNULL, stderr=DEVNULL)
    return proxy_proc


def initialize_keystone(platform, environment):
    """
    Initialize the keystone environment needed to the tests
    :param platform:
    :param environment:
    :return:
{'fraction_non_alphanumeric': 0.14280730317478776, 'fraction_numerical': 0.0025584370275613445, 'mean_word_length': 3.4698544698544698, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4654653', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2554, 'n_words': 822}