/// <summary> /// Use RunProgramInGuest to execute cmd.exe /C "guestCommandLine" > file and parse the result. /// </summary> /// <param name="guestCommandLine">Guest command line, argument passed to cmd.exe.</param> /// <returns>Standard output.</returns> public IShellOutput RunCommandInGuest(string guestCommandLine) { string guestStdOutFilename = _guestos.VirtualMachine.CreateTempFileInGuest(); string guestStdErrFilename = _guestos.VirtualMachine.CreateTempFileInGuest(); string guestCommandBatch = _guestos.VirtualMachine.CreateTempFileInGuest() + ".bat"; string hostCommandBatch = Path.GetTempFileName(); StringBuilder hostCommand = new StringBuilder(); hostCommand.AppendLine("@echo off"); hostCommand.AppendLine(guestCommandLine); File.WriteAllText(hostCommandBatch, hostCommand.ToString()); try { _guestos.VirtualMachine.CopyFileFromHostToGuest(hostCommandBatch, guestCommandBatch); string cmdArgs = string.Format("> \"{0}\" 2>\"{1}\"", guestStdOutFilename, guestStdErrFilename); _guestos.VirtualMachine.RunProgramInGuest(guestCommandBatch, cmdArgs); ShellOutput output = new ShellOutput( _guestos.ReadFile(guestStdOutFilename), _guestos.ReadFile(guestStdErrFilename)); return output; } finally { File.Delete(hostCommandBatch); _guestos.VirtualMachine.DeleteFileFromGuest(guestCommandBatch); _guestos.VirtualMachine.DeleteFileFromGuest(guestStdOutFilename); _guestos.VirtualMachine.DeleteFileFromGuest(guestStdErrFilename); } }
void AddConsoleText(ShellOutput result) { if (result != null) { string text = ""; if (result.IsError) { text = result.Error; } else { text = result.Output; } int length = txtConsole.TextLength; txtConsole.AppendText(text); txtConsole.SelectionStart = length; txtConsole.SelectionLength = text.Length; if (result.IsError) { txtConsole.SelectionColor = Color.Orange; } else { txtConsole.SelectionColor = Color.White; } txtConsole.AppendText(Environment.NewLine); txtConsole.SelectionStart = txtConsole.Text.Length; txtConsole.ScrollToCaret(); } }
/// <summary> /// Use RunProgramInGuest to execute cmd.exe /C "guestCommandLine" > file and parse the result. /// </summary> /// <param name="guestCommandLine">The guest command line.</param> /// <param name="options">The options.</param> /// <param name="timeoutInSeconds">The timeout in seconds.</param> /// <returns></returns> public ShellOutput RunCommandInGuest(string guestCommandLine, int options, int timeoutInSeconds) { string guestStdOutFilename = _vm.CreateTempFileInGuest(); string guestStdErrFilename = _vm.CreateTempFileInGuest(); string guestCommandBatch = _vm.CreateTempFileInGuest() + ".bat"; string hostCommandBatch = Path.GetTempFileName(); StringBuilder hostCommand = new StringBuilder(); hostCommand.AppendLine("@echo off"); hostCommand.AppendLine(guestCommandLine); File.WriteAllText(hostCommandBatch, hostCommand.ToString()); try { _vm.CopyFileFromHostToGuest(hostCommandBatch, guestCommandBatch); string cmdArgs = string.Format("> \"{0}\" 2>\"{1}\"", guestStdOutFilename, guestStdErrFilename); _vm.RunProgramInGuest(guestCommandBatch, cmdArgs, options, timeoutInSeconds); ShellOutput output = new ShellOutput(); output.StdOut = ReadFile(guestStdOutFilename); output.StdErr = ReadFile(guestStdErrFilename); return(output); } finally { File.Delete(hostCommandBatch); _vm.DeleteFileFromGuest(guestCommandBatch); _vm.DeleteFileFromGuest(guestStdOutFilename); _vm.DeleteFileFromGuest(guestStdErrFilename); } }
/// <summary> /// Use RunProgramInGuest to execute cmd.exe /C "guestCommandLine" > file and parse the result. /// </summary> /// <param name="guestCommandLine">Guest command line, argument passed to cmd.exe.</param> /// <returns>Standard output.</returns> public IShellOutput RunCommandInGuest(string guestCommandLine) { string guestStdOutFilename = _guestos.VirtualMachine.CreateTempFileInGuest(); string guestStdErrFilename = _guestos.VirtualMachine.CreateTempFileInGuest(); string guestCommandBatch = _guestos.VirtualMachine.CreateTempFileInGuest() + ".bat"; string hostCommandBatch = Path.GetTempFileName(); StringBuilder hostCommand = new StringBuilder(); hostCommand.AppendLine("@echo off"); hostCommand.AppendLine(guestCommandLine); File.WriteAllText(hostCommandBatch, hostCommand.ToString()); try { _guestos.VirtualMachine.CopyFileFromHostToGuest(hostCommandBatch, guestCommandBatch); string cmdArgs = string.Format("> \"{0}\" 2>\"{1}\"", guestStdOutFilename, guestStdErrFilename); _guestos.VirtualMachine.RunProgramInGuest(guestCommandBatch, cmdArgs); ShellOutput output = new ShellOutput( _guestos.ReadFile(guestStdOutFilename), _guestos.ReadFile(guestStdErrFilename)); return(output); } finally { File.Delete(hostCommandBatch); _guestos.VirtualMachine.DeleteFileFromGuest(guestCommandBatch); _guestos.VirtualMachine.DeleteFileFromGuest(guestStdOutFilename); _guestos.VirtualMachine.DeleteFileFromGuest(guestStdErrFilename); } }
private static void ProcessBuildOutput(JobContext <BuildInfo> context, ShellOutput o) { string l = o.Data; if (l.StartsWith("Running parts")) { var match = Regex.Match(l, @"^Running\sparts\s\((?<PartCount>\d+)\sremain"); int count; if (match.Success && int.TryParse(match.Groups["PartCount"].Value, out count) && count > 0) { if (context.Information.PartCount <= count) { context.Information.PartCount = count; } var percent = 1.0 - ((double)count / context.Information.PartCount); context.Progress.Value = (int)(percent * 100); context.Progress.ShortStatus = count.ToString(); return; } } context.OutputVM.SendOutput(l); }
/// <summary> /// Use RunProgramInGuest to execute cmd.exe /C "guestCommandLine" > file and parse the result. /// </summary> /// <param name="guestCommandLine">The guest command line.</param> /// <param name="options">The options.</param> /// <param name="timeoutInSeconds">The timeout in seconds.</param> /// <returns></returns> public ShellOutput RunCommandInGuest(string guestCommandLine, int options, int timeoutInSeconds) { string guestStdOutFilename = _vm.CreateTempFileInGuest(); string guestStdErrFilename = _vm.CreateTempFileInGuest(); string guestCommandBatch = _vm.CreateTempFileInGuest() + ".bat"; string hostCommandBatch = Path.GetTempFileName(); StringBuilder hostCommand = new StringBuilder(); hostCommand.AppendLine("@echo off"); hostCommand.AppendLine(guestCommandLine); File.WriteAllText(hostCommandBatch, hostCommand.ToString()); bool exceptionThrown = false; try { _vm.CopyFileFromHostToGuest(hostCommandBatch, guestCommandBatch); string cmdArgs = string.Format("> \"{0}\" 2>\"{1}\"", guestStdOutFilename, guestStdErrFilename); _vm.RunProgramInGuest(guestCommandBatch, cmdArgs, options, timeoutInSeconds); ShellOutput output = new ShellOutput(); output.StdOut = ReadFile(guestStdOutFilename); output.StdErr = ReadFile(guestStdErrFilename); return output; } catch (Exception) { exceptionThrown = true; throw; } finally { File.Delete(hostCommandBatch); //do not swallow exceptions RunOptionallyThrowingException(() => { _vm.DeleteFileFromGuest(guestCommandBatch); }, !exceptionThrown); RunOptionallyThrowingException(() => { _vm.DeleteFileFromGuest(guestStdOutFilename); }, !exceptionThrown); RunOptionallyThrowingException(() => { _vm.DeleteFileFromGuest(guestStdErrFilename); }, !exceptionThrown); } }
private static void ProcessBootstrapOutput(JobContext <BootstrapInfo> context, ShellOutput o) { string line = o.Data; if (string.IsNullOrEmpty(line)) { return; } if (line.StartsWith("Found team:") && line.Length >= 13) { string team = line.Substring(11).Trim(); if (!string.IsNullOrEmpty(team)) { context.Progress.StatusMessage = "Bootstrapping team " + team; } } else if (line.StartsWith("Cloning")) { context.Information.Counter += 1; context.Progress.ShortStatus = context.Information.Counter.ToString(); } context.OutputVM.SendOutput(line); }
private void Initalize() { ThemeOutput = Bash(@" for f in ~/.themes/* do if [[ -d ""$f/gtk-3.0"" ]] then echo $(basename -- ""$f"") fi done ") + Bash(@" for f in /usr/share/themes/* do if [[ -d ""$f/gtk-3.0"" ]] then echo $(basename -- ""$f"") fi done"); IconOutput = Bash(@"for f in /usr/share/icons/* do if [[ -f ""$f/index.theme"" ]] then echo $(basename -- ""$f"") fi done ") + Bash(@"for f in ~/.local/share/icons/* do if [[ -f ""$f/index.theme"" ]] then echo $(basename -- ""$f"") fi done ") + Bash(@"for f in ~/.icons/* do if [[ -f ""$f/index.theme"" ]] then echo $(basename -- ""$f"") fi done "); CursorOutput = Bash(@"for f in /usr/share/icons/* do if [[ -f ""$f/index.theme"" && -d ""$f/cursors"" ]] then echo $(basename -- ""$f"") fi done ") + Bash(@"for f in ~/.local/share/icons/* do if [[ -f ""$f/index.theme"" && -d ""$f/cursors"" ]] then echo $(basename -- ""$f"") fi done ") + Bash(@"for f in ~/.icons/* do if [[ -f ""$f/index.theme"" && -d ""$f/cursors"" ]] then echo $(basename -- ""$f"") fi done "); ShellOutput = Bash(@" for f in /usr/share/themes/* do if [[ -d ""$f/gnome-shell"" ]] then echo $(basename -- ""$f"") fi done") + Bash(@" for f in ~/.themes/* do if [[ -d ""$f/gnome-shell"" ]] then echo $(basename -- ""$f"") fi done"); Box box = new Box(Orientation.Vertical, 6); ThemeList = ThemeOutput.Split("\n").ToList(); ThemeList.RemoveAll(s => string.IsNullOrWhiteSpace(s)); // ThemeList.ForEach(s=>Console.Write(s+"\n")); ThemeList = ThemeList.Distinct().ToList(); ThemeList.Sort(); IconList = IconOutput.Split("\n").ToList(); IconList.RemoveAll(s => string.IsNullOrWhiteSpace(s)); // IconList.ForEach(s=>Console.Write(s+"\n")); IconList = IconList.Distinct().ToList(); IconList.Sort(); ShellList = ShellOutput.Split("\n").ToList(); ShellList.RemoveAll(s => string.IsNullOrWhiteSpace(s)); // iconList.ForEach(s=>Console.Write(s+"\n")); ShellList = ShellList.Distinct().ToList(); ShellList.Sort(); CursorList = CursorOutput.Split("\n").ToList(); CursorList.RemoveAll(s => string.IsNullOrWhiteSpace(s)); CursorList = CursorList.Distinct().ToList(); CursorList.Sort(); UserThemeExtensionExists = CheckUserThemeExtExists(); }
/// <summary> /// Executes cmd.exe /C "guestCommandLine" > file and parses the result. /// </summary> /// <param name="guestCommandLine">Guest command line, argument passed to cmd.exe.</param> /// <param name="startTimeout"></param> /// <param name="executionTimeout"></param> /// <param name="interactWithDesktop"></param> /// <returns>Standard output.</returns> public IShellOutput ExecuteElevatedCommandInGuest(string guestCommandLine, TimeSpan startTimeout, TimeSpan? executionTimeout = null, bool interactWithDesktop = true) { string guestStdOutFilename = Env.CreateTempFileInGuest(); string guestStdErrFilename = Env.CreateTempFileInGuest(); string guestCommandBatch = Env.CreateTempFileInGuest() + ".bat"; string hostCommandBatch = Path.GetTempFileName(); var hostCommand = new StringBuilder(); hostCommand.AppendLine("@echo off"); hostCommand.AppendLine(guestCommandLine); File.WriteAllText(hostCommandBatch, hostCommand.ToString()); try { Env.CopyFileFromHostToGuest(hostCommandBatch, guestCommandBatch); string cmdArgs = string.Format("> \"{0}\" 2>\"{1}\"", guestStdOutFilename, guestStdErrFilename); OsTestLogger.WriteLine("ExecuteElevatedCommandInGuest: " + guestCommandLine); var commandResult = PsExecWrapperInstance.ExecuteElevatedCommandInGuest(guestCommandBatch + " " + cmdArgs, null, startTimeout, executionTimeout, interactWithDesktop); var stdOut = ReadFile(guestStdOutFilename); var stdErr = ReadFile(guestStdErrFilename); var output = new ShellOutput(stdOut, stdErr, commandResult.ExitCode); OsTestLogger.WriteLine("VM_StdOut:" + output.StdOut); OsTestLogger.WriteLine("VM_StdErr:" + output.StdErr); return output; } finally { File.Delete(hostCommandBatch); Env.DeleteFileFromGuest(guestCommandBatch,true); Env.DeleteFileFromGuest(guestStdOutFilename, true); Env.DeleteFileFromGuest(guestStdErrFilename, true); } }