OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright (c) 2011 Google Inc. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 import os | |
8 import tempfile | |
9 import shutil | |
10 import subprocess | |
11 | |
12 | |
13 def TestCommands(commands, files={}, env={}): | |
14 """Run commands in a temporary directory, returning true if they all succeed. | |
15 Return false on failures or if any commands produce output. | |
16 | |
17 Arguments: | |
18 commands: an array of shell-interpretable commands, e.g. ['ls -l', 'pwd'] | |
19 each will be expanded with Python %-expansion using env first. | |
20 files: a dictionary mapping filename to contents; | |
21 files will be created in the temporary directory before running | |
22 the command. | |
23 env: a dictionary of strings to expand commands with. | |
24 """ | |
25 tempdir = tempfile.mkdtemp() | |
26 try: | |
27 for name, contents in files.items(): | |
28 f = open(os.path.join(tempdir, name), 'wb') | |
29 f.write(contents) | |
30 f.close() | |
31 for command in commands: | |
32 proc = subprocess.Popen(command % env, shell=True, | |
33 stdout=subprocess.PIPE, | |
34 stderr=subprocess.STDOUT, | |
35 cwd=tempdir) | |
36 output = proc.communicate()[0] | |
37 if proc.returncode != 0 or output: | |
38 return False | |
39 return True | |
40 finally: | |
41 shutil.rmtree(tempdir) | |
42 return False | |
43 | |
44 | |
45 def TestArSupportsT(ar_command='ar', cc_command='cc'): | |
46 """Test whether 'ar' supports the 'T' flag.""" | |
47 return TestCommands(['%(cc)s -c test.c', | |
48 '%(ar)s crsT test.a test.o', | |
49 '%(cc)s test.a'], | |
50 files={'test.c': 'int main(){}'}, | |
51 env={'ar': ar_command, 'cc': cc_command}) | |
52 | |
53 | |
54 def main(): | |
55 # Run the various test functions and print the results. | |
56 def RunTest(description, function, **kwargs): | |
57 print "Testing " + description + ':', | |
58 if function(**kwargs): | |
59 print 'ok' | |
60 else: | |
61 print 'fail' | |
62 RunTest("ar 'T' flag", TestArSupportsT) | |
63 RunTest("ar 'T' flag with ccache", TestArSupportsT, cc_command='ccache cc') | |
64 return 0 | |
65 | |
66 | |
67 if __name__ == '__main__': | |
68 sys.exit(main()) | |
OLD | NEW |