check_file_existence.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. # Copyright (c) 2022 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import os
  17. import sys
  18. def main():
  19. parser = argparse.ArgumentParser(
  20. description='Validate that some specific files exist (or not)')
  21. parser.add_argument('--touch', help="Create this file on success")
  22. parser.add_argument('--exists', action='append', default=[], help="Validate that these files exist")
  23. parser.add_argument('--missing', action='append', default=[], help="Validate that these files DO NOT exist")
  24. args = parser.parse_args()
  25. if args.touch:
  26. if os.path.exists(args.touch):
  27. os.remove(args.touch)
  28. for name in args.exists:
  29. if not os.path.exists(name):
  30. print(f"File {name} was NOT FOUND.")
  31. sys.exit(1)
  32. for name in args.missing:
  33. if os.path.exists(name):
  34. print(f"File {name} was FOUND but expected missing.")
  35. sys.exit(1)
  36. if args.touch:
  37. open(args.touch, "wb").close()
  38. sys.exit(0)
  39. if __name__ == '__main__':
  40. main()