• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / mozregression / 12010897098

25 Nov 2024 01:19PM CUT coverage: 89.405%. First build
12010897098

Pull #1890

github

web-flow
Merge ebbe71393 into ec74e4071
Pull Request #1890: build(deps): bump taskcluster from 68.0.4 to 75.0.0

2523 of 2822 relevant lines covered (89.4%)

13.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.64
/mozregression/main.py
1
"""
2
Entry point for the mozregression command line.
3
"""
4

5
from __future__ import absolute_import
14✔
6

7
import atexit
14✔
8
import os
14✔
9
import shlex
14✔
10
import sys
14✔
11

12
import colorama
14✔
13
import mozfile
14✔
14
import requests
14✔
15
from mozlog import get_proxy_logger
14✔
16
from requests.exceptions import HTTPError, RequestException
14✔
17

18
from mozregression import __version__
14✔
19
from mozregression.approx_persist import ApproxPersistChooser
14✔
20
from mozregression.bisector import Bisection, Bisector, IntegrationHandler, NightlyHandler
14✔
21
from mozregression.bugzilla import bug_url, find_bugids_in_push
14✔
22
from mozregression.cli import cli
14✔
23
from mozregression.config import DEFAULT_EXPAND, TC_CREDENTIALS_FNAME
14✔
24
from mozregression.download_manager import BuildDownloadManager
14✔
25
from mozregression.errors import GoodBadExpectationError, MozRegressionError
14✔
26
from mozregression.fetch_build_info import IntegrationInfoFetcher, NightlyInfoFetcher
14✔
27
from mozregression.json_pushes import JsonPushes
14✔
28
from mozregression.launchers import REGISTRY as APP_REGISTRY
14✔
29
from mozregression.network import set_http_session
14✔
30
from mozregression.persist_limit import PersistLimit
14✔
31
from mozregression.telemetry import UsageMetrics, get_system_info, send_telemetry_ping_oop
14✔
32
from mozregression.tempdir import safe_mkdtemp
14✔
33
from mozregression.test_runner import CommandTestRunner, ManualTestRunner
14✔
34

35
LOG = get_proxy_logger("main")
14✔
36

37

38
class Application(object):
14✔
39
    def __init__(self, fetch_config, options):
14✔
40
        self.fetch_config = fetch_config
14✔
41
        self.options = options
14✔
42
        self._test_runner = None
14✔
43
        self._bisector = None
14✔
44
        self._build_download_manager = None
14✔
45
        self._download_dir = options.persist
14✔
46
        self._rm_download_dir = False
14✔
47
        if not options.persist:
14✔
48
            self._download_dir = safe_mkdtemp()
14✔
49
            self._rm_download_dir = True
14✔
50
        launcher_class = APP_REGISTRY.get(fetch_config.app_name)
14✔
51
        launcher_class.check_is_runnable()
14✔
52
        # init global profile if required
53
        self._global_profile = None
14✔
54
        if options.profile_persistence in ("clone-first", "reuse"):
14✔
55
            self._global_profile = launcher_class.create_profile(
×
56
                profile=options.profile,
57
                addons=options.addons,
58
                preferences=options.preferences,
59
                clone=options.profile_persistence == "clone-first",
60
            )
61
            options.cmdargs = options.cmdargs + ["--allow-downgrade"]
×
62
        elif options.profile:
14✔
63
            options.cmdargs = options.cmdargs + ["--allow-downgrade"]
14✔
64

65
    def clear(self):
14✔
66
        if self._build_download_manager:
14✔
67
            # cancel all possible downloads
68
            self._build_download_manager.cancel()
14✔
69
        if self._rm_download_dir:
14✔
70
            if self._build_download_manager:
14✔
71
                # we need to wait explicitly for downloading threads completion
72
                # here because it may remove a file in the download dir - and
73
                # in that case we could end up with a race condition when
74
                # we will remove the download dir. See
75
                # https://bugzilla.mozilla.org/show_bug.cgi?id=1231745
76
                self._build_download_manager.wait(raise_if_error=False)
