Eli Bendersky | b922360 | 2011-12-28 10:06:55 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # test/utils.py |
| 3 | # |
| 4 | # Some common utils for tests |
| 5 | # |
| 6 | # Eli Bendersky (eliben@gmail.com) |
| 7 | # This code is in the public domain |
| 8 | #------------------------------------------------------------------------------- |
Eli Bendersky | ccdb554 | 2013-03-31 14:19:21 -0700 | [diff] [blame^] | 9 | import os, sys, subprocess, tempfile |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame] | 10 | from elftools.common.py3compat import bytes2str |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def run_exe(exe_path, args): |
| 14 | """ Runs the given executable as a subprocess, given the |
| 15 | list of arguments. Captures its return code (rc) and stdout and |
| 16 | returns a pair: rc, stdout_str |
| 17 | """ |
| 18 | popen_cmd = [exe_path] + args |
| 19 | if os.path.splitext(exe_path)[1] == '.py': |
Eli Bendersky | ccdb554 | 2013-03-31 14:19:21 -0700 | [diff] [blame^] | 20 | popen_cmd.insert(0, sys.executable) |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 21 | proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE) |
| 22 | proc_stdout = proc.communicate()[0] |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame] | 23 | return proc.returncode, bytes2str(proc_stdout) |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def is_in_rootdir(): |
| 27 | """ Check whether the current dir is the root dir of pyelftools |
| 28 | """ |
| 29 | dirstuff = os.listdir('.') |
| 30 | return 'test' in dirstuff and 'elftools' in dirstuff |
| 31 | |
Eli Bendersky | b922360 | 2011-12-28 10:06:55 +0200 | [diff] [blame] | 32 | |
| 33 | def dump_output_to_temp_files(testlog, *args): |
| 34 | """ Dumps the output strings given in 'args' to temp files: one for each |
| 35 | arg. |
| 36 | """ |
| 37 | for i, s in enumerate(args): |
| 38 | fd, path = tempfile.mkstemp( |
| 39 | prefix='out' + str(i + 1) + '_', |
| 40 | suffix='.stdout') |
| 41 | file = os.fdopen(fd, 'w') |
| 42 | file.write(s) |
| 43 | file.close() |
| 44 | testlog.info('@@ Output #%s dumped to file: %s' % (i + 1, path)) |
| 45 | |