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/report Python3 decoding errors, parse all suites if --suites=... argument not present #292

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 24 additions & 4 deletions scripts/ccpp_prebuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from common import encode_container, decode_container, decode_container_as_dict, execute
from common import CCPP_INTERNAL_VARIABLES, CCPP_STATIC_API_MODULE, CCPP_INTERNAL_VARIABLE_DEFINITON_FILE
from common import STANDARD_VARIABLE_TYPES, STANDARD_INTEGER_TYPE, CCPP_TYPE
from common import SUITE_DEFINITION_FILENAME_PATTERN
from common import split_var_name_and_array_reference
from metadata_parser import merge_dictionaries, parse_scheme_tables, parse_variable_tables
from mkcap import CapsMakefile, CapsCMakefile, CapsSourcefile, \
Expand Down Expand Up @@ -48,10 +49,10 @@ def parse_arguments():
configfile = args.config
clean = args.clean
debug = args.debug
if not args.suites:
parser.print_help()
sys.exit(-1)
sdfs = [ 'suite_{0}.xml'.format(x) for x in args.suites.split(',')]
if args.suites:
sdfs = [ 'suite_{0}.xml'.format(x) for x in args.suites.split(',')]
else:
sdfs = None
builddir = args.builddir
return (success, configfile, clean, debug, sdfs, builddir)

Expand Down Expand Up @@ -160,6 +161,19 @@ def clean_files(config):
execute(cmd)
return success

def get_all_suites(suites_dir):
success = False
logging.info("No suites were given, compiling a list of all suites")
sdfs = []
for f in os.listdir(suites_dir):
match = SUITE_DEFINITION_FILENAME_PATTERN.match(f)
if match:
logging.info('Adding suite definition file {}'.format(f))
sdfs.append(f)
if sdfs:
success = True
return (success, sdfs)

Comment on lines +164 to +176
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like a lot of work to do instead of something like:

sdfs = glob.glob(suite_dir, "suite_*.xml")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's right ... when I was making those changes I wasn't sure whether I needed to get the suite name (which comes for free as match.group(1) with my approach) or not. Turned out I didn't.

In any case, even the more expensive approach costs probably less than a tenth of a second, so that should be ok.

def parse_suites(suites_dir, sdfs):
"""Parse suite definition files for prebuild"""
logging.info('Parsing suite definition files ...')
Expand Down Expand Up @@ -695,6 +709,12 @@ def main():
logging.info('CCPP prebuild clean completed successfully, exiting.')
sys.exit(0)

# If no suite definition files were given, get all of them
if not sdfs:
(success, sdfs) = get_all_suites(config['suites_dir'])
if not success:
raise Exception('Call to get_all_sdfs failed.')

# Parse suite definition files for prebuild
(success, suites) = parse_suites(config['suites_dir'], sdfs)
if not success:
Expand Down
13 changes: 8 additions & 5 deletions scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
CCPP_STATIC_API_MODULE = 'ccpp_static_api'
CCPP_STATIC_SUBROUTINE_NAME = 'ccpp_physics_{stage}'

# Filename pattern for suite definition files
SUITE_DEFINITION_FILENAME_PATTERN = re.compile('^suite_(.*)\.xml$')

def execute(cmd, abort = True):
"""Runs a local command in a shell. Waits for completion and
returns status, stdout and stderr. If abort = True, abort in
Expand All @@ -56,18 +59,18 @@ def execute(cmd, abort = True):
status = p.returncode
if debug:
message = 'Execution of "{0}" returned with exit code {1}\n'.format(cmd, status)
message += ' stdout: "{0}"\n'.format(stdout.rstrip('\n'))
message += ' stderr: "{0}"'.format(stderr.rstrip('\n'))
message += ' stdout: "{0}"\n'.format(stdout.decode('ascii').rstrip('\n'))
message += ' stderr: "{0}"'.format(stderr.decode('ascii').rstrip('\n'))
logging.debug(message)
if not status == 0:
message = 'Execution of command {0} failed, exit code {1}\n'.format(cmd, status)
message += ' stdout: "{0}"\n'.format(stdout.rstrip('\n'))
message += ' stderr: "{0}"'.format(stderr.rstrip('\n'))
message += ' stdout: "{0}"\n'.format(stdout.decode('ascii').rstrip('\n'))
message += ' stderr: "{0}"'.format(stderr.decode('ascii').rstrip('\n'))
if abort:
raise Exception(message)
else:
logging.error(message)
return (status, stdout.rstrip('\n'), stderr.rstrip('\n'))
return (status, stdout.decode('ascii').rstrip('\n'), stderr.decode('ascii').rstrip('\n'))

def split_var_name_and_array_reference(var_name):
"""Split an expression like foo(:,a,1:ddt%ngas)
Expand Down
10 changes: 8 additions & 2 deletions scripts/metadata_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ def parse_variable_tables(filename):

# Read all lines of the file at once
with (open(filename, 'r')) as file:
file_lines = file.readlines()
try:
file_lines = file.readlines()
except UnicodeDecodeError:
raise Exception("Decoding error while trying to read file {}, check that the file only contains ASCII characters".format(filename))

lines = []
buffer = ''
Expand Down Expand Up @@ -464,7 +467,10 @@ def parse_scheme_tables(filename):

# Read all lines of the file at once
with (open(filename, 'r')) as file:
file_lines = file.readlines()
try:
file_lines = file.readlines()
except UnicodeDecodeError:
raise Exception("Decoding error while trying to read file {}, check that the file only contains ASCII characters".format(filename))

lines = []
original_line_numbers = []
Expand Down