protected virtual async Task GetAnalyzers() { Stopwatch sw = new Stopwatch(); sw.Start(); // Just in case clear AlternativeCompiler so it is not set to Roslyn or anything else by // the CS-Script installed (if any) on the host OS CSScript.GlobalSettings.UseAlternativeCompiler = null; CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Mono; CSScript.CacheEnabled = false; //Script caching is broken on Mono: https://github.com/oleg-shilo/cs-script/issues/10 CSScript.KeepCompilingHistory = true; DirectoryInfo analyzers_dir = new DirectoryInfo(Path.Combine(this.DevAuditDirectory, "Analyzers", this.AnalyzerType)); if (!analyzers_dir.Exists) { this.AnalyzersInitialized = false; return; } this.AnalyzerScripts = analyzers_dir.GetFiles("*.cs", SearchOption.AllDirectories).ToList(); foreach (FileInfo f in this.AnalyzerScripts) { string script = string.Empty; using (FileStream fs = f.OpenRead()) using (StreamReader r = new StreamReader(fs)) { script = await r.ReadToEndAsync(); } try { ByteCodeAnalyzer ba = (ByteCodeAnalyzer)await CSScript.Evaluator.LoadCodeAsync(script, this.HostEnvironment.ScriptEnvironment, this.Modules, this.Configuration, this.ApplicationOptions); this.Analyzers.Add(ba); this.HostEnvironment.Info("Loaded {0} analyzer from {1}.", ba.Name, f.FullName); } catch (csscript.CompilerException ce) { HostEnvironment.Error("Compiler error(s) compiling analyzer {0}.", f.FullName); IDictionaryEnumerator en = ce.Data.GetEnumerator(); while (en.MoveNext()) { List <string> v = (List <string>)en.Value; if (v.Count > 0) { if ((string)en.Key == "Errors") { HostEnvironment.ScriptEnvironment.Error(v.Aggregate((s1, s2) => s1 + Environment.NewLine + s2)); } else if ((string)en.Key == "Warnings") { HostEnvironment.ScriptEnvironment.Warning(v.Aggregate((s1, s2) => s1 + Environment.NewLine + s2)); } else { HostEnvironment.ScriptEnvironment.Error("{0} : {1}", en.Key, v.Aggregate((s1, s2) => s1 + Environment.NewLine + s2)); } } } throw ce; } } sw.Stop(); if (this.AnalyzerScripts.Count == 0) { this.HostEnvironment.Info("No {0} analyzers found in {1}.", this.AnalyzerType, analyzers_dir.FullName); return; } else if (this.AnalyzerScripts.Count > 0 && this.Analyzers.Count > 0) { if (this.Analyzers.Count < this.AnalyzerScripts.Count) { this.HostEnvironment.Warning("Failed to load {0} of {1} analyzer(s).", this.AnalyzerScripts.Count - this.Analyzers.Count, this.AnalyzerScripts.Count); } this.HostEnvironment.Success("Loaded {0} out of {1} analyzer(s) in {2} ms.", this.Analyzers.Count, this.AnalyzerScripts.Count, sw.ElapsedMilliseconds); this.AnalyzersInitialized = true; return; } else { this.HostEnvironment.Error("Failed to load {0} analyzer(s).", this.AnalyzerScripts.Count); return; } }
public override IEnumerable <OSSIndexQueryObject> GetPackages(params string[] o) { List <OSSIndexQueryObject> packages = new List <OSSIndexQueryObject> (); if (this.UseDockerContainer) { Docker.ProcessStatus process_status; string process_output, process_error; if (Docker.ExecuteInContainer(this.DockerContainerId, @"rpm -qa --qf ""%{NAME} %{VERSION}\n""", out process_status, out process_output, out process_error)) { string[] p = process_output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Regex process_output_pattern = new Regex(@"^(\S+)\s(\S+)", RegexOptions.Compiled); Match m; for (int i = 0; i < p.Count(); i++) { m = process_output_pattern.Match(p[i]); if (!m.Success) { throw new Exception("Could not parse rpm command output row: " + i.ToString() + "\n" + p [i]); } else { packages.Add(new OSSIndexQueryObject("rpm", m.Groups [1].Value, m.Groups [2].Value)); } } } else { throw new Exception(string.Format("Error running {0} command on docker container {1}: {2}", @"rpm -qa --qf ""%{NAME} %{VERSION}\n""", this.DockerContainerId, process_error)); } return(packages); } else { string command = @"rpm"; string arguments = @"-qa --qf ""%{NAME} %{VERSION}\n"""; Regex process_output_pattern = new Regex(@"^(\S+)\s(\S+)", RegexOptions.Compiled); HostEnvironment.ProcessStatus process_status; string process_output, process_error; if (HostEnvironment.Execute(command, arguments, out process_status, out process_output, out process_error)) { string[] p = process_output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Match m; for (int i = 0; i < p.Count(); i++) { m = process_output_pattern.Match(p[i]); if (!m.Success) { throw new Exception("Could not parse rpm command output row: " + i.ToString() + "\n" + p [i]); } else { packages.Add(new OSSIndexQueryObject("rpm", m.Groups [1].Value, m.Groups [2].Value)); } } } else { throw new Exception(string.Format("Error running {0} {1} command in host environment: {2}.", command, arguments, process_error)); } return(packages); } }