예제 #1
0
def __load_build_file(source_dir, blade):
    """
    Load and execute the BUILD file, which is a Python script, in source_dir.
    Statements in BUILD depends on global variable current_source_dir, and will
    register build target/rules into global variables target_database.
    Report error and exit if path/BUILD does NOT exist.
    """

    if not os.path.isdir(source_dir):
        _report_not_exist('Directory', source_dir, source_dir, blade)
        return False

    old_current_source_path = blade.get_current_source_path()
    try:
        blade.set_current_source_path(source_dir)
        build_file = os.path.join(source_dir, 'BUILD')
        if os.path.isfile(build_file):
            try:
                # The magic here is that a BUILD file is a Python script,
                # which can be loaded and executed by execfile().
                global __current_globals
                __current_globals = build_rules.get_all()
                exec_file(build_file, __current_globals, None)
                return True
            except SystemExit:
                console.fatal('%s: Fatal error' % build_file)
            except:  # pylint: disable=bare-except
                console.fatal('Parse error in %s\n%s' %
                              (build_file, traceback.format_exc()))
        else:
            _report_not_exist('File', build_file, source_dir, blade)
    finally:
        blade.set_current_source_path(old_current_source_path)

    return False
예제 #2
0
def _load_extension(name):
    """Load symbols from file or obtain from loaded cache."""
    full_path = _expand_include_path(name)
    if full_path in __loaded_extension_info:
        return __loaded_extension_info[full_path]

    if not os.path.isfile(full_path):
        console.diagnose(_current_source_location(), 'error',
                         'File "%s" does not exist' % name)
        return {}

    # The symbols in the current context should be invisible to the extension,
    # make an isolated symbol set to implement this approach.
    origin_globals = build_rules.get_all()
    extension_globals = origin_globals.copy()
    exec_file(full_path, extension_globals, None)
    # Extract new symbols
    result = {}
    for symbol, value in extension_globals.items():
        if symbol.startswith('_'):
            continue
        if symbol in origin_globals and value is origin_globals[symbol]:
            continue
        if isinstance(value, types.ModuleType):
            continue
        result[symbol] = value
    __loaded_extension_info[full_path] = result
    return result
예제 #3
0
def _get_globals_for_build_file(source_dir):
    """Get global variables for BUILD files."""
    result = build_rules.get_all()
    global_config = config.get_section('global_config')
    if global_config.get('restricted_dsl') and source_dir not in global_config.get('unrestricted_dsl_dirs'):
        result['__builtins__'] = restricted.safe_builtins
    result['blade'] = dsl_api.get_blade_module()
    return result
def _load_build_file(source_dir, processed_source_dirs, blade):
    """Load the BUILD and place the targets into database.

    Invoked by _load_targets.  Load and execute the BUILD
    file, which is a Python script, in source_dir.  Statements in BUILD
    depends on global variable current_source_dir, and will register build
    target/rules into global variables target_database.  Report error
    and exit if path/BUILD does NOT exist.
    The parameters processed_source_dirs refers to a set defined in the
    caller and used to avoid duplicated execution of BUILD files.

    """
    source_dir = os.path.normpath(source_dir)
    # TODO(yiwang): the character '#' is a magic value.
    if source_dir in processed_source_dirs or source_dir == '#':
        return
    processed_source_dirs.add(source_dir)

    if not os.path.exists(source_dir):
        _report_not_exist('Directory', source_dir, source_dir, blade)
        return

    old_current_source_path = blade.get_current_source_path()
    try:
        blade.set_current_source_path(source_dir)
        build_file = os.path.join(source_dir, 'BUILD')
        if os.path.exists(build_file) and not os.path.isdir(build_file):
            try:
                # The magic here is that a BUILD file is a Python script,
                # which can be loaded and executed by execfile().
                global __current_globles
                __current_globles = build_rules.get_all()
                exec_file(build_file, __current_globles, None)
            except SystemExit:
                console.fatal('%s: Fatal error' % build_file)
            except:  # pylint: disable=bare-except
                console.fatal('Parse error in %s\n%s' % (
                    build_file, traceback.format_exc()))
        else:
            _report_not_exist('File', build_file, source_dir, blade)
    finally:
        blade.set_current_source_path(old_current_source_path)
예제 #5
0
def _load_build_file(source_dir, processed_source_dirs, blade):
    """Load the BUILD and place the targets into database.

    Invoked by _load_targets.  Load and execute the BUILD
    file, which is a Python script, in source_dir.  Statements in BUILD
    depends on global variable current_source_dir, and will register build
    target/rules into global variables target_database.  Report error
    and exit if path/BUILD does NOT exist.
    The parameters processed_source_dirs refers to a set defined in the
    caller and used to avoid duplicated execution of BUILD files.

    """
    source_dir = os.path.normpath(source_dir)
    # TODO(yiwang): the character '#' is a magic value.
    if source_dir in processed_source_dirs or source_dir == '#':
        return
    processed_source_dirs.add(source_dir)

    if not os.path.exists(source_dir):
        _report_not_exist(source_dir, source_dir, blade)

    old_current_source_path = blade.get_current_source_path()
    blade.set_current_source_path(source_dir)
    build_file = os.path.join(source_dir, 'BUILD')
    if os.path.exists(build_file) and not os.path.isdir(build_file):
        try:
            # The magic here is that a BUILD file is a Python script,
            # which can be loaded and executed by execfile().
            global __current_globles
            __current_globles = build_rules.get_all()
            execfile(build_file, __current_globles, None)
        except SystemExit:
            console.error_exit('%s: fatal error' % build_file)
        except:  # pylint: disable=bare-except
            console.error_exit('Parse error in %s\n%s' % (
                    build_file, traceback.format_exc()))
    else:
        _report_not_exist(source_dir, build_file, blade)

    blade.set_current_source_path(old_current_source_path)
예제 #6
0
def _load_extension(name):
    """Load symbols from file or obtain from loaded cache."""
    full_path = _expand_include_path(name)
    if full_path in __loaded_extension_info:
        return __loaded_extension_info[full_path]
    # The symbols in the current context should be invisible to the extension,
    # make an isolated symbol set to implement this approach.
    origin_globals = build_rules.get_all()
    extension_globals = origin_globals.copy()
    exec_file(full_path, extension_globals, None)
    # Extract new symbols
    result = {}
    for symbol, value in extension_globals.items():
        if symbol.startswith('_'):
            continue
        if symbol in origin_globals and value is origin_globals[symbol]:
            continue
        if type(value) is types.ModuleType:
            continue
        result[symbol] = value
    __loaded_extension_info[full_path] = result
    return result