Skip to content

Commit

Permalink
Refactor a little Python rule logic
Browse files Browse the repository at this point in the history
Merge PyBinary and PyTest classes into a single PyExecutable class.

RELNOTES: None
PiperOrigin-RevId: 227142490
  • Loading branch information
brandjon authored and Copybara-Service committed Dec 28, 2018
1 parent 1cfa750 commit f451cd8
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@

package com.google.devtools.build.lib.bazel.rules.python;

import com.google.devtools.build.lib.rules.python.PyBinary;
import com.google.devtools.build.lib.rules.python.PyExecutable;
import com.google.devtools.build.lib.rules.python.PythonSemantics;

/**
* Implementation of the {@code py_binary} rule for Bazel.
*/
public class BazelPyBinary extends PyBinary {
/** Bazel-specific implementation of {@code py_binary}. */
public class BazelPyBinary extends PyExecutable {
@Override
protected PythonSemantics createSemantics() {
return new BazelPythonSemantics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import com.google.devtools.build.lib.rules.python.PyLibrary;
import com.google.devtools.build.lib.rules.python.PythonSemantics;

/**
* Implementation of the {@code py_library} rule for Bazel.
*/
/** Bazel-specific implementation of the {@code py_library}. */
public class BazelPyLibrary extends PyLibrary {
@Override
protected PythonSemantics createSemantics() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@

package com.google.devtools.build.lib.bazel.rules.python;

import com.google.devtools.build.lib.rules.python.PyTest;
import com.google.devtools.build.lib.rules.python.PyExecutable;
import com.google.devtools.build.lib.rules.python.PythonSemantics;

/**
* Implementation of the {@code py_test} rule for Bazel.
*/
public class BazelPyTest extends PyTest {
/** Bazel-specific implementation of {@code py_test}. */
public class BazelPyTest extends PyExecutable {
@Override
protected PythonSemantics createSemantics() {
return new BazelPythonSemantics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ private static Artifact createWindowsExeLauncher(
}

@Override
public void postInitBinary(RuleContext ruleContext, RunfilesSupport runfilesSupport,
PyCommon common) throws InterruptedException {
public void postInitExecutable(
RuleContext ruleContext, RunfilesSupport runfilesSupport, PyCommon common) {
if (ruleContext.getFragment(PythonConfiguration.class).buildPythonZip()) {
FilesToRunProvider zipper = ruleContext.getExecutablePrerequisite("$zipper", Mode.HOST);
Artifact executable = common.getExecutable();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 The Bazel Authors. All rights reserved.
// Copyright 2018 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.
Expand All @@ -11,6 +11,7 @@
// 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 com.google.devtools.build.lib.actions.Artifact;
Expand All @@ -30,31 +31,20 @@
import java.util.ArrayList;
import java.util.List;

/**
* An implementation for the {@code py_binary} rule.
*/
public abstract class PyBinary implements RuleConfiguredTargetFactory {
/** Common implementation logic for {@code py_binary} and {@code py_test}. */
public abstract class PyExecutable implements RuleConfiguredTargetFactory {

/**
* Create a {@link PythonSemantics} object that governs
* the behavior of this rule.
* Creates a pluggable semantics object to be used for the analysis of a target of this rule type.
*/
protected abstract PythonSemantics createSemantics();

@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
PyCommon common = new PyCommon(ruleContext);
PythonSemantics semantics = createSemantics();

RuleConfiguredTargetBuilder builder = init(ruleContext, createSemantics(), common);
if (builder == null) {
return null;
}
return builder.build();
}

static RuleConfiguredTargetBuilder init(
RuleContext ruleContext, PythonSemantics semantics, PyCommon common)
throws InterruptedException, RuleErrorException {
ruleContext.initConfigurationMakeVariableContext(new CcFlagsSupplier(ruleContext));

List<Artifact> srcs = common.validateSrcs();
Expand Down Expand Up @@ -122,14 +112,15 @@ static RuleConfiguredTargetBuilder init(
new RuleConfiguredTargetBuilder(ruleContext);
common.addCommonTransitiveInfoProviders(builder, semantics, common.getFilesToBuild(), imports);

semantics.postInitBinary(ruleContext, runfilesSupport, common);
semantics.postInitExecutable(ruleContext, runfilesSupport, common);

return builder
.setFilesToBuild(common.getFilesToBuild())
.add(RunfilesProvider.class, runfilesProvider)
.setRunfilesSupport(runfilesSupport, realExecutable)
.addNativeDeclaredProvider(new PyCcLinkParamsProvider(ccInfo))
.add(PythonImportsProvider.class, new PythonImportsProvider(imports));
.add(PythonImportsProvider.class, new PythonImportsProvider(imports))
.build();
}

private static Runfiles collectCommonRunfiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import java.util.ArrayList;
import java.util.List;

/**
* An implementation for the {@code py_library} rule.
*/
/** Base implementation of {@code py_library}. */
public abstract class PyLibrary implements RuleConfiguredTargetFactory {

/**
Expand All @@ -40,7 +38,7 @@ public abstract class PyLibrary implements RuleConfiguredTargetFactory {
protected abstract PythonSemantics createSemantics();

@Override
public ConfiguredTarget create(final RuleContext ruleContext)
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
PythonSemantics semantics = createSemantics();
PyCommon common = new PyCommon(ruleContext);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@
*/
public interface PythonSemantics {
/**
* Called at the beginning of the analysis of {@code py_binary} rules to validate its attributes.
* Called at the beginning of the analysis of {@code py_binary}, {@code py_test}, and {@code
* py_library} targets to validate their attributes.
*/
void validate(RuleContext ruleContext, PyCommon common);

/** Extends for the default and data runfiles of {@code py_binary} rules with custom elements. */
/**
* Extends for the default and data runfiles of {@code py_binary} and {@code py_test} rules with
* custom elements.
*/
void collectRunfilesForBinary(
RuleContext ruleContext, Runfiles.Builder builder, PyCommon common, CcInfo ccInfo)
throws InterruptedException, RuleErrorException;

/** Extends the default runfiles of {@code py_binary} rules with custom elements. */
/**
* Extends the default runfiles of {@code py_binary} and {@code py_test} rules with custom
* elements.
*/
void collectDefaultRunfilesForBinary(RuleContext ruleContext, Runfiles.Builder builder)
throws InterruptedException;

Expand Down Expand Up @@ -77,11 +84,12 @@ Artifact createExecutable(
throws InterruptedException, RuleErrorException;

/**
* Called at the end of the analysis of {@code py_binary} rules.
* Called at the end of the analysis of {@code py_binary} and {@code py_test} targets.
*
* @throws InterruptedException
*/
void postInitBinary(RuleContext ruleContext, RunfilesSupport runfilesSupport,
PyCommon common) throws InterruptedException;
void postInitExecutable(RuleContext ruleContext, RunfilesSupport runfilesSupport, PyCommon common)
throws InterruptedException;

CcInfo buildCcInfoProvider(Iterable<? extends TransitiveInfoCollection> deps);
}

0 comments on commit f451cd8

Please sign in to comment.