1 from __future__ import absolute_import
2 import os, signal, subprocess, sys
7 import lit.ShUtil as ShUtil
8 import lit.Test as Test
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 executeShCmd(cmd, cfg, cwd, results):
25 if isinstance(cmd, ShUtil.Seq):
27 res = executeShCmd(cmd.lhs, cfg, cwd, results)
28 return executeShCmd(cmd.rhs, cfg, cwd, results)
31 raise InternalShellError(cmd,"unsupported shell operator: '&'")
34 res = executeShCmd(cmd.lhs, cfg, cwd, results)
36 res = executeShCmd(cmd.rhs, cfg, cwd, results)
40 res = executeShCmd(cmd.lhs, cfg, cwd, results)
45 res = executeShCmd(cmd.rhs, cfg, cwd, results)
48 raise ValueError('Unknown shell command: %r' % cmd.op)
50 assert isinstance(cmd, ShUtil.Pipeline)
52 input = subprocess.PIPE
56 # To avoid deadlock, we use a single stderr stream for piped
57 # output. This is null until we have seen some output using
59 for i,j in enumerate(cmd.commands):
60 # Apply the redirections, we use (N,) as a sentinel to indicate stdin,
61 # stdout, stderr for N equal to 0, 1, or 2 respectively. Redirects to or
62 # from a file are represented with a list [file, mode, file-object]
63 # where file-object is initially None.
64 redirects = [(0,), (1,), (2,)]
67 redirects[2] = [r[1], 'w', None]
68 elif r[0] == ('>>',2):
69 redirects[2] = [r[1], 'a', None]
70 elif r[0] == ('>&',2) and r[1] in '012':
71 redirects[2] = redirects[int(r[1])]
72 elif r[0] == ('>&',) or r[0] == ('&>',):
73 redirects[1] = redirects[2] = [r[1], 'w', None]
75 redirects[1] = [r[1], 'w', None]
77 redirects[1] = [r[1], 'a', None]
79 redirects[0] = [r[1], 'r', None]
81 raise InternalShellError(j,"Unsupported redirect: %r" % (r,))
83 # Map from the final redirections to something subprocess can handle.
85 for index,r in enumerate(redirects):
90 raise InternalShellError(j,"Unsupported redirect for stdin")
92 result = subprocess.PIPE
94 result = subprocess.STDOUT
97 raise InternalShellError(j,"Unsupported redirect on stdout")
98 result = subprocess.PIPE
101 if kAvoidDevNull and r[0] == '/dev/null':
102 r[2] = tempfile.TemporaryFile(mode=r[1])
104 r[2] = open(r[0], r[1])
105 # Workaround a Win32 and/or subprocess bug when appending.
107 # FIXME: Actually, this is probably an instance of PR6753.
110 opened_files.append(r[2])
112 final_redirects.append(result)
114 stdin, stdout, stderr = final_redirects
116 # If stderr wants to come from stdout, but stdout isn't a pipe, then put
117 # stderr on a pipe and treat it as stdout.
118 if (stderr == subprocess.STDOUT and stdout != subprocess.PIPE):
119 stderr = subprocess.PIPE
120 stderrIsStdout = True
122 stderrIsStdout = False
124 # Don't allow stderr on a PIPE except for the last
125 # process, this could deadlock.
127 # FIXME: This is slow, but so is deadlock.
128 if stderr == subprocess.PIPE and j != cmd.commands[-1]:
129 stderr = tempfile.TemporaryFile(mode='w+b')
130 stderrTempFiles.append((i, stderr))
132 # Resolve the executable path ourselves.
134 executable = lit.util.which(args[0], cfg.environment['PATH'])
136 raise InternalShellError(j, '%r: command not found' % j.args[0])
138 # Replace uses of /dev/null with temporary files.
140 for i,arg in enumerate(args):
141 if arg == "/dev/null":
142 f = tempfile.NamedTemporaryFile(delete=False)
144 named_temp_files.append(f.name)
147 procs.append(subprocess.Popen(args, cwd=cwd,
148 executable = executable,
152 env = cfg.environment,
153 close_fds = kUseCloseFDs))
155 # Immediately close stdin for any process taking stdin from us.
156 if stdin == subprocess.PIPE:
157 procs[-1].stdin.close()
158 procs[-1].stdin = None
160 # Update the current stdin source.
161 if stdout == subprocess.PIPE:
162 input = procs[-1].stdout
164 input = procs[-1].stderr
166 input = subprocess.PIPE
168 # Explicitly close any redirected files. We need to do this now because we
169 # need to release any handles we may have on the temporary files (important
170 # on Win32, for example). Since we have already spawned the subprocess, our
171 # handles have already been transferred so we do not need them anymore.
172 for f in opened_files:
175 # FIXME: There is probably still deadlock potential here. Yawn.
176 procData = [None] * len(procs)
177 procData[-1] = procs[-1].communicate()
179 for i in range(len(procs) - 1):
180 if procs[i].stdout is not None:
181 out = procs[i].stdout.read()
184 if procs[i].stderr is not None:
185 err = procs[i].stderr.read()
188 procData[i] = (out,err)
190 # Read stderr out of the temp files.
191 for i,f in stderrTempFiles:
193 procData[i] = (procData[i][0], f.read())
196 for i,(out,err) in enumerate(procData):
197 res = procs[i].wait()
198 # Detect Ctrl-C in subprocess.
199 if res == -signal.SIGINT:
200 raise KeyboardInterrupt
202 # Ensure the resulting output is always of string type.
204 out = str(out.decode('ascii'))
208 err = str(err.decode('ascii'))
212 results.append((cmd.commands[i], out, err, res))
214 # Python treats the exit code as a signed char.
218 exitCode = min(exitCode, res)
220 exitCode = max(exitCode, res)
224 # Remove any named temporary files we created.
225 for f in named_temp_files:
232 exitCode = not exitCode
236 def executeScriptInternal(test, litConfig, tmpBase, commands, cwd):
240 cmds.append(ShUtil.ShParser(ln, litConfig.isWindows,
241 test.config.pipefail).parse())
243 return lit.Test.Result(Test.FAIL, "shell parser error on: %r" % ln)
247 cmd = ShUtil.Seq(cmd, '&&', c)
251 exitCode = executeShCmd(cmd, test.config, cwd, results)
252 except InternalShellError:
253 e = sys.exc_info()[1]
255 results.append((e.command, '', e.message, exitCode))
258 for i,(cmd, cmd_out,cmd_err,res) in enumerate(results):
259 out += 'Command %d: %s\n' % (i, ' '.join('"%s"' % s for s in cmd.args))
260 out += 'Command %d Result: %r\n' % (i, res)
261 out += 'Command %d Output:\n%s\n\n' % (i, cmd_out)
262 out += 'Command %d Stderr:\n%s\n\n' % (i, cmd_err)
264 return out, err, exitCode
266 def executeScript(test, litConfig, tmpBase, commands, cwd):
267 bashPath = litConfig.getBashPath();
268 isWin32CMDEXE = (litConfig.isWindows and not bashPath)
269 script = tmpBase + '.script'
275 if litConfig.isWindows and not isWin32CMDEXE:
276 mode += 'b' # Avoid CRLFs when writing bash scripts.
277 f = open(script, mode)
279 f.write('\nif %ERRORLEVEL% NEQ 0 EXIT\n'.join(commands))
281 if test.config.pipefail:
282 f.write('set -o pipefail;')
283 f.write('{ ' + '; } &&\n{ '.join(commands) + '; }')
288 command = ['cmd','/c', script]
291 command = [bashPath, script]
293 command = ['/bin/sh', script]
294 if litConfig.useValgrind:
295 # FIXME: Running valgrind on sh is overkill. We probably could just
296 # run on clang with no real loss.
297 command = litConfig.valgrindArgs + command
299 return lit.util.executeCommand(command, cwd=cwd,
300 env=test.config.environment)
302 def parseIntegratedTestScriptCommands(source_path):
304 parseIntegratedTestScriptCommands(source_path) -> commands
306 Parse the commands in an integrated test script file into a list of
307 (line_number, command_type, line).
310 # This code is carefully written to be dual compatible with Python 2.5+ and
311 # Python 3 without requiring input files to always have valid codings. The
312 # trick we use is to open the file in binary mode and use the regular
313 # expression library to find the commands, with it scanning strings in
314 # Python2 and bytes in Python3.
316 # Once we find a match, we do require each script line to be decodable to
317 # ascii, so we convert the outputs to ascii before returning. This way the
318 # remaining code can work with "strings" agnostic of the executing Python
322 # Encode to Latin1 to get binary data.
323 return str.encode('ISO-8859-1')
324 keywords = ('RUN:', 'XFAIL:', 'REQUIRES:', 'END.')
325 keywords_re = re.compile(
326 to_bytes("(%s)(.*)\n" % ("|".join(k for k in keywords),)))
328 f = open(source_path, 'rb')
330 # Read the entire file contents.
333 # Iterate over the matches.
335 last_match_position = 0
336 for match in keywords_re.finditer(data):
337 # Compute the updated line number by counting the intervening
339 match_position = match.start()
340 line_number += data.count(to_bytes('\n'), last_match_position,
342 last_match_position = match_position
344 # Convert the keyword and line to ascii strings and yield the
345 # command. Note that we take care to return regular strings in
346 # Python 2, to avoid other code having to differentiate between the
347 # str and unicode types.
348 keyword,ln = match.groups()
349 yield (line_number, str(keyword[:-1].decode('ascii')),
350 str(ln.decode('ascii')))
354 def parseIntegratedTestScript(test, normalize_slashes=False,
355 extra_substitutions=[]):
356 """parseIntegratedTestScript - Scan an LLVM/Clang style integrated test
357 script and extract the lines to 'RUN' as well as 'XFAIL' and 'REQUIRES'
358 information. The RUN lines also will have variable substitution performed.
361 # Get the temporary location, this is always relative to the test suite
362 # root, not test source root.
364 # FIXME: This should not be here?
365 sourcepath = test.getSourcePath()
366 sourcedir = os.path.dirname(sourcepath)
367 execpath = test.getExecPath()
368 execdir,execbase = os.path.split(execpath)
369 tmpDir = os.path.join(execdir, 'Output')
370 tmpBase = os.path.join(tmpDir, execbase)
372 # Normalize slashes, if requested.
373 if normalize_slashes:
374 sourcepath = sourcepath.replace('\\', '/')
375 sourcedir = sourcedir.replace('\\', '/')
376 tmpDir = tmpDir.replace('\\', '/')
377 tmpBase = tmpBase.replace('\\', '/')
379 # We use #_MARKER_# to hide %% while we do the other substitutions.
380 substitutions = list(extra_substitutions)
381 substitutions.extend([('%%', '#_MARKER_#')])
382 substitutions.extend(test.config.substitutions)
383 substitutions.extend([('%s', sourcepath),
386 ('%{pathsep}', os.pathsep),
387 ('%t', tmpBase + '.tmp'),
389 ('#_MARKER_#', '%')])
391 # "%/[STpst]" should be normalized.
392 substitutions.extend([
393 ('%/s', sourcepath.replace('\\', '/')),
394 ('%/S', sourcedir.replace('\\', '/')),
395 ('%/p', sourcedir.replace('\\', '/')),
396 ('%/t', tmpBase.replace('\\', '/') + '.tmp'),
397 ('%/T', tmpDir.replace('\\', '/')),
400 # Collect the test lines from the script.
403 for line_number, command_type, ln in \
404 parseIntegratedTestScriptCommands(sourcepath):
405 if command_type == 'RUN':
406 # Trim trailing whitespace.
409 # Substitute line number expressions
410 ln = re.sub('%\(line\)', str(line_number), ln)
411 def replace_line_number(match):
412 if match.group(1) == '+':
413 return str(line_number + int(match.group(2)))
414 if match.group(1) == '-':
415 return str(line_number - int(match.group(2)))
416 ln = re.sub('%\(line *([\+-]) *(\d+)\)', replace_line_number, ln)
418 # Collapse lines with trailing '\\'.
419 if script and script[-1][-1] == '\\':
420 script[-1] = script[-1][:-1] + ln
423 elif command_type == 'XFAIL':
424 test.xfails.extend([s.strip() for s in ln.split(',')])
425 elif command_type == 'REQUIRES':
426 requires.extend([s.strip() for s in ln.split(',')])
427 elif command_type == 'END':
428 # END commands are only honored if the rest of the line is empty.
432 raise ValueError("unknown script command type: %r" % (
435 # Apply substitutions to the script. Allow full regular
436 # expression syntax. Replace each matching occurrence of regular
437 # expression pattern a with substitution b in line ln.
439 # Apply substitutions
440 for a,b in substitutions:
442 b = b.replace("\\","\\\\")
443 ln = re.sub(a, b, ln)
445 # Strip the trailing newline and any extra whitespace.
447 script = [processLine(ln)
450 # Verify the script contains a run line.
452 return lit.Test.Result(Test.UNRESOLVED, "Test has no run line!")
454 # Check for unterminated run lines.
455 if script[-1][-1] == '\\':
456 return lit.Test.Result(Test.UNRESOLVED,
457 "Test has unterminated run lines (with '\\')")
459 # Check that we have the required features:
460 missing_required_features = [f for f in requires
461 if f not in test.config.available_features]
462 if missing_required_features:
463 msg = ', '.join(missing_required_features)
464 return lit.Test.Result(Test.UNSUPPORTED,
465 "Test requires the following features: %s" % msg)
467 return script,tmpBase,execdir
469 def executeShTest(test, litConfig, useExternalSh,
470 extra_substitutions=[]):
471 if test.config.unsupported:
472 return (Test.UNSUPPORTED, 'Test is unsupported')
474 res = parseIntegratedTestScript(test, useExternalSh, extra_substitutions)
475 if isinstance(res, lit.Test.Result):
477 if litConfig.noExecute:
478 return lit.Test.Result(Test.PASS)
480 script, tmpBase, execdir = res
482 # Create the output directory if it does not already exist.
483 lit.util.mkdir_p(os.path.dirname(tmpBase))
486 res = executeScript(test, litConfig, tmpBase, script, execdir)
488 res = executeScriptInternal(test, litConfig, tmpBase, script, execdir)
489 if isinstance(res, lit.Test.Result):
492 out,err,exitCode = res
498 # Form the output log.
499 output = """Script:\n--\n%s\n--\nExit Code: %d\n\n""" % (
500 '\n'.join(script), exitCode)
502 # Append the outputs, if present.
504 output += """Command Output (stdout):\n--\n%s\n--\n""" % (out,)
506 output += """Command Output (stderr):\n--\n%s\n--\n""" % (err,)
508 return lit.Test.Result(status, output)