Eric Fiselier | dd7a483 | 2015-01-22 18:05:58 +0000 | [diff] [blame] | 1 | """not.py is a utility for inverting the return code of commands. |
| 2 | It acts similar to llvm/utils/not. |
| 3 | ex: python /path/to/not.py ' echo hello |
| 4 | echo $? // (prints 1) |
| 5 | """ |
| 6 | |
| 7 | import distutils.spawn |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | |
| 12 | def main(): |
| 13 | argv = list(sys.argv) |
| 14 | del argv[0] |
| 15 | if len(argv) > 0 and argv[0] == '--crash': |
| 16 | del argv[0] |
| 17 | expectCrash = True |
| 18 | else: |
| 19 | expectCrash = False |
| 20 | if len(argv) == 0: |
| 21 | return 1 |
| 22 | prog = distutils.spawn.find_executable(argv[0]) |
| 23 | if prog is None: |
| 24 | sys.stderr.write('Failed to find program %s' % argv[0]) |
| 25 | return 1 |
| 26 | rc = subprocess.call(argv) |
| 27 | if rc < 0: |
| 28 | return 0 if expectCrash else 1 |
| 29 | if expectCrash: |
| 30 | return 1 |
| 31 | return rc == 0 |
| 32 | |
| 33 | |
| 34 | if __name__ == '__main__': |
| 35 | exit(main()) |