Skip to content

Commit

Permalink
Rename stripPrefix= to strip_prefix=
Browse files Browse the repository at this point in the history
Fixes #16496

Closes #24019.

RELNOTES: The stripPrefix parameter of repository_ctx.download_and_extract()
and repository_ctx.extract() has been renamed to strip_prefix; the deprecated
stripPrefix name remains usable for compatibility.
PiperOrigin-RevId: 687224164
Change-Id: Iffaba2e65c049a2ff8bb98d1fba52e0bba9e5a02
  • Loading branch information
tetromino authored and copybara-github committed Oct 18, 2024
1 parent 2ce9e35 commit 49d7bb6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -904,16 +904,18 @@ public Object download(
".tzst", "tar.bz2", ".tbz", ".ar", or ".deb" here.
"""),
@Param(
name = "stripPrefix",
name = "strip_prefix",
defaultValue = "''",
named = true,
doc =
"""
A directory prefix to strip from the extracted files.
Many archives contain a top-level directory that contains all files in the \
archive. Instead of needing to specify this prefix over and over in the \
<code>build_file</code>, this field can be used to strip it from extracted \
files.
A directory prefix to strip from the extracted files. Many archives contain a
top-level directory that contains all files in the archive. Instead of needing to
specify this prefix over and over in the <code>build_file</code>, this field can
be used to strip it from extracted files.
<p>For compatibility, this parameter may also be used under the deprecated name
<code>stripPrefix</code>.
"""),
@Param(
name = "allow_fail",
Expand Down Expand Up @@ -973,6 +975,12 @@ public Object download(
contain non-Unicode filenames, or which have files that would extract to \
the same path on case-insensitive filesystems.
"""),
@Param(
name = "stripPrefix",
documented = false,
positional = false,
named = true,
defaultValue = "''"),
})
public StructImpl downloadAndExtract(
Object url,
Expand All @@ -986,8 +994,10 @@ public StructImpl downloadAndExtract(
Dict<?, ?> headersUnchecked, // <String, List<String> | String> expected
String integrity,
Dict<?, ?> renameFiles, // <String, String> expected
String oldStripPrefix,
StarlarkThread thread)
throws RepositoryFunctionException, InterruptedException, EvalException {
stripPrefix = renamedStripPrefix("download_and_extract", stripPrefix, oldStripPrefix);
ImmutableMap<URI, Map<String, List<String>>> authHeaders =
getAuthHeaders(getAuthContents(authUnchecked, "auth"));

Expand All @@ -996,8 +1006,9 @@ public StructImpl downloadAndExtract(
ImmutableList<URL> urls =
getUrls(
url,
/*ensureNonEmpty=*/ !allowFail,
/*checksumGiven=*/ !Strings.isNullOrEmpty(sha256) || !Strings.isNullOrEmpty(integrity));
/* ensureNonEmpty= */ !allowFail,
/* checksumGiven= */ !Strings.isNullOrEmpty(sha256)
|| !Strings.isNullOrEmpty(integrity));
Optional<Checksum> checksum;
RepositoryFunctionException checksumValidation = null;
try {
Expand Down Expand Up @@ -1141,15 +1152,19 @@ public StructImpl downloadAndExtract(
"path to the directory where the archive will be unpacked,"
+ " relative to the repository directory."),
@Param(
name = "stripPrefix",
name = "strip_prefix",
defaultValue = "''",
named = true,
doc =
"a directory prefix to strip from the extracted files."
+ "\nMany archives contain a top-level directory that contains all files in the"
+ " archive. Instead of needing to specify this prefix over and over in the"
+ " <code>build_file</code>, this field can be used to strip it from extracted"
+ " files."),
"""
a directory prefix to strip from the extracted files. Many archives contain a
top-level directory that contains all files in the archive. Instead of needing to
specify this prefix over and over in the <code>build_file</code>, this field can be
used to strip it from extracted files.
<p>For compatibility, this parameter may also be used under the deprecated name
<code>stripPrefix</code>.
"""),
@Param(
name = "rename_files",
defaultValue = "{}",
Expand All @@ -1173,15 +1188,23 @@ public StructImpl downloadAndExtract(
+ "not attempt to watch the file; passing 'auto' will only attempt to watch "
+ "the file when it is legal to do so (see <code>watch()</code> docs for more "
+ "information."),
@Param(
name = "stripPrefix",
documented = false,
positional = false,
named = true,
defaultValue = "''"),
})
public void extract(
Object archive,
Object output,
String stripPrefix,
Dict<?, ?> renameFiles, // <String, String> expected
String watchArchive,
String oldStripPrefix,
StarlarkThread thread)
throws RepositoryFunctionException, InterruptedException, EvalException {
stripPrefix = renamedStripPrefix("extract", stripPrefix, oldStripPrefix);
StarlarkPath archivePath = getPath(archive);

if (!archivePath.exists()) {
Expand Down Expand Up @@ -1258,6 +1281,20 @@ public boolean isFinished() {
}
}

private static String renamedStripPrefix(String method, String stripPrefix, String oldStripPrefix)
throws EvalException {
if (oldStripPrefix.isEmpty()) {
return stripPrefix;
}
if (stripPrefix.isEmpty()) {
return oldStripPrefix;
}
throw Starlark.errorf(
"%s() got multiple values for parameter 'strip_prefix' (via compatibility alias"
+ " 'stripPrefix')",
method);
}

@StarlarkMethod(
name = "file",
doc = "Generates a file in the repository directory with the provided content.",
Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/bazel/external_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ def _rule_impl(ctx):
result = ctx.download_and_extract(
url = [],
type = "zip",
stripPrefix="ext",
strip_prefix="ext",
sha256 = ctx.attr.sha256,
allow_fail = True,
)
Expand Down

0 comments on commit 49d7bb6

Please sign in to comment.