Skip to content

Commit

Permalink
Merge pull request #166 from simplitech/#164---python-support
Browse files Browse the repository at this point in the history
#164   Python support
  • Loading branch information
Relfos authored Mar 1, 2019
2 parents 5c6407d + 8520346 commit c51ba39
Show file tree
Hide file tree
Showing 16 changed files with 963 additions and 171 deletions.
1 change: 1 addition & 0 deletions NEO-Debugger-Core/NEO-Debugger-Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<ProjectReference Include="..\Neo-Emulator\Neo-Emulator.csproj" />
<ProjectReference Include="..\Neo-Compiler-Lib\Neo-Compiler-Lib.csproj" />
<ProjectReference Include="..\Neo-Boa-Proxy-Lib\Neo-Boa-Proxy-Lib.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.10.0" />
Expand Down
7 changes: 7 additions & 0 deletions Neo-Boa-Proxy-Lib/Neo-Boa-Proxy-Lib.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
88 changes: 88 additions & 0 deletions Neo-Boa-Proxy-Lib/PythonCompilerProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace Neo_Boa_Proxy_Lib
{
public class PythonCompilerProxy
{
public static bool Execute(string outputFilePath, string pythonExecutableName, Action<string> logCallback)
{
bool success = false;
var proc = new Process();
var info = new ProcessStartInfo();

var loadCode = $"from boa.compiler import Compiler;Compiler.load_and_save('{outputFilePath}')";
info.FileName = pythonExecutableName;
info.Arguments = $"-c \"{loadCode}\"";

info.UseShellExecute = false;
info.RedirectStandardInput = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.CreateNoWindow = true;
proc.EnableRaisingEvents = true;

proc.StartInfo = info;

try
{
logCallback("Starting compilation...");

proc.Start();
proc.WaitForExit();

var log = FetchLog(proc.StandardOutput.ReadToEnd());
string last = null;
foreach (var temp in log)
{
var line = temp.Replace("\r", "");
if (string.IsNullOrEmpty(line))
{
continue;
}
logCallback(line);
last = line;
}

if (log.Length == 0)
{
success = true;
}

log = FetchLog(proc.StandardError.ReadToEnd());
foreach (var line in log)
{
logCallback(line);
}

if (log.Length > 0)
{
success = false;
}

if (proc.ExitCode != 0 || !success)
{
logCallback("Error during compilation.");
}
else
{
success = true;
logCallback("Compilation successful.");
}
}
catch (Exception ex)
{
logCallback(ex.Message);
}

return success;
}

private static string[] FetchLog(string content)
{
return content.Split('\n').Where(x => !string.IsNullOrEmpty(x)).ToArray();
}

}
}
33 changes: 23 additions & 10 deletions Neo-Debugger-Core-UnitTests/InvocationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
using NeoDebuggerCore.Utils;
using NUnit.Framework;

namespace Neo_Debugger_UI_UnitTests
namespace Neo
{
[TestFixture]
public class DebuggerCoreUnitTests
{

private string _compilerFolder = Path.Combine(Path.Combine(
Directory.GetParent(TestContext.CurrentContext.TestDirectory).Parent.Parent.Parent.FullName,
"Output"), "neo-compiler");

[Test]
"Output"), "neo-compiler");

private NeonCompiler compiler = NeonCompiler.GetInstance(new DebuggerSettings());

[Test]
public void TestParameterParsing()
{
var argList = DebuggerUtils.GetArgsListAsNode("\"symbol\"");
Expand All @@ -40,20 +42,31 @@ public void TestUseCompilerFromOutputFolder()
}

[Test]
public void TestSimpleExecution()
public void TestCSharpCompiler()
{
var path = TestContext.CurrentContext.TestDirectory;
Directory.SetCurrentDirectory(path);
var compiler = NeonCompiler.GetInstance(new DebuggerSettings());
Directory.SetCurrentDirectory(path);
var fullFilePath = Path.Combine(path, "SampleContract.cs");
var sourceCode = File.ReadAllText(fullFilePath);
Assert.NotNull(sourceCode);
var compilerFolder = Path.Combine(Path.Combine(Directory.GetParent(path).Parent.Parent.Parent.FullName, "Output"), "neo-compiler");
var compiled = compiler.CompileContract(sourceCode, fullFilePath, Neo.Debugger.Core.Data.SourceLanguage.CSharp, compilerFolder);
Assert.IsTrue(compiled);
}

[Test]
}

[Test]
public void TestPythonCompiler()
{
var path = TestContext.CurrentContext.TestDirectory;
Directory.SetCurrentDirectory(path);
var fullFilePath = Path.Combine(path, "NEP5.py");
var sourceCode = File.ReadAllText(fullFilePath);
Assert.NotNull(sourceCode);
var compiled = compiler.CompileContract(sourceCode, fullFilePath, Neo.Debugger.Core.Data.SourceLanguage.Python);
Assert.IsTrue(compiled);
}

[Test]
[Ignore("It won't pass in all OS")]
public void TestGetCompilerInstance()
{
Expand Down
Loading

0 comments on commit c51ba39

Please sign in to comment.