DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (2e5cf37deea0)

VCS Links

Line Code
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

"""Expose Firefox release information."""

import collections
import os

from mercurial.i18n import _
from mercurial import (
    configitems,
    error,
    extensions,
    pycompat,
    registrar,
    revset,
    templateutil,
)
from mercurial.hgweb import (
    webcommands,
    webutil,
)

OUR_DIR = os.path.normpath(os.path.dirname(__file__))
with open(os.path.join(OUR_DIR, '..', 'bootstrap.py')) as f:
    exec(f.read())

import mozautomation.releasedb as releasedb

from mozhg.util import (
    is_firefox_repo,
)


minimumhgversion = b'4.8'
testedwith = b'4.8 4.9 5.0 5.1 5.2'

configtable = {}
configitem = registrar.configitem(configtable)

configitem(b'mozilla', b'enablefirefoxreleases',  # deprecated, use firefox_releasing
           default=configitems.dynamicdefault)
configitem(b'mozilla', b'firefox_releasing',
           default=configitems.dynamicdefault)
configitem(b'mozilla', b'firefoxreleasesdb',
           default=configitems.dynamicdefault)

revsetpredicate = registrar.revsetpredicate()


def extsetup(ui):
    extensions.wrapfunction(webutil, b'changesetentry', changesetentry)

    setattr(webcommands, 'firefoxreleases', firefox_releases_web_command)
    webcommands.__all__.append(b'firefoxreleases')


def db_for_repo(repo):
    """Obtain a FirefoxReleaseDatabase for a repo or None."""
    if not repo.local():
        return None

    if (not repo.ui.configbool(b'mozilla', b'enablefirefoxreleases', False)  # deprecated
            and not repo.ui.configbool(b'mozilla', b'firefox_releasing', False)):
        return None

    if not is_firefox_repo(repo):
        return None

    default = repo.vfs.join(b'firefoxreleases.db')
    path = repo.ui.config(b'mozilla', b'firefoxreleasesdb', default)

    if not os.path.exists(path):
        return None

    return releasedb.FirefoxReleaseDatabase(
        pycompat.sysstr(path), bytestype=pycompat.bytestr
    )


def release_builds(db, repo, filter_unknown_revision=True):
    """Obtain Firefox release builds.

    By default, only builds associated with revisions in this repo are returned.
    """
    for build in db.builds():
        if filter_unknown_revision and build[b'revision'] not in repo:
            continue

        yield build


def release_builds_by_revision(db, repo):
    """Obtain builds indexed by integer revision."""
    by_rev = collections.defaultdict(list)

    for build in db.builds():
        try:
            ctx = repo[build[b'revision']]
        except error.RepoLookupError:
            continue

        by_rev[ctx.rev()].append(build)

    return by_rev


def release_configurations(db, repo):
    """Obtain a set of release configurations seen in this repo.

    Essentially, finds the set of (channel, platform) tuples that are present
    in this repo.
    """
    return db.unique_release_configurations(
        fltr=lambda build: build[b'revision'] in repo)


def _releases_mapped_generator(context, builds):
    """Generates build object mappings for use in the template layer
    """
    for i, build in enumerate(builds):
        build[b'parity'] = pycompat.bytestr(i % 2)
        build[b'anchor'] = releasedb.build_anchor(build)
        yield build


def firefox_releases_web_command(web):
    """Show information about Firefox releases."""

    req = web.req

    repo = web.repo

    db = db_for_repo(repo)
    if not db:
        error_message = b'Firefox release info not available'
        return web.sendtemplate(b'error', error=error_message)

    platform = req.qsparams[b'platform'] if b'platform' in req.qsparams else None
    builds = []

    for build in release_builds(db, repo):
        if platform and build[b'platform'] != platform:
            continue

        builds.append(build)

    releases_mapping_generator = templateutil.mappinggenerator(_releases_mapped_generator, args=(builds,))

    return web.sendtemplate(b'firefoxreleases', releases=releases_mapping_generator)


def release_config(build):
    return build[b'channel'], build[b'platform']


