1 | import __main__ |
---|
2 | import argparse |
---|
3 | import contextlib |
---|
4 | import datetime |
---|
5 | import fileinput |
---|
6 | import multiprocessing |
---|
7 | import os |
---|
8 | import re |
---|
9 | import resource |
---|
10 | import signal |
---|
11 | import stat |
---|
12 | import subprocess |
---|
13 | import sys |
---|
14 | import tempfile |
---|
15 | import time |
---|
16 | import types |
---|
17 | |
---|
18 | from pybin import settings |
---|
19 | |
---|
20 | ################################################################################ |
---|
21 | # shell helpers |
---|
22 | ################################################################################ |
---|
23 | |
---|
24 | # helper functions to run terminal commands |
---|
25 | def sh(*cmd, timeout = False, output_file = None, input_file = None, input_text = None, error = subprocess.STDOUT, ignore_dry_run = False, pass_fds = []): |
---|
26 | try: |
---|
27 | cmd = list(cmd) |
---|
28 | |
---|
29 | if input_file and input_text: |
---|
30 | return 401, "Cannot use both text and file inputs" |
---|
31 | |
---|
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 |
---|
38 | |
---|
39 | if error and not isinstance(error, int): |
---|
40 | cmd += " 2> " |
---|
41 | cmd += error |
---|
42 | |
---|
43 | if input_file and not isinstance(input_file, int) and os.path.isfile(input_file): |
---|
44 | cmd += " < " |
---|
45 | cmd += input_file |
---|
46 | |
---|
47 | print(cmd) |
---|
48 | return 0, None |
---|
49 | |
---|
50 | with contextlib.ExitStack() as onexit: |
---|
51 | # add input redirection if needed |
---|
52 | input_file = openfd(input_file, 'r', onexit, True) |
---|
53 | |
---|
54 | # add output redirection if needed |
---|
55 | output_file = openfd(output_file, 'w', onexit, False) |
---|
56 | |
---|
57 | # add error redirection if needed |
---|
58 | error = openfd(error, 'w', onexit, False) |
---|
59 | |
---|
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( |
---|
64 | cmd, |
---|
65 | **({'input' : bytes(input_text, encoding='utf-8')} if input_text else {'stdin' : input_file}), |
---|
66 | stdout = output_file, |
---|
67 | stderr = error, |
---|
68 | pass_fds = pass_fds |
---|
69 | ) as proc: |
---|
70 | |
---|
71 | try: |
---|
72 | out, _ = proc.communicate( |
---|
73 | timeout = settings.timeout.single if timeout else None |
---|
74 | ) |
---|
75 | |
---|
76 | return proc.returncode, out.decode("latin-1") if out else None |
---|
77 | except subprocess.TimeoutExpired: |
---|
78 | if settings.timeout2gdb: |
---|
79 | print("Process {} timeout".format(proc.pid)) |
---|
80 | proc.communicate() |
---|
81 | return 124, str(None) |
---|
82 | else: |
---|
83 | proc.send_signal(signal.SIGABRT) |
---|
84 | proc.communicate() |
---|
85 | return 124, str(None) |
---|
86 | |
---|
87 | except Exception as ex: |
---|
88 | print ("Unexpected error: %s" % ex) |
---|
89 | raise |
---|
90 | |
---|
91 | def is_empty(fname): |
---|
92 | if not os.path.isfile(fname): |
---|
93 | return True |
---|
94 | |
---|
95 | if os.stat(fname).st_size == 0: |
---|
96 | return True |
---|
97 | |
---|
98 | return False |
---|
99 | |
---|
100 | def is_ascii(fname): |
---|
101 | if settings.dry_run: |
---|
102 | print("is_ascii: %s" % fname) |
---|
103 | return (True, "") |
---|
104 | |
---|
105 | if not os.path.isfile(fname): |
---|
106 | return (False, "No file") |
---|
107 | |
---|
108 | code, out = sh("file", fname, output_file=subprocess.PIPE) |
---|
109 | if code != 0: |
---|
110 | return (False, "'file EXPECT' failed with code {}".format(code)) |
---|
111 | |
---|
112 | match = re.search(".*: (.*)", out) |
---|
113 | |
---|
114 | if not match: |
---|
115 | return (False, "Unreadable file type: '{}'".format(out)) |
---|
116 | |
---|
117 | if "ASCII text" in match.group(1): |
---|
118 | return (True, "") |
---|
119 | |
---|
120 | return (False, "File type should be 'ASCII text', was '{}'".format(match.group(1))) |
---|
121 | |
---|
122 | def is_exe(fname): |
---|
123 | return os.path.isfile(fname) and os.access(fname, os.X_OK) |
---|
124 | |
---|
125 | def openfd(file, mode, exitstack, checkfile): |
---|
126 | if not file: |
---|
127 | return file |
---|
128 | |
---|
129 | if isinstance(file, int): |
---|
130 | return file |
---|
131 | |
---|
132 | if checkfile and not os.path.isfile(file): |
---|
133 | return None |
---|
134 | |
---|
135 | file = open(file, mode, encoding="latin-1") # use latin-1 so all chars mean something. |
---|
136 | exitstack.push(file) |
---|
137 | return file |
---|
138 | |
---|
139 | # Remove 1 or more files silently |
---|
140 | def rm( files ): |
---|
141 | if isinstance(files, str ): files = [ files ] |
---|
142 | for file in files: |
---|
143 | sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL ) |
---|
144 | |
---|
145 | # Create 1 or more directory |
---|
146 | def mkdir( files ): |
---|
147 | if isinstance(files, str ): files = [ files ] |
---|
148 | for file in files: |
---|
149 | p = os.path.normpath( file ) |
---|
150 | d = os.path.dirname ( p ) |
---|
151 | sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL ) |
---|
152 | |
---|
153 | |
---|
154 | def chdir( dest = __main__.__file__ ): |
---|
155 | abspath = os.path.abspath(dest) |
---|
156 | dname = os.path.dirname(abspath) |
---|
157 | os.chdir(dname) |
---|
158 | |
---|
159 | # diff two files |
---|
160 | def diff( lhs, rhs ): |
---|
161 | # fetch return code and error from the diff command |
---|
162 | return sh( |
---|
163 | '''diff''', |
---|
164 | '''--text''', |
---|
165 | '''--old-group-format=\t\tmissing lines :\n%<''', |
---|
166 | '''--new-line-format=\t\t%dn\t%L''', |
---|
167 | '''--new-group-format=\t\tnew lines : \n%>''', |
---|
168 | '''--old-line-format=\t\t%dn\t%L''', |
---|
169 | '''--unchanged-group-format=%=''', |
---|
170 | '''--changed-group-format=\t\texpected :\n%<\t\tgot :\n%>''', |
---|
171 | '''--unchanged-line-format=''', |
---|
172 | lhs, |
---|
173 | rhs, |
---|
174 | output_file=subprocess.PIPE |
---|
175 | ) |
---|
176 | |
---|
177 | # call make |
---|
178 | def make(target, *, flags = '', output_file = None, error = None, error_file = None, silent = False): |
---|
179 | test_param = """test="%s" """ % (error_file) if error_file else None |
---|
180 | cmd = [ |
---|
181 | *settings.make, |
---|
182 | '-s' if silent else None, |
---|
183 | test_param, |
---|
184 | settings.ast.flags, |
---|
185 | settings.arch.flags, |
---|
186 | settings.debug.flags, |
---|
187 | settings.install.flags, |
---|
188 | settings.distcc if settings.distribute else None, |
---|
189 | flags, |
---|
190 | target |
---|
191 | ] |
---|
192 | cmd = [s for s in cmd if s] |
---|
193 | return sh(*cmd, output_file=output_file, error=error, pass_fds=settings.make_jobfds) |
---|
194 | |
---|
195 | def make_recon(target): |
---|
196 | cmd = [ |
---|
197 | *settings.make, |
---|
198 | '-W', |
---|
199 | os.path.abspath(os.path.join(settings.BUILDDIR, '../driver/cfa')), |
---|
200 | '--recon', |
---|
201 | target |
---|
202 | ] |
---|
203 | cmd = [s for s in cmd if s] |
---|
204 | return sh(*cmd, output_file=subprocess.PIPE) |
---|
205 | |
---|
206 | def which(program): |
---|
207 | fpath, fname = os.path.split(program) |
---|
208 | if fpath: |
---|
209 | if is_exe(program): |
---|
210 | return program |
---|
211 | else: |
---|
212 | for path in os.environ["PATH"].split(os.pathsep): |
---|
213 | exe_file = os.path.join(path, program) |
---|
214 | if is_exe(exe_file): |
---|
215 | return exe_file |
---|
216 | return None |
---|
217 | |
---|
218 | @contextlib.contextmanager |
---|
219 | def tempdir(): |
---|
220 | cwd = os.getcwd() |
---|
221 | with tempfile.TemporaryDirectory() as temp: |
---|
222 | os.chdir(temp) |
---|
223 | try: |
---|
224 | yield temp |
---|
225 | finally: |
---|
226 | os.chdir(cwd) |
---|
227 | |
---|
228 | def killgroup(): |
---|
229 | try: |
---|
230 | os.killpg(os.getpgrp(), signal.SIGINT) |
---|
231 | except KeyboardInterrupt: |
---|
232 | pass # expected |
---|
233 | except Exception as exc: |
---|
234 | print("Unexpected exception", file=sys.stderr) |
---|
235 | print(exc, file=sys.stderr) |
---|
236 | sys.stderr.flush() |
---|
237 | sys.exit(2) |
---|
238 | |
---|
239 | ################################################################################ |
---|
240 | # file handling |
---|
241 | ################################################################################ |
---|
242 | # move a file |
---|
243 | def mv(source, dest): |
---|
244 | ret, _ = sh("mv", source, dest) |
---|
245 | return ret |
---|
246 | |
---|
247 | # cat one file into the other |
---|
248 | def cat(source, dest): |
---|
249 | ret, _ = sh("cat", source, output_file=dest) |
---|
250 | return ret |
---|
251 | |
---|
252 | # helper function to replace patterns in a file |
---|
253 | def file_replace(fname, pat, s_after): |
---|
254 | if settings.dry_run: |
---|
255 | print("replacing '%s' with '%s' in %s" % (pat, s_after, fname)) |
---|
256 | return |
---|
257 | |
---|
258 | file = fileinput.FileInput(fname, inplace=True, backup='.bak') |
---|
259 | for line in file: |
---|
260 | print(line.replace(pat, s_after), end='') |
---|
261 | file.close() |
---|
262 | |
---|
263 | # helper function to check if a files contains only a specific string |
---|
264 | def file_contains_only(file, text) : |
---|
265 | with open(file, encoding="latin-1") as f: # use latin-1 so all chars mean something. |
---|
266 | ff = f.read().strip() |
---|
267 | result = ff == text.strip() |
---|
268 | |
---|
269 | return result |
---|
270 | |
---|
271 | # transform path to canonical form |
---|
272 | def canonical_path(path): |
---|
273 | abspath = os.path.abspath(os.path.realpath(__main__.__file__)) |
---|
274 | dname = os.path.dirname(abspath) |
---|
275 | return os.path.join(dname, os.path.normpath(path) ) |
---|
276 | |
---|
277 | # compare path even if form is different |
---|
278 | def path_cmp(lhs, rhs): |
---|
279 | return canonical_path( lhs ) == canonical_path( rhs ) |
---|
280 | |
---|
281 | # walk all files in a path |
---|
282 | def path_walk( op ): |
---|
283 | dname = settings.SRCDIR |
---|
284 | for dirname, _, names in os.walk(dname): |
---|
285 | for name in names: |
---|
286 | path = os.path.join(dirname, name) |
---|
287 | op( path ) |
---|
288 | |
---|
289 | ################################################################################ |
---|
290 | # system |
---|
291 | ################################################################################ |
---|
292 | def jobserver_version(): |
---|
293 | make_ret, out = sh('make', '.test_makeflags', '-j2', output_file=subprocess.PIPE) |
---|
294 | if make_ret != 0: |
---|
295 | with open (errf, "r") as myfile: |
---|
296 | error=myfile.read() |
---|
297 | print("ERROR: cannot find Makefile jobserver version", file=sys.stderr) |
---|
298 | print(" test returned : \n%s" % out, file=sys.stderr) |
---|
299 | sys.exit(1) |
---|
300 | |
---|
301 | re_jobs = re.search("--jobserver-(auth|fds)", out) |
---|
302 | if not re_jobs: |
---|
303 | print("ERROR: cannot find Makefile jobserver version", file=sys.stderr) |
---|
304 | print(" MAKEFLAGS are : \n%s" % out, file=sys.stderr) |
---|
305 | sys.exit(1) |
---|
306 | |
---|
307 | return "--jobserver-{}".format(re_jobs.group(1)) |
---|
308 | |
---|
309 | def prep_recursive_make(N): |
---|
310 | if N < 2: |
---|
311 | return [] |
---|
312 | |
---|
313 | # create the pipe |
---|
314 | (r, w) = os.pipe() |
---|
315 | |
---|
316 | # feel it with N-1 tokens, (Why N-1 and not N, I don't know it's in the manpage for make) |
---|
317 | os.write(w, b'+' * (N - 1)); |
---|
318 | |
---|
319 | # prep the flags for make |
---|
320 | make_flags = ["-j{}".format(N), "--jobserver-auth={},{}".format(r, w)] |
---|
321 | |
---|
322 | # tell make about the pipes |
---|
323 | os.environ["MAKEFLAGS"] = os.environ["MFLAGS"] = " ".join(make_flags) |
---|
324 | print(os.environ["MFLAGS"]) |
---|
325 | |
---|
326 | # make sure pass the pipes to our children |
---|
327 | settings.update_make_fds(r, w) |
---|
328 | |
---|
329 | return make_flags |
---|
330 | |
---|
331 | # count number of jobs to create |
---|
332 | def job_count( options ): |
---|
333 | # check if the user already passed in a number of jobs for multi-threading |
---|
334 | make_env = os.environ.get('MAKEFLAGS') |
---|
335 | make_flags = make_env.split() if make_env else None |
---|
336 | jobstr = jobserver_version() |
---|
337 | |
---|
338 | if options.jobs and make_flags: |
---|
339 | print('WARNING: -j options should not be specified when called form Make', file=sys.stderr) |
---|
340 | |
---|
341 | # Top level make is calling the shots, just follow |
---|
342 | if make_flags: |
---|
343 | # do we have -j and --jobserver-... |
---|
344 | jobopt = None |
---|
345 | exists_fds = None |
---|
346 | for f in make_flags: |
---|
347 | jobopt = f if f.startswith("-j") else jobopt |
---|
348 | exists_fds = f if f.startswith(jobstr) else exists_fds |
---|
349 | |
---|
350 | # do we have limited parallelism? |
---|
351 | if exists_fds : |
---|
352 | print("limited {}".format(exists_fds)) |
---|
353 | try: |
---|
354 | rfd, wfd = tuple(exists_fds.split('=')[1].split(',')) |
---|
355 | except: |
---|
356 | print("ERROR: jobserver has unrecoginzable format, was '{}'".format(exists_fds), file=sys.stderr) |
---|
357 | sys.exit(1) |
---|
358 | |
---|
359 | print("{} {}".format(rfd, wfd)) |
---|
360 | |
---|
361 | # read the token pipe to count number of available tokens and restore the pipe |
---|
362 | # this assumes the test suite script isn't invoked in parellel with something else |
---|
363 | tokens = os.read(int(rfd), 65536) |
---|
364 | os.write(int(wfd), tokens) |
---|
365 | |
---|
366 | # the number of tokens is off by one for obscure but well documented reason |
---|
367 | # see man make for more details |
---|
368 | options.jobs = len(tokens) + 1 |
---|
369 | |
---|
370 | # do we have unlimited parallelism? |
---|
371 | elif jobopt and jobopt != "-j1": |
---|
372 | # check that this actually make sense |
---|
373 | if jobopt != "-j": |
---|
374 | print("ERROR: -j option passed by make but no {}, was '{}'".format(jobstr, jobopt), file=sys.stderr) |
---|
375 | sys.exit(1) |
---|
376 | |
---|
377 | # we can create as many things as we want |
---|
378 | # how much hardware do we have? |
---|
379 | if settings.distribute: |
---|
380 | # remote hardware is allowed |
---|
381 | # how much do we have? |
---|
382 | ret, jstr = sh("distcc", "-j", output_file=subprocess.PIPE, ignore_dry_run=True) |
---|
383 | options.jobs = int(jstr.strip()) if ret == 0 else multiprocessing.cpu_count() |
---|
384 | else: |
---|
385 | # remote isn't allowed, use local cpus |
---|
386 | options.jobs = multiprocessing.cpu_count() |
---|
387 | |
---|
388 | make_flags = prep_recursive_make(options.jobs) |
---|
389 | |
---|
390 | |
---|
391 | # then no parallelism |
---|
392 | else: |
---|
393 | options.jobs = 1 |
---|
394 | |
---|
395 | # keep all flags make passed along |
---|
396 | flags = make_flags |
---|
397 | |
---|
398 | # Arguments are calling the shots, fake the top level make |
---|
399 | elif options.jobs: |
---|
400 | # make sure we have a valid number of jobs that corresponds to user input |
---|
401 | if options.jobs <= 0 : |
---|
402 | print('ERROR: Invalid number of jobs', file=sys.stderr) |
---|
403 | sys.exit(1) |
---|
404 | |
---|
405 | flags = prep_recursive_make(options.jobs) |
---|
406 | |
---|
407 | # No one says to run in parallel, then don't |
---|
408 | else : |
---|
409 | options.jobs = 1 |
---|
410 | flags = [] |
---|
411 | |
---|
412 | # Make sure we call make as expected |
---|
413 | settings.update_make_cmd( flags ) |
---|
414 | |
---|
415 | # return the job count |
---|
416 | return options.jobs |
---|
417 | |
---|
418 | # enable core dumps for all the test children |
---|
419 | resource.setrlimit(resource.RLIMIT_CORE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) |
---|
420 | |
---|
421 | ################################################################################ |
---|
422 | # misc |
---|
423 | ################################################################################ |
---|
424 | |
---|
425 | # get hash for given configuration |
---|
426 | def config_hash(): |
---|
427 | path = os.path.normpath(os.path.join( |
---|
428 | settings.SRCDIR, |
---|
429 | )) |
---|
430 | |
---|
431 | distcc_hash = os.path.join(settings.SRCDIR, '../tools/build/distcc_hash') |
---|
432 | config = "%s-%s" % (settings.arch.target, settings.debug.path) |
---|
433 | _, out = sh(distcc_hash, config, output_file=subprocess.PIPE, ignore_dry_run=True) |
---|
434 | return out.strip() |
---|
435 | |
---|
436 | # get pretty string for time of day |
---|
437 | def pretty_now(): |
---|
438 | ts = time.time() |
---|
439 | print(ts, file=sys.stderr) |
---|
440 | return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S') |
---|
441 | |
---|
442 | # check if arguments is yes or no |
---|
443 | def yes_no(string): |
---|
444 | if string == "yes" : |
---|
445 | return True |
---|
446 | if string == "no" : |
---|
447 | return False |
---|
448 | raise argparse.ArgumentTypeError(msg) |
---|
449 | |
---|
450 | # Convert a function that converts a string to one that converts comma separated string. |
---|
451 | def comma_separated(elements): |
---|
452 | return lambda string: [elements(part) for part in string.split(',')] |
---|
453 | |
---|
454 | def fancy_print(text): |
---|
455 | column = which('column') |
---|
456 | if column: |
---|
457 | subprocess.run(column, input=bytes(text + "\n", "UTF-8")) |
---|
458 | else: |
---|
459 | print(text) |
---|
460 | |
---|
461 | |
---|
462 | def core_info(path): |
---|
463 | if not os.path.isfile(path): |
---|
464 | return 1, "ERR Executable path is wrong" |
---|
465 | |
---|
466 | cmd = os.path.join(settings.SRCDIR, "pybin/print-core.gdb") |
---|
467 | if not os.path.isfile(cmd): |
---|
468 | return 1, "ERR Printing format for core dumps not found" |
---|
469 | |
---|
470 | core = os.path.join(os.getcwd(), "core" ) |
---|
471 | |
---|
472 | if not os.path.isfile(core): |
---|
473 | return 1, "ERR No core dump (limit soft: {} hard: {})".format(*resource.getrlimit(resource.RLIMIT_CORE)) |
---|
474 | |
---|
475 | try: |
---|
476 | return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output_file=subprocess.PIPE) |
---|
477 | except: |
---|
478 | return 1, "ERR Could not read core with gdb" |
---|
479 | |
---|
480 | def core_archive(dst, name, exe): |
---|
481 | # Get the core dump |
---|
482 | core = os.path.join(os.getcwd(), "core" ) |
---|
483 | |
---|
484 | # update the path for this test |
---|
485 | dst = os.path.join(dst, name) |
---|
486 | |
---|
487 | # make a directory for this test |
---|
488 | # mkdir makes the parent directory only so add a dummy |
---|
489 | mkdir( os.path.join(dst, "core") ) |
---|
490 | |
---|
491 | # moves the files |
---|
492 | mv( core, os.path.join(dst, "core" ) ) |
---|
493 | mv( exe , os.path.join(dst, "exe" ) ) |
---|
494 | |
---|
495 | # return explanatory test |
---|
496 | return "Archiving %s (executable and core) to %s" % (os.path.relpath(exe, settings.BUILDDIR), os.path.relpath(dst, settings.original_path)) |
---|
497 | |
---|
498 | class Timed: |
---|
499 | def __enter__(self): |
---|
500 | self.start = time.time() |
---|
501 | return self |
---|
502 | |
---|
503 | def __exit__(self, *args): |
---|
504 | self.end = time.time() |
---|
505 | self.duration = self.end - self.start |
---|
506 | |
---|
507 | def timed(src, timeout): |
---|
508 | expire = time.time() + timeout |
---|
509 | i = iter(src) |
---|
510 | with contextlib.suppress(StopIteration): |
---|
511 | while True: |
---|
512 | yield i.next(max(expire - time.time(), 0)) |
---|
513 | |
---|
514 | def fmtDur( duration ): |
---|
515 | if duration : |
---|
516 | hours, rem = divmod(duration, 3600) |
---|
517 | minutes, rem = divmod(rem, 60) |
---|
518 | seconds, millis = divmod(rem, 1) |
---|
519 | return "%2d:%02d.%03d" % (minutes, seconds, millis * 1000) |
---|
520 | return " n/a" |
---|