-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add py_builtins and py_internal helper namespaces in Bazel to allow S…
…tarlark to use Java helpers * `py_builtins` is an Starlark builtins-internal-only namespace for functions that only builtins should use. * `py_internal` is for regular Starlark for testing and other esoteric purposes. Usage of this is restricted to packages relating to the rule implementations to prevent using these internal APIs. (These already exist in the Google-internal build; this makes them defined in Bazel, too, though they're currently no-ops; followup CLs will begin moving common code out of the Google-py_internal and into the common one). Work towards #15897 PiperOrigin-RevId: 485146419 Change-Id: I71902e60471a4227c1993778bda60cbbe6e316f0
- Loading branch information
1 parent
a8d0d50
commit 2405238
Showing
7 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBuiltins.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2022 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. | ||
|
||
package com.google.devtools.build.lib.bazel.rules.python; | ||
|
||
import com.google.devtools.build.lib.rules.python.PyBuiltins; | ||
|
||
/** PyBuiltins with Bazel-specific functionality. */ | ||
public final class BazelPyBuiltins extends PyBuiltins {} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/google/devtools/build/lib/rules/python/PyBuiltins.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2022 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. | ||
package com.google.devtools.build.lib.rules.python; | ||
|
||
import net.starlark.java.annot.StarlarkBuiltin; | ||
import net.starlark.java.eval.StarlarkValue; | ||
|
||
/** Bridge to allow builtins bzl code to call Java code. */ | ||
@StarlarkBuiltin(name = "py_builtins", documented = false) | ||
public abstract class PyBuiltins implements StarlarkValue { | ||
public static final String NAME = "py_builtins"; | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core/ContextGuardedValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2022 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. | ||
|
||
package com.google.devtools.build.lib.starlarkbuildapi.core; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.devtools.build.lib.cmdline.BazelCompileContext; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import com.google.devtools.build.lib.cmdline.PackageIdentifier; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
import net.starlark.java.eval.GuardedValue; | ||
import net.starlark.java.eval.StarlarkSemantics; | ||
|
||
/** | ||
* Wrapper on a value in the predeclared lexical block that controls its accessibility to Starlark | ||
* based on the context, in particular the package path the requesting .bzl file falls under. | ||
*/ | ||
public final class ContextGuardedValue { | ||
|
||
private ContextGuardedValue() {} | ||
|
||
/** | ||
* Creates a guard which only permits access of the given object when the requesting .bzl file is | ||
* in a specific patckage path. An error is thrown if accessing it is done outside the allowed | ||
* package paths. | ||
*/ | ||
public static GuardedValue onlyInAllowedRepos( | ||
Object obj, ImmutableSet<PackageIdentifier> allowedPrefixes) { | ||
return new GuardedValue() { | ||
@Override | ||
public boolean isObjectAccessibleUsingSemantics( | ||
StarlarkSemantics semantics, @Nullable Object clientData) { | ||
// Filtering of predeclareds is only done at compile time, when the client data is | ||
// BazelCompileContext and not BazelModuleContext. | ||
if (clientData != null && clientData instanceof BazelCompileContext) { | ||
BazelCompileContext context = (BazelCompileContext) clientData; | ||
Label label = context.label(); | ||
|
||
for (PackageIdentifier prefix : allowedPrefixes) { | ||
if (label.getRepository().equals(prefix.getRepository()) | ||
&& label.getPackageFragment().startsWith(prefix.getPackageFragment())) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getErrorFromAttemptingAccess(String name) { | ||
return name | ||
+ " may only be used from one of the following repositories or prefixes: " | ||
+ allowedPrefixes.stream() | ||
.map(PackageIdentifier::toString) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
|
||
@Override | ||
public Object getObject() { | ||
return obj; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/starlark/builtins_bzl/common/python/py_internal.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2022 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. | ||
"""PYTHON RULE IMPLEMENTATION ONLY: Do not use outside of the rule implementations and their tests. | ||
Various builtin Starlark defined objects exposed for non-builtin Starlark. | ||
""" | ||
|
||
# This replaces the Java-defined name using exports.bzl toplevels mapping. | ||
py_internal = struct() |