Skip to content

Commit

Permalink
Open-source merge_feature_manifests.py
Browse files Browse the repository at this point in the history
Fixes issue #69.

PiperOrigin-RevId: 529538900
Change-Id: Ie368df7f77665e00bb651b745700f174fce98c2c
  • Loading branch information
ted-xie authored and Copybara-Service committed May 4, 2023
1 parent a6e892e commit 744528d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
11 changes: 10 additions & 1 deletion prereqs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ def rules_android_prereqs():
"https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.4/rules_license-0.0.4.tar.gz",
],
sha256 = "6157e1e68378532d0241ecd15d3c45f6e5cfd98fc10846045509fb2a7cc9e381",
)
)

maybe(
http_archive,
name = "py_absl",
sha256 = "0fb3a4916a157eb48124ef309231cecdfdd96ff54adf1660b39c0d4a9790a2c0",
urls = [
"https://github.com/abseil/abseil-py/archive/refs/tags/v1.4.0.tar.gz",
],
strip_prefix = "abseil-py-1.4.0",
)

18 changes: 18 additions & 0 deletions rules/android_application/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ bzl_library(
"//rules/flags:bzl",
],
)

py_binary(
name = "merge_feature_manifests",
srcs = ["merge_feature_manifests.py"],
python_version = "PY3",
visibility = ["//visibility:public"],
deps = [
"@py_absl//absl:app",
"@py_absl//absl/flags",
],
)

filegroup(
name = "merge_feature_manifests.par",
srcs = [":merge_feature_manifests"],
output_group = "python_zip_file",
)

66 changes: 66 additions & 0 deletions rules/android_application/merge_feature_manifests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2022 The Bazel 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.

"""Tool to merge the <dist /> element from a feature manifest into the main manifest."""

import xml.etree.ElementTree as ET

from absl import app
from absl import flags

_MAIN_MANIFEST = flags.DEFINE_string("main_manifest", None,
"Input main manifestl")
_FEATURE_MANIFEST = flags.DEFINE_string("feature_manifest", None,
"Output feature manifest")
_TITLE = flags.DEFINE_string("feature_title", None, "Feature title")
_OUT = flags.DEFINE_string("out", None, "Output manifest")


def _register_namespace(f):
"""Registers namespaces in ET global state and returns dict of namspaces."""
ns = {}
with open(f) as xml:
ns_parser = ET.XMLPullParser(events=["start-ns"])
ns_parser.feed(xml.read())
ns_parser.close()
for _, ns_tuple in ns_parser.read_events():
try:
ET.register_namespace(ns_tuple[0], ns_tuple[1])
ns[ns_tuple[0]] = ns_tuple[1]
except ValueError:
pass
return ns


def main(argv):
if len(argv) > 1:
raise app.UsageError("Too many command-line arguments.")

# Parse namespaces first to keep the prefix.
ns = {}
ns.update(_register_namespace(_MAIN_MANIFEST.value))
ns.update(_register_namespace(_FEATURE_MANIFEST.value))

main_manifest = ET.parse(_MAIN_MANIFEST.value)
feature_manifest = ET.parse(_FEATURE_MANIFEST.value)

dist = feature_manifest.find("dist:module", ns)
dist.set("{%s}title" % ns["dist"], _TITLE.value)
main_manifest.getroot().append(dist)

main_manifest.write(_OUT.value, encoding="utf-8", xml_declaration=True)


if __name__ == "__main__":
app.run(main)

0 comments on commit 744528d

Please sign in to comment.