#!/usr/bin/env python3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Creates a dummy RTS filter file and a dummy inverse filter file if a
  real ones do not exist yet. Real filter files (and their inverse) are
  generated by the RTS binary for suites with any skippable tests. The
  rest of the suites need to have dummy files because gn will expect the
  file to be present.

  Implementation uses try / except because the filter files are written
  relatively close to when this code creates the dummy files.

  The following type of implementation would have a race condition:
  if not os.path.isfile(filter_file):
    open(filter_file, 'w') as fp:
      fp.write('*')
"""
import errno
import os
import sys


def main():
  filter_file = sys.argv[1]
  # '*' is a dummy that means run everything
  write_filter_file(filter_file, '*')

  inverted_filter_file = sys.argv[2]
  # '-*' is a dummy that means run nothing
  write_filter_file(inverted_filter_file, '-*')


def write_filter_file(filter_file, filter_string):
  directory = os.path.dirname(filter_file)
  try:
    os.makedirs(directory)
  except OSError as err:
    if err.errno == errno.EEXIST:
      pass
    else:
      raise
  try:
    fp = os.open(filter_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
  except OSError as err:
    if err.errno == errno.EEXIST:
      pass
    else:
      raise
  else:
    with os.fdopen(fp, 'w') as file_obj:
      file_obj.write(filter_string)


if __name__ == '__main__':
  sys.exit(main())
