From 42196a43019a55db06bd93050a7aaf1fb10d5150 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 26 Sep 2022 15:08:04 +0200 Subject: [PATCH] Add CurrentRepository() to Python runfiles library `runfiles.CurrentRepository()` can be used to get the canonical name of the Bazel repository containing the caller at runtime. This information is required to look up runfiles while taking repository mappings into account. --- src/test/py/bazel/py_test.py | 102 ++++++++++++++++++++ tools/python/gen_runfiles_constants.bzl | 30 ++++++ tools/python/runfiles/BUILD | 5 +- tools/python/runfiles/BUILD.tools | 10 +- tools/python/runfiles/runfiles.py | 48 +++++++++ tools/python/runfiles/runfiles_constants.py | 2 + tools/python/runfiles/runfiles_test.py | 3 + 7 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 tools/python/gen_runfiles_constants.bzl create mode 100644 tools/python/runfiles/runfiles_constants.py diff --git a/src/test/py/bazel/py_test.py b/src/test/py/bazel/py_test.py index c75923b3c5c29e..3fe96d26a688e2 100644 --- a/src/test/py/bazel/py_test.py +++ b/src/test/py/bazel/py_test.py @@ -207,6 +207,108 @@ def testPyTestWithStdlibCollisionRunsRemotely(self): self.AssertExitCode(exit_code, 0, stderr, stdout) self.assertIn('Test ran', stdout) +class PyRunfilesLibraryTest(test_base.TestBase): + def testPyRunfilesLibraryCurrentRepository(self): + self.CreateWorkspaceWithDefaultRepos('WORKSPACE', [ + 'local_repository(', + ' name = "other_repo",', + ' path = "other_repo_path",', + ')' + ]) + + self.ScratchFile('pkg/BUILD.bazel', [ + 'py_library(', + ' name = "library",', + ' srcs = ["library.py"],', + ' visibility = ["//visibility:public"],', + ' deps = ["@bazel_tools//tools/python/runfiles"],', + ')', + '', + 'py_binary(', + ' name = "binary",', + ' srcs = ["binary.py"],', + ' deps = [', + ' ":library",', + ' "@bazel_tools//tools/python/runfiles",' + ' ],', + ')', + '', + 'py_test(', + ' name = "test",', + ' srcs = ["test.py"],', + ' deps = [', + ' ":library",', + ' "@bazel_tools//tools/python/runfiles",', + ' ],', + ')', + ]) + self.ScratchFile('pkg/library.py', [ + 'from bazel_tools.tools.python.runfiles import runfiles', + 'def print_repo_name():', + ' print("in pkg/library.py: \'%s\'" % runfiles.CurrentRepository())', + ]) + self.ScratchFile('pkg/binary.py', [ + 'from bazel_tools.tools.python.runfiles import runfiles', + 'from pkg import library', + 'library.print_repo_name()', + 'print("in pkg/binary.py: \'%s\'" % runfiles.CurrentRepository())', + ]) + self.ScratchFile('pkg/test.py', [ + 'from bazel_tools.tools.python.runfiles import runfiles', + 'from pkg import library', + 'library.print_repo_name()', + 'print("in pkg/test.py: \'%s\'" % runfiles.CurrentRepository())', + ]) + + self.ScratchFile('other_repo_path/WORKSPACE') + self.ScratchFile('other_repo_path/pkg/BUILD.bazel', [ + 'py_binary(', + ' name = "binary",', + ' srcs = ["binary.py"],', + ' deps = [', + ' "@//pkg:library",', + ' "@bazel_tools//tools/python/runfiles",' + ' ],', + ')', + '', + 'py_test(', + ' name = "test",', + ' srcs = ["test.py"],', + ' deps = [', + ' "@//pkg:library",', + ' "@bazel_tools//tools/python/runfiles",', + ' ],', + ')', + ]) + self.ScratchFile('other_repo_path/pkg/binary.py', [ + 'from bazel_tools.tools.python.runfiles import runfiles', + 'from pkg import library', + 'library.print_repo_name()', + 'print("in external/other_repo/pkg/binary.py: \'%s\'" % runfiles.CurrentRepository())', + ]) + self.ScratchFile('other_repo_path/pkg/test.py', [ + 'from bazel_tools.tools.python.runfiles import runfiles', + 'from pkg import library', + 'library.print_repo_name()', + 'print("in external/other_repo/pkg/test.py: \'%s\'" % runfiles.CurrentRepository())', + ]) + + _, stdout, _ = self.RunBazel(['run', '//pkg:binary']) + self.assertIn('in pkg/binary.py: \'\'', stdout) + self.assertIn('in pkg/library.py: \'\'', stdout) + + _, stdout, _ = self.RunBazel(['test', '//pkg:test', '--test_output=streamed']) + self.assertIn('in pkg/test.py: \'\'', stdout) + self.assertIn('in pkg/library.py: \'\'', stdout) + + _, stdout, _ = self.RunBazel(['run', '@other_repo//pkg:binary']) + self.assertIn('in external/other_repo/pkg/binary.py: \'other_repo\'', stdout) + self.assertIn('in pkg/library.py: \'\'', stdout) + + _, stdout, _ = self.RunBazel(['test', '@other_repo//pkg:test', '--test_output=streamed']) + self.assertIn('in external/other_repo/pkg/test.py: \'other_repo\'', stdout) + self.assertIn('in pkg/library.py: \'\'', stdout) + if __name__ == '__main__': unittest.main() diff --git a/tools/python/gen_runfiles_constants.bzl b/tools/python/gen_runfiles_constants.bzl new file mode 100644 index 00000000000000..8ed5b4fa07a43c --- /dev/null +++ b/tools/python/gen_runfiles_constants.bzl @@ -0,0 +1,30 @@ +# Copyright 2022 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +_RUNFILES_CONSTANTS_TEMPLATE = """# The name of the runfiles directory corresponding to the main repository. +MAIN_REPOSITORY_RUNFILES_DIRECTORY = '%s' +""" + +def _gen_runfiles_constants_impl(ctx): + out = ctx.actions.declare_file(ctx.attr.name + ".py") + ctx.actions.write(out, _RUNFILES_CONSTANTS_TEMPLATE % ctx.workspace_name) + + return DefaultInfo( + files = depset([out]), + runfiles = ctx.runfiles([out]), + ) + +gen_runfiles_constants = rule( + implementation = _gen_runfiles_constants_impl, +) diff --git a/tools/python/runfiles/BUILD b/tools/python/runfiles/BUILD index 21a88a9174c320..151be7da53d684 100644 --- a/tools/python/runfiles/BUILD +++ b/tools/python/runfiles/BUILD @@ -20,7 +20,10 @@ filegroup( py_library( name = "runfiles", testonly = 1, - srcs = ["runfiles.py"], + srcs = [ + "runfiles.py", + "runfiles_constants.py", + ], ) py_test( diff --git a/tools/python/runfiles/BUILD.tools b/tools/python/runfiles/BUILD.tools index 3bfe889f34fc3f..e355338e1b517e 100644 --- a/tools/python/runfiles/BUILD.tools +++ b/tools/python/runfiles/BUILD.tools @@ -1,7 +1,15 @@ +load("//tools/python:gen_runfiles_constants.bzl", "gen_runfiles_constants") load("//tools/python:private/defs.bzl", "py_library") py_library( name = "runfiles", - srcs = ["runfiles.py"], + srcs = [ + "runfiles.py", + ":runfiles_constants", + ], visibility = ["//visibility:public"], ) + +gen_runfiles_constants( + name = "runfiles_constants", +) diff --git a/tools/python/runfiles/runfiles.py b/tools/python/runfiles/runfiles.py index 03cff1c27ee386..62949d22bb11a2 100644 --- a/tools/python/runfiles/runfiles.py +++ b/tools/python/runfiles/runfiles.py @@ -58,9 +58,51 @@ p = subprocess.Popen([r.Rlocation("path/to/binary")], env, ...) """ +import inspect import os import posixpath +import sys +from .runfiles_constants import MAIN_REPOSITORY_RUNFILES_DIRECTORY + +def _FindPythonRunfilesRoot(): + python_runfiles_root = __file__ + # Walk up our own runfiles path to the root of the runfiles tree from which + # the current file is being run. + for _ in range("bazel_tools/tools/python/runfiles/runfiles.py".count("/") + 1): + python_runfiles_root = os.path.dirname(python_runfiles_root) + return python_runfiles_root + + +_PYTHON_RUNFILES_ROOT = _FindPythonRunfilesRoot() + +def CurrentRepository(frame = 0): + """Returns the canonical name of the caller's Bazel repository. + + Args: + frame: int; the stack frame to return the repository name for (defaults to + 0, the caller of this function) + Returns: + the canonical name of the Bazel repository containing the Python file that + calls this function + Raises: + ValueError: if the caller cannot be determined or the caller's file path + is not contained in the Python runfiles tree + """ + caller_path = inspect.getframeinfo(sys._getframe(frame + 1), 1).filename + caller_runfiles_path = os.path.relpath(caller_path, _PYTHON_RUNFILES_ROOT) + # Converts all path separators to os.path.sep. + caller_runfiles_path = os.path.normpath(caller_runfiles_path) + if caller_runfiles_path.startswith(".." + os.path.sep): + raise ValueError('{} does not lie under the runfiles root {}'.format(caller_path, _PYTHON_RUNFILES_ROOT)) + + caller_runfiles_directory = caller_runfiles_path[:caller_runfiles_path.find(os.path.sep)] + if caller_runfiles_directory == MAIN_REPOSITORY_RUNFILES_DIRECTORY: + # The canonical name of the main repository is the empty string. + return '' + # For all other repositories, the name of the runfiles directory is the + # canonical name. + return caller_runfiles_directory def CreateManifestBased(manifest_path): return _Runfiles(_ManifestBased(manifest_path)) @@ -114,6 +156,12 @@ class _Runfiles(object): def __init__(self, strategy): self._strategy = strategy + python_runfiles_root = __file__ + # Walk up our own runfiles path to the root of the runfiles tree from which + # the current file is being run. + for _ in range("bazel_tools/tools/python/runfiles/runfiles.py".count("/") + 1): + python_runfiles_root = os.path.dirname(python_runfiles_root) + self._python_runfiles_root = python_runfiles_root def Rlocation(self, path): """Returns the runtime path of a runfile. diff --git a/tools/python/runfiles/runfiles_constants.py b/tools/python/runfiles/runfiles_constants.py new file mode 100644 index 00000000000000..b86c9e7aa3a4f5 --- /dev/null +++ b/tools/python/runfiles/runfiles_constants.py @@ -0,0 +1,2 @@ +# Only used by tests. +MAIN_REPOSITORY_RUNFILES_DIRECTORY = "io_bazel" diff --git a/tools/python/runfiles/runfiles_test.py b/tools/python/runfiles/runfiles_test.py index 70168cbb6107e5..b581738085cc4f 100644 --- a/tools/python/runfiles/runfiles_test.py +++ b/tools/python/runfiles/runfiles_test.py @@ -262,6 +262,9 @@ def testPathsFromEnvvars(self): self.assertEqual(mf, "") self.assertEqual(dr, "") + def testCurrentRepository(self): + self.assertEqual(runfiles.CurrentRepository(), "") + @staticmethod def IsWindows(): return os.name == "nt"