[chronojump/execute-process: 1/5] Refactors code: creates a new class to execute external processes (named ExecuteProcess).



commit d6b07520851f3c3cb0ac42c27bdfc25f0c2b77d4
Author: Carles Pina i Estany <carles pina cat>
Date:   Tue Oct 4 20:07:17 2016 +0200

    Refactors code: creates a new class to execute external processes (named ExecuteProcess).
    
    ChronojumpImporter uses it.

 chronojump.csproj         |    1 +
 po/POTFILES.in            |    1 +
 src/Makefile.am           |    1 +
 src/chronojumpImporter.cs |   68 ++++++++---------------------
 src/executeProcess.cs     |  104 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 126 insertions(+), 49 deletions(-)
---
diff --git a/chronojump.csproj b/chronojump.csproj
index d88c065..7aa9c13 100644
--- a/chronojump.csproj
+++ b/chronojump.csproj
@@ -883,6 +883,7 @@
     <Compile Include="src\sqlite\chronopicRegister.cs" />
     <Compile Include="src\chronojumpImporter.cs" />
     <Compile Include="src\staticClassState.cs" />
+    <Compile Include="src\executeProcess.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="src\" />
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4a2bc35..0ad3e83 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -54,6 +54,7 @@ src/gui/runType.cs
 src/gui/server.cs
 src/gui/session.cs
 src/gui/stats.cs
+src/executeProcess.cs
 src/json.cs
 src/jumpType.cs
 src/Mini/chronojump_mini.cs
diff --git a/src/Makefile.am b/src/Makefile.am
index 175960c..e7e0fee 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -181,6 +181,7 @@ SOURCES = \
        chronopicDetect.cs\
        chronopicRegister.cs\
        chronojumpImporter.cs\
+       executeProcess.cs\
        oldCodeNeedToDBConvert/person.cs\
        oldCodeNeedToDBConvert/personSession.cs\
        oldCodeNeedToDBConvert/sqlite/person.cs\
diff --git a/src/chronojumpImporter.cs b/src/chronojumpImporter.cs
index bff9a54..68bbff8 100644
--- a/src/chronojumpImporter.cs
+++ b/src/chronojumpImporter.cs
@@ -97,11 +97,11 @@ class ChronojumpImporter
 
                List<string> parameters = new List<string> ();
                parameters.Add ("--source");
-               parameters.Add (CommandLineEncoder.EncodeArgText (sourceFile));
+               parameters.Add (sourceFile));
                parameters.Add ("--destination");
-               parameters.Add (CommandLineEncoder.EncodeArgText (destinationFile));
+               parameters.Add (destinationFile);
                parameters.Add ("--source_session");
-               parameters.Add (CommandLineEncoder.EncodeArgText (session));
+               parameters.Add (session);
 
                Result result = executeChronojumpImporter (parameters);
 
@@ -167,55 +167,25 @@ class ChronojumpImporter
                        importer_script_path = CommandLineEncoder.EncodeArgText (System.IO.Path.Combine 
(Util.GetPrefixDir (), "bin/chronojump_importer.py"));
                }
 
-               Process process = new Process();
-               ProcessStartInfo processStartInfo;
-
-               processStartInfo = new ProcessStartInfo();
-
-               processStartInfo.Arguments = importer_script_path + " " + string.Join (" ", parameters);
-               processStartInfo.FileName = importer_executable;
-
-               LogB.Debug ("chronojump-importer fileName: " + processStartInfo.FileName);
-               LogB.Debug ("chronojump-importer Arguments: " + processStartInfo.Arguments);
-
-               processStartInfo.CreateNoWindow = true;
-               processStartInfo.UseShellExecute = false;
-               processStartInfo.RedirectStandardInput = false;
-               processStartInfo.RedirectStandardError = true;
-               processStartInfo.RedirectStandardOutput = true;
-
-               process.StartInfo = processStartInfo;
-
-               try {
-                       process.Start();
-               }
-               catch(Exception e) {
-                       string errorMessage = String.Format (Catalog.GetString("Cannot start:\n" +
-                                                                              "{0}\n" +
-                                                                              "with the parameters:" +
-                                                                              "{1}\n" +
-                                                                              "Exception:\n" +
-                                                                              "{2}"),
-                                                            processStartInfo.FileName, 
processStartInfo.Arguments, e.Message);
-                       return new Result (false, "", errorMessage);
-               }
-
-               string allOutput = "";
-               allOutput += process.StandardOutput.ReadToEnd();
-               allOutput += process.StandardError.ReadToEnd();
-
-               process.WaitForExit ();
-
-               if (process.ExitCode != 0) {
-                       string errorMessage = String.Format (Catalog.GetString("Error executing: {0}\n" +
-                                                                              "with the parameters: {1}\n" +
-                                                                              "output: {2}"),
-                                                            processStartInfo.FileName, 
processStartInfo.Arguments, allOutput);
+               parameters.Insert (0, importer_script_path);
+               ExecuteProcess.Result execute_result = ExecuteProcess.run (importer_executable, parameters);
 
+               if (execute_result.exitCode != 0) {
                        // Python interpretar was executed but the Python file wasn't found or the script 
failed
-                       return new Result (false, allOutput, errorMessage);
+                       string errorMessage = "";
+
+                       if (execute_result.errorMessage == "") {
+                               // The Python script has been executed and failed (syntax error, crashed).
+                               // The error message will be in the output:
+                               errorMessage = execute_result.allOutput;
+                       } else {
+                               // The Python script has not been executed, return the error message from 
ExecuteProcess
+                               errorMessage = execute_result.errorMessage;
+                       }
+                       return new Result (false, execute_result.allOutput, errorMessage);
                }
 
-               return new Result (true, allOutput);
+               // All good, returns the output
+               return new Result (true, execute_result.allOutput);
        }
 }
