From 54eeb38a368811538fcdc68c629b721e74c6cb62 Mon Sep 17 00:00:00 2001
From: fzahle <frza@dtu.dk>
Date: Mon, 20 May 2019 22:20:37 +0200
Subject: [PATCH] py3 fixes for testsuite runner

---
 .gitlab-ci.yml                     |  7 +++-
 ellipsyswrapper/ellipsys_output.py | 20 ++++++-----
 ellipsyswrapper/testsuite.py       | 58 ++++++++++++++----------------
 3 files changed, 45 insertions(+), 40 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 761ac72..e675c64 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,8 +1,13 @@
 image: frza/gitlab-ci-debian
 
-test:
+test2:
   script:
     - source activate py27
     - pip install -e .
     - nosetests
+test3:
+  script:
+    - source activate py35
+    - pip install -e .
+    - nosetests
 
diff --git a/ellipsyswrapper/ellipsys_output.py b/ellipsyswrapper/ellipsys_output.py
index 15aeefc..b0603fb 100644
--- a/ellipsyswrapper/ellipsys_output.py
+++ b/ellipsyswrapper/ellipsys_output.py
@@ -43,10 +43,11 @@ class EllipSys1DOutput(object):
                 var_map[ivar] = name
                 if not hasattr(self, 'pt_ext_%s' % name):
                     setattr(self, 'pt_ext_%s' % name, [])
+                pos = fid.tell()
                 line = fid.readline()
                 ivar += 1
             # unroll readline so all data is read by loadtxt
-            fid.seek(-len(line), 1)
+            fid.seek(pos, 0)
             data = np.loadtxt(fid)
             for j in range(data.shape[1]):
                 var = getattr(self, 'pt_ext_%s' % var_map[j])
@@ -156,7 +157,7 @@ class EllipSys2DOutput(object):
                                 self.fy_ave[nlev].append(np.mean(grid_force[ave:, 5]))
                                 self.mz_ave[nlev].append(np.mean(grid_force[ave:, 6]))
                             except:
-                                print 'Error on reading of',filename
+                                print('Error on reading of', filename)
 
                 else:
                 # there's only one surface
@@ -176,7 +177,7 @@ class EllipSys2DOutput(object):
                             self.fy_ave.append(np.mean(grid_force[ave:, 5]))
                             self.mz_ave.append(np.mean(grid_force[ave:, 6]))
                         except:
-                            print 'Error on reading of',filename
+                            print('Error on reading of', filename)
                 nlev += 1
 
         # read the pressure distributions
@@ -362,7 +363,7 @@ class EllipSys3DOutput(object):
                                 self.my_ave[nlev].append(np.mean(grid_force[ave:, 9]))
                                 self.mz_ave[nlev].append(np.mean(grid_force[ave:, 10]))
                             except:
-                                print 'Error on reading of',filename
+                                print('Error on reading of', filename)
 
                 else:
                 # there's only one surface
@@ -391,7 +392,7 @@ class EllipSys3DOutput(object):
                             self.my_ave.append(np.mean(grid_force[ave:, 9]))
                             self.mz_ave.append(np.mean(grid_force[ave:, 10]))
                         except:
-                            print 'Error on reading of',filename
+                            print('Error on reading of', filename)
                 nlev += 1
 
         # read the grid.?F? files
@@ -449,10 +450,11 @@ class EllipSys3DOutput(object):
                 var_map[ivar] = name
                 if not hasattr(self, 'pt_ext_%s' % name):
                     setattr(self, 'pt_ext_%s' % name, [])
+                pos = fid.tell()
                 line = fid.readline()
                 ivar += 1
             # unroll readline so all data is read by loadtxt
-            fid.seek(-len(line), 1)
+            fid.seek(pos, 0)
             data = np.loadtxt(fid)
             for j in range(data.shape[1]):
                 var = getattr(self, 'pt_ext_%s' % var_map[j])
@@ -475,10 +477,11 @@ class EllipSys3DOutput(object):
                 var_map[ivar] = name
                 if not hasattr(self, 'les_ext_%s' % name):
                     setattr(self, 'les_ext_%s' % name, [])
+                pos = fid.tell()
                 line = fid.readline()
                 ivar += 1
             # unroll readline so all data is read by loadtxt
-            fid.seek(-len(line), 1)
+            fid.seek(pos, 0)
             data = np.loadtxt(fid)
             for j in range(data.shape[1]):
                 var = getattr(self, 'les_ext_%s' % var_map[j])
