From 1e5a24c3b8c859882667b0c91a6bb4459f5d1285 Mon Sep 17 00:00:00 2001 From: Lisa Malenfant Date: Sat, 30 Sep 2023 15:08:09 -0700 Subject: [PATCH 1/3] Fix the SonarCloud issues in MCCL and MCPP --- .gitignore | 1 + .../CommandLine.cs | 8 +- .../Program.cs | 150 +++++++++--------- src/Vts.MonteCarlo.PostProcessor/Program.cs | 19 +-- 4 files changed, 84 insertions(+), 94 deletions(-) diff --git a/.gitignore b/.gitignore index a65a5b199..69f75023d 100644 --- a/.gitignore +++ b/.gitignore @@ -94,3 +94,4 @@ src/README.md src/Vts.MonteCarlo.CommandLineApplication/Properties/launchSettings.json .sonarqube/ src/Vts/Vts.xml +src/.github/ diff --git a/src/Vts.MonteCarlo.CommandLineApplication/CommandLine.cs b/src/Vts.MonteCarlo.CommandLineApplication/CommandLine.cs index 9831fc271..9b249f703 100644 --- a/src/Vts.MonteCarlo.CommandLineApplication/CommandLine.cs +++ b/src/Vts.MonteCarlo.CommandLineApplication/CommandLine.cs @@ -23,9 +23,9 @@ public Switch(string name, Action> handler) Handler = handler; } - public string Name { get; private set; } - public string ShortForm { get; private set; } - public Action> Handler { get; private set; } + public string Name { get; } + public string ShortForm { get; } + public Action> Handler { get; } public int InvokeHandler(string[] values) { @@ -37,7 +37,7 @@ public int InvokeHandler(string[] values) /* The regex that extracts names and comma-separated values for switches in the form ([="value 1",value2,...])+ */ private static readonly Regex ArgRegex = - new Regex(@"(?[^=]+)=?((?\""?)(?(?(quoted)[^\""]+|[^,]+))\""?,?)*", + new(@"(?[^=]+)=?((?\""?)(?(?(quoted)[^\""]+|[^,]+))\""?,?)*", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(10)); diff --git a/src/Vts.MonteCarlo.CommandLineApplication/Program.cs b/src/Vts.MonteCarlo.CommandLineApplication/Program.cs index 5d8fa45c5..ff22b060b 100644 --- a/src/Vts.MonteCarlo.CommandLineApplication/Program.cs +++ b/src/Vts.MonteCarlo.CommandLineApplication/Program.cs @@ -1,5 +1,3 @@ -//#define PROCESS_ATTACH_DEBUG - using NLog; using System; using System.Collections.Generic; @@ -12,12 +10,6 @@ namespace Vts.MonteCarlo.CommandLineApplication { - - #region CommandLine Arguments Parser - - /* Simple commandline argument parser written by Ananth B. http://www.ananthonline.net */ - - #endregion /// /// Monte Carlo command line application program. Type "mc help" for /// a description of the different command line parameters. @@ -35,15 +27,11 @@ public static class Program /// int = 2 (infile exists but does not pass validation) public static int Main(string[] args) { -#if PROCESS_ATTACH_DEBUG - Console.Read(); -#endif var inFile = ""; var inFiles = new List(); var outName = ""; var outPath = ""; - var CPUCount = "1"; // default is to use 1 - var infoOnlyOption = false; + var cpuCount = "1"; // default is to use 1 IList paramSweep = new List(); args.Process(() => @@ -59,12 +47,10 @@ public static int Main(string[] args) ShowHelp(helpTopic); else ShowHelp(); - infoOnlyOption = true; }), new CommandLine.Switch("geninfiles", val => { GenerateDefaultInputFiles(); - infoOnlyOption = true; }), new CommandLine.Switch("infile", val => { @@ -91,21 +77,21 @@ public static int Main(string[] args) }), new CommandLine.Switch("cpucount", val => { - CPUCount = val.First(); - if (CPUCount == "all") + cpuCount = val.First(); + if (cpuCount == "all") { - CPUCount = Environment.ProcessorCount.ToString(); - Logger.Info(() => "changed to maximum CPUs on system " + CPUCount); + cpuCount = Environment.ProcessorCount.ToString(); + Logger.Info(() => "changed to maximum CPUs on system " + cpuCount); } else { - if (!int.TryParse(CPUCount, out var CPUCountInt)) + if (!int.TryParse(cpuCount, out var cpuCountInt)) { - Logger.Info(() => "unknown cpucount option " + CPUCount); + Logger.Info(() => $"unknown cpucount option {cpuCount}"); } else { - Logger.Info(() => "number of CPUs specified as " + CPUCount); + Logger.Info(() => $"number of CPUs specified as {cpuCountInt}"); } } }), @@ -134,9 +120,13 @@ public static int Main(string[] args) Logger.Info(() => "parameter sweep specified as " + sweepString[0] + " values"); })); - if (!infoOnlyOption) + if (CheckInfoOnly(args)) { - Func checkValid = simInput => + LogManager.Configuration = null; + return 0; + } + + Func checkValid = simInput => { var validationResult = MonteCarloSetup.ValidateSimulationInput(simInput); if (validationResult.IsValid) return true; @@ -145,80 +135,86 @@ public static int Main(string[] args) Console.Write("\nRemarks:" + validationResult.Remarks); return false; }; - SimulationInput input; - if (paramSweep.Any() || inFiles.Any()) + SimulationInput input; + if (paramSweep.Any() || inFiles.Any()) + { + IList inputs; + if (paramSweep.Any()) { - IList inputs; - if (paramSweep.Any()) - { - input = MonteCarloSetup.ReadSimulationInputFromFile(inFile); - if (input == null) - { - return 1; - } - if (!string.IsNullOrEmpty(outName)) - { - input.OutputName = outName; - } - - inputs = MonteCarloSetup.ApplyParameterSweeps(input, paramSweep).ToList(); - } - else // if infiles.Count() > 0 + input = MonteCarloSetup.ReadSimulationInputFromFile(inFile); + if (input == null) { - inputs = inFiles.Select(file => MonteCarloSetup.ReadSimulationInputFromFile(file)).ToList(); - if (!inputs.Any()) - { - return 1; - } + return 1; } - // validate input - if (inputs.Any(simulationInput => !checkValid(simulationInput))) + if (!string.IsNullOrEmpty(outName)) { - return 2; - } - // make sure input does not specify Database if CPUCount>1 - if (int.Parse(CPUCount) > 1 && (inputs.First().Options.Databases != null && inputs.First().Options.Databases.Count != 0)) - { - CPUCount = 1.ToString(); - Logger.Info(() => "parallel processing cannot be performed when a Database is specified, changed CPUCount to 1"); + input.OutputName = outName; } - MonteCarloSetup.RunSimulations(inputs, outPath, int.Parse(CPUCount)); - Logger.Info("\nSimulations complete."); - return 0; + inputs = MonteCarloSetup.ApplyParameterSweeps(input, paramSweep).ToList(); } - - input = MonteCarloSetup.ReadSimulationInputFromFile(inFile); - if (input == null) + else // if infiles.Count() > 0 { - return 1; + inputs = inFiles.Select(file => MonteCarloSetup.ReadSimulationInputFromFile(file)).ToList(); + if (!inputs.Any()) + { + return 1; + } } - - if (!checkValid(input)) - return 2; - - if (!string.IsNullOrEmpty(outName)) + // validate input + if (inputs.Any(simulationInput => !checkValid(simulationInput))) { - input.OutputName = outName; + return 2; } - // make sure input does not specify Database if CPUCount>1 - if (int.Parse(CPUCount) > 1 && (input.Options.Databases != null && input.Options.Databases?.Count != 0)) + if (int.Parse(cpuCount) > 1 && (inputs.First().Options.Databases != null && inputs.First().Options.Databases.Count != 0)) { - CPUCount = 1.ToString(); - Logger.Info(() => - "parallel processing cannot be performed when a Database is specified, changed CPUCount to 1"); + cpuCount = 1.ToString(); + Logger.Info(() => "parallel processing cannot be performed when a Database is specified, changed CPUCount to 1"); } - MonteCarloSetup.RunSimulation(input, outPath, int.Parse(CPUCount)); - Logger.Info("\nSimulation complete."); + MonteCarloSetup.RunSimulations(inputs, outPath, int.Parse(cpuCount)); + Logger.Info("\nSimulations complete."); return 0; } - LogManager.Configuration = null; + input = MonteCarloSetup.ReadSimulationInputFromFile(inFile); + if (input == null) + { + return 1; + } + + if (!checkValid(input)) + return 2; + + if (!string.IsNullOrEmpty(outName)) + { + input.OutputName = outName; + } + + // make sure input does not specify Database if CPUCount>1 + if (int.Parse(cpuCount) > 1 && (input.Options.Databases != null && input.Options.Databases?.Count != 0)) + { + cpuCount = 1.ToString(); + Logger.Info(() => + "parallel processing cannot be performed when a Database is specified, changed CPUCount to 1"); + } + + MonteCarloSetup.RunSimulation(input, outPath, int.Parse(cpuCount)); + Logger.Info("\nSimulation complete."); return 0; } + private static bool CheckInfoOnly(IEnumerable args) + { + foreach (var arg in args) + { + if (arg.StartsWith("help")) return true; + if (arg.Equals("geninfiles")) return true; + } + return false; + } + private static void GenerateDefaultInputFiles() { try diff --git a/src/Vts.MonteCarlo.PostProcessor/Program.cs b/src/Vts.MonteCarlo.PostProcessor/Program.cs index 46d7a909c..46eaf5fe0 100644 --- a/src/Vts.MonteCarlo.PostProcessor/Program.cs +++ b/src/Vts.MonteCarlo.PostProcessor/Program.cs @@ -7,12 +7,6 @@ namespace Vts.MonteCarlo.PostProcessor { - #region CommandLine Arguments Parser - - /* Simple commandline argument parser written by Ananth B. http://www.ananthonline.net */ - - #endregion - public static class Program { /// @@ -23,14 +17,10 @@ public static class Program /// int = 2 (infile exists but does not pass validation) public static int Main(string[] args) { -#if PROCESS_ATTACH_DEBUG - Console.Read(); -#endif var inFile = "infile.txt"; var inPath = ""; var outName = ""; var outPath = ""; - var infoOnlyOption = false; args.Process(() => { Console.WriteLine(@"Virtual Photonics MC Post-Processor 1.0"); @@ -40,13 +30,11 @@ public static int Main(string[] args) }, new CommandLine.Switch("help", val => { - infoOnlyOption = true; ShowHelp(); }), new CommandLine.Switch("geninfiles", val => { GenerateDefaultInputFiles(); - infoOnlyOption = true; }), new CommandLine.Switch("infile", val => { @@ -70,7 +58,7 @@ public static int Main(string[] args) }) ); - if (infoOnlyOption) return 0; + if (CheckInfoOnly(args)) return 0; var input = PostProcessorSetup.ReadPostProcessorInputFromFile(inFile); if (input == null) { @@ -94,6 +82,11 @@ public static int Main(string[] args) return 0; } + private static bool CheckInfoOnly(string[] args) + { + return args.Contains("help") || args.Contains("geninfiles"); + } + private static void GenerateDefaultInputFiles() { var infiles = PostProcessorInputProvider.GenerateAllPostProcessorInputs(); From a0e2d849b23d73f4b216d853afaaea0b98edd5a0 Mon Sep 17 00:00:00 2001 From: Lisa Malenfant Date: Sat, 30 Sep 2023 16:45:48 -0700 Subject: [PATCH 2/3] Removed the CommandLineTests.cs file from MCPP because it is identical to the one in MCCL Added InternalsVisibleTo for the MCPP application because it is needed for the stand-alone MCPP --- .../Program.cs | 1 + .../CommandLineTests.cs | 34 ------------------- 2 files changed, 1 insertion(+), 34 deletions(-) delete mode 100644 src/Vts.MonteCarlo.PostProcessor.Test/CommandLineTests.cs diff --git a/src/Vts.MonteCarlo.CommandLineApplication/Program.cs b/src/Vts.MonteCarlo.CommandLineApplication/Program.cs index ff22b060b..9ff7b5882 100644 --- a/src/Vts.MonteCarlo.CommandLineApplication/Program.cs +++ b/src/Vts.MonteCarlo.CommandLineApplication/Program.cs @@ -7,6 +7,7 @@ using Vts.Common.Logging; [assembly: InternalsVisibleTo("Vts.MonteCarlo.CommandLineApplication.Test")] +[assembly: InternalsVisibleTo("Vts.MonteCarlo.PostProcessor.CommandLineApplication.Test")] namespace Vts.MonteCarlo.CommandLineApplication { diff --git a/src/Vts.MonteCarlo.PostProcessor.Test/CommandLineTests.cs b/src/Vts.MonteCarlo.PostProcessor.Test/CommandLineTests.cs deleted file mode 100644 index 9b1f1ff71..000000000 --- a/src/Vts.MonteCarlo.PostProcessor.Test/CommandLineTests.cs +++ /dev/null @@ -1,34 +0,0 @@ -using NUnit.Framework; -using System; -using System.Linq; - -namespace Vts.MonteCarlo.CommandLineApplication.Test -{ - [TestFixture] - internal class CommandLineTests - { - [Test] - public void CommandLine_switch_test() - { - var arguments = new[] { "h=topic" }; - arguments.Process(() => Console.WriteLine(@"Usage"), - new CommandLine.Switch("help", "h", arg => - { - var helpTopic = arg.First(); - Assert.AreEqual("topic", helpTopic); - })); - } - - [Test] - public void CommandLine_no_switches_test() - { - var arguments = new[] { "undefined=true" }; - arguments.Process(() => Console.WriteLine(@"Usage"), - new CommandLine.Switch("help", "h", arg => - { - var undefined = arg.First(); - Assert.AreEqual("topic", undefined); - })); - } - } -} From 678f72ce4d6447ae0f3e98ae753138e821e2f516 Mon Sep 17 00:00:00 2001 From: Lisa Malenfant Date: Sun, 1 Oct 2023 14:40:35 -0700 Subject: [PATCH 3/3] Added the specific version to the header --- .gitignore | 12 +++++++++--- src/Vts.MonteCarlo.PostProcessor/Program.cs | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 69f75023d..f758e06ed 100644 --- a/.gitignore +++ b/.gitignore @@ -79,9 +79,17 @@ CVS ####################### *.nuspec *.nupkg -src/.vs/ + +# Misc ignores # +################ +.vs/ +.sonarqube/ publish/ matlab/post_processing/one_layer_all_detectors/ + +# Nested MCCL Repo ignores # +############################ +src/.vs/ src/Vts.MonteCarlo.sln src/Vts.MonteCarlo.PostProcessor/Vts.MonteCarlo.PostProcessor.Application.csproj src/Vts.MonteCarlo.PostProcessor.Test/Vts.MonteCarlo.PostProcessor.Application.Test.csproj @@ -90,8 +98,6 @@ src/Vts.MonteCarlo.CommandLineApplication.Test/Vts.MonteCarlo.Application.Test.c src/.gitignore src/license.md src/README.md -.vs/ src/Vts.MonteCarlo.CommandLineApplication/Properties/launchSettings.json -.sonarqube/ src/Vts/Vts.xml src/.github/ diff --git a/src/Vts.MonteCarlo.PostProcessor/Program.cs b/src/Vts.MonteCarlo.PostProcessor/Program.cs index 46eaf5fe0..da38168ef 100644 --- a/src/Vts.MonteCarlo.PostProcessor/Program.cs +++ b/src/Vts.MonteCarlo.PostProcessor/Program.cs @@ -23,7 +23,7 @@ public static int Main(string[] args) var outPath = ""; args.Process(() => { - Console.WriteLine(@"Virtual Photonics MC Post-Processor 1.0"); + Console.WriteLine(@"Virtual Photonics MC Post-Processor {GetVersionNumber(3)}\n"")"); Console.WriteLine(); Console.WriteLine(@"For more information type mc_post help"); Console.WriteLine();