diff options
author | lberki <lberki@google.com> | 2018-02-14 15:12:23 +0100 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2018-02-14 15:54:48 +0100 |
commit | fa0fac2a4e8a2e5c01b8390878289d00dcc17dba (patch) | |
tree | 848acb70c03d5cbccd1c30234bef513a4a871f40 | |
parent | 0f9c6ea574918dda094cf5423fa3822112846c30 (diff) |
Automated rollback of commit 0f9c6ea574918dda094cf5423fa3822112846c30.
*** Reason for rollback ***
Breaks Kokoro and I accidentally submitted the change without presubmit checks.
*** Original change description ***
Make __init__.py files creation optional
Introduce a new attribute to py_binary and py_test to control whether to
create `__init__.py` or not.
Fixes https://github.com/bazelbuild/rules_python/issues/55
Closes #4470.
PiperOrigin-RevId: 185676592
-rw-r--r-- | examples/py/BUILD | 1 | ||||
-rw-r--r-- | examples/py/bin.py | 2 | ||||
-rw-r--r-- | examples/py_native/BUILD | 3 | ||||
-rw-r--r-- | examples/py_native/bin.py | 2 | ||||
-rw-r--r-- | examples/py_native/fail.py | 2 | ||||
-rw-r--r-- | examples/py_native/test.py | 2 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyRuleClasses.java | 49 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java | 7 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java | 1 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java | 6 | ||||
-rw-r--r-- | src/test/py/bazel/py_test.py | 41 |
11 files changed, 27 insertions, 89 deletions
diff --git a/examples/py/BUILD b/examples/py/BUILD index 314bc16ef5..39ef8c0452 100644 --- a/examples/py/BUILD +++ b/examples/py/BUILD @@ -8,7 +8,6 @@ py_library( py_binary( name = "bin", srcs = ["bin.py"], - legacy_create_init = False, deps = [":lib"], ) diff --git a/examples/py/bin.py b/examples/py/bin.py index cdc01c4b65..f4a6666ddd 100644 --- a/examples/py/bin.py +++ b/examples/py/bin.py @@ -1,3 +1,3 @@ -import lib +from examples.py import lib print("Fib(5)=%d" % lib.Fib(5)) diff --git a/examples/py_native/BUILD b/examples/py_native/BUILD index aafa7b77c7..9d52fd6848 100644 --- a/examples/py_native/BUILD +++ b/examples/py_native/BUILD @@ -10,7 +10,6 @@ filegroup( py_binary( name = "bin", srcs = ["bin.py"], - legacy_create_init = False, deps = [ ":lib", "//examples/py_native/fibonacci", @@ -25,7 +24,6 @@ py_library( py_test( name = "test", srcs = ["test.py"], - legacy_create_init = False, deps = [ ":lib", "//examples/py_native/fibonacci", @@ -35,6 +33,5 @@ py_test( py_test( name = "fail", srcs = ["fail.py"], - legacy_create_init = False, deps = [":lib"], ) diff --git a/examples/py_native/bin.py b/examples/py_native/bin.py index b7e1223988..45c68b26e5 100644 --- a/examples/py_native/bin.py +++ b/examples/py_native/bin.py @@ -1,7 +1,7 @@ # pylint: disable=superfluous-parens """A tiny example binary for the native Python rules of Bazel.""" +from examples.py_native.lib import GetNumber from fib import Fib -from lib import GetNumber print("The number is %d" % GetNumber()) print("Fib(5) == %d" % Fib(5)) diff --git a/examples/py_native/fail.py b/examples/py_native/fail.py index a505ac41aa..98e35f4ee7 100644 --- a/examples/py_native/fail.py +++ b/examples/py_native/fail.py @@ -1,6 +1,6 @@ """A tiny example binary for the native Python rules of Bazel.""" import unittest -from lib import GetNumber +from examples.py_native.lib import GetNumber class TestGetNumber(unittest.TestCase): diff --git a/examples/py_native/test.py b/examples/py_native/test.py index b860940a38..f9543aa727 100644 --- a/examples/py_native/test.py +++ b/examples/py_native/test.py @@ -1,8 +1,8 @@ """A tiny example binary for the native Python rules of Bazel.""" import unittest +from examples.py_native.lib import GetNumber from fib import Fib -from lib import GetNumber class TestGetNumber(unittest.TestCase): diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyRuleClasses.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyRuleClasses.java index 43091e86fb..77e7e42007 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyRuleClasses.java @@ -18,7 +18,6 @@ import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; import static com.google.devtools.build.lib.packages.BuildType.TRISTATE; -import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; import static com.google.devtools.build.lib.syntax.Type.STRING; import static com.google.devtools.build.lib.syntax.Type.STRING_LIST; @@ -136,14 +135,14 @@ public final class BazelPyRuleClasses { @Override public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironment env) { return builder - /* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(data) --> - The list of files needed by this binary at runtime. - See general comments about <code>data</code> at - <a href="${link common-definitions#common-attributes}"> - Attributes common to all build rules</a>. - Also see the <a href="${link py_library.data}"><code>data</code></a> argument of - the <a href="${link py_library}"><code>py_library</code></a> rule for details. - <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ + /* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(data) --> + The list of files needed by this binary at runtime. + See general comments about <code>data</code> at + <a href="${link common-definitions#common-attributes}"> + Attributes common to all build rules</a>. + Also see the <a href="${link py_library.data}"><code>data</code></a> argument of + the <a href="${link py_library}"><code>py_library</code></a> rule for details. + <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ /* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(main) --> The name of the source file that is the main entry point of the application. @@ -158,13 +157,11 @@ public final class BazelPyRuleClasses { Valid values are <code>"PY2"</code> (default) or <code>"PY3"</code>. Python 3 support is experimental. <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add( - attr("default_python_version", STRING) - .value(PythonVersion.defaultTargetPythonVersion().toString()) - .allowedValues(new AllowedValueSet(PythonVersion.getTargetPythonValues())) - .nonconfigurable( - "read by PythonUtils.getNewPythonVersion, which doesn't have access" - + " to configuration keys")) + .add(attr("default_python_version", STRING) + .value(PythonVersion.defaultTargetPythonVersion().toString()) + .allowedValues(new AllowedValueSet(PythonVersion.getTargetPythonValues())) + .nonconfigurable("read by PythonUtils.getNewPythonVersion, which doesn't have access" + + " to configuration keys")) /* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(srcs) --> The list of source files that are processed to create the target. This includes all your checked-in code and any @@ -173,21 +170,11 @@ public final class BazelPyRuleClasses { probably belong in <code>srcs</code> and library targets probably belong in <code>deps</code>, but don't worry about it too much. <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add( - attr("srcs", LABEL_LIST) - .mandatory() - .allowedFileTypes(PYTHON_SOURCE) - .direct_compile_time_input() - .allowedFileTypes(BazelPyRuleClasses.PYTHON_SOURCE)) - /* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(legacy_create_init) --> - Whether to implicitly create empty __init__.py files in the runfiles tree. - These are created in every directory containing Python source code or - shared libraries, and every parent directory of those directories. - Default is true for backward compatibility. If false, the user is responsible - for creating __init__.py files (empty or not) and adding them to `srcs` or `deps` - of Python targets as required. - <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("legacy_create_init", BOOLEAN).value(true)) + .add(attr("srcs", LABEL_LIST) + .mandatory() + .allowedFileTypes(PYTHON_SOURCE) + .direct_compile_time_input() + .allowedFileTypes(BazelPyRuleClasses.PYTHON_SOURCE)) /* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(stamp) --> Enable link stamping. Whether to encode build information into the binary. Possible values: diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java index b2ef88db97..e0706923f2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java @@ -27,7 +27,6 @@ import com.google.devtools.build.lib.rules.cpp.CcCommon.CcFlagsSupplier; import com.google.devtools.build.lib.rules.cpp.CcLinkParams; import com.google.devtools.build.lib.rules.cpp.CcLinkParamsInfo; import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore; -import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.ArrayList; import java.util.List; @@ -141,11 +140,7 @@ public abstract class PyBinary implements RuleConfiguredTargetFactory { } semantics.collectDefaultRunfiles(ruleContext, builder); builder.add(ruleContext, PythonRunfilesProvider.TO_RUNFILES); - - if (!ruleContext.attributes().has("legacy_create_init", Type.BOOLEAN) - || ruleContext.attributes().get("legacy_create_init", Type.BOOLEAN)) { - builder.setEmptyFilesSupplier(PythonUtils.GET_INIT_PY_FILES); - } + builder.setEmptyFilesSupplier(PythonUtils.GET_INIT_PY_FILES); semantics.collectRunfilesForBinary(ruleContext, builder, common); return builder.build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java index f41d99baca..adc0ccae37 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java @@ -83,6 +83,7 @@ public abstract class PyLibrary implements RuleConfiguredTargetFactory { } else { runfilesBuilder.addTransitiveArtifacts(filesToBuild); } + runfilesBuilder.setEmptyFilesSupplier(PythonUtils.GET_INIT_PY_FILES); runfilesBuilder.add(ruleContext, PythonRunfilesProvider.TO_RUNFILES); runfilesBuilder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES); diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java index b1fecc017e..61dd892ff6 100644 --- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java +++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java @@ -1449,9 +1449,9 @@ public class SkylarkRuleContextTest extends SkylarkTestCase { scratch.file( "test/BUILD", "load('//test:rule.bzl', 'skylark_rule')", - "py_binary(name = 'lib', srcs = ['lib.py', 'lib2.py'])", + "py_library(name = 'lib', srcs = ['a.py', 'b.py'])", "skylark_rule(name = 'foo', dep = ':lib')", - "py_binary(name = 'lib_with_init', srcs = ['lib_with_init.py', 'lib2.py', '__init__.py'])", + "py_library(name = 'lib_with_init', srcs = ['a.py', 'b.py', '__init__.py'])", "skylark_rule(name = 'foo_with_init', dep = ':lib_with_init')"); SkylarkRuleContext ruleContext = createRuleContext("//test:foo"); @@ -1460,7 +1460,7 @@ public class SkylarkRuleContextTest extends SkylarkTestCase { ruleContext, "[f.short_path for f in ruleContext.attr.dep.default_runfiles.files]"); assertThat(filenames).isInstanceOf(SkylarkList.class); SkylarkList filenamesList = (SkylarkList) filenames; - assertThat(filenamesList).containsExactly("test/lib", "test/lib.py", "test/lib2.py"); + assertThat(filenamesList).containsExactly("test/a.py", "test/b.py").inOrder(); Object emptyFilenames = evalRuleContextCode( ruleContext, "list(ruleContext.attr.dep.default_runfiles.empty_filenames)"); diff --git a/src/test/py/bazel/py_test.py b/src/test/py/bazel/py_test.py index 3d97e79105..26fe88a0d6 100644 --- a/src/test/py/bazel/py_test.py +++ b/src/test/py/bazel/py_test.py @@ -65,46 +65,5 @@ class PyTest(test_base.TestBase): .endswith('/a/b.py')) -class TestInitPyFiles(test_base.TestBase): - - def createSimpleFiles(self, create_init=True): - self.ScratchFile('WORKSPACE') - - self.ScratchFile('src/a/BUILD', [ - 'py_binary(name="a", srcs=["a.py"], deps=[":b"], legacy_create_init=%s)' - % create_init, - 'py_library(name="b", srcs=["b.py"])', - ]) - - self.ScratchFile('src/a/a.py', [ - 'from src.a import b', - 'b.Hello()', - ]) - - self.ScratchFile('src/a/b.py', [ - 'def Hello():', - ' print("Hello, World")', - ]) - - def testInitPyFilesCreated(self): - self.createSimpleFiles() - exit_code, _, stderr = self.RunBazel(['build', '//src/a:a']) - self.AssertExitCode(exit_code, 0, stderr) - self.assertTrue( - os.path.exists('bazel-bin/src/a/a.runfiles/__main__/src/__init__.py')) - self.assertTrue( - os.path.exists('bazel-bin/src/a/a.runfiles/__main__/src/a/__init__.py')) - - -def testInitPyFilesNotCreatedWhenLegacyCreateInitIsSet(self): - self.createSimpleFiles(create_init=False) - exit_code, _, stderr = self.RunBazel(['build', '//src/a:a']) - self.AssertExitCode(exit_code, 0, stderr) - self.assertFalse( - os.path.exists('bazel-bin/src/a/a.runfiles/__main__/src/__init__.py')) - self.assertFalse( - os.path.exists('bazel-bin/src/a/a.runfiles/__main__/src/a/__init__.py')) - - if __name__ == '__main__': unittest.main() |