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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
27 | using System.IO;
|
---|
28 | using System.ComponentModel;
|
---|
29 | using System.Reflection;
|
---|
30 | using ICSharpCode.SharpZipLib.Zip;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.PluginInfrastructure.Advanced {
|
---|
33 | internal class InstallationManager {
|
---|
34 |
|
---|
35 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreUpdatePlugin;
|
---|
36 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreRemovePlugin;
|
---|
37 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreInstallPlugin;
|
---|
38 |
|
---|
39 | internal event EventHandler<PluginInfrastructureEventArgs> PluginUpdated;
|
---|
40 | internal event EventHandler<PluginInfrastructureEventArgs> PluginRemoved;
|
---|
41 | internal event EventHandler<PluginInfrastructureEventArgs> PluginInstalled;
|
---|
42 |
|
---|
43 | private string pluginDir;
|
---|
44 | public InstallationManager(string pluginDir) {
|
---|
45 | this.pluginDir = pluginDir;
|
---|
46 | }
|
---|
47 |
|
---|
48 | //public IEnumerable<string> Show(IEnumerable<string> pluginNames) {
|
---|
49 | // foreach (PluginDescription desc in GetPluginDescriptions(pluginNames)) {
|
---|
50 | // yield return GetInformation(desc);
|
---|
51 | // }
|
---|
52 | //}
|
---|
53 |
|
---|
54 | //internal string GetInformation(string pluginName) {
|
---|
55 | // return GetInformation(GetPluginDescription(pluginName));
|
---|
56 | //}
|
---|
57 |
|
---|
58 | //private string GetInformation(PluginDescription desc) {
|
---|
59 | // StringBuilder builder = new StringBuilder();
|
---|
60 | // builder.Append("Name: ").AppendLine(desc.Name);
|
---|
61 | // builder.Append("Version: ").AppendLine(desc.Version.ToString());
|
---|
62 | // builder.AppendLine("Description:").AppendLine(desc.Description);
|
---|
63 | // if (!string.IsNullOrEmpty(desc.ContactName)) {
|
---|
64 | // builder.Append("Contact: ").Append(desc.ContactName).Append(", ").AppendLine(desc.ContactEmail);
|
---|
65 | // }
|
---|
66 | // builder.AppendLine("This plugin is " + desc.PluginState.ToString().ToLowerInvariant() + ".");
|
---|
67 | // builder.AppendLine("Files: ");
|
---|
68 | // foreach (var file in desc.Files) {
|
---|
69 | // builder.AppendLine(file.Type + " " + file.Name);
|
---|
70 | // }
|
---|
71 | // builder.AppendLine().AppendLine("Directly depends on:");
|
---|
72 | // if (desc.Dependencies.Count() == 0) builder.AppendLine("None");
|
---|
73 | // foreach (var dependency in desc.Dependencies) {
|
---|
74 | // builder.AppendLine(dependency.Name + " " + dependency.Version);
|
---|
75 | // }
|
---|
76 | // builder.AppendLine().AppendFormat("Plugins directly dependent on {0}:", desc.Name).AppendLine();
|
---|
77 | // var dependents = from x in pluginManager.Plugins
|
---|
78 | // where x.Dependencies.Contains(desc)
|
---|
79 | // select x;
|
---|
80 | // if (dependents.Count() == 0) builder.AppendLine("None");
|
---|
81 | // foreach (var dependent in dependents) {
|
---|
82 | // builder.AppendLine(dependent.Name + " " + dependent.Version);
|
---|
83 | // }
|
---|
84 | // builder.AppendLine();
|
---|
85 | // if (desc.PluginState == PluginState.Disabled) {
|
---|
86 | // builder.AppendLine(DetermineProblem(desc));
|
---|
87 | // }
|
---|
88 |
|
---|
89 | // return builder.ToString();
|
---|
90 | //}
|
---|
91 |
|
---|
92 | //private static string DetermineProblem(PluginDescription desc) {
|
---|
93 | // // either any file is missing
|
---|
94 | // StringBuilder builder = new StringBuilder();
|
---|
95 | // builder.AppendLine("Problem report:");
|
---|
96 | // builder.AppendLine(desc.LoadingErrorInformation);
|
---|
97 | // return builder.ToString();
|
---|
98 | //}
|
---|
99 |
|
---|
100 | //private PluginDescription GetPluginDescription(string pluginName) {
|
---|
101 | // var exactMatch = from pluginDesc in pluginManager.Plugins
|
---|
102 | // where string.Equals(pluginName, pluginDesc.Name, StringComparison.InvariantCultureIgnoreCase)
|
---|
103 | // select pluginDesc;
|
---|
104 | // var inexactMatch = from pluginDesc in pluginManager.Plugins
|
---|
105 | // where MatchPluginNameInexact(pluginName, pluginDesc.Name)
|
---|
106 | // select pluginDesc;
|
---|
107 | // return exactMatch.Count() > 0 ? exactMatch.Single() : inexactMatch.First();
|
---|
108 | //}
|
---|
109 |
|
---|
110 | //private IEnumerable<PluginDescription> GetPluginDescriptions(IEnumerable<string> pluginNames) {
|
---|
111 | // return from pluginName in pluginNames
|
---|
112 | // select GetPluginDescription(pluginName);
|
---|
113 | //}
|
---|
114 |
|
---|
115 | //private static bool MatchPluginNameInexact(string similarName, string actualName) {
|
---|
116 | // return
|
---|
117 | // // Core-3.2 == HeuristicLab.Core-3.2
|
---|
118 | // actualName.Equals("HeuristicLab." + similarName, StringComparison.InvariantCultureIgnoreCase) ||
|
---|
119 | // // HeuristicLab.Core == HeuristicLab.Core-3.2 (this should be save because we checked for exact matches first)
|
---|
120 | // (Math.Abs(actualName.Length - similarName.Length) <= 4 && actualName.StartsWith(similarName, StringComparison.InvariantCultureIgnoreCase)) ||
|
---|
121 | // // Core == HeuristicLab.Core-3.2
|
---|
122 | // (Math.Abs(actualName.Length - similarName.Length) <= 17 && actualName.StartsWith("HeuristicLab." + similarName, StringComparison.InvariantCultureIgnoreCase));
|
---|
123 | //}
|
---|
124 |
|
---|
125 |
|
---|
126 | /// <summary>
|
---|
127 | /// Retrieves a list of plugins available at the remote server
|
---|
128 | /// </summary>
|
---|
129 | /// <param name="connectionString"></param>
|
---|
130 | /// <returns></returns>
|
---|
131 | public IEnumerable<IPluginDescription> GetRemotePluginList(string connectionString) {
|
---|
132 | using (var client = new DeploymentService.UpdateClient()) {
|
---|
133 | return client.GetPlugins();
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | /// <summary>
|
---|
138 | /// Retrieves the list of products available at the remote server
|
---|
139 | /// </summary>
|
---|
140 | /// <param name="connectionString"></param>
|
---|
141 | /// <returns></returns>
|
---|
142 | public IEnumerable<DeploymentService.ProductDescription> GetRemoteProductList(string connectionString) {
|
---|
143 | using (var client = new DeploymentService.UpdateClient()) {
|
---|
144 | return client.GetProducts();
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | /// <summary>
|
---|
149 | /// Installs plugins from remote server
|
---|
150 | /// </summary>
|
---|
151 | /// <param name="connectionString"></param>
|
---|
152 | /// <param name="pluginNames"></param>
|
---|
153 | public void Install(string connectionString, IEnumerable<IPluginDescription> plugins) {
|
---|
154 | using (var client = new DeploymentService.UpdateClient()) {
|
---|
155 | var args = new PluginInfrastructureCancelEventArgs(plugins.Select(x => x.Name + " " + x.Version));
|
---|
156 | OnPreInstall(args);
|
---|
157 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
158 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
159 | Unpack(zippedPackage);
|
---|
160 | OnInstalled(new PluginInfrastructureEventArgs(plugin));
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /// <summary>
|
---|
166 | /// Updates plugins from remote server
|
---|
167 | /// </summary>
|
---|
168 | /// <param name="pluginNames"></param>
|
---|
169 | public void Update(string connectionString, IEnumerable<IPluginDescription> plugins) {
|
---|
170 | PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins.Select(x => x.Name + " " + x.Version));
|
---|
171 | OnPreUpdate(args);
|
---|
172 | if (!args.Cancel) {
|
---|
173 | using (var client = new DeploymentService.UpdateClient()) {
|
---|
174 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
175 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
176 | Unpack(zippedPackage);
|
---|
177 | OnUpdated(new PluginInfrastructureEventArgs(plugin));
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /// <summary>
|
---|
184 | /// Deletes all plugin files from local installation
|
---|
185 | /// </summary>
|
---|
186 | /// <param name="pluginNames"></param>
|
---|
187 | public void Remove(IEnumerable<IPluginDescription> plugins) {
|
---|
188 | var fileNames = from pluginToDelete in plugins
|
---|
189 | from file in pluginToDelete.Files
|
---|
190 | select Path.Combine(pluginDir, file.Name);
|
---|
191 | var args = new PluginInfrastructureCancelEventArgs(fileNames);
|
---|
192 | OnPreDelete(args);
|
---|
193 | if (!args.Cancel) {
|
---|
194 | foreach (string fileName in fileNames) {
|
---|
195 | File.Delete(fileName);
|
---|
196 | OnDeleted(new PluginInfrastructureEventArgs(fileName));
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void Unpack(byte[] zippedPackage) {
|
---|
202 | using (ZipInputStream s = new ZipInputStream(new MemoryStream(zippedPackage))) {
|
---|
203 | ZipEntry theEntry;
|
---|
204 | string tmpEntry = String.Empty;
|
---|
205 | while ((theEntry = s.GetNextEntry()) != null) {
|
---|
206 | string directoryName = pluginDir;
|
---|
207 | string fileName = Path.GetFileName(theEntry.Name);
|
---|
208 | // create directory
|
---|
209 | if (directoryName != "") {
|
---|
210 | Directory.CreateDirectory(directoryName);
|
---|
211 | }
|
---|
212 | if (fileName != String.Empty) {
|
---|
213 | string fullPath = Path.Combine(directoryName, fileName);
|
---|
214 | string fullDirPath = Path.GetDirectoryName(fullPath);
|
---|
215 | if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
|
---|
216 | FileStream streamWriter = File.Create(fullPath);
|
---|
217 | int size = 2048;
|
---|
218 | byte[] data = new byte[2048];
|
---|
219 | while (true) {
|
---|
220 | size = s.Read(data, 0, data.Length);
|
---|
221 | if (size > 0) {
|
---|
222 | streamWriter.Write(data, 0, size);
|
---|
223 | } else {
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | streamWriter.Close();
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | private void OnPreUpdate(PluginInfrastructureCancelEventArgs args) {
|
---|
234 | if (PreUpdatePlugin != null) PreUpdatePlugin(this, args);
|
---|
235 | }
|
---|
236 |
|
---|
237 | private void OnUpdated(PluginInfrastructureEventArgs args) {
|
---|
238 | if (PluginUpdated != null) PluginUpdated(this, args);
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void OnPreDelete(PluginInfrastructureCancelEventArgs args) {
|
---|
242 | if (PreRemovePlugin != null) PreRemovePlugin(this, args);
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void OnDeleted(PluginInfrastructureEventArgs args) {
|
---|
246 | if (PluginRemoved != null) PluginRemoved(this, args);
|
---|
247 | }
|
---|
248 |
|
---|
249 | private void OnPreInstall(PluginInfrastructureCancelEventArgs args) {
|
---|
250 | if (PreInstallPlugin != null) PreInstallPlugin(this, args);
|
---|
251 | }
|
---|
252 |
|
---|
253 | private void OnInstalled(PluginInfrastructureEventArgs args) {
|
---|
254 | if (PluginInstalled != null) PluginInstalled(this, args);
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|