File: generate_feature_manifest.py

package info (click to toggle)
thunderbird 1%3A140.3.1esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,608,628 kB
  • sloc: cpp: 7,671,698; javascript: 5,901,131; ansic: 3,898,955; python: 1,413,270; xml: 653,997; asm: 462,284; java: 180,948; sh: 113,489; makefile: 20,460; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (248 lines) | stat: -rw-r--r-- 9,444 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import json
import sys
from pathlib import Path
from urllib.parse import urlparse

import jsonschema
import yaml
from jsonschema.validators import validator_for

HEADER_LINE = (
    "// This file was generated by generate_feature_manifest.py from FeatureManifest.yaml."
    " DO NOT EDIT.\n"
)

FEATURE_SCHEMA = Path("schemas", "ExperimentFeature.schema.json")

NIMBUS_FALLBACK_PREFS = (
    "constexpr std::pair<nsLiteralCString, nsLiteralCString>"
    "NIMBUS_FALLBACK_PREFS[]{{{}}};"
)

# Do not add new feature IDs to this list! isEarlyStartup is being deprecated.
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1875331 for details.
ALLOWED_ISEARLYSTARTUP_FEATURE_IDS = {
    "aboutwelcome",
    "bounceTrackingProtection",
    "newtab",
    "pocketNewtab",
    "preonboarding",
    "testFeature",
    "upgradeDialog",
}

DISALLOWED_PREFS = {
    # Disabling either of these prefs will cause unenrollment from all active
    # enrollments, which will then cause the pref to reset.
    "app.shield.optoutstudies.enabled": (
        "disabling Nimbus causes immediate unenrollment"
    ),
    "datareporting.healthreport.uploadEnabled": (
        "disabling telemetry causes immediate unenrollment"
    ),
    # Changing the Remote Settings endpoint will cause unenrollment from the recipe.
    "services.settings.server": (
        "changing the Remote Settings endpoint will break clients"
    ),
    "messaging-system.rsexperimentloader.collection": (
        "changing the Nimbus collection will break clients"
    ),
    "nimbus.debug": "internal Nimbus preference for QA",
    # This pref controls the return value of xpc::IsInAutomation(), which is
    # used by code to check if we are in a test.
    "security.turn_off_all_security_so_that_viruses_can_take_over_this_computer": (
        "this pref is automation-only and is unsafe to enable outside tests"
    ),
}


def write_fm_headers(fd):
    fd.write(HEADER_LINE)