@@ -501,10 +504,11 @@ class EllipSys3DOutput(object):
                 var_map[ivar] = name
                 if not hasattr(self, 'ha_ext_%s' % name):
                     setattr(self, 'ha_ext_%s' % name, [])
+                pos = fid.tell()
                 line = fid.readline()
                 ivar += 1
             # unroll readline so all data is read by loadtxt
-            fid.seek(-len(line), 1)
+            fid.seek(pos, 0)
             data = np.loadtxt(fid)
             for j in range(data.shape[1]):
                 var = getattr(self, 'ha_ext_%s' % var_map[j])
diff --git a/ellipsyswrapper/testsuite.py b/ellipsyswrapper/testsuite.py
index 5988bc6..837385b 100644
--- a/ellipsyswrapper/testsuite.py
+++ b/ellipsyswrapper/testsuite.py
@@ -2,9 +2,7 @@
 import os,sys
 import glob
 import numpy as np
-import commands
 import subprocess
-import cPickle
 import time
 import shutil
 import unittest2 as unittest
@@ -43,13 +41,13 @@ class CaseRunner(object):
 
         self.root_dir = os.getcwd()
 
-        for k, w in kwargs.iteritems():
+        for k, w in kwargs.items():
             if hasattr(self, k):
                 setattr(self, k, w)
 
         self.inputobj = read_ellipsys_input(os.path.join(self.directory, self.inputfile))
         for inp in self.inputs:
-            self.inputobj.set_entry(inp.keys()[0], inp.values()[0])
+            self.inputobj.set_entry(sorted(inp)[0], sorted(inp.values())[0])
 
         if self.solver == 'ellipsys1d':
             self._dim = 1
@@ -70,13 +68,13 @@ class CaseRunner(object):
         testroot = '/'.join(self.directory.split('/')[:-1])
         test_dir = os.path.join(testroot, self.casename)
         self.testroot = testroot
-        print '#################################################################################'
-        print '### Test case: '+self.casename
-        print '#################################################################################'
+        print('#################################################################################')
+        print('### Test case: '+self.casename)
+        print('#################################################################################')
         if os.path.exists(test_dir):
             if not self.keep_directory:
                 # raise RuntimeWarning('Found old run directory %s, and deleted it!' % test_dir)
-                print 'Found old run directory %s, and deleted it!' % test_dir
+                print('Found old run directory %s, and deleted it!' % test_dir)
                 shutil.rmtree(test_dir)
                 os.mkdir(test_dir)
             else:
@@ -98,7 +96,7 @@ class CaseRunner(object):
         self._run('output')
 
         if self.test_restart:
