Пример #1
0
def _copy_outputs_multi(
        bazel_path,  # type: str
        outputs,  # type: List[Text]
        out_dir,  # type: Text
        preserve_paths,  # type: bool
        preserve_symlinks,  # type: bool
        _dedup_files,  # type: Text
        bazel_args,  # type: Optional[List[str]]
        bazel_build_args,  # type: Optional[List[str]]
):
    # type: (...) -> None
    # ask bazel where the bazel-bin directory is, to support read-only
    # workspaces where bazel is not allowed to create a bazel-bin symlink
    # at the root of the workspace
    bazel_info_cmd = [bazel_path]
    if bazel_args:
        bazel_info_cmd += bazel_args
    bazel_info_cmd.append("info")
    if bazel_build_args:
        bazel_info_cmd += bazel_build_args
    bazel_bin_dir = bazel_utils.check_output_silently(bazel_info_cmd +
                                                      ["bazel-bin"]).strip()
    wpool = multiprocessing.Pool(initializer=_init_worker)

    args = [(
        [output],
        bazel_bin_dir,
        out_dir,
        preserve_paths,
        preserve_symlinks,
        _dedup_files,
    ) for output in outputs]
    try:
        # Use async + timeout to make sure KeyboardInterrupt fires.
        wpool.map_async(_copy_outputs_wrapper, args, chunksize=1).get(3600)
    except KeyboardInterrupt:
        wpool.terminate()
        wpool.join()
        raise
Пример #2
0
def _build_targets(
    args,
    bazel_args,
    mode_args,
    pkg_target,
    pkg_prefix,
    name="",
    data=(),
    output_extension="tmp",
    file_map=None,
    preserve_symlinks=False,
    dedup_files=False,
):
    if not pkg_target.name.endswith(output_extension):
        raise bazel_utils.BazelError(
            "invalid target '%s' - must end with .%s" %
            (pkg_target.label, output_extension))
    # Treat data as our list of targets.
    targets = [bazel_utils.BazelTarget(x) for x in data]

    if targets:
        bazel_cmd = [args.bazel_path] + bazel_args + ["build"
                                                      ] + mode_args + data
        subprocess.check_call(bazel_cmd)

    # ask bazel where bazel-bin and bazel-genfiles are, instead of relying on
    # the symlinks, to support read-only workspaces
    pkg_dir_root = bazel_utils.check_output_silently(
        [args.bazel_path] + bazel_args + ["info"] + mode_args +
        ["bazel-genfiles"]).strip()
    out_dir_root = bazel_utils.check_output_silently([args.bazel_path] +
                                                     bazel_args + ["info"] +
                                                     mode_args +
                                                     ["bazel-bin"]).strip()

    pkg_dir = os.path.join(pkg_dir_root, pkg_target.package,
                           pkg_target.name + "-tmp")
    out_file = os.path.join(out_dir_root, pkg_target.package, pkg_target.name)

    out_dir = pkg_dir
    if pkg_prefix:
        out_dir = os.path.join(pkg_dir, pkg_prefix.strip("/"))
    # Prep and move things into the pkg_dir so they get squashed.
    if os.path.exists(out_file):
        os.remove(out_file)
    if os.path.exists(pkg_dir):
        shutil.rmtree(pkg_dir)
    os.makedirs(pkg_dir)
    if pkg_dir != out_dir:
        if os.path.exists(out_dir):
            shutil.rmtree(out_dir)
        os.makedirs(out_dir)

    if file_map:
        for dst, src in six.iteritems(file_map):
            if src:
                pkg_dst = os.path.join(pkg_dir, dst.strip("/"))
                pkg_dst_dir = os.path.dirname(pkg_dst)
                if not os.path.exists(pkg_dst_dir):
                    os.makedirs(pkg_dst_dir)
                shutil.copy2(os.path.join(pkg_target.package, src), pkg_dst)
            else:
                # If there is no source, assume it's a directory.
                pkg_dst_dir = os.path.join(pkg_dir, dst.strip("/"))
                if not os.path.exists(pkg_dst_dir):
                    os.makedirs(pkg_dst_dir)
    if targets:
        copy_labels(
            [t.label for t in targets],
            out_dir,
            preserve_symlinks=preserve_symlinks,
            _dedup_files=dedup_files,
            bazel_args=bazel_args,
            bazel_build_args=mode_args,
        )
    return pkg_dir, out_file