Skip to content

Commit

Permalink
Make __init__.py files creation optional
Browse files Browse the repository at this point in the history
Introduce a new attribute to py_binary and py_test to control whether to
create `__init__.py` or not.

Fixes bazelbuild/rules_python#55
  • Loading branch information
mouadino committed Jan 17, 2018
1 parent e01d0c8 commit ea3e8f7
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
*~
# Ignore Vim swap files.
.*.swp
# Ignore python artifacts.
*.pyc
# Ignore files generated by IDEs.
/.classpath
/.factorypath
Expand Down
1 change: 1 addition & 0 deletions examples/py/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ py_binary(
name = "bin",
srcs = ["bin.py"],
deps = [":lib"],
legacy_create_init = False,
)

filegroup(
Expand Down
2 changes: 1 addition & 1 deletion examples/py/bin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from examples.py import lib
import lib

print("Fib(5)=%d" % lib.Fib(5))
3 changes: 3 additions & 0 deletions examples/py_native/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ py_binary(
":lib",
"//examples/py_native/fibonacci",
],
legacy_create_init = False,
)

py_library(
Expand All @@ -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,
)
2 changes: 1 addition & 1 deletion examples/py_native/bin.py
Original file line number Diff line number Diff line change
@@ -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())
Expand Down
2 changes: 1 addition & 1 deletion examples/py_native/fail.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
2 changes: 1 addition & 1 deletion examples/py_native/test.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -176,6 +177,11 @@ public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironmen
.allowedFileTypes(PYTHON_SOURCE)
.direct_compile_time_input()
.allowedFileTypes(BazelPyRuleClasses.PYTHON_SOURCE))
/* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(legacy_create_init) -->
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.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("legacy_create_init", BOOLEAN).value(true))
/* <!-- #BLAZE_RULE($base_py_binary).ATTRIBUTE(stamp) -->
Enable link stamping.
Whether to encode build information into the binary. Possible values:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit ea3e8f7

Please sign in to comment.