\ No newline at end of file
diff --git a/src/executeProcess.cs b/src/executeProcess.cs
new file mode 100644
index 0000000..a276210
--- /dev/null
+++ b/src/executeProcess.cs
@@ -0,0 +1,104 @@
+using System.Collections.Generic;
+using System.Diagnostics;
+using System;
+using Mono.Unix;
+
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or   
+ * (at your option) any later version.
+ *    
+ * ChronoJump is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2016   Carles Pina i Estany <carles pina cat>
+ */
+
+/* This class executes a process and returns the result. */
+class ExecuteProcess
+{
+       public struct Result
+       {
+               // Encapsulates the result of a run: stdout, stderr, exitCode (-1 if process couldn't start),
+               // success (if exitCode != 0) and errorMessage to be shown to the user.
+               public string stdout;
+               public string stderr;
+               public string allOutput;
+
+               public int exitCode;
+               public bool success;
+               public string errorMessage;
+
+               public Result(string stdout, string stderr, int exitCode, string errorMessage = "")
+               {
+                       this.stdout = stdout;
+                       this.stderr = stderr;
+                       this.allOutput = stdout + stderr;
+                       this.exitCode = exitCode;
+                       this.success = (exitCode == 0);
+                       this.errorMessage = errorMessage;
+               }
+       };
+
+       // Executes file_name without creating a Window and without using the shell
+       // with the parameters. Waits that it finishes it. Returns the stdout and stderr.
+       public static Result run(string file_name, List<string> parameters)
+       {
+               Process process = new Process();
+               ProcessStartInfo processStartInfo = new ProcessStartInfo();
+
+               processStartInfo.FileName = file_name;
+
+               string parameters_string = "";
+               foreach (string parameter in parameters)
+               {
+                       parameters_string += CommandLineEncoder.EncodeArgText (parameter) + " ";
+               }
+
+               processStartInfo.Arguments = parameters_string;
+
+               LogB.Debug ("ExecuteProcess FileName: " + processStartInfo.FileName);
+               LogB.Debug ("ExecuteProcess Arguments: " + processStartInfo.Arguments);
+
+               processStartInfo.CreateNoWindow = true;
+               processStartInfo.UseShellExecute = false;
+               processStartInfo.RedirectStandardInput = false;
+               processStartInfo.RedirectStandardError = true;
+               processStartInfo.RedirectStandardOutput = true;
+
+               process.StartInfo = processStartInfo;
+
+               try {
+                       process.Start();
+               }
+               catch(Exception e) {
+                       string errorMessage = String.Format (Catalog.GetString("Cannot start:\n" +
+                                                                              "{0}\n" +
+                                                                              "with the parameters:" +
+                                                                              "{1}\n" +
+                                                                              "Exception:\n" +
+                                                                              "{2}"),
+                                                            processStartInfo.FileName, parameters_string, 
e.Message);
+                       LogB.Warning (errorMessage);
+                       return new Result ("", "", -1, errorMessage);
+               }
+
+               string stdout = process.StandardOutput.ReadToEnd();
+               string stderr = process.StandardError.ReadToEnd();
+
+               process.WaitForExit ();
+
+               int exitCode = process.ExitCode;
+
+               return new Result (stdout, stderr, exitCode);
+       }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]