-            print '### case '+self.casename+': test restart from coarse grid level'
+            print('### case '+self.casename+': test restart from coarse grid level')
             # remove output files
             files = ['grid.%iforce',
                      'grid.%ipoints',
@@ -111,7 +109,7 @@ class CaseRunner(object):
                 for f in files:
                     try:
                         os.remove(f % i)
-                        print 'removed old output file', f % i
+                        print('removed old output file', f % i)
                     except:
                         pass
             # read number of grid levels and mstep
@@ -137,7 +135,7 @@ class CaseRunner(object):
             sys.stdout.flush()
             self._run('output_rst')
             # restart again on finer grlev with the other half of iterations.
-            print '### case '+self.casename+': test restart at finest level from mstep =',mstep[0]
+            print('### case '+self.casename+': test restart at finest level from mstep =', mstep[0])
             if os.path.isfile('grid.rst'):
                 os.system('rm grid.rst')
             os.system('ln -s grid.RST.01 grid.rst')
@@ -151,18 +149,18 @@ class CaseRunner(object):
             self._run('output_rst')
             # Compare restart files
             os.system('ln -s grid.RST.01 grid.brst')
-            print 'compare restart files'
+            print('compare restart files')
             test = subprocess.call('cmp grid.arst grid.brst', shell=True)
             if(test==0):
-                print 'restart files are equal'
+                print('restart files are equal')
             elif(test==1):
-                print 'restart files are not equal!'
-                print 'compare fields'
+                print('restart files are not equal!')
+                print('compare fields')
                 toolspath = os.getenv('TOOLS_PATH')
                 os.system(toolspath+'/CompareRST')
                 sys.exit()
             else:
-                print 'unknown output from cmp'
+                print('unknown output from cmp')
         # Clean up restart files
         os.system('rm *RST* *rst*')
         os.chdir(self.root_dir)
@@ -176,13 +174,12 @@ class CaseRunner(object):
 
         if self.exec_mode == 'pbs':
             command = 'qsub RunBatch1.1'
-            self.jobid = commands.getoutput(command)
-            print 'submitted case', self.directory, jobid
+            self.jobid = subprocess.call(command, shell=True)
+            print('submitted case', self.directory, jobid)
         elif self.exec_mode == 'python':
             t0 = time.time()
             exepath = os.path.join(epath, self.flowfield)
             command = 'mpirun --allow-run-as-root -np %i python %s' % (self.nprocs, exepath)
-            # out = commands.getoutput(command)
             if self.debug:
               status = subprocess.call(command, shell=True)
             else:
@@ -191,7 +188,7 @@ class CaseRunner(object):
               status = subprocess.call(command, shell=True,stdout=FNULL)
             self.read_data(self.project, outname)
             self.jobid = None
-            print 'case %s ran in %f seconds' %(self.casename, time.time() - t0)
+            print('case %s ran in %f seconds' %(self.casename, time.time() - t0))
         elif self.exec_mode == 'shell':
             t0 = time.time()
             exepath = os.path.join(epath, self.flowfield)
@@ -207,14 +204,13 @@ class CaseRunner(object):
               # Suppress output from EllipSys
               FNULL = open(os.devnull, 'w')
               status = subprocess.call(command, shell=True,stdout=FNULL)
-            # status, out = commands.getstatusoutput(command)
             if status != 0:
                 raise RuntimeError('Case %s failed!' % self.casename)
             self.read_data(self.project, outname)
             self.jobid = None
-            print 'case %s ran in %f seconds' %(self.casename, time.time() - t0)
+            print('case %s ran in %f seconds' %(self.casename, time.time() - t0))
         elif self.exec_mode == 'dry_run':
-            print 'Running in dry run mode, copying pre-computed data!'
+            print('Running in dry run mode, copying pre-computed data!')
             files = glob.glob(os.path.join(self.root_dir, self.directory, '*'))
             for f in files:
                 shutil.copy(f, '.')
@@ -230,7 +226,7 @@ class CaseRunner(object):
         delfiles = list(set(files)-set(self.files))
         for file in delfiles:
             if 'pkl' not in file:
-                print 'deleting file', file
+                print('deleting file', file)
                 os.remove(file)
         os.chdir('..')
 
@@ -250,11 +246,11 @@ class CaseRunner(object):
 
     def write_result(self):
 
-        import cPickle
+        import Pickle
 
         os.chdir(self.directory)
         fid = open(self.casename+'.pkl','w')
-        cPickle.dump(self.output, fid)
+        Pickle.dump(self.output, fid)
         fid.close()
         os.chdir('..')
 
@@ -267,7 +263,7 @@ class EllipSysTestCase(object):
             self.solver = self.casedict['solver']
         else:
             self.solver = 'ellipsys2d'
-            print 'Warning: solver not set in casedict, assuming youre running EllipSys2D!'
+            print('Warning: solver not set in casedict, assuming youre running EllipSys2D!')
 
         if 'exec_mode' in self.casedict and self.casedict['exec_mode'] == 'python':
             skip = False
@@ -308,16 +304,16 @@ class EllipSysTestCase(object):
         if 'casename' not in self.casedict.keys():
             case.casename = self.__class__.__name__.split('.')[-1]
         case.run_case()
-        print 'Checking results ...'
+        print('Checking results ...')
         self.__check_results(case, case.output)
         if case.test_restart:
-            print 'Checking rst results ...'
+            print('Checking rst results ...')
             self.__check_results(case, case.output_rst, suffix='_rst')
         os.chdir(root_dir)
 
     def __check_results(self, case, output, suffix=''):
 
-        print 'Test variables:', case.variables
+        print('Test variables:', case.variables)
         if len(case.variables) != 0:
             for name in case.variables:
                 with self.subTest(variable=name + suffix):
@@ -333,7 +329,7 @@ class EllipSysTestCase(object):
                                                         rtol=10**(-case.tol),
                                                         atol=10**(-case.tol))
         else:
-            for name, expected in case.reg_data.__dict__.iteritems():
+            for name, expected in case.reg_data.__dict__.items():
                 result = getattr(output, name)
                 with self.subTest(variable=name + suffix):
                     if isinstance(expected, list):
-- 
GitLab