From 3e13218edd58585f08d9f8d56ceb7d9d1a764103 Mon Sep 17 00:00:00 2001
From: Manuel Guenther <manuel.guenther@idiap.ch>
Date: Tue, 13 Aug 2013 10:04:01 +0200
Subject: [PATCH] Made gridtk python 3 compatible.

---
 gridtk/local.py          |  4 +++-
 gridtk/manager.py        | 32 +++++++++++++++++---------------
 gridtk/script/grid.py    |  9 +++++----
 gridtk/sge.py            |  8 +++++---
 gridtk/tests/__init__.py |  9 ++++++---
 5 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/gridtk/local.py b/gridtk/local.py
index 7615a7c..5f3e5f2 100644
--- a/gridtk/local.py
+++ b/gridtk/local.py
@@ -6,6 +6,8 @@
 """Defines the job manager which can help you managing submitted grid jobs.
 """
 
+from __future__ import print_function
+
 import subprocess
 import time
 import copy, os, sys
@@ -43,7 +45,7 @@ class JobManagerLocal(JobManager):
     logger.info("Added job '%s' to the database" % job)
 
     if dry_run:
-      print "Would have added the Job", job, "to the database to be executed locally."
+      print("Would have added the Job", job, "to the database to be executed locally.")
       self.session.delete(job)
       logger.info("Deleted job '%s' from the database due to dry-run option" % job)
       job_id = None
diff --git a/gridtk/manager.py b/gridtk/manager.py
index f34cbdf..cba9e73 100644
--- a/gridtk/manager.py
+++ b/gridtk/manager.py
@@ -1,4 +1,6 @@
 
+from __future__ import print_function
+
 import os
 import subprocess
 from .models import Base, Job, ArrayJob
@@ -167,18 +169,18 @@ class JobManager:
     header = [fields[k].center(lengths[k]) for k in range(len(lengths))]
 
     # print header
-    print '  '.join(header)
-    print delimiter
+    print('  '.join(header))
+    print(delimiter)
 
 
     self.lock()
     for job in self.get_jobs(job_ids):
-      print job.format(format, dependency_length, None if long else 43)
+      print(job.format(format, dependency_length, None if long else 43))
       if print_array_jobs and job.array:
-        print array_delimiter
+        print(array_delimiter)
         for array_job in job.array:
-          print array_job.format(array_format)
-        print array_delimiter
+          print(array_job.format(array_format))
+        print(array_delimiter)
 
     self.unlock()
 
@@ -190,17 +192,17 @@ class JobManager:
       out_file, err_file = job.std_out_file(), job.std_err_file()
       if output and out_file is not None and os.path.exists(out_file) and os.stat(out_file).st_size > 0:
         logger.info("Contents of output file: '%s'" % out_file)
-        print open(out_file).read().rstrip()
-        print "-"*20
+        print(open(out_file).read().rstrip())
+        print("-"*20)
       if error and err_file is not None and os.path.exists(err_file) and os.stat(err_file).st_size > 0:
         logger.info("Contents of error file: '%s'" % err_file)
-        print open(err_file).read().rstrip()
-        print "-"*40
+        print(open(err_file).read().rstrip())
+        print("-"*40)
 
     def _write_array_jobs(array_jobs):
       for array_job in array_jobs:
         if unfinished or array_job.status in ('success', 'failure'):
-          print "Array Job", str(array_job.id), ":"
+          print("Array Job", str(array_job.id), ":")
           _write_contents(array_job)
 
     self.lock()
@@ -209,7 +211,7 @@ class JobManager:
     if array_ids:
       if len(job_ids) != 1: logger.error("If array ids are specified exactly one job id must be given.")
       array_jobs = list(self.session.query(ArrayJob).join(Job).filter(Job.id.in_(job_ids)).filter(Job.unique == ArrayJob.job_id).filter(ArrayJob.id.in_(array_ids)))
