|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | + |
| 9 | + |
| 10 | +namespace PythonShell { |
| 11 | + public class ShellConfig { |
| 12 | + public String DefaultPythonPath { get; set; } = "python3"; |
| 13 | + public String DefaultPythonVersion { get; set; } = "3.9.13"; |
| 14 | + public String? DefaultWorkingDirectory { get; set; } = null; |
| 15 | + public String? PythonRequireFile { get; set; } = null; |
| 16 | + public String[]? PythonRequires { get; set; } = null; |
| 17 | + |
| 18 | + internal String? AppDir { get; set; } = null; |
| 19 | + internal String TempDir { |
| 20 | + get => Path.Join(AppDir, "temp"); |
| 21 | + } |
| 22 | + internal String InstanceDir { |
| 23 | + get => Path.Join(AppDir, "instances"); |
| 24 | + } |
| 25 | + internal String? DefaultPythonEnvPath { get; set; } = null; |
| 26 | + |
| 27 | + public ShellConfig() { |
| 28 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { |
| 29 | + if (this.DefaultPythonPath == "python" || this.DefaultPythonPath == "python2" || this.DefaultPythonPath == "python3") { |
| 30 | + this.DefaultPythonPath = $"/usr/bin/{this.DefaultPythonPath}"; |
| 31 | + } |
| 32 | + else if (!File.Exists(this.DefaultPythonPath)) { |
| 33 | + this.DefaultPythonPath = "/usr/bin/python3"; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (this.DefaultWorkingDirectory != null) { |
| 38 | + this.DefaultWorkingDirectory = Path.GetFullPath(this.DefaultWorkingDirectory); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public class Shell { |
| 44 | + private List<Int32> RunningProccese { get; set; } = new List<Int32>(); |
| 45 | + |
| 46 | + public Boolean CreateDefaultEnv { get; set; } = false; |
| 47 | + public ShellConfig Config { get; set; } = new ShellConfig(); |
| 48 | + public Boolean Resolved { |
| 49 | + get => this.RunningProccese.Count == 0; |
| 50 | + } |
| 51 | + |
| 52 | + public async Task Initialize() { |
| 53 | + await Utils.Util.InitializeApp(this.Config, this.CreateDefaultEnv); |
| 54 | + } |
| 55 | + |
| 56 | + public void Clear() { |
| 57 | + foreach (var path in Directory.GetDirectories(this.Config.InstanceDir)) { |
| 58 | + if (Path.GetFileName(path).ToLower() != "default") { |
| 59 | + Console.WriteLine(path); |
| 60 | + // Directory.Delete(path, true); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public async Task<Process?> RunFile(String pythonFile, String? workingDirectory = null, Boolean useInstance = false, String? instanceName = null, Boolean echo = true, Listener.ShellListener? listener = null) { |
| 66 | + String pythonToRun; |
| 67 | + String instanceDir = ""; |
| 68 | + if (useInstance) { |
| 69 | + var instanceMap = instanceName == null ? Utils.Util.CreateShellInstance(this.Config, instanceName) : Utils.Util.GetShellInstance(this.Config, instanceName); |
| 70 | + pythonToRun = instanceMap.Python; |
| 71 | + instanceDir = instanceMap.Dir; |
| 72 | + } |
| 73 | + else { |
| 74 | + pythonToRun = this.Config.DefaultPythonEnvPath ?? this.Config.DefaultPythonPath; |
| 75 | + } |
| 76 | + |
| 77 | + var process = new Process { |
| 78 | + StartInfo = new ProcessStartInfo { |
| 79 | + FileName = pythonToRun, |
| 80 | + ArgumentList = { "-u", Path.GetFullPath(pythonFile) }, |
| 81 | + WorkingDirectory = this.Config.DefaultWorkingDirectory ?? workingDirectory!, |
| 82 | + UseShellExecute = true, WindowStyle = ProcessWindowStyle.Hidden |
| 83 | + } |
| 84 | + }!; |
| 85 | + Int32 processId = Int32.MaxValue; |
| 86 | + |
| 87 | + process.OutputDataReceived += (s, e) => { |
| 88 | + if (echo) { |
| 89 | + Console.WriteLine(e.Data); |
| 90 | + } |
| 91 | + |
| 92 | + listener!.OnMessage!(e.Data!); |
| 93 | + }; |
| 94 | + process.ErrorDataReceived += (s, e) => { |
| 95 | + this.RunningProccese.Remove(processId); |
| 96 | + if (useInstance) { |
| 97 | + Directory.Delete(instanceDir, true); |
| 98 | + } |
| 99 | + |
| 100 | + listener!.OnError!(e); |
| 101 | + }; |
| 102 | + process.Exited += (s, e) => { |
| 103 | + this.RunningProccese.Remove(processId); |
| 104 | + if (useInstance) { |
| 105 | + Directory.Delete(instanceDir, true); |
| 106 | + } |
| 107 | + |
| 108 | + listener!.OnComplete!(); |
| 109 | + }; |
| 110 | + process.Start(); |
| 111 | + processId = process.Id; |
| 112 | + this.RunningProccese.Add(processId); |
| 113 | + await process.WaitForExitAsync(); |
| 114 | + |
| 115 | + return process; |
| 116 | + } |
| 117 | + |
| 118 | + public async Task<Process?> RunString(String pythonCode, String? workingDirectory = null, Boolean useInstance = false, String? instanceName = null, Boolean echo = true, Listener.ShellListener? listener = null) { |
| 119 | + String tempPythonFileName = DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss"); |
| 120 | + String tempPythonFile; |
| 121 | + if (useInstance) { |
| 122 | + var instanceMap = instanceName == null ? Utils.Util.CreateShellInstance(this.Config, instanceName) : Utils.Util.GetShellInstance(this.Config, instanceName); |
| 123 | + tempPythonFile = Path.Join(instanceMap.Dir, "temp", tempPythonFileName); |
| 124 | + } |
| 125 | + else { |
| 126 | + tempPythonFile = Path.Join(this.Config.TempDir, tempPythonFileName); |
| 127 | + } |
| 128 | + File.WriteAllText(tempPythonFile, pythonCode, System.Text.Encoding.UTF8); |
| 129 | + |
| 130 | + var newListener = new Listener.ShellListener { |
| 131 | + OnMessage = listener!.OnMessage, |
| 132 | + OnError = (e) => { |
| 133 | + listener!.OnError!(e); |
| 134 | + if (File.Exists(tempPythonFile)) { |
| 135 | + File.Delete(tempPythonFile); |
| 136 | + } |
| 137 | + }, |
| 138 | + OnComplete = () => { |
| 139 | + listener!.OnComplete!(); |
| 140 | + if (File.Exists(tempPythonFile)) { |
| 141 | + File.Delete(tempPythonFile); |
| 142 | + } |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + return await this.RunFile( |
| 147 | + tempPythonFile, workingDirectory, useInstance, instanceName, echo, newListener |
| 148 | + ); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments