Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/platform/resource_loader.py: 50%

44 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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.""" 

16import os as _os 

17import sys as _sys 

18 

19from tensorflow.python.util import tf_inspect as _inspect 

20from tensorflow.python.util.tf_export import tf_export 

21 

22# pylint: disable=g-import-not-at-top 

23try: 

24 from rules_python.python.runfiles import runfiles 

25except ImportError: 

26 runfiles = None 

27# pylint: enable=g-import-not-at-top 

28 

29 

30@tf_export(v1=['resource_loader.load_resource']) 

31def load_resource(path): 

32 """Load the resource at given path, where path is relative to tensorflow/. 

33 

34 Args: 

35 path: a string resource path relative to tensorflow/. 

36 

37 Returns: 

38 The contents of that resource. 

39 

40 Raises: 

41 IOError: If the path is not found, or the resource can't be opened. 

42 """ 

43 with open(get_path_to_datafile(path), 'rb') as f: 

44 return f.read() 

45 

46 

47# pylint: disable=protected-access 

48@tf_export(v1=['resource_loader.get_data_files_path']) 

49def get_data_files_path(): 

50 """Get a direct path to the data files colocated with the script. 

51 

52 Returns: 

53 The directory where files specified in data attribute of py_test 

54 and py_binary are stored. 

55 """ 

56 return _os.path.dirname(_inspect.getfile(_sys._getframe(1))) 

57 

58 

59@tf_export(v1=['resource_loader.get_root_dir_with_all_resources']) 

60def get_root_dir_with_all_resources(): 

61 """Get a root directory containing all the data attributes in the build rule. 

62 

63 Returns: 

64 The path to the specified file present in the data attribute of py_test 

65 or py_binary. Falls back to returning the same as get_data_files_path if it 

66 fails to detect a bazel runfiles directory. 

67 """ 

68 script_dir = get_data_files_path() 

69 

70 # Create a history of the paths, because the data files are located relative 

71 # to the repository root directory, which is directly under runfiles 

72 # directory. 

73 directories = [script_dir] 

74 data_files_dir = '' 

75 

76 while True: 

77 candidate_dir = directories[-1] 

78 current_directory = _os.path.basename(candidate_dir) 

79 if '.runfiles' in current_directory: 

80 # Our file should never be directly under runfiles. 

81 # If the history has only one item, it means we are directly inside the 

82 # runfiles directory, something is wrong, fall back to the default return 

83 # value, script directory. 

84 if len(directories) > 1: 

85 data_files_dir = directories[-2] 

86 

87 break 

88 else: 

89 new_candidate_dir = _os.path.dirname(candidate_dir) 

90 # If we are at the root directory these two will be the same. 

91 if new_candidate_dir == candidate_dir: 

92 break 

93 else: 

94 directories.append(new_candidate_dir) 

95 

96 return data_files_dir or script_dir 

97 

98 

99@tf_export(v1=['resource_loader.get_path_to_datafile']) 

100def get_path_to_datafile(path): 

101 """Get the path to the specified file in the data dependencies. 

102 

103 The path is relative to tensorflow/ 

104 

105 Args: 

106 path: a string resource path relative to tensorflow/ 

107 

108 Returns: 

109 The path to the specified file present in the data attribute of py_test 

110 or py_binary. 

111 

112 Raises: 

113 IOError: If the path is not found, or the resource can't be opened. 

114 """ 

115 # First, try finding in the new path. 

116 if runfiles: 

117 r = runfiles.Create() 

118 new_fpath = r.Rlocation( 

119 _os.path.abspath(_os.path.join('tensorflow', path))) 

120 if new_fpath is not None and _os.path.exists(new_fpath): 

121 return new_fpath 

122 

123 # Then, the old style path, as people became dependent on this buggy call. 

124 old_filepath = _os.path.join( 

125 _os.path.dirname(_inspect.getfile(_sys._getframe(1))), path) 

126 return old_filepath 

127 

128 

129@tf_export(v1=['resource_loader.readahead_file_path']) 

130def readahead_file_path(path, readahead='128M'): # pylint: disable=unused-argument 

131 """Readahead files not implemented; simply returns given path.""" 

132 return path