예제 #1
0
        /// =======================================================================================================
        public static bool Run(string auto_alias, string binary_name, string number = null, bool new_form = false)
        {
            int        commandnumber = Convert.ToInt32(number);
            int        batchnumber   = 0;                          // Default
            AutoReport autoreport    = new AutoReport(auto_alias); // Always something to report

            autoreport.Enabled = (bool)FormAccess(FormDelegates.ReportEnabled, null, auto_alias);
            List <Control> interactivecontrols = new List <Control>();

            if (!Running) // Not allowed to run while running
            {
                string autovars = (string)FormAccess(FormDelegates.GetAutoVars, null, auto_alias);
                if (!autovars.Contains(COMMAND_MODE_DIRTY_RUN))
                {
                    CleanRun(auto_alias);
                }
                AutoTasks    = new List <Task>();
                BatchTasks   = new List <Task>();
                CommandTasks = new List <Task>();
                Running      = true;
                RunMode      = RunType.Command;
                string       binary_path = DISK.AutoAddBaseDirectory(auto_alias + @"\" + binary_name);
                DataGridView batchdata   = Data.BlankDgv((int)CAT.BatchFields.NumberOfColumns);
                DataGridView autodata    = Data.BlankDgv((int)CAT.AutoFields.NumberOfColumns);
                if (File.Exists(binary_path))
                {
                    if (Regex.IsMatch(binary_path, "auto.bin"))
                    {
                        Data.LoadDataGrid(binary_path, ref autodata);
                        RunMode = number == null ? RunType.Auto : RunType.Batch;
                        if (RunMode == RunType.Batch)
                        {
                            Data.LoadDataGrid(new FileInfo(binary_path).Directory.FullName + @"\batch" + number + ".bin", ref batchdata);
                        }
                    }
                    else if (Regex.IsMatch(binary_path, @"batch\d*.bin"))
                    {
                        Data.LoadDataGrid(new FileInfo(binary_path).Directory.FullName + @"\auto.bin", ref autodata);
                        RunMode = number == null ? RunType.Batch : RunType.Command;
                        Data.LoadDataGrid(binary_path, ref batchdata);
                        string subvalue = Regex.Match(binary_path, @"batch\d*.bin").Value;
                        string newvalue = Regex.Match(subvalue, @"[0-9]+").Value;
                        batchnumber = Convert.ToInt32(newvalue);
                    }
                    else
                    {
                        Running = false; throw new Exception("Binary " + binary_path + " is invalid - thus did not start automation");
                    }
                }
                else
                {
                    Running = false; throw new Exception("Binary " + binary_path + " does not exist - thus did not start automation");
                }
                switch (RunMode)
                {
                case RunType.Command:
                    if (Regex.IsMatch(Data.GetCell(batchdata, (int)CAT.BatchFields.Mode, commandnumber), "-nr"))
                    {
                        autoreport.Enabled = false;
                    }
                    autoreport.AddBatch(new BatchReport(batchnumber, Data.GetCell(autodata, (int)CAT.AutoFields.Name, batchnumber)));
                    RunCommand(autoreport, new Map(auto_alias, batchnumber, commandnumber), batchdata, out interactivecontrols, GetAutoVars(autodata, batchnumber, auto_alias));
                    autoreport.WaitForBatch(batchnumber);
                    Running = false;
                    break;

                case RunType.Batch:
                    if (Regex.IsMatch(Data.GetCell(autodata, (int)CAT.AutoFields.Mode, batchnumber), "-nr"))
                    {
                        autoreport.Enabled = false;
                    }
                    RunBatch(autoreport, auto_alias, batchnumber, autodata, batchdata, out interactivecontrols);
                    Running = false;
                    break;

                case RunType.Auto:
                    RunAuto(autoreport, auto_alias, binary_name, out interactivecontrols);
                    Running = false;
                    break;
                }
                autoreport.WaitForAuto();
                if (autoreport.Enabled)
                {
                    ShowReport(autoreport);
                }
                if (interactivecontrols != null && interactivecontrols.Count > 0)
                {
                    InteractiveShowControls(interactivecontrols);
                }
                return(true);
            }
            else
            {
                autoreport.AddOverlay("Busy running automation - thus did not start new automation - please wait (or click abort then retry run)...", Status.BUSY);
                return(false);
            }
        }
예제 #2
0
        /// =======================================================================================================
        // String based command line executer. Return all the standard output text. Ignore error text, as this isn't considered important (this is not a debug application).
        public static Status RunCmd(AutoReport auto_report,
                                    Map map,
                                    string fileName       = "cmd.exe",
                                    string path           = @"C:\",
                                    string command        = "",
                                    int timeout           = 120,
                                    string commandPrefix  = "/C",
                                    bool use_shell        = false,
                                    bool redirect_std_out = true,
                                    bool redirect_std_in  = false,
                                    bool redirect_std_err = false,
                                    bool nowindow         = true)
        {
            DateTime starttime = DateTime.Now;

            if (!CAT.AbortAll)
            {
                try { if (string.IsNullOrWhiteSpace(path) && Directory.Exists(command))
                      {
                          command = @"%SystemRoot%\explorer.exe " + command; auto_report.AddOverlayedMessage(map, "Applied assumption: " + command, Status.WARN);
                      }
                } catch { }
                path = DISK.AutoAddBaseDirectory(path);
                if (!Directory.Exists(path))
                {
                    if (auto_report != null)
                    {
                        auto_report.AddOverlayedMessage(map, "Path '" + path + "' is invalid", Status.FAIL);
                    }
                }
                Process process = new Process();
                try
                {
                    ProcessStartInfo processinfo = new ProcessStartInfo()
                    {
                        CreateNoWindow         = nowindow,
                        UseShellExecute        = use_shell,
                        RedirectStandardOutput = redirect_std_out,
                        RedirectStandardError  = redirect_std_err,
                        RedirectStandardInput  = redirect_std_in,
                        WorkingDirectory       = path,
                        WindowStyle            = ProcessWindowStyle.Normal,
                        FileName  = fileName,
                        Arguments = commandPrefix + command
                    };
                    string alias = path + ">" + command;
                    process.StartInfo = processinfo;
                    process.Start();
                    string standardoutputtext = "";
                    string overlayname        = "standardOutput" + map.A + map.B + map.C;
                    if (auto_report != null)
                    {
                        auto_report.AddOverlay(overlayname, "", Status.INFO);
                    }
                    while (!process.StandardOutput.EndOfStream)
                    {
                        string line = process.StandardOutput.ReadLine();
                        standardoutputtext += line + "\r\n";
                        if (auto_report != null)
                        {
                            auto_report.UpdateOverlay(overlayname, standardoutputtext, Status.INFO); auto_report.UpdateCommandProgress(map);
                        }
                        if (CAT.AbortAll || CAT.AbortCurrent)
                        {
                            CAT.AbortCurrent = false;
                            process.Close();
                            if (auto_report != null)
                            {
                                auto_report.AddOverlayedMessage(map, "User aborted after " + (DateTime.Now - starttime).TotalSeconds + "s", Status.ABORT);
                            }
                            return(Status.ABORT);
                        }
                        Thread.Sleep(10);
                        if ((DateTime.Now - starttime).TotalMilliseconds > (timeout * 1000))
                        {
                            process.Close();
                            if (auto_report != null)
                            {
                                auto_report.AddOverlayedMessage(map, "Timeout occured (timeout = " + timeout + "s)", Status.TIMEOUT);
                            }
                            return(Status.TIMEOUT);
                        }
                    }
                    if (auto_report != null)
                    {
                        auto_report.AddMessage(map, standardoutputtext, Status.INFO);
                    }
                    process.Close();
                    return(Status.PASS);
                }
                catch (Exception e)
                {
                    try { process.Close(); } catch { }
                    if (auto_report != null)
                    {
                        auto_report.AddOverlayedMessage(map, ErrorMsg(e), Status.FAIL);
                    }
                }
            }
            else
            {
                return(Status.ABORT);
            }
            return(Status.PASS);
        }