Emil Lundmark | 64a33f2 | 2022-09-21 13:20:22 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
| 2 | |
| 3 | # Copyright (c) 2022 The WebRTC Project Authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
| 11 | import sys |
| 12 | from typing import Set |
| 13 | |
| 14 | import argparse |
| 15 | import dataclasses |
| 16 | |
| 17 | |
| 18 | # TODO(bugs.webrtc.org/14154): End date and bug should also be stored. |
| 19 | @dataclasses.dataclass(frozen=True) |
| 20 | class FieldTrial: |
| 21 | """Representation of all attributes associated with a field trial. |
| 22 | |
| 23 | Attributes: |
| 24 | key: Field trial key. |
| 25 | """ |
| 26 | key: str |
| 27 | |
| 28 | |
| 29 | # As per the policy in `g3doc/field-trials.md`, all field trials should be |
| 30 | # registered in the container below. Please keep the keys sorted. |
| 31 | REGISTERED_FIELD_TRIALS: Set[FieldTrial] = { |
| 32 | FieldTrial(''), # TODO(bugs.webrtc.org/14154): Populate |
| 33 | } |
| 34 | |
| 35 | |
| 36 | def RegistryHeader(field_trials: Set[FieldTrial] = None) -> str: |
| 37 | """Generates a C++ header with all field trial keys. |
| 38 | |
| 39 | Args: |
| 40 | field_trials: Field trials to include in the header. |
| 41 | |
| 42 | Returns: |
| 43 | String representation of a C++ header file containing all field trial keys. |
| 44 | |
| 45 | >>> trials = {FieldTrial('B'), FieldTrial('A'), FieldTrial('B')} |
| 46 | >>> print(RegistryHeader(trials)) |
| 47 | // This file was automatically generated. Do not edit. |
| 48 | <BLANKLINE> |
| 49 | #ifndef GEN_REGISTERED_FIELD_TRIALS_H_ |
| 50 | #define GEN_REGISTERED_FIELD_TRIALS_H_ |
| 51 | <BLANKLINE> |
| 52 | #include "absl/strings/string_view.h" |
| 53 | <BLANKLINE> |
| 54 | namespace webrtc { |
| 55 | <BLANKLINE> |
| 56 | inline constexpr absl::string_view kRegisteredFieldTrials[] = { |
| 57 | "A", |
| 58 | "B", |
| 59 | }; |
| 60 | <BLANKLINE> |
| 61 | } // namespace webrtc |
| 62 | <BLANKLINE> |
| 63 | #endif // GEN_REGISTERED_FIELD_TRIALS_H_ |
| 64 | <BLANKLINE> |
| 65 | """ |
| 66 | if not field_trials: |
| 67 | field_trials = REGISTERED_FIELD_TRIALS |
| 68 | registered_keys = [f.key for f in field_trials] |
| 69 | keys = '\n'.join(f' "{k}",' for k in sorted(registered_keys)) |
| 70 | return ('// This file was automatically generated. Do not edit.\n' |
| 71 | '\n' |
| 72 | '#ifndef GEN_REGISTERED_FIELD_TRIALS_H_\n' |
| 73 | '#define GEN_REGISTERED_FIELD_TRIALS_H_\n' |
| 74 | '\n' |
| 75 | '#include "absl/strings/string_view.h"\n' |
| 76 | '\n' |
| 77 | 'namespace webrtc {\n' |
| 78 | '\n' |
| 79 | 'inline constexpr absl::string_view kRegisteredFieldTrials[] = {\n' |
| 80 | f'{keys}\n' |
| 81 | '};\n' |
| 82 | '\n' |
| 83 | '} // namespace webrtc\n' |
| 84 | '\n' |
| 85 | '#endif // GEN_REGISTERED_FIELD_TRIALS_H_\n') |
| 86 | |
| 87 | |
| 88 | def CmdHeader(args: argparse.Namespace) -> None: |
| 89 | args.output.write(RegistryHeader()) |
| 90 | |
| 91 | |
| 92 | def main() -> None: |
| 93 | parser = argparse.ArgumentParser() |
| 94 | subcommand = parser.add_subparsers(dest='cmd') |
| 95 | parser_header = subcommand.add_parser( |
| 96 | 'header', |
| 97 | help='generate C++ header file containing registered field trial keys') |
| 98 | parser_header.add_argument('--output', |
| 99 | default=sys.stdout, |
| 100 | type=argparse.FileType('w'), |
| 101 | required=False, |
| 102 | help='output file') |
| 103 | parser_header.set_defaults(cmd=CmdHeader) |
| 104 | args = parser.parse_args() |
| 105 | |
| 106 | if not args.cmd: |
| 107 | parser.print_help(sys.stderr) |
| 108 | sys.exit(1) |
| 109 | |
| 110 | args.cmd(args) |
| 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | main() |