簡體   English   中英

如何在bazel中壓縮文件

[英]How do I zip files in bazel

我的存儲庫中有一組文件。 如何從bazel中的那些文件中生成一個zip文件。 我找到了tar.gz等的規則,但是找不到找到zip存檔的方法。

找到提到拉鏈的參考資料,但無法弄清楚如何加載和使用它。 可以使用淡褐色的人幫忙嗎?

拉鏈實用程序位於@bazel_tools//tools/zip:zipper ,其用法是:

Usage: zipper [vxc[fC]] x.zip [-d exdir] [[zip_path1=]file1 ... [zip_pathn=]filen]
  v verbose - list all file in x.zip
  x extract - extract files in x.zip to current directory, or
       an optional directory relative to the current directory
       specified through -d option
  c create  - add files to x.zip
  f flatten - flatten files to use with create or extract operation
  C compress - compress files when using the create operation
x and c cannot be used in the same command-line.

For every file, a path in the zip can be specified. Examples:
  zipper c x.zip a/b/__init__.py= # Add an empty file at a/b/__init__.py
  zipper c x.zip a/b/main.py=foo/bar/bin.py # Add file foo/bar/bin.py at a/b/main.py

If the zip path is not specified, it is assumed to be the file path.

因此,它可以被用在genrule這樣的:

$ tree
.
├── BUILD
├── dir
│   ├── a
│   ├── b
│   └── c
└── WORKSPACE

1 directory, 5 files


$ cat BUILD
genrule(
  name = "gen_zip",
  srcs = glob(["dir/*"]),
  tools = ["@bazel_tools//tools/zip:zipper"],
  outs = ["files.zip"],
  cmd = "$(location @bazel_tools//tools/zip:zipper) c $@ $(SRCS)",
)


$ bazel build :files.zip
INFO: Analyzed target //:files.zip (7 packages loaded, 41 targets configured).
INFO: Found 1 target...
Target //:files.zip up-to-date:
  bazel-bin/files.zip
INFO: Elapsed time: 0.653s, Critical Path: 0.08s
INFO: 1 process: 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions


$ unzip -l bazel-bin/files.zip
Archive:  bazel-bin/files.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2010-01-01 00:00   dir/a
        0  2010-01-01 00:00   dir/b
        0  2010-01-01 00:00   dir/c
---------                     -------
        0                     3 files

可以類似地在Starlark中使用它:

def _some_rule_impl(ctx):

  zipper_inputs = []
  zipper_args = ctx.actions.args()
  zipper_args.add("c", ctx.outputs.zip.path)
  ....
  ctx.actions.run(
    inputs = zipper_inputs,
    outputs = [ctx.outputs.zip],
    executable = ctx.executable._zipper,
    arguments = zipper_args,
    progress_message = "Creating zip...",
    mnemonic = "zipper",
  )


some_rule = rule(
  implementation = _some_rule_impl,
  attrs = {
    "deps": attr.label_list(),
    "$zipper": attr.label(default = Label("@bazel_tools//tools/zip:zipper"), cfg = "host", executable=True),
  },
  outputs = {"zip": "%{name}.zip"},
)

最近將一個基本的pkg_zip規則添加到rules_pkg 這是單元測試中的一個基本用法示例:

load("@rules_pkg//:pkg.bzl", "pkg_zip")

pkg_zip(
    name = "test_zip_basic",
    srcs = [
        "testdata/hello.txt",
        "testdata/loremipsum.txt",
    ],
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM