Requirements.cs 17.9 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
using System;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
using IWshRuntimeLibrary;
using System.Web.Script.Serialization;

namespace AtualizadorVLibras
{
    class Requirements
    {
        //false - extrair, true - ignorar
        //ex:
        //{"Major":5,"Minor":1,"Build":1,"Revision":20}
        public static bool verificarVersao(string atual, string extraida){
            if(System.IO.File.Exists(atual)){
                if(!System.IO.File.Exists(extraida)){
                    return false;
                }
                JavaScriptSerializer serializer = new JavaScriptSerializer();

                StreamReader sr = new StreamReader(atual);
                string versaoAtual = sr.ReadToEnd();
                dynamic arrayAtual = serializer.DeserializeObject(versaoAtual);

                sr = new StreamReader(extraida);
                string versaoExtraida = sr.ReadToEnd();
                dynamic arrayExtraida = serializer.DeserializeObject(versaoExtraida);

                int atualMaj = Convert.ToInt32(arrayAtual["Major"]);
                int atualMin = Convert.ToInt32(arrayAtual["Minor"]);
                int atualBui = Convert.ToInt32(arrayAtual["Build"]);
                int atualRev = Convert.ToInt32(arrayAtual["Revision"]);

                int ExtraidaMaj = Convert.ToInt32(arrayExtraida["Major"]);
                int ExtraidaMin = Convert.ToInt32(arrayExtraida["Minor"]);
                int ExtraidaBui = Convert.ToInt32(arrayExtraida["Build"]);
                int ExtraidaRev = Convert.ToInt32(arrayExtraida["Revision"]);

				//É importante que o operador utilizado seja '!=', no lugar de '>', pq em aplicações clickonce é possível reverter versão!
                if(atualMaj != ExtraidaMaj){
                    //Console.WriteLine("Major é mais recente em version: {0} {1}", atualMaj, ExtraidaMaj);
                    return false;
                }
				if (atualMin != ExtraidaMin){
                    //Console.WriteLine("Minor é mais recente em version: {0} {1}", atualMin, ExtraidaMin);
                    return false;
                }
				if (atualBui != ExtraidaBui){
                    //Console.WriteLine("Build é mais recente em version: {0} {1}", atualBui, ExtraidaBui);
                    return false;
                }
				if (atualRev != ExtraidaRev){
                    //Console.WriteLine("Revision é mais recente em version: {0} {1}", atualRev, ExtraidaRev);
                    return false;
                }
            }
            return true;
        }
    	public static bool IsInstalled(RegistryKey[] regs){
    		foreach(RegistryKey reg in regs){
    			if(reg != null){
    				return true;
    			}
    		}
    		return false;
    	}

		public static bool IsInstalled(RegistryKey reg){
    		if(reg != null){
    			return true;
    		}else{
	    		return false;
    		}
    	}

    	public static void Uninstall(RegistryKey[] regs){
    		foreach(RegistryKey reg in regs){
    			if(IsInstalled(reg)){
    				try{
	    				Process uninsProc = new Process();
			            ProcessStartInfo uninsProcInfo = new ProcessStartInfo();
			            uninsProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
			            uninsProcInfo.FileName = "cmd.exe";
			            string comandoUnins;
			            comandoUnins = "/C" + reg.GetValue("QuietUninstallString").ToString().Replace("\"", "");
			            uninsProcInfo.Arguments = comandoUnins;
			            uninsProc.StartInfo = uninsProcInfo;
			            uninsProc.Start();
			            //uninsProc.WaitForExit();
    				}catch(Exception){
    					//NOTHING
    				}
    			}
    		}
    	}

