<commit_before>/*
 * Copyright (C) 2017 Black Duck Software Inc.
 * http://www.blackducksoftware.com/
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Black Duck Software ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Black Duck Software.
 */
package com.blackducksoftware.integration.hub.packman.util.executable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class ExecutableRunner {
    private final Logger logger = LoggerFactory.getLogger(ExecutableRunner.class);

    /**
     * The output of the executable's execution will NOT be logged.
     */
    public ExecutableOutput executeQuietly(final Executable executable) throws ExecutableRunnerException {
        return execute(executable, true);
    }

    /**
     * The standard output of the executable's execution will be logged at DEBUG and any error output will be logged as
     * ERROR.
     */
    public ExecutableOutput executeLoudly(final Executable executable) throws ExecutableRunnerException {
        return execute(executable, false);
    }

    /**
     * If runQuietly == false, the standard output of the executable's execution will be logged at DEBUG and any error
     * output will be logged as
     * ERROR. Otherwise, the output of the executable's execution will NOT be logged.
     */
    public ExecutableOutput execute(final Executable executable, final boolean runQuietly) throws ExecutableRunnerException {
        logger.debug(String.format("Running executable >%s", executable.getExecutableDescription()));
        try {
            final ProcessBuilder processBuilder = executable.createProcessBuilder();
            final Process process = processBuilder.start();
            final int exitCode = process.waitFor();
            final String standardOutput = printStream(process.getInputStream(), runQuietly, false, exitCode);
            final String errorOutput = printStream(process.getErrorStream(), runQuietly, true, exitCode);
            final ExecutableOutput output = new ExecutableOutput(standardOutput, errorOutput);
            if (StringUtils.isNotBlank(errorOutput)) {
                throw new ExecutableRunnerException(output);
            }
            return output;
        } catch (final IOException | InterruptedException e) {
            throw new ExecutableRunnerException(e);
        }
    }

    private String printStream(final InputStream inputStream, final boolean runQuietly, final boolean error, final int exitCode) throws IOException {
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        final StringBuilder stringBuilder = new StringBuilder();

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line + "\n");
            if (!runQuietly) {
                if (error && StringUtils.isNotBlank(line) && exitCode != 0) {
                    logger.error(line);
                } else {
                    logger.debug(line);
                }
            }
        }
        return stringBuilder.toString();<commit_msg>Removing the exit code check<commit_after>/*
 * Copyright (C) 2017 Black Duck Software Inc.
 * http://www.blackducksoftware.com/
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Black Duck Software ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Black Duck Software.
 */
package com.blackducksoftware.integration.hub.packman.util.executable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class ExecutableRunner {
    private final Logger logger = LoggerFactory.getLogger(ExecutableRunner.class);

    /**
     * The output of the executable's execution will NOT be logged.
     */
    public ExecutableOutput executeQuietly(final Executable executable) throws ExecutableRunnerException {
        return execute(executable, true);
    }

    /**
     * The standard output of the executable's execution will be logged at DEBUG and any error output will be logged as
     * ERROR.
     */
    public ExecutableOutput executeLoudly(final Executable executable) throws ExecutableRunnerException {
        return execute(executable, false);
    }

    /**
     * If runQuietly == false, the standard output of the executable's execution will be logged at DEBUG and any error
     * output will be logged as
     * ERROR. Otherwise, the output of the executable's execution will NOT be logged.
     */
    public ExecutableOutput execute(final Executable executable, final boolean runQuietly) throws ExecutableRunnerException {
        logger.debug(String.format("Running executable >%s", executable.getExecutableDescription()));
        try {
            final ProcessBuilder processBuilder = executable.createProcessBuilder();
            final Process process = processBuilder.start();
            final String standardOutput = printStream(process.getInputStream(), runQuietly, false);
            final String errorOutput = printStream(process.getErrorStream(), runQuietly, true);
            final ExecutableOutput output = new ExecutableOutput(standardOutput, errorOutput);
            if (StringUtils.isNotBlank(errorOutput)) {
                throw new ExecutableRunnerException(output);
            }
            return output;
        } catch (final IOException e) {
            throw new ExecutableRunnerException(e);
        }
    }

    private String printStream(final InputStream inputStream, final boolean runQuietly, final boolean error) throws IOException {
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        final StringBuilder stringBuilder = new StringBuilder();

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line + "\n");
            if (!runQuietly) {
                if (error && StringUtils.isNotBlank(line)) {
                    logger.error(line);
                } else {
                    logger.debug(line);
                }
            }
        }
        return stringBuilder.toString();
{'fraction_non_alphanumeric': 0.07766851931024654, 'fraction_numerical': 0.0024226877583012683, 'mean_word_length': 3.08974358974359, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5941371', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1648, 'n_words': 549}