Ejemplo n.º 1
0
        /// <summary>
        /// Run the given stage on a sketch. This method is guaranteed not to modify
        /// the given sketch.
        /// </summary>
        /// <param name="stage">the stage</param>
        /// <param name="sketch">the sketch</param>
        /// <param name="filename">the name of the file the sketch was loaded from</param>
        /// <param name="writeIntermediary">true if intermediate files should be written</param>
        private void runStageOnSketch(ProcessStage stage, Sketch.Sketch sketch, string filename, bool writeIntermediary)
        {
            Sketch.Sketch mutable = new Sketch.Sketch(sketch);
            if (verbose)
            {
                Console.WriteLine("Executing stage " + stage.name);
            }

            stage.run(mutable, filename);

            if (writeIntermediary)
            {
                TextWriter handle;
                if (consoleOutput)
                {
                    handle = Console.Out;
                }
                else
                {
                    handle = new StreamWriter(intermediaryPath + "\\" +
                                              stage.shortname + "_" + fileTimeStamp() + stage.outputFiletype);
                }
                stage.finalize(handle, intermediaryPath);
                if (!consoleOutput)
                {
                    handle.Close();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Run the TestRig
        /// </summary>
        public void run()
        {
            DateTime startTime = DateTime.Now;

            // Start loading all sketches asynchronously
            AsyncProducer <string, Sketch.Sketch> sketches = new AsyncProducer <string, Sketch.Sketch>(files, delegate(string file) { return(loadFile(file)); });

            // Print the files to be loaded
            Console.WriteLine("Files to be processed (" + files.Count + "):");
            foreach (string filename in files)
            {
                Console.WriteLine("    " + filename);
            }

            ProcessStage stage = null;

            foreach (ProcessStage theStage in executionQueue)
            {
                theStage.start();
            }

            for (int i = 0; i < files.Count; i++)
            {
                string Filename = files[i];
                #region EXECUTE STAGE QUEUE

                // Get a local copy of the execution queue to mess with
                Queue <ProcessStage> localExecQueue = new Queue <ProcessStage>(executionQueue);

                string File_path = System.IO.Path.GetDirectoryName(Filename);
                string File_name = System.IO.Path.GetFileNameWithoutExtension(Filename);
                string File_extn = System.IO.Path.GetExtension(Filename);
                if (File_extn == "")
                {
                    ExitWithError(Filename + " does not have an extension.");
                }


                string pointer = "---(" + (i + 1) + "/" + files.Count + ")------------------------> ";
                string name    = File_name + File_extn;
                string top     = "";
                int    len     = pointer.Length + name.Length + 1;
                for (int m = 0; m < len; ++m)
                {
                    top += '_';
                }

                Console.WriteLine(top);
                Console.WriteLine(pointer + File_name + File_extn + " |");
                Console.WriteLine(top + "|");

                sketch = sketches.Consume();

                while (localExecQueue.Count != 0)
                {
                    stage = localExecQueue.Dequeue();
                    bool writeIntermediary = generateIntermediaries && localExecQueue.Count != 0 && i == files.Count - 1;

                    if (catchExceptions)
                    {
                        try
                        {
                            runStageOnSketch(stage, sketch, Filename, writeIntermediary);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            Console.WriteLine(e.StackTrace);
                        }
                    }
                    else
                    {
                        runStageOnSketch(stage, sketch, Filename, writeIntermediary);
                    }
                }

                // Write output
                if (i == files.Count - 1)
                {
                    foreach (ProcessStage outputStage in executionQueue)
                    {
                        TextWriter outHandle  = null;
                        string     outputFile = "";
                        try
                        {
                            if (consoleOutput)
                            {
                                outHandle = Console.Out;
                            }
                            else
                            {
                                if (outputLocations.ContainsKey(outputStage))
                                {
                                    outHandle = outputLocations[outputStage];
                                }
                                else
                                {
                                    outputFile = outputPath + outputStage.shortname +
                                                 "_" + fileTimeStamp();
                                    string outputFileName = outputFile + outputStage.outputFiletype;
                                    if (System.IO.File.Exists(outputFileName))
                                    {
                                        outputFileName = outputFile + "_0" + outputStage.outputFiletype;
                                    }
                                    outHandle = new StreamWriter(outputFileName);
                                    Console.WriteLine("Writing output to file " + outputFileName);
                                }
                            }
                            outputStage.finalize(outHandle, outputPath);
                        }
                        catch (IOException e)
                        {
                            Console.WriteLine(e.Message);
                            Console.WriteLine(e.StackTrace);
                            Console.WriteLine("Due to various errors, there will be no output.");
                        }
                        finally
                        {
                            if (!consoleOutput && outHandle != null)
                            {
                                outHandle.Close();
                            }
                        }
                    }
                }

                #endregion
            }


            Console.WriteLine("------------------------------------");
            DateTime endTime     = DateTime.Now;
            TimeSpan elapsedTime = endTime - startTime;
            Console.WriteLine("Execution took " + elapsedTime.TotalSeconds + "s");

            // Cleanup
            // Allow each stage to do some post-processing (usually only necessary for test stages)
            while (executionQueue.Count != 0)
            {
                ProcessStage current_stage = (ProcessStage)executionQueue.Dequeue();
                current_stage.finalize();
            }

            if (pause)
            {
                Console.WriteLine("Press ENTER to continue...");
                Console.ReadLine();
            }
        }