forked from microsoft/qsharp-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.cs
More file actions
128 lines (120 loc) · 4.74 KB
/
Copy pathProcess.cs
File metadata and controls
128 lines (120 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
namespace Microsoft.Quantum.QsCompiler
{
public static class ProcessRunner
{
/// <summary>
/// Starts the given process and accumulates the received output and error data in the given StringBuilders.
/// Returns true if the process completed within the specified time without throwing an exception, and false otherwise.
/// Any thrown exception is returned as out parameter.
/// </summary>
public static bool Run(Process process, StringBuilder output, StringBuilder error, out Exception? ex, int timeout)
{
using (var outputWaitHandle = new AutoResetEvent(false))
using (var errorWaitHandle = new AutoResetEvent(false))
{
void AddOutput(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
}
void AddError(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
}
process.OutputDataReceived += AddOutput;
process.ErrorDataReceived += AddError;
try
{
ex = null;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
return process.WaitForExit(timeout)
&& outputWaitHandle.WaitOne()
&& errorWaitHandle.WaitOne();
}
catch (Exception e)
{
ex = e;
}
finally
{
// unsubscribe such that the AutoResetEvents are not accessed after disposing
process.OutputDataReceived -= AddOutput;
process.ErrorDataReceived -= AddError;
}
return ex == null;
}
}
/// <summary>
/// Starts and runs a process invoking the given command with the given arguments.
/// If a dictionary of environment variables and their desired values is specified,
/// sets these environment variables prior to execution and resets them afterwards.
/// Accumulates the received output and error data in the respective StringBuilder and returns them as out parameters.
/// Returns true if the process completed within the specified time without throwing an exception, and false otherwise.
/// Returns the exit code of the process as well as any thrown exception as out parameter.
/// </summary>
public static bool Run(
string command,
string args,
out StringBuilder outstream,
out StringBuilder errstream,
out int exitCode,
out Exception? ex,
IDictionary<string, string>? envVariables = null,
int timeout = 10000)
{
var process = new Process();
process.StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
var origEnvVariables = new Dictionary<string, string>();
foreach (var entry in envVariables ?? new Dictionary<string, string>())
{
var orig = Environment.GetEnvironmentVariable(entry.Key);
origEnvVariables.Add(entry.Key, orig);
Environment.SetEnvironmentVariable(entry.Key, entry.Value);
}
(outstream, errstream) = (new StringBuilder(), new StringBuilder());
try
{
var exited = Run(process, outstream, errstream, out ex, timeout);
exitCode = process.ExitCode;
return exited;
}
finally
{
foreach (var entry in origEnvVariables)
{
Environment.SetEnvironmentVariable(entry.Key, entry.Value);
}
}
}
}
}