python,runfiles: move to different package

Move the Python runfiles library from
`@bazel_tools//tools/runfiles:py-runfiles` to
`@bazel_tools//tools/python/runfiles:runfiles`

Also rename the testdata runfiles.py to foo.py.
This file was not a mock runfiles library, just a
client file using the runfiles library that was
also called runfiles.py

Fixes https://github.com/bazelbuild/bazel/issues/4878

Change-Id: I874b230c93679d4454ac91e816932c8272ecc5c7

Closes #4981.

Change-Id: I908e0ab7ec61225e82f70793b1a05432e7f0b07e
PiperOrigin-RevId: 192256481
diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py
index 1a9a670..21ed5fd 100644
--- a/src/test/py/bazel/runfiles_test.py
+++ b/src/test/py/bazel/runfiles_test.py
@@ -74,7 +74,7 @@
     for s, t in [
         ("WORKSPACE.mock", "WORKSPACE"),
         ("foo/BUILD.mock", "foo/BUILD"),
-        ("foo/runfiles.py", "foo/runfiles.py"),
+        ("foo/foo.py", "foo/foo.py"),
         ("foo/datadep/hello.txt", "foo/datadep/hello.txt"),
         ("bar/BUILD.mock", "bar/BUILD"),
         ("bar/bar.py", "bar/bar.py"),
@@ -131,19 +131,15 @@
 
       i += 2
 
-  # TODO(laszlocsomor): re-enable after
-  # https://github.com/bazelbuild/bazel/issues/4878 is fixed.
-  # def testPythonRunfilesLibraryInBazelToolsRepo(self):
-  #  self._AssertPythonRunfilesLibraryInBazelToolsRepo("py", "Python")
+  def testPythonRunfilesLibraryInBazelToolsRepo(self):
+    self._AssertPythonRunfilesLibraryInBazelToolsRepo("py", "Python")
 
   def testRunfilesLibrariesFindRunfilesWithoutEnvvars(self):
     for s, t in [
         ("WORKSPACE.mock", "WORKSPACE"),
         ("bar/BUILD.mock", "bar/BUILD"),
-        # TODO(laszlocsomor): uncomment Python files after
-        # https://github.com/bazelbuild/bazel/issues/4878 is fixed.
-        # ("bar/bar.py", "bar/bar.py"),
-        # ("bar/bar-py-data.txt", "bar/bar-py-data.txt"),
+        ("bar/bar.py", "bar/bar.py"),
+        ("bar/bar-py-data.txt", "bar/bar-py-data.txt"),
         ("bar/Bar.java", "bar/Bar.java"),
         ("bar/bar-java-data.txt", "bar/bar-java-data.txt"),
     ]:
@@ -155,12 +151,11 @@
     self.AssertExitCode(exit_code, 0, stderr)
     bazel_bin = stdout[0]
 
-    exit_code, _, stderr = self.RunBazel(["build", "//bar:bar-java"])
+    exit_code, _, stderr = self.RunBazel(
+        ["build", "//bar:bar-py", "//bar:bar-java"])
     self.AssertExitCode(exit_code, 0, stderr)
 
-    # TODO(laszlocsomor): add Python after
-    # https://github.com/bazelbuild/bazel/issues/4878 is fixed.
-    for lang in [("java", "Java", "Bar.java")]:
+    for lang in [("py", "Python", "bar.py"), ("java", "Java", "Bar.java")]:
       if test_base.TestBase.IsWindows():
         bin_path = os.path.join(bazel_bin, "bar/bar-%s.exe" % lang[0])
       else:
diff --git a/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock b/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock
index 92098bc..b6d52f7 100644
--- a/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock
+++ b/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock
@@ -5,7 +5,7 @@
     srcs = ["bar.py"],
     data = ["bar-py-data.txt"],
     main = "bar.py",
-    deps = ["@bazel_tools//tools/runfiles:py-runfiles"],
+    deps = ["@bazel_tools//tools/python/runfiles"],
 )
 
 java_binary(
diff --git a/src/test/py/bazel/testdata/runfiles_test/bar/bar.py b/src/test/py/bazel/testdata/runfiles_test/bar/bar.py
index a88b10b..43d2152 100644
--- a/src/test/py/bazel/testdata/runfiles_test/bar/bar.py
+++ b/src/test/py/bazel/testdata/runfiles_test/bar/bar.py
@@ -14,7 +14,7 @@
 """Mock Python binary, only used in tests."""
 
 from __future__ import print_function
-from bazel_tools.tools.runfiles import runfiles
+from bazel_tools.tools.python.runfiles import runfiles
 
 print('Hello Python Bar!')
 r = runfiles.Create()
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock b/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
index 15a3a7d..04fc23e 100644
--- a/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
@@ -1,13 +1,13 @@
 py_binary(
     name = "runfiles-py",
-    srcs = ["runfiles.py"],
+    srcs = ["foo.py"],
     data = [
         "datadep/hello.txt",
         "//bar:bar-py",
         "//bar:bar-java",
     ],
-    main = "runfiles.py",
-    deps = ["@bazel_tools//tools/runfiles:py-runfiles"],
+    main = "foo.py",
+    deps = ["@bazel_tools//tools/python/runfiles"],
 )
 
 java_binary(
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py b/src/test/py/bazel/testdata/runfiles_test/foo/foo.py
similarity index 90%
rename from src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py
rename to src/test/py/bazel/testdata/runfiles_test/foo/foo.py
index 3210044..141c67f 100644
--- a/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/foo.py
@@ -18,7 +18,7 @@
 import os
 import subprocess
 
-from bazel_tools.tools.runfiles import runfiles
+from bazel_tools.tools.python.runfiles import runfiles
 
 
 def IsWindows():
@@ -65,8 +65,8 @@
       print(out[0])  # e.g. "Hello Python Bar!"
       print(out[1])  # e.g. "rloc=/tmp/foo_ws/bar/bar-py-data.txt"
     else:
-      raise Exception("ERROR: error running bar-%s: %s" % (lang,
-                                                           SplitToLines(err)))
+      raise Exception(
+          "ERROR: error running bar-%s: %s" % (lang, SplitToLines(err)))
 
 
 if __name__ == "__main__":
diff --git a/src/tools/runfiles/BUILD b/src/tools/runfiles/BUILD
index 0df3386..81e156d 100644
--- a/src/tools/runfiles/BUILD
+++ b/src/tools/runfiles/BUILD
@@ -22,24 +22,11 @@
         "BUILD.tools",
         "runfiles.cc",
         "runfiles.h",
-        "runfiles.py",
         "//src/tools/runfiles/java/com/google/devtools/build/runfiles:embedded_tools",
     ],
     visibility = ["//src:__pkg__"],
 )
 
