source: tests/pybin/tools.py@ 0608e007

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 0608e007 was 8364209, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Tests now send SIGABRT instead and SIGTERM when a test takes too long

  • Property mode set to 100644
File size: 10.2 KB
RevLine 
[c07d724]1import __main__
2import argparse
[a45fc7b]3import contextlib
[dcfedca]4import datetime
[0c13238]5import fileinput
[bacc36c]6import multiprocessing
[c07d724]7import os
8import re
[0c13238]9import resource
[bacc36c]10import signal
[c07d724]11import stat
[1bb2488]12import subprocess
[bacc36c]13import sys
[f806b61]14import tempfile
[0c13238]15import time
[5b993e0]16import types
[c07d724]17
[bacc36c]18from pybin import settings
[c07d724]19
[bacc36c]20################################################################################
21# shell helpers
22################################################################################
23
[c07d724]24# helper functions to run terminal commands
[eea20cd]25def sh(*cmd, timeout = False, output_file = None, input_file = None, input_text = None, error = subprocess.STDOUT, ignore_dry_run = False):
[8364209]26 try:
27 cmd = list(cmd)
[1bb2488]28
[8364209]29 if input_file and input_text:
30 return 401, "Cannot use both text and file inputs"
[d65f92c]31
[8364209]32 # if this is a dry_run, only print the commands that would be ran
33 if settings.dry_run and not ignore_dry_run:
34 cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd))
35 if output_file and not isinstance(output_file, int):
36 cmd += " > "
37 cmd += output_file
[ea62265]38
[8364209]39 if error and not isinstance(error, int):
40 cmd += " 2> "
41 cmd += error
[ea62265]42
[8364209]43 if input_file and not isinstance(input_file, int) and os.path.isfile(input_file):
44 cmd += " < "
45 cmd += input_file
[ea62265]46
[8364209]47 print(cmd)
48 return 0, None
[bacc36c]49
[8364209]50 with contextlib.ExitStack() as onexit:
51 # add input redirection if needed
52 input_file = openfd(input_file, 'r', onexit, True)
[a45fc7b]53
[8364209]54 # add output redirection if needed
55 output_file = openfd(output_file, 'w', onexit, False)
[f806b61]56
[8364209]57 # add error redirection if needed
58 error = openfd(error, 'w', onexit, False)
[a45fc7b]59
[8364209]60 # run the desired command
61 # use with statement to make sure proc is cleaned
62 # don't use subprocess.run because we want to send SIGABRT on exit
63 with subprocess.Popen(
[2b10f95]64 cmd,
[d65f92c]65 **({'input' : bytes(input_text, encoding='utf-8')} if input_text else {'stdin' : input_file}),
66 stdout = output_file,
[8364209]67 stderr = error
68 ) as proc:
69
70 try:
71 out, _ = proc.communicate(
72 timeout = settings.timeout.single if timeout else None
73 )
74
75 return proc.returncode, out.decode("utf-8") if out else None
76 except subprocess.TimeoutExpired:
77 proc.send_signal(signal.SIGABRT)
78 proc.communicate()
79 return 124, str(None)
80
81 except Exception as ex:
82 print ("Unexpected error: %s" % ex)
83 raise
[c07d724]84
[f85bc15]85def is_ascii(fname):
[202ad72]86 if settings.dry_run:
87 print("is_ascii: %s" % fname)
88 return True
89
[f85bc15]90 if not os.path.isfile(fname):
91 return False
92
[d65f92c]93 code, out = sh("file %s" % fname, output_file=subprocess.PIPE)
[f85bc15]94 if code != 0:
95 return False
96
97 match = re.search(".*: (.*)", out)
98
99 if not match:
100 return False
101
[e1bdccb]102 return match.group(1).startswith("ASCII text")
[f85bc15]103
[5bf1f3e]104def is_exe(fname):
105 return os.path.isfile(fname) and os.access(fname, os.X_OK)
106
[f806b61]107def openfd(file, mode, exitstack, checkfile):
108 if not file:
109 return file
110
111 if isinstance(file, int):
112 return file
113
114 if checkfile and not os.path.isfile(file):
115 return None
116
117 file = open(file, mode)
118 exitstack.push(file)
119 return file
120
[c07d724]121# Remove 1 or more files silently
[bacc36c]122def rm( files ):
[5b993e0]123 if isinstance(files, str ): files = [ files ]
124 for file in files:
[d65f92c]125 sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
[c07d724]126
[a95c117]127# Create 1 or more directory
128def mkdir( files ):
[5b993e0]129 if isinstance(files, str ): files = [ files ]
130 for file in files:
131 p = os.path.normpath( file )
132 d = os.path.dirname ( p )
[d65f92c]133 sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
[28582b2]134
[a95c117]135
[c07d724]136def chdir( dest = __main__.__file__ ):
137 abspath = os.path.abspath(dest)
138 dname = os.path.dirname(abspath)
139 os.chdir(dname)
140
[bacc36c]141# diff two files
142def diff( lhs, rhs ):
143 # fetch return code and error from the diff command
[a45fc7b]144 return sh(
145 '''diff''',
146 '''--text''',
147 '''--old-group-format=\t\tmissing lines :\n%<''',
148 '''--new-line-format=\t\t%dn\t%L''',
149 '''--new-group-format=\t\tnew lines : \n%>''',
150 '''--old-line-format=\t\t%dn\t%L''',
151 '''--unchanged-group-format=%=''',
152 '''--changed-group-format=\t\texpected :\n%<\t\tgot :\n%>''',
153 '''--unchanged-line-format=''',
154 lhs,
155 rhs,
[d65f92c]156 output_file=subprocess.PIPE
[a45fc7b]157 )
[bacc36c]158
159# call make
[d65f92c]160def make(target, *, flags = '', output_file = None, error = None, error_file = None, silent = False):
[a45fc7b]161 test_param = """test="%s" """ % (error_file) if error_file else None
162 cmd = [
163 *settings.make,
164 '-s' if silent else None,
[bacc36c]165 test_param,
[575a6e5]166 settings.arch.flags,
[f3b9efc]167 settings.debug.flags,
[a5121bf]168 settings.install.flags,
[d65f92c]169 settings.distcc if settings.distribute else None,
[bacc36c]170 flags,
[a45fc7b]171 target
172 ]
173 cmd = [s for s in cmd if s]
[d65f92c]174 return sh(*cmd, output_file=output_file, error=error)
[bacc36c]175
[ed45af6]176def which(program):
177 fpath, fname = os.path.split(program)
178 if fpath:
179 if is_exe(program):
180 return program
181 else:
182 for path in os.environ["PATH"].split(os.pathsep):
183 exe_file = os.path.join(path, program)
184 if is_exe(exe_file):
185 return exe_file
186
187 return None
[0c13238]188
[f806b61]189@contextlib.contextmanager
190def tempdir():
191 cwd = os.getcwd()
192 with tempfile.TemporaryDirectory() as temp:
193 os.chdir(temp)
194 try:
195 yield temp
196 finally:
197 os.chdir(cwd)
198
[35a408b7]199def killgroup():
200 try:
201 os.killpg(os.getpgrp(), signal.SIGINT)
202 except KeyboardInterrupt:
203 pass # expected
204 except Exception as exc:
205 print("Unexpected exception", file=sys.stderr)
206 print(exc, file=sys.stderr)
207 sys.stderr.flush()
208 sys.exit(2)
209
[bacc36c]210################################################################################
211# file handling
212################################################################################
[0c13238]213# move a file
214def mv(source, dest):
[a45fc7b]215 ret, _ = sh("mv", source, dest)
[0c13238]216 return ret
217
218# cat one file into the other
219def cat(source, dest):
[d65f92c]220 ret, _ = sh("cat", source, output_file=dest)
[0c13238]221 return ret
[bacc36c]222
[c07d724]223# helper function to replace patterns in a file
224def file_replace(fname, pat, s_after):
[202ad72]225 if settings.dry_run:
226 print("replacing '%s' with '%s' in %s" % (pat, s_after, fname))
227 return
228
[f85bc15]229 file = fileinput.FileInput(fname, inplace=True, backup='.bak')
230 for line in file:
231 print(line.replace(pat, s_after), end='')
232 file.close()
[c07d724]233
[0ad0c55]234# helper function to check if a files contains only a specific string
[5bf1f3e]235def file_contains_only(file, text) :
[c07d724]236 with open(file) as f:
237 ff = f.read().strip()
238 result = ff == text.strip()
239
[5bf1f3e]240 return result
[c07d724]241
[bacc36c]242# transform path to canonical form
[5bf1f3e]243def canonical_path(path):
[f85bc15]244 abspath = os.path.abspath(__main__.__file__)
245 dname = os.path.dirname(abspath)
246 return os.path.join(dname, os.path.normpath(path) )
[c07d724]247
[bacc36c]248# compare path even if form is different
[5bf1f3e]249def path_cmp(lhs, rhs):
250 return canonical_path( lhs ) == canonical_path( rhs )
[c07d724]251
[bacc36c]252# walk all files in a path
[5bf1f3e]253def path_walk( op ):
[56de5932]254 dname = settings.SRCDIR
[5b993e0]255 for dirname, _, names in os.walk(dname):
256 for name in names:
257 path = os.path.join(dirname, name)
258 op( path )
[bacc36c]259
260################################################################################
261# system
262################################################################################
263# count number of jobs to create
[5bf1f3e]264def job_count( options, tests ):
[bacc36c]265 # check if the user already passed in a number of jobs for multi-threading
[d142ec5]266 if not options.jobs:
267 make_flags = os.environ.get('MAKEFLAGS')
268 force = bool(make_flags)
269 make_jobs_fds = re.search("--jobserver-(auth|fds)=\s*([0-9]+),([0-9]+)", make_flags) if make_flags else None
270 if make_jobs_fds :
271 tokens = os.read(int(make_jobs_fds.group(2)), 1024)
272 options.jobs = len(tokens)
273 os.write(int(make_jobs_fds.group(3)), tokens)
274 else :
[34e1494]275 if settings.distribute:
276 ret, jstr = sh("distcc", "-j", output_file=subprocess.PIPE, ignore_dry_run=True)
277 if ret == 0:
278 options.jobs = int(jstr.strip())
279 else :
280 options.jobs = multiprocessing.cpu_count()
281 else:
282 options.jobs = multiprocessing.cpu_count()
[bacc36c]283 else :
[d142ec5]284 force = True
[bacc36c]285
286 # make sure we have a valid number of jobs that corresponds to user input
287 if options.jobs <= 0 :
288 print('ERROR: Invalid number of jobs', file=sys.stderr)
289 sys.exit(1)
290
[d142ec5]291 return min( options.jobs, len(tests) ), force
[bacc36c]292
[0c13238]293# enable core dumps for all the test children
294resource.setrlimit(resource.RLIMIT_CORE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
295
[bacc36c]296################################################################################
297# misc
298################################################################################
299
[d65f92c]300# get hash for given configuration
301def config_hash():
302 path = os.path.normpath(os.path.join(
303 settings.SRCDIR,
304 ))
305
306 distcc_hash = os.path.join(settings.SRCDIR, '../tools/build/distcc_hash')
307 config = "%s-%s" % (settings.arch.target, settings.debug.path)
[eea20cd]308 _, out = sh(distcc_hash, config, output_file=subprocess.PIPE, ignore_dry_run=True)
[d65f92c]309 return out.strip()
310
[5c4a473]311# get pretty string for time of day
[dcfedca]312def pretty_now():
313 ts = time.time()
314 print(ts, file=sys.stderr)
315 return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S')
316
[bacc36c]317# check if arguments is yes or no
318def yes_no(string):
319 if string == "yes" :
320 return True
321 if string == "no" :
322 return False
323 raise argparse.ArgumentTypeError(msg)
[f3b9efc]324
[ed45af6]325def fancy_print(text):
326 column = which('column')
327 if column:
[1bb2488]328 subprocess.run(column, input=bytes(text + "\n", "UTF-8"))
[ed45af6]329 else:
330 print(text)
[0c13238]331
332
[5bf1f3e]333def core_info(path):
[f806b61]334 if not os.path.isfile(path):
335 return 1, "ERR Executable path is wrong"
336
[0c13238]337 cmd = os.path.join(settings.SRCDIR, "pybin/print-core.gdb")
338 if not os.path.isfile(cmd):
339 return 1, "ERR Printing format for core dumps not found"
340
[f806b61]341 core = os.path.join(os.getcwd(), "core" )
[0c13238]342
343 if not os.path.isfile(core):
344 return 1, "ERR No core dump"
345
[d65f92c]346 return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output_file=subprocess.PIPE)
[0c13238]347
[dcfedca]348def core_archive(dst, name, exe):
[143e6f3]349 # Get the core dump
[dcfedca]350 core = os.path.join(os.getcwd(), "core" )
351
[143e6f3]352 # update the path for this test
353 dst = os.path.join(dst, name)
[dcfedca]354
355 # make a directory for this test
[143e6f3]356 # mkdir makes the parent directory only so add a dummy
[ea2074e]357 mkdir(os.path.join(dst, name ))
[dcfedca]358
359 # moves the files
360 mv( core, os.path.join(dst, "core" ) )
361 mv( exe , os.path.join(dst, name ) )
362
363 # return explanatory test
364 return "Archiving %s (executable and core) to %s" % (os.path.relpath(exe, settings.BUILDDIR), os.path.relpath(dst, settings.original_path))
365
[0c13238]366class Timed:
367 def __enter__(self):
368 self.start = time.time()
369 return self
370
371 def __exit__(self, *args):
372 self.end = time.time()
[35a408b7]373 self.duration = self.end - self.start
374
375def timed(src, timeout):
376 expire = time.time() + timeout
377 i = iter(src)
378 while True:
379 yield i.next(max(expire - time.time(), 0))
Note: See TracBrowser for help on using the repository browser.