public void RunElevatedProcess() { var fact = Factories.First(); var output = new List <string>(); var redirector = new ListRedirector(output); using (var process = ProcessOutput.RunElevated( fact.Configuration.InterpreterPath, new[] { "-c", "import os, sys; print(sys.version[:3]); print(os.getcwd()); print(os.getenv('TEST_KEY')); sys.exit(7)" }, fact.Configuration.GetPrefixPath(), new[] { new KeyValuePair <string, string>("TEST_KEY", "TEST_VALUE") }, redirector, quoteArgs: true, elevate: false // don't really elevate for the test )) { Assert.IsTrue(process.Wait(TimeSpan.FromSeconds(30)), "Running " + fact.Configuration.Description + " exceeded timeout"); Console.WriteLine(string.Join(Environment.NewLine, output)); Assert.AreEqual(7, process.ExitCode); AssertUtil.AreEqual(output, fact.Configuration.Version.ToString(), PathUtils.TrimEndSeparator(fact.Configuration.GetPrefixPath()), "TEST_VALUE" ); } }
private async Task <bool> ExportAsync(string envPath, string destinationSpecFilePath, string[] args, ICondaEnvironmentManagerUI ui, CancellationToken ct) { bool success = false; using (await _working.LockAsync(ct)) { await EnsureActivatedAsync(); var operation = "conda " + string.Join(" ", args); ui?.OnOperationStarted(this, operation); ui?.OnOutputTextReceived(this, Strings.CondaExportStarted.FormatUI(envPath)); try { if (!PathUtils.IsValidPath(envPath)) { ui?.OnErrorTextReceived(this, Strings.CondaFolderNotFoundError.FormatUI(envPath)); success = false; return(success); } var entries = new List <string>(); var capture = new ListRedirector(entries); var redirector = new TeeRedirector(CondaEnvironmentManagerUIRedirector.Get(this, ui), capture); success = await DoOperationAsync(args, ui, ct, redirector); if (success) { try { using (var writer = new StreamWriter(destinationSpecFilePath, false, Encoding.UTF8)) { foreach (var line in entries) { await writer.WriteLineAsync(line); } } } catch (IOException ex) { ui?.OnErrorTextReceived(this, ex.Message); success = false; } catch (UnauthorizedAccessException ex) { ui?.OnErrorTextReceived(this, ex.Message); success = false; } catch (ArgumentException ex) { ui?.OnErrorTextReceived(this, ex.Message); success = false; } } return(success); } finally { var msg = success ? Strings.CondaExportSuccess : Strings.CondaExportFailed; ui?.OnOutputTextReceived(this, msg.FormatUI(envPath)); ui?.OnOperationFinished(this, operation, success); } } }
private static async Task <ProcessOutputResult> RunPythonScript(Redirector redirector, string interpreterPath, string script, string parameters) { var outputLines = new List <string>(); var errorLines = new List <string>(); ProcessOutput output = null; var arguments = "\"{0}\" {1}".FormatInvariant(script, parameters); var listRedirector = new ListRedirector(outputLines, errorLines); var outerRedirector = new TeeRedirector(redirector, listRedirector); output = ProcessOutput.Run(interpreterPath, new string[] { arguments }, null, null, false, outerRedirector); var result = await WaitForOutput(interpreterPath, output); result.StandardOutputLines = outputLines.ToArray(); result.StandardErrorLines = errorLines.ToArray(); return(result); }
public async Task <string> CloneAsync(string repoUrl, string targetParentFolderPath) { Directory.CreateDirectory(targetParentFolderPath); string localTemplateFolder = GetClonedFolder(repoUrl, targetParentFolderPath); if (Directory.Exists(localTemplateFolder)) { ShellUtils.DeleteDirectory(localTemplateFolder); } // Ensure we always capture the output, because we need to check for errors in stderr var stdOut = new List <string>(); var stdErr = new List <string>(); Redirector redirector; if (_redirector != null) { redirector = new TeeRedirector(_redirector, new ListRedirector(stdOut, stdErr)); } else { redirector = new ListRedirector(stdOut, stdErr); } var arguments = new string[] { "clone", repoUrl }; using (var output = ProcessOutput.Run(_gitExeFilePath, arguments, targetParentFolderPath, GetEnvironment(), false, redirector)) { await output; var r = new ProcessOutputResult() { ExeFileName = _gitExeFilePath, ExitCode = output.ExitCode, StandardOutputLines = stdOut.ToArray(), StandardErrorLines = stdErr.ToArray(), }; if (output.ExitCode < 0 || HasFatalError(stdErr)) { if (Directory.Exists(localTemplateFolder)) { // Don't leave a failed clone on disk try { ShellUtils.DeleteDirectory(localTemplateFolder); } catch (Exception ex) when(!ex.IsCriticalException()) { } } throw new ProcessException(r); } if (!Directory.Exists(localTemplateFolder)) { throw new ProcessException(r); } return(localTemplateFolder); } }