-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotPythonCommand.cs
More file actions
228 lines (209 loc) · 7.06 KB
/
Copy pathDotPythonCommand.cs
File metadata and controls
228 lines (209 loc) · 7.06 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using DotPython.Hosting.Packaging;
using DotPython.Language.Diagnostics;
using DotPython.Language.Text;
using DotPython.Runtime.Managed;
using DotPython.Runtime.Managed.Execution;
namespace DotPython.Cli;
internal static class DotPythonCommand
{
public static int Run(
IReadOnlyList<string> arguments,
TextReader standardInput,
TextWriter standardOutput,
TextWriter standardError,
CancellationToken cancellationToken = default
)
{
ArgumentNullException.ThrowIfNull(arguments);
ArgumentNullException.ThrowIfNull(standardInput);
ArgumentNullException.ThrowIfNull(standardOutput);
ArgumentNullException.ThrowIfNull(standardError);
if (arguments.Count == 0)
{
standardError.WriteLine(
"dotpython: interactive mode is not implemented; use -c, -, or a script path"
);
return 2;
}
if (arguments[0] is "-h" or "--help")
{
WriteHelp(standardOutput);
return 0;
}
if (arguments[0] is "-V" or "--version")
{
var compatibility = ManagedRuntimeDescriptor.Compatibility;
standardOutput.WriteLine(
$"DotPython {compatibility.Implementation} (Python {compatibility.LanguageVersion})"
);
return 0;
}
if (arguments[0] == "wheel")
{
return RunWheelCommand(arguments, standardOutput, standardError);
}
if (
!TryReadSource(
arguments,
standardInput,
standardError,
out var source,
out var moduleSearchPath
)
)
{
return 2;
}
try
{
var engine = new ManagedPythonEngine(
new ManagedModuleDiscoveryOptions { SearchPaths = [moduleSearchPath] }
);
var result = engine.Execute(
source,
standardOutput,
cancellationToken: cancellationToken
);
if (result.Success)
{
return 0;
}
foreach (var diagnostic in result.Diagnostics)
{
WriteDiagnostic(result.Source, diagnostic, standardError);
}
return 1;
}
catch (OperationCanceledException)
{
standardError.WriteLine("dotpython: execution cancelled");
return 130;
}
catch (Exception exception)
when (exception
is IOException
or InvalidDataException
or UnauthorizedAccessException
or ArgumentException
)
{
standardError.WriteLine($"dotpython: module discovery failed: {exception.Message}");
return 1;
}
}
private static int RunWheelCommand(
IReadOnlyList<string> arguments,
TextWriter standardOutput,
TextWriter standardError
)
{
if (arguments.Count != 3 || arguments[1] != "inspect")
{
standardError.WriteLine("dotpython: usage: dotpython wheel inspect <artifact.whl>");
return 2;
}
try
{
var inspection = PythonWheelInspector.Inspect(arguments[2]);
standardOutput.WriteLine(PythonWheelInspectionJson.Serialize(inspection));
return inspection.IsValid ? 0 : 1;
}
catch (IOException exception)
{
standardError.WriteLine(
$"dotpython: cannot inspect '{arguments[2]}': {exception.Message}"
);
return 1;
}
catch (UnauthorizedAccessException exception)
{
standardError.WriteLine(
$"dotpython: cannot inspect '{arguments[2]}': {exception.Message}"
);
return 1;
}
}
private static bool TryReadSource(
IReadOnlyList<string> arguments,
TextReader standardInput,
TextWriter standardError,
out SourceText source,
out string moduleSearchPath
)
{
if (arguments[0] == "-c")
{
if (arguments.Count < 2)
{
standardError.WriteLine("dotpython: argument expected for -c");
source = new SourceText(string.Empty, "<string>");
moduleSearchPath = Directory.GetCurrentDirectory();
return false;
}
source = new SourceText(arguments[1], "<string>");
moduleSearchPath = Directory.GetCurrentDirectory();
return true;
}
if (arguments[0] == "-")
{
source = new SourceText(standardInput.ReadToEnd(), "<stdin>");
moduleSearchPath = Directory.GetCurrentDirectory();
return true;
}
if (arguments[0].StartsWith('-'))
{
standardError.WriteLine($"dotpython: unsupported option '{arguments[0]}'");
source = new SourceText(string.Empty, "<command-line>");
moduleSearchPath = Directory.GetCurrentDirectory();
return false;
}
try
{
var fullPath = Path.GetFullPath(arguments[0]);
source = new SourceText(File.ReadAllText(fullPath), fullPath);
moduleSearchPath = Path.GetDirectoryName(fullPath) ?? Directory.GetCurrentDirectory();
return true;
}
catch (IOException exception)
{
standardError.WriteLine(
$"dotpython: cannot read '{arguments[0]}': {exception.Message}"
);
source = new SourceText(string.Empty, arguments[0]);
moduleSearchPath = Directory.GetCurrentDirectory();
return false;
}
catch (UnauthorizedAccessException exception)
{
standardError.WriteLine(
$"dotpython: cannot read '{arguments[0]}': {exception.Message}"
);
source = new SourceText(string.Empty, arguments[0]);
moduleSearchPath = Directory.GetCurrentDirectory();
return false;
}
}
private static void WriteDiagnostic(
SourceText source,
Diagnostic diagnostic,
TextWriter standardError
)
{
var position = source.GetLinePosition(Math.Min(diagnostic.Span.Start, source.Length));
standardError.WriteLine(
$"{source.FilePath ?? "<input>"}:{position.Line + 1}:{position.Character + 1}: "
+ $"{diagnostic.Code}: {diagnostic.Message}"
);
}
private static void WriteHelp(TextWriter output)
{
output.WriteLine("Usage: dotpython -c command [args]");
output.WriteLine(" dotpython - [args]");
output.WriteLine(" dotpython script.py [args]");
output.WriteLine(" dotpython wheel inspect artifact.whl");
output.WriteLine();
output.WriteLine(
"Current managed subset: literals, names, assignment, arithmetic, and calls."
);
}
}