- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonShell.cs
More file actions
Latest commit
102 lines (85 loc) · 4.94 KB
/
Copy pathPythonShell.cs
File metadata and controls
102 lines (85 loc) · 4.94 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
usingSystem;
usingSystem.IO;
usingSystem.Threading.Tasks;
usingSystem.Linq;
usingSystem.IO.Compression;
usingSystem.Runtime.InteropServices;
namespacePythonShell{
publicclassPythonShellConfig{
publicStringDefaultPythonPath{get;set;}="python3";
publicStringDefaultPythonVersion{get;set;}="3.9.13";
publicPythonShellConfig(){
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)||RuntimeInformation.IsOSPlatform(OSPlatform.OSX)){
if(this.DefaultPythonPath=="python"||this.DefaultPythonPath=="python2"||this.DefaultPythonPath=="python3"){
this.DefaultPythonPath=$"/usr/bin/{this.DefaultPythonPath}";
}
elseif(!File.Exists(this.DefaultPythonPath)){
this.DefaultPythonPath="/usr/bin/python3";
}
}
}
}
publicclassPythonShell{
publicPythonShell(PythonShellConfigshellConfig){}
publicvoidClear(){
Shell.ShellManager.Clear();
foreach(varpathinDirectory.GetDirectories(Config.TempDir)){
Directory.Delete(path,true);
}
foreach(varpathinDirectory.GetFiles(Config.TempDir)){
File.Delete(path);
}
}
publicasyncTaskInitialize(){
Console.WriteLine("Initializing shell...");
Config.DefaultPythonVersion=Config.CheckPythonVersion(Config.DefaultPythonVersion);
if(!Directory.Exists(Config.AppDir))Directory.CreateDirectory(Config.AppDir);
if(!Directory.Exists(Config.InstanceDir))Directory.CreateDirectory(Config.InstanceDir);
if(!Directory.Exists(Config.TempDir))Directory.CreateDirectory(Config.TempDir);
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){
Console.WriteLine("Check default python binary files...");
StringpythonDir=Path.Join(Config.AppDir,"python");
if(!Directory.Exists(pythonDir)){
StringpythonZipFile=Path.Join(Config.TempDir,"python.zip");
awaitUtil.DownloadFile($"https://www.python.org/ftp/python/{Config.DefaultPythonVersion}/python-{Config.DefaultPythonVersion}-embed-{Config.PythonRuntimeBit}.zip",pythonZipFile,"binary");
ZipFile.ExtractToDirectory(pythonZipFile,pythonDir);
File.Delete(pythonZipFile);
varpthFile=Path.Join(pythonDir,$"python{Config.DefaultPythonVersion.Replace($".{Config.DefaultPythonVersion.Split(".").Last()}","").Replace(".","")}._pth");
File.WriteAllText(
pthFile,
File.ReadAllText(pthFile).Replace("#import site","import site")
);
}
Console.WriteLine("Python check finished.");
Config.DefaultPythonPath=Path.Join(pythonDir,"python.exe");
}
if(File.Exists(Config.DefaultPythonPath)){
Console.WriteLine("Default settings for virtualenv...");
varp=Util.RunSettingCmd(Config.DefaultPythonPath,newString[]{"-m","pip","install","pip","--upgrade"});
if(p.ExitCode!=0){
StringpipInstallFile=Path.Join(Config.TempDir,"get-pip.py");
awaitUtil.DownloadFile("https://bootstrap.pypa.io/pip/get-pip.py",pipInstallFile,"text");
Util.RunSettingCmd(Config.DefaultPythonPath,newString[]{pipInstallFile});
File.Delete(pipInstallFile);
}
Util.RunSettingCmd(Config.DefaultPythonPath,newString[]{"-m","pip","install","virtualenv","--upgrade"});
Console.WriteLine("Virtualenv settings finished.");
}
StringdefaultEnvdir=Path.Join(Config.InstanceDir,"default");
if(!Directory.Exists(defaultEnvdir)){
Console.WriteLine("Creating default env...");
Shell.ShellManager.CreateInstance("default");
Console.WriteLine("Default env created.");
}
Console.WriteLine("Shell initialized.");
}
publicasyncTaskRunFile(StringpythonFile,Shell.ShellInstance?instance=null,String?workingDirectory=null,Listener.ShellListener?listener=null,Booleanecho=true){
instance=instance??Shell.ShellManager.GetInstance("default");
awaitinstance.RunFile(pythonFile,workingDirectory,listener,echo);
}
publicasyncTaskRunString(StringpythonCode,Shell.ShellInstance?instance=null,String?workingDirectory=null,Listener.ShellListener?listener=null,Booleanecho=true){
instance=instance??Shell.ShellManager.GetInstance("default");
awaitinstance.RunString(pythonCode,workingDirectory,listener,echo);
}
}
}