1- #!/usr/bin/env python3
1+ #!/usr/bin/env python
22# Copyright (c) 2019 Arm Limited and Contributors. All rights reserved.
33#
44# SPDX-License-Identifier: Apache-2.0
1010import re
1111import subprocess
1212import sys
13+ from enum import Enum
14+
15+ class ReturnCode (Enum ):
16+ """Return codes."""
17+
18+ SUCCESS = 0
19+ ERROR = 1
20+ INVALID_OPTIONS = 2
21+
1322
1423log = logging .getLogger (__name__ )
1524
@@ -41,25 +50,26 @@ def get_symbol_table(self, elf_file):
4150 "Get the symbol table for ELF format file '{}'" .format (elf_file )
4251 )
4352
44- cmd = [* OBJECT_FILE_ANALYSIS_CMD , elf_file ]
53+ cmd = [OBJECT_FILE_ANALYSIS_CMD [ 0 ], OBJECT_FILE_ANALYSIS_CMD [ 1 ] , elf_file ]
4554 log .debug ("command: '{}'" .format (cmd ))
4655 try :
47- process = subprocess .run (
48- cmd ,
49- check = True ,
50- stdin = None ,
51- stdout = subprocess .PIPE ,
52- stderr = subprocess .STDOUT ,
56+ process = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
57+ except OSError as error :
58+ raise SymbolTableError (
59+ "Getting symbol table for ELF format file '{}' failed,"
60+ " error: {}" .format (elf_file , error )
5361 )
54- except subprocess .CalledProcessError as error :
55- err_output = error .stdout .decode ()
56- msg = (
62+
63+ stdout , _ = process .communicate ()
64+
65+ if process .returncode :
66+ raise SymbolTableError (
5767 "Getting symbol table for ELF format file '{}' failed,"
58- " error: {}" .format (elf_file , err_output )
68+ " error: {}" .format (elf_file , stdout . decode () )
5969 )
60- raise SymbolTableError (msg )
6170
62- symbol_table = process .stdout .decode ()
71+ symbol_table = stdout .decode ()
72+
6373 log .debug ("Symbol table:\n {}\n " .format (symbol_table ))
6474
6575 return symbol_table
@@ -69,13 +79,17 @@ class SymbolTableError(Exception):
6979 """An exception for a failure to obtain a symbol table."""
7080
7181
82+ class FloatSymbolsFound (Exception ):
83+ """An exception generated when floating point symbols are found."""
84+
7285class ArgumentParserWithDefaultHelp (argparse .ArgumentParser ):
7386 """Subclass that always shows the help message on invalid arguments."""
7487
7588 def error (self , message ):
7689 """Error handler."""
7790 sys .stderr .write ("error: {}\n " .format (message ))
7891 self .print_help ()
92+ raise SystemExit (ReturnCode .INVALID_OPTIONS .value )
7993
8094
8195def set_log_verbosity (increase_verbosity ):
@@ -91,6 +105,7 @@ def check_float_symbols(elf_file):
91105
92106 Return the floating point symbols found.
93107 """
108+ print ("Checking {} for floating point symbols" .format (elf_file ))
94109 parser = SymbolParser ()
95110 symbol_table = parser .get_symbol_table (elf_file )
96111
@@ -105,11 +120,12 @@ def check_action(args):
105120 """Entry point for checking the ELF file."""
106121 float_symbols = check_float_symbols (args .elf_file )
107122 if float_symbols :
108- print ("Found float symbols:" )
123+ print ("Failed - Found float symbols:" )
109124 for float_symbol in float_symbols :
110125 print (float_symbol )
126+ raise FloatSymbolsFound ("Found float symbols in {}" .format (args .elf_file ))
111127 else :
112- print ("No float symbols found." )
128+ print ("Passed - No float symbols found." )
113129
114130
115131def parse_args ():
@@ -160,9 +176,15 @@ def run_elf_floats_checker():
160176 args .func (args )
161177
162178
163- if __name__ == "__main__" :
179+ def _main () :
164180 """Run elf-floats-checker."""
165181 try :
166182 run_elf_floats_checker ()
167183 except Exception as error :
168184 print (error )
185+ return ReturnCode .ERROR .value
186+ else :
187+ return ReturnCode .SUCCESS .value
188+
189+ if __name__ == "__main__" :
190+ sys .exit (_main ())
0 commit comments