Changes in tests/pybin/tools.py [143e6f3:d65f92c]
- File:
-
- 1 edited
-
tests/pybin/tools.py (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/pybin/tools.py
r143e6f3 rd65f92c 2 2 import argparse 3 3 import contextlib 4 import datetime5 4 import fileinput 6 5 import multiprocessing … … 23 22 24 23 # helper functions to run terminal commands 25 def sh(*cmd, timeout = False, output = None, input = None, error = subprocess.STDOUT):24 def sh(*cmd, timeout = False, output_file = None, input_file = None, input_text = None, error = subprocess.STDOUT): 26 25 cmd = list(cmd) 26 27 if input_file and input_text: 28 return 401, "Cannot use both text and file inputs" 27 29 28 30 # if this is a dry_run, only print the commands that would be ran 29 31 if settings.dry_run : 30 32 cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd)) 31 if output and not isinstance(output, int):33 if output_file and not isinstance(output_file, int): 32 34 cmd += " > " 33 cmd += output 35 cmd += output_file 34 36 35 37 if error and not isinstance(error, int): … … 37 39 cmd += error 38 40 39 if input and not isinstance(input, int) and os.path.isfile(input):41 if input_file and not isinstance(input_file, int) and os.path.isfile(input_file): 40 42 cmd += " < " 41 cmd += input 43 cmd += input_file 42 44 43 45 print(cmd) … … 46 48 with contextlib.ExitStack() as onexit: 47 49 # add input redirection if needed 48 input = openfd(input, 'r', onexit, True)50 input_file = openfd(input_file, 'r', onexit, True) 49 51 50 52 # add output redirection if needed 51 output = openfd(output, 'w', onexit, False)53 output_file = openfd(output_file, 'w', onexit, False) 52 54 53 55 # add error redirection if needed … … 58 60 proc = subprocess.run( 59 61 cmd, 60 stdin =input,61 stdout =output,62 stderr =error,63 timeout =settings.timeout.single if timeout else None62 **({'input' : bytes(input_text, encoding='utf-8')} if input_text else {'stdin' : input_file}), 63 stdout = output_file, 64 stderr = error, 65 timeout = settings.timeout.single if timeout else None 64 66 ) 67 65 68 return proc.returncode, proc.stdout.decode("utf-8") if proc.stdout else None 66 69 except subprocess.TimeoutExpired: … … 75 78 return False 76 79 77 code, out = sh("file %s" % fname, output =subprocess.PIPE)80 code, out = sh("file %s" % fname, output_file=subprocess.PIPE) 78 81 if code != 0: 79 82 return False … … 107 110 if isinstance(files, str ): files = [ files ] 108 111 for file in files: 109 sh( 'rm', '-f', file, output =subprocess.DEVNULL, error=subprocess.DEVNULL )112 sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL ) 110 113 111 114 # Create 1 or more directory … … 115 118 p = os.path.normpath( file ) 116 119 d = os.path.dirname ( p ) 117 sh( 'mkdir', '-p', d, output =subprocess.DEVNULL, error=subprocess.DEVNULL )120 sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL ) 118 121 119 122 … … 138 141 lhs, 139 142 rhs, 140 output =subprocess.PIPE143 output_file=subprocess.PIPE 141 144 ) 142 145 143 146 # call make 144 def make(target, *, flags = '', output = None, error = None, error_file = None, silent = False):147 def make(target, *, flags = '', output_file = None, error = None, error_file = None, silent = False): 145 148 test_param = """test="%s" """ % (error_file) if error_file else None 146 149 cmd = [ … … 151 154 settings.debug.flags, 152 155 settings.install.flags, 156 settings.distcc if settings.distribute else None, 153 157 flags, 154 158 target 155 159 ] 156 160 cmd = [s for s in cmd if s] 157 return sh(*cmd, output =output, error=error)161 return sh(*cmd, output_file=output_file, error=error) 158 162 159 163 def which(program): … … 201 205 # cat one file into the other 202 206 def cat(source, dest): 203 ret, _ = sh("cat", source, output =dest)207 ret, _ = sh("cat", source, output_file=dest) 204 208 return ret 205 209 … … 274 278 ################################################################################ 275 279 276 def pretty_now(): 277 ts = time.time() 278 print(ts, file=sys.stderr) 279 return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S') 280 # get hash for given configuration 281 def config_hash(): 282 path = os.path.normpath(os.path.join( 283 settings.SRCDIR, 284 )) 285 286 distcc_hash = os.path.join(settings.SRCDIR, '../tools/build/distcc_hash') 287 config = "%s-%s" % (settings.arch.target, settings.debug.path) 288 _, out = sh(distcc_hash, config, output_file=subprocess.PIPE) 289 return out.strip() 280 290 281 291 # check if arguments is yes or no … … 308 318 return 1, "ERR No core dump" 309 319 310 return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output=subprocess.PIPE) 311 312 def core_archive(dst, name, exe): 313 # Get the core dump 314 core = os.path.join(os.getcwd(), "core" ) 315 316 # update the path for this test 317 dst = os.path.join(dst, name) 318 319 # make a directory for this test 320 # mkdir makes the parent directory only so add a dummy 321 mkdir(os.path.join(dst, "dir")) 322 323 # moves the files 324 mv( core, os.path.join(dst, "core" ) ) 325 mv( exe , os.path.join(dst, name ) ) 326 327 # return explanatory test 328 return "Archiving %s (executable and core) to %s" % (os.path.relpath(exe, settings.BUILDDIR), os.path.relpath(dst, settings.original_path)) 320 return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output_file=subprocess.PIPE) 329 321 330 322 class Timed:
Note:
See TracChangeset
for help on using the changeset viewer.