summaryrefslogtreecommitdiffstats
path: root/src/windows
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-11-26 11:50:52 +0100
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-11-27 13:53:19 +0100
commite4be5c34eb71da9a705bc62fdfcfe6d763022747 (patch)
tree70883c72fd8baa07ca225e5a7fee173158283bd2 /src/windows
parenta36c4daa23d32aeee9539374aa591152ef2c914b (diff)
src: add support for direct command executionv1.3.2-25
Update live shell run mode for the new REST API interface. Evaluate the "inline" field to diferentiate between execution of script in /opt/opengnsys/shell/ and a cmd execution. Remove usage of echo argument of the API REST. Update Windows and Linux mode for direct command execution. Set OutputEncoding environment variable to 'utf-8' in Windows to unify the encoding of stdout for the invoked programs. Decode stdout to utf-8-sig to remove potential BOM. While at this, remove strange legacy ;|\n\r terminator.
Diffstat (limited to 'src/windows')
-rw-r--r--src/windows/ogOperations.py35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/windows/ogOperations.py b/src/windows/ogOperations.py
index 23e962c..9a80b0a 100644
--- a/src/windows/ogOperations.py
+++ b/src/windows/ogOperations.py
@@ -7,10 +7,10 @@
# (at your option) any later version.
import os
+import locale
import ctypes
import psutil
import subprocess
-from subprocess import CalledProcessError
import multiprocessing as mp
from multiprocessing import Process, freeze_support
from src.log import OgError
@@ -84,20 +84,27 @@ class OgWindowsOperations:
def shellrun(self, request, ogRest):
cmd = request.getrun()
+ is_inline = request.get_inline()
+
+ if not is_inline:
+ raise OgError("Only inline mode is supported on Windows")
+
+ env = os.environ.copy()
+ env['OutputEncoding'] = 'utf8'
+
try:
- result = subprocess.run(cmd,
- shell=True,
- stdin=subprocess.DEVNULL,
- capture_output=True,
- text=True,
- check=True)
- except CalledProcessError as error:
- if error.stderr:
- return error.stderr
- if error.stdout:
- return error.stdout
- return "{Non zero exit code and empty output}"
- return (result.returncode, cmd, result.stdout)
+ ogRest.proc = subprocess.Popen(
+ cmd,
+ stdout=subprocess.PIPE,
+ shell=True,
+ env=env,
+ )
+ output, error = ogRest.proc.communicate()
+ except (OSError, subprocess.SubprocessError) as e:
+ raise OgError(f'Error when running "shell run" subprocess: {e}') from e
+
+ output = output.decode('utf-8-sig', errors='replace')
+ return (ogRest.proc.returncode, cmd, output)
def session(self, request, ogRest):
raise OgError('Function not implemented')