Skip to content

Commit

Permalink
Move merge_runfiles_with_generated_inits_empty_files_supplier to common
Browse files Browse the repository at this point in the history
PyBuiltins.

This particular function relies on the Bazel/Google-specific PythonSemantics
init py filter functions, so required a bit of refactoring to pass around
the appropriate function to the helpers.

Work towards bazelbuild#15897

PiperOrigin-RevId: 488742400
Change-Id: I1e7b2ce3c69b3e89415aa0ce65457f12c1923011
  • Loading branch information
rickeylev authored and copybara-github committed Nov 15, 2022
1 parent 37553cb commit 2a471a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
import com.google.devtools.build.lib.rules.python.PyBuiltins;

/** PyBuiltins with Bazel-specific functionality. */
public final class BazelPyBuiltins extends PyBuiltins {}
public final class BazelPyBuiltins extends PyBuiltins {
public BazelPyBuiltins() {
super(BazelPythonSemantics.GET_INIT_PY_FILES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
public abstract class PyBuiltins implements StarlarkValue {
public static final String NAME = "py_builtins";

private final Runfiles.EmptyFilesSupplier emptyFilesSupplier;

protected PyBuiltins(Runfiles.EmptyFilesSupplier emptyFilesSupplier) {
this.emptyFilesSupplier = emptyFilesSupplier;
}

@StarlarkMethod(
name = "is_singleton_depset",
doc = "Efficiently checks if the depset is a singleton.",
Expand Down Expand Up @@ -259,6 +265,36 @@ public Object newEmptyRunfilesWithMiddleman(
.build();
}

@StarlarkMethod(
name = "merge_runfiles_with_generated_inits_empty_files_supplier",
doc =
"Create a runfiles that generates missing __init__.py files using Java and "
+ "the internal EmptyFilesProvider interface",
parameters = {
@Param(name = "ctx", positional = false, named = true, defaultValue = "unbound"),
@Param(
name = "runfiles",
positional = false,
named = true,
defaultValue = "unbound",
doc = "Runfiles to merge into the result; must be non-empty"),
})
public Object mergeRunfilesWithGeneratedInitsEmptyFilesSupplier(
StarlarkRuleContext starlarkCtx, Runfiles runfiles) throws EvalException {
if (runfiles.isEmpty()) {
// The Runfiles merge functions have an optimization to detect an empty runfiles and return a
// singleton. Unfortunately, this optimization considers an empty runfiles with
// a emptyFilesSupplier as empty, so then drops it when returning the singleton. To work
// around this, require that there is *something* in the runfiles.
throw Starlark.errorf("input runfiles cannot be empty");
}
return new Runfiles.Builder(
starlarkCtx.getWorkspaceName(), starlarkCtx.getConfiguration().legacyExternalRunfiles())
.setEmptyFilesSupplier(emptyFilesSupplier)
.merge(runfiles)
.build();
}

@StarlarkMethod(
name = "declare_constant_metadata_file",
doc = "Declare a file that always reports it is unchanged.",
Expand Down

0 comments on commit 2a471a2

Please sign in to comment.