    	//CRIA ATALHO
    	public static void criaAtalho(string path){//string pathPython2){
            object shDesktop = (object)"Desktop";
            WshShell shell = new WshShell();
            string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\VLibras.lnk";
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
            shortcut.Description = "VLibras";
            string vlibrasPath = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, @"VLibras\");
            shortcut.IconLocation = Path.Combine(vlibrasPath, @"icon_vlibras.ico");
            //shortcut.Hotkey = "Ctrl+Shift+N";
            shortcut.TargetPath = Path.Combine(vlibrasPath, @"Player\vlibrasPlayer.exe");
			shortcut.WorkingDirectory = Path.Combine(vlibrasPath, @"Player\");
			shortcut.Save();

//		    //string driveAtual = @" % HOMEPATH%\..\..";
//		    //string pathPython2 = driveAtual+@"Python27\python.exe";
//		    //string pathPython2 = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Machine);
//
//            //string pathPython2 = GetRegistroPythonPath();
//		    int idx = pathPython2.IndexOf(@";");
//		    //pathPython2 = pathPython2.Remove(idx, pathPython2.Length - idx);
//		    //pathPython2 = "\""+pathPython2 + "\\python.exe\"";
//
//		    //Console.WriteLine("VERIFICAÇÃO DA LINHA criaAtalho: "+pathPython2);
//		    //Console.ReadKey();
//
//            ProcessStartInfo procInfo = new ProcessStartInfo();
//            procInfo.FileName = pathPython2;//"cmd.exe";
//		    //procInfo.Arguments = "/C py -2 "+Directory.GetCurrentDirectory()+@"\criaLnk.py";
//		    //procInfo.Arguments = "/C "+pathPython2+" -m "+Directory.GetCurrentDirectory()+@"\criaLnk.py";
//		    //procInfo.Arguments = "/C "+pathPython2+" \""+Directory.GetCurrentDirectory()+"\\criaLnk.py\"";
//		    procInfo.Arguments = "\""+Directory.GetCurrentDirectory()+@"\criaLnk.py";
//		    procInfo.UseShellExecute = false;
//		    procInfo.RedirectStandardOutput = true;
//		    procInfo.WindowStyle = ProcessWindowStyle.Hidden;
//
//		    //Process process = new Process();
//		    //process.StartInfo = procInfo;
//		    //process.Start();
//	        using(Process process = Process.Start(procInfo)){
//	            using(StreamReader reader = process.StandardOutput){
//	                string result = reader.ReadToEnd();
//	                Console.Write(result);
//	            }
//	            process.WaitForExit();
//	        }
//	        //Console.WriteLine("pausa: {0}",procInfo.Arguments);
//	        //Console.ReadKey();
    	}

    	//NÃO VAI SER MAIS UTILIZADO DEVIDO AO PYTHON PORTÁTIL
    	public static string GetRegistroPythonPath(){
    		var lm64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            var lm32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
        	var cr64 = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64);
            var cr32 = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32);
        	var cu64 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
            var cu32 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);

    		RegistryKey pythonReg = null;
        	if(lm64.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath") != null){
	        	pythonReg = lm64.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath");
        	}
        	if(lm32.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath") != null){
        		pythonReg = lm32.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath");
        	}
        	if(cu64.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath") != null){
        		pythonReg = cu64.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath");
        	}
        	if(cu32.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath") != null){
        		pythonReg = cu32.OpenSubKey(@"SOFTWARE\Python\PythonCore\2.7\InstallPath");
        	}
        	if(lm64.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath") != null){
        		pythonReg = lm64.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath");
        	}
        	if(lm32.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath") != null){
        		pythonReg = lm32.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath");
        	}
        	if(cu64.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath") != null){
        		pythonReg = cu64.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath");
        	}
        	if(cu32.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath") != null){
        		pythonReg = cu32.OpenSubKey(@"SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath");
        	}
        	if(pythonReg != null){
        		//CONIFGURAÇÃO DA VARIÁVEL PYTHONPATH
        		string python = pythonReg.GetValue(null).ToString();
        		//python = python.Remove(python.Length - 1).ToString();
	        	return "\""+python+@"python.exe"+"\"";
        	}else{
        		return "";
        	}
    	}

    	//NÃO VAI SER MAIS UTILIZADO DEVIDO AO PYTHON PORTÁTIL
    	//INSTALA, SE NECESSÁRIO, OS MÓDULOS DO PYTHON
    	public static void InstallWheels(bool force){
            /*var cr64 = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64);
            var cr32 = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32);
    		RegistryKey pythonShell = cr64.OpenSubKey(@"Python.File\Shell\open\command");
            if(pythonShell == null){
                pythonShell = cr32.OpenSubKey(@"Python.File\Shell\open\command");
            }
            path do python na máquina
            string pythonCMD = pythonShell.GetValue(null).ToString();
            pythonCMD = pythonCMD.Remove(pythonCMD.Length - 9).Replace("\"", "");
		    procInfo.FileName = pythonCMD;*/

            //INFORMAÇÕES SOBRE O PROCESSO QUE VAI SER INICIADO
            //string driveAtual = @"%HOMEPATH%\..\..";
		    //string pathPython2 = driveAtual+@"Python27\python.exe";

		    //string pathPython2 = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Machine);
		    string pathPython2 = GetRegistroPythonPath();
		    int idx = pathPython2.IndexOf(@";");
		    //pathPython2 = pathPython2.Remove(idx, pathPython2.Length - idx);
		    //pathPython2 = "\""+pathPython2 + "\\python.exe\"";

		    //Console.WriteLine("VERIFICAÇÃO DA LINHA insWhls: "+pathPython2);
		    //Console.ReadKey();
            //ATUALIZAÇÃO DO PIP
            ProcessStartInfo procInfo = new ProcessStartInfo();
            //procInfo.FileName = "py";
            //procInfo.Arguments = "-2 -m pip install --upgrade pip";
            procInfo.FileName = pathPython2;
            procInfo.Arguments = "-m pip install --upgrade pip";
		    procInfo.UseShellExecute = false;
		    procInfo.RedirectStandardOutput = true;
		    procInfo.WindowStyle = ProcessWindowStyle.Hidden;

		    //Process process = new Process();
		    //process.StartInfo = procInfo;
		    //process.Start();
		    //process.WaitForExit();
            string result = "";
		    using(Process process = Process.Start(procInfo)){
	            using(StreamReader reader = process.StandardOutput){
                    while(!reader.EndOfStream){
    	                result = reader.ReadLine() + "\n";
    	                Console.Write(result);
                    }
	            }
	            process.WaitForExit();
	        }
		    Console.WriteLine("Pip atualizado.");
		    Console.WriteLine("Instalando os módulos. Isso pode levar alguns minutos. Por favor, aguarde.");
		    //INSTALAÇÃO DOS MÓDULOS
            if(force){
            	//instalação forçada
            	//procInfo.Arguments = "-2 "+Directory.GetCurrentDirectory()+@"\checkwheels.py --force";
            	//procInfo.Arguments = "-m "+Directory.GetCurrentDirectory()+@"\checkwheels.py --force";
            	procInfo.Arguments = "\""+Directory.GetCurrentDirectory()+@"\checkwheels.py"+"\""+" --force";
           	}else{
			    //procInfo.Arguments = "-2 "+Directory.GetCurrentDirectory()+@"\checkwheels.py";
			    //procInfo.Arguments = "-m "+Directory.GetCurrentDirectory()+@"\checkwheels.py";
			    procInfo.Arguments = "\""+Directory.GetCurrentDirectory()+@"\checkwheels.py"+"\"";
           	}
           	//Console.WriteLine("VERIFICAÇÃO DA LINHA insWhls2: "+procInfo.Arguments);
		    //Console.ReadKey();
            //process = new Process();
		    //process.StartInfo = procInfo;
		    //process.Start();
		    //process.WaitForExit();
            result = "";
		    using(Process process = Process.Start(procInfo)){
	            using(StreamReader reader = process.StandardOutput){
	                while(!reader.EndOfStream){
                        result = reader.ReadLine() + "\n";
                        Console.Write(result);
                    }
	            }
	            process.WaitForExit();
	        }
		}


		//SE FOR USAR BOTAR PYTHONPATH2 ENTRE ASPAS!!!!!!!!!!
		/*public static bool ChecarVersaoPython(){
			string driveAtual = @"%HOMEPATH%\..\..";
		    string pathPython2 = driveAtual+@"Python27\python.exe";

			//string comandoIns = "/C py -2 --version";
			string comandoIns = @"/C "+"\""pathPython2+"\""+" --version";
			//Process e Process Information
            ProcessStartInfo insProcInfo = new ProcessStartInfo();
            Process insProc = new Process();

            insProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
            insProcInfo.FileName = "cmd.exe";
            insProcInfo.Arguments = comandoIns;
            insProcInfo.UseShellExecute = false;
            insProcInfo.CreateNoWindow = true;
            insProcInfo.RedirectStandardError = true;
            //insProcInfo.Verb = "runas"; //ADM

            insProc.StartInfo = insProcInfo;
            insProc.Start();
            string output = insProc.StandardError.ReadToEnd();
            insProc.WaitForExit();
            if(output == null || output == ""){
            	return false;
            }
            output = output.Remove(output.Length-2);
            string version = output.Replace("Python ", "");
            string majorMinorVersion = version.Remove(3);
            if(majorMinorVersion.Equals("2.7")){
            	string patchVersion = version.Replace("2.7.","");
            	int x = -1;
            	int.TryParse(patchVersion, out x);
            	if(x >= 9){
            		return true;
            	}
            }
            return false;
        }*/

    	public static void Install(string pathInstaller64, string pathInstaller86){
    		string pathInstaller;
    		if(Environment.Is64BitOperatingSystem){
                pathInstaller = pathInstaller64;
            }else{
            	pathInstaller = pathInstaller86;
			}
			if(pathInstaller == ""){
				return;
			}
			//string comandoIns = "/C" + pathInstaller + " -s -v -qn";
			string comandoIns = @"msiexec /i "+"\""+pathInstaller+"\""+@" /quiet /qn /norestart";
			//Process e Process Information
            ProcessStartInfo insProcInfo = new ProcessStartInfo();
            Process insProc = new Process();

            insProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
            insProcInfo.FileName = "cmd.exe";
            insProcInfo.Arguments = comandoIns;
            //insProcInfo.Verb = "runas"; //ADM

            insProc.StartInfo = insProcInfo;
            insProc.Start();
            //insProc.WaitForExit();
    	}

    	public static void Install(string pathInstaller64, string pathInstaller86, string args){
    		string pathInstaller;
    		if(Environment.Is64BitOperatingSystem){
                pathInstaller = pathInstaller64;
            }else{
            	pathInstaller = pathInstaller86;
			}
			if(pathInstaller == ""){
				return;
			}
			//string comandoIns = "/C" + pathInstaller + " -s -v -qn";
			string comandoIns = @"msiexec /i "+"\""+pathInstaller+"\" "+args;
			//Process e Process Information
            ProcessStartInfo insProcInfo = new ProcessStartInfo();
            Process insProc = new Process();

            insProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
            insProcInfo.FileName = "cmd.exe";
            insProcInfo.Arguments = comandoIns;
            //insProcInfo.Verb = "runas"; //ADM

            insProc.StartInfo = insProcInfo;
            insProc.Start();
            //insProc.WaitForExit();
    	}

    	public static void InstallAndWait(string pathInstaller64, string pathInstaller86){
    		string pathInstaller;
    		if(Environment.Is64BitOperatingSystem){
                pathInstaller = pathInstaller64;
            }else{
            	pathInstaller = pathInstaller86;
			}
			if(pathInstaller == ""){
				return;
			}
			//string comandoIns = "/C" + pathInstaller + " -s -v -qn";
			string comandoIns = @"/i "+"\""+pathInstaller+"\"";
			//Process e Process Information
            ProcessStartInfo insProcInfo = new ProcessStartInfo();
            //insProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
            Process insProc = new Process();

            insProcInfo.FileName = "msiexec";
            insProcInfo.Arguments = comandoIns;
            //insProcInfo.Verb = "runas"; //ADM
            //Console.WriteLine("VERIFICAÇÃO DE LINHA insWait: "+ insProcInfo.Arguments);
            //Console.ReadKey();
            insProc.StartInfo = insProcInfo;
            insProc.Start();
            insProc.WaitForExit();
    	}

    	//qn - instalação sem interface, passive instalação passiva
    	public static void InstallAndWait(string pathInstaller64, string pathInstaller86, string args){
    		string pathInstaller;
    		if(Environment.Is64BitOperatingSystem){
                pathInstaller = pathInstaller64;
            }else{
            	pathInstaller = pathInstaller86;
			}
			if(pathInstaller == ""){
				return;
			}
			//string comandoIns = "/C" + pathInstaller + " -s -v -qn";
			string comandoIns = @"/i "+"\""+pathInstaller+"\" "+args;
			//Process e Process Information
            ProcessStartInfo insProcInfo = new ProcessStartInfo();
            //insProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
            Process insProc = new Process();

            insProcInfo.FileName = "msiexec";
            insProcInfo.Arguments = comandoIns;
            //insProcInfo.Verb = "runas"; //ADM

            insProc.StartInfo = insProcInfo;
            insProc.Start();
            insProc.WaitForExit();
    	}

    }
}