-
Notifications
You must be signed in to change notification settings - Fork 13
/
Program.cs
416 lines (379 loc) · 18.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/// Copyright (c) 2013-2016, ITI GmbH
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions are met:
///
/// 1. Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
///
/// 2. Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
///
/// 3. Neither the name of the copyright holder nor the names of its contributors
/// may be used to endorse or promote products derived from this software without
/// specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
/// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
/// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
/// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
/// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
/// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
namespace CsvCompare
{
class Program
{
/// global Log object
private static Log _log = new Log();
/// The main entry of the application
/// @para cmdArgs contains an array of command line parameters that are parsed using CommandLine.Dll
public static void Main(string[] cmdArgs)
{
//Global catch to prevent crash
try { Run(cmdArgs); }
catch (Exception ex)
{
if (null != _log)
_log.Error("Exception during run: {0}", ex.Message);
else
Console.Error.WriteLine("Exception during run: {0}", ex.Message);
var options = new Options();
Console.WriteLine(options.GetUsage());
#if DEBUG
Console.ReadLine();
#endif
Environment.Exit(2);
}
}
private static void Run(string[] cmdArgs)
{
var options = new Options();
Parser parser = new Parser();
if (parser.ParseArguments(cmdArgs, options))
{
if (options.Verbosity > 0)
_log.Verbosity = (LogLevel)options.Verbosity;
else
#if DEBUG
_log.Verbosity = LogLevel.Debug;
#endif
_log.WriteLine(LogLevel.Debug, "Using CSV Compare Version {0} ({1})", Info.AssemblyVersion, Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture);
_log.WriteLine(LogLevel.Debug, "Starting new result check @{0}", DateTime.Now);
_log.WriteLine(LogLevel.Debug, "Parsing command line options: {0} * {1}", Environment.NewLine, string.Join(" ", cmdArgs));
_log.WriteLine(LogLevel.Debug, "Successfully parsed the following options:");
_log.WriteLine(LogLevel.Debug, "Operation mode is {0}", options.Mode);
_log.WriteLine(LogLevel.Debug, "Tolerance is {0}", options.Tolerance);
if (options.Delimiter == 0)
{
_log.WriteLine(LogLevel.Debug, "Delimiter is not explicitly set, so I am using \";\"");
options.Delimiter = ';';
}
else
_log.WriteLine(LogLevel.Debug, "Delimiter to be used to parse CSV has been explicitly set and is \"{0}\"", options.Delimiter);
if (null == options.Logfile)
_log.WriteLine(LogLevel.Debug, "Logfile is empty");
else
{
if (!options.OverrideOutput && File.Exists(options.Logfile))
{
options.Logfile = Path.Combine(Path.GetDirectoryName(options.Logfile), string.Format(CultureInfo.CurrentCulture, "{0:yyyy-MM-ddTHH-mm-ss}_log.txt", DateTime.Now));
_log.WriteLine(LogLevel.Warning, "Logfile already exists and --override has been set to false. Changed log file name to \"{0}\"", options.Logfile);
}
else
_log.WriteLine(LogLevel.Debug, "Logfile is {0}", options.Logfile);
_log.SetLogFile(new FileInfo(options.Logfile));
}
if (options.Items.Count == 0)
{
_log.Error("No files or directories to be compared have been given.");
Console.WriteLine(options.GetUsage());
Environment.Exit(2);
}
MetaReport meta = new MetaReport();
if (String.IsNullOrEmpty(options.ReportDir))//report to directory of compare file if no report directory has been set
{
FileAttributes attr = File.GetAttributes(options.Items[0]);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
options.ReportDir = Path.GetFullPath(options.Items[0]);
else
options.ReportDir = Path.GetDirectoryName(options.Items[0]);
_log.WriteLine(LogLevel.Warning, "No report directory has been set, using \"{0}\"", options.ReportDir);
}
else
{
meta.ReportDirSet = true;
options.ReportDir = Path.GetFullPath(options.ReportDir);//normalize report dir (i.e. make absolut if relative)
if (!Directory.Exists(options.ReportDir))
Directory.CreateDirectory(options.ReportDir);
}
meta.FileName = new FileInfo(Path.Combine(options.ReportDir, string.Format(CultureInfo.CurrentCulture, "{0:yyyy-MM-dd}-index.html", DateTime.Now)));
switch (options.Mode)
{
case OperationMode.CsvFileCompare://compare the file given as option 1 with the file given as option 2
if (options.Items.Count != 2)
{
_log.Error("Not enough arguments have been given. Please specify base and compare file.");
Console.WriteLine(options.GetUsage());
Environment.Exit(2);
}
Report rep = CheckFiles(options);
if (null != rep && !options.NoMetaReport)
{
meta.Reports.Add(rep);//Return "1" on invalid testresults
meta.WriteReport(_log, options);
}
break;
case OperationMode.CsvTreeCompare://compare files in the dirctory option 1 with the files in directory option 2
if (options.Items.Count != 2)
{
_log.Error("Not enough arguments have been given. Please specify base and compare directory.");
Console.WriteLine(options.GetUsage());
Environment.Exit(2);
}
CheckTrees(meta, options);
if(!options.NoMetaReport)
meta.WriteReport(_log, options);
break;
case OperationMode.FmuChecker://run FMU checker on all FMUs in directory given via option 1 and compare the result to CSVs in the source directory
if (options.Items.Count != 1)
{
if (options.Items.Count == 0)
_log.Error("Not enough arguments have been given. Please specify a compare directory with FMUs.");
else
_log.Error("Too many arguments have been given. Please specify a compare directory with FMUs only.");
Console.WriteLine(options.GetUsage());
Environment.Exit(2);
}
if (string.IsNullOrEmpty(options.CheckerPath))
{
_log.Error("No path to FMU Checker binary given.");
Console.WriteLine(options.GetUsage());
}
else
{
CheckFMUTree(ref meta, options);
meta.WriteReport(_log, options);
}
break;
case OperationMode.PlotOnly:
foreach (string item in options.Items)
using (CsvFile file = new CsvFile(item, options, _log))
{
meta.Reports.Add(file.PlotCsvFile(_log));
}
foreach (Report r in meta.Reports)
if (!r.WriteReport(_log, options.NoMetaReport ? null : meta.FileName.FullName, options))
_log.Error("Error writing report to {0}", r.FileName);
if (!options.NoMetaReport)
meta.WriteReport(_log, options);
break;
default://Invalid mode
Console.WriteLine(options.GetUsage());
break;
}
}
else
Console.WriteLine(options.GetUsage());
#if DEBUG
//Keep window open in debug mode
_log.WriteLine(LogLevel.Debug, "Done.");
Console.ReadLine();
#endif
}
private static void CheckFMUTree(ref MetaReport meta, Options options)
{
DirectoryInfo dirCompare, dirBase;
string sTempDir = string.Format(CultureInfo.CurrentCulture, "{0}CSV-Compare-{1:yyyyMMdd}", Path.GetTempPath(), DateTime.Now);
dirBase = new DirectoryInfo(options.Items[0]);
if (!dirBase.Exists)
{
_log.Error("The directory \"{0}\" containing FMU files does not exist.", dirBase.FullName);
Environment.Exit(2);
}
string outFile = string.Empty;
dirCompare = new DirectoryInfo(sTempDir);
if (!dirCompare.Exists)
Directory.CreateDirectory(dirCompare.ToString());
if (!String.IsNullOrEmpty(options.ReportDir))
meta = new MetaReport(options.ReportDir);
else
meta = new MetaReport();
foreach (FileInfo file in dirBase.GetFiles("*.fmu", SearchOption.AllDirectories))
{
if (!RunFMUChecker(options, dirCompare, ref outFile, file))//Skip to next FMU on error
continue;
string sBase = Path.Combine(file.Directory.FullName, Path.GetFileNameWithoutExtension(file.Name) + ".csv");
_log.WriteLine(LogLevel.Debug, "Searching for CSV {0} ", sBase);
if (!File.Exists(sBase))//if $FILENAME$.csv could not be found search for protocol.csv
{
sBase = Path.Combine(file.Directory.FullName, "protocol.csv");
_log.WriteLine(LogLevel.Information, "Not found trying \"{0}\"", sBase);
if (!File.Exists(sBase))
{
_log.Error("Found nothing to compare with, skipping {0}", file.Name);
continue;
}
}
_log.WriteLine(LogLevel.Information, "Trying to compare with \"{0}\"", sBase);
try
{
CsvFile csvCompare = new CsvFile(outFile, options, _log);
try
{
meta.Reports.Add(csvCompare.CompareFiles(_log, new CsvFile(sBase, options, _log), Path.Combine(options.ReportDir, file.Name + ".html"), ref options));
}
catch (ArgumentNullException ex) { _log.Error(ex.Message); }
}
catch (ArgumentException ex)
{
_log.Error(ex.Message);
continue;
}
}
}
private static void CheckTrees(MetaReport meta, Options options)
{
DirectoryInfo dirCompare = new DirectoryInfo(options.Items[0]);
DirectoryInfo dirBase = new DirectoryInfo(options.Items[1]);
FileInfo[] files = dirCompare.GetFiles("*.csv", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
_log.WriteLine(LogLevel.Debug, "Searching for file {0} in {1}", file.Name, dirBase.FullName);
//Try different locations for the base file
string sBaseFile = string.Empty;
if (dirBase.GetFiles(file.Name).Length == 1)//1. Base file is in the, via command line, given base directory
{
sBaseFile = Path.Combine(dirBase.FullName, file.Name);
_log.WriteLine(LogLevel.Debug, "Found base file in given base directory \"{0}\", comparing ...", dirBase.Name);
}
else
{
try
{
sBaseFile = file.FullName.Replace(options.Items[0], options.Items[1]);//2. Base file is in the same directory structure as the compare file
if (!File.Exists(sBaseFile))
{
_log.WriteLine(LogLevel.Warning, "{0} not found in {1} or {2}, skipping validation.", file.Name, dirBase.Name, sBaseFile);
continue;
}
else
_log.WriteLine(LogLevel.Debug, "Found base file in given base directory \"{0}\", comparing ...", dirBase);
}
catch (Exception ex)
{
_log.Error(ex.Message);
continue;
}
}
Report r = CheckFiles(options, file.FullName, sBaseFile);
meta.Reports.Add(r);
}
}
private static Report CheckFiles(Options options, string Compare = null, string Base = null)
{
//Check Arguments
if (string.IsNullOrEmpty(Compare))
if (options.Items.Count == 2)
Compare = options.Items[0];
else
throw new ArgumentException("You have to set compare and base CSV files!");
if (string.IsNullOrEmpty(Base))
if (options.Items.Count == 2)
Base = options.Items[1];
else
throw new ArgumentException("You have to set compare and base CSV files!");
try
{
using (CsvFile csvCompare = new CsvFile(Compare, options, _log))
{
csvCompare.ShowRelativeErrors = !options.AbsoluteError;
try
{
using (CsvFile csvBase = new CsvFile(Base, options, _log))
{
//#if DEBUG //Save CSV files during DEBUG session
// if (!string.IsNullOrEmpty(options.ReportDir))
// {
// csvBase.Save(options.ReportDir, options);
// csvCompare.Save(options.ReportDir, options);
// }
// else
// {
// csvBase.Save(options);
// csvCompare.Save(options);
// }
//#endif
_log.WriteLine(LogLevel.Debug, "Exiting with exit code \"{0}\".", Environment.ExitCode);
return csvCompare.CompareFiles(_log, csvBase, ref options);
}
}
catch (ArgumentException argEx)
{
_log.Error(argEx.Message);
return null;
}
catch (FileNotFoundException)
{
throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, "Base file \"{0}\" does not exist, exiting.", Base));
}
}
}
catch (ArgumentException ex)
{
_log.Error("Nothing has been parsed; maybe wrong CSV format?");
_log.Error("Exception said: {0}", ex.Message);
Environment.ExitCode = 2;
return null;
}
catch (FileNotFoundException)
{
throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, "Compare file \"{0}\" does not exist, exiting.", Compare));
}
}
private static bool RunFMUChecker(Options options, DirectoryInfo dirCompare, ref string outFile, FileInfo file)
{
bool bRet = true;
string sArgs;
using (Process p = new Process())
{
outFile = string.Format(CultureInfo.CurrentCulture, "{0}/{1}.csv", dirCompare.FullName, Path.GetFileNameWithoutExtension(file.FullName));
p.StartInfo = new ProcessStartInfo(options.CheckerPath);
if (!string.IsNullOrEmpty(options.CheckerArgs))
sArgs = options.CheckerArgs;
else
sArgs = "-l 1 -h 1e-2 -s 1.5";
p.StartInfo.Arguments = string.Format(CultureInfo.CurrentCulture, "{0} -o \"{1}\" \"{2}\"", sArgs, outFile, file.FullName);
p.StartInfo.UseShellExecute = false;
_log.WriteLine(LogLevel.Debug, "Run \"{0} {1}\"", p.StartInfo.FileName, p.StartInfo.Arguments);
if (p.Start())
p.WaitForExit();
if (p.ExitCode != 0)
{
_log.Error("Error during FMU Checker run");
bRet = false;
}
}
return bRet;
}
}
}