Skip to content

Commit

Permalink
Merge pull request bazelbuild#227 from dannysullivan/prefix-substrings
Browse files Browse the repository at this point in the history
Avoid stripping prefixes with incomplete directory names.
  • Loading branch information
dannysullivan authored Sep 10, 2020
2 parents e0d807d + 569a0e5 commit 60fbda7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
12 changes: 10 additions & 2 deletions pkg/path.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,25 @@ def _short_path_dirname(path):
return ""
return sp[:last_pkg]

def dest_path(f, strip_prefix):
def dest_path(f, strip_prefix, data_path_without_prefix = ""):
"""Returns the short path of f, stripped of strip_prefix."""
f_short_path = safe_short_path(f)

if strip_prefix == None:
# If no strip_prefix was specified, use the package of the
# given input as the strip_prefix.
strip_prefix = _short_path_dirname(f)
if not strip_prefix:
return f_short_path
if f_short_path.startswith(strip_prefix):
# Check that the last directory in strip_prefix is a complete
# directory (so that we don't strip part of a dir name)
prefix_last_dir_index = strip_prefix.rfind("/")
prefix_last_dir = strip_prefix[prefix_last_dir_index + 1:]

# Avoid stripping prefix if final directory is incomplete
if prefix_last_dir not in f_short_path.split("/"):
strip_prefix = data_path_without_prefix

return f_short_path[len(strip_prefix):]
return f_short_path

Expand Down
4 changes: 3 additions & 1 deletion pkg/pkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _pkg_tar_impl(ctx):

# Compute the relative path
data_path = compute_data_path(ctx.outputs.out, ctx.attr.strip_prefix)
data_path_without_prefix = compute_data_path(ctx.outputs.out, '.')

# Find a list of path remappings to apply.
remap_paths = ctx.attr.remap_paths
Expand Down Expand Up @@ -82,7 +83,8 @@ def _pkg_tar_impl(ctx):
file_inputs = ctx.files.srcs[:]

args += [
"--file=%s=%s" % (_quote(f.path), _remap(remap_paths, dest_path(f, data_path)))
"--file=%s=%s" % (_quote(f.path), _remap(
remap_paths, dest_path(f, data_path, data_path_without_prefix)))
for f in file_inputs
]
for target, f_dest_path in ctx.attr.files.items():
Expand Down
9 changes: 9 additions & 0 deletions pkg/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ pkg_tar(
strip_prefix = ".",
)

pkg_tar(
name = "test-tar-strip_prefix-substring",
srcs = [
":etc/nsswitch.conf",
],
strip_prefix = "et",
)

pkg_tar(
name = "test-tar-files_dict",
files = {
Expand Down Expand Up @@ -388,6 +396,7 @@ py_test(
":test-tar-strip_prefix-empty.tar",
":test-tar-strip_prefix-etc.tar",
":test-tar-strip_prefix-none.tar",
":test-tar-strip_prefix-substring.tar",
],
python_version = "PY3",
deps = [
Expand Down
8 changes: 8 additions & 0 deletions pkg/tests/path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def testTopLevel(self):
path = pkg_bzl.dest_path(File('baz'), None)
self.assertEqual('baz', path)

def testPartialDirectoryMatch(self):
path = pkg_bzl.dest_path(File('foo/bar/baz'), 'fo')
self.assertEqual('foo/bar/baz', path)

def testPartialDirectoryMatchWithDataPath(self):
path = pkg_bzl.dest_path(File('foo/bar/baz'), 'foo/ba', 'foo')
self.assertEqual('/bar/baz', path)


class ComputeDataPathTest(unittest.TestCase):
"""Testing for _data_path_out."""
Expand Down
8 changes: 8 additions & 0 deletions pkg/tests/pkg_tar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def test_strip_prefix_etc(self):
]
self.assertTarFileContent('test-tar-strip_prefix-etc.tar', content)

def test_strip_prefix_substring(self):
content = [
{'name': '.', 'isdir': True},
{'name': './etc', 'isdir': True},
{'name': './etc/nsswitch.conf'},
]
self.assertTarFileContent('test-tar-strip_prefix-substring.tar', content)

def test_strip_prefix_dot(self):
content = [
{'name': '.'},
Expand Down

0 comments on commit 60fbda7

Please sign in to comment.