def changesetentry(orig, web, ctx):
    """Add metadata for an individual changeset in hgweb."""
    d = orig(web, ctx)

    d = pycompat.byteskwargs(d)

    repo = web.repo

    db = db_for_repo(repo)
    if not db:
        return pycompat.strkwargs(d)

    releases = release_info_for_changeset(db, repo, ctx)

    if releases[b'this']:
        d[b'firefox_releases_here'] = []
        d[b'firefox_releases_first'] = []

        for config, build in sorted(releases[b'this'].items()):
            build[b'anchor'] = releasedb.build_anchor(build)

            # Set links to previous and future releases.
            if config in releases[b'previous']:
                build[b'previousnode'] = releases[b'previous'][config][b'revision']

            d[b'firefox_releases_here'].append(build)
            d[b'firefox_releases_first'].append(build)

    if releases[b'future']:
        d.setdefault(b'firefox_releases_first', [])

        for config, build in sorted(releases[b'future'].items()):
            build[b'anchor'] = releasedb.build_anchor(build)

            if build not in d[b'firefox_releases_first']:
                d[b'firefox_releases_first'].append(build)

    if releases[b'previous']:
        d[b'firefox_releases_last'] = []

        for config, build in sorted(releases[b'previous'].items()):
            build[b'anchor'] = releasedb.build_anchor(build)

            d[b'firefox_releases_last'].append(build)

    # Used so we don't display "first release with" and "last release without".
    # We omit displaying in this scenario because we're not confident in the
    # data and don't want to take chances with inaccurate data.
    if b'firefox_releases_first' in d and b'firefox_releases_last' in d:
        d[b'have_first_and_last_firefox_releases'] = True

    # Do some template fixes
    # TODO build via a generator
    if b'firefox_releases_first' in d:
        d[b'firefox_releases_first'] = templateutil.mappinglist(d[b'firefox_releases_first'])

    if b'firefox_releases_last' in d:
        d[b'firefox_releases_last'] = templateutil.mappinglist(d[b'firefox_releases_last'])

    if b'firefox_releases_here' in d:
        d[b'firefox_releases_here'] = templateutil.mappinglist(d[b'firefox_releases_here'])

    return pycompat.strkwargs(d)


def release_info_for_changeset(db, repo, ctx):
    """Given a changeset, obtain relevant release info."""
    # Find the previous release before this changeset. We walk ancestors
    # and store the first seen build entry for each release configuration.
    with db.cache_builds():
        revisions = release_builds_by_revision(db, repo)
        configs = release_configurations(db, repo)

    previous_releases = {}
    cl = repo.changelog

    for rev in cl.ancestors([ctx.rev()]):
        if rev not in revisions:
            continue

        for build in revisions[rev]:
            config = release_config(build)
            previous_releases.setdefault(config, build)

        # Found all release configurations. All previous releases identified.
        if len(configs) == len(previous_releases):
            break

    # Find releases on exactly this changeset.
    this_releases = {}

    for build in revisions.get(ctx.rev(), []):
        this_releases[release_config(build)] = build

    # Now find the first releases with this changeset. This is similar to above
    # except we "walk" descendants. Actual descendant walking can be slow
    # because data is indexed by ancestors. Since changelog data is ordered
    # and we have a mapping from revision to builds, we instead iterate over
    # future revisions. When we find a revision with builds, we verify the
    # start node is an ancestor otherwise we keep going.
    #
    # There is potential for this code to consume a lot of CPU. If the
    # start revision is early in the repo and we're searching for a config
    # that doesn't exist, we could perform many isancestor() checks as we
    # traverse to the end of the repo. This can be fixed by capping search
    # length. Another strategy would be conditionally checking isancestor()
    # if that revision has a config we're interested in. The latter is only
    # partial mitigation. But it might be good enough as a first step.
    # Of course, if the underlying data is append-only, then the mapping can
    # be cached.

    first_releases = dict(this_releases)

    for rev in cl.revs(ctx.rev()):
        if len(configs) == len(first_releases):
            break

        if rev not in revisions:
            continue

        # We're not walking descendants. Verify start rev actually is ancestor.
        if not cl.isancestor(ctx.node(), repo[rev].node()):
            continue

        for build in revisions[rev]:
            config = release_config(build)
            first_releases.setdefault(config, build)

    future_releases = {k: v for k, v in first_releases.items()
                       if k not in this_releases}

    return {
        b'previous': previous_releases,
        b'this': this_releases,
        b'first': first_releases,
        b'future': future_releases,
    }


@revsetpredicate(b'firefoxrelease')
def revset_firefoxrelease(repo, subset, x):
    """``firefoxrelease([channel=], [platform=])

    Changesets that have Firefox releases built from them.

    Accepts the following named arguments:

    channel
       Which release channel to look at. e.g. ``nightly``. Multiple channels
       can be delimited by spaces.
    platform
       Which platform to limit builds to. e.g. ``win32``. Multiple platforms
       can be delimited by spaces.

    If multiple filters are requested filters are combined using logical AND.

    If no filters are specified, all revisions having a Firefox release are
    matched.
    """
    args = revset.getargsdict(x, b'firefoxrelease', b'channel platform')

    channels = set()

    if b'channel' in args:
        channels = set(revset.getstring(args[b'channel'],
                                        _(b'channel requires a string')).split())

    platforms = set()

    if b'platform' in args:
        platforms = set(revset.getstring(args[b'platform'],
                                         _(b'platform requires a '
                                           b'string')).split())

    db = db_for_repo(repo)
    if not db:
        repo.ui.warn(_(b'(warning: firefoxrelease() revset not available)\n'))
        return revset.baseset()

    def get_revs():
        for rev, builds in release_builds_by_revision(db, repo).items():
            for build in builds:
                if channels and build[b'channel'] not in channels:
                    continue

                if platforms and build[b'platform'] not in platforms:
                    continue

                yield rev
                break

    return subset & revset.generatorset(get_revs())