Skip to content

Commit

Permalink
Add workspace name to module_space in python stub_template.txt file
Browse files Browse the repository at this point in the history
Fixed #1040

RELNOTES:none

--
MOS_MIGRATED_REVID=118941667
  • Loading branch information
meteorcloudy authored and kchodorow committed Apr 5, 2016
1 parent b456840 commit 2784fa0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public void createExecutable(RuleContext ruleContext, PyCommon common,
ImmutableList.of(
Substitution.of("%main%", main),
Substitution.of("%python_binary%", pythonBinary),
Substitution.of("%imports%", Joiner.on(":").join(imports))),
Substitution.of("%imports%", Joiner.on(":").join(imports)),
Substitution.of("%workspace_name%", ruleContext.getWorkspaceName())),
true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ def Main():
sys.argv[0])

python_imports = '%imports%'
python_path_entries = CreatePythonPathEntries(python_imports, module_space)
module_space_with_workspace_name = module_space
if '%workspace_name%' != '':
module_space_with_workspace_name = os.path.join(module_space, '%workspace_name%')

external_dir = os.path.join(module_space, 'external')
python_path_entries = CreatePythonPathEntries(
python_imports, module_space_with_workspace_name)

external_dir = os.path.join(module_space_with_workspace_name, 'external')
if os.path.isdir(external_dir):
external_entries = [os.path.join(external_dir, d) for d in os.listdir(external_dir)]
repositories = [d for d in external_entries if os.path.isdir(d)]
Expand Down
75 changes: 75 additions & 0 deletions src/test/shell/bazel/bazel_rules_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,79 @@ EOF
expect_log bazel-genfiles/external/r/package/c/d
}

function test_python_with_workspace_name() {

create_new_workspace
cd ${new_workspace_dir}
mkdir -p {module_a,module_b}
local remote_path="${new_workspace_dir}"

cat > module_a/BUILD <<EOF
package(default_visibility = ["//visibility:public"])
py_library(name = "foo", srcs=["foo.py"])
EOF

cat > module_b/BUILD <<EOF
package(default_visibility = ["//visibility:public"])
py_library(name = "bar", deps = ["//module_a:foo"], srcs=["bar.py"],)
py_binary(name = "bar2", deps = ["//module_a:foo"], srcs=["bar2.py"],)
EOF

cat > module_a/foo.py <<EOF
def GetNumber():
return 42
EOF

cat > module_b/bar.py <<EOF
from module_a import foo
def PrintNumber():
print "Print the number %d" % foo.GetNumber()
EOF

cat > module_b/bar2.py <<EOF
from module_a import foo
print "The number is %d" % foo.GetNumber()
EOF

cd ${WORKSPACE_DIR}
mkdir -p {module1,module2}
cat > WORKSPACE <<EOF
workspace(name = "foobar")
local_repository(name="remote", path="${remote_path}")
EOF
cat > module1/BUILD <<EOF
package(default_visibility = ["//visibility:public"])
py_library(name = "fib", srcs=["fib.py"],)
EOF
cat > module2/BUILD <<EOF
py_binary(name = "bez",
deps = ["@remote//module_a:foo", "@remote//module_b:bar", "//module1:fib"],
srcs = ["bez.py"],)
EOF

cat > module1/fib.py <<EOF
def Fib(n):
if n == 0 or n == 1:
return 1
else:
return Fib(n-1) + Fib(n-2)
EOF

cat > module2/bez.py <<EOF
from external.remote.module_a import foo
from external.remote.module_b import bar
from module1 import fib
print "The number is %d" % foo.GetNumber()
bar.PrintNumber()
print "Fib(10) is %d" % fib.Fib(10)
EOF
bazel run //module2:bez >$TEST_log
expect_log "The number is 42"
expect_log "Print the number 42"
expect_log "Fib(10) is 89"
bazel run @remote//module_b:bar2 >$TEST_log
expect_log "The number is 42"
}

run_suite "rules test"

0 comments on commit 2784fa0

Please sign in to comment.