xref: /aosp_15_r20/external/libcxx/utils/not.py (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker#===----------------------------------------------------------------------===##
2*58b9f456SAndroid Build Coastguard Worker#
3*58b9f456SAndroid Build Coastguard Worker#                     The LLVM Compiler Infrastructure
4*58b9f456SAndroid Build Coastguard Worker#
5*58b9f456SAndroid Build Coastguard Worker# This file is dual licensed under the MIT and the University of Illinois Open
6*58b9f456SAndroid Build Coastguard Worker# Source Licenses. See LICENSE.TXT for details.
7*58b9f456SAndroid Build Coastguard Worker#
8*58b9f456SAndroid Build Coastguard Worker#===----------------------------------------------------------------------===##
9*58b9f456SAndroid Build Coastguard Worker
10*58b9f456SAndroid Build Coastguard Worker"""not.py is a utility for inverting the return code of commands.
11*58b9f456SAndroid Build Coastguard WorkerIt acts similar to llvm/utils/not.
12*58b9f456SAndroid Build Coastguard Workerex: python /path/to/not.py ' echo hello
13*58b9f456SAndroid Build Coastguard Worker    echo $? // (prints 1)
14*58b9f456SAndroid Build Coastguard Worker"""
15*58b9f456SAndroid Build Coastguard Worker
16*58b9f456SAndroid Build Coastguard Workerimport distutils.spawn
17*58b9f456SAndroid Build Coastguard Workerimport subprocess
18*58b9f456SAndroid Build Coastguard Workerimport sys
19*58b9f456SAndroid Build Coastguard Worker
20*58b9f456SAndroid Build Coastguard Worker
21*58b9f456SAndroid Build Coastguard Workerdef main():
22*58b9f456SAndroid Build Coastguard Worker    argv = list(sys.argv)
23*58b9f456SAndroid Build Coastguard Worker    del argv[0]
24*58b9f456SAndroid Build Coastguard Worker    if len(argv) > 0 and argv[0] == '--crash':
25*58b9f456SAndroid Build Coastguard Worker        del argv[0]
26*58b9f456SAndroid Build Coastguard Worker        expectCrash = True
27*58b9f456SAndroid Build Coastguard Worker    else:
28*58b9f456SAndroid Build Coastguard Worker        expectCrash = False
29*58b9f456SAndroid Build Coastguard Worker    if len(argv) == 0:
30*58b9f456SAndroid Build Coastguard Worker        return 1
31*58b9f456SAndroid Build Coastguard Worker    prog = distutils.spawn.find_executable(argv[0])
32*58b9f456SAndroid Build Coastguard Worker    if prog is None:
33*58b9f456SAndroid Build Coastguard Worker        sys.stderr.write('Failed to find program %s' % argv[0])
34*58b9f456SAndroid Build Coastguard Worker        return 1
35*58b9f456SAndroid Build Coastguard Worker    rc = subprocess.call(argv)
36*58b9f456SAndroid Build Coastguard Worker    if rc < 0:
37*58b9f456SAndroid Build Coastguard Worker        return 0 if expectCrash else 1
38*58b9f456SAndroid Build Coastguard Worker    if expectCrash:
39*58b9f456SAndroid Build Coastguard Worker        return 1
40*58b9f456SAndroid Build Coastguard Worker    return rc == 0
41*58b9f456SAndroid Build Coastguard Worker
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard Workerif __name__ == '__main__':
44*58b9f456SAndroid Build Coastguard Worker    exit(main())
45