Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging functionality to InteractiveScriptTemplate.py.template #2282

Merged
merged 9 commits into from
Feb 14, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import glob
import mimetypes
import os
import shutil
import subprocess
import sys
import time

Expand All @@ -13,10 +14,12 @@ import time

###WNSANDBOX_SOURCE###


sys.path.insert( 0, '###GANGA_PYTHONPATH###' )

statfileName = os.path.join( '###OUTPUTDIR###', '__jobstatus__' )
logfile = os.path.join('###OUTPUTDIR###', 'stdout')
errfile = os.path.join('###OUTPUTDIR###', 'stderr')

try:
statfile = open( statfileName, 'w' )
except IOError as x:
Expand Down Expand Up @@ -71,7 +74,31 @@ commandList = [
'os.system(%s)"' % commandStr ]
commandString = ';'.join( commandList )

result = os.system( '%s' % commandString )
try:
with open(logfile, 'wb') as stdout, open(errfile, 'wb') as stderr:
process = subprocess.Popen(commandString, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)

while True:
output = process.stdout.readline()
if output == b'' and process.poll() is not None:
break
if output:
print(output.strip().decode('utf-8'))
stdout.write(output)
err = process.stderr.readline()
if err:
print(err.strip().decode('utf-8'), file=sys.stderr)
stderr.write(err)


result = process.poll()

except IOError as x:
print('ERROR: Unable to write status file or log file.')
print('ERROR: ',x)
raise

###WN_POSTPROCESSING###
for patternToZip in ###PATTERNS_TO_ZIP###:
Expand All @@ -86,4 +113,3 @@ statfile.write( 'STOP: ' + timeString + os.linesep )
statfile.flush()
statfile.close()


31 changes: 31 additions & 0 deletions ganga/GangaCore/test/GPI/TestInteractive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from GangaCore.testlib.GangaUnitTest import GangaUnitTest

class TestInteractiveJobOutput(GangaUnitTest):
def testInteractiveJobOutput(self):
from GangaCore.GPI import Job, Executable, Interactive, LocalFile
from GangaTest.Framework.utils import sleep_until_completed
import os

app = Executable()
app.exe = 'echo'
app.args = ["Hello World",""]
j = Job(backend=Interactive(), application=app, outputfiles=[LocalFile('stdout'), LocalFile('stderr')])
j.submit()

self.assertTrue(sleep_until_completed(j, 60), 'Timeout on registering Interactive job as completed')

stdout_path = os.path.join(j.outputdir, 'stdout')
stderr_path = os.path.join(j.outputdir, 'stderr')
self.assertTrue(os.path.exists(stdout_path), 'stdout file not created')
self.assertTrue(os.path.exists(stderr_path), 'stderr file not created')

expected_stdout_content = b'Hello World'
expected_stderr_content = b''

with open(stdout_path, 'rb') as stdout_file:
actual_stdout_content = stdout_file.read()
self.assertIn(expected_stdout_content, actual_stdout_content, 'Incorrect content in stdout file')

with open(stderr_path, 'rb') as stderr_file:
actual_stderr_content = stderr_file.read()
self.assertIn(expected_stderr_content, actual_stderr_content, 'Incorrect content in stderr file')
Loading