Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add package_file_name to pkg_deb and pkg_zip #282

Merged
merged 17 commits into from
Feb 4, 2021
Merged
30 changes: 29 additions & 1 deletion examples/naming_package_files/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

licenses(["notice"])

load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar")
load(":package_upload.bzl", "debian_upload")
load(":my_package_name.bzl", "basic_naming", "names_from_toolchains")

config_setting(
Expand Down Expand Up @@ -74,3 +75,30 @@ pkg_tar(
package_file_name = "example2-{cc_cpu}-{compiler}-{compilation_mode}.tar",
package_variables = ":toolchain_vars",
)

# Let
aiuto marked this conversation as resolved.
Show resolved Hide resolved
pkg_deb(
name = "a_deb_package",
data = ":example1",
description = "what it does",
maintainer = "someone@somewhere.com",
package = "foo_tools",
# Note: We could not know target_cpu at the time I write this rule.
package_file_name = "foo_tools-1-{target_cpu}.deb",
package_variables = ":my_naming_vars",
version = "1",
)

# This does not actually build anything. It writes a skelaton of a script that
# could upload the .deb and .changes pair. You should see something like
# $ cat bazel-bin/upload.sh
# # Uploading foo_tools-1-k8
# gsutil cp bazel-out/k8-fastbuild/bin/foo_tools-1-k8.deb gs://my_mirror/foo_tools-1-k8.deb
# gsutil cp bazel-out/k8-fastbuild/bin/foo_tools-1-k8.changes gs://my_mirror/foo_tools-1-k8.changes
debian_upload(
name = "upload",
out = "upload.sh",
host = "my_mirror",
# Refer to the target label, without having to know the final file name.
package = ":a_deb_package",
)
51 changes: 51 additions & 0 deletions examples/naming_package_files/package_upload.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2021 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.
"""Example of how we can use PackageArtifactInfo to find an output name."""

load("@rules_pkg//:providers.bzl", "PackageArtifactInfo")

def _debian_upload_impl(ctx):
# Find out the basename of the deb file we created.
pai = ctx.attr.package[PackageArtifactInfo]
package_basename = pai.file_name.split(".")[0]
content = ["# Uploading %s" % package_basename]
for f in ctx.attr.package[DefaultInfo].default_runfiles.files.to_list():
if f.basename.startswith(package_basename):
content.append("gsutil cp %s gs://%s/%s" % (
f.path,
ctx.attr.host,
f.basename,
))
ctx.actions.write(ctx.outputs.out, "\n".join(content))
return
aiuto marked this conversation as resolved.
Show resolved Hide resolved

debian_upload = rule(
implementation = _debian_upload_impl,
doc = """A demonstraion of consuming PackageArtifactInfo to get a file name.""",
attrs = {
"package": attr.label(
doc = "Package to upload",
mandatory = True,
providers = [[PackageArtifactInfo]],
aiuto marked this conversation as resolved.
Show resolved Hide resolved
),
"host": attr.string(
doc = "Host to upload to",
mandatory = True,
),
"out": attr.output(
doc = "Script file to create.",
mandatory = True,
),
},
)
6 changes: 5 additions & 1 deletion pkg/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library")
licenses(["notice"])

exports_files(
glob(["*.bzl"]),
glob([
"*.bzl",
"internal/**",
]),
visibility = ["//visibility:public"],
)

Expand All @@ -21,6 +24,7 @@ filegroup(
"*.bzl",
"*.py",
"*.md",
"internal/**",
]) + [
"BUILD",
"LICENSE",
Expand Down
11 changes: 6 additions & 5 deletions pkg/distro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pkg_tar(
srcs = [
":small_workspace",
"//:standard_package",
"//toolchains:standard_package",
"//releasing:standard_package",
"//toolchains:standard_package",
aiuto marked this conversation as resolved.
Show resolved Hide resolved
],
extension = "tar.gz",
# It is all source code, so make it read-only.
Expand All @@ -59,10 +59,10 @@ print_rel_notes(
name = "relnotes",
outs = ["relnotes.txt"],
deps_method = "rules_pkg_dependencies",
org = 'bazelbuild',
mirror_host = "mirror.bazel.build",
org = "bazelbuild",
repo = "rules_pkg",
version = version,
mirror_host = 'mirror.bazel.build'
)

py_test(
Expand All @@ -73,8 +73,8 @@ py_test(
":release_version.py",
],
data = [
":distro",
"testdata/BUILD.tmpl",
":distro",
],
local = True,
python_version = "PY3",
Expand All @@ -83,8 +83,8 @@ py_test(
"noci",
],
deps = [
"@bazel_tools//tools/python/runfiles",
"//releasing:release_utils",
"@bazel_tools//tools/python/runfiles",
],
)

Expand All @@ -97,6 +97,7 @@ genrule(
bzl_library(
name = "rules_pkg_lib",
srcs = [
"//:internal/util.bzl",
aiuto marked this conversation as resolved.
Show resolved Hide resolved
"//:package_variables.bzl",
"//:path.bzl",
"//:pkg.bzl",
Expand Down
52 changes: 49 additions & 3 deletions pkg/docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@

</div>

<a name="common"></a>
## Common Attributes

These attributes are used in several rules within this module.

**ATTRIBUTES**

| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :-------------: | :-------------: | :------------- |
| out | Name of the output file. This file will always be created and used to access the package content. If `package_file_name` is also specified, `out` will be a symlink. | String | required | |
| package_file_name | The name of the file which will contain the package. The name may contain variables in the form `{var}`. The values for substitution are specified through `package_variables`.| String | optional | package type specific |
| package_variables | A target that provides `PackageVariablesInfo` to substitute into `package_file_name`.| <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
Comment on lines +21 to +25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be laid out so that the whitespace is aligned when this file is rendered as text?

Not a big deal, though. I may do it myself for pkg_files and friends.


See
[examples/naming_package_files](https://github.com/bazelbuild/rules_pkg/tree/main/examples/naming_package_files)
for examples of how `out`, `package_file_name`, and `package_variables`
interact.

<a name="pkg_tar"></a>
## pkg_tar

```python
pkg_tar(name, extension, strip_prefix, package_dir, srcs,
mode, modes, deps, symlinks)
mode, modes, deps, symlinks, package_file_name, package_variables)
```

Creates a tar file from a list of inputs.
Expand Down Expand Up @@ -244,14 +262,23 @@ Creates a tar file from a list of inputs.
</p>
</td>
</tr>
<tr>
<td><code>package_file_name</code></td>
<td>See <a href="#common">Common Attributes</a></td>
</tr>
<tr>
<td><code>package_variables</code></td>
<td>See <a href="#common">Common Attributes</a></td>
</tr>
</tbody>
</table>

<a name="pkg_zip"></a>
## pkg_zip

```python
pkg_zip(name, extension, package_dir, srcs, timestamp)
pkg_zip(name, extension, package_dir, srcs, timestamp, package_file_name,
package_variables)
```

Creates a zip file from a list of inputs.
Expand Down Expand Up @@ -279,6 +306,7 @@ Creates a zip file from a list of inputs.
<td>
<code>String, default to 'zip'</code>
<p>
<b>Deprecated. Use <code>out</code> or <code>package_file_name</code> to specify the output file name.</b>
The extension for the resulting zipfile. The output
file will be '<i>name</i>.<i>extension</i>'.
</p>
Expand Down Expand Up @@ -319,6 +347,14 @@ Creates a zip file from a list of inputs.
</p>
</td>
</tr>
<tr>
<td><code>package_file_name</code></td>
<td>See <a href="#common">Common Attributes</a></td>
</tr>
<tr>
<td><code>package_variables</code></td>
<td>See <a href="#common">Common Attributes</a></td>
</tr>
</tbody>
</table>

Expand All @@ -329,7 +365,7 @@ Creates a zip file from a list of inputs.
pkg_deb(name, data, package, architecture, maintainer, preinst, postinst, prerm, postrm,
version, version_file, description, description_file, built_using, built_using_file,
priority, section, homepage, depends, suggests, enhances, breaks, conflicts,
predepends, recommends, replaces)
predepends, recommends, replaces, package_file_name, package_variables)
```

Create a debian package. See <a
Expand Down Expand Up @@ -510,6 +546,16 @@ for more details on this.
</p>
</td>
</tr>
<tr>
<td><code>package_file_name</code></td>
<td>See <a href="#common">Common Attributes</a>
Default: "%{package}-%{version}-%{architecture}.deb"
</td>
</tr>
<tr>
<td><code>package_variables</code></td>
<td>See <a href="#common">Common Attributes</a></td>
</tr>
</tbody>
</table>

Expand Down
59 changes: 59 additions & 0 deletions pkg/internal/util.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2021 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.
"""Internal utilities for rules_pkg."""

load(":providers.bzl", "PackageArtifactInfo", "PackageVariablesInfo")

def setup_output_files(ctx, package_file_name=None):
"""Handle processing of out and package_file_name.

By default, output is to the "out" attribute. If package_file_name is
given, we will write to that name instead, and out will be a symlink to it.

Callers should:
- write to ouput_file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: output_file

- add outputs to their returned DefaultInfo(files) provider
- return a PackageArtifactInfo provider of the form:
label: ctx.label.name
file_name: output_name

Args:
ctx: context
package_file_name: computed value for package_file_name

Returns:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should say "tuple of ...", or perhaps the return value should be a struct?

structs don't support destructuring assignment like with tuples, but I think I'd be fine with either.

ouputs: list(output handles)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: outputs

output_file: file handle to write to
output_name: name of output file
"""
outputs = [ctx.outputs.out]
if not package_file_name:
package_file_name = ctx.attr.package_file_name
if package_file_name:
output_name = package_file_name
if ctx.attr.package_variables:
package_variables = ctx.attr.package_variables[PackageVariablesInfo]
output_name = output_name.format(**package_variables.values)
elif package_file_name.find("{") >= 0:
fail("package_variables is required when using '{' in package_file_name")
output_file = ctx.actions.declare_file(output_name)
outputs.append(output_file)
ctx.actions.symlink(
output = ctx.outputs.out,
target_file = output_file,
)
else:
output_file = ctx.outputs.out
output_name = ctx.outputs.out.basename
return outputs, output_file, output_name
Loading