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

Modularize manifest python code #384

Merged
merged 3 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"],
)
39 changes: 17 additions & 22 deletions pkg/private/build_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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>
import manifest


class TarFile(object):
Expand Down Expand Up @@ -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():
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 manfiest 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