Main.cs 3.3 KB

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