Commit 6cc08646 authored by Manuel Günther's avatar Manuel Günther

Further improved python3 compatibility; fixed some tests.

parent f008c662
......@@ -8,10 +8,8 @@ import sys
if sys.version_info[0] >= 3:
from pickle import dumps, loads
python_3 = True
else:
from cPickle import dumps, loads
python_3 = False
from .tools import logger
......@@ -173,13 +171,22 @@ class Job(Base):
def get_command_line(self):
return loads(self.command_line) if python_3 else loads(str(self.command_line))
"""Returns the command line for the job."""
# In python 2, the command line is unicode, which needs to be converted to string before pickling;
# In python 3, the command line is bytes, which can be pickled directly
return loads(self.command_line) if isinstance(self.command_line, bytes) else loads(str(self.command_line))
def get_array(self):
return loads(self.array_string) if python_3 else loads(str(self.array_string))
"""Returns the array arguments for the job; usually a string."""
# In python 2, the command line is unicode, which needs to be converted to string before pickling;
# In python 3, the command line is bytes, which can be pickled directly
return loads(self.array_string) if isinstance(self.array_string, bytes) else loads(str(self.array_string))
def get_arguments(self):
return loads(self.grid_arguments) if python_3 else loads(str(self.grid_arguments))
"""Returns the additional options for the grid (such as the queue, memory requirements, ...)."""
# In python 2, the command line is unicode, which needs to be converted to string before pickling;
# In python 3, the command line is bytes, which can be pickled directly
return loads(self.grid_arguments) if isinstance(self.grid_arguments, bytes) else loads(str(self.grid_arguments))
def get_jobs_we_wait_for(self):
return [j.waited_for_job for j in self.jobs_we_have_to_wait_for if j.waited_for_job is not None]
......
......@@ -26,6 +26,8 @@ def environ(context):
pi = subprocess.Popen(command, stdout = subprocess.PIPE)
# overwrite the default environment
for line in pi.stdout:
if isinstance(line, bytes) and not isinstance(line, str):
line = line.decode('utf8')
(key, _, value) = line.partition("=")
os.environ[key.strip()] = value.strip()
except OSError as e:
......
......@@ -28,8 +28,7 @@ class GridTKTest(unittest.TestCase):
def tearDown(self):
# make sure that all scheduler jobs are stopped after exiting
if self.scheduler_job:
self.scheduler_job.send_signal(signal.SIGINT)
self.scheduler_job.wait()
self.scheduler_job.kill()
# Clean up the mess that we created
import shutil
shutil.rmtree(self.temp_dir)
......@@ -80,7 +79,7 @@ class GridTKTest(unittest.TestCase):
# sleep some time to assure that the scheduler was able to start the first job
time.sleep(4)
# ... and kill the scheduler
self.scheduler_job.send_signal(signal.SIGINT)
self.scheduler_job.kill()
self.scheduler_job = None
# now, the first job needs to have status failure, and the second needs to be queued
......@@ -104,7 +103,7 @@ class GridTKTest(unittest.TestCase):
# sleep some time to assure that the scheduler was able to finish the first and start the second job
time.sleep(9)
# ... and kill the scheduler
self.scheduler_job.send_signal(signal.SIGINT)
self.scheduler_job.kill()
self.scheduler_job = None
# Job 1 and two array jobs of job two should be finished now, the other two still need to be queued
......@@ -138,9 +137,8 @@ class GridTKTest(unittest.TestCase):
# now, let the scheduler run all jobs
self.scheduler_job = subprocess.Popen(['./bin/jman', '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '1', '--parallel', '2', '--die-when-finished'])
# ... and kill the scheduler
time.sleep(10)
assert self.scheduler_job.poll() is not None
# and wait for the job to finish (the timeout argument to Popen only exists from python 3.3 onwards)
self.scheduler_job.wait()
self.scheduler_job = None
# check that all output files are generated again
......@@ -188,9 +186,8 @@ class GridTKTest(unittest.TestCase):
# and execute them, but without writing the log files
self.scheduler_job = subprocess.Popen(['./bin/jman', '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '0.1', '--parallel', '2', '--die-when-finished', '--no-log-files'])
# ... and kill the scheduler
time.sleep(3)
assert self.scheduler_job.poll() is not None
# and wait for the job to finish (the timeout argument to Popen only exists from python 3.3 onwards)
self.scheduler_job.wait()
self.scheduler_job = None
# assert that the log files are not there
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment