OSDN Git Service

na-get-lib,ウイルススキャンしても進捗が進んでいなかったバグを修正した。
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetInstall.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using NaGet.Packages.Install;\r
4 using NaGet.Packages;\r
5 using NaGet.Net;\r
6 \r
7 namespace NaGet.SubCommands\r
8 {\r
9         public class NaGetInstall : NaGetTaskSet\r
10         {\r
11                 private bool done = false;\r
12                 \r
13                 private int currentTaskSetIndex = -1;\r
14                 \r
15                 private PackageListsManager pkgListMan;\r
16                 \r
17                 /// <summary>\r
18                 /// ダウンロードに使うダウンローダオブジェクト\r
19                 /// </summary>\r
20                 public Downloader Downloader {\r
21                         get {\r
22                                 if (downloader == null) {\r
23                                         downloader = new Downloader();\r
24                                 }\r
25                                 return downloader;\r
26                         }\r
27                 }\r
28                 \r
29                 private Downloader downloader;\r
30                 \r
31                 private bool packageInstallerDownloaded = false;\r
32                 \r
33                 public override bool Cancelable {\r
34                         get { return ! done; }\r
35                 }\r
36                 \r
37                 /// <summary>\r
38                 /// インストールするパッケージ\r
39                 /// </summary>\r
40                 public Installation[] Installations;\r
41                 \r
42                 /// <summary>\r
43                 /// コンストラクタ\r
44                 /// </summary>\r
45                 /// <param name="pkgs">インストールするパッケージ</param>\r
46                 public NaGetInstall(PackageListsManager pkgListMan, Package[] pkgs)\r
47                         : this(pkgListMan, Installation.ConvertInstallations(pkgs))\r
48                 {\r
49                 }\r
50                 \r
51                 /// <summary>\r
52                 /// コンストラクタ\r
53                 /// </summary>\r
54                 /// <param name="installations">インストール処理の配列</param>\r
55                 public NaGetInstall(PackageListsManager pkgMan, Installation[] installations)\r
56                 {\r
57                         pkgListMan = pkgMan;\r
58                         \r
59                         Installations = installations;\r
60                         initializeMainTaskSetNames();\r
61                 }\r
62                 \r
63                 private void initializeMainTaskSetNames()\r
64                 {\r
65                         List<string> taskSetNames = new List<string>();\r
66                         \r
67                         for (int i =0; i < Installations.Length; i++) {\r
68                                 taskSetNames.Add(string.Format("取得: {0}", Installations[i].ToString()));\r
69                                 taskSetNames.Add(string.Format("ウイルススキャン: {0}", Installations[i].ToString()));\r
70                         }\r
71                         taskSetNames.Add("インストーラの検証");\r
72                         for (int i =0; i < Installations.Length; i++) {\r
73                                 taskSetNames.Add(string.Format("インストール: {0}", Installations[i].ToString()));\r
74                         }\r
75                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
76                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
77                         \r
78                         TaskSetNames = taskSetNames.ToArray();\r
79                 }\r
80                                 \r
81                 public override void Run()\r
82                 {\r
83                         currentTaskSetIndex = 0;\r
84                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "インストール処理開始");\r
85                         \r
86                         {\r
87                                 // ハッシュ非適合なインストーラの表\r
88                                 List<Installation> invalidInstallers = null;\r
89                                 \r
90                                 while (invalidInstallers == null || invalidInstallers.Count > 0) {\r
91                                         currentTaskSetIndex = 0;\r
92                                         packageInstallerDownloaded = false;\r
93                                         \r
94                                         runDownloadAndVirusCheckInstallers();\r
95                                         if (done) return; // もしrunDownloadInstallers()内でエラー終了していたなら終了\r
96                                         \r
97                                         packageInstallerDownloaded = true;\r
98                                         \r
99                                         // ハッシュの壊れているインストーラを取得\r
100                                         invalidInstallers = runCheckHashForInstaller();\r
101                                         \r
102                                         // ハッシュが壊れているときの対策\r
103                                         if (invalidInstallers.Count > 0) {\r
104                                                 string msg = string.Format("{0}個のパッケージでファイルが壊れている可能性があります\n強制的にインストールを続行しますか?",\r
105                                                                            invalidInstallers.Count);\r
106                                                 NaGetTaskQueryResult result = RaiseTaskSetQueryEvent(msg, NaGetTaskQueryResult.CONTINUE\r
107                                                                                                      | NaGetTaskQueryResult.RETRY\r
108                                                                                                      | NaGetTaskQueryResult.CANCEL);\r
109                                                 \r
110                                                 switch (result) {\r
111                                                         case NaGetTaskQueryResult.CONTINUE:\r
112                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ハッシュの非整合を無視してインストールを継続");\r
113                                                                 invalidInstallers.Clear(); // ハッシュ非適合パッケージを強制的に抹消\r
114                                                                 break;\r
115                                                         case NaGetTaskQueryResult.RETRY:\r
116                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ダウンロード処理を再試行");\r
117                                                                 \r
118                                                                 foreach (Installation invalidInst in invalidInstallers) {\r
119                                                                         invalidInst.RemoveDownloadedFile();\r
120                                                                 }\r
121                                                                 \r
122                                                                 break;\r
123                                                         //case NaGetTaskQueryResult.CANCEL:\r
124                                                         default:\r
125                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
126                                                                 done = true;\r
127                                                                 return;\r
128                                                 }\r
129                                         }\r
130                                         currentTaskSetIndex ++;\r
131                                 }\r
132                         }\r
133                         \r
134                         foreach (Installation inst in Installations) {\r
135                                 string installTaskMsg = TaskSetNames[currentTaskSetIndex];\r
136                                 if (inst.Silent && (!inst.SupportsSilentOnly)) {\r
137                                         installTaskMsg += " (サイレントインストール)";\r
138                                 }\r
139                                 \r
140                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, installTaskMsg);\r
141                                 \r
142                                 try {\r
143                                         \r
144                                         inst.ErrorDataReceived += this.ReceivedErrorData;\r
145                                         inst.OutputDataReceived += this.ReceivedOutputData;\r
146                                         int exitCode = inst.Install();\r
147                                         if (exitCode != 0) {\r
148                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "インストールが正常に終えていない可能性があります。インストーラの終了コード:"+exitCode);\r
149                                         }\r
150                                         \r
151                                         pkgListMan.WriteInstallationLog(inst);\r
152                                 } catch (Exception e) {\r
153                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
154                                         done = true;\r
155                                         return;\r
156                                 }\r
157                                 currentTaskSetIndex ++;\r
158                                 \r
159                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, installTaskMsg);\r
160                                 \r
161                                 if (cancelCalled) {\r
162                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
163                                         done = true;\r
164                                         return;\r
165                                 }\r
166                         }\r
167                         pkgListMan.SaveSystemInstalledLogList(); // ログのコミット\r
168                         \r
169                         runLocalUpdate();\r
170                         \r
171                         done = true;\r
172                         \r
173                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);\r
174                 }\r
175                 \r
176                 \r
177                 /// <summary>\r
178                 /// 処理内容のダウンロード・ウイルススキャン部分のサブルーチン\r
179                 /// </summary>\r
180                 private void runDownloadAndVirusCheckInstallers()\r
181                 {\r
182                         using (DownloadScanner scanner = new DownloadScanner()) {\r
183                                 scanner.Init();\r
184                                 foreach (Installation inst in Installations) {\r
185                                         if (! inst.IsInstallablePackage()) {\r
186                                                 string msg = string.Format("{0}はインストールすることができません", inst.ToString());\r
187                                                 \r
188                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, msg);\r
189                                                 done = true;\r
190                                                 return;\r
191                                         }\r
192                                         \r
193                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
194                                         \r
195                                         if (! inst.Downloaded) {\r
196                                                 try {\r
197                                                         inst.Download(Downloader);\r
198                                                 } catch (NaGetTaskCanceledException) {\r
199                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "インストーラのダウンロード処理がキャンセルされました");\r
200                                                         done = true;\r
201                                                         return;\r
202                                                 } catch (System.Net.WebException e) {\r
203                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
204                                                         if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
205                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続されていません。");\r
206                                                         } else {\r
207                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。");\r
208                                                         }\r
209                                                         done = true;\r
210                                                         return;\r
211                                                 } catch (Exception e) {\r
212                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
213                                                         done = true;\r
214                                                         return;\r
215                                                 }\r
216                                         }\r
217                                         currentTaskSetIndex ++;\r
218                                         \r
219                                         if (inst.Downloaded) { // 正常終了\r
220                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
221                                         } else { // インストールが完了せずに終わった=失敗=エラー\r
222                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("{0}のインストーラを正常にダウンロードできませんでした", inst.ToString()));\r
223                                                 done = true;\r
224                                                 return;\r
225                                         }\r
226                                         \r
227                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
228                                         if (scanner.HasScanner) {\r
229                                                 try {\r
230                                                         inst.ScanInstallerFile(scanner);\r
231                                                 } catch (System.Runtime.InteropServices.COMException ex) {\r
232                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING,\r
233                                                                           string.Format("{0} (E{1})", ex.Message, ex.ErrorCode));\r
234                                                 } catch (System.IO.FileNotFoundException ex) {\r
235                                                         if (ex.InnerException is System.Runtime.InteropServices.COMException) {\r
236                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING,\r
237                                                                                   string.Format("{0} (E{1})", ex.InnerException.Message, ((System.Runtime.InteropServices.COMException) ex.InnerException).ErrorCode));\r
238                                                         }\r
239                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "インストーラがウイルススキャナによって削除されました。");\r
240                                                         done = true;\r
241                                                         return;\r
242                                                 }\r
243                                         } else {\r
244                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, string.Format("ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)"));\r
245                                         }\r
246                                         currentTaskSetIndex ++;\r
247                                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
248                                 }\r
249                         }\r
250                 }\r
251                 \r
252                 /// <summary>\r
253                 /// ダウンロードしたパッケージが整合したか否かハッシュでチェック\r
254                 /// </summary>\r
255                 /// <returns>整合しなかったインストーラのリスト</returns>\r
256                 private List<Installation> runCheckHashForInstaller()\r
257                 {\r
258                         List<Installation> invalidInstallers = new List<Installation>();\r
259                         \r
260                         int i = 0;\r
261                         foreach (Installation inst in Installations) {\r
262                                 float percent = (CurrentTaskSetIndex+((float)i / Installations.Length))*100f/TaskSetNames.Length;\r
263                                 \r
264                                 if (inst.GetRegisteredHashCount() > 0) {\r
265                                         RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "検証: "+inst.ToString(), percent);\r
266                                         \r
267                                         if (inst.IsInstallablePackage() && inst.VerifyHashValues() == false) {\r
268                                                 invalidInstallers.Add(inst);\r
269                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "検証: "+inst.ToString() + " 非整合", percent);\r
270                                         } else {\r
271                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "検証: "+inst.ToString() + " OK", percent);\r
272                                         }\r
273                                 }\r
274                                 i++;\r
275                         }\r
276                         \r
277                         return invalidInstallers;\r
278                 }\r
279                 \r
280                 private void runLocalUpdate()\r
281                 {\r
282                         // インストールトリストの更新\r
283                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
284                         pkgListMan.DetectInstalledPkgs();\r
285                         pkgListMan.SaveInstalledPackageList();\r
286                         currentTaskSetIndex++;\r
287                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
288                 \r
289                         // システムにインストールされているリストの更新\r
290                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
291                         pkgListMan.DetectSystemInstalledPkgs();\r
292                         pkgListMan.SaveSystemInstalledPackageList();\r
293                         currentTaskSetIndex++;\r
294                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
295                 }\r
296                 \r
297                 public override bool Done {\r
298                         get { return done; }\r
299                 }\r
300                 \r
301                 public override int CurrentTaskSetIndex {\r
302                         get { return currentTaskSetIndex; }\r
303                 }\r
304                 \r
305                 private bool cancelCalled = false;\r
306                 \r
307                 public override bool Cancel()\r
308                 {\r
309                         cancelCalled = true;\r
310                         if (! packageInstallerDownloaded) {\r
311                                 return Downloader.Cancel();\r
312                         } else return true;\r
313                 }\r
314         }\r
315 }\r