@@ -26,18 +26,40 @@ private static String GetShellInstanceDir(ShellConfig config, String? instanceNa
2626 }
2727 }
2828
29- private static async Task DownloadFile ( String url , String destFile ) {
29+ private static async Task DownloadFile ( String url , String destFile , String fileType = "text" ) {
3030 var client = new HttpClient ( ) ;
3131 var resp = await client . GetAsync ( url ) ;
32- using ( var fs = new FileStream ( destFile , FileMode . CreateNew ) ) {
33- await resp . Content . CopyToAsync ( fs ) ;
32+
33+ if ( fileType . ToLower ( ) == "binary" ) {
34+ File . WriteAllBytes ( destFile , await resp . Content . ReadAsByteArrayAsync ( ) ) ;
35+ }
36+ else if ( fileType . ToLower ( ) == "text" ) {
37+ File . WriteAllText ( destFile , await resp . Content . ReadAsStringAsync ( ) ) ;
3438 }
3539 }
3640
41+ private static Process RunSettingCmd ( String pythonToRun , String [ ] arguments ) {
42+ var process = new Process {
43+ StartInfo = new ProcessStartInfo {
44+ FileName = pythonToRun ,
45+ Arguments = String . Join ( " " , arguments ) ,
46+ UseShellExecute = false ,
47+ RedirectStandardOutput = true , RedirectStandardError = true
48+ }
49+ } ! ;
50+
51+ process . Start ( ) ;
52+ process . WaitForExit ( ) ;
53+
54+ return process ;
55+ }
56+
3757 public static async Task InitializeApp ( ShellConfig config , Boolean createDefaultEnv ) {
58+ Console . WriteLine ( "Initializing shell..." ) ;
3859 config . DefaultPythonVersion = Util . CheckPythonVersion ( config . DefaultPythonVersion ) ;
3960
40- String userHomeDir = System . Environment . GetEnvironmentVariable ( "HOME" ) ! ;
61+ Console . WriteLine ( "Check shell app directory..." ) ;
62+ var userHomeDir = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
4163 config . AppDir = Path . Join ( userHomeDir , ".python_shell.net" ) ;
4264 if ( ! Directory . Exists ( config . AppDir ) ) {
4365 Directory . CreateDirectory ( config . AppDir ) ;
@@ -50,51 +72,60 @@ public static async Task InitializeApp(ShellConfig config, Boolean createDefault
5072 if ( ! Directory . Exists ( config . InstanceDir ) ) {
5173 Directory . CreateDirectory ( config . InstanceDir ) ;
5274 }
75+ Console . WriteLine ( "Shell app directory check finished." ) ;
5376
5477 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ) {
78+ Console . WriteLine ( "Check default python binary files..." ) ;
5579 String pythonDir = Path . Join ( config . AppDir , "python" ) ;
56- String pythonZipFile = Path . Join ( config . TempDir , "python.zip" ) ;
57- await Util . DownloadFile ( $ "https://www.python.org/ftp/python/${ config . DefaultPythonVersion } /python-{ config . DefaultPythonVersion } -embed-amd64.zip", pythonZipFile ) ;
58- ZipFile . ExtractToDirectory ( pythonZipFile , pythonDir ) ;
59- File . Delete ( pythonZipFile ) ;
60-
61- var pthFile = Util . GetPythonPthFile ( config , pythonDir ) ;
62- File . WriteAllText (
63- pthFile ,
64- File . ReadAllText ( pthFile ) . Replace ( "#import site" , "import site" ) ,
65- System . Text . Encoding . UTF8
66- ) ;
80+ if ( ! Directory . Exists ( pythonDir ) ) {
81+ String pythonZipFile = Path . Join ( config . TempDir , "python.zip" ) ;
82+ await Util . DownloadFile ( $ "https://www.python.org/ftp/python/{ config . DefaultPythonVersion } /python-{ config . DefaultPythonVersion } -embed-amd64.zip", pythonZipFile , "binary" ) ;
83+ ZipFile . ExtractToDirectory ( pythonZipFile , pythonDir ) ;
84+ File . Delete ( pythonZipFile ) ;
85+
86+ var pthFile = Util . GetPythonPthFile ( config , pythonDir ) ;
87+ File . WriteAllText (
88+ pthFile ,
89+ File . ReadAllText ( pthFile ) . Replace ( "#import site" , "import site" )
90+ ) ;
91+ }
92+ Console . WriteLine ( "Python check finished." ) ;
6793
68- config . DefaultPythonPath = Path . Join ( pythonDir , "python" ) ;
94+ config . DefaultPythonPath = Path . Join ( pythonDir , "python.exe " ) ;
6995 }
7096
7197 if ( File . Exists ( config . DefaultPythonPath ) ) {
72- var p = Process . Start ( config . DefaultPythonPath , new String [ ] { "-m" , "pip" , "install" , "pip" , "--upgrade" } ) ! ;
73- p . WaitForExit ( ) ;
98+ Console . WriteLine ( "Default settings for virtualenv..." ) ;
99+ var p = Util . RunSettingCmd ( config . DefaultPythonPath , new String [ ] { "-m" , "pip" , "install" , "pip" , "--upgrade" } ) ;
74100 if ( p . ExitCode != 0 ) {
75101 String pipInstallFile = Path . Join ( config . TempDir , "get-pip.py" ) ;
76- await Util . DownloadFile ( "https://bootstrap.pypa.io/pip/get-pip.py" , pipInstallFile ) ;
77- Process . Start ( config . DefaultPythonPath , new String [ ] { pipInstallFile } ) . WaitForExit ( ) ;
102+ await Util . DownloadFile ( "https://bootstrap.pypa.io/pip/get-pip.py" , pipInstallFile , "text" ) ;
103+ Util . RunSettingCmd ( config . DefaultPythonPath , new String [ ] { pipInstallFile } ) ;
78104 File . Delete ( pipInstallFile ) ;
79105 }
80106
81- Process . Start ( config . DefaultPythonPath , new String [ ] { "-m" , "pip" , "install" , "virtualenv" , "--upgrade" } ) . WaitForExit ( ) ;
107+ Util . RunSettingCmd ( config . DefaultPythonPath , new String [ ] { "-m" , "pip" , "install" , "virtualenv" , "--upgrade" } ) ;
108+ Console . WriteLine ( "Virtualenv settings finished." ) ;
82109 }
83110
84111 String defaultEnvdir = Util . GetShellInstanceDir ( config , "default" ) ;
85112 if ( createDefaultEnv ) {
113+ Console . WriteLine ( "Creating default env..." ) ;
86114 if ( Directory . Exists ( defaultEnvdir ) ) {
87115 config . DefaultPythonEnvPath = Util . GetVirtualEnv ( config , "default" ) ;
88116 }
89117 else {
90118 config . DefaultPythonEnvPath = Util . CreateShellInstance ( config , "default" ) . Python ;
91119 }
92120
93- Util . InstallRequiresToEnv ( config , config . DefaultPythonEnvPath ) ;
121+ // Util.InstallRequiresToEnv(config, config.DefaultPythonEnvPath);
122+ Console . WriteLine ( "Default env created." ) ;
94123 }
95124 // else if (File.Exists(config.DefaultPythonPath)) {
96125 // Util.InstallRequiresToEnv(config, config.DefaultPythonPath);
97126 // }
127+
128+ Console . WriteLine ( "Shell initialized." ) ;
98129 }
99130
100131 public static String CheckPythonVersion ( String rawPythonVersion ) {
@@ -126,7 +157,7 @@ public static String CheckPythonVersion(String rawPythonVersion) {
126157 }
127158
128159 public static String GetPythonPthFile ( ShellConfig config , String pythonDiir ) {
129- return Path . Join ( pythonDiir , $ "python{ config . DefaultPythonVersion . Replace ( $ ".{ config . DefaultPythonVersion . Split ( "." ) . Last ( ) } ", "" ) } ._pth") ;
160+ return Path . Join ( pythonDiir , $ "python{ config . DefaultPythonVersion . Replace ( $ ".{ config . DefaultPythonVersion . Split ( "." ) . Last ( ) } ", "" ) . Replace ( "." , "" ) } ._pth") ;
130161 }
131162
132163 public static void ClearShellInstance ( ShellConfig config , String instanceName ) {
@@ -140,38 +171,42 @@ public static void ClearShellInstance(ShellConfig config, String instanceName) {
140171 }
141172 }
142173
143- public static InstanceMap CreateShellInstance ( ShellConfig config , String ? instanceName = null ) {
174+ public static InstanceMap CreateShellInstance ( ShellConfig config , String ? instanceName = null , Boolean echo = true ) {
144175 instanceName = instanceName ?? DateTime . Now . ToString ( "yyyy.MM.dd.HH.mm.ss" ) ;
176+ if ( echo ) Console . WriteLine ( $ "Creating shell instance [{ instanceName } ]...") ;
145177 String instanceDir = Util . GetShellInstanceDir ( config , instanceName ) ;
146178 String envPython ;
147179
148180 if ( ! Directory . Exists ( instanceDir ) ) {
149181 Directory . CreateDirectory ( instanceDir ) ;
150182 Directory . CreateDirectory ( Path . Join ( instanceDir , "temp" ) ) ;
151- envPython = Util . CreateVirtualEnv ( config , instanceName ) ;
183+ envPython = Util . CreateVirtualEnv ( config , instanceName , echo : echo ) ;
152184 }
153185 else {
154186 Util . ClearShellInstance ( config , instanceName ) ;
155187 envPython = Util . GetVirtualEnv ( config , instanceName ) ;
156188 }
189+ if ( echo ) Console . WriteLine ( "Shell instance created." ) ;
157190
158191 return new InstanceMap {
159192 Dir = instanceDir , Python = envPython
160193 } ;
161194 }
162195
163- public static String CreateVirtualEnv ( ShellConfig config , String instanceName , String [ ] ? pythonRequires = null ) {
196+ public static String CreateVirtualEnv ( ShellConfig config , String instanceName , String [ ] ? pythonRequires = null , Boolean echo = true ) {
197+ if ( echo ) Console . WriteLine ( "Creating virtualenv..." ) ;
164198 pythonRequires = pythonRequires ?? config . PythonRequires ;
165199
166200 String envDir = Path . Join ( Util . GetShellInstanceDir ( config , instanceName ) , "env" ) ;
167201 if ( instanceName . ToLower ( ) == "default" ) {
168202 config . DefaultPythonEnvPath = Util . GetVirtualEnv ( config , "default" ) ;
169203 }
170204
171- Process . Start ( config . DefaultPythonPath , new String [ ] { "-m" , "virtualenv" , envDir } ) . WaitForExit ( ) ;
205+ Util . RunSettingCmd ( config . DefaultPythonPath , new String [ ] { "-m" , "virtualenv" , envDir } ) ;
172206 String envPython = Util . GetVirtualEnv ( config , instanceName ) ;
173- Process . Start ( envPython , new String [ ] { "-m" , "pip" , "install" , "pip" , "--upgrade" } ) . WaitForExit ( ) ;
207+ Util . RunSettingCmd ( envPython , new String [ ] { "-m" , "pip" , "install" , "pip" , "--upgrade" } ) ;
174208 Util . InstallRequiresToEnv ( config , envPython , pythonRequires : pythonRequires ) ;
209+ if ( echo ) Console . WriteLine ( "Virtualenv created." ) ;
175210
176211 return envPython ;
177212 }
@@ -180,7 +215,7 @@ public static void DeleteShellInstance(ShellConfig config, String instanceName)
180215 Directory . Delete ( Util . GetShellInstanceDir ( config , instanceName ) , true ) ;
181216 }
182217
183- public static InstanceMap GetShellInstance ( ShellConfig config , String instanceName ) {
218+ public static InstanceMap GetShellInstance ( ShellConfig config , String instanceName , Boolean echo = true ) {
184219 String instanceDir = Util . GetShellInstanceDir ( config , instanceName ) ;
185220 if ( Directory . Exists ( instanceDir ) ) {
186221 return new InstanceMap {
@@ -189,7 +224,7 @@ public static InstanceMap GetShellInstance(ShellConfig config, String instanceNa
189224 } ;
190225 }
191226 else {
192- return Util . CreateShellInstance ( config , instanceName ) ;
227+ return Util . CreateShellInstance ( config , instanceName , echo : echo ) ;
193228 }
194229 }
195230
@@ -204,18 +239,20 @@ public static String GetVirtualEnv(ShellConfig config, String instanceName) {
204239 }
205240 }
206241
207- public static void InstallRequiresToEnv ( ShellConfig config , String envPython , String [ ] ? pythonRequires = null ) {
242+ public static void InstallRequiresToEnv ( ShellConfig config , String envPython , String [ ] ? pythonRequires = null , Boolean echo = true ) {
208243 pythonRequires = pythonRequires ?? config . PythonRequires ;
209244
245+ if ( echo ) Console . WriteLine ( "Installing requirements..." ) ;
210246 if ( pythonRequires == null && config . PythonRequireFile != null ) {
211- Process . Start ( envPython , new String [ ] { "-m" , "pip" , "install" , "-r" , config . PythonRequireFile } ) . WaitForExit ( ) ;
247+ Util . RunSettingCmd ( envPython , new String [ ] { "-m" , "pip" , "install" , "-r" , config . PythonRequireFile } ) ;
212248 }
213249 else if ( pythonRequires != null ) {
214250 String tempPythonRequireFile = Path . Join ( config . TempDir , DateTime . Now . ToString ( "yyyy.MM.dd.HH.mm.ss" ) ) ;
215- File . WriteAllText ( tempPythonRequireFile , String . Join ( "\n " , pythonRequires ) , System . Text . Encoding . UTF8 ) ;
216- Process . Start ( envPython , new String [ ] { "-m" , "pip" , "install" , "-r" , tempPythonRequireFile } ) . WaitForExit ( ) ;
251+ File . WriteAllText ( tempPythonRequireFile , String . Join ( "\n " , pythonRequires ) ) ;
252+ Util . RunSettingCmd ( envPython , new String [ ] { "-m" , "pip" , "install" , "-r" , tempPythonRequireFile } ) ;
217253 File . Delete ( tempPythonRequireFile ) ;
218254 }
255+ if ( echo ) Console . WriteLine ( "Requirements installed." ) ;
219256 }
220257
221258 public static void RemoveShellInstance ( ShellConfig config , String instanceName ) {
0 commit comments