Data class containing parameters required to execute a new process
        /* for test purposes */
        public static bool ExecuteJavaRunner(AnalysisConfig config, IEnumerable<string> userCmdLineArguments, ILogger logger, string exeFileName, string propertiesFileName)
        {
            Debug.Assert(File.Exists(exeFileName), "The specified exe file does not exist: " + exeFileName);
            Debug.Assert(File.Exists(propertiesFileName), "The specified properties file does not exist: " + propertiesFileName);

            IgnoreSonarRunnerHome(logger);

            IEnumerable<string> allCmdLineArgs = GetAllCmdLineArgs(propertiesFileName, userCmdLineArguments, config);

            IDictionary<string, string> envVarsDictionary = GetAdditionalEnvVariables(logger);
            Debug.Assert(envVarsDictionary != null);

            logger.LogInfo(Resources.MSG_CallingSonarRunner);
            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(exeFileName, logger)
            {
                CmdLineArgs = allCmdLineArgs,
                WorkingDirectory = Path.GetDirectoryName(exeFileName),
                EnvironmentVariables = envVarsDictionary
            };
            ProcessRunner runner = new ProcessRunner();

            bool success = runner.Execute(runnerArgs);

            success = success && !runner.ErrorsLogged;

            if (success)
            {
                logger.LogInfo(Resources.MSG_SonarRunnerCompleted);
            }
            else
            {
                logger.LogError(Resources.ERR_SonarRunnerExecutionFailed);
            }
            return success;
        }
        public void V0_9Upgrade_CalledFromV0_9()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(typeof(V0_9UpgradeMessageExe.Program).Assembly.Location, logger)
            {
                WorkingDirectory = this.TestContext.DeploymentDirectory
            };

            // Act
            ProcessRunner runner = new ProcessRunner();
            bool success = runner.Execute(runnerArgs);

            // Assert
            Assert.IsFalse(success);
            logger.AssertSingleErrorExists(SonarQube.V0_9UpgradeMessageExe.Resources.UpgradeMessage);
            logger.AssertErrorsLogged(1);
        }
        /* for test purposes */
        public static bool ExecuteJavaRunner(AnalysisConfig config, IEnumerable<string> userCmdLineArguments, ILogger logger, string exeFileName, string propertiesFileName)
        {
            Debug.Assert(File.Exists(exeFileName), "The specified exe file does not exist: " + exeFileName);
            Debug.Assert(File.Exists(propertiesFileName), "The specified properties file does not exist: " + propertiesFileName);

            IgnoreSonarScannerHome(logger);

            IEnumerable<string> allCmdLineArgs = GetAllCmdLineArgs(propertiesFileName, userCmdLineArguments, config);

            IDictionary<string, string> envVarsDictionary = GetAdditionalEnvVariables(logger);
            Debug.Assert(envVarsDictionary != null);

            logger.LogInfo(Resources.MSG_SonarScannerCalling);

            Debug.Assert(!String.IsNullOrWhiteSpace(config.SonarRunnerWorkingDirectory), "The working dir should have been set in the analysis config");
            Debug.Assert(Directory.Exists(config.SonarRunnerWorkingDirectory), "The working dir should exist");

            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(exeFileName, logger)
            {
                CmdLineArgs = allCmdLineArgs,
                WorkingDirectory = config.SonarRunnerWorkingDirectory,
                EnvironmentVariables = envVarsDictionary
            };

            ProcessRunner runner = new ProcessRunner();

            // SONARMSBRU-202 Note that the Sonar Scanner may write warnings to stderr so
            // we should only rely on the exit code when deciding if it ran successfully
            bool success = runner.Execute(runnerArgs);

            if (success)
            {
                logger.LogInfo(Resources.MSG_SonarScannerCompleted);
            }
            else
            {
                logger.LogError(Resources.ERR_SonarScannerExecutionFailed);
            }
            return success;
        }
