Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/InstallationManager.cs @ 3155

Last change on this file since 3155 was 3092, checked in by gkronber, 15 years ago

Fixed relevant warnings in the plugin infrastructure (didn't fix warnings about XML comments of members that will be removed soon). #915 (Remove warnings from HL 3.3 solution)

File size: 11.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.PluginInfrastructure.Manager;
27using System.IO;
28using System.ComponentModel;
29using System.Reflection;
30using ICSharpCode.SharpZipLib.Zip;
31using System.ServiceModel;
32
33namespace HeuristicLab.PluginInfrastructure.Advanced {
34  internal class InstallationManager {
35
36    internal event EventHandler<PluginInfrastructureCancelEventArgs> PreUpdatePlugin;
37    internal event EventHandler<PluginInfrastructureCancelEventArgs> PreRemovePlugin;
38    internal event EventHandler<PluginInfrastructureCancelEventArgs> PreInstallPlugin;
39
40    internal event EventHandler<PluginInfrastructureEventArgs> PluginUpdated;
41    internal event EventHandler<PluginInfrastructureEventArgs> PluginRemoved;
42    internal event EventHandler<PluginInfrastructureEventArgs> PluginInstalled;
43
44    private string pluginDir;
45    public InstallationManager(string pluginDir) {
46      this.pluginDir = pluginDir;
47    }
48
49    //public IEnumerable<string> Show(IEnumerable<string> pluginNames) {
50    //  foreach (PluginDescription desc in GetPluginDescriptions(pluginNames)) {
51    //    yield return GetInformation(desc);
52    //  }
53    //}
54
55    //internal string GetInformation(string pluginName) {
56    //  return GetInformation(GetPluginDescription(pluginName));
57    //}
58
59    //private string GetInformation(PluginDescription desc) {
60    //  StringBuilder builder = new StringBuilder();
61    //  builder.Append("Name: ").AppendLine(desc.Name);
62    //  builder.Append("Version: ").AppendLine(desc.Version.ToString());
63    //  builder.AppendLine("Description:").AppendLine(desc.Description);
64    //  if (!string.IsNullOrEmpty(desc.ContactName)) {
65    //    builder.Append("Contact: ").Append(desc.ContactName).Append(", ").AppendLine(desc.ContactEmail);
66    //  }
67    //  builder.AppendLine("This plugin is " + desc.PluginState.ToString().ToLowerInvariant() + ".");
68    //  builder.AppendLine("Files: ");
69    //  foreach (var file in desc.Files) {
70    //    builder.AppendLine(file.Type + " " + file.Name);
71    //  }
72    //  builder.AppendLine().AppendLine("Directly depends on:");
73    //  if (desc.Dependencies.Count() == 0) builder.AppendLine("None");
74    //  foreach (var dependency in desc.Dependencies) {
75    //    builder.AppendLine(dependency.Name + " " + dependency.Version);
76    //  }
77    //  builder.AppendLine().AppendFormat("Plugins directly dependent on {0}:", desc.Name).AppendLine();
78    //  var dependents = from x in pluginManager.Plugins
79    //                   where x.Dependencies.Contains(desc)
80    //                   select x;
81    //  if (dependents.Count() == 0) builder.AppendLine("None");
82    //  foreach (var dependent in dependents) {
83    //    builder.AppendLine(dependent.Name + " " + dependent.Version);
84    //  }
85    //  builder.AppendLine();
86    //  if (desc.PluginState == PluginState.Disabled) {
87    //    builder.AppendLine(DetermineProblem(desc));
88    //  }
89
90    //  return builder.ToString();
91    //}
92
93    //private static string DetermineProblem(PluginDescription desc) {
94    //  // either any file is missing
95    //  StringBuilder builder = new StringBuilder();
96    //  builder.AppendLine("Problem report:");
97    //  builder.AppendLine(desc.LoadingErrorInformation);
98    //  return builder.ToString();
99    //}
100
101    //private PluginDescription GetPluginDescription(string pluginName) {
102    //  var exactMatch = from pluginDesc in pluginManager.Plugins
103    //                   where string.Equals(pluginName, pluginDesc.Name, StringComparison.InvariantCultureIgnoreCase)
104    //                   select pluginDesc;
105    //  var inexactMatch = from pluginDesc in pluginManager.Plugins
106    //                     where MatchPluginNameInexact(pluginName, pluginDesc.Name)
107    //                     select pluginDesc;
108    //  return exactMatch.Count() > 0 ? exactMatch.Single() : inexactMatch.First();
109    //}
110
111    //private IEnumerable<PluginDescription> GetPluginDescriptions(IEnumerable<string> pluginNames) {
112    //  return from pluginName in pluginNames
113    //         select GetPluginDescription(pluginName);
114    //}
115
116    //private static bool MatchPluginNameInexact(string similarName, string actualName) {
117    //  return
118    //    // Core-3.2 == HeuristicLab.Core-3.2
119    //    actualName.Equals("HeuristicLab." + similarName, StringComparison.InvariantCultureIgnoreCase) ||
120    //    // HeuristicLab.Core == HeuristicLab.Core-3.2 (this should be save because we checked for exact matches first)
121    //    (Math.Abs(actualName.Length - similarName.Length) <= 4 && actualName.StartsWith(similarName, StringComparison.InvariantCultureIgnoreCase)) ||
122    //    // Core == HeuristicLab.Core-3.2
123    //    (Math.Abs(actualName.Length - similarName.Length) <= 17 && actualName.StartsWith("HeuristicLab." + similarName, StringComparison.InvariantCultureIgnoreCase));
124    //}
125
126
127    /// <summary>
128    /// Retrieves a list of plugins available at the remote server
129    /// </summary>
130    /// <returns></returns>
131    public IEnumerable<IPluginDescription> GetRemotePluginList() {
132      var client = DeploymentService.UpdateClientFactory.CreateClient();
133      try {
134        List<IPluginDescription> plugins = new List<IPluginDescription>(client.GetPlugins());
135        client.Close();
136        return plugins;
137      }
138      catch (FaultException) {
139        client.Abort();
140        return new IPluginDescription[] { };
141      }
142    }
143
144    /// <summary>
145    /// Retrieves the list of products available at the remote server
146    /// </summary>
147    /// <returns></returns>
148    public IEnumerable<DeploymentService.ProductDescription> GetRemoteProductList() {
149      var client = DeploymentService.UpdateClientFactory.CreateClient();
150      try {
151        List<DeploymentService.ProductDescription> products = new List<DeploymentService.ProductDescription>(client.GetProducts());
152        client.Close();
153        return products;
154      }
155      catch (FaultException) {
156        client.Abort();
157        return new DeploymentService.ProductDescription[] { };
158      }
159    }
160
161    /// <summary>
162    ///  Installs plugins from remote server
163    /// </summary>
164    /// <param name="plugins"></param>
165    public void Install(IEnumerable<IPluginDescription> plugins) {
166      var args = new PluginInfrastructureCancelEventArgs(plugins);
167      OnPreInstall(args);
168      if (!args.Cancel) {
169        var client = DeploymentService.UpdateClientFactory.CreateClient();
170        try {
171          foreach (DeploymentService.PluginDescription plugin in plugins) {
172            byte[] zippedPackage = client.GetPlugin(plugin);
173            Unpack(zippedPackage);
174            OnInstalled(new PluginInfrastructureEventArgs(plugin));
175          }
176          client.Close();
177        }
178        catch (FaultException) {
179          client.Abort();
180        }
181      }
182    }
183
184    /// <summary>
185    /// Updates plugins from remote server
186    /// </summary>
187    /// <param name="plugins"></param>
188    public void Update(IEnumerable<IPluginDescription> plugins) {
189      PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
190      OnPreUpdate(args);
191      if (!args.Cancel) {
192        var client = DeploymentService.UpdateClientFactory.CreateClient();
193        try {
194          foreach (DeploymentService.PluginDescription plugin in plugins) {
195            byte[] zippedPackage = client.GetPlugin(plugin);
196            Unpack(zippedPackage);
197            OnUpdated(new PluginInfrastructureEventArgs(plugin));
198          }
199          client.Close();
200        }
201        catch (FaultException) {
202          client.Abort();
203        }
204      }
205    }
206
207    /// <summary>
208    /// Deletes all plugin files from local installation
209    /// </summary>
210    /// <param name="plugins"></param>
211    public void Remove(IEnumerable<IPluginDescription> plugins) {
212      var fileNames = from pluginToDelete in plugins
213                      from file in pluginToDelete.Files
214                      select Path.Combine(pluginDir, file.Name);
215      var args = new PluginInfrastructureCancelEventArgs(plugins);
216      OnPreDelete(args);
217      if (!args.Cancel) {
218        foreach (string fileName in fileNames) {
219          File.Delete(fileName);
220          OnDeleted(new PluginInfrastructureEventArgs(fileName));
221        }
222      }
223    }
224
225    private void Unpack(byte[] zippedPackage) {
226      using (ZipInputStream s = new ZipInputStream(new MemoryStream(zippedPackage))) {
227        ZipEntry theEntry;
228        string tmpEntry = String.Empty;
229        while ((theEntry = s.GetNextEntry()) != null) {
230          string directoryName = pluginDir;
231          string fileName = Path.GetFileName(theEntry.Name);
232          // create directory
233          if (directoryName != "") {
234            Directory.CreateDirectory(directoryName);
235          }
236          if (fileName != String.Empty) {
237            string fullPath = Path.Combine(directoryName, fileName);
238            string fullDirPath = Path.GetDirectoryName(fullPath);
239            if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
240            FileStream streamWriter = File.Create(fullPath);
241            int size = 2048;
242            byte[] data = new byte[2048];
243            while (true) {
244              size = s.Read(data, 0, data.Length);
245              if (size > 0) {
246                streamWriter.Write(data, 0, size);
247              } else {
248                break;
249              }
250            }
251            streamWriter.Close();
252          }
253        }
254      }
255    }
256
257    private void OnPreUpdate(PluginInfrastructureCancelEventArgs args) {
258      if (PreUpdatePlugin != null) PreUpdatePlugin(this, args);
259    }
260
261    private void OnUpdated(PluginInfrastructureEventArgs args) {
262      if (PluginUpdated != null) PluginUpdated(this, args);
263    }
264
265    private void OnPreDelete(PluginInfrastructureCancelEventArgs args) {
266      if (PreRemovePlugin != null) PreRemovePlugin(this, args);
267    }
268
269    private void OnDeleted(PluginInfrastructureEventArgs args) {
270      if (PluginRemoved != null) PluginRemoved(this, args);
271    }
272
273    private void OnPreInstall(PluginInfrastructureCancelEventArgs args) {
274      if (PreInstallPlugin != null) PreInstallPlugin(this, args);
275    }
276
277    private void OnInstalled(PluginInfrastructureEventArgs args) {
278      if (PluginInstalled != null) PluginInstalled(this, args);
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.