14✔
77
            mozfile.remove(self._download_dir)
14✔
78
        if self._global_profile and self.options.profile_persistence == "clone-first":
14✔
79
            self._global_profile.cleanup()
×
80

81
    @property
14✔
82
    def test_runner(self):
14✔
83
        if self._test_runner is None:
14✔
84
            if self.options.command is None:
14✔
85
                self._test_runner = ManualTestRunner(
14✔
86
                    launcher_kwargs=dict(
87
                        addons=self.options.addons,
88
                        profile=self._global_profile or self.options.profile,
89
                        cmdargs=self.options.cmdargs,
90
                        preferences=self.options.preferences,
91
                        adb_profile_dir=self.options.adb_profile_dir,
92
                    )
93
                )
94
            else:
95
                self._test_runner = CommandTestRunner(self.options.command)
14✔
96
        return self._test_runner
14✔
97

98
    @property
14✔
99
    def bisector(self):
14✔
100
        if self._bisector is None:
14✔
101
            self._bisector = Bisector(
14✔
102
                self.fetch_config,
103
                self.test_runner,
104
                self.build_download_manager,
105
                dl_in_background=self.options.background_dl,
106
                approx_chooser=(
107
                    None if self.options.approx_policy != "auto" else ApproxPersistChooser(7)
108
                ),
109
            )
110
        return self._bisector
14✔
111

112
    @property
14✔
113
    def build_download_manager(self):
14✔
114
        if self._build_download_manager is None:
14✔
115
            background_dl_policy = self.options.background_dl_policy
14✔
116
            if not self.options.persist:
14✔
117
                # cancel background downloads forced
118
                background_dl_policy = "cancel"
14✔
119
            self._build_download_manager = BuildDownloadManager(
14✔
120
                self._download_dir,
121
                background_dl_policy=background_dl_policy,
122
                persist_limit=PersistLimit(self.options.persist_size_limit),
123
            )
124
        return self._build_download_manager
14✔
125

126
    def bisect_nightlies(self):
14✔
127
        good_date, bad_date = self.options.good, self.options.bad
14✔
128
        handler = NightlyHandler(
14✔
129
            find_fix=self.options.find_fix,
130
            ensure_good_and_bad=self.options.mode != "no-first-check",
131
        )
132
        result = self._do_bisect(handler, good_date, bad_date)
14✔
133
        if result == Bisection.FINISHED:
14✔
134
            LOG.info("Got as far as we can go bisecting nightlies...")
14✔
135
            handler.print_range()
14✔
136
            if self.fetch_config.can_go_integration():
14✔
137
                LOG.info("Switching bisection method to taskcluster")
14✔
138
                self.fetch_config.set_repo(self.fetch_config.get_nightly_repo(handler.bad_date))
14✔
139
                return self._bisect_integration(
14✔
140
                    handler.good_revision, handler.bad_revision, expand=DEFAULT_EXPAND
141
                )
142
        elif result == Bisection.USER_EXIT:
14✔
143
            self._print_resume_info(handler)
14✔
144
        else:
145
            # NO_DATA
146
            LOG.info(
14✔
147
                "Unable to get valid builds within the given"
148
                " range. You should try to launch mozregression"
149
                " again with a larger date range."
150
            )
151
            return 1
14✔
152
        return 0
14✔
153

154
    def bisect_integration(self):
14✔
155
        return self._bisect_integration(
14✔
156
            self.options.good,
157
            self.options.bad,
158
            ensure_good_and_bad=self.options.mode != "no-first-check",
159
        )
160

161
    def _bisect_integration(self, good_rev, bad_rev, ensure_good_and_bad=False, expand=0):
14✔
162
        LOG.info(
14✔
163
            "Getting %s builds between %s and %s"
164
            % (self.fetch_config.integration_branch, good_rev, bad_rev)
165
        )
166
        handler = IntegrationHandler(
14✔
167
            find_fix=self.options.find_fix, ensure_good_and_bad=ensure_good_and_bad
168
        )
169
        result = self._do_bisect(handler, good_rev, bad_rev, expand=expand)
