Introduce tensorboard_zip_file build rule

This rule can depend on web_library or tensorboard_html_binary. In
both cases it will create a .zip file containing all the transitive
web server paths. This can be used to deploy static assets to web
servers.

A small change was also made to Vulcanize to support path overriding.

PiperOrigin-RevId: 157655047
This commit is contained in:
Justine Tunney 2017-05-31 16:59:13 -07:00 committed by TensorFlower Gardener
parent 7ed44f4c92
commit faac0331c2
4 changed files with 148 additions and 1 deletions

View File

@ -17,6 +17,18 @@ java_binary(
], ],
) )
java_binary(
name = "Zipper",
srcs = ["Zipper.java"],
visibility = ["//visibility:public"],
deps = [
"@com_google_guava",
"@com_google_protobuf_java",
"@io_bazel_rules_closure//java/io/bazel/rules/closure/webfiles",
"@io_bazel_rules_closure//java/io/bazel/rules/closure/webfiles:build_info_java_proto",
],
)
filegroup( filegroup(
name = "all_files", name = "all_files",
srcs = glob(["**"]), srcs = glob(["**"]),

View File

@ -0,0 +1,73 @@
// Copyright 2017 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.tensorflow.tensorboard.vulcanize;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.protobuf.TextFormat;
import io.bazel.rules.closure.webfiles.BuildInfo.WebfileInfo;
import io.bazel.rules.closure.webfiles.BuildInfo.Webfiles;
import io.bazel.rules.closure.webfiles.BuildInfo.WebfilesSource;
import io.bazel.rules.closure.webfiles.WebfilesWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.Deflater;
/**
* Simple one-off solution for TensorBoard zipping of web_library rules.
*
* <p>This is intended to collect static assets for production web server deployment. The paths of
* files inside the zip will be web paths, with the prefix slash removed. These files will be
* topologically ordered, i.e. web files higher up in the build tree come first.
*/
public final class Zipper {
public static void main(String[] args) throws IOException {
Set<String> alreadyZipped = new HashSet<>();
try (WebfilesWriter writer =
new WebfilesWriter(
Files.newByteChannel(
Paths.get(args[0]),
StandardOpenOption.WRITE,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING),
Deflater.BEST_SPEED)) {
for (int i = 1; i < args.length; i++) {
Webfiles manifest = loadWebfilesPbtxt(Paths.get(args[i]));
for (WebfilesSource src : manifest.getSrcList()) {
if (!alreadyZipped.add(src.getWebpath())) {
continue;
}
try (InputStream input = Files.newInputStream(Paths.get(src.getPath()))) {
writer.writeWebfile(
WebfileInfo.newBuilder().setWebpath(src.getWebpath()).build(), input);
}
}
}
}
}
private static Webfiles loadWebfilesPbtxt(Path path) throws IOException {
Webfiles.Builder build = Webfiles.newBuilder();
TextFormat.getParser().merge(new String(Files.readAllBytes(path), UTF_8), build);
return build.build();
}
}

View File

@ -16,11 +16,14 @@ load("@io_bazel_rules_closure//closure/private:defs.bzl", "unfurl", "long_path")
def _tensorboard_html_binary(ctx): def _tensorboard_html_binary(ctx):
deps = unfurl(ctx.attr.deps, provider="webfiles") deps = unfurl(ctx.attr.deps, provider="webfiles")
manifests = set(order="link") manifests = set()
files = set() files = set()
webpaths = set()
for dep in deps: for dep in deps:
manifests += dep.webfiles.manifests manifests += dep.webfiles.manifests
webpaths += dep.webfiles.webpaths
files += dep.data_runfiles.files files += dep.data_runfiles.files
webpaths += [ctx.attr.output_path]
# vulcanize # vulcanize
ctx.action( ctx.action(
@ -69,6 +72,11 @@ def _tensorboard_html_binary(ctx):
transitive_runfiles += dep.data_runfiles.files transitive_runfiles += dep.data_runfiles.files
return struct( return struct(
files=set([ctx.outputs.html]), files=set([ctx.outputs.html]),
webfiles=struct(
manifest=manifest,
manifests=manifests,
webpaths=webpaths,
dummy=ctx.outputs.html),
runfiles=ctx.runfiles( runfiles=ctx.runfiles(
files=ctx.files.data + [manifest, files=ctx.files.data + [manifest,
params_file, params_file,

View File

@ -0,0 +1,54 @@
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@io_bazel_rules_closure//closure/private:defs.bzl", "unfurl", "long_path")
def _tensorboard_zip_file(ctx):
deps = unfurl(ctx.attr.deps, provider="webfiles")
manifests = set(order="link")
files = set()
webpaths = set()
for dep in deps:
manifests += dep.webfiles.manifests
webpaths += dep.webfiles.webpaths
files += dep.data_runfiles.files
ctx.action(
inputs=list(manifests + files),
outputs=[ctx.outputs.zip],
executable=ctx.executable._Zipper,
arguments=([ctx.outputs.zip.path] +
[m.path for m in manifests]),
progress_message="Zipping %d files" % len(webpaths))
transitive_runfiles = set()
for dep in deps:
transitive_runfiles += dep.data_runfiles.files
return struct(
files=set([ctx.outputs.zip]),
runfiles=ctx.runfiles(
files=ctx.files.data + [ctx.outputs.zip],
transitive_files=transitive_runfiles))
tensorboard_zip_file = rule(
implementation=_tensorboard_zip_file,
attrs={
"data": attr.label_list(cfg="data", allow_files=True),
"deps": attr.label_list(providers=["webfiles"], mandatory=True),
"_Zipper": attr.label(
default=Label("//tensorflow/tensorboard/java/org/tensorflow/tensorboard/vulcanize:Zipper"),
executable=True,
cfg="host"),
},
outputs={
"zip": "%{name}.zip",
})