/// <summary> /// Download and execute a file from a url. /// </summary> /// <param name="environmentFolder">The folder where the file should be downloaded to</param> /// <param name="url">The URL to get the file from</param> /// <param name="commandLineArguments">Any command line arguments to use when running file</param> /// <param name="downloadedFileName">The name of the file to download</param> private static void DownloadAndExecuteFile(string environmentFolder, string url, string commandLineArguments, string downloadedFileName) { Directory.CreateDirectory(Path.GetDirectoryName(downloadedFileName)); if (url != null) { WebClient myWebClient = new WebClient(); byte[] bytes = myWebClient.DownloadData(url); using (FileStream writer = File.Create(downloadedFileName)) writer.Write(bytes, 0, bytes.Length); } if (Path.GetExtension(downloadedFileName) == ".exe") { ProcessStartInfo startInfo = new ProcessStartInfo { FileName = downloadedFileName, Arguments = commandLineArguments, WorkingDirectory = environmentFolder, UseShellExecute = true, CreateNoWindow = true }; Process p = Process.Start(startInfo); p.WaitForExit(); } else { ZipUtilities.UnZipFiles(downloadedFileName, environmentFolder, null); } File.Delete(downloadedFileName); }
/// <summary> /// Creates a instance of a yield prophet spec from a zip file. /// </summary> /// <param name="zipFileName">The name of the .zip file.</param> /// <returns>The newly create yieldprophet object.</returns> private static YieldProphet YieldProphetFromZip(string zipFileName) { YieldProphet yieldProphet; string tempFolder = Path.GetTempFileName(); File.Delete(tempFolder); Directory.CreateDirectory(tempFolder); FileStream reader = File.OpenRead(zipFileName); try { string[] fileNames = ZipUtilities.UnZipFiles(reader, tempFolder, null); string fileName = Path.Combine(tempFolder, "YieldProphet.xml"); if (!File.Exists(fileName)) { // Look for first XML file. foreach (string file in fileNames) { if (file.Contains(".xml")) { fileName = file; break; } } } yieldProphet = YieldProphetUtility.YieldProphetFromFile(fileName); yieldProphet.ReportName = Path.GetFileNameWithoutExtension(fileName); } finally { reader.Close(); } Directory.Delete(tempFolder, true); return(yieldProphet); }
/// <summary> /// Ensure the correct runtime is ready to go. Return a path to the correct folder /// that contains the APSIM executables. /// </summary> /// <param name="environment"></param> /// <returns></returns> public static string SetupRunTimeEnvironment(RuntimeEnvironment environment) { string binFolder = null; string environmentFolder = null; string url = null; string commandLineArguments = null; if (environment.APSIMRevision != null) { environmentFolder = environment.APSIMRevision; if (environment.RuntimePackage != null) { environmentFolder += "-"; environmentFolder += environment.RuntimePackage; } url = @"http://bob.apsim.info/files/" + environment.APSIMRevision + ".binaries.WINDOWS.INTEL.exe"; binFolder = Path.Combine(environmentFolder, "Temp", "Model"); } if (environment.APSIMxBuildNumber > 0) { url = WebUtilities.CallRESTService <string>("http://www.apsim.info/APSIM.Builds.Service/Builds.svc/GetURLOfVersionForIssue?issueid=" + environment.APSIMxBuildNumber); environmentFolder = "ApsimX-" + environment.APSIMxBuildNumber; if (environment.RuntimePackage != null) { environmentFolder += "-"; environmentFolder += environment.RuntimePackage; } commandLineArguments = "/SILENT /NOICONS /DIR=\".\""; binFolder = Path.Combine(environmentFolder, "Bin"); } else if (environment.AusfarmRevision != null) { environmentFolder = environment.AusfarmRevision; if (environment.RuntimePackage != null) { environmentFolder += "-"; environmentFolder += environment.RuntimePackage; } string packageFileName = Path.Combine("RuntimePackages", environment.AusfarmRevision + ".zip"); ZipUtilities.UnZipFiles(packageFileName, environmentFolder, null); binFolder = Path.Combine(environmentFolder, "Ausfarm"); } environmentFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), environmentFolder); if (!Directory.Exists(environmentFolder)) { string downloadedFileName = Path.Combine(environmentFolder, "Temp.exe"); // Download the file DownloadAndExecuteFile(environmentFolder, url, commandLineArguments, downloadedFileName); // Copy in the extra runtime packages. if (environment.RuntimePackage != null) { string packageFileName = Path.Combine("RuntimePackages", environment.RuntimePackage + ".zip"); ZipUtilities.UnZipFiles(packageFileName, environmentFolder, null); } } return(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), binFolder)); }