-      if array_jobs: print array_jobs[0].job
+      if array_jobs: print(array_jobs[0].job)
       _write_array_jobs(array_jobs)
 
     else:
@@ -218,14 +220,14 @@ class JobManager:
       for job in jobs:
         if job.array:
           if (unfinished or job.status in ('success', 'failure', 'executing')):
-            print job
+            print(job)
             _write_array_jobs(job.array)
         else:
           if unfinished or job.status in ('success', 'failure'):
-            print job
+            print(job)
             _write_contents(job)
         if job.log_dir is not None:
-          print "-"*60
+          print("-"*60)
 
     self.unlock()
 
diff --git a/gridtk/script/grid.py b/gridtk/script/grid.py
index 707ad4b..8e2077e 100755
--- a/gridtk/script/grid.py
+++ b/gridtk/script/grid.py
@@ -6,6 +6,8 @@
 """Executes a given command within the context of a shell script that has its
 enviroment set like Idiap's 'SETSHELL grid' does."""
 
+from __future__ import print_function
+
 import os
 import sys
 
@@ -22,10 +24,9 @@ def main():
   if prog == 'grid':
     # act as before
     if len(sys.argv) < 2:
-      print __doc__
-      print "usage: %s <command> [arg [arg ...]]" % \
-          os.path.basename(sys.argv[0])
-      sys.exit(1)
+      print(__doc__)
+      print("usage: %s <command> [arg [arg ...]]" % os.path.basename(sys.argv[0]))
+      return 1
 
     replace('grid', sys.argv[1:])
   else:
diff --git a/gridtk/sge.py b/gridtk/sge.py
index 27b7986..e72c76a 100644
--- a/gridtk/sge.py
+++ b/gridtk/sge.py
@@ -6,6 +6,8 @@
 """Defines the job manager which can help you managing submitted grid jobs.
 """
 
+from __future__ import print_function
+
 from .manager import JobManager
 from .setshell import environ
 from .models import add_job
@@ -76,9 +78,9 @@ class JobManagerSGE(JobManager):
     job = add_job(self.session, command_line, name, dependencies, array, log_dir=log_dir, stop_on_failure=stop_on_failure, context=self.context, **kwargs)
     logger.info("Added job '%s' to the database." % job)
     if dry_run:
-      print "Would have added the Job"
-      print job
-      print "to the database to be executed in the grid with options:", str(kwargs)
+      print("Would have added the Job")
+      print(job)
+      print("to the database to be executed in the grid with options:", str(kwargs))
       self.session.delete(job)
       logger.info("Deleted job '%s' from the database due to dry-run option" % job)
       job_id = None
diff --git a/gridtk/tests/__init__.py b/gridtk/tests/__init__.py
index 5c0f1a0..b60c562 100644
--- a/gridtk/tests/__init__.py
+++ b/gridtk/tests/__init__.py
@@ -1,3 +1,6 @@
+
+from __future__ import print_function
+
 import unittest
 import nose
 
@@ -44,7 +47,7 @@ class GridTKTest(unittest.TestCase):
     # check that the database was created successfully
     assert os.path.exists(self.database)
 
-    print
+    print()
     # test that the list command works (should also work with the "default" grid manager
     jman.main(['./bin/jman', '--database', self.database, 'list', '--job-ids', '1'])
     jman.main(['./bin/jman', '--database', self.database, 'list', '--job-ids', '2', '--print-array-jobs', '--print-dependencies'])
@@ -164,11 +167,11 @@ class GridTKTest(unittest.TestCase):
       assert jobs[1].array[i].result == 0
     job_manager.unlock()
 
-    print
+    print()
     # test that the list command still works
     jman.main(['./bin/jman', '--database', self.database, 'list', '--print-array-jobs'])
 
-    print
+    print()
     # test that the list command still works
     jman.main(['./bin/jman', '--database', self.database, 'report'])
 
-- 
GitLab