Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22import copy 

23import unittest 

24import os.path 

25import posixpath 

26import pickle 

27 

28from lsst.daf.butler import LocationFactory, ButlerURI 

29from lsst.daf.butler.core._butlerUri import os2posix, posix2os 

30 

31 

32class LocationTestCase(unittest.TestCase): 

33 """Tests for Location within datastore 

34 """ 

35 

36 def testButlerUri(self): 

37 """Tests whether ButlerURI instantiates correctly given different 

38 arguments. 

39 """ 

40 # Root to use for relative paths 

41 testRoot = "/tmp/" 

42 

43 # uriStrings is a list of tuples containing test string, forceAbsolute, 

44 # forceDirectory as arguments to ButlerURI and scheme, netloc and path 

45 # as expected attributes. Test asserts constructed equals to expected. 

46 # 1) no determinable schemes (ensures schema and netloc are not set) 

47 osRelFilePath = os.path.join(testRoot, "relative/file.ext") 

48 uriStrings = [ 

49 ("relative/file.ext", True, False, "", "", osRelFilePath), 

50 ("relative/file.ext", False, False, "", "", "relative/file.ext"), 

51 ("test/../relative/file.ext", True, False, "", "", osRelFilePath), 

52 ("test/../relative/file.ext", False, False, "", "", "relative/file.ext"), 

53 ("relative/dir", False, True, "", "", "relative/dir/") 

54 ] 

55 # 2) implicit file scheme, tests absolute file and directory paths 

56 uriStrings.extend(( 

57 ("/rootDir/absolute/file.ext", True, False, "file", "", '/rootDir/absolute/file.ext'), 

58 ("~/relative/file.ext", True, False, "file", "", os.path.expanduser("~/relative/file.ext")), 

59 ("~/relative/file.ext", False, False, "file", "", os.path.expanduser("~/relative/file.ext")), 

60 ("/rootDir/absolute/", True, False, "file", "", "/rootDir/absolute/"), 

61 ("/rootDir/absolute", True, True, "file", "", "/rootDir/absolute/"), 

62 ("~/rootDir/absolute", True, True, "file", "", os.path.expanduser("~/rootDir/absolute/")) 

63 )) 

64 # 3) explicit file scheme, absolute and relative file and directory URI 

65 posixRelFilePath = posixpath.join(testRoot, "relative/file.ext") 

66 uriStrings.extend(( 

67 ("file:///rootDir/absolute/file.ext", True, False, "file", "", "/rootDir/absolute/file.ext"), 

68 ("file:relative/file.ext", True, False, "file", "", posixRelFilePath), 

69 ("file:///absolute/directory/", True, False, "file", "", "/absolute/directory/"), 

70 ("file:///absolute/directory", True, True, "file", "", "/absolute/directory/") 

71 )) 

72 # 4) S3 scheme (ensured Keys as dirs and fully specified URIs work) 

73 uriStrings.extend(( 

74 ("s3://bucketname/rootDir/", True, False, "s3", "bucketname", "/rootDir/"), 

75 ("s3://bucketname/rootDir", True, True, "s3", "bucketname", "/rootDir/"), 

76 ("s3://bucketname/rootDir/relative/file.ext", True, False, "s3", 

77 "bucketname", "/rootDir/relative/file.ext") 

78 )) 

79 # 5) HTTPS scheme 

80 uriStrings.extend(( 

81 ("https://www.lsst.org/rootDir/", True, False, "https", "www.lsst.org", "/rootDir/"), 

82 ("https://www.lsst.org/rootDir", True, True, "https", "www.lsst.org", "/rootDir/"), 

83 ("https://www.lsst.org/rootDir/relative/file.ext", True, False, "https", 

84 "www.lsst.org", "/rootDir/relative/file.ext") 

85 )) 

86 

87 for uriInfo in uriStrings: 

88 uri = ButlerURI(uriInfo[0], root=testRoot, forceAbsolute=uriInfo[1], 

89 forceDirectory=uriInfo[2]) 

90 with self.subTest(uri=uriInfo[0]): 

91 self.assertEqual(uri.scheme, uriInfo[3], "test scheme") 

92 self.assertEqual(uri.netloc, uriInfo[4], "test netloc") 

93 self.assertEqual(uri.path, uriInfo[5], "test path") 

94 

95 # test root becomes abspath(".") when not specified, note specific 

96 # file:// scheme case 

97 uriStrings = ( 

98 ("file://relative/file.ext", True, False, "file", "relative", "/file.ext"), 

99 ("file:relative/file.ext", False, False, "file", "", os.path.abspath("relative/file.ext")), 

100 ("file:relative/dir/", True, True, "file", "", os.path.abspath("relative/dir")+"/"), 

101 ("relative/file.ext", True, False, "", "", os.path.abspath("relative/file.ext")) 

102 ) 

103 

104 for uriInfo in uriStrings: 

105 uri = ButlerURI(uriInfo[0], forceAbsolute=uriInfo[1], forceDirectory=uriInfo[2]) 

106 with self.subTest(uri=uriInfo[0]): 

107 self.assertEqual(uri.scheme, uriInfo[3], "test scheme") 

108 self.assertEqual(uri.netloc, uriInfo[4], "test netloc") 

109 self.assertEqual(uri.path, uriInfo[5], "test path") 

110 

111 # File replacement 

112 uriStrings = ( 

113 ("relative/file.ext", "newfile.fits", "relative/newfile.fits"), 

114 ("relative/", "newfile.fits", "relative/newfile.fits"), 

115 ("https://www.lsst.org/butler/", "butler.yaml", "/butler/butler.yaml"), 

116 ("s3://amazon/datastore/", "butler.yaml", "/datastore/butler.yaml"), 

117 ("s3://amazon/datastore/mybutler.yaml", "butler.yaml", "/datastore/butler.yaml") 

118 ) 

119 

120 for uriInfo in uriStrings: 

121 uri = ButlerURI(uriInfo[0], forceAbsolute=False) 

122 uri.updateFile(uriInfo[1]) 

123 with self.subTest(uri=uriInfo[0]): 

124 self.assertEqual(uri.path, uriInfo[2]) 

125 

126 # Check that schemeless can become file scheme 

127 schemeless = ButlerURI("relative/path.ext") 

128 filescheme = ButlerURI("/absolute/path.ext") 

129 self.assertFalse(schemeless.scheme) 

130 self.assertEqual(filescheme.scheme, "file") 

131 self.assertNotEqual(type(schemeless), type(filescheme)) 

132 

133 # Copy constructor 

134 uri = ButlerURI("s3://amazon/datastore", forceDirectory=True) 

135 uri2 = ButlerURI(uri) 

136 self.assertEqual(uri, uri2) 

137 uri = ButlerURI("file://amazon/datastore/file.txt") 

138 uri2 = ButlerURI(uri) 

139 self.assertEqual(uri, uri2) 

140 

141 # Copy constructor using subclass 

142 uri3 = type(uri)(uri) 

143 self.assertEqual(type(uri), type(uri3)) 

144 

145 # Explicit copy 

146 uri4 = copy.copy(uri3) 

147 self.assertEqual(uri4, uri3) 

148 uri4 = copy.deepcopy(uri3) 

149 self.assertEqual(uri4, uri3) 

150 

151 def testUriJoin(self): 

152 uri = ButlerURI("a/b/c/d", forceDirectory=True, forceAbsolute=False) 

153 uri2 = uri.join("e/f/g.txt") 

154 self.assertEqual(str(uri2), "a/b/c/d/e/f/g.txt", f"Checking joined URI {uri} -> {uri2}") 

155 

156 uri = ButlerURI("a/b/c/d/old.txt", forceAbsolute=False) 

157 uri2 = uri.join("e/f/g.txt") 

158 self.assertEqual(str(uri2), "a/b/c/d/e/f/g.txt", f"Checking joined URI {uri} -> {uri2}") 

159 

160 uri = ButlerURI("a/b/c/d", forceDirectory=True, forceAbsolute=True) 

161 uri2 = uri.join("e/f/g.txt") 

162 self.assertTrue(str(uri2).endswith("a/b/c/d/e/f/g.txt"), f"Checking joined URI {uri} -> {uri2}") 

163 

164 uri = ButlerURI("s3://bucket/a/b/c/d", forceDirectory=True) 

165 uri2 = uri.join("newpath/newfile.txt") 

166 self.assertEqual(str(uri2), "s3://bucket/a/b/c/d/newpath/newfile.txt") 

167 

168 uri = ButlerURI("s3://bucket/a/b/c/d/old.txt") 

169 uri2 = uri.join("newpath/newfile.txt") 

170 self.assertEqual(str(uri2), "s3://bucket/a/b/c/d/newpath/newfile.txt") 

171 

172 def testButlerUriSerialization(self): 

173 """Test that we can pickle and yaml""" 

174 uri = ButlerURI("a/b/c/d") 

175 uri2 = pickle.loads(pickle.dumps(uri)) 

176 self.assertEqual(uri, uri2) 

177 self.assertFalse(uri2.dirLike) 

178 

179 uri = ButlerURI("a/b/c/d", forceDirectory=True) 

180 uri2 = pickle.loads(pickle.dumps(uri)) 

181 self.assertEqual(uri, uri2) 

182 self.assertTrue(uri2.dirLike) 

183 

184 def testFileLocation(self): 

185 root = os.path.abspath(os.path.curdir) 

186 factory = LocationFactory(root) 

187 print(f"Factory created: {factory}") 

188 

189 pathInStore = "relative/path/file.ext" 

190 loc1 = factory.fromPath(pathInStore) 

191 

192 self.assertEqual(loc1.path, os.path.join(root, pathInStore)) 

193 self.assertEqual(loc1.pathInStore, pathInStore) 

194 self.assertTrue(loc1.uri.geturl().startswith("file:///")) 

195 self.assertTrue(loc1.uri.geturl().endswith("file.ext")) 

196 loc1.updateExtension("fits") 

197 self.assertTrue(loc1.uri.geturl().endswith("file.fits"), 

198 f"Checking 'fits' extension in {loc1.uri}") 

199 loc1.updateExtension("fits.gz") 

200 self.assertEqual(loc1.uri.basename(), "file.fits.gz") 

201 self.assertTrue(loc1.uri.geturl().endswith("file.fits.gz"), 

202 f"Checking 'fits.gz' extension in {loc1.uri}") 

203 self.assertEqual(loc1.getExtension(), ".fits.gz") 

204 loc1.updateExtension(".jpeg") 

205 self.assertTrue(loc1.uri.geturl().endswith("file.jpeg"), 

206 f"Checking 'jpeg' extension in {loc1.uri}") 

207 loc1.updateExtension(None) 

208 self.assertTrue(loc1.uri.geturl().endswith("file.jpeg"), 

209 f"Checking unchanged extension in {loc1.uri}") 

210 loc1.updateExtension("") 

211 self.assertTrue(loc1.uri.geturl().endswith("file"), f"Checking no extension in {loc1.uri}") 

212 self.assertEqual(loc1.getExtension(), "") 

213 

214 def testRelativeRoot(self): 

215 root = os.path.abspath(os.path.curdir) 

216 factory = LocationFactory(os.path.curdir) 

217 

218 pathInStore = "relative/path/file.ext" 

219 loc1 = factory.fromPath(pathInStore) 

220 

221 self.assertEqual(loc1.path, os.path.join(root, pathInStore)) 

222 self.assertEqual(loc1.pathInStore, pathInStore) 

223 self.assertEqual(loc1.uri.scheme, "file") 

224 

225 def testQuotedRoot(self): 

226 """Test we can handle quoted characters.""" 

227 root = "/a/b/c+1/d" 

228 factory = LocationFactory(root) 

229 

230 pathInStore = "relative/path/file.ext.gz" 

231 

232 for pathInStore in ("relative/path/file.ext.gz", 

233 "relative/path+2/file.ext.gz", 

234 "relative/path+3/file#.ext.gz"): 

235 loc1 = factory.fromPath(pathInStore) 

236 

237 self.assertEqual(loc1.pathInStore, pathInStore) 

238 self.assertEqual(loc1.path, os.path.join(root, pathInStore)) 

239 self.assertIn("%", str(loc1.uri)) 

240 self.assertEqual(loc1.getExtension(), ".ext.gz") 

241 

242 def testHttpLocation(self): 

243 root = "https://www.lsst.org/butler/datastore" 

244 factory = LocationFactory(root) 

245 print(f"Factory created: {factory}") 

246 

247 pathInStore = "relative/path/file.ext" 

248 loc1 = factory.fromPath(pathInStore) 

249 

250 self.assertEqual(loc1.path, posixpath.join("/butler/datastore", pathInStore)) 

251 self.assertEqual(loc1.pathInStore, pathInStore) 

252 self.assertEqual(loc1.uri.scheme, "https") 

253 self.assertEqual(loc1.uri.basename(), "file.ext") 

254 loc1.updateExtension("fits") 

255 self.assertTrue(loc1.uri.basename(), "file.fits") 

256 

257 def testPosix2OS(self): 

258 """Test round tripping of the posix to os.path conversion helpers.""" 

259 testPaths = ("/a/b/c.e", "a/b", "a/b/", "/a/b", "/a/b/", "a/b/c.e") 

260 for p in testPaths: 

261 with self.subTest(path=p): 

262 self.assertEqual(os2posix(posix2os(p)), p) 

263 

264 def testSplit(self): 

265 """Tests split functionality.""" 

266 testRoot = "/tmp/" 

267 

268 testPaths = ("/absolute/file.ext", "/absolute/", 

269 "file:///absolute/file.ext", "file:///absolute/", 

270 "s3://bucket/root/file.ext", "s3://bucket/root/", 

271 "https://www.lsst.org/root/file.ext", "https://www.lsst.org/root/", 

272 "relative/file.ext", "relative/") 

273 

274 osRelExpected = os.path.join(testRoot, "relative") 

275 expected = (("file:///absolute/", "file.ext"), ("file:///absolute/", ""), 

276 ("file:///absolute/", "file.ext"), ("file:///absolute/", ""), 

277 ("s3://bucket/root/", "file.ext"), ("s3://bucket/root/", ""), 

278 ("https://www.lsst.org/root/", "file.ext"), ("https://www.lsst.org/root/", ""), 

279 (f"file://{osRelExpected}/", "file.ext"), (f"file://{osRelExpected}/", "")) 

280 

281 for p, e in zip(testPaths, expected): 

282 with self.subTest(path=p): 

283 uri = ButlerURI(p, testRoot) 

284 head, tail = uri.split() 

285 self.assertEqual((head.geturl(), tail), e) 

286 

287 # explicit file scheme should force posixpath, check os.path is ignored 

288 posixRelFilePath = posixpath.join(testRoot, "relative") 

289 uri = ButlerURI("file:relative/file.ext", testRoot) 

290 head, tail = uri.split() 

291 self.assertEqual((head.geturl(), tail), (f"file://{posixRelFilePath}/", "file.ext")) 

292 

293 # check head can be empty and we do not get an absolute path back 

294 uri = ButlerURI("file.ext", forceAbsolute=False) 

295 head, tail = uri.split() 

296 self.assertEqual((head.geturl(), tail), ("./", "file.ext")) 

297 

298 # ensure empty path splits to a directory URL 

299 uri = ButlerURI("", forceAbsolute=False) 

300 head, tail = uri.split() 

301 self.assertEqual((head.geturl(), tail), ("./", "")) 

302 

303 uri = ButlerURI(".", forceAbsolute=False) 

304 head, tail = uri.split() 

305 self.assertEqual((head.geturl(), tail), ("./", "")) 

306 

307 

308if __name__ == "__main__": 308 ↛ 309line 308 didn't jump to line 309, because the condition on line 308 was never true

309 unittest.main()