14✔
170
        if result == Bisection.FINISHED:
14✔
171
            LOG.info("No more integration revisions, bisection finished.")
14✔
172
            handler.print_range()
14✔
173
            if handler.good_revision == handler.bad_revision:
14✔
174
                LOG.warning(
14✔
175
                    "It seems that you used two changesets that are in"
176
                    " the same push. Check the pushlog url."
177
                )
178
            elif len(handler.build_range) == 2:
×
179
                # range reduced to 2 pushes (at least ones with builds):
180
                # one good, one bad.
181
                result = handler.handle_merge()
×
182
                if result:
×
183
                    branch, good_rev, bad_rev = result
×
184
                    self.fetch_config.set_repo(branch)
×
185
                    return self._bisect_integration(good_rev, bad_rev, expand=DEFAULT_EXPAND)
×
186
                else:
187
                    # This code is broken, it prints out the message even when
188
                    # there are multiple bug numbers or commits in the range.
189
                    # Somebody should fix it before re-enabling it.
190
                    return 0
×
191
                    # print a bug if:
192
                    # (1) there really is only one bad push (and we're not
193
                    # just missing the builds for some intermediate builds)
194
                    # (2) there is only one bug number in that push
195
                    jp = JsonPushes(handler.build_range[1].repo_name)
196
                    num_pushes = len(
197
                        jp.pushes_within_changes(
198
                            handler.build_range[0].changeset,
199
                            handler.build_range[1].changeset,
200
                        )
201
                    )
202
                    if num_pushes == 2:
203
                        bugids = find_bugids_in_push(
204
                            handler.build_range[1].repo_name,
205
                            handler.build_range[1].changeset,
206
                        )
207
                        if len(bugids) == 1:
208
                            word = "fix" if handler.find_fix else "regression"
209
                            LOG.info(
210
                                "Looks like the following bug has the "
211
                                " changes which introduced the"
212
                                " {}:\n{}".format(word, bug_url(bugids[0]))
213
                            )
214
        elif result == Bisection.USER_EXIT:
14✔
215
            self._print_resume_info(handler)
14✔
216
        else:
217
            # NO_DATA. With integration branches, this can not happen if changesets
218
            # are incorrect - so builds are probably too old
219
            LOG.info(
14✔
220
                "There are no build artifacts for these changesets (they are probably too old)."
221
            )
222
            return 1
14✔
223
        return 0
14✔
224

225
    def _do_bisect(self, handler, good, bad, **kwargs):
14✔
226
        try:
14✔
227
            return self.bisector.bisect(handler, good, bad, **kwargs)
14✔
228
        except (KeyboardInterrupt, MozRegressionError, RequestException) as exc:
14✔
229
            if (
14✔
230
                handler.good_revision is not None
231
                and handler.bad_revision is not None
232
                and not isinstance(exc, GoodBadExpectationError)
233
            ):
234
                atexit.register(self._on_exit_print_resume_info, handler)
14✔
235
            raise
14✔
236

237
    def _print_resume_info(self, handler):
14✔
238
        # copy sys.argv, remove every --good/--bad/--repo related argument,
239
        # then add our own
240
        argv = sys.argv[:]
14✔
241
        args = ("--good", "--bad", "-g", "-b", "--good-rev", "--bad-rev", "--repo")
14✔
242
        indexes_to_remove = []
14✔
243
        for i, arg in enumerate(argv):
14✔
244
            if i in indexes_to_remove:
14✔
245
                continue
14✔
246
            for karg in args:
14✔
247
                if karg == arg:
14✔
248
                    # handle '--good 2015-01-01'
249
                    indexes_to_remove.extend((i, i + 1))
14✔
250
                    break
14✔
251
                elif arg.startswith(karg + "="):
14✔
252
                    # handle '--good=2015-01-01'
253
                    indexes_to_remove.append(i)
×
254
                    break
×
255
        for i in reversed(indexes_to_remove):
14✔
256
            del argv[i]
14✔
257

258
        argv.append("--repo=%s" % handler.build_range[0].repo_name)
14✔
259

