1 import os, signal, subprocess, sys
11 class InternalShellError(Exception):
12 def __init__(self, command, message):
13 self.command = command
14 self.message = message
16 kIsWindows = platform.system() == 'Windows'
18 # Don't use close_fds on Windows.
19 kUseCloseFDs = not kIsWindows
21 # Use temporary files to replace /dev/null on Windows.
22 kAvoidDevNull = kIsWindows
24 def executeCommand(command, cwd=None, env=None):
25 p = subprocess.Popen(command, cwd=cwd,
26 stdin=subprocess.PIPE,
27 stdout=subprocess.PIPE,
28 stderr=subprocess.PIPE,
30 out,err = p.communicate()
33 # Detect Ctrl-C in subprocess.
34 if exitCode == -signal.SIGINT:
35 raise KeyboardInterrupt
37 return out, err, exitCode
39 def executeShCmd(cmd, cfg, cwd, results):
40 if isinstance(cmd, ShUtil.Seq):
42 res = executeShCmd(cmd.lhs, cfg, cwd, results)
43 return executeShCmd(cmd.rhs, cfg, cwd, results)
46 raise NotImplementedError,"unsupported test command: '&'"
49 res = executeShCmd(cmd.lhs, cfg, cwd, results)
51 res = executeShCmd(cmd.rhs, cfg, cwd, results)
54 res = executeShCmd(cmd.lhs, cfg, cwd, results)
59 res = executeShCmd(cmd.rhs, cfg, cwd, results)
62 raise ValueError,'Unknown shell command: %r' % cmd.op
64 assert isinstance(cmd, ShUtil.Pipeline)
66 input = subprocess.PIPE
70 # To avoid deadlock, we use a single stderr stream for piped
71 # output. This is null until we have seen some output using
73 for i,j in enumerate(cmd.commands):
74 # Apply the redirections, we use (N,) as a sentinal to indicate stdin,
75 # stdout, stderr for N equal to 0, 1, or 2 respectively. Redirects to or
76 # from a file are represented with a list [file, mode, file-object]
77 # where file-object is initially None.
78 redirects = [(0,), (1,), (2,)]
81 redirects[2] = [r[1], 'w', None]
82 elif r[0] == ('>>',2):
83 redirects[2] = [r[1], 'a', None]
84 elif r[0] == ('>&',2) and r[1] in '012':
85 redirects[2] = redirects[int(r[1])]
86 elif r[0] == ('>&',) or r[0] == ('&>',):
87 redirects[1] = redirects[2] = [r[1], 'w', None]
89 redirects[1] = [r[1], 'w', None]
91 redirects[1] = [r[1], 'a', None]
93 redirects[0] = [r[1], 'r', None]
95 raise NotImplementedError,"Unsupported redirect: %r" % (r,)
97 # Map from the final redirections to something subprocess can handle.
99 for index,r in enumerate(redirects):
104 raise NotImplementedError,"Unsupported redirect for stdin"
106 result = subprocess.PIPE
108 result = subprocess.STDOUT
111 raise NotImplementedError,"Unsupported redirect on stdout"
112 result = subprocess.PIPE
115 if kAvoidDevNull and r[0] == '/dev/null':
116 r[2] = tempfile.TemporaryFile(mode=r[1])
118 r[2] = open(r[0], r[1])
119 # Workaround a Win32 and/or subprocess bug when appending.
121 # FIXME: Actually, this is probably an instance of PR6753.
124 opened_files.append(r[2])
126 final_redirects.append(result)
128 stdin, stdout, stderr = final_redirects
130 # If stderr wants to come from stdout, but stdout isn't a pipe, then put
131 # stderr on a pipe and treat it as stdout.
132 if (stderr == subprocess.STDOUT and stdout != subprocess.PIPE):
133 stderr = subprocess.PIPE
134 stderrIsStdout = True
136 stderrIsStdout = False
138 # Don't allow stderr on a PIPE except for the last
139 # process, this could deadlock.
141 # FIXME: This is slow, but so is deadlock.
142 if stderr == subprocess.PIPE and j != cmd.commands[-1]:
143 stderr = tempfile.TemporaryFile(mode='w+b')
144 stderrTempFiles.append((i, stderr))
146 # Resolve the executable path ourselves.
148 args[0] = Util.which(args[0], cfg.environment['PATH'])
150 raise InternalShellError(j, '%r: command not found' % j.args[0])
152 # Replace uses of /dev/null with temporary files.
154 for i,arg in enumerate(args):
155 if arg == "/dev/null":
156 f = tempfile.NamedTemporaryFile(delete=False)
158 named_temp_files.append(f.name)
161 procs.append(subprocess.Popen(args, cwd=cwd,
165 env = cfg.environment,
166 close_fds = kUseCloseFDs))
168 # Immediately close stdin for any process taking stdin from us.
169 if stdin == subprocess.PIPE:
170 procs[-1].stdin.close()
171 procs[-1].stdin = None
173 # Update the current stdin source.
174 if stdout == subprocess.PIPE:
175 input = procs[-1].stdout
177 input = procs[-1].stderr
179 input = subprocess.PIPE
181 # FIXME: There is probably still deadlock potential here. Yawn.
182 procData = [None] * len(procs)
183 procData[-1] = procs[-1].communicate()
185 for i in range(len(procs) - 1):
186 if procs[i].stdout is not None:
187 out = procs[i].stdout.read()
190 if procs[i].stderr is not None:
191 err = procs[i].stderr.read()
194 procData[i] = (out,err)
196 # Read stderr out of the temp files.
197 for i,f in stderrTempFiles:
199 procData[i] = (procData[i][0], f.read())
202 for i,(out,err) in enumerate(procData):
203 res = procs[i].wait()
204 # Detect Ctrl-C in subprocess.
205 if res == -signal.SIGINT:
206 raise KeyboardInterrupt
208 results.append((cmd.commands[i], out, err, res))
210 # Python treats the exit code as a signed char.
212 exitCode = min(exitCode, res)
214 exitCode = max(exitCode, res)
218 # Explicitly close any redirected files.
219 for f in opened_files:
222 # Remove any named temporary files we created.
223 for f in named_temp_files:
230 exitCode = not exitCode
234 def executeScriptInternal(test, litConfig, tmpBase, commands, cwd):
235 ln = ' &&\n'.join(commands)
237 cmd = ShUtil.ShParser(ln, litConfig.isWindows).parse()
239 return (Test.FAIL, "shell parser error on: %r" % ln)
243 exitCode = executeShCmd(cmd, test.config, cwd, results)
244 except InternalShellError,e:
250 for i,(cmd, cmd_out,cmd_err,res) in enumerate(results):
251 out += 'Command %d: %s\n' % (i, ' '.join('"%s"' % s for s in cmd.args))
252 out += 'Command %d Result: %r\n' % (i, res)
253 out += 'Command %d Output:\n%s\n\n' % (i, cmd_out)
254 out += 'Command %d Stderr:\n%s\n\n' % (i, cmd_err)
256 return out, err, exitCode
258 def executeTclScriptInternal(test, litConfig, tmpBase, commands, cwd):
262 # Given the unfortunate way LLVM's test are written, the line gets
263 # backslash substitution done twice.
264 ln = TclUtil.TclLexer(ln).lex_unquoted(process_all = True)
267 tokens = list(TclUtil.TclLexer(ln).lex())
269 return (Test.FAIL, "Tcl lexer error on: %r" % ln)
271 # Validate there are no control tokens.
273 if not isinstance(t, str):
275 "Invalid test line: %r containing %r" % (ln, t))
278 cmds.append(TclUtil.TclExecCommand(tokens).parse_pipeline())
280 return (Test.FAIL, "Tcl 'exec' parse error on: %r" % ln)
282 if litConfig.useValgrind:
283 for pipeline in cmds:
284 if pipeline.commands:
285 # Only valgrind the first command in each pipeline, to avoid
286 # valgrinding things like grep, not, and FileCheck.
287 cmd = pipeline.commands[0]
288 cmd.args = litConfig.valgrindArgs + cmd.args
292 cmd = ShUtil.Seq(cmd, '&&', c)
294 # FIXME: This is lame, we shouldn't need bash. See PR5240.
295 bashPath = litConfig.getBashPath()
296 if litConfig.useTclAsSh and bashPath:
297 script = tmpBase + '.script'
301 print >>f, 'set -o pipefail'
302 cmd.toShell(f, pipefail = True)
306 print >>sys.stdout, cmd
307 print >>sys.stdout, open(script).read()
311 command = [litConfig.getBashPath(), script]
312 out,err,exitCode = executeCommand(command, cwd=cwd,
313 env=test.config.environment)
315 return out,err,exitCode
319 exitCode = executeShCmd(cmd, test.config, cwd, results)
320 except InternalShellError,e:
321 results.append((e.command, '', e.message + '\n', 255))
326 for i,(cmd, cmd_out, cmd_err, res) in enumerate(results):
327 out += 'Command %d: %s\n' % (i, ' '.join('"%s"' % s for s in cmd.args))
328 out += 'Command %d Result: %r\n' % (i, res)
329 out += 'Command %d Output:\n%s\n\n' % (i, cmd_out)
330 out += 'Command %d Stderr:\n%s\n\n' % (i, cmd_err)
332 return out, err, exitCode
334 def executeScript(test, litConfig, tmpBase, commands, cwd):
335 script = tmpBase + '.script'
336 if litConfig.isWindows:
341 if litConfig.isWindows:
342 f.write('\nif %ERRORLEVEL% NEQ 0 EXIT\n'.join(commands))
344 f.write(' &&\n'.join(commands))
348 if litConfig.isWindows:
349 command = ['cmd','/c', script]
351 command = ['/bin/sh', script]
352 if litConfig.useValgrind:
353 # FIXME: Running valgrind on sh is overkill. We probably could just
354 # run on clang with no real loss.
355 command = litConfig.valgrindArgs + command
357 return executeCommand(command, cwd=cwd, env=test.config.environment)
359 def isExpectedFail(xfails, xtargets, target_triple):
360 # Check if any xfail matches this target.
362 if item == '*' or item in target_triple:
367 # If so, see if it is expected to pass on this target.
369 # FIXME: Rename XTARGET to something that makes sense, like XPASS.
370 for item in xtargets:
371 if item == '*' or item in target_triple:
376 def parseIntegratedTestScript(test, normalize_slashes=False):
377 """parseIntegratedTestScript - Scan an LLVM/Clang style integrated test
378 script and extract the lines to 'RUN' as well as 'XFAIL' and 'XTARGET'
379 information. The RUN lines also will have variable substitution performed.
382 # Get the temporary location, this is always relative to the test suite
383 # root, not test source root.
385 # FIXME: This should not be here?
386 sourcepath = test.getSourcePath()
387 sourcedir = os.path.dirname(sourcepath)
388 execpath = test.getExecPath()
389 execdir,execbase = os.path.split(execpath)
390 tmpBase = os.path.join(execdir, 'Output', execbase)
391 if test.index is not None:
392 tmpBase += '_%d' % test.index
394 # Normalize slashes, if requested.
395 if normalize_slashes:
396 sourcepath = sourcepath.replace('\\', '/')
397 sourcedir = sourcedir.replace('\\', '/')
398 tmpBase = tmpBase.replace('\\', '/')
400 # We use #_MARKER_# to hide %% while we do the other substitutions.
401 substitutions = [('%%', '#_MARKER_#')]
402 substitutions.extend(test.config.substitutions)
403 substitutions.extend([('%s', sourcepath),
406 ('%t', tmpBase + '.tmp'),
407 # FIXME: Remove this once we kill DejaGNU.
408 ('%abs_tmp', tmpBase + '.tmp'),
409 ('#_MARKER_#', '%')])
411 # Collect the test lines from the script.
416 for ln in open(sourcepath):
418 # Isolate the command to run.
419 index = ln.index('RUN:')
422 # Trim trailing whitespace.
425 # Collapse lines with trailing '\\'.
426 if script and script[-1][-1] == '\\':
427 script[-1] = script[-1][:-1] + ln
431 items = ln[ln.index('XFAIL:') + 6:].split(',')
432 xfails.extend([s.strip() for s in items])
433 elif 'XTARGET:' in ln:
434 items = ln[ln.index('XTARGET:') + 8:].split(',')
435 xtargets.extend([s.strip() for s in items])
436 elif 'REQUIRES:' in ln:
437 items = ln[ln.index('REQUIRES:') + 9:].split(',')
438 requires.extend([s.strip() for s in items])
440 # Check for END. lines.
441 if ln[ln.index('END.'):].strip() == 'END.':
444 # Apply substitutions to the script.
446 # Apply substitutions
447 for a,b in substitutions:
450 # Strip the trailing newline and any extra whitespace.
452 script = map(processLine, script)
454 # Verify the script contains a run line.
456 return (Test.UNRESOLVED, "Test has no run line!")
458 # Check for unterminated run lines.
459 if script[-1][-1] == '\\':
460 return (Test.UNRESOLVED, "Test has unterminated run lines (with '\\')")
462 # Check that we have the required features:
463 missing_required_features = [f for f in requires
464 if f not in test.config.available_features]
465 if missing_required_features:
466 msg = ', '.join(missing_required_features)
467 return (Test.UNSUPPORTED,
468 "Test requires the following features: %s" % msg)
470 isXFail = isExpectedFail(xfails, xtargets, test.suite.config.target_triple)
471 return script,isXFail,tmpBase,execdir
473 def formatTestOutput(status, out, err, exitCode, failDueToStderr, script):
474 output = StringIO.StringIO()
475 print >>output, "Script:"
477 print >>output, '\n'.join(script)
479 print >>output, "Exit Code: %r" % exitCode,
481 print >>output, "(but there was output on stderr)"
485 print >>output, "Command Output (stdout):"
490 print >>output, "Command Output (stderr):"
494 return (status, output.getvalue())
496 def executeTclTest(test, litConfig):
497 if test.config.unsupported:
498 return (Test.UNSUPPORTED, 'Test is unsupported')
500 # Parse the test script, normalizing slashes in substitutions on Windows
501 # (since otherwise Tcl style lexing will treat them as escapes).
502 res = parseIntegratedTestScript(test, normalize_slashes=kIsWindows)
506 script, isXFail, tmpBase, execdir = res
508 if litConfig.noExecute:
509 return (Test.PASS, '')
511 # Create the output directory if it does not already exist.
512 Util.mkdir_p(os.path.dirname(tmpBase))
514 res = executeTclScriptInternal(test, litConfig, tmpBase, script, execdir)
518 # Test for failure. In addition to the exit code, Tcl commands are
519 # considered to fail if there is any standard error output.
520 out,err,exitCode = res
522 ok = exitCode != 0 or err
528 ok = exitCode == 0 and not err
537 # Set a flag for formatTestOutput so it can explain why the test was
538 # considered to have failed, despite having an exit code of 0.
539 failDueToStderr = exitCode == 0 and err
541 return formatTestOutput(status, out, err, exitCode, failDueToStderr, script)
543 def executeShTest(test, litConfig, useExternalSh):
544 if test.config.unsupported:
545 return (Test.UNSUPPORTED, 'Test is unsupported')
547 res = parseIntegratedTestScript(test)
551 script, isXFail, tmpBase, execdir = res
553 if litConfig.noExecute:
554 return (Test.PASS, '')
556 # Create the output directory if it does not already exist.
557 Util.mkdir_p(os.path.dirname(tmpBase))
560 res = executeScript(test, litConfig, tmpBase, script, execdir)
562 res = executeScriptInternal(test, litConfig, tmpBase, script, execdir)
566 out,err,exitCode = res
583 # Sh tests are not considered to fail just from stderr output.
584 failDueToStderr = False
586 return formatTestOutput(status, out, err, exitCode, failDueToStderr, script)