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