diff --git a/tensorflow/python/BUILD b/tensorflow/python/BUILD index 36c7283df85..69624080620 100644 --- a/tensorflow/python/BUILD +++ b/tensorflow/python/BUILD @@ -238,8 +238,8 @@ py_library( ":pywrap_tfe", ":util", "//tensorflow/core:protos_all_py", - "@absl_py//absl:app", "@absl_py//absl/flags", + "@rules_python//python/runfiles", "@six_archive//:six", ], ) diff --git a/tensorflow/python/framework/file_system_test.py b/tensorflow/python/framework/file_system_test.py index 8687bc5a785..546621f8471 100644 --- a/tensorflow/python/framework/file_system_test.py +++ b/tensorflow/python/framework/file_system_test.py @@ -18,8 +18,6 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import os - from tensorflow.python.framework import dtypes from tensorflow.python.framework import test_util from tensorflow.python.framework import load_library @@ -33,8 +31,8 @@ from tensorflow.python.util import compat class FileSystemTest(test.TestCase): def setUp(self): - file_system_library = os.path.join(resource_loader.get_data_files_path(), - "test_file_system.so") + file_system_library = resource_loader.get_path_to_datafile( + "test_file_system.so") load_library.load_file_system_library(file_system_library) @test_util.run_deprecated_v1 diff --git a/tensorflow/python/kernel_tests/decode_jpeg_op_test.py b/tensorflow/python/kernel_tests/decode_jpeg_op_test.py index 8f682614483..ebde47e8ec7 100644 --- a/tensorflow/python/kernel_tests/decode_jpeg_op_test.py +++ b/tensorflow/python/kernel_tests/decode_jpeg_op_test.py @@ -33,9 +33,6 @@ from tensorflow.python.ops import variables from tensorflow.python.platform import resource_loader from tensorflow.python.platform import test -prefix_path = resource_loader.get_path_to_datafile( - '../../core/lib/jpeg/testdata') - class DecodeJpegBenchmark(test.Benchmark): """Evaluate tensorflow DecodeJpegOp performance.""" @@ -66,7 +63,8 @@ class DecodeJpegBenchmark(test.Benchmark): """ ops.reset_default_graph() - image_file_path = os.path.join(prefix_path, image_name) + image_file_path = resource_loader.get_path_to_datafile( + os.path.join('core', 'lib', 'jpeg', 'testdata', image_name)) if tile is None: image_content = variable_scope.get_variable( diff --git a/tensorflow/python/lib/io/tf_record_test.py b/tensorflow/python/lib/io/tf_record_test.py index cefa5a49b76..0c71c898701 100644 --- a/tensorflow/python/lib/io/tf_record_test.py +++ b/tensorflow/python/lib/io/tf_record_test.py @@ -28,12 +28,9 @@ import six from tensorflow.python.framework import errors_impl from tensorflow.python.lib.io import tf_record -from tensorflow.python.platform import resource_loader from tensorflow.python.platform import test from tensorflow.python.util import compat -prefix_path = resource_loader.get_path_to_datafile("../../../core/lib") - TFRecordCompressionType = tf_record.TFRecordCompressionType # Edgar Allan Poe's 'Eldorado' diff --git a/tensorflow/python/platform/resource_loader.py b/tensorflow/python/platform/resource_loader.py index 8f4c5c190cc..c2eb4f803d1 100644 --- a/tensorflow/python/platform/resource_loader.py +++ b/tensorflow/python/platform/resource_loader.py @@ -23,6 +23,13 @@ import sys as _sys from tensorflow.python.util import tf_inspect as _inspect from tensorflow.python.util.tf_export import tf_export +# pylint: disable=g-import-not-at-top +try: + from rules_python.python.runfiles import runfiles +except ImportError: + runfiles = None +# pylint: enable=g-import-not-at-top + @tf_export(v1=['resource_loader.load_resource']) def load_resource(path): @@ -37,11 +44,7 @@ def load_resource(path): Raises: IOError: If the path is not found, or the resource can't be opened. """ - tensorflow_root = (_os.path.join( - _os.path.dirname(__file__), _os.pardir, _os.pardir)) - path = _os.path.join(tensorflow_root, path) - path = _os.path.abspath(path) - with open(path, 'rb') as f: + with open(get_path_to_datafile(path), 'rb') as f: return f.read() @@ -113,8 +116,18 @@ def get_path_to_datafile(path): Raises: IOError: If the path is not found, or the resource can't be opened. """ - data_files_path = _os.path.dirname(_inspect.getfile(_sys._getframe(1))) - return _os.path.join(data_files_path, path) + # First, try finding in the new path. + if runfiles: + r = runfiles.Create() + new_fpath = r.Rlocation( + _os.path.abspath(_os.path.join('tensorflow', path))) + if new_fpath is not None and _os.path.exists(new_fpath): + return new_fpath + + # Then, the old style path, as people became dependent on this buggy call. + old_filepath = _os.path.join( + _os.path.dirname(_inspect.getfile(_sys._getframe(1))), path) + return old_filepath @tf_export(v1=['resource_loader.readahead_file_path']) diff --git a/tensorflow/tools/api/tests/api_compatibility_test.py b/tensorflow/tools/api/tests/api_compatibility_test.py index 2b1065e16ae..fe71887fa67 100644 --- a/tensorflow/tools/api/tests/api_compatibility_test.py +++ b/tensorflow/tools/api/tests/api_compatibility_test.py @@ -79,8 +79,10 @@ _VERBOSE_DIFFS_HELP = """ false, only print which libraries have differences. """ -_API_GOLDEN_FOLDER_V1 = resource_loader.get_path_to_datafile('../golden/v1') -_API_GOLDEN_FOLDER_V2 = resource_loader.get_path_to_datafile('../golden/v2') +_API_GOLDEN_FOLDER_V1 = os.path.join( + resource_loader.get_data_files_path(), '..', 'golden', 'v1') +_API_GOLDEN_FOLDER_V2 = os.path.join( + resource_loader.get_data_files_path(), '..', 'golden', 'v2') _TEST_README_FILE = resource_loader.get_path_to_datafile('README.txt') _UPDATE_WARNING_FILE = resource_loader.get_path_to_datafile( 'API_UPDATE_WARNING.txt') diff --git a/tensorflow/workspace.bzl b/tensorflow/workspace.bzl index 5e6260c5137..950199dbc6e 100755 --- a/tensorflow/workspace.bzl +++ b/tensorflow/workspace.bzl @@ -904,6 +904,15 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""): ], ) + tf_http_archive( + name = "rules_python", + sha256 = "aa96a691d3a8177f3215b14b0edc9641787abaaa30363a080165d06ab65e1161", + urls = [ + "https://storage.googleapis.com/mirror.tensorflow.org/github.com/bazelbuild/rules_python/releases/download/0.0.1/rules_python-0.0.1.tar.gz", + "https://github.com/bazelbuild/rules_python/releases/download/0.0.1/rules_python-0.0.1.tar.gz", + ], + ) + tf_http_archive( name = "build_bazel_rules_android", sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",