-py_library(
-    name = "py-runfiles",
-    srcs = ["runfiles.py"],
-)
-
-py_test(
-    name = "py-runfiles-test",
-    srcs = ["runfiles_test.py"],
-    main = "runfiles_test.py",
-    deps = [":py-runfiles"],
-)
-
 cc_library(
     name = "cc-runfiles",
     srcs = ["runfiles.cc"],
diff --git a/tools/BUILD b/tools/BUILD
index 4488574..21a95fc 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -57,7 +57,7 @@
         "//tools/def_parser:srcs",
         "//tools/platforms:srcs",
         "//tools/objc:srcs",
-        "//tools/python:srcs",
+        "//tools/python:embedded_tools",
         "//tools/runfiles:embedded_tools",
         "//tools/test:srcs",
         "//tools/osx/crosstool:srcs",
diff --git a/tools/python/BUILD b/tools/python/BUILD
index 6933f9d..ab09128 100644
--- a/tools/python/BUILD
+++ b/tools/python/BUILD
@@ -6,10 +6,28 @@
 )
 
 filegroup(
-    name = "srcs",
+    name = "srcs_and_embedded_tools",
     srcs = [
         # Tools are build from the workspace for tests.
         "2to3.sh",
         "BUILD",
     ],
+    visibility = ["//visibility:private"],
+)
+
+filegroup(
+    name = "srcs",
+    srcs = [
+        ":srcs_and_embedded_tools",
+        "//tools/python/runfiles:srcs",
+    ],
+)
+
+filegroup(
+    name = "embedded_tools",
+    srcs = [
+        ":srcs_and_embedded_tools",
+        "//tools/python/runfiles:embedded_tools",
+    ],
+    visibility = ["//tools:__pkg__"],
 )
diff --git a/tools/python/runfiles/BUILD b/tools/python/runfiles/BUILD
new file mode 100644
index 0000000..79273e8
--- /dev/null
+++ b/tools/python/runfiles/BUILD
@@ -0,0 +1,35 @@
+package(default_visibility = ["//visibility:private"])
+
+filegroup(
+    name = "srcs",
+    srcs = glob(
+        ["**"],
+        exclude = [
+            ".*",
+            "*~",
+        ],
+    ),
+    visibility = ["//tools/python:__pkg__"],
+)
+
+filegroup(
+    name = "embedded_tools",
+    srcs = [
+        "BUILD.tools",
+        "runfiles.py",
+    ],
+    visibility = ["//tools/python:__pkg__"],
+)
+
+py_library(
+    name = "runfiles",
+    testonly = 1,
+    srcs = ["runfiles.py"],
+)
+
+py_test(
+    name = "runfiles_test",
+    srcs = ["runfiles_test.py"],
+    visibility = ["//visibility:public"],
+    deps = [":runfiles"],
+)
diff --git a/tools/python/runfiles/BUILD.tools b/tools/python/runfiles/BUILD.tools
new file mode 100644
index 0000000..4080d68
--- /dev/null
+++ b/tools/python/runfiles/BUILD.tools
@@ -0,0 +1,5 @@
+py_library(
+    name = "runfiles",
+    srcs = ["runfiles.py"],
+    visibility = ["//visibility:public"],
+)
diff --git a/src/tools/runfiles/runfiles.py b/tools/python/runfiles/runfiles.py
similarity index 98%
rename from src/tools/runfiles/runfiles.py
rename to tools/python/runfiles/runfiles.py
index e4938b7..17e4121 100644
--- a/src/tools/runfiles/runfiles.py
+++ b/tools/python/runfiles/runfiles.py
@@ -15,7 +15,7 @@
 
 Usage:
 
-from bazel_tools.tools.runfiles import runfiles
+from bazel_tools.tools.python.runfiles import runfiles
 
 r = runfiles.Create()
 with open(r.Rlocation("io_bazel/foo/bar.txt"), "r") as f:
@@ -35,7 +35,7 @@
 right environment variables for them:
 
   import subprocess
-  from bazel_tools.tools.runfiles import runfiles
+  from bazel_tools.tools.python.runfiles import runfiles
 
   r = runfiles.Create()
   env = {}
diff --git a/src/tools/runfiles/runfiles_test.py b/tools/python/runfiles/runfiles_test.py
similarity index 98%
rename from src/tools/runfiles/runfiles_test.py
rename to tools/python/runfiles/runfiles_test.py
index cfc3275..bbe1c84 100644
--- a/src/tools/runfiles/runfiles_test.py
+++ b/tools/python/runfiles/runfiles_test.py
@@ -17,7 +17,7 @@
 import tempfile
 import unittest
 
-from src.tools.runfiles import runfiles
+from tools.python.runfiles import runfiles
 
 
 class RunfilesTest(unittest.TestCase):