|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2024 The Chromium Authors |
| 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 if a real ones do not exist yet. |
| 6 | + Real filter files are generated for suites with skippable tests. |
| 7 | + Not every test suite will have filter data to use and therefore |
| 8 | + no filter file will be created. This ensures that a file exists |
| 9 | + to avoid file not found errors. The files will contain no skippable |
| 10 | + tests, so there is no effect. |
| 11 | +
|
| 12 | + Implementation uses try / except because the filter files are written |
| 13 | + relatively close to when this code creates the dummy files. |
| 14 | +
|
| 15 | + The following type of implementation would have a race condition: |
| 16 | + if not os.path.isfile(filter_file): |
| 17 | + open(filter_file, 'w') as fp: |
| 18 | + fp.write('*') |
| 19 | +""" |
| 20 | +import errno |
| 21 | +import os |
| 22 | +import sys |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + filter_file = sys.argv[1] |
| 27 | + # '*' is a dummy that means run everything |
| 28 | + write_filter_file(filter_file, '*') |
| 29 | + |
| 30 | + |
| 31 | +def write_filter_file(filter_file, filter_string): |
| 32 | + directory = os.path.dirname(filter_file) |
| 33 | + try: |
| 34 | + os.makedirs(directory) |
| 35 | + except OSError as err: |
| 36 | + if err.errno == errno.EEXIST: |
| 37 | + pass |
| 38 | + else: |
| 39 | + raise |
| 40 | + try: |
| 41 | + fp = os.open(filter_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY) |
| 42 | + except OSError as err: |
| 43 | + if err.errno == errno.EEXIST: |
| 44 | + pass |
| 45 | + else: |
| 46 | + raise |
| 47 | + else: |
| 48 | + with os.fdopen(fp, 'w') as file_obj: |
| 49 | + file_obj.write(filter_string) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + sys.exit(main()) |
0 commit comments