Skip to content

Commit

Permalink
Merge branch 'main' into tar_empty
Browse files Browse the repository at this point in the history
  • Loading branch information
aiuto committed Jul 15, 2021
2 parents 2edabbb + f8d3d67 commit 830bb6b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
8 changes: 8 additions & 0 deletions pkg/private/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ py_binary(
":archive",
":build_info",
":helpers",
":manifest",
],
)

Expand All @@ -116,3 +117,10 @@ py_binary(
":helpers",
],
)

py_library(
name = "manifest",
srcs = ["manifest.py"],
srcs_version = "PY3",
visibility = ["//visibility:public"],
)
40 changes: 17 additions & 23 deletions pkg/private/build_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +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 <src>
ENTRY_IS_LINK = 1 # Entry is a symlink: dest -> <src>
ENTRY_IS_DIR = 2 # Entry is an empty dir
ENTRY_IS_TREE = 3 # Entry is a tree artifact: take tree from <src>
ENTRY_IS_EMPTY_FILE = 4 # Entry is a an empty file
import manifest


class TarFile(object):
Expand Down Expand Up @@ -263,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():
Expand Down
41 changes: 41 additions & 0 deletions pkg/private/manifest.py
Original file line number Diff line number Diff line change
@@ -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 <src>
ENTRY_IS_LINK = 1 # Entry is a symlink: dest -> <src>
ENTRY_IS_DIR = 2 # Entry is an owned dir, possibly empty
ENTRY_IS_TREE = 3 # Entry is a tree artifact: take tree from <src>

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))
2 changes: 1 addition & 1 deletion pkg/private/pkg_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 <src>
ENTRY_IS_LINK = 1 # Entry is a symlink: dest -> <src>
ENTRY_IS_DIR = 2 # Entry is an empty dir
Expand Down

0 comments on commit 830bb6b

Please sign in to comment.