<commit_before>/*******************************************************************************
 * Copyright (c) 2011 Coverity, Inc

 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Coverity, Inc - initial implementation and documentation
 *******************************************************************************/
package jenkins.plugins.coverity;

import com.coverity.ws.v3.CovRemoteServiceException_Exception;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.LauncherDecorator;
import hudson.Proc;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Executor;
import hudson.model.Node;
import hudson.model.Queue;
import hudson.model.TaskListener;
import hudson.remoting.Channel;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * This makes sure that all commands executed (when invocation assistance is enabled) are run through cov-build.
 */
@Extension
public class CoverityLauncherDecorator extends LauncherDecorator {

	private static final Logger logger = Logger.getLogger(CoverityLauncherDecorator.class.getName());
	/**
	 * A ThreadLocal that is used to disable cov-build when running other Coverity tools during the build.
	 */
	public static ThreadLocal<Boolean> SKIP = new ThreadLocal<Boolean>() {
		@Override
		protected Boolean initialValue() {
			return false;
		}
	};

	@Override
	public Launcher decorate(Launcher launcher, Node node) {
		Executor executor = Executor.currentExecutor();
		if(executor == null) {
			return launcher;
		}

		Queue.Executable exec = executor.getCurrentExecutable();
		if(!(exec instanceof AbstractBuild)) {
			return launcher;
		}
		AbstractBuild build = (AbstractBuild) exec;

		AbstractProject project = build.getProject();

		CoverityPublisher publisher = (CoverityPublisher) project.getPublishersList().get(CoverityPublisher.class);
		if(publisher == null) {
			return launcher;
		}

		InvocationAssistance ii = publisher.getInvocationAssistance();
		if(ii == null) {
			return launcher;
		}

		// Do not run cov-build if language is "CSHARP"
		boolean onlyCS = true;
		for(CIMStream cs : publisher.getCimStreams()) {
			CIMInstance cim = publisher.getDescriptor().getInstance(cs.getInstance());
			String id = cs.getInstance() + "/" + cs.getStream();
			try {
				String language = cim.getStream(cs.getStream()).getLanguage();
				if(!"CSHARP".equals(language)) {
					onlyCS = false;
					break;
				}
			} catch(CovRemoteServiceException_Exception e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			} catch(IOException e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			}
		}
		//look for old-style stream format
		if(publisher.getCimInstance() != null) {
			CIMInstance cim = publisher.getDescriptor().getInstance(publisher.getCimInstance());
			String id = publisher.getCimInstance() + "/" + publisher.getStream();
			try {
				String language = cim.getStream(publisher.getStream()).getLanguage();
				if(!"CSHARP".equals(language)) {
					onlyCS = false;
				}
			} catch(CovRemoteServiceException_Exception e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			} catch(IOException e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			}
		}
		if(onlyCS) {
			logger.info("Only streams of type CSHARP were found, skipping cov-build");

			return launcher;
		}

		FilePath temp;

		try {
			if(ii.getIntermediateDir() == null) {
				FilePath coverityDir = node.getRootPath().child("coverity");
				coverityDir.mkdirs();
				temp = coverityDir.createTempDir("temp-", null);
			} else {
				temp = new FilePath(node.getChannel(), ii.getIntermediateDir());
				temp.mkdirs();
			}

			build.addAction(new CoverityTempDir(temp, ii.getIntermediateDir() == null));
		} catch(IOException e) {
			throw new RuntimeException("Error while creating temporary directory for Coverity", e);
		} catch(InterruptedException e) {
			throw new RuntimeException("Interrupted while creating temporary directory for Coverity");
		}

		try {
			String covBuild = "cov-build";
			TaskListener listener = launcher.getListener();
			String home = publisher.getDescriptor().getHome(node, build.getEnvironment(listener));
			if(ii.getSaOverride() != null) {
				home = new CoverityInstallation(ii.getSaOverride()).forEnvironment(build.getEnvironment(listener)).getHome();<commit_msg>Re-order initialization of intermediate directory<commit_after>/*******************************************************************************
 * Copyright (c) 2011 Coverity, Inc

 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Coverity, Inc - initial implementation and documentation
 *******************************************************************************/
package jenkins.plugins.coverity;

import com.coverity.ws.v3.CovRemoteServiceException_Exception;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.LauncherDecorator;
import hudson.Proc;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Executor;
import hudson.model.Node;
import hudson.model.Queue;
import hudson.model.TaskListener;
import hudson.remoting.Channel;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * This makes sure that all commands executed (when invocation assistance is enabled) are run through cov-build.
 */
@Extension
public class CoverityLauncherDecorator extends LauncherDecorator {

	private static final Logger logger = Logger.getLogger(CoverityLauncherDecorator.class.getName());
	/**
	 * A ThreadLocal that is used to disable cov-build when running other Coverity tools during the build.
	 */
	public static ThreadLocal<Boolean> SKIP = new ThreadLocal<Boolean>() {
		@Override
		protected Boolean initialValue() {
			return false;
		}
	};

	@Override
	public Launcher decorate(Launcher launcher, Node node) {
		Executor executor = Executor.currentExecutor();
		if(executor == null) {
			return launcher;
		}

		Queue.Executable exec = executor.getCurrentExecutable();
		if(!(exec instanceof AbstractBuild)) {
			return launcher;
		}
		AbstractBuild build = (AbstractBuild) exec;

		AbstractProject project = build.getProject();

		CoverityPublisher publisher = (CoverityPublisher) project.getPublishersList().get(CoverityPublisher.class);
		if(publisher == null) {
			return launcher;
		}

		InvocationAssistance ii = publisher.getInvocationAssistance();
		if(ii == null) {
			return launcher;
		}

		FilePath temp;

		try {
			if(ii.getIntermediateDir() == null) {
				FilePath coverityDir = node.getRootPath().child("coverity");
				coverityDir.mkdirs();
				temp = coverityDir.createTempDir("temp-", null);
			} else {
				temp = new FilePath(node.getChannel(), ii.getIntermediateDir());
				temp.mkdirs();
			}

			build.addAction(new CoverityTempDir(temp, ii.getIntermediateDir() == null));
		} catch(IOException e) {
			throw new RuntimeException("Error while creating temporary directory for Coverity", e);
		} catch(InterruptedException e) {
			throw new RuntimeException("Interrupted while creating temporary directory for Coverity");
		}

		// Do not run cov-build if language is "CSHARP"
		boolean onlyCS = true;
		for(CIMStream cs : publisher.getCimStreams()) {
			CIMInstance cim = publisher.getDescriptor().getInstance(cs.getInstance());
			String id = cs.getInstance() + "/" + cs.getStream();
			try {
				String language = cim.getStream(cs.getStream()).getLanguage();
				if(!"CSHARP".equals(language)) {
					onlyCS = false;
					break;
				}
			} catch(CovRemoteServiceException_Exception e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			} catch(IOException e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			}
		}
		//look for old-style stream format
		if(publisher.getCimInstance() != null) {
			CIMInstance cim = publisher.getDescriptor().getInstance(publisher.getCimInstance());
			String id = publisher.getCimInstance() + "/" + publisher.getStream();
			try {
				String language = cim.getStream(publisher.getStream()).getLanguage();
				if(!"CSHARP".equals(language)) {
					onlyCS = false;
				}
			} catch(CovRemoteServiceException_Exception e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			} catch(IOException e) {
				throw new RuntimeException("Error while retrieving stream information for instance/stream: " + id, e);
			}
		}
		if(onlyCS) {
			logger.info("Only streams of type CSHARP were found, skipping cov-build");

			return launcher;
		}

		try {
			String covBuild = "cov-build";
			TaskListener listener = launcher.getListener();
			String home = publisher.getDescriptor().getHome(node, build.getEnvironment(listener));
			if(ii.getSaOverride() != null) {
				home = new CoverityInstallation(ii.getSaOverride()).forEnvironment(build.getEnvironment(listener)).getHome();
{'fraction_non_alphanumeric': 0.13897860314897054, 'fraction_numerical': 0.0018167137666532096, 'mean_word_length': 4.675257731958763, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7661240', 'n_tokens_mistral': 3151, 'n_tokens_neox': 2732, 'n_words': 727}