Skip to content

Commit

Permalink
Change resource_loader to use bazel's python tools.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 294051693
Change-Id: I8a6c618be59e9f774e6b854ca9635cfac4a81cd0
  • Loading branch information
gunan authored and tensorflower-gardener committed Feb 9, 2020
1 parent d959e4b commit b818cbe
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion tensorflow/python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand Down
6 changes: 2 additions & 4 deletions tensorflow/python/framework/file_system_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
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
Expand All @@ -33,8 +31,8 @@
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
Expand Down
6 changes: 2 additions & 4 deletions tensorflow/python/kernel_tests/decode_jpeg_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
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."""
Expand Down Expand Up @@ -66,7 +63,8 @@ def _evalDecodeJpeg(self,
"""
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(
Expand Down
3 changes: 0 additions & 3 deletions tensorflow/python/lib/io/tf_record_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@

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'
Expand Down
21 changes: 14 additions & 7 deletions tensorflow/python/platform/resource_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import os as _os
import sys as _sys

from rules_python.python.runfiles import runfiles

from tensorflow.python.util import tf_inspect as _inspect
from tensorflow.python.util.tf_export import tf_export

Expand All @@ -37,11 +39,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()


Expand Down Expand Up @@ -113,8 +111,17 @@ 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.
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'])
Expand Down
6 changes: 4 additions & 2 deletions tensorflow/tools/api/tests/api_compatibility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@
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')
Expand Down
9 changes: 9 additions & 0 deletions tensorflow/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b818cbe

Please sign in to comment.