author Mike Hommey <>
Fri, 25 Mar 2016 16:30:42 +0900
changeset 290973 c2d739e9a7bc8463e1c7c29ba624e17c36f1e613
parent 290971 f97bcb2eaf41b62bb7d222f8a627a45e17fbbf54
child 290977 e521d0d6b1bf2fe858519d98e452581953cdee6a
permissions -rw-r--r--
Bug 1257516 - Expose a sandboxed logger to moz.configure and use it. r=ted This removes the warn() function and makes the die() function use the logger instead of print.

# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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

# Templates implementing some generic checks.

# Helper to display "checking" messages
#   @checking('for foo')
#   def foo():
#       return 'foo'
# is equivalent to:
#   def foo():
#'checking for foo... ')
#       ret = foo
#       return ret
# This can be combined with e.g. @depends:
#   @depends(some_option)
#   @checking('for something')
#   def check(value):
#       ...
# An optional callback can be given, that will be used to format the returned
# value when displaying it.
def checking(what, callback=None):
    def decorator(func):
        def wrapped(*args, **kwargs):
  'checking %s... ', what)
            ret = func(*args, **kwargs)
            if callback:
            elif ret is True:
            elif ret is False:
            return ret
        return wrapped
    return decorator

# Template to check for programs in $PATH.
#   check('PROG', ('a', 'b'))
# will look for 'a' or 'b' in $PATH, and set_config PROG to the one
# it can find. If PROG is already set from the environment or command line,
# use that value instead.
def check_prog(var, progs, allow_missing=False):
    option(env=var, nargs=1, help='Path to the %s program' % var.lower())

    if not (isinstance(progs, tuple) or isinstance(progs, list)):
        configure_error('progs should be a list or tuple!')
    progs = list(progs)

    @checking('for %s' % var.lower(), lambda x: x or 'not found')
    def check(value):
        if value:
            progs[:] = value
        for prog in progs:
            result = find_program(prog)
            if result:
                return result

    @depends(check, var)
    def postcheck(value, raw_value):
        if value is None and (not allow_missing or raw_value):
            from mozbuild.shellutil import quote
            die('Cannot find %s (tried: %s)', var.lower(),
                ', '.join(quote(p) for p in progs))

    def normalized_for_config(value):
        return ':' if value is None else value

    set_config(var, normalized_for_config)

    return check