aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2018-02-15 02:59:13 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-02-15 03:00:36 -0800
commitbb1d0856fffd1c57ed1fd74f3b3c3287f6b66f47 (patch)
tree212abc71b053377f10c221e1695819cde19df6e3 /src/tools
parent3edf41b70de9bb1a8702d0342beeb2ad13898d71 (diff)
runfiles library: py_binary can run java_binary
Add "JAVA_RUNFILES" (and "RUNFILES_DIR") to the dict that Runfiles.EnvVars() returns in the Python runfiles library, so Python programs that use the Python runfiles library in @bazel_tools can now run java_binary data-dependencies as subprocesses and the latter will find the runfiles. See https://github.com/bazelbuild/bazel/issues/4460 Change-Id: I0566f6d3c68631a1d99e67964fbe8019ee82324b PiperOrigin-RevId: 185812948
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/runfiles/runfiles.py24
-rw-r--r--src/tools/runfiles/runfiles_test.py67
2 files changed, 79 insertions, 12 deletions
diff --git a/src/tools/runfiles/runfiles.py b/src/tools/runfiles/runfiles.py
index 8b00539a00..3e062034fb 100644
--- a/src/tools/runfiles/runfiles.py
+++ b/src/tools/runfiles/runfiles.py
@@ -175,8 +175,23 @@ class _ManifestBased(object):
result[tokens[0]] = tokens[1]
return result
+ def _GetRunfilesDir(self):
+ if self._path.endswith("/MANIFEST") or self._path.endswith("\\MANIFEST"):
+ return self._path[:-len("/MANIFEST")]
+ elif self._path.endswith(".runfiles_manifest"):
+ return self._path[:-len("_manifest")]
+ else:
+ return ""
+
def EnvVars(self):
- return {"RUNFILES_MANIFEST_FILE": self._path}
+ directory = self._GetRunfilesDir()
+ return {
+ "RUNFILES_MANIFEST_FILE": self._path,
+ "RUNFILES_DIR": directory,
+ # TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
+ # pick up RUNFILES_DIR.
+ "JAVA_RUNFILES": directory,
+ }
class _DirectoryBased(object):
@@ -196,4 +211,9 @@ class _DirectoryBased(object):
return posixpath.join(self._runfiles_root, path)
def EnvVars(self):
- return {"RUNFILES_DIR": self._runfiles_root}
+ return {
+ "RUNFILES_DIR": self._runfiles_root,
+ # TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
+ # pick up RUNFILES_DIR.
+ "JAVA_RUNFILES": self._runfiles_root,
+ }
diff --git a/src/tools/runfiles/runfiles_test.py b/src/tools/runfiles/runfiles_test.py
index eb022a675c..e4715568eb 100644
--- a/src/tools/runfiles/runfiles_test.py
+++ b/src/tools/runfiles/runfiles_test.py
@@ -42,7 +42,7 @@ class RunfilesTest(unittest.TestCase):
lambda: r.Rlocation("/foo"))
def testCreatesManifestBasedRunfiles(self):
- with _MockFile(["a/b c/d"]) as mf:
+ with _MockFile(contents=["a/b c/d"]) as mf:
r = runfiles.Create({
"RUNFILES_MANIFEST_FILE": mf.Path(),
"RUNFILES_DIR": "ignored when RUNFILES_MANIFEST_FILE has a value",
@@ -50,7 +50,46 @@ class RunfilesTest(unittest.TestCase):
})
self.assertEqual(r.Rlocation("a/b"), "c/d")
self.assertIsNone(r.Rlocation("foo"))
- self.assertDictEqual(r.EnvVars(), {"RUNFILES_MANIFEST_FILE": mf.Path()})
+
+ def testManifestBasedRunfilesEnvVars(self):
+ with _MockFile(name="MANIFEST") as mf:
+ r = runfiles.Create({
+ "RUNFILES_MANIFEST_FILE": mf.Path(),
+ "TEST_SRCDIR": "always ignored",
+ })
+ self.assertDictEqual(
+ r.EnvVars(), {
+ "RUNFILES_MANIFEST_FILE": mf.Path(),
+ "RUNFILES_DIR": mf.Path()[:-len("/MANIFEST")],
+ "JAVA_RUNFILES": mf.Path()[:-len("/MANIFEST")],
+ })
+
+ with _MockFile(name="foo.runfiles_manifest") as mf:
+ r = runfiles.Create({
+ "RUNFILES_MANIFEST_FILE": mf.Path(),
+ "TEST_SRCDIR": "always ignored",
+ })
+ self.assertDictEqual(
+ r.EnvVars(), {
+ "RUNFILES_MANIFEST_FILE":
+ mf.Path(),
+ "RUNFILES_DIR": (
+ mf.Path()[:-len("foo.runfiles_manifest")] + "foo.runfiles"),
+ "JAVA_RUNFILES": (
+ mf.Path()[:-len("foo.runfiles_manifest")] + "foo.runfiles"),
+ })
+
+ with _MockFile(name="x_manifest") as mf:
+ r = runfiles.Create({
+ "RUNFILES_MANIFEST_FILE": mf.Path(),
+ "TEST_SRCDIR": "always ignored",
+ })
+ self.assertDictEqual(
+ r.EnvVars(), {
+ "RUNFILES_MANIFEST_FILE": mf.Path(),
+ "RUNFILES_DIR": "",
+ "JAVA_RUNFILES": "",
+ })
def testCreatesDirectoryBasedRunfiles(self):
r = runfiles.Create({
@@ -59,7 +98,16 @@ class RunfilesTest(unittest.TestCase):
})
self.assertEqual(r.Rlocation("a/b"), "runfiles/dir/a/b")
self.assertEqual(r.Rlocation("foo"), "runfiles/dir/foo")
- self.assertDictEqual(r.EnvVars(), {"RUNFILES_DIR": "runfiles/dir"})
+
+ def testDirectoryBasedRunfilesEnvVars(self):
+ r = runfiles.Create({
+ "RUNFILES_DIR": "runfiles/dir",
+ "TEST_SRCDIR": "always ignored",
+ })
+ self.assertDictEqual(r.EnvVars(), {
+ "RUNFILES_DIR": "runfiles/dir",
+ "JAVA_RUNFILES": "runfiles/dir",
+ })
def testFailsToCreateManifestBasedBecauseManifestDoesNotExist(self):
@@ -69,7 +117,7 @@ class RunfilesTest(unittest.TestCase):
self.assertRaisesRegexp(IOError, "non-existing path", _Run)
def testFailsToCreateAnyRunfilesBecauseEnvvarsAreNotDefined(self):
- with _MockFile(["a b"]) as mf:
+ with _MockFile(contents=["a b"]) as mf:
runfiles.Create({
"RUNFILES_MANIFEST_FILE": mf.Path(),
"RUNFILES_DIR": "whatever",
@@ -83,7 +131,7 @@ class RunfilesTest(unittest.TestCase):
self.assertIsNone(runfiles.Create({"FOO": "bar"}))
def testManifestBasedRlocation(self):
- with _MockFile([
+ with _MockFile(contents=[
"Foo/runfile1", "Foo/runfile2 C:/Actual Path\\runfile2",
"Foo/Bar/runfile3 D:\\the path\\run file 3.txt"
]) as mf:
@@ -93,7 +141,6 @@ class RunfilesTest(unittest.TestCase):
self.assertEqual(
r.Rlocation("Foo/Bar/runfile3"), "D:\\the path\\run file 3.txt")
self.assertIsNone(r.Rlocation("unknown"))
- self.assertDictEqual(r.EnvVars(), {"RUNFILES_MANIFEST_FILE": mf.Path()})
def testDirectoryBasedRlocation(self):
# The _DirectoryBased strategy simply joins the runfiles directory and the
@@ -101,7 +148,6 @@ class RunfilesTest(unittest.TestCase):
# nor does it check that the path exists.
r = runfiles.CreateDirectoryBased("foo/bar baz//qux/")
self.assertEqual(r.Rlocation("arg"), "foo/bar baz//qux/arg")
- self.assertDictEqual(r.EnvVars(), {"RUNFILES_DIR": "foo/bar baz//qux/"})
@staticmethod
def IsWindows():
@@ -110,13 +156,14 @@ class RunfilesTest(unittest.TestCase):
class _MockFile(object):
- def __init__(self, contents):
- self._contents = contents
+ def __init__(self, name=None, contents=None):
+ self._contents = contents or []
+ self._name = name or "x"
self._path = None
def __enter__(self):
tmpdir = os.environ.get("TEST_TMPDIR")
- self._path = os.path.join(tempfile.mkdtemp(dir=tmpdir), "x")
+ self._path = os.path.join(tempfile.mkdtemp(dir=tmpdir), self._name)
with open(self._path, "wt") as f:
f.writelines(l + "\n" for l in self._contents)
return self