add_rts_filters.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. # Copyright (c) 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Creates a dummy RTS filter file and a dummy inverse filter file if a
  6. real ones do not exist yet. Real filter files (and their inverse) are
  7. generated by the RTS binary for suites with any skippable tests. The
  8. rest of the suites need to have dummy files because gn will expect the
  9. file to be present.
  10. Implementation uses try / except because the filter files are written
  11. relatively close to when this code creates the dummy files.
  12. The following type of implementation would have a race condition:
  13. if not os.path.isfile(filter_file):
  14. open(filter_file, 'w') as fp:
  15. fp.write('*')
  16. """
  17. import errno
  18. import os
  19. import sys
  20. def main():
  21. filter_file = sys.argv[1]
  22. # '*' is a dummy that means run everything
  23. write_filter_file(filter_file, '*')
  24. inverted_filter_file = sys.argv[2]
  25. # '-*' is a dummy that means run nothing
  26. write_filter_file(inverted_filter_file, '-*')
  27. def write_filter_file(filter_file, filter_string):
  28. directory = os.path.dirname(filter_file)
  29. try:
  30. os.makedirs(directory)
  31. except OSError as err:
  32. if err.errno == errno.EEXIST:
  33. pass
  34. else:
  35. raise
  36. try:
  37. fp = os.open(filter_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
  38. except OSError as err:
  39. if err.errno == errno.EEXIST:
  40. pass
  41. else:
  42. raise
  43. else:
  44. with os.fdopen(fp, 'w') as file_obj:
  45. file_obj.write(filter_string)
  46. if __name__ == '__main__':
  47. sys.exit(main())