diff --git a/pkg/distro/BUILD b/pkg/distro/BUILD index 9ef2f454..d96ffe39 100644 --- a/pkg/distro/BUILD +++ b/pkg/distro/BUILD @@ -3,13 +3,16 @@ package( ) load("@rules_pkg//:pkg.bzl", "pkg_tar", "version") +load("@rules_pkg//releasing:defs.bzl", "print_rel_notes") # Build the artifact to put on the github release page. pkg_tar( name = "rules_pkg-%s" % version, srcs = [ "@rules_pkg//:standard_package", + "@rules_pkg//releasing:standard_package", ], + strip_prefix = ".", extension = "tar.gz", # It is all source code, so make it read-only. mode = "0444", @@ -18,32 +21,11 @@ pkg_tar( package_dir = ".", ) -py_library( - name = "util", - srcs = [ - "__init__.py", - "release_tools.py", - ":release_version.py", - ], - srcs_version = "PY3", - deps = [ - "@bazel_tools//tools/python/runfiles", - ], -) - -py_binary( - name = "print_rel_notes", - srcs = [ - "print_rel_notes.py", - ], - data = [ - ":rules_pkg-%s.tar.gz" % version, - ], - python_version = "PY3", - deps = [ - ":util", - "@bazel_tools//tools/python/runfiles", - ], +print_rel_notes( + name = "relnotes", + repo = "rules_pkg", + version = version, + outs = ["relnotes.txt"], ) py_test( @@ -51,6 +33,7 @@ py_test( size = "large", srcs = [ "packaging_test.py", + ":release_version.py", ], data = [ ":rules_pkg-%s.tar.gz" % version, @@ -63,7 +46,7 @@ py_test( "noci", ], deps = [ - ":util", + "@rules_pkg//releasing:release_utils", "@bazel_tools//tools/python/runfiles", ], ) diff --git a/pkg/distro/packaging_test.py b/pkg/distro/packaging_test.py index 3b2783a4..1a769e9e 100644 --- a/pkg/distro/packaging_test.py +++ b/pkg/distro/packaging_test.py @@ -19,7 +19,7 @@ import unittest from bazel_tools.tools.python.runfiles import runfiles -from distro import release_tools +from releasing import release_tools from distro import release_version _VERBOSE = True @@ -30,20 +30,23 @@ class PackagingTest(unittest.TestCase): def setUp(self): self.data_files = runfiles.Create() + self.repo = 'rules_pkg' + self.version = release_version.RELEASE_VERSION def testBuild(self): - # Set up a fresh Bazel workspace + # Set up a fresh Bazel workspace using the currently build repo. tempdir = os.path.join(os.environ['TEST_TMPDIR'], 'build') if not os.path.exists(tempdir): os.makedirs(tempdir) with open(os.path.join(tempdir, 'WORKSPACE'), 'w') as workspace: - version = release_version.RELEASE_VERSION - file_name = release_tools.package_basename(version) - local_path, sha256 = release_tools.get_package_info(version) + file_name = release_tools.package_basename(self.repo, self.version) + local_path = runfiles.Create().Rlocation( + os.path.join('rules_pkg', 'distro', file_name)) + sha256 = release_tools.get_package_sha256(local_path) workspace_content = '\n'.join(( 'workspace(name = "test_rules_pkg_packaging")', release_tools.workspace_content( - 'file://%s' % local_path, sha256) + 'file://%s' % local_path, self.repo, sha256) )) workspace.write(workspace_content) if _VERBOSE: diff --git a/pkg/distro/print_rel_notes.py b/pkg/distro/print_rel_notes.py deleted file mode 100644 index 5f88b80a..00000000 --- a/pkg/distro/print_rel_notes.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2019 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. -"""Print release notes for the package. - -""" - -import sys -import textwrap - -from bazel_tools.tools.python.runfiles import runfiles -from distro import release_tools -from distro import release_version - - -def print_notes(version): - file_name = release_tools.package_basename(version) - _, sha256 = release_tools.get_package_info(version) - - url = 'https://github.com/bazelbuild/rules_pkg/releases/download/%s/%s' % ( - version, file_name) - print(textwrap.dedent( - """ - - **WORKSPACE setup** - - ``` - """).strip()) - print(release_tools.workspace_content(url, sha256)) - print(textwrap.dedent( - """ - ``` - - **Using the rules** - - See [the source](https://github.com/bazelbuild/rules_pkg/tree/master/pkg). - """).strip()) - - -def main(_): - print_notes(release_version.RELEASE_VERSION) - - -if __name__ == '__main__': - main(sys.argv) diff --git a/pkg/distro/release_tools.py b/pkg/distro/release_tools.py deleted file mode 100644 index 38b15175..00000000 --- a/pkg/distro/release_tools.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright 2019 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. -"""Utilities to help create a rule set release.""" - -import hashlib -import os -import textwrap - -from bazel_tools.tools.python.runfiles import runfiles - - -def package_basename(version): - return 'rules_pkg-%s.tar.gz' % version - - -def get_package_info(version): - tar_path = runfiles.Create().Rlocation( - os.path.join('rules_pkg', 'distro', package_basename(version))) - with open(tar_path, 'rb') as pkg_content: - tar_sha256 = hashlib.sha256(pkg_content.read()).hexdigest() - return tar_path, tar_sha256 - - -def workspace_content(url, sha256): - # Set up a fresh Bazel workspace - return textwrap.dedent( - """ - load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - http_archive( - name = "rules_pkg", - url = "%s", - sha256 = "%s", - ) - - load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") - rules_pkg_dependencies() - """ % (url, sha256)).strip() - - -""" - # We do a little dance of renaming *.tmpl to *, mostly so that we do not - # have a BUILD file in testdata, which would create a package boundary. - def CopyTestFile(source_name, dest_name): - source_path = self.data_files.Rlocation( - os.path.join('rules_pkg', 'distro', 'testdata', source_name)) - with open(source_path) as inp: - with open(os.path.join(tempdir, dest_name), 'w') as out: - content = inp.read() - out.write(content) - - CopyTestFile('BUILD.tmpl', 'BUILD') - - os.chdir(tempdir) - build_result = subprocess.check_output(['bazel', 'build', ':dummy_tar']) - if _VERBOSE: - print('=== Build Result ===') - print(build_result) - - content = subprocess.check_output( - ['/bin/tar', 'tzf', 'bazel-bin/dummy_tar.tar.gz']) - self.assertEqual('./\n./BUILD\n', content) -""" diff --git a/pkg/releasing/BUILD b/pkg/releasing/BUILD new file mode 100644 index 00000000..afb9ee22 --- /dev/null +++ b/pkg/releasing/BUILD @@ -0,0 +1,42 @@ +package( + default_visibility = ["//visibility:public"], +) + +# WARNING: 2019-08-05. This is experimental and subject to change. +# It is expected to be available for use concurently with the Bazel +# 1.0 release. + +# Sample usage: +# load("@rules_pkg//releasing:defs.bzl", "print_rel_notes") +# print_rel_notes( +# name = "relnotes", +# repo = "rules_pkg", +# version = "2.1", +# outs = ["relnotes.txt"], +# ) + +filegroup( + name = "standard_package", + srcs = glob(["BUILD", "*.bzl", "*.py", "*.md"]), + visibility = ["@//distro:__pkg__"], +) + +py_library( + name = "release_utils", + srcs = [ + "__init__.py", + "release_tools.py", + ], + srcs_version = "PY3", +) + +py_binary( + name = "print_rel_notes", + srcs = [ + "print_rel_notes.py", + ], + python_version = "PY3", + deps = [ + ":release_utils", + ], +) diff --git a/pkg/releasing/__init__.py b/pkg/releasing/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pkg/releasing/print_rel_notes.py b/pkg/releasing/print_rel_notes.py new file mode 100644 index 00000000..1c264557 --- /dev/null +++ b/pkg/releasing/print_rel_notes.py @@ -0,0 +1,63 @@ +# Copyright 2019 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. +"""Print release notes for a package. + +""" + +import sys +from string import Template +import textwrap + +from releasing import release_tools + + +def print_notes(repo, version, tarball_path, org='bazelbuild'): + file_name = release_tools.package_basename(repo, version) + sha256 = release_tools.get_package_sha256(tarball_path) + + url = 'https://github.com/%s/%s/releases/download/%s/%s' % ( + org, repo, version, file_name) + workspace_stanza = release_tools.workspace_content(url, repo, sha256) + relnotes_template = Template(textwrap.dedent( + """ + ------------------------ snip ---------------------------- + **New Features** + + **Incompatible Changes** + + **WORKSPACE setup** + + ``` + ${workspace_stanza} + ``` + + **Using the rules** + + See [the source](https://github.com/${org}/${repo}/tree/master). + ------------------------ snip ---------------------------- + + """).strip()) + print(relnotes_template.substitute({ + 'org': org, + 'repo': repo, + 'workspace_stanza': workspace_stanza, + })) + + +def main(args): + print_notes(repo=args[1], version=args[2], tarball_path=args[3]) + + +if __name__ == '__main__': + main(sys.argv) diff --git a/pkg/releasing/release_tools.py b/pkg/releasing/release_tools.py new file mode 100644 index 00000000..6a8509b0 --- /dev/null +++ b/pkg/releasing/release_tools.py @@ -0,0 +1,49 @@ +# Copyright 2019 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. +"""Utilities to help create a rule set release.""" + +import hashlib +import os +from string import Template +import textwrap + + +def package_basename(repo, version): + return '%s-%s.tar.gz' % (repo, version) + + +def get_package_sha256(tarball_path): + with open(tarball_path, 'rb') as pkg_content: + tar_sha256 = hashlib.sha256(pkg_content.read()).hexdigest() + return tar_sha256 + + +def workspace_content(url, repo, sha256): + # Set up a fresh Bazel workspace + workspace_stanza_template = Template(textwrap.dedent( + """ + load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + http_archive( + name = "${repo}", + url = "${url}", + sha256 = "${sha256}", + ) + load("@${repo}//:deps.bzl", "${repo}_dependencies") + ${repo}_dependencies() + """).strip()) + return workspace_stanza_template.substitute({ + 'url': url, + 'sha256': sha256, + 'repo': repo, + })