Format .bzl files with buildifier

This is a part of a large-scale change: [] . All .bzl files are being formatted with buildifier.

To format a file manually run `buildifier path/to/file.bzl`. Integration with `g4 fix` will be available later, but you can try using `g4 fix --format=bzl`.

Tested:
    tap_presubmit
    Some tests failed; test failures are believed to be unrelated to this CL

BEGIN_PUBLIC
Format .bzl files with buildifier
END_PUBLIC

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=203461813
This commit is contained in:
jianglai 2018-07-06 04:55:56 -07:00
parent 390939815a
commit 0223cea8cc
6 changed files with 2462 additions and 2432 deletions

View file

@ -17,44 +17,44 @@
ZIPPER = "@bazel_tools//tools/zip:zipper" ZIPPER = "@bazel_tools//tools/zip:zipper"
def long_path(ctx, file_): def long_path(ctx, file_):
"""Constructs canonical runfile path relative to TEST_SRCDIR. """Constructs canonical runfile path relative to TEST_SRCDIR.
Args: Args:
ctx: A Skylark rule context. ctx: A Skylark rule context.
file_: A File object that should appear in the runfiles for the test. file_: A File object that should appear in the runfiles for the test.
Returns: Returns:
A string path relative to TEST_SRCDIR suitable for use in tests and A string path relative to TEST_SRCDIR suitable for use in tests and
testing infrastructure. testing infrastructure.
""" """
if file_.short_path.startswith("../"): if file_.short_path.startswith("../"):
return file_.short_path[3:] return file_.short_path[3:]
if file_.owner and file_.owner.workspace_root: if file_.owner and file_.owner.workspace_root:
return file_.owner.workspace_root + "/" + file_.short_path return file_.owner.workspace_root + "/" + file_.short_path
return ctx.workspace_name + "/" + file_.short_path return ctx.workspace_name + "/" + file_.short_path
def collect_runfiles(targets): def collect_runfiles(targets):
"""Aggregates runfiles from targets. """Aggregates runfiles from targets.
Args: Args:
targets: A list of Bazel targets. targets: A list of Bazel targets.
Returns: Returns:
A list of Bazel files. A list of Bazel files.
""" """
data = depset() data = depset()
for target in targets: for target in targets:
if hasattr(target, "runfiles"): if hasattr(target, "runfiles"):
data += target.runfiles.files data += target.runfiles.files
continue continue
if hasattr(target, "data_runfiles"): if hasattr(target, "data_runfiles"):
data += target.data_runfiles.files data += target.data_runfiles.files
if hasattr(target, "default_runfiles"): if hasattr(target, "default_runfiles"):
data += target.default_runfiles.files data += target.default_runfiles.files
return data return data
def _get_runfiles(target, attribute): def _get_runfiles(target, attribute):
runfiles = getattr(target, attribute, None) runfiles = getattr(target, attribute, None)
if runfiles: if runfiles:
return runfiles.files return runfiles.files
return [] return []

View file

@ -17,37 +17,37 @@
load("//java/google/registry/builddefs:defs.bzl", "ZIPPER") load("//java/google/registry/builddefs:defs.bzl", "ZIPPER")
def registry_ear_file(name, out, configs, wars, **kwargs): def registry_ear_file(name, out, configs, wars, **kwargs):
"""Creates an EAR archive by combining WAR archives.""" """Creates an EAR archive by combining WAR archives."""
cmd = [ cmd = [
"set -e", "set -e",
"repo=$$(pwd)", "repo=$$(pwd)",
"zipper=$$repo/$(location %s)" % ZIPPER, "zipper=$$repo/$(location %s)" % ZIPPER,
"tmp=$$(mktemp -d $${TMPDIR:-/tmp}/registry_ear_file.XXXXXXXXXX)", "tmp=$$(mktemp -d $${TMPDIR:-/tmp}/registry_ear_file.XXXXXXXXXX)",
"cd $${tmp}", "cd $${tmp}",
]
for target, dest in configs.items():
cmd += [
"mkdir -p $${tmp}/$$(dirname %s)" % dest,
"ln -s $${repo}/$(location %s) $${tmp}/%s" % (target, dest),
] ]
for target, dest in wars.items(): for target, dest in configs.items():
cmd += [
"mkdir -p $${tmp}/$$(dirname %s)" % dest,
"ln -s $${repo}/$(location %s) $${tmp}/%s" % (target, dest),
]
for target, dest in wars.items():
cmd += [
"mkdir " + dest,
"cd " + dest,
"$${zipper} x $${repo}/$(location %s)" % target,
"cd ..",
]
cmd += [ cmd += [
"mkdir " + dest, "$${zipper} cC $${repo}/$@ $$(find . | sed 1d | cut -c 3- | LC_ALL=C sort)",
"cd " + dest, "cd $${repo}",
"$${zipper} x $${repo}/$(location %s)" % target, "rm -rf $${tmp}",
"cd ..",
] ]
cmd += [ native.genrule(
"$${zipper} cC $${repo}/$@ $$(find . | sed 1d | cut -c 3- | LC_ALL=C sort)", name = name,
"cd $${repo}", srcs = configs.keys() + wars.keys(),
"rm -rf $${tmp}", outs = [out],
] cmd = "\n".join(cmd),
native.genrule( tools = [ZIPPER],
name = name, message = "Generating EAR archive",
srcs = configs.keys() + wars.keys(), **kwargs
outs = [out], )
cmd = "\n".join(cmd),
tools = [ZIPPER],
message = "Generating EAR archive",
**kwargs
)

View file

@ -121,116 +121,136 @@ the zipfile. If the file doesn't exist, you'll get an error.
""" """
load('//java/google/registry/builddefs:defs.bzl', load(
'ZIPPER', "//java/google/registry/builddefs:defs.bzl",
'collect_runfiles', "ZIPPER",
'long_path') "collect_runfiles",
"long_path",
)
def _zip_file(ctx): def _zip_file(ctx):
"""Implementation of zip_file() rule.""" """Implementation of zip_file() rule."""
for s, d in ctx.attr.mappings.items(): for s, d in ctx.attr.mappings.items():
if (s.startswith('/') or s.endswith('/') or if (s.startswith("/") or s.endswith("/") or
d.startswith('/') or d.endswith('/')): d.startswith("/") or d.endswith("/")):
fail('mappings should not begin or end with slash') fail("mappings should not begin or end with slash")
srcs = depset() srcs = depset()
srcs += ctx.files.srcs srcs += ctx.files.srcs
srcs += ctx.files.data srcs += ctx.files.data
srcs += collect_runfiles(ctx.attr.data) srcs += collect_runfiles(ctx.attr.data)
mapped = _map_sources(ctx, srcs, ctx.attr.mappings) mapped = _map_sources(ctx, srcs, ctx.attr.mappings)
cmd = [ cmd = [
'#!/bin/sh', "#!/bin/sh",
'set -e', "set -e",
'repo="$(pwd)"', 'repo="$(pwd)"',
'zipper="${repo}/%s"' % ctx.file._zipper.path, 'zipper="${repo}/%s"' % ctx.file._zipper.path,
'archive="${repo}/%s"' % ctx.outputs.out.path, 'archive="${repo}/%s"' % ctx.outputs.out.path,
'tmp="$(mktemp -d "${TMPDIR:-/tmp}/zip_file.XXXXXXXXXX")"', 'tmp="$(mktemp -d "${TMPDIR:-/tmp}/zip_file.XXXXXXXXXX")"',
'cd "${tmp}"', 'cd "${tmp}"',
] ]
cmd += ['"${zipper}" x "${repo}/%s"' % dep.zip_file.path cmd += [
for dep in ctx.attr.deps] '"${zipper}" x "${repo}/%s"' % dep.zip_file.path
cmd += ['rm %s' % filename for filename in ctx.attr.exclude] for dep in ctx.attr.deps
cmd += ['mkdir -p "${tmp}/%s"' % zip_path ]
for zip_path in depset( cmd += ["rm %s" % filename for filename in ctx.attr.exclude]
[zip_path[:zip_path.rindex('/')] cmd += [
for _, zip_path in mapped if '/' in zip_path])] 'mkdir -p "${tmp}/%s"' % zip_path
cmd += ['ln -sf "${repo}/%s" "${tmp}/%s"' % (path, zip_path) for zip_path in depset(
for path, zip_path in mapped] [
cmd += [ zip_path[:zip_path.rindex("/")]
('find . | sed 1d | cut -c 3- | LC_ALL=C sort' + for _, zip_path in mapped
' | xargs "${zipper}" cC "${archive}"'), if "/" in zip_path
'cd "${repo}"', ],
'rm -rf "${tmp}"', )
] ]
if hasattr(ctx, 'bin_dir'): cmd += [
script = ctx.new_file(ctx.bin_dir, '%s.sh' % ctx.label.name) 'ln -sf "${repo}/%s" "${tmp}/%s"' % (path, zip_path)
else: for path, zip_path in mapped
# TODO(kchodorow): remove this once Bazel 4.0+ is required. ]
script = ctx.new_file(ctx.configuration.bin_dir, '%s.sh' % ctx.label.name) cmd += [
ctx.file_action(output=script, content='\n'.join(cmd), executable=True) ("find . | sed 1d | cut -c 3- | LC_ALL=C sort" +
inputs = [ctx.file._zipper] ' | xargs "${zipper}" cC "${archive}"'),
inputs += [dep.zip_file for dep in ctx.attr.deps] 'cd "${repo}"',
inputs += list(srcs) 'rm -rf "${tmp}"',
ctx.action( ]
inputs=inputs, if hasattr(ctx, "bin_dir"):
outputs=[ctx.outputs.out], script = ctx.new_file(ctx.bin_dir, "%s.sh" % ctx.label.name)
executable=script, else:
mnemonic='zip', # TODO(kchodorow): remove this once Bazel 4.0+ is required.
progress_message='Creating zip with %d inputs %s' % ( script = ctx.new_file(ctx.configuration.bin_dir, "%s.sh" % ctx.label.name)
len(inputs), ctx.label)) ctx.file_action(output = script, content = "\n".join(cmd), executable = True)
return struct(files=depset([ctx.outputs.out]), zip_file=ctx.outputs.out) inputs = [ctx.file._zipper]
inputs += [dep.zip_file for dep in ctx.attr.deps]
inputs += list(srcs)
ctx.action(
inputs = inputs,
outputs = [ctx.outputs.out],
executable = script,
mnemonic = "zip",
progress_message = "Creating zip with %d inputs %s" % (
len(inputs),
ctx.label,
),
)
return struct(files = depset([ctx.outputs.out]), zip_file = ctx.outputs.out)
def _map_sources(ctx, srcs, mappings): def _map_sources(ctx, srcs, mappings):
"""Calculates paths in zip file for srcs.""" """Calculates paths in zip file for srcs."""
# order mappings with more path components first
mappings = sorted([(-len(source.split('/')), source, dest) # order mappings with more path components first
for source, dest in mappings.items()]) mappings = sorted([
# get rid of the integer part of tuple used for sorting (-len(source.split("/")), source, dest)
mappings = [(source, dest) for _, source, dest in mappings] for source, dest in mappings.items()
mappings_indexes = range(len(mappings)) ])
used = {i: False for i in mappings_indexes}
mapped = [] # get rid of the integer part of tuple used for sorting
for file_ in srcs: mappings = [(source, dest) for _, source, dest in mappings]
run_path = long_path(ctx, file_) mappings_indexes = range(len(mappings))
zip_path = None used = {i: False for i in mappings_indexes}
mapped = []
for file_ in srcs:
run_path = long_path(ctx, file_)
zip_path = None
for i in mappings_indexes:
source = mappings[i][0]
dest = mappings[i][1]
if not source:
if dest:
zip_path = dest + "/" + run_path
else:
zip_path = run_path
elif source == run_path:
if dest:
zip_path = dest
else:
zip_path = run_path
elif run_path.startswith(source + "/"):
if dest:
zip_path = dest + run_path[len(source):]
else:
zip_path = run_path[len(source) + 1:]
else:
continue
used[i] = True
break
if not zip_path:
fail("no mapping matched: " + run_path)
mapped += [(file_.path, zip_path)]
for i in mappings_indexes: for i in mappings_indexes:
source = mappings[i][0] if not used[i]:
dest = mappings[i][1] fail('superfluous mapping: "%s" -> "%s"' % mappings[i])
if not source: return mapped
if dest:
zip_path = dest + '/' + run_path
else:
zip_path = run_path
elif source == run_path:
if dest:
zip_path = dest
else:
zip_path = run_path
elif run_path.startswith(source + '/'):
if dest:
zip_path = dest + run_path[len(source):]
else:
zip_path = run_path[len(source) + 1:]
else:
continue
used[i] = True
break
if not zip_path:
fail('no mapping matched: ' + run_path)
mapped += [(file_.path, zip_path)]
for i in mappings_indexes:
if not used[i]:
fail('superfluous mapping: "%s" -> "%s"' % mappings[i])
return mapped
zip_file = rule( zip_file = rule(
implementation=_zip_file, implementation = _zip_file,
output_to_genfiles = True, output_to_genfiles = True,
attrs={ attrs = {
'out': attr.output(mandatory=True), "out": attr.output(mandatory = True),
'srcs': attr.label_list(allow_files=True), "srcs": attr.label_list(allow_files = True),
'data': attr.label_list(cfg='data', allow_files=True), "data": attr.label_list(cfg = "data", allow_files = True),
'deps': attr.label_list(providers=['zip_file']), "deps": attr.label_list(providers = ["zip_file"]),
'exclude': attr.string_list(), "exclude": attr.string_list(),
'mappings': attr.string_dict(), "mappings": attr.string_dict(),
'_zipper': attr.label(default=Label(ZIPPER), single_file=True), "_zipper": attr.label(default = Label(ZIPPER), single_file = True),
}) },
)

File diff suppressed because it is too large Load diff

View file

@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# #
# .'``'. ... # .'``'. ...
# :o o `....'` ; # :o o `....'` ;

View file

@ -14,48 +14,53 @@
"""Build rule for unit testing the zip_file() rule.""" """Build rule for unit testing the zip_file() rule."""
load('//java/google/registry/builddefs:defs.bzl', 'ZIPPER') load("//java/google/registry/builddefs:defs.bzl", "ZIPPER")
def _impl(ctx): def _impl(ctx):
"""Implementation of zip_contents_test() rule.""" """Implementation of zip_contents_test() rule."""
cmd = [ cmd = [
'set -e', "set -e",
'repo="$(pwd)"', 'repo="$(pwd)"',
'zipper="${repo}/%s"' % ctx.file._zipper.short_path, 'zipper="${repo}/%s"' % ctx.file._zipper.short_path,
'archive="${repo}/%s"' % ctx.file.src.short_path, 'archive="${repo}/%s"' % ctx.file.src.short_path,
('listing="$("${zipper}" v "${archive}"' + ('listing="$("${zipper}" v "${archive}"' +
' | grep -v ^d | awk \'{print $3}\' | LC_ALL=C sort)"'), ' | grep -v ^d | awk \'{print $3}\' | LC_ALL=C sort)"'),
'if [[ "${listing}" != "%s" ]]; then' % ( 'if [[ "${listing}" != "%s" ]]; then' % (
'\n'.join(ctx.attr.contents.keys())), "\n".join(ctx.attr.contents.keys())
' echo "archive had different file listing:"', ),
' "${zipper}" v "${archive}" | grep -v ^d', ' echo "archive had different file listing:"',
' exit 1', ' "${zipper}" v "${archive}" | grep -v ^d',
'fi', " exit 1",
'tmp="$(mktemp -d "${TMPDIR:-/tmp}/zip_contents_test.XXXXXXXXXX")"', "fi",
'cd "${tmp}"', 'tmp="$(mktemp -d "${TMPDIR:-/tmp}/zip_contents_test.XXXXXXXXXX")"',
'"${zipper}" x "${archive}"', 'cd "${tmp}"',
] '"${zipper}" x "${archive}"',
for path, data in ctx.attr.contents.items():
cmd += [
'if [[ "$(cat "%s")" != "%s" ]]; then' % (path, data),
' echo "%s had different contents:"' % path,
' cat "%s"' % path,
' exit 1',
'fi',
] ]
cmd += ['cd "${repo}"', for path, data in ctx.attr.contents.items():
'rm -rf "${tmp}"'] cmd += [
ctx.file_action( 'if [[ "$(cat "%s")" != "%s" ]]; then' % (path, data),
output=ctx.outputs.executable, ' echo "%s had different contents:"' % path,
content='\n'.join(cmd), ' cat "%s"' % path,
executable=True) " exit 1",
return struct(runfiles=ctx.runfiles([ctx.file.src, ctx.file._zipper])) "fi",
]
cmd += [
'cd "${repo}"',
'rm -rf "${tmp}"',
]
ctx.file_action(
output = ctx.outputs.executable,
content = "\n".join(cmd),
executable = True,
)
return struct(runfiles = ctx.runfiles([ctx.file.src, ctx.file._zipper]))
zip_contents_test = rule( zip_contents_test = rule(
implementation=_impl, implementation = _impl,
test=True, test = True,
attrs={ attrs = {
'src': attr.label(allow_single_file=True), "src": attr.label(allow_single_file = True),
'contents': attr.string_dict(), "contents": attr.string_dict(),
'_zipper': attr.label(default=Label(ZIPPER), single_file=True), "_zipper": attr.label(default = Label(ZIPPER), single_file = True),
}) },
)