260
        if hasattr(handler, "good_date"):
14✔
261
            argv.append("--good=%s" % handler.good_date)
14✔
262
            argv.append("--bad=%s" % handler.bad_date)
14✔
263
        else:
264
            argv.append("--good=%s" % handler.good_revision)
14✔
265
            argv.append("--bad=%s" % handler.bad_revision)
14✔
266

267
        LOG.info("To resume, run:")
14✔
268
        LOG.info(" ".join([shlex.quote(arg) for arg in argv]))
14✔
269

270
    def _on_exit_print_resume_info(self, handler):
14✔
271
        handler.print_range()
14✔
272
        self._print_resume_info(handler)
14✔
273

274
    def _launch(self, fetcher_class):
14✔
275
        fetcher = fetcher_class(self.fetch_config)
×
276
        build_info = fetcher.find_build_info(self.options.launch)
×
277
        self.build_download_manager.focus_download(build_info)
×
278
        self.test_runner.run_once(build_info)
×
279

280
    def launch_nightlies(self):
14✔
281
        self._launch(NightlyInfoFetcher)
×
282

283
    def launch_integration(self):
14✔
284
        self._launch(IntegrationInfoFetcher)
×
285

286

287
def pypi_latest_version():
14✔
288
    url = "https://pypi.python.org/pypi/mozregression/json"
14✔
289
    return requests.get(url, timeout=10).json()["info"]["version"]
14✔
290

291

292
def check_mozregression_version():
14✔
293
    try:
14✔
294
        mozregression_version = pypi_latest_version()
14✔
295
    except (RequestException, KeyError, ValueError):
14✔
296
        LOG.critical("Unable to get latest version from pypi.")
14✔
297
        return
14✔
298

299
    if __version__ != mozregression_version:
14✔
300
        LOG.warning(
14✔
301
            "You are using mozregression version %s, "
302
            "however version %s is available." % (__version__, mozregression_version)
303
        )
304

305
        LOG.warning(
14✔
306
            "You should consider upgrading via the 'pip install"
307
            " --upgrade mozregression' command."
308
        )
309

310

311
def main(
14✔
312
    argv=None,
313
    namespace=None,
314
    check_new_version=True,
315
    mozregression_variant="console",
316
):
317
    """
318
    main entry point of mozregression command line.
319
    """
320
    # terminal color support on windows
321
    if os.name == "nt":
14✔
322
        colorama.init()
×
323

324
    config, app = None, None
14✔
325
    try:
14✔
326
        config = cli(argv=argv, namespace=namespace)
14✔
327
        if check_new_version:
14✔
328
            check_mozregression_version()
14✔
329
        config.validate()
14✔
330
        set_http_session(get_defaults={"timeout": config.options.http_timeout})
14✔
331

332
        app = Application(config.fetch_config, config.options)
14✔
333
        send_telemetry_ping_oop(
14✔
334
            UsageMetrics(
335
                variant=mozregression_variant,
336
                appname=config.fetch_config.app_name,
337
                build_type=config.fetch_config.build_type,
338
                good=config.options.good,
339
                bad=config.options.bad,
340
                launch=config.options.launch,
341
                **get_system_info(),
342
            ),
343
            config.enable_telemetry,
344
        )
345

346
        method = getattr(app, config.action)
14✔
347
        sys.exit(method())
14✔
348

349
    except KeyboardInterrupt:
14✔
350
        sys.exit("\nInterrupted.")
14✔
351
    except (MozRegressionError, RequestException) as exc:
14✔
352
        if isinstance(exc, HTTPError) and exc.response.status_code == 401:
14✔
353
            # remove the taskcluster credential file - looks like it's wrong
354
            # anyway. This will force mozregression to ask again next time.
355
            mozfile.remove(TC_CREDENTIALS_FNAME)
×
356
        LOG.error(str(exc)) if config else sys.exit(str(exc))
14✔
357
        sys.exit(1)
14✔
358
    finally:
359
        if app:
14✔
360
            app.clear()
14✔
361

362

363
if __name__ == "__main__":
364
    main()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc