Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / telemetry / build_scripts / gen_userinteraction_data.py
blobb12cbde2399b1f410c0da598a0a78aa3adf2002f
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 # Write out UserInteraction information for C++. The UserInteractions are
6 # defined in a file provided as a command-line argument.
8 import sys
9 from os import path
11 from mozparsers import parse_user_interactions
12 from mozparsers.shared_telemetry_utils import ParserError, static_assert
14 COMPONENTS_PATH = path.abspath(
15 path.join(path.dirname(__file__), path.pardir, path.pardir)
17 sys.path.append(
18 path.join(COMPONENTS_PATH, "glean", "build_scripts", "glean_parser_ext")
20 import sys
22 from string_table import StringTable
24 # The banner/text at the top of the generated file.
25 banner = """/* This file is auto-generated, only for internal use in
26 TelemetryUserInteraction.h, see gen_userinteraction_data.py. */
27 """
29 file_header = """\
30 #ifndef mozilla_TelemetryUserInteractionData_h
31 #define mozilla_TelemetryUserInteractionData_h
32 #include "core/UserInteractionInfo.h"
33 """
35 file_footer = """\
36 #endif // mozilla_TelemetryUserInteractionData_h
37 """
40 def write_user_interaction_table(user_interactions, output, string_table):
41 head = """
42 namespace mozilla {
43 namespace Telemetry {
44 namespace UserInteractionID {
45 const static uint32_t UserInteractionCount = %d;
46 } // namespace UserInteractionID
47 } // namespace Telemetry
48 } // namespace mozilla
49 """
51 print(head % len(user_interactions), file=output)
53 print("namespace {", file=output)
55 table_name = "gUserInteractions"
56 print("constexpr UserInteractionInfo %s[] = {" % table_name, file=output)
58 for u in user_interactions:
59 name_index = string_table.stringIndex(u.label)
60 print(" UserInteractionInfo({}),".format(name_index), file=output)
61 print("};", file=output)
63 static_assert(
64 output,
65 "sizeof(%s) <= UINT32_MAX" % table_name,
66 "index overflow of UserInteractionInfo table %s" % table_name,
69 print("} // namespace", file=output)
72 def main(output, *filenames):
73 # Load the UserInteraction data.
74 user_interactions = []
75 for filename in filenames:
76 try:
77 batch = parse_user_interactions.load_user_interactions(filename)
78 user_interactions.extend(batch)
79 except ParserError as ex:
80 print("\nError processing %s:\n%s\n" % (filename, str(ex)), file=sys.stderr)
81 sys.exit(1)
83 # Write the scalar data file.
84 print(banner, file=output)
85 print(file_header, file=output)
87 string_table = StringTable()
89 # Write the data for individual UserInteractions.
90 write_user_interaction_table(user_interactions, output, string_table)
91 print("", file=output)
93 # Write the string table.
94 string_table_name = "gUserInteractionsStringTable"
95 string_table.writeDefinition(output, string_table_name)
96 static_assert(
97 output, "sizeof(%s) <= UINT32_MAX" % string_table_name, "index overflow"
99 print("", file=output)
101 print(file_footer, file=output)
104 if __name__ == "__main__":
105 main(sys.stdout, *sys.argv[1:])