<commit_before>########
# Copyright (c) 2014 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 collections
import subprocess
import fcntl
import select
import os
import errno

from cloudify import utils
from cloudify.utils import get_manager_ip
from cloudify.decorators import operation


@operation
def run(ctx, script_path=None, **kwargs):
    """
    Execute bash scripts.

        Parameters:

            scripts - A dictionary mapping lifecycle events to script paths.

            script_path - The path to the script relative
                          to the blueprints root directory.
                          Will only be used if the 'scripts' argument is None.
        Exceptions:

            If both 'scripts' and 'script_path' is None.
            A runtime exception will be raised since there is no
            script to run.
    """

    if script_path:
        sh = ctx.get_resource(script_path)
        return bash(sh, ctx)
    if 'scripts' in ctx.properties:
        operation_simple_name = ctx.operation.split('.')[-1:].pop()
        scripts = ctx.properties['scripts']
        if operation_simple_name not in scripts:
            ctx.logger.info("No script mapping found for operation {0}. "
                            "Nothing to do.".format(operation_simple_name))
            return None
        sh = ctx.get_resource(scripts[operation_simple_name])
        return bash(sh, ctx)

    raise RuntimeError('No script to run')


def bash(path, ctx):
    with open(path, "r") as myfile:
        cat = myfile.read()
    ctx.logger.info('Executing this file: %s with content: \n%s' % (path, cat))
    return execute('/bin/bash %s' % path, ctx)


def execute(command, ctx):
    ctx.logger.info('Running command: %s' % command)
    env = setup_environment(ctx)
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               env=env)
    make_async(process.stdout)
    make_async(process.stderr)

    stdout = str()
    stderr = str()
    return_code = None

    while True:
        # Wait for data to become available
        select.select([process.stdout, process.stderr], [], [])

        # Try reading some data from each
        stdout_piece = read_async(process.stdout)
        stderr_piece = read_async(process.stderr)

        if stdout_piece:
            ctx.logger.info(stdout_piece)
        if stderr_piece:
            ctx.logger.error(stderr_piece)

        stdout += stdout_piece
        stderr += stderr_piece
        return_code = process.poll()

        if return_code is not None:
            break<commit_msg>CFY-448 print output after polling for return code<commit_after>########
# Copyright (c) 2014 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 collections
import subprocess
import fcntl
import select
import os
import errno

from cloudify import utils
from cloudify.utils import get_manager_ip
from cloudify.decorators import operation


@operation
def run(ctx, script_path=None, **kwargs):
    """
    Execute bash scripts.

        Parameters:

            scripts - A dictionary mapping lifecycle events to script paths.

            script_path - The path to the script relative
                          to the blueprints root directory.
                          Will only be used if the 'scripts' argument is None.
        Exceptions:

            If both 'scripts' and 'script_path' is None.
            A runtime exception will be raised since there is no
            script to run.
    """

    if script_path:
        sh = ctx.get_resource(script_path)
        return bash(sh, ctx)
    if 'scripts' in ctx.properties:
        operation_simple_name = ctx.operation.split('.')[-1:].pop()
        scripts = ctx.properties['scripts']
        if operation_simple_name not in scripts:
            ctx.logger.info("No script mapping found for operation {0}. "
                            "Nothing to do.".format(operation_simple_name))
            return None
        sh = ctx.get_resource(scripts[operation_simple_name])
        return bash(sh, ctx)

    raise RuntimeError('No script to run')


def bash(path, ctx):
    with open(path, "r") as myfile:
        cat = myfile.read()
    ctx.logger.info('Executing this file: %s with content: \n%s' % (path, cat))
    return execute('/bin/bash %s' % path, ctx)


def execute(command, ctx):
    ctx.logger.info('Running command: %s' % command)
    env = setup_environment(ctx)
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               env=env)
    make_async(process.stdout)
    make_async(process.stderr)

    stdout = str()
    stderr = str()
    return_code = None

    while True:
        # Wait for data to become available
        select.select([process.stdout, process.stderr], [], [])

        return_code = process.poll()

        # Try reading some data from each
        stdout_piece = read_async(process.stdout)
        stderr_piece = read_async(process.stderr)

        if stdout_piece:
            ctx.logger.info(stdout_piece)
        if stderr_piece:
            ctx.logger.error(stderr_piece)

        stdout += stdout_piece
        stderr += stderr_piece

        if return_code is not None:
            break
{'fraction_non_alphanumeric': 0.08490566037735849, 'fraction_numerical': 0.003557067738942159, 'mean_word_length': 2.4253177966101696, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '616877', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1696, 'n_words': 606}