public TaskPyRepl(String arg) { type = arg; pengine = IPythonUtil.GetPyEngine(); pscope = IPythonUtil.GetNewScope(); this.ProcessCommand(); }
/// <summary> /// Invoike DLR Py script /// </summary> /// <param name="resource"></param> /// <param name="method"></param> internal static void ModePyExec(String resource, String method, String targs) { String pyCode = ""; switch (method.ToLower()) { case "mem": Console.WriteLine("Memory resource"); break; case "sfile": Console.WriteLine("File resource"); // Local FS load if (File.Exists(resource)) { try { pyCode = File.ReadAllText(resource); } catch (Exception fex) { GeneralUtil.Usage(fex.Message); } } else { GeneralUtil.Usage("Check file location: " + resource); } break; default: GeneralUtil.Usage("Unknown -method: " + method); break; } // once we get resource code data by a valid method, execute it in DLR try { dynamic pengine = IPythonUtil.GetPyEngine(); dynamic pscope = IPythonUtil.GetNewScope(); if (ConfigUtil.DEBUG) { Console.WriteLine("Python Code: \n\n__BEGIN__\n\n{0}\n\n__END__\n\n", pyCode); } dynamic pythonScript = IPythonUtil.GetPyEngine().CreateScriptSourceFromString(pyCode); pscope.SetVariable("targs", targs); pythonScript.Execute(pscope); } catch (Exception ae) { GeneralUtil.Usage("Iron Python Scope/Execution not created: " + ae.Message); } }
/// <summary> /// Run Cs extension ora Py Script /// </summary> /// <param name="mmLocation"></param> public static void RunMMRecord(String mmLocation) { Dictionary <String, MMRecord> mmfRepo = MMemoryLoader.GetMMFRepo(); Console.WriteLine("In RunMMRecord"); if (mmfRepo.ContainsKey(mmLocation)) { MMRecord mmr; if (mmfRepo.TryGetValue(mmLocation, out mmr)) { Console.WriteLine("After TryGetValue"); Console.WriteLine("Found {0}, {1}, {2}", mmr.ResourceSize, mmr.ResourceType, mmr.ResourceMMFile); switch (mmr.ResourceType.ToLower()) { case "py": try { dynamic pengine = IPythonUtil.GetPyEngine(); dynamic pscope = IPythonUtil.GetNewScope(); String pyCodeContent = MMemoryLoader.MMRecordContentString(mmLocation); if (ConfigUtil.DEBUG) { Console.WriteLine("Python Code: \n\n__BEGIN__\n\n{0}\n\n__END__\n\n", pyCodeContent); } dynamic pythonScript = IPythonUtil.GetPyEngine().CreateScriptSourceFromString(pyCodeContent); Console.WriteLine("Execute Python script ({0}) contents from memory", mmLocation); pythonScript.Execute(pscope); } catch (Exception ae) { Console.WriteLine("Iron Python Scope/Execution not created: {0}", ae.Message); } break; case "cs": Console.WriteLine("ResourceType {0}", mmr.ResourceType); String csCodeContent = MMemoryLoader.MMRecordContentString(mmLocation); Console.WriteLine("csCodeContent {0}", csCodeContent); // Load CS Source Code of the extension, MMRecord's name is the name of the TypeToRun param // when MMemoryLoader loads resource it might contain extension on the file, strip it when passing to TypeToRun // Name of the MMRecord is the name of the TypeToRun minus extension. Name your extension file accordingly // Example: If Memory File is WmiQuery.cs then TypeToRun is `WmiQuery` DynCSharpRunner.CompileRunXSource(csCodeContent, String.Join(".", new String[2] { "Typhoon.Extensions", mmLocation.Replace( String.Concat(new String[] { ".", mmr.ResourceType }), String.Empty) })); break; default: Console.WriteLine("Unknown ResourceType {0}", mmr.ResourceType); break; } } else { Console.WriteLine("Unable to get {0}. Valid value?", mmLocation); } } else { Console.WriteLine("Unable to find {0}. Valid value?", mmLocation); } }