not.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #===----------------------------------------------------------------------===##
  2. #
  3. # 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
  6. #
  7. #===----------------------------------------------------------------------===##
  8. """not.py is a utility for inverting the return code of commands.
  9. It acts similar to llvm/utils/not.
  10. ex: python /path/to/not.py ' echo hello
  11. echo $? // (prints 1)
  12. """
  13. import subprocess
  14. import sys
  15. def which_cannot_find_program(prog):
  16. # Allow for import errors on distutils.spawn
  17. try:
  18. import distutils.spawn
  19. prog = distutils.spawn.find_executable(prog[0])
  20. if prog is None:
  21. sys.stderr.write('Failed to find program %s' % prog[0])
  22. return True
  23. return False
  24. except:
  25. return False
  26. def main():
  27. argv = list(sys.argv)
  28. del argv[0]
  29. if len(argv) > 0 and argv[0] == '--crash':
  30. del argv[0]
  31. expectCrash = True
  32. else:
  33. expectCrash = False
  34. if len(argv) == 0:
  35. return 1
  36. if which_cannot_find_program(argv[0]):
  37. return 1
  38. rc = subprocess.call(argv)
  39. if rc < 0:
  40. return 0 if expectCrash else 1
  41. if expectCrash:
  42. return 1
  43. return rc == 0
  44. if __name__ == '__main__':
  45. exit(main())