Exemplo n.º 4
0
        public bool CompileJar(string jarContentDirectory, string manifestFilePath, string fullJarPath, ILogger logger)
        {
            if (string.IsNullOrWhiteSpace(fullJarPath))
            {
                throw new ArgumentNullException("fullJarPath");
            }
            if (string.IsNullOrWhiteSpace(jarContentDirectory))
            {
                throw new ArgumentNullException("jarLayoutDirectory");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            string jarExePath = TryFindJavaExe(JAR_BUILDER_EXE);
            Debug.Assert(!string.IsNullOrEmpty(jarExePath), "Failed to locate the jar exe, although the JDK appears to be installed");

            // jar cvfm test.jar MANIFEST.MF myorg\*.class* resources\*.*
            string[] cmdLineArgs = new string[]
                {
                "cvfm",
                ProcessRunnerArguments.GetQuotedArg(fullJarPath),
                ProcessRunnerArguments.GetQuotedArg(manifestFilePath),
                "." // directory - will be processed recursively
                };

            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(jarExePath, logger)
            {
                CmdLineArgs = cmdLineArgs,
                WorkingDirectory = jarContentDirectory
            };

            ProcessRunner runner = new ProcessRunner();
            bool success = runner.Execute(runnerArgs);

            return success;
        }
Exemplo n.º 5
0
 /// <summary>
 /// Returns whether the property contains any sensitive data
 /// </summary>
 public bool ContainsSensitiveData()
 {
     return(ProcessRunnerArguments.ContainsSensitiveData(Id) || ProcessRunnerArguments.ContainsSensitiveData(Value));
 }
Exemplo n.º 6
0
        /// <summary>
        /// Runs the specified executable and returns a boolean indicating success or failure
        /// </summary>
        /// <remarks>The standard and error output will be streamed to the logger. Child processes do not inherit the env variables from the parent automatically</remarks>
        public bool Execute(ProcessRunnerArguments runnerArgs)
        {
            if (runnerArgs == null)
            {
                throw new ArgumentNullException("runnerArgs");
            }
            Debug.Assert(!string.IsNullOrWhiteSpace(runnerArgs.ExeName), "Process runner exe name should not be null/empty");
            Debug.Assert( runnerArgs.Logger!= null, "Process runner logger should not be null/empty");

            this.outputLogger = runnerArgs.Logger;

            if (!File.Exists(runnerArgs.ExeName))
            {
                this.outputLogger.LogError(Resources.ERROR_ProcessRunner_ExeNotFound, runnerArgs.ExeName);
                this.ExitCode = ErrorCode;
                return false;
            }

            ProcessStartInfo psi = new ProcessStartInfo()
            {
                FileName = runnerArgs.ExeName,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                UseShellExecute = false, // required if we want to capture the error output
                ErrorDialog = false,
                CreateNoWindow = true,
                Arguments = runnerArgs.GetQuotedCommandLineArgs(),
                WorkingDirectory = runnerArgs.WorkingDirectory
            };

            SetEnvironmentVariables(psi, runnerArgs.EnvironmentVariables, runnerArgs.Logger);

            bool succeeded;
            Process process = null;
            try
            {
                process = new Process();

                process.StartInfo = psi;
                process.ErrorDataReceived += OnErrorDataReceived;
                process.OutputDataReceived += OnOutputDataReceived;

                process.Start();
                process.BeginErrorReadLine();
                process.BeginOutputReadLine();

                // Warning: do not log the raw command line args as they
                // may contain sensitive data
                this.outputLogger.LogDebug(Resources.MSG_ExecutingFile,
                    runnerArgs.ExeName,
                    runnerArgs.GetCommandLineArgsLogText(),
                    runnerArgs.WorkingDirectory,
                    runnerArgs.TimeoutInMilliseconds,
                    process.Id);

                succeeded = process.WaitForExit(runnerArgs.TimeoutInMilliseconds);
                if (succeeded)
                {
                    process.WaitForExit(); // Give any asynchronous events the chance to complete
                }

                // false means we asked the process to stop but it didn't.
                // true: we might still have timed out, but the process ended when we asked it to
                if (succeeded)
                {
                    this.outputLogger.LogDebug(Resources.MSG_ExecutionExitCode, process.ExitCode);
                    this.ExitCode = process.ExitCode;
                }
                else
                {
                    this.ExitCode = ErrorCode;
                    this.outputLogger.LogWarning(Resources.WARN_ExecutionTimedOut, runnerArgs.TimeoutInMilliseconds, runnerArgs.ExeName);
                }

                succeeded = succeeded && (this.ExitCode == 0);
            }
            finally
            {
                process.ErrorDataReceived -= OnErrorDataReceived;
                process.OutputDataReceived -= OnOutputDataReceived;

                process.Dispose();
            }
            return succeeded;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Runs the specified executable and returns a boolean indicating success or failure
        /// </summary>
        /// <remarks>The standard and error output will be streamed to the logger. Child processes do not inherit the env variables from the parent automatically</remarks>
        public bool Execute(ProcessRunnerArguments runnerArgs)
        {
            if (runnerArgs == null)
            {
                throw new ArgumentNullException("runnerArgs");
            }
            Debug.Assert(!string.IsNullOrWhiteSpace(runnerArgs.ExeName), "Process runner exe name should not be null/empty");
            Debug.Assert(runnerArgs.Logger != null, "Process runner logger should not be null/empty");

            outputLogger = runnerArgs.Logger;

            if (!File.Exists(runnerArgs.ExeName))
            {
                outputLogger.LogError(Resources.ERROR_ProcessRunner_ExeNotFound, runnerArgs.ExeName);
                ExitCode = ErrorCode;
                return(false);
            }

            var psi = new ProcessStartInfo()
            {
                FileName = runnerArgs.ExeName,
                RedirectStandardError  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false, // required if we want to capture the error output
                ErrorDialog            = false,
                CreateNoWindow         = true,
                Arguments        = runnerArgs.GetEscapedArguments(),
                WorkingDirectory = runnerArgs.WorkingDirectory
            };

            SetEnvironmentVariables(psi, runnerArgs.EnvironmentVariables, runnerArgs.Logger);

            bool succeeded;

            using (var process = new Process())
            {
                process.StartInfo           = psi;
                process.ErrorDataReceived  += OnErrorDataReceived;
                process.OutputDataReceived += OnOutputDataReceived;

                process.Start();
                process.BeginErrorReadLine();
                process.BeginOutputReadLine();

                // Warning: do not log the raw command line args as they
                // may contain sensitive data
                outputLogger.LogDebug(Resources.MSG_ExecutingFile,
                                      runnerArgs.ExeName,
                                      runnerArgs.AsLogText(),
                                      runnerArgs.WorkingDirectory,
                                      runnerArgs.TimeoutInMilliseconds,
                                      process.Id);

                succeeded = process.WaitForExit(runnerArgs.TimeoutInMilliseconds);
                if (succeeded)
                {
                    process.WaitForExit(); // Give any asynchronous events the chance to complete
                }

                // false means we asked the process to stop but it didn't.
                // true: we might still have timed out, but the process ended when we asked it to
                if (succeeded)
                {
                    outputLogger.LogDebug(Resources.MSG_ExecutionExitCode, process.ExitCode);
                    ExitCode = process.ExitCode;
                }
                else
                {
                    ExitCode = ErrorCode;

                    try
                    {
                        process.Kill();
                        outputLogger.LogWarning(Resources.WARN_ExecutionTimedOutKilled, runnerArgs.TimeoutInMilliseconds, runnerArgs.ExeName);
                    }
                    catch
                    {
                        outputLogger.LogWarning(Resources.WARN_ExecutionTimedOutNotKilled, runnerArgs.TimeoutInMilliseconds, runnerArgs.ExeName);
                    }
                }

                succeeded = succeeded && (ExitCode == 0);
            }

            return(succeeded);
        }
        /// <summary>
        /// Launches the build wrapper and asks it to attach to the current process.
        /// Returns whether the build wrapper was succesfully attached or not.
        /// </summary>
        private bool Attach(string outputDirectory)
        {
            string currentPID = GetProcessId();

            // Choose either the 32- or 64-bit version of the monitor to match the current process
            string exeName = (Environment.Is64BitOperatingSystem ? BuildWrapperExeName64 : BuildWrapperExeName32);
            string monitorExeFilePath = Path.Combine(this.BinDirectoryPath, exeName);

            ProcessRunnerArguments args = new ProcessRunnerArguments(monitorExeFilePath, new MSBuildLoggerAdapter(this.Log));
            args.TimeoutInMilliseconds = this.buildWrapperTimeoutInMs;

            // See https://jira.sonarsource.com/browse/CPP-1469 for the specification of the arguments to pass to the Cpp plugin
            // --msbuild - task < PID > < OUTPUT_DIR >
            args.CmdLineArgs = new string[] {
                "--msbuild-task",
                currentPID,
                outputDirectory
            };

            this.Log.LogMessage(MessageImportance.Low, Resources.BuildWrapper_WaitingForAttach, currentPID);

            ProcessRunner runner = new ProcessRunner();
            bool success = runner.Execute(args);

            if (success)
            {
                this.Log.LogMessage(MessageImportance.Low, Resources.BuildWrapper_AttachedSuccessfully);
            }
            else
            {
                this.Log.LogError(Resources.BuildWrapper_FailedToAttach);
            }

            return success;
        }
Exemplo n.º 9
0
        private static int PostProcess(IBootstrapperSettings settings, ILogger logger)
        {
            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(settings.PostProcessorFilePath, logger)
            {
                CmdLineArgs = settings.ChildCmdLineArgs,
                WorkingDirectory = settings.TempDirectory
            };

            ProcessRunner runner = new ProcessRunner();
            runner.Execute(runnerArgs);

            return runner.ExitCode;
        }
Exemplo n.º 10
0
        private static int PreProcess(IBuildAgentUpdater updater, IBootstrapperSettings settings, ILogger logger)
        {
            string downloadBinPath = settings.DownloadDirectory;

            logger.LogInfo(Resources.MSG_PreparingDirectories);
            if (!Utilities.TryEnsureEmptyDirectories(logger,
                settings.TempDirectory,
                downloadBinPath))
            {
                return ErrorCode;
            }

            string server = settings.SonarQubeUrl;
            Debug.Assert(!string.IsNullOrWhiteSpace(server), "Not expecting the server url to be null/empty");
            logger.LogDebug(Resources.MSG_ServerUrl, server);

            logger.LogInfo(Resources.MSG_CheckingForUpdates);
            if (!updater.TryUpdate(server, downloadBinPath, logger))
            {
                logger.LogError(Resources.ERROR_FailedToUpdateRunnerBinaries);
                return ErrorCode;
            }

            if (!updater.CheckBootstrapperApiVersion(settings.SupportedBootstrapperVersionsFilePath, settings.BootstrapperVersion))
            {
                logger.LogError(Resources.ERROR_VersionMismatch);
                return ErrorCode;
            }

            var preprocessorFilePath = settings.PreProcessorFilePath;

            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(preprocessorFilePath, logger)
            {
                CmdLineArgs = settings.ChildCmdLineArgs,
                WorkingDirectory = settings.TempDirectory,
            };
            ProcessRunner runner = new ProcessRunner();
            runner.Execute(runnerArgs);

            return runner.ExitCode;
        }
Exemplo n.º 11
0
        private static int PostProcess(IBootstrapperSettings settings, ILogger logger)
        {

            if (!File.Exists(settings.PostProcessorFilePath))
            {
                logger.LogError(Resources.ERROR_PostProcessExeNotFound, settings.PostProcessorFilePath);
                return ErrorCode;
            }

            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(settings.PostProcessorFilePath, logger)
            {
                CmdLineArgs = settings.ChildCmdLineArgs,
                WorkingDirectory = settings.TempDirectory
            };

            ProcessRunner runner = new ProcessRunner();
            runner.Execute(runnerArgs);

            return runner.ExitCode;
        }
Exemplo n.º 12
0
 /// <summary>
 /// Returns the property formatted as a sonar-runner "-D" argument
 /// </summary>
 public string AsSonarRunnerArg()
 {
     return(string.Format(System.Globalization.CultureInfo.InvariantCulture, "-D{0}={1}", this.Id, ProcessRunnerArguments.GetQuotedArg(this.Value)));
 }
Exemplo n.º 13
0
        public bool CompileSources(IEnumerable<string> args, ILogger logger)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            string exePath = TryFindJavaExe(JAVA_COMPILER_EXE);
            Debug.Assert(!string.IsNullOrEmpty(exePath), "Failed to locate the Java compiler exe, although the JDK appears to be installed");

            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(exePath, logger)
            {
                CmdLineArgs = args
            };

            ProcessRunner runner = new ProcessRunner();
            bool success = runner.Execute(runnerArgs);

            return success;
        }
        // was internal
        public static bool ConvertBinaryToXml(string converterExeFilePath, string inputBinaryFilePath, string outputXmlFilePath, ILogger logger)
        {
            Debug.Assert(!string.IsNullOrEmpty(converterExeFilePath), "Expecting the conversion tool path to have been set");
            Debug.Assert(File.Exists(converterExeFilePath), "Expecting the converter exe to exist: " + converterExeFilePath);
            Debug.Assert(Path.IsPathRooted(inputBinaryFilePath), "Expecting the input file name to be a full absolute path");
            Debug.Assert(File.Exists(inputBinaryFilePath), "Expecting the input file to exist: " + inputBinaryFilePath);
            Debug.Assert(Path.IsPathRooted(outputXmlFilePath), "Expecting the output file name to be a full absolute path");

            List<string> args = new List<string>();
            args.Add("analyze");
            args.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture, @"/output:""{0}""", outputXmlFilePath));
            args.Add(inputBinaryFilePath);

            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(converterExeFilePath, logger)
            {
                WorkingDirectory = Path.GetDirectoryName(outputXmlFilePath),
                CmdLineArgs = args,
                TimeoutInMilliseconds = ConversionTimeoutInMs
            };

            ProcessRunner runner = new ProcessRunner();
            bool success = runner.Execute(runnerArgs);

            if (success)
            {
                // Check the output file actually exists
                if (!File.Exists(outputXmlFilePath))
                {
                    logger.LogError(Resources.CONV_ERROR_OutputFileNotFound, outputXmlFilePath);
                    success = false;
                }
            }
            else
            {
                logger.LogError(Resources.CONV_ERROR_ConversionToolFailed, inputBinaryFilePath);
            }

            return success;
        }
