From ea02b8a51fc924755971f78e392100b50d8dc5a2 Mon Sep 17 00:00:00 2001 From: David Chen Date: Wed, 10 Feb 2016 12:52:11 +0000 Subject: Add imports attribute to Bazel native Python rules to allow adding directories to PYTHONPATH. Fixes #702 RELNOTES: Add imports attribute to native Python rules. -- MOS_MIGRATED_REVID=114314430 --- examples/py_native/BUILD | 10 ++++++++-- examples/py_native/bin.py | 2 ++ examples/py_native/fibonacci/BUILD | 6 ++++++ examples/py_native/fibonacci/fib.py | 8 ++++++++ examples/py_native/test.py | 4 ++++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 examples/py_native/fibonacci/BUILD create mode 100644 examples/py_native/fibonacci/fib.py (limited to 'examples') diff --git a/examples/py_native/BUILD b/examples/py_native/BUILD index d6fdfa957b..5a82223cf8 100644 --- a/examples/py_native/BUILD +++ b/examples/py_native/BUILD @@ -7,7 +7,10 @@ filegroup( py_binary( name = "bin", srcs = ["bin.py"], - deps = [":lib"], + deps = [ + ":lib", + "//examples/py_native/fibonacci", + ], ) py_library( @@ -18,7 +21,10 @@ py_library( py_test( name = "test", srcs = ["test.py"], - deps = [":lib"], + deps = [ + ":lib", + "//examples/py_native/fibonacci", + ], ) py_test( diff --git a/examples/py_native/bin.py b/examples/py_native/bin.py index f79379a237..7b656278f6 100644 --- a/examples/py_native/bin.py +++ b/examples/py_native/bin.py @@ -1,4 +1,6 @@ """A tiny example binary for the native Python rules of Bazel.""" from examples.py_native.lib import GetNumber +from fib import Fib print "The number is %d" % GetNumber() +print "Fib(5) == %d" % Fib(5) diff --git a/examples/py_native/fibonacci/BUILD b/examples/py_native/fibonacci/BUILD new file mode 100644 index 0000000000..c3ee9b0fea --- /dev/null +++ b/examples/py_native/fibonacci/BUILD @@ -0,0 +1,6 @@ +py_library( + name = "fibonacci", + srcs = ["fib.py"], + imports = ["."], + visibility = ["//examples/py_native:__pkg__"], +) diff --git a/examples/py_native/fibonacci/fib.py b/examples/py_native/fibonacci/fib.py new file mode 100644 index 0000000000..645a937a0e --- /dev/null +++ b/examples/py_native/fibonacci/fib.py @@ -0,0 +1,8 @@ +"""An example binary to test the imports attribute of native Python rules.""" + + +def Fib(n): + if n == 0 or n == 1: + return 1 + else: + return Fib(n-1) + Fib(n-2) diff --git a/examples/py_native/test.py b/examples/py_native/test.py index 811eee144e..f9543aa727 100644 --- a/examples/py_native/test.py +++ b/examples/py_native/test.py @@ -1,6 +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 class TestGetNumber(unittest.TestCase): @@ -8,6 +10,8 @@ class TestGetNumber(unittest.TestCase): def test_ok(self): self.assertEquals(GetNumber(), 42) + def test_fib(self): + self.assertEquals(Fib(5), 8) if __name__ == '__main__': unittest.main() -- cgit v1.2.3