Skip to content

Commit

Permalink
Automated rollback of commit af004d1.
Browse files Browse the repository at this point in the history
*** Reason for rollback ***

Breaks many targets in the depot that use the native python rules.

*** Original change description ***

Remove Python native rule classes and py_runtime

PiperOrigin-RevId: 682373910
Change-Id: Ib874b62ff5f3fbf0899a6990d970b04b87dd9d93
  • Loading branch information
katre authored and copybara-github committed Oct 4, 2024
1 parent ef1a21f commit 41507df
Show file tree
Hide file tree
Showing 27 changed files with 1,257 additions and 225 deletions.
1 change: 0 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ genrule(
"//third_party:rules_jvm_external_6.0.patch",
"//third_party/protobuf:BUILD",
"//third_party/protobuf:remove_rules_rust.patch",
"//third_party/protobuf:add_python_loads.patch",
],
outs = ["MODULE.bazel.lock.dist"],
cmd = " && ".join([
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ archive_override(
# Temporarily patch out rules_rust stuff from protobuf. Not just because we don't need it,
# but also because it introduces huge dependency bloat: rules_rust -> aspect_rules_js ->
# aspect_rules_lint -> rules_buf.
patches = ["//third_party/protobuf:remove_rules_rust.patch", "//third_party/protobuf:add_python_loads.patch"],
patches = ["//third_party/protobuf:remove_rules_rust.patch"],
strip_prefix = "protobuf-3b62052186d39775090fb074adcba078ea622f54",
urls = ["https://github.com/protocolbuffers/protobuf/archive/3b62052186d39775090fb074adcba078ea622f54.zip"],
)
Expand Down
1 change: 0 additions & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.actions.ActionEnvironment;
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.BaseRuleClasses.EmptyRule;
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider.RuleSet;
import com.google.devtools.build.lib.analysis.PackageSpecificationProvider;
Expand All @@ -35,7 +36,10 @@
import com.google.devtools.build.lib.analysis.config.RequiresOptions;
import com.google.devtools.build.lib.bazel.BazelConfiguration;
import com.google.devtools.build.lib.bazel.repository.LocalConfigPlatformRule;
import com.google.devtools.build.lib.bazel.rules.python.BazelPyBinaryRule;
import com.google.devtools.build.lib.bazel.rules.python.BazelPyBuiltins;
import com.google.devtools.build.lib.bazel.rules.python.BazelPyRuleClasses;
import com.google.devtools.build.lib.bazel.rules.python.BazelPyTestRule;
import com.google.devtools.build.lib.bazel.rules.python.BazelPythonConfiguration;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.packages.PackageCallable;
Expand All @@ -49,6 +53,7 @@
import com.google.devtools.build.lib.rules.platform.PlatformRules;
import com.google.devtools.build.lib.rules.proto.BazelProtoCommon;
import com.google.devtools.build.lib.rules.proto.ProtoConfiguration;
import com.google.devtools.build.lib.rules.python.PyRuntimeRule;
import com.google.devtools.build.lib.rules.python.PythonConfiguration;
import com.google.devtools.build.lib.rules.repository.CoreWorkspaceRules;
import com.google.devtools.build.lib.rules.repository.NewLocalRepositoryRule;
Expand Down Expand Up @@ -311,6 +316,13 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
builder.addConfigurationFragment(PythonConfiguration.class);
builder.addConfigurationFragment(BazelPythonConfiguration.class);

builder.addRuleDefinition(new BazelPyRuleClasses.PyBaseRule());
builder.addRuleDefinition(new BazelPyRuleClasses.PyBinaryBaseRule());
builder.addRuleDefinition(new EmptyRule("py_library") {});
builder.addRuleDefinition(new BazelPyBinaryRule());
builder.addRuleDefinition(new BazelPyTestRule());
builder.addRuleDefinition(new PyRuntimeRule());

// This symbol is overridden by exports.bzl
builder.addBzlToplevel(
"py_internal",
Expand All @@ -321,8 +333,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {

try {
builder.addWorkspaceFileSuffix(
ResourceFileLoader.loadResource(
BazelPythonConfiguration.class, "python.WORKSPACE"));
ResourceFileLoader.loadResource(BazelPyBinaryRule.class, "python.WORKSPACE"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2014 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.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.bazel.rules.python.BazelPyRuleClasses.PyBinaryBaseRule;
import com.google.devtools.build.lib.packages.RuleClass;

/**
* Rule definition for the {@code py_binary} rule.
*/
public final class BazelPyBinaryRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
/* <!-- #BLAZE_RULE(py_binary).NAME -->
<br/>If <code>main</code> is unspecified, this should be the same as the name
of the source file that is the main entry point of the application,
minus the extension. For example, if your entry point is called
<code>main.py</code>, then your name should be <code>main</code>.
<!-- #END_BLAZE_RULE.NAME --> */
return builder
.build();
}

@Override
public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("py_binary")
.ancestors(PyBinaryBaseRule.class, BaseRuleClasses.BinaryBaseRule.class)
.factoryClass(BaseRuleClasses.EmptyRuleConfiguredTargetFactory.class)
.build();
}
}

/*<!-- #BLAZE_RULE (NAME = py_binary, TYPE = BINARY, FAMILY = Python) -->
<p>
A <code>py_binary</code> is an executable Python program consisting
of a collection of <code>.py</code> source files (possibly belonging
to other <code>py_library</code> rules), a <code>*.runfiles</code>
directory tree containing all the code and data needed by the
program at run-time, and a stub script that starts up the program with
the correct initial environment and data.
</p>
<h4 id="py_binary_examples">Examples</h4>
<pre class="code">
py_binary(
name = "foo",
srcs = ["foo.py"],
data = [":transform"], # a cc_binary which we invoke at run time
deps = [
":foolib", # a py_library
],
)
</pre>
<p>If you want to run a <code>py_binary</code> from within another binary or
test (for example, running a python binary to set up some mock resource from
within a java_test) then the correct approach is to make the other binary or
test depend on the <code>py_binary</code> in its data section. The other
binary can then locate the <code>py_binary</code> relative to the source
directory.
</p>
<pre class="code">
py_binary(
name = "test_main",
srcs = ["test_main.py"],
deps = [":testing"],
)
java_library(
name = "testing",
srcs = glob(["*.java"]),
data = [":test_main"]
)
</pre>
<!-- #END_BLAZE_RULE -->*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2014 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 static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;

import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.bazel.rules.python.BazelPyRuleClasses.PyBaseRule;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.rules.python.PyRuleClasses;
import com.google.devtools.build.lib.rules.python.PythonConfiguration;

/**
* Rule definition for the {@code py_library} rule.
*/
public final class BazelPyLibraryRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
.requiresConfigurationFragments(PythonConfiguration.class)

/* <!-- #BLAZE_RULE(py_library).ATTRIBUTE(srcs) -->
The list of source (<code>.py</code>) files that are processed to create the target.
This includes all your checked-in code and any generated source files.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(
attr("srcs", LABEL_LIST)
.direct_compile_time_input()
.allowedFileTypes(PyRuleClasses.PYTHON_SOURCE))
.build();
}

@Override
public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("py_library")
.ancestors(PyBaseRule.class)
.factoryClass(BaseRuleClasses.EmptyRuleConfiguredTargetFactory.class)
.build();
}
}

/*<!-- #BLAZE_RULE (NAME = py_library, TYPE = LIBRARY, FAMILY = Python) -->
<!-- #END_BLAZE_RULE -->*/
Loading

0 comments on commit 41507df

Please sign in to comment.