<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.

import os
from os.path import dirname
import tempfile
import shutil
import tarfile
import filecmp

import testtools

from cloudify.exceptions import NonRecoverableError
from cloudify.mocks import MockCloudifyContext
from cloudify.utils import LocalCommandRunner
from cloudify.utils import setup_default_logger
from plugin_installer.tasks import install, get_url, update_includes, \
    parse_pip_version, is_pip6_or_higher, extract_plugin_dir
from cloudify.constants import CELERY_WORK_DIR_PATH_KEY
from cloudify.constants import VIRTUALENV_PATH_KEY
from cloudify.constants import LOCAL_IP_KEY
from cloudify.constants import MANAGER_FILE_SERVER_BLUEPRINTS_ROOT_URL_KEY
from plugin_installer.tests.file_server import FileServer
from plugin_installer.tests.file_server import PORT


logger = setup_default_logger('test_plugin_installer')

MOCK_PLUGIN = 'mock-plugin'
MOCK_PLUGIN_WITH_DEPENDENCIES = 'mock-with-dependencies-plugin'
ZIP_SUFFIX = 'zip'
TAR_SUFFIX = 'tar'
TEST_BLUEPRINT_ID = 'mock_blueprint_id'
PLUGINS_DIR = '{0}/plugins'.format(TEST_BLUEPRINT_ID)
MANAGER_FILE_SERVER_BLUEPRINTS_ROOT_URL = 'http://localhost:{0}' \
    .format(PORT)


def _get_local_path(ctx, plugin):
    return os.path.join(dirname(__file__),
                        plugin['source'])


class PluginInstallerTestCase(testtools.TestCase):

    @classmethod
    def setUpClass(cls):
        # create tar files for the mock plugins used by the tests
        cls.create_plugin_tar(MOCK_PLUGIN)
        cls.create_plugin_tar(MOCK_PLUGIN_WITH_DEPENDENCIES)

        test_file_server = None
        try:
            # start file server
            local_dir = dirname(__file__)
            test_file_server = FileServer(local_dir)
            test_file_server.start()
        except Exception as e:
            logger.info('Failed to start local file server, '
                        'reported error: {0}'.format(e.message))
            if test_file_server:
                try:
                    test_file_server.stop()
                except Exception as e:
                    logger.info('failed to stop local file server: {0}'
                                .format(e.message))

    @classmethod
    def tearDownClass(cls):
        local_dir = dirname(__file__)
        test_file_server = FileServer(local_dir)
        if test_file_server:
            try:
                test_file_server.stop()
            except Exception as e:<commit_msg>installing virtualenv to later create virtual envs that will get the tests python settings and not yhe default system settings<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.

import os
from os.path import dirname
import tempfile
import shutil
import tarfile
import filecmp

import testtools

from cloudify.exceptions import NonRecoverableError
from cloudify.mocks import MockCloudifyContext
from cloudify.utils import LocalCommandRunner
from cloudify.utils import setup_default_logger
from plugin_installer.tasks import install, get_url, update_includes, \
    parse_pip_version, is_pip6_or_higher, extract_plugin_dir
from cloudify.constants import CELERY_WORK_DIR_PATH_KEY
from cloudify.constants import VIRTUALENV_PATH_KEY
from cloudify.constants import LOCAL_IP_KEY
from cloudify.constants import MANAGER_FILE_SERVER_BLUEPRINTS_ROOT_URL_KEY
from plugin_installer.tests.file_server import FileServer
from plugin_installer.tests.file_server import PORT


logger = setup_default_logger('test_plugin_installer')
runner = LocalCommandRunner()

MOCK_PLUGIN = 'mock-plugin'
MOCK_PLUGIN_WITH_DEPENDENCIES = 'mock-with-dependencies-plugin'
ZIP_SUFFIX = 'zip'
TAR_SUFFIX = 'tar'
TEST_BLUEPRINT_ID = 'mock_blueprint_id'
PLUGINS_DIR = '{0}/plugins'.format(TEST_BLUEPRINT_ID)
MANAGER_FILE_SERVER_BLUEPRINTS_ROOT_URL = 'http://localhost:{0}' \
    .format(PORT)


def _get_local_path(ctx, plugin):
    return os.path.join(dirname(__file__),
                        plugin['source'])


class PluginInstallerTestCase(testtools.TestCase):

    @classmethod
    def setUpClass(cls):
        # install virtual env. this ensures the env that will be later created
        # will use the python env the test is running in (e.g. might be 2.6)
        runner.run('pip install virtualenv')

        # create tar files for the mock plugins used by the tests
        cls.create_plugin_tar(MOCK_PLUGIN)
        cls.create_plugin_tar(MOCK_PLUGIN_WITH_DEPENDENCIES)

        test_file_server = None
        try:
            # start file server
            local_dir = dirname(__file__)
            test_file_server = FileServer(local_dir)
            test_file_server.start()
        except Exception as e:
            logger.info('Failed to start local file server, '
                        'reported error: {0}'.format(e.message))
            if test_file_server:
                try:
                    test_file_server.stop()
                except Exception as e:
                    logger.info('failed to stop local file server: {0}'
                                .format(e.message))

    @classmethod
    def tearDownClass(cls):
        local_dir = dirname(__file__)
        test_file_server = FileServer(local_dir)
        if test_file_server:
            try:
                test_file_server.stop()
            except Exception as e:
{'fraction_non_alphanumeric': 0.06751572809574957, 'fraction_numerical': 0.004296455424274973, 'mean_word_length': 3.237971391417425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4264071', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1835, 'n_words': 547}