pylib/mozhg/mozhg/mozbuildinfo.py
author Steven MacLeod <smacleod@mozilla.com>
Mon, 17 Oct 2016 18:52:08 -0400
changeset 4817 407adc612136435f4fa14f1f9f29f9c1e8eed2f6
parent 2751 142431d76639dae1d570a4a7c4f8131bd5bec86d
child 7083 7ede0a306189fc047bd08e650da394561b2e96b8
permissions -rw-r--r--
vagrant: add openssl-devel to installation (Bug 1310800). r=mars New versions of the centos7 box require explicit installation of openssl-devel in order to compile python crypto. MozReview-Commit-ID: IPlFSL6SXiB

# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

import mercurial.error as error

import mozbuild.frontend.context as mbcontext
import mozbuild.frontend.reader as reader
import mozpack.hg as mozpackhg


def filesinfo(repo, ctx, paths=None):
    """Obtain mozbuild files info for a changectx and list of files.

    If the list of files is not specified, the files changed by the changeset
    are used.

    Returns a dict containing moz.build derived file info. The dict has a
    ``files`` key which contains a dict of file path to dict of metadata.
    The ``aggregate`` key contains a dict of additional, aggregate metadata
    that applies to all requests paths.

    WARNING: RISK OF UNTRUSTED CODE EXECUTION.

    moz.build files are Python files and evaluating them requires executing
    Python code. While moz.build files are sandboxed, Python sandboxes aren't
    sufficient security sandboxes. A specially crafted moz.build file could
    almost certainly escape the sandbox and gain access to a) the calling
    Python's frames and variables b) the ability to import arbitrary Python
    modules c) access to the filesystem and the ability to execute any program
    therein. THIS FUNCTION SHOULD ONLY BE CALLED BY PRIVILEGE LIMITED USERS
    AND PROCESSES.
    """

    # This method only works if the repository has a moz.build file in the
    # root directory, as the moz.build file info reading mode requires one.
    if 'moz.build' not in ctx:
        return None

    paths = paths or ctx.files()
    if not paths:
        return None

    finder = mozpackhg.MercurialNativeRevisionFinder(repo, ctx.rev(),
            recognize_repo_paths=True)

    config = reader.EmptyConfig(repo.root)
    br = reader.BuildReader(config, finder=finder)
    info = br.files_info(paths)

    return {
        'files': {p: f.asdict() for p, f in info.items()},
        'aggregate': mbcontext.Files.aggregate(info),
    }