blob: 2efc8e3cca0d6fc395a4e6ab22939011d6d42336 [file] [log] [blame]
Eric Fiselier120ec472016-01-19 21:58:49 +00001#===----------------------------------------------------------------------===##
2#
Chandler Carruthd2012102019-01-19 10:56:40 +00003# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Eric Fiselier120ec472016-01-19 21:58:49 +00006#
7#===----------------------------------------------------------------------===##
8
Eric Fiselier7ca90ed2015-01-22 18:05:58 +00009"""not.py is a utility for inverting the return code of commands.
10It acts similar to llvm/utils/not.
11ex: python /path/to/not.py ' echo hello
12 echo $? // (prints 1)
13"""
14
Eric Fiselier7ca90ed2015-01-22 18:05:58 +000015import subprocess
16import sys
17
Eric Fiselier362c5122019-07-12 01:13:05 +000018def which_cannot_find_program(prog):
19 # Allow for import errors on distutils.spawn
20 try:
21 import distutils.spawn
22 prog = distutils.spawn.find_executable(prog[0])
23 if prog is None:
24 sys.stderr.write('Failed to find program %s' % prog[0])
25 return True
26 return False
27 except:
28 return False
Eric Fiselier7ca90ed2015-01-22 18:05:58 +000029
30def main():
31 argv = list(sys.argv)
32 del argv[0]
33 if len(argv) > 0 and argv[0] == '--crash':
34 del argv[0]
35 expectCrash = True
36 else:
37 expectCrash = False
38 if len(argv) == 0:
39 return 1
Eric Fiselier362c5122019-07-12 01:13:05 +000040 if which_cannot_find_program(argv[0]):
Eric Fiselier7ca90ed2015-01-22 18:05:58 +000041 return 1
42 rc = subprocess.call(argv)
43 if rc < 0:
44 return 0 if expectCrash else 1
45 if expectCrash:
46 return 1
47 return rc == 0
48
49
50if __name__ == '__main__':
51 exit(main())