[chronojump/chronojump-importer] Handles commandLineEncoding arguments correctly (in case of spaces, double quotes, etc.).
- From: Carles Pina i Estany <carlespina src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump/chronojump-importer] Handles commandLineEncoding arguments correctly (in case of spaces, double quotes, etc.).
- Date: Mon, 26 Sep 2016 23:16:47 +0000 (UTC)
commit 80ca67c7f58b681c872beba5b8f502a44ba04147
Author: Carles Pina i Estany <carles pina cat>
Date: Tue Sep 27 01:15:57 2016 +0200
Handles commandLineEncoding arguments correctly (in case of spaces, double quotes, etc.).
Executes the correctly file on Windows.
chronojump.csproj | 1 +
src/Makefile.am | 1 +
src/commandLineEncoder.cs | 102 +++++++++++++++++++++++++++++++++++++++++++++
src/gui/chronojump.cs | 14 ++++--
src/utilEncoder.cs | 10 ----
5 files changed, 114 insertions(+), 14 deletions(-)
---
diff --git a/chronojump.csproj b/chronojump.csproj
index c100701..35d6f31 100644
--- a/chronojump.csproj
+++ b/chronojump.csproj
@@ -1039,6 +1039,7 @@
<Compile Include="src\gui\chronopicWizard.cs" />
<Compile Include="src\utilMath.cs" />
<Compile Include="src\gui\old\chronojumpServerOld.cs" />
+ <Compile Include="src\commandLineEncoder.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
diff --git a/src/Makefile.am b/src/Makefile.am
index d66d568..011132a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -165,6 +165,7 @@ SOURCES = \
utilMath.cs\
utilVideo.cs\
constants.cs\
+ commandLineEncoder.cs\
report.cs\
sport.cs\
log.cs\
diff --git a/src/commandLineEncoder.cs b/src/commandLineEncoder.cs
new file mode 100644
index 0000000..851c200
--- /dev/null
+++ b/src/commandLineEncoder.cs
@@ -0,0 +1,102 @@
+// This file is copied and adapted from
https://raw.githubusercontent.com/ericpopivker/Command-Line-Encoder/master/CommandLineEncoder/CommandLineEncoder/Utils.cs
+
+using System;
+using System.Text.RegularExpressions;
+
+public class CommandLineEncoder
+{
+ private const string EscapedSlashN = "[SlashN]";
+
+ public static string EncodeArgText(string original)
+ {
+ var result = original;
+ result = TryEncodeNewLine(result);
+ result = TryEncodeSlashesFollowedByQuotes(result);
+ result = TryEncodeQuotes(result);
+ result = TryEncodeLastSlash(result);
+ return result;
+ }
+
+ private static string TryEncodeNewLine(string original)
+ {
+ var result = original.Replace("\\n", EscapedSlashN);
+
+ result = result.Replace(Environment.NewLine, "\\n");
+ return result;
+ }
+
+ private static string TryEncodeSlashesFollowedByQuotes(string original)
+ {
+ var regexPattern = @"\\+""";
+
+ string result = Regex.Replace(original, regexPattern,
+ delegate(Match match)
+ {
+ string matchText = match.ToString();
+ string justSlashes = matchText.Remove(matchText.Length - 1);
+ return justSlashes + justSlashes + "\""; //double up the slashes
+ });
+
+ return result;
+ }
+
+ private static string TryEncodeQuotes(string original)
+ {
+ var result = original.Replace("\"", "\"\"");
+ return result;
+ }
+
+ private static string TryEncodeLastSlash(string original)
+ {
+ var regexPattern = @"\\+$";
+
+ string result = Regex.Replace(original, regexPattern,
+ delegate(Match match)
+ {
+ string matchText = match.ToString();
+ return matchText + matchText; //double up the slashes
+ });
+
+ return result;
+ }
+
+ public static string DecodeArgText(string original)
+ {
+ string decoded = original;
+
+ decoded = TryDecodeNewLine(decoded);
+
+ return decoded;
+ }
+
+
+ private static string TryDecodeNewLine(string original)
+ {
+ var result = original.Replace("\\n", Environment.NewLine);
+
+ result = result.Replace(EscapedSlashN, "\\n");
+
+ return result;
+ }
+
+
+
+ public static string EscapeBackSlashes(string text)
+ {
+ var regexPattern = "\\\\";
+ var result = text;
+ var regex = new Regex(regexPattern);
+ var matches = regex.Matches(text);
+
+ for (int i = matches.Count - 1; i >= 0; i--)
+ {
+ var match = matches[i];
+ var index = match.Index + match.Length;
+
+ if (index >= text.Length || text[index] == '\\')
+ result = result.Insert(match.Index, "\\");
+ }
+
+ return result;
+ }
+}
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 69f70df..e01aae0 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2405,11 +2405,16 @@ public partial class ChronoJumpWindow
private void ImportSessionFromDatabase(string databasePath, int sessionNumber)
{
- string pythonExecutable = UtilEncoder.GetPythonExecutable ();
string source_filename = databasePath;
string destination_filename = Sqlite.DatabaseFilePath;
string session = Convert.ToString (sessionNumber);
- string importer_executable = System.IO.Path.Combine (Util.GetPrefixDir (), "bin" +
Path.DirectorySeparatorChar + "chronojump_importer.py");
+ string importer_executable;
+
+ if (UtilAll.IsWindows()) {
+ importer_executable = System.IO.Path.Combine (Util.GetPrefixDir (),
"bin\\chronojump-importer\\chronojump_importer.py");
+ } else {
+ importer_executable = System.IO.Path.Combine (Util.GetPrefixDir (), "bin" +
Path.DirectorySeparatorChar + "chronojump_importer.py");
+ }
Process process = new Process();
ProcessStartInfo processStartInfo;
@@ -2418,8 +2423,8 @@ public partial class ChronoJumpWindow
// TODO: use this to escape the arguments:
https://github.com/ericpopivker/Command-Line-Encoder/blob/master/CommandLineEncoder/CommandLineEncoder/Utils.cs
// Otherwise source_filename with double quotes, spaces, etc. wouldn't work
- processStartInfo.Arguments = importer_executable + " --source " + source_filename + "
--destination " + destination_filename + " --source_session " + session;
- processStartInfo.FileName = pythonExecutable;
+ processStartInfo.Arguments = " --source " + CommandLineEncoder.EncodeArgText
(source_filename) + " --destination " + CommandLineEncoder.EncodeArgText (destination_filename) + "
--source_session " + CommandLineEncoder.EncodeArgText (session);
+ processStartInfo.FileName = importer_executable;
LogB.Debug ("chronojump-importer fileName:" + processStartInfo.FileName);
LogB.Debug ("chronojump-importer Arguments:" + processStartInfo.Arguments);
@@ -2464,6 +2469,7 @@ public partial class ChronoJumpWindow
process.WaitForExit ();
updateComboStats ();
+ new DialogMessage (Constants.MessageTypes.INFO, Catalog.GetString ("Session imported"));
}
private void on_open_activate (object o, EventArgs args)
diff --git a/src/utilEncoder.cs b/src/utilEncoder.cs
index df161bb..9cca602 100644
--- a/src/utilEncoder.cs
+++ b/src/utilEncoder.cs
@@ -177,16 +177,6 @@ public class UtilEncoder
LogB.Error(Constants.FileNotFound);
return false;
}
-
- public static string GetPythonExecutable() {
- if (UtilAll.IsWindows ()) {
- //on Windows we need the \"str\" to call without problems in path with spaces
- return "\"" + System.IO.Path.Combine(Util.GetPrefixDir(), "python3" +
Path.DirectorySeparatorChar + "python.exe") + "\"";
- } else {
- // We assume that it's installed in the PATH and that we don't care if it's Python2
or Python3
- return "python";
- }
- }
/*
private static string getEncoderScriptCapturePython() {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]