Changeset 4a60488 for tests/pybin/tools.py
- Timestamp:
- Sep 27, 2019, 3:35:46 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 90ce35aa
- Parents:
- 8e1467d (diff), 849720f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/pybin/tools.py
r8e1467d r4a60488 2 2 import argparse 3 3 import contextlib 4 import datetime 4 5 import fileinput 5 6 import multiprocessing … … 22 23 23 24 # helper functions to run terminal commands 24 def sh(*cmd, timeout = False, output = None, input = None, error = subprocess.STDOUT):25 def sh(*cmd, timeout = False, output_file = None, input_file = None, input_text = None, error = subprocess.STDOUT, ignore_dry_run = False): 25 26 cmd = list(cmd) 26 27 28 if input_file and input_text: 29 return 401, "Cannot use both text and file inputs" 30 27 31 # if this is a dry_run, only print the commands that would be ran 28 if settings.dry_run :32 if settings.dry_run and not ignore_dry_run: 29 33 cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd)) 30 if output and not isinstance(output, int):34 if output_file and not isinstance(output_file, int): 31 35 cmd += " > " 32 cmd += output 36 cmd += output_file 33 37 34 38 if error and not isinstance(error, int): … … 36 40 cmd += error 37 41 38 if input and not isinstance(input, int) and os.path.isfile(input):42 if input_file and not isinstance(input_file, int) and os.path.isfile(input_file): 39 43 cmd += " < " 40 cmd += input 44 cmd += input_file 41 45 42 46 print(cmd) … … 45 49 with contextlib.ExitStack() as onexit: 46 50 # add input redirection if needed 47 input = openfd(input, 'r', onexit, True)51 input_file = openfd(input_file, 'r', onexit, True) 48 52 49 53 # add output redirection if needed 50 output = openfd(output, 'w', onexit, False)54 output_file = openfd(output_file, 'w', onexit, False) 51 55 52 56 # add error redirection if needed … … 57 61 proc = subprocess.run( 58 62 cmd, 59 stdin =input,60 stdout =output,61 stderr =error,62 timeout =settings.timeout.single if timeout else None63 **({'input' : bytes(input_text, encoding='utf-8')} if input_text else {'stdin' : input_file}), 64 stdout = output_file, 65 stderr = error, 66 timeout = settings.timeout.single if timeout else None 63 67 ) 68 64 69 return proc.returncode, proc.stdout.decode("utf-8") if proc.stdout else None 65 70 except subprocess.TimeoutExpired: … … 74 79 return False 75 80 76 code, out = sh("file %s" % fname, output =subprocess.PIPE)81 code, out = sh("file %s" % fname, output_file=subprocess.PIPE) 77 82 if code != 0: 78 83 return False … … 106 111 if isinstance(files, str ): files = [ files ] 107 112 for file in files: 108 sh( 'rm', '-f', file, output =subprocess.DEVNULL, error=subprocess.DEVNULL )113 sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL ) 109 114 110 115 # Create 1 or more directory … … 114 119 p = os.path.normpath( file ) 115 120 d = os.path.dirname ( p ) 116 sh( 'mkdir', '-p', d, output =subprocess.DEVNULL, error=subprocess.DEVNULL )121 sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL ) 117 122 118 123 … … 137 142 lhs, 138 143 rhs, 139 output =subprocess.PIPE144 output_file=subprocess.PIPE 140 145 ) 141 146 142 147 # call make 143 def make(target, *, flags = '', output = None, error = None, error_file = None, silent = False):148 def make(target, *, flags = '', output_file = None, error = None, error_file = None, silent = False): 144 149 test_param = """test="%s" """ % (error_file) if error_file else None 145 150 cmd = [ … … 150 155 settings.debug.flags, 151 156 settings.install.flags, 157 settings.distcc if settings.distribute else None, 152 158 flags, 153 159 target 154 160 ] 155 161 cmd = [s for s in cmd if s] 156 return sh(*cmd, output =output, error=error)162 return sh(*cmd, output_file=output_file, error=error) 157 163 158 164 def which(program): … … 200 206 # cat one file into the other 201 207 def cat(source, dest): 202 ret, _ = sh("cat", source, output =dest)208 ret, _ = sh("cat", source, output_file=dest) 203 209 return ret 204 210 … … 255 261 os.write(int(make_jobs_fds.group(3)), tokens) 256 262 else : 257 options.jobs = multiprocessing.cpu_count() 263 if settings.distribute: 264 ret, jstr = sh("distcc", "-j", output_file=subprocess.PIPE, ignore_dry_run=True) 265 if ret == 0: 266 options.jobs = int(jstr.strip()) 267 else : 268 options.jobs = multiprocessing.cpu_count() 269 else: 270 options.jobs = multiprocessing.cpu_count() 258 271 else : 259 272 force = True … … 272 285 # misc 273 286 ################################################################################ 287 288 # get hash for given configuration 289 def config_hash(): 290 path = os.path.normpath(os.path.join( 291 settings.SRCDIR, 292 )) 293 294 distcc_hash = os.path.join(settings.SRCDIR, '../tools/build/distcc_hash') 295 config = "%s-%s" % (settings.arch.target, settings.debug.path) 296 _, out = sh(distcc_hash, config, output_file=subprocess.PIPE, ignore_dry_run=True) 297 return out.strip() 298 299 # get pretty string for time of day 300 def pretty_now(): 301 ts = time.time() 302 print(ts, file=sys.stderr) 303 return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S') 274 304 275 305 # check if arguments is yes or no … … 302 332 return 1, "ERR No core dump" 303 333 304 return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output=subprocess.PIPE) 334 return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output_file=subprocess.PIPE) 335 336 def core_archive(dst, name, exe): 337 # Get the core dump 338 core = os.path.join(os.getcwd(), "core" ) 339 340 # update the path for this test 341 dst = os.path.join(dst, name) 342 343 # make a directory for this test 344 # mkdir makes the parent directory only so add a dummy 345 mkdir(os.path.join(dst, "dir")) 346 347 # moves the files 348 mv( core, os.path.join(dst, "core" ) ) 349 mv( exe , os.path.join(dst, name ) ) 350 351 # return explanatory test 352 return "Archiving %s (executable and core) to %s" % (os.path.relpath(exe, settings.BUILDDIR), os.path.relpath(dst, settings.original_path)) 305 353 306 354 class Timed:
Note:
See TracChangeset
for help on using the changeset viewer.