示例#1
0
def SubRespire(build_filepath,
               params_file,
               out_dir,
               function_name,
               timestamp_file=None):
    with open(params_file, 'r') as f:
        params_content = f.read()

    registry_files_basename = registry_helpers.GetHashedBaseFilename(
        build_filepath, function_name, params_content)

    respire_filepaths = registry_helpers.GetSubRespireFilepaths(
        out_dir, registry_files_basename)

    build_dir = os.path.dirname(build_filepath)

    # Make sure that the path has the current respire module's directory in it
    # first thing.
    sys.path.insert(0, build_dir)

    # Change the system current directory to the build file's directory to make
    # it so that build files can be more hermetic in assuming the cwd is the
    # directory they reside in.
    original_cwd = os.getcwd()
    os.chdir(build_dir)

    # Make sure that all modules have access to the respire standard build
    # library.
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'buildlib'))

    build_module = respire_python_wrapper_helpers.LoadSourceModule(
        'respire_build', build_filepath)

    registry = Registry(out_dir, build_filepath, build_module,
                        respire_filepaths)

    params = json_utils.DecodeFromJSONWithFlattening(params_content)
    if not hasattr(build_module, function_name):
        raise Exception(
            'Requested respire function, "%s", not found in build file: %s.' %
            (function_name, build_filepath))
    function = getattr(build_module, function_name)
    if not _IsFunctionParameter('registry', function):
        raise Exception(
            'You may only pass functions with a "registry" parameter to '
            'SubRespire().')

    out_params = function(registry=registry, **params)
    (out_params_content, out_futures) = json_utils.EncodeToJSON(out_params)

    _GenerateSystemCommandForFlattenedOutput(
        registry, out_futures, respire_filepaths.output_filepath,
        respire_filepaths.flattened_output_filepath)

    if timestamp_file:
        # Timestamp the timestamp file!
        open(timestamp_file, 'w').close()

    registry_contents = registry.CompileToString()

    respire_python_wrapper_helpers.WriteToFileIfContentsDiffer(
        respire_filepaths.registry_filepath, registry_contents)

    respire_python_wrapper_helpers.WriteToFileIfContentsDiffer(
        respire_filepaths.output_filepath, out_params_content)

    deps = (respire_python_wrapper_helpers.
            GetCurrentNonBuiltInImportDependencySources() + registry.deps)

    deps_file_content = '\n'.join(deps)

    respire_python_wrapper_helpers.WriteToFileIfContentsDiffer(
        respire_filepaths.deps_filepath, deps_file_content)

    os.chdir(original_cwd)

    return True