diff --git a/pkg/private/BUILD b/pkg/private/BUILD index 18159005..0d51ca84 100644 --- a/pkg/private/BUILD +++ b/pkg/private/BUILD @@ -91,6 +91,7 @@ py_binary( ":archive", ":build_info", ":helpers", + ":manifest", ], ) @@ -116,3 +117,10 @@ py_binary( ":helpers", ], ) + +py_library( + name = "manifest", + srcs = ["manifest.py"], + srcs_version = "PY3", + visibility = ["//visibility:public"], +) diff --git a/pkg/private/build_tar.py b/pkg/private/build_tar.py index dbf1906e..64dac0ce 100644 --- a/pkg/private/build_tar.py +++ b/pkg/private/build_tar.py @@ -22,12 +22,7 @@ import archive import helpers import build_info - -# These must be kept in sync with the values from private/pkg_files.bzl -ENTRY_IS_FILE = 0 # Entry is a file: take content from -ENTRY_IS_LINK = 1 # Entry is a symlink: dest -> -ENTRY_IS_DIR = 2 # Entry is an empty dir -ENTRY_IS_TREE = 3 # Entry is a tree artifact: take tree from +import manifest class TarFile(object): @@ -262,32 +257,32 @@ def add_tree(self, tree_top, destpath, mode=None, ids=None, names=None): uname=names[0], gname=names[1]) - def add_manifest_entry(self, entry, file_attributes): - entry_type, dest, src, mode, user, group = entry + def add_manifest_entry(self, entry_list, file_attributes): + entry = manifest.ManifestEntry(*entry_list) # Use the pkg_tar mode/owner remaping as a fallback - non_abs_path = dest.strip('/') + non_abs_path = entry.dest.strip('/') if file_attributes: attrs = file_attributes(non_abs_path) else: attrs = {} # But any attributes from the manifest have higher precedence - if mode is not None and mode != '': - attrs['mode'] = int(mode, 8) - if user: - if group: - attrs['names'] = (user, group) + if entry.mode is not None and entry.mode != '': + attrs['mode'] = int(entry.mode, 8) + if entry.user: + if entry.group: + attrs['names'] = (entry.user, entry.group) else: # Use group that legacy tar process would assign - attrs['names'] = (user, attrs.get('names')[1]) - if entry_type == ENTRY_IS_LINK: - self.add_link(dest, src) - elif entry_type == ENTRY_IS_DIR: - self.add_empty_dir(dest, **attrs) - elif entry_type == ENTRY_IS_TREE: - self.add_tree(src, dest, **attrs) + attrs['names'] = (entry.user, attrs.get('names')[1]) + if entry.entry_type == manifest.ENTRY_IS_LINK: + self.add_link(entry.dest, entry.src) + elif entry.entry_type == manifest.ENTRY_IS_DIR: + self.add_empty_dir(entry.dest, **attrs) + elif entry.entry_type == manifest.ENTRY_IS_TREE: + self.add_tree(entry.src, entry.dest, **attrs) else: - self.add_file(src, dest, **attrs) + self.add_file(entry.src, entry.dest, **attrs) def main(): diff --git a/pkg/private/manifest.py b/pkg/private/manifest.py new file mode 100644 index 00000000..4da52375 --- /dev/null +++ b/pkg/private/manifest.py @@ -0,0 +1,41 @@ +# 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. + +"""Common package builder manifest helpers +""" + +import collections + +# These must be kept in sync with the declarations in private/pkg_files.bzl +ENTRY_IS_FILE = 0 # Entry is a file: take content from +ENTRY_IS_LINK = 1 # Entry is a symlink: dest -> +ENTRY_IS_DIR = 2 # Entry is an owned dir, possibly empty +ENTRY_IS_TREE = 3 # Entry is a tree artifact: take tree from + +ManifestEntry = collections.namedtuple("ManifestEntry", + ['entry_type', 'dest', 'src', 'mode', 'user', 'group']) + + +def entry_type_to_string(et): + """Entry type stringifier""" + if et == ENTRY_IS_FILE: + return "file" + elif et == ENTRY_IS_LINK: + return "symlink", + elif et == ENTRY_IS_DIR: + return "directory" + elif et == ENTRY_IS_TREE: + return "tree" + else: + raise ValueError("Invalid entry id {}".format(et)) diff --git a/pkg/private/pkg_files.bzl b/pkg/private/pkg_files.bzl index 8d7cc516..e8002cd3 100644 --- a/pkg/private/pkg_files.bzl +++ b/pkg/private/pkg_files.bzl @@ -42,7 +42,7 @@ load( ) # Possible values for entry_type -# These must be kept in sync with the declarations in private/build_*.py +# These must be kept in sync with the declarations in private/manifest.py. ENTRY_IS_FILE = 0 # Entry is a file: take content from ENTRY_IS_LINK = 1 # Entry is a symlink: dest -> ENTRY_IS_DIR = 2 # Entry is an empty dir