Exemplo n.º 15
0
        private string RunPluginInspector(string jarFilePath, string tempDir)
        {
            Debug.Assert(!string.IsNullOrEmpty(this.inspectorClassFilePath));

            string reportFilePath = Path.Combine(tempDir, "report.xml");

            IEnumerable<string> jarFiles = GetRuntimeDependencies();

            // Construct the class path argument
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("-cp \"{0}\"", Path.GetDirectoryName(this.inspectorClassFilePath));
            foreach (string dependencyFilePath in jarFiles)
            {
                sb.AppendFormat(";{0}", dependencyFilePath);
            }

            IList<string> cmdLineArgs = new List<string>();
            cmdLineArgs.Add(sb.ToString()); // options must preceed the name of the class to execute
            cmdLineArgs.Add(PluginInspectorFullClassName);
            cmdLineArgs.Add(jarFilePath); // parameter(s) to pass to the class
            cmdLineArgs.Add(reportFilePath);

            ProcessRunnerArguments runnerArgs = new ProcessRunnerArguments(GetJavaExePath(), logger);
            runnerArgs.CmdLineArgs = cmdLineArgs;

            ProcessRunner runner = new ProcessRunner();
            bool success = runner.Execute(runnerArgs);

            Assert.IsTrue(success, "Test error: failed to execute the PluginInspector");
            Assert.IsTrue(File.Exists(reportFilePath), "Test error: failed to create the PluginInspector report");
            return reportFilePath;
        }