Skip to content

Commit

Permalink
Merge pull request #138 from mandli/fix-python2-incompatibility
Browse files Browse the repository at this point in the history
Fix python2 incompatibility
  • Loading branch information
mandli authored Jul 23, 2019
2 parents b6642f7 + 6c1e0f0 commit a06d78a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
27 changes: 17 additions & 10 deletions src/python/clawutil/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
from __future__ import absolute_import
from __future__ import print_function
import os
from inspect import signature
import sys
import inspect
import tarfile
import zipfile
import string
import six
from six.moves import range
from six.moves import input

try:
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen

import tarfile
import zipfile
import string

import numpy as np
import six
from six.moves import range
from six.moves import input

# ======================
# Remote file handling
Expand Down Expand Up @@ -302,7 +302,7 @@ def close_data_file(self):
self._out_file = None


def write(self,out_file,data_source='setrun.py'):
def write(self, out_file, data_source='setrun.py'):
r"""Write out all data files in this ClawData object"""

# Open data file
Expand Down Expand Up @@ -528,7 +528,14 @@ def write(self, out_dir = ''):
if isinstance(data_object, UserData):
fname = data_object.__fname__
else:
fname = signature(data_object.write).parameters['out_file'].default
if six.PY2:
argspec = inspect.getargspec(data_object.write)
index = argspec.args.index('out_file') - (len(argspec.args)
- len(argspec.defaults))
fname = argspec.defaults[index]
else:
argspec = inspect.signature(data_object.write)
fname = argspec.parameters['out_file'].default
fpath = os.path.join(out_dir,fname)
if isinstance(data_object, amrclaw.GaugeData):
data_object.write(self.clawdata.num_eqn, self.clawdata.num_aux, out_file=fpath)
Expand Down
54 changes: 32 additions & 22 deletions src/python/clawutil/runclaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@

from __future__ import absolute_import
from __future__ import print_function
import six

import os
import sys
import glob
import shutil
import shlex
import subprocess
import time

from clawpack.clawutil.data import ClawData
from clawpack.clawutil.claw_git_status import make_git_status_file

# define an execution error class that returns a
Expand All @@ -30,7 +35,7 @@ def __str__(self):

def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=None,
rundir=None, print_git_status=False, nohup=False, nice=None,
xclawout=subprocess.PIPE, xclawerr=subprocess.PIPE):
xclawout=None, xclawerr=None, verbose=True):
"""
Run the Fortran version of Clawpack using executable xclawcmd, which is
typically set to 'xclaw', 'xamr', etc.
Expand Down Expand Up @@ -66,14 +71,12 @@ def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=None,
``xclawerr=subprocess.STDOUT``.
"""
from clawpack.clawutil.data import ClawData
import os,glob,shutil,time
verbose = True

try:
nice = int(nice)
except:
nice = None
if nice is not None:
try:
nice = int(nice)
except:
nice = None

# convert strings passed in from Makefile to boolean:
if type(overwrite) is str:
Expand Down Expand Up @@ -145,8 +148,15 @@ def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=None,
except:
print("==> runclaw: Could not move directory... copy already exists?")


os.makedirs(outdir, exist_ok=True)
if six.PY2:
if (not os.path.isdir(outdir)):
try:
os.mkdir(outdir)
except:
print("Cannot make directory ",outdir)
return
else:
os.makedirs(outdir, exist_ok=True)

if print_git_status not in [False,'False']:
# create files claw_git_status.txt and claw_git_diffs.txt in
Expand Down Expand Up @@ -184,7 +194,7 @@ def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=None,
else:
if rundir != outdir:
for file in datafiles:
shutil.copy(file,os.path.join(outdir,os.path.basename(file)))
shutil.copy(file, os.path.join(outdir,os.path.basename(file)))

# execute command to run fortran program:
if nohup:
Expand All @@ -204,29 +214,29 @@ def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=None,
print("\n==> Running with command:\n ", cmd)

cmd_split = shlex.split(cmd)
if isinstance(xclawout,str):
if isinstance(xclawout, str):
xclawout = open(xclawout,'w', encoding='utf-8',
buffering=1)
if isinstance(xclawerr,str):
if isinstance(xclawerr, str):
xclawerr = open(xclawerr,'w', encoding='utf-8',
buffering=1)
try:
p = subprocess.run(cmd_split,
cwd=outdir,
stdout=xclawout,
stderr=xclawerr,
encoding='utf-8',
check=True)
proc = subprocess.check_call(cmd_split,
cwd=outdir,
stdout=xclawout,
stderr=xclawerr)

except subprocess.CalledProcessError as cpe:
raise ClawExeError('error', cpe.returncode, cpe.cmd,
exe_error_str = "\n\n*** FORTRAN EXE FAILED ***\n"
raise ClawExeError(exe_error_str, cpe.returncode, cpe.cmd,
output=cpe.output,
stderr=cpe.stderr) from cpe
stderr=cpe.stderr)

print('==> runclaw: Done executing %s via clawutil.runclaw.py' %\
xclawcmd)
print('==> runclaw: Output is in ', outdir)

return p
return proc


#----------------------------------------------------------
Expand Down

0 comments on commit a06d78a

Please sign in to comment.