From 883f63aa1e915db5f0a2e5ee912a1c00e49d8bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20G=C3=B6ransson?= Date: Mon, 13 Apr 2015 19:14:38 +0200 Subject: [PATCH] Added parameter '--throw-exception', which can not be specified togheter with '--rna', that will remove the catch-exception code added the epilogue code if '--rna' isn't specified. This feature, in combination with using Popen() instead of call(), will make it possible to catch errors in the RNA code, when not specifying '--rna', which otherwise would go unnoticed. --- ribosome.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/ribosome.py b/ribosome.py index bfd5ab8..c330e49 100755 --- a/ribosome.py +++ b/ribosome.py @@ -308,7 +308,9 @@ def rethrow(e, rnafile, linemap): parser = argparse.ArgumentParser( version = "ribosome code generator, version 1.14") parser.add_argument('dna', type=file) -parser.add_argument('--rna', action='store_true') +group = parser.add_mutually_exclusive_group() +group.add_argument('--rna', action='store_true') +group.add_argument('--throw-exception', action='store_true') # Pwd __dir__ = os.path.dirname(os.path.realpath(__file__)) @@ -353,7 +355,7 @@ def rnawrite(s): # Generate RNA Prologue code rnawrite(PROLOGUE) -if not args.rna: +if not args.rna and not args.throw_exception: rnawrite('try:\n') # Process the DNA file. @@ -388,7 +390,7 @@ def rnawrite(s): # Lets save the left and right spaces, if any lspace = line.replace(line.lstrip(), '') # Add 4 spaces to accommodate our try except clause if not rna - if not args.rna: + if not args.rna and not args.throw_exception: lspace = lspace + ' ' * 4 rspace = line.replace(line.rstrip(), '') # followed by stripping the line @@ -477,7 +479,7 @@ def rnawrite(s): # Generate RNA epilogue code. dnastack = [[None, 'ribosome', __line__() + 1]] rna.write('\n') -if not args.rna: +if not args.rna and not args.throw_exception: rna.write('except Exception as e:\n') rna.write(' LINEMAP = [\n') last = None @@ -501,7 +503,17 @@ def rnawrite(s): if not args.rna: import subprocess # Execute the RNA file. Pass it any arguments not used by ribosome. - subprocess.call(['python', rnafile] + sys.argv[2:]) + pipes = subprocess.Popen(['python', rnafile] + sys.argv[2:], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = pipes.communicate() + + # Print generated RNA file contents + sys.stdout.write(stdout) + # Delete the RNA file. os.remove(rnafile) + # If there was an error in the RNA file, print the error and exit with a non-zero status + if args.throw_exception and stderr is not None and len(stderr) > 0: + sys.stderr.write(stderr) + sys.exit(1) +