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

Fix python2 incompatibility #138

Merged
merged 5 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 30 additions & 21 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,28 @@ 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,
output=cpe.output,
stderr=cpe.stderr) from cpe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the from cpe just keeps the existing call stack when raising the error, so potentially helps a bit with debugging. Is that a python 3-only syntax? Or is there another reason you dropped that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know this was added to Python 3.x unfortunately.

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