Use the python flatbuffers library with the correct file paths

PiperOrigin-RevId: 334662368
Change-Id: I648738fc64997b4b21afffcf125732f0d13b45de
This commit is contained in:
Meghna Natraj 2020-09-30 13:06:12 -07:00 committed by TensorFlower Gardener
parent 11fc1489d0
commit f98fc1387d
2 changed files with 38 additions and 10 deletions

View File

@ -1,4 +1,5 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_library")
load(":build_defs.bzl", "flatbuffer_py_strip_prefix_srcs")
package(default_visibility = ["//visibility:public"])
@ -102,8 +103,8 @@ cc_library(
visibility = ["//visibility:public"],
)
filegroup(
name = "runtime_py_srcs",
flatbuffer_py_strip_prefix_srcs(
name = "flatbuffer_py_strip_prefix",
srcs = [
"python/flatbuffers/__init__.py",
"python/flatbuffers/builder.py",
@ -114,6 +115,21 @@ filegroup(
"python/flatbuffers/table.py",
"python/flatbuffers/util.py",
],
strip_prefix = "python/flatbuffers/",
)
filegroup(
name = "runtime_py_srcs",
srcs = [
"__init__.py",
"builder.py",
"compat.py",
"encode.py",
"number_types.py",
"packer.py",
"table.py",
"util.py",
],
)
py_library(

View File

@ -361,18 +361,30 @@ _gen_flatbuffer_srcs = rule(
output_to_genfiles = True,
)
def flatbuffer_py_strip_prefix_srcs(name, srcs = [], strip_prefix = ""):
"""Strips path prefix.
Args:
name: Rule name. (required)
srcs: Source .py files. (required)
strip_prefix: Path that needs to be stripped from the srcs filepaths. (required)
"""
for src in srcs:
native.genrule(
name = name + "_" + src.replace(".", "_").replace("/", "_"),
srcs = [src],
outs = [src.replace(strip_prefix, "")],
cmd = "cp $< $@",
)
def _concat_flatbuffer_py_srcs_impl(ctx):
# Merge all generated python files. The files are concatenated and the
# import statements are removed. Finally we import the flatbuffer runtime
# library.
# Merge all generated python files.
command = "find '%s' -name '*.py' -exec cat {} + | sed '/import flatbuffers/d'"
command += " | sed '1s/^/import flatbuffers\\'$'\\n/' > %s"
ctx.actions.run_shell(
inputs = ctx.attr.deps[0].files,
outputs = [ctx.outputs.out],
command = (
"find '%s' -name '*.py' -exec cat {} + |" +
"sed 's/from flatbuffers.compat import import_numpy/import numpy as np' |" +
"sed '/np = import_numpy()/d' > %s"
) % (
command = command % (
ctx.attr.deps[0].files.to_list()[0].path,
ctx.outputs.out.path,
),