def validate_feature_manifest(schema_path, manifest_path, manifest):
    TOPSRCDIR = Path(__file__).parent.parent.parent.parent.parent

    with open(schema_path) as f:
        schema = json.load(f)

    set_prefs = {}
    fallback_prefs = {}

    for feature_id, feature in manifest.items():
        try:
            jsonschema.validate(feature, schema)

            is_early_startup = feature.get("isEarlyStartup", False)
            allowed_is_early_startup = feature_id in ALLOWED_ISEARLYSTARTUP_FEATURE_IDS
            if is_early_startup != allowed_is_early_startup:
                if is_early_startup:
                    print(f"Feature {feature_id} is marked isEarlyStartup: true")
                    print(
                        "isEarlyStartup is deprecated and no new isEarlyStartup features can be added"
                    )
                    print(
                        "See https://bugzilla.mozilla.org/show_bug.cgi?id=1875331 for details"
                    )
                    raise Exception("isEarlyStartup is deprecated")
                else:
                    print(
                        f"Feature {feature_id} is not early startup but is in the allow list."
                    )
                    print("Please remove it from generate_feature_manifest.py")
                raise Exception("isEarlyStartup is deprecated")

            if is_early_startup and feature.get("allowCoenrollment", False):
                raise Exception(
                    "A feature cannot use isEarlyStartup and allowCoenrollment"
                )

            for variable, variable_def in feature.get("variables", {}).items():
                set_pref = variable_def.get("setPref", {}).get("pref")
                if set_pref is None:
                    continue

                if reason := DISALLOWED_PREFS.get(set_pref):
                    raise Exception(
                        f"Pref {set_pref} cannot be controlled by Nimbus: {reason}"
                    )

                if feature.get("allowCoenrollment", False):
                    raise Exception("A feature cannot use co-enrollment and setPref")

                if set_pref in set_prefs:
                    other_feature = set_prefs[set_pref][0]
                    other_variable = set_prefs[set_pref][1]
                    print("Multiple variables cannot declare the same setPref")
                    print(
                        f"{feature_id} variable {variable} wants to set pref {set_pref}"
                    )
                    print(
                        f"{other_feature} variable {other_variable} wants to set pref "
                        f"{set_pref}"
                    )
                    raise Exception("Set prefs are exclusive")

                set_prefs[set_pref] = (feature_id, variable)

                fallback_pref = variable_def.get("fallbackPref")
                if fallback_pref is not None:
                    fallback_prefs[fallback_pref] = (feature_id, variable)

                conflicts = [
                    (
                        "setPref",
                        fallback_pref,
                        "fallbackPref",
                        set_prefs.get(fallback_pref),
                    ),
                    ("fallbackPref", set_pref, "setPref", fallback_prefs.get(set_pref)),
                ]

                for kind, pref, other_kind, conflict in conflicts:
                    if conflict is not None:
                        print(
                            "The same pref cannot be specified in setPref and fallbackPref"
                        )
                        print(
                            f"{feature_id} variable {variable} has specified {kind} {pref}"
                        )
                        print(
                            f"{conflict[0]} variable {conflict[1]} has specified {other_kind} "
                            f"{pref}"
                        )
                        raise Exception("Set prefs and fallback prefs cannot overlap")

            if "schema" in feature:
                schema_path = TOPSRCDIR / feature["schema"]["path"]
                if not schema_path.exists():
                    raise Exception(f"Schema does not exist at {schema_path}")

                with schema_path.open() as f:
                    try:
                        schema_contents = json.load(f)
                    except Exception as e:
                        raise Exception(f"Cannot parse schema for {feature_id}") from e

                try:
                    validator_for(schema_contents).check_schema(schema_contents)
                except Exception as e:
                    raise Exception(f"Invalid schema for {feature_id}") from e

                uri = urlparse(feature["schema"]["uri"])
                if uri.scheme not in ("resource", "chrome"):
                    raise Exception(
                        "Only resource:// and chrome:// URIs are supported for schemas"
                    )

        except Exception as e:
            print("Error while validating FeatureManifest.yaml")
            print(f"On key: {feature_id}")
            print(f"Input file: {manifest_path}")
            raise e


def generate_feature_manifest(fd, input_file):
    write_fm_headers(fd)

    try:
        with open(input_file, encoding="utf-8") as f:
            manifest = yaml.safe_load(f)

        validate_feature_manifest(
            Path(input_file).parent / FEATURE_SCHEMA, input_file, manifest
        )

        fd.write(f"export const FeatureManifest = {json.dumps(manifest)};")
    except OSError as e:
        print(f"{input_file}: error:\n  {e}\n")
        sys.exit(1)


def platform_feature_manifest_array(features):
    entries = []
    for feature, featureData in features.items():
        # Features have to be tagged isEarlyStartup to be accessible
        # to Nimbus platform API
        if not featureData.get("isEarlyStartup", False):
            continue
        entries.extend(
            '{{ "{}_{}"_ns, "{}"_ns }}'.format(
                feature, variable, variableData["fallbackPref"]
            )
            for (variable, variableData) in featureData.get("variables", {}).items()
            if variableData.get("fallbackPref", False)
        )
    return NIMBUS_FALLBACK_PREFS.format(", ".join(entries))


def generate_platform_feature_manifest(fd, input_file):
    write_fm_headers(fd)

    def file_structure(data):
        return "\n".join(
            [
                "#ifndef mozilla_NimbusFeaturesManifest_h",
                "#define mozilla_NimbusFeaturesManifest_h",
                "#include <utility>",
                '#include "mozilla/Maybe.h"',
                '#include "nsStringFwd.h"',
                "namespace mozilla {",
                platform_feature_manifest_array(data),
                '#include "./lib/NimbusFeatureManifest.inc.h"',
                "}  // namespace mozilla",
                "#endif  // mozilla_NimbusFeaturesManifest_h",
            ]
        )

    try:
        with open(input_file, encoding="utf-8") as yaml_input:
            data = yaml.safe_load(yaml_input)
            fd.write(file_structure(data))
    except OSError as e:
        print(f"{input_file}: error:\n  {e}\n")
        sys.exit(1)