diff --git a/.gitignore b/.gitignore index 9b4e4ea7ee73c2..e60a6dac46edab 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ *~ # Ignore Vim swap files. .*.swp +# Ignore python artifacts. +*.pyc # Ignore files generated by IDEs. /.classpath /.factorypath diff --git a/examples/py/BUILD b/examples/py/BUILD index 39ef8c0452bf05..4466642f5f332c 100644 --- a/examples/py/BUILD +++ b/examples/py/BUILD @@ -9,6 +9,7 @@ py_binary( name = "bin", srcs = ["bin.py"], deps = [":lib"], + legacy_create_init = False, ) filegroup( diff --git a/examples/py/bin.py b/examples/py/bin.py index f4a6666dddc8a2..cdc01c4b65b06f 100644 --- a/examples/py/bin.py +++ b/examples/py/bin.py @@ -1,3 +1,3 @@ -from examples.py import lib +import lib print("Fib(5)=%d" % lib.Fib(5)) diff --git a/examples/py_native/BUILD b/examples/py_native/BUILD index 9d52fd68483bbc..d85b886a59dadb 100644 --- a/examples/py_native/BUILD +++ b/examples/py_native/BUILD @@ -14,6 +14,7 @@ py_binary( ":lib", "//examples/py_native/fibonacci", ], + legacy_create_init = False, ) py_library( @@ -28,10 +29,12 @@ py_test( ":lib", "//examples/py_native/fibonacci", ], + legacy_create_init = False, ) py_test( name = "fail", srcs = ["fail.py"], deps = [":lib"], + legacy_create_init = False, ) diff --git a/examples/py_native/bin.py b/examples/py_native/bin.py index 45c68b26e525d2..eefedb06651e17 100644 --- a/examples/py_native/bin.py +++ b/examples/py_native/bin.py @@ -1,6 +1,6 @@ # pylint: disable=superfluous-parens """A tiny example binary for the native Python rules of Bazel.""" -from examples.py_native.lib import GetNumber +from lib import GetNumber from fib import Fib print("The number is %d" % GetNumber()) diff --git a/examples/py_native/fail.py b/examples/py_native/fail.py index 98e35f4ee7fc09..a505ac41aa0fbb 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 examples.py_native.lib import GetNumber +from lib import GetNumber class TestGetNumber(unittest.TestCase): diff --git a/examples/py_native/test.py b/examples/py_native/test.py index f9543aa7272536..52e81bbffb2fc9 100644 --- a/examples/py_native/test.py +++ b/examples/py_native/test.py @@ -1,7 +1,7 @@ """A tiny example binary for the native Python rules of Bazel.""" import unittest -from examples.py_native.lib import GetNumber +from lib import GetNumber from fib import Fib 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 be90d861e3a56c..13d398ad14a97b 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,6 +18,7 @@ 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; @@ -176,6 +177,11 @@ public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironmen .allowedFileTypes(PYTHON_SOURCE) .direct_compile_time_input() .allowedFileTypes(BazelPyRuleClasses.PYTHON_SOURCE)) + /* + Controls whether to create __init__.py files in paths where they don't exists + or not, default is to true for backward compatibility and to support legacy behavior. + */ + .add(attr("legacy_create_init", BOOLEAN).value(true)) /* 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 e0706923f2f409..37653e93ea49ea 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,6 +27,7 @@ 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; @@ -140,7 +141,10 @@ private static Runfiles collectCommonRunfiles(RuleContext ruleContext, PyCommon } semantics.collectDefaultRunfiles(ruleContext, builder); builder.add(ruleContext, PythonRunfilesProvider.TO_RUNFILES); - builder.setEmptyFilesSupplier(PythonUtils.GET_INIT_PY_FILES); + + if (ruleContext.attributes().get("legacy_create_init", Type.BOOLEAN)) { + 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 adc0ccae37ab99..f41d99baca7cc5 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,7 +83,6 @@ protected void collect(CcLinkParams.Builder builder, boolean linkingStatically, } else { runfilesBuilder.addTransitiveArtifacts(filesToBuild); } - runfilesBuilder.setEmptyFilesSupplier(PythonUtils.GET_INIT_PY_FILES); runfilesBuilder.add(ruleContext, PythonRunfilesProvider.TO_RUNFILES); runfilesBuilder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES);