Main.cs (3418B)
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 5 namespace waflauncher 6 { 7 class MainClass 8 { 9 public static System.Diagnostics.Process exec(string command,params string[] args) { 10 String argstring = String.Join(" ",args); 11 System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo(command,argstring); 12 startinfo.UseShellExecute = false; 13 System.Diagnostics.Process p; 14 try { 15 p = Process.Start(startinfo); 16 } catch (System.ComponentModel.Win32Exception){ 17 return null; 18 } 19 p.WaitForExit(); 20 return p; 21 } 22 23 public static int Main (string[] args) 24 { 25 //I run waf and if not successful we try on-the-fly install of python 26 if(!runWaf(args)){ 27 //but first we ask the user if it's okay to install software on their computer 28 if(mayInstall()){ 29 //I install python and try running waf yet another time 30 installPython(); 31 if(!runWaf(args)){ 32 //If it still fails something has gone horrible wrong 33 Console.WriteLine("Python not fully working"); 34 return 1; 35 } 36 } else { 37 Console.WriteLine("Not automatically installing Python"); 38 Console.WriteLine("Please download and install http://www.python.org/ftp/python/2.7.1/python-2.7.1.msi"); 39 Console.WriteLine("or if you have python installed make sure it is on %PATH%"); 40 Console.WriteLine("or run this command again and answer yes"); 41 } 42 } 43 return 0; 44 } 45 46 public static bool mayInstall() { 47 Console.Write("Download and install python [Y/n]? "); 48 ConsoleKeyInfo a = Console.ReadKey(); 49 Console.WriteLine(); 50 switch(a.KeyChar){ 51 case 'Y': 52 case 'y': 53 case '\n': 54 case '\r': 55 return true; 56 //If unsure default to not doing it 57 default: 58 return false; 59 } 60 } 61 62 public static String getwafDir(){ 63 //This changes the current directory to the place where the exe exists 64 System.Reflection.Assembly a = System.Reflection.Assembly.GetEntryAssembly(); 65 String path = System.IO.Path.GetDirectoryName(a.Location); 66 return path + System.IO.Path.DirectorySeparatorChar; 67 } 68 69 public static bool runWaf(string[] args){ 70 Process p = exec("python", getwafDir() + "waf", String.Join(" ",args)); 71 //If command could be execeuted return true 72 if (p != null) return true; 73 //If not try with the direct path to the default installation which is where installPython() will install it to 74 //This is done since the %PATH% variable might not be setup to include python 75 76 List<String> versions = new List<String>() { "27", "32", "26", "31", "25", "30" }; 77 foreach (String v in versions) { 78 p = exec("C:\\Python"+v+"\\python.exe", "waf", String.Join(" ",args)); 79 if (p != null) return true; 80 } 81 return false; 82 } 83 84 public static void installPython(){ 85 //Make a filename to download python to 86 String filename = System.IO.Path.GetTempPath() + Char.ToString(System.IO.Path.DirectorySeparatorChar) + "python-2.7.1.msi"; 87 88 System.Net.WebClient web = new System.Net.WebClient(); 89 Console.WriteLine ("Downloading python 2.7"); 90 web.DownloadFile("http://www.python.org/ftp/python/2.7.1/python-2.7.1.msi",filename); 91 Console.WriteLine ("python2.7 downloaded to " + filename); 92 93 Console.WriteLine ("Installing python"); 94 //filename must be quoted or else msiexec will fail 95 exec("msiexec","/qn","/i","\"" +filename + "\""); 96 Console.WriteLine ("Python is now installed"); 97 } 98 } 99 } 100