• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Resource management library."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import os as _os
21import sys as _sys
22
23from tensorflow.python.util import tf_inspect as _inspect
24from tensorflow.python.util.tf_export import tf_export
25
26# pylint: disable=g-import-not-at-top
27try:
28  from rules_python.python.runfiles import runfiles
29except ImportError:
30  runfiles = None
31# pylint: enable=g-import-not-at-top
32
33
34@tf_export(v1=['resource_loader.load_resource'])
35def load_resource(path):
36  """Load the resource at given path, where path is relative to tensorflow/.
37
38  Args:
39    path: a string resource path relative to tensorflow/.
40
41  Returns:
42    The contents of that resource.
43
44  Raises:
45    IOError: If the path is not found, or the resource can't be opened.
46  """
47  with open(get_path_to_datafile(path), 'rb') as f:
48    return f.read()
49
50
51# pylint: disable=protected-access
52@tf_export(v1=['resource_loader.get_data_files_path'])
53def get_data_files_path():
54  """Get a direct path to the data files colocated with the script.
55
56  Returns:
57    The directory where files specified in data attribute of py_test
58    and py_binary are stored.
59  """
60  return _os.path.dirname(_inspect.getfile(_sys._getframe(1)))
61
62
63@tf_export(v1=['resource_loader.get_root_dir_with_all_resources'])
64def get_root_dir_with_all_resources():
65  """Get a root directory containing all the data attributes in the build rule.
66
67  Returns:
68    The path to the specified file present in the data attribute of py_test
69    or py_binary. Falls back to returning the same as get_data_files_path if it
70    fails to detect a bazel runfiles directory.
71  """
72  script_dir = get_data_files_path()
73
74  # Create a history of the paths, because the data files are located relative
75  # to the repository root directory, which is directly under runfiles
76  # directory.
77  directories = [script_dir]
78  data_files_dir = ''
79
80  while True:
81    candidate_dir = directories[-1]
82    current_directory = _os.path.basename(candidate_dir)
83    if '.runfiles' in current_directory:
84      # Our file should never be directly under runfiles.
85      # If the history has only one item, it means we are directly inside the
86      # runfiles directory, something is wrong, fall back to the default return
87      # value, script directory.
88      if len(directories) > 1:
89        data_files_dir = directories[-2]
90
91      break
92    else:
93      new_candidate_dir = _os.path.dirname(candidate_dir)
94      # If we are at the root directory these two will be the same.
95      if new_candidate_dir == candidate_dir:
96        break
97      else:
98        directories.append(new_candidate_dir)
99
100  return data_files_dir or script_dir
101
102
103@tf_export(v1=['resource_loader.get_path_to_datafile'])
104def get_path_to_datafile(path):
105  """Get the path to the specified file in the data dependencies.
106
107  The path is relative to tensorflow/
108
109  Args:
110    path: a string resource path relative to tensorflow/
111
112  Returns:
113    The path to the specified file present in the data attribute of py_test
114    or py_binary.
115
116  Raises:
117    IOError: If the path is not found, or the resource can't be opened.
118  """
119  # First, try finding in the new path.
120  if runfiles:
121    r = runfiles.Create()
122    new_fpath = r.Rlocation(
123        _os.path.abspath(_os.path.join('tensorflow', path)))
124    if new_fpath is not None and _os.path.exists(new_fpath):
125      return new_fpath
126
127  # Then, the old style path, as people became dependent on this buggy call.
128  old_filepath = _os.path.join(
129      _os.path.dirname(_inspect.getfile(_sys._getframe(1))), path)
130  return old_filepath
131
132
133@tf_export(v1=['resource_loader.readahead_file_path'])
134def readahead_file_path(path, readahead='128M'):  # pylint: disable=unused-argument
135  """Readahead files not implemented; simply returns given path."""
136  return path
137