public void OnWarning(string warning) { PictConstants.Trace("Got warning with behavior {0}: {1}", warningBehavior, warning); if (this.warningBehavior == PictWarningBehaviors.Ignore) { PictConstants.Trace("Ignoring warning: {0}", warning); return; } if ((this.warningBehavior & PictWarningBehaviors.FireWarningBehaviorEvent) != 0) { if (this.WarningBehaviorEvent != null) { this.WarningBehaviorEvent(this, new PictWarningEventArgs(warning)); } } if ((this.warningBehavior & PictWarningBehaviors.WriteToConsoleError) != 0) { Console.Error.WriteLine(warning); } if ((this.warningBehavior & PictWarningBehaviors.ThrowException) != 0) { PictConstants.Trace("Throwing {0}", warning); throw new PairwiseException(warning); } }
public static void Save(PairwisePictCache cache, string filename) { if (filename == null) { throw new ArgumentNullException("filename"); } lock (mutex) { try { if (cache.Changed) { if (formatter == null) { formatter = new BinaryFormatter(); } using (FileStream fs = new FileStream(filename, FileMode.Create)) { formatter.Serialize(fs, cache); } PictConstants.Trace("Saved to {0}, entries = {1}", filename, cache.Count); cache.ResetChanged(); } else { PictConstants.Trace("Didn't change!"); } } catch (Exception e) { PictConstants.Trace("Error saving cache to {0}: {1}", filename, e.Message); } } }
void DisposeWork() { if (!disposed) { if (cache != null && cache.Changed) { PairwisePictCacheHelper.Save(cache, cacheFileName); } else if (cacheFileName != null) { PictConstants.Trace("Didn't change, so skipping saving to {0}", cacheFileName); } GC.SuppressFinalize(this); // prevent multiple disposes? disposed = true; } }
public static PairwisePictCache LoadOrCreate(string filename) { if (filename == null) { throw new ArgumentNullException(); } lock (mutex) { PairwisePictCache ppc; if (loaded.ContainsKey(filename)) { ppc = (PairwisePictCache)loaded[filename]; PictConstants.Trace("Using cached object instead of loading from {0}", filename); } else { if (formatter == null) { formatter = new BinaryFormatter(); } try { using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate)) { ppc = (PairwisePictCache)formatter.Deserialize(fs); Pict.PictConstants.Trace("Loaded cache from {0}", filename); } } catch (Exception e) { Pict.PictConstants.Trace("Error loading cache from {0}: {1}", filename, e.Message); ppc = new PairwisePictCache(); } loaded[filename] = ppc; } return(ppc); } }
public string[][] MaybeExecutePict(PairwiseSettings settings, string inputContent) { string key = GetHash(settings, inputContent); if (cache != null && settings.CanCache && cache.Contains(key)) { PictConstants.Trace("Found key, using cached value!"); this.last = cache[key]; return(last.ParsedOutput); } string randomFileName = Guid.NewGuid().ToString() + PictConstants.TempFileSuffix; using (StreamWriter sw = new StreamWriter(randomFileName)) { sw.WriteLine("# " + randomFileName + " " + settings.GetPictArgs()); sw.WriteLine(inputContent); } try { string[][] ret = AlwaysExecutePictOnFileName(randomFileName, settings); if (cache != null && settings.CanCache) { // always execute pict set last correctly cache[key] = this.LastExecutionInformation; } return(ret); } finally { File.Delete(randomFileName); } }
string[][] AlwaysExecutePictOnFileNameCore(string modelFileName, string argsForPict, PairwiseSettings settings) { if (modelFileName == null) { throw new ArgumentNullException("modelFileName"); } if (argsForPict == null) { argsForPict = ""; } else { argsForPict = argsForPict.Trim(); } if (!File.Exists(modelFileName)) { throw new FileNotFoundException("Pict model not found: " + modelFileName); } ProcessStartInfo psi = new ProcessStartInfo(); // this will throw if not found psi.FileName = PairwiseSettings.GetPictExecutableFullPath(); PictConstants.Trace("Attempting to use {0}", psi.FileName); psi.Arguments = (MaybeQuote(modelFileName) + " " + argsForPict).Trim(); psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; DateTime started = DateTime.Now; PictConstants.Trace("Starting " + psi.Arguments); using (Process process = Process.Start(psi)) { PictConstants.Trace("Started successfully"); string[][] parsed = Split(process.StandardOutput, PictConstants.OutputValueSeparator); process.StandardOutput.Close(); int seed = 0; bool wasRandom = false; process.WaitForExit(); PictConstants.Trace("Done waiting for exit"); TimeSpan elapsed = DateTime.Now - started; PictConstants.Trace("Elapsed time: {0}", elapsed); // note: there's some better way to do this?? string allStdErr = process.StandardError.ReadToEnd(); process.StandardError.Close(); if (allStdErr.Length != 0) { PictConstants.Trace("Begin stderr/{0}/end stderr*", allStdErr); ArrayList msgs = new ArrayList(); foreach (string s in allStdErr.Split('\r', '\n')) { if (s.Trim().Length != 0) { msgs.Add(s.TrimEnd()); } } for (int i = 0; i < msgs.Count; ++i) { string message = (string)msgs[i]; if (message.Trim().Length == 0) { continue; } if (message.StartsWith(PictConstants.UsedSeedMessage)) { message = message.Replace(PictConstants.UsedSeedMessage, "").Trim(); wasRandom = true; seed = int.Parse(message, PictConstants.Culture); PictConstants.Trace("{0} {1}", wasRandom, seed); } else if (message.StartsWith(PictConstants.WarningMessage) || (message.IndexOf("arning") != -1)) { while (i < msgs.Count - 1 && ((string)msgs[i + 1]).StartsWith(" ")) { message += Environment.NewLine + ((string)msgs[++i]); } if (settings != null) { settings.OnWarning(message.Trim()); } } else { PictConstants.Trace("throwing /{0}/", message.Trim()); throw new PairwiseException(message.Trim()); } } } // string stdout = sb.ToString(); PictExecutionInformation pei = new PictExecutionInformation(); pei.FileName = modelFileName; pei.Generated = DateTime.Now; pei.Options = argsForPict; pei.RandomSeedSpecified = wasRandom; // pei.RawOutput = stdout; pei.ParsedOutput = parsed; if (wasRandom) { pei.RandomSeed = seed; } this.last = pei; return(parsed); } // end using(Process) }
public static PairwiseSettings Parse(string argsForPict) { if (argsForPict == null) { throw new ArgumentNullException("argsForPict"); } PairwiseSettings settings = new PairwiseSettings(2, false); foreach (string raw in argsForPict.Split()) { string s = raw.Trim(); if (s.Length == 0) { continue; } if (s[0] != '/') { throw new ArgumentOutOfRangeException("Unrecognized prefix: " + s); } char cmd = Char.ToLower(s[1], PictConstants.Culture); if (s.Length > 3 && s[2] != ':') { throw new ArgumentOutOfRangeException("Unrecognized delimiter: " + s); } PictConstants.Trace(s + " " + s.Length); string extra = s.Length > 2 ? s.Substring(3) : ""; switch (cmd) { case 'o': settings.Order = Int32.Parse(extra, PictConstants.Culture); break; case 'd': settings.delimParameter = GetChar(extra); settings.setValueSeparator = settings.delimParameter + " "; break; case 'a': settings.aliasDelimiter = GetChar(extra); break; case 'n': settings.negativePrefix = GetChar(extra); settings.negativePrefixString = settings.negativePrefix.ToString(PictConstants.Culture); break; case 'c': settings.IsCaseSensitive = true; break; case 'r': settings.RandomizeGeneration = true; if (extra.Length != 0) { settings.RandomSeedSpecified = true; settings.RandomSeed = Int32.Parse(extra); } break; case 'e': settings.seedFile = extra; break; default: throw new ArgumentOutOfRangeException("Unrecognized: " + raw); } } PictConstants.Trace("Parse: {0} -> {1}", argsForPict, settings.GetPictArgs()); return(settings); }
public static string GetPictExecutableFullPath() { if (fullPath == null) { lock (mutex) { if (fullPath == null) { string dir = Environment.CurrentDirectory; //Environment.ExpandEnvironmentVariables(pictDirectory); PictConstants.Trace("Directory = {0}", dir); string arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE").ToLower(PictConstants.Culture).Trim(); string id = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"); PictConstants.Trace("Arch = {0}, ID = {1}", arch, id); string file; if (arch == "ia64") { file = "pict.ia64.exe"; } else if (arch == "amd64") { file = "pict.amd64.exe"; } else if (arch == "x86") { file = "pict.exe"; } else { PictConstants.Trace("Unrecognized arch: {0}, defaulting to pict.exe", arch); file = "pict.exe"; } string combined = Path.Combine(dir, file); PictConstants.Trace("Combined path: {0}", combined); string temp = Path.GetFullPath(combined); PictConstants.Trace("Checking full path: {0}", temp); if (!File.Exists(temp)) { string msg = string.Format("Couldn't find {2} in {0} for processor {1}", dir, arch, file); if (dir != pictDirectory) { msg += "(original directory = " + dir + ")"; } throw new FileNotFoundException(msg, temp); } fullPath = temp; } } } return(fullPath); }