Skip to content

Commit

Permalink
Merge pull request #100 from FherStk:v2.20.1
Browse files Browse the repository at this point in the history
V2.20.1
  • Loading branch information
FherStk authored Feb 3, 2022
2 parents 7dd68d1 + 24eda8c commit 153de7d
Show file tree
Hide file tree
Showing 136 changed files with 1,521 additions and 1,401 deletions.
10 changes: 5 additions & 5 deletions cli/Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static bool Update(Output output){
output.Indent();

output.Write("Retrieving the list of changes... ");
var result = shell.RunCommand("git remote update");
var result = shell.Run("git remote update");
if(result.code == 0) output.WriteResponse(new List<string>());
else
{
Expand All @@ -156,7 +156,7 @@ private static bool Update(Output output){
}

output.Write("Looking for new versions... ");
result = shell.RunCommand((Core.Utils.CurrentOS == Utils.OS.WIN ? "set LC_ALL=C.UTF-8 & git status -uno" : "LC_ALL=C git status -uno"));
result = shell.Run((Core.Utils.CurrentOS == Utils.OS.WIN ? "set LC_ALL=C.UTF-8 & git status -uno" : "LC_ALL=C git status -uno"));
if(result.code == 0) output.WriteResponse(new List<string>());
else{
output.WriteResponse(result.response);
Expand Down Expand Up @@ -184,7 +184,7 @@ private static bool Update(Output output){
output.Indent();

output.Write("Updating local database... ");
result = shell.RunCommand("git fetch --all");
result = shell.Run("git fetch --all");
if(result.code == 0) output.WriteResponse(new List<string>());
else
{
Expand All @@ -193,7 +193,7 @@ private static bool Update(Output output){
}

output.Write("Removing local changes... ");
result = shell.RunCommand("git reset --hard origin/master");
result = shell.Run("git reset --hard origin/master");
if(result.code == 0) output.WriteResponse(new List<string>());
else
{
Expand All @@ -202,7 +202,7 @@ private static bool Update(Output output){
}

output.Write("Downloading updates... ");
result = shell.RunCommand("git pull");
result = shell.Run("git pull");
if(result.code == 0) output.WriteResponse(new List<string>());
else
{
Expand Down
2 changes: 1 addition & 1 deletion core/AutoCheck.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Authors>Fernando Porrino Serrano</Authors>
<Product>AutoCheck.Core</Product>
<Copyright>Copyright © 2022</Copyright>
<VersionPrefix>2.20.0</VersionPrefix>
<VersionPrefix>2.20.1</VersionPrefix>
<VersionSuffix>stable</VersionSuffix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<AssemblyFileVersion>$(AssemblyVersion)</AssemblyFileVersion>
Expand Down
12 changes: 6 additions & 6 deletions core/connectors/Postgres.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@ public void CreateDataBase(bool replace = false)
{
//Once path is ok on windows and unix the almost same code will be used.
case Utils.OS.WIN:
var win = ls.RunCommand($"SET \"{cmdPassword}\" && {cmdCreate}", this.BinPath);
var win = ls.Run($"SET \"{cmdPassword}\" && {cmdCreate}", this.BinPath);
if(win.code > 0) throw new Exception(win.response.Replace("\n", ""));
break;

case Utils.OS.MAC:
case Utils.OS.GNU:
var gnu = ls.RunCommand($"{cmdPassword} {cmdCreate}");
var gnu = ls.Run($"{cmdPassword} {cmdCreate}");
if(gnu.code > 0) throw new Exception(gnu.response.Replace("\n", ""));
break;
}
Expand All @@ -276,14 +276,14 @@ public void ImportSqlDump(string dumpPath)
{
//Once path is ok on windows and unix the almost same code will be used.
case Utils.OS.WIN:
var win = ls.RunCommand($"SET \"{cmdPassword}\" && {cmdRestore}", this.BinPath);
var win = ls.Run($"SET \"{cmdPassword}\" && {cmdRestore}", this.BinPath);
if(win.code > 0) throw new Exception(win.response.Replace("\n", ""));

break;

case Utils.OS.MAC:
case Utils.OS.GNU:
var gnu = ls.RunCommand($"{cmdPassword} {cmdRestore.Replace("\"", "'")}");
var gnu = ls.Run($"{cmdPassword} {cmdRestore}");
if(gnu.code > 0) throw new Exception(gnu.response.Replace("\n", ""));
break;
}
Expand All @@ -309,13 +309,13 @@ public void DropDataBase()
{
//Once path is ok on windows and unix the almost same code will be used.
case Utils.OS.WIN:
var win = ls.RunCommand($"SET \"{cmdPassword}\" && {cmdDrop}", this.BinPath);
var win = ls.Run($"SET \"{cmdPassword}\" && {cmdDrop}", this.BinPath);
if(win.code > 0) throw new Exception(win.response.Replace("\n", ""));
break;

case Utils.OS.MAC:
case Utils.OS.GNU:
var gnu = ls.RunCommand($"{cmdPassword} {cmdDrop}");
var gnu = ls.Run($"{cmdPassword} {cmdDrop}");
if(gnu.code > 0) throw new Exception(gnu.response.Replace("\n", ""));
break;
}
Expand Down
123 changes: 110 additions & 13 deletions core/connectors/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ You should have received a copy of the GNU Affero General Public License
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using AutoCheck.Core.Exceptions;
using Renci.SshNet;
using ToolBox.Bridge;
Expand All @@ -32,8 +33,7 @@ namespace AutoCheck.Core.Connectors{
/// <summary>
/// Allows in/out operations and/or data validations with a local (bash) or remote computer (like ssh, scp, etc.).
/// </summary>
public class Shell : Base{

public class Shell : Base{
/// <summary>
/// The remote host OS.
/// </summary>
Expand Down Expand Up @@ -165,8 +165,8 @@ public void TestConnection(){
/// <param name="command">The command to run.</param>
/// <param name="timeout">Timeout in milliseconds, 0 for no timeout.</param>
/// <returns>The return code and the complete response.</returns>
public (int code, string response) RunCommand(string command, int timeout=0){
return RunCommand(command, "", timeout);
public (int code, string response) Run(string command, int timeout=0){
return Run(command, "", timeout);
}

/// <summary>
Expand All @@ -176,14 +176,19 @@ public void TestConnection(){
/// <param name="path">The path where the command must run.</param>
/// <param name="timeout">Timeout in milliseconds, 0 for no timeout.</param>
/// <returns>The return code and the complete response.</returns>
public (int code, string response) RunCommand(string command, string path, int timeout=0){
public (int code, string response) Run(string command, string path, int timeout=0){
var result = (IsLocal ? RunLocal(command, path, timeout) : RunRemote(command, path, timeout));
return (result.exitCode, (string.IsNullOrEmpty(result.stdErr.Trim(Environment.NewLine.ToArray())) ? result.stdOut : result.stdErr));

/*
//source: https://docs.microsoft.com/es-es/dotnet/standard/parallel-programming/how-to-cancel-a-task-and-its-children
using (var tokenSource = new CancellationTokenSource()){
var cancelToken = tokenSource.Token;
var exitCode = 0;
var stdOut = string.Empty;
var stdErr = string.Empty;
var task = Task.Run(() => {
if(IsLocal){
Response r = LocalShell.Term(command, ToolBox.Bridge.Output.Hidden, path);
Expand All @@ -193,6 +198,7 @@ public void TestConnection(){
}
else{
this.RemoteShell.Connect();
if(timeout > 0) this.RemoteShell.ConnectionInfo.Timeout = TimeSpan.FromMilliseconds(timeout);
SshCommand s = this.RemoteShell.RunCommand(command);
this.RemoteShell.Disconnect();
Expand All @@ -205,17 +211,108 @@ public void TestConnection(){
}, cancelToken);
var completed = true;
if(timeout == 0) task.Wait();
else task.Wait(timeout);
else task.Wait(timeout, cancelToken);
if(task.Status == TaskStatus.Running){
//timeout
if(completed) return task.Result;
else{
//TODO: this cancels anything so background processes will continue working...
// I need a timeout for Term (has not) and RunCommand (pending to check) in order to cancel de task
// If can't, maybe native process execution should be performed: https://docs.microsoft.com/es-es/dotnet/api/system.diagnostics.process.start?view=net-6.0
// https://docs.microsoft.com/es-es/dotnet/api/system.diagnostics.processwindowstyle?view=net-6.0#System_Diagnostics_ProcessWindowStyle_Hidden/
tokenSource.Cancel();
throw new TimeoutException();
}
else return task.Result;
}
}
}
*/
}

private (int exitCode, string stdOut, string stdErr) RunRemote(string command, string path, int timeout=0){
if(!string.IsNullOrEmpty(path)) throw new NotImplementedException("Sorry");

this.RemoteShell.Connect();
if(timeout > 0) this.RemoteShell.ConnectionInfo.Timeout = TimeSpan.FromMilliseconds(timeout);
var rr = this.RemoteShell.RunCommand(command);
this.RemoteShell.Disconnect();

return (rr.ExitStatus, rr.Result, rr.Error);
}

private (int exitCode, string stdOut, string stdErr) RunLocal(string command, string path, int timeout=0){
//splitting command and argument list
var arguments = command;

switch(Utils.CurrentOS){
case Utils.OS.GNU:
case Utils.OS.MAC:
command = "bash";
arguments = $"-c \"{arguments}\"";
break;

case Utils.OS.WIN:
command = "cmd.exe";
arguments = $"/C \"{arguments}\"";
break;

}

var psi = new ProcessStartInfo(command, arguments) {
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false
};
if(!string.IsNullOrEmpty(path)) psi.WorkingDirectory = path;

//setting up return data
string stdOut = string.Empty;
string stdErr = string.Empty;
int exitCode = 0;

Process proc = null;
try{
//running in parallel in order to kill after timeout
var task = Task.Run(() => {
//Source: https://docs.microsoft.com/es-es/dotnet/api/system.diagnostics.processstartinfo?view=net-6.0
try{
proc = Process.Start(psi); //throws exception for unexisting commands
if (proc == null) throw new Exception("Unable to execute the given local command.");

using (var sr = proc.StandardOutput)
if (!sr.EndOfStream) stdOut = sr.ReadToEnd();

using (var sr = proc.StandardError)
if (!sr.EndOfStream) stdErr = sr.ReadToEnd();

//Must wait after everything has been read, otherwise could deadlock with large outputs
proc.WaitForExit();
exitCode = proc.ExitCode;
}
catch(Exception ex){
exitCode = 127;
stdErr = ex.Message;
}

});

var completed = true;
if(timeout == 0) task.Wait();
else completed = task.Wait(timeout);

if(completed) return (exitCode, stdOut, stdErr);
else throw new TimeoutException();
}
finally{
//process must end always
if (proc != null && !proc.HasExited) proc.Kill();
}
}





/// <summary>
/// Returns the first folder's path found, using the given folder name or search pattern.
Expand Down Expand Up @@ -468,13 +565,13 @@ public string DownloadFolder(string path, string folder=null, bool recursive=fal
{
case Utils.OS.WIN:
//TODO: must be tested!
var win = RunCommand($"dir \"{path}\" /AD /b /s");
var win = Run($"dir \"{path}\" /AD /b /s");
items = win.response.Split("\r\n");
break;

case Utils.OS.MAC:
case Utils.OS.GNU:
var gnu = RunCommand($"find '{path}' -mindepth 1 {(recursive ? "" : "-maxdepth 1")} -name '{item}' -type {(folder ? 'd' : 'f')} 2>&-");
var gnu = Run($"find '{path}' -mindepth 1 {(recursive ? "" : "-maxdepth 1")} -name '{item}' -type {(folder ? 'd' : 'f')} 2>&-");
items = gnu.response.Split("\n").Where(x => !string.IsNullOrEmpty(x)).ToArray();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion core/copy/SourceCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override void Compare(){
try{
//Setting up execution
var lang = Path.GetExtension(FilePattern).TrimStart('.');
var result = shell.RunCommand($"java -jar jplag-3.0.0-jar-with-dependencies.jar -c parallel -n -1 -r \"{output}\" -l {lang} \"{path}\"", Utils.UtilsFolder);
var result = shell.Run($"java -jar jplag-3.0.0-jar-with-dependencies.jar -c parallel -n -1 -r \"{output}\" -l {lang} \"{path}\"", Utils.UtilsFolder);

//Parsing result (JPlag creates a CSV file with the output data)
var csv = new Connectors.Csv(Path.Combine(output, "matches_avg.csv"), ';', null, false);
Expand Down
2 changes: 1 addition & 1 deletion core/main/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ private void ForEachRemoteTarget(Remote[] remote, Action<OS, string, string, str
if(!arguments.ContainsKey("path")) arguments.Add("path", string.Empty);
if(!arguments.ContainsKey("timeout")) arguments.Add("timeout", timeout);
arguments.Add("command", command);
command = "RunCommand";
command = "Run";
}

//Retry the execution
Expand Down
4 changes: 2 additions & 2 deletions docs/api/.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@
"AutoCheck.Core.Connectors.Shell.Password": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.Port": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.RemoteOS": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.RunCommand(System.String,System.Int32)": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.RunCommand(System.String,System.String,System.Int32)": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.Run(System.String,System.Int32)": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.Run(System.String,System.String,System.Int32)": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.TestConnection": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.Shell.Username": "AutoCheck.Core.Connectors.Shell.yml",
"AutoCheck.Core.Connectors.TextStream": "AutoCheck.Core.Connectors.TextStream.yml",
Expand Down
12 changes: 6 additions & 6 deletions docs/api/AutoCheck.Core.Connectors.Atom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ items:
source:
remote:
path: core/connectors/Atom.cs
branch: v2.20.0
branch: v2.20.1
repo: https://github.com/FherStk/AutoCheck.git
id: Atom
path: ../core/connectors/Atom.cs
Expand Down Expand Up @@ -74,7 +74,7 @@ items:
source:
remote:
path: core/connectors/Atom.cs
branch: v2.20.0
branch: v2.20.1
repo: https://github.com/FherStk/AutoCheck.git
id: .ctor
path: ../core/connectors/Atom.cs
Expand Down Expand Up @@ -110,7 +110,7 @@ items:
source:
remote:
path: core/connectors/Atom.cs
branch: v2.20.0
branch: v2.20.1
repo: https://github.com/FherStk/AutoCheck.git
id: .ctor
path: ../core/connectors/Atom.cs
Expand Down Expand Up @@ -153,7 +153,7 @@ items:
source:
remote:
path: core/connectors/Atom.cs
branch: v2.20.0
branch: v2.20.1
repo: https://github.com/FherStk/AutoCheck.git
id: .ctor
path: ../core/connectors/Atom.cs
Expand Down Expand Up @@ -194,7 +194,7 @@ items:
source:
remote:
path: core/connectors/Atom.cs
branch: v2.20.0
branch: v2.20.1
repo: https://github.com/FherStk/AutoCheck.git
id: Dispose
path: ../core/connectors/Atom.cs
Expand Down Expand Up @@ -229,7 +229,7 @@ items:
source:
remote:
path: core/connectors/Atom.cs
branch: v2.20.0
branch: v2.20.1
repo: https://github.com/FherStk/AutoCheck.git
id: ValidateAtomAgainstW3C
path: ../core/connectors/Atom.cs
Expand Down
Loading

0 comments on commit 153de7d

Please sign in to comment.