in tools/AstVisualizer/PythonExtractor/Extractor.cs [20:88]
def reverseArray(arr, start, end):
while (start < end):
temp = arr[start]
arr[start] = arr[end]
arr[end] = temp
start += 1
end = end-1
")]
public class Extractor : ILanguageExtractor
{
private static string GetTempDirectory()
{
string path;
do
{
path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
} while (Directory.Exists(path));
Directory.CreateDirectory(path);
return path;
}
public (XDocument, IEnumerable<IDiagnosticItem>) Extract(string source)
{
// Create a temporary directory and place the incoming source in there.
string sourceDirectory = GetTempDirectory();
string pythonFilePath = Path.Combine(sourceDirectory, "source.py");
string resultDirectory = GetTempDirectory();
try
{
File.WriteAllText(pythonFilePath, source);
// Calculate the path to the python extractor. We do this by traversing the
// known structure of the source tree for now.
var localDllPath = typeof(Extractor).Assembly.Location;
var parts = localDllPath.Split(Path.DirectorySeparatorChar);
// Get rid of all the directories leading to the ast extractor (i.e. this executable).
var root = string.Join(Path.DirectorySeparatorChar.ToString(), parts.Take<string>(parts.Length - 6));
// Add the directory path to the extractor .py file.
string PythonExtractorSource = Path.Combine(root, "Extractors", "Python", "extractor", "pythonextractor.py");
// Start the python extractor.
using (var process = new Process())
{
process.StartInfo.FileName = "python";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute= false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.StartInfo.Arguments = $"\"{PythonExtractorSource}\" --formatxml=False \"{sourceDirectory}\" \"{resultDirectory}\"";
process.Start();
// string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
return (XDocument.Load(Path.Combine(resultDirectory, "source.xml")), null);
}
finally
{
// Clean up the temporary directories and their content.
Directory.Delete(sourceDirectory, recursive: true);
Directory.Delete(resultDirectory, recursive: true);
}
}
}