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 testUriExtensions(self): 

185 """Test extension extraction.""" 

186 

187 files = (("file.fits.gz", ".fits.gz"), 

188 ("file.fits", ".fits"), 

189 ("file.fits.xz", ".fits.xz"), 

190 ("file.fits.tar", ".tar"), 

191 ("file", ""), 

192 ("flat_i_sim_1.4_blah.fits.gz", ".fits.gz"), 

193 ("flat_i_sim_1.4_blah.txt", ".txt"), 

194 ("flat_i_sim_1.4_blah.fits.fz", ".fits.fz"), 

195 ("flat_i_sim_1.4_blah.fits.txt", ".txt"), 

196 ) 

197 

198 for file, expected in files: 

199 uri = ButlerURI(f"a/b/{file}") 

200 self.assertEqual(uri.getExtension(), expected) 

201 

202 def testFileLocation(self): 

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

204 factory = LocationFactory(root) 

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

206 

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

208 loc1 = factory.fromPath(pathInStore) 

209 

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

211 self.assertEqual(loc1.pathInStore, pathInStore) 

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

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

214 loc1.updateExtension("fits") 

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

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

217 loc1.updateExtension("fits.gz") 

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

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

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

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

222 loc1.updateExtension(".jpeg") 

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

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

225 loc1.updateExtension(None) 

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

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

228 loc1.updateExtension("") 

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

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

231 

232 def testRelativeRoot(self): 

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

234 factory = LocationFactory(os.path.curdir) 

235 

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

237 loc1 = factory.fromPath(pathInStore) 

238 

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

240 self.assertEqual(loc1.pathInStore, pathInStore) 

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

242 

243 with self.assertRaises(ValueError): 

244 factory.fromPath("../something") 

245 

246 def testQuotedRoot(self): 

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

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

249 factory = LocationFactory(root) 

250 

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

252 

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

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

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

256 loc1 = factory.fromPath(pathInStore) 

257 

258 self.assertEqual(loc1.pathInStore, pathInStore) 

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

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

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

262 

263 def testHttpLocation(self): 

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

265 factory = LocationFactory(root) 

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

267 

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

269 loc1 = factory.fromPath(pathInStore) 

270 

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

272 self.assertEqual(loc1.pathInStore, pathInStore) 

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

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

275 loc1.updateExtension("fits") 

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

277 

278 def testPosix2OS(self): 

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

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

281 for p in testPaths: 

282 with self.subTest(path=p): 

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

284 

285 def testSplit(self): 

286 """Tests split functionality.""" 

287 testRoot = "/tmp/" 

288 

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

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

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

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

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

294 

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

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

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

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

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

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

301 

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

303 with self.subTest(path=p): 

304 uri = ButlerURI(p, testRoot) 

305 head, tail = uri.split() 

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

307 

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

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

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

311 head, tail = uri.split() 

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

313 

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

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

316 head, tail = uri.split() 

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

318 

319 # ensure empty path splits to a directory URL 

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

321 head, tail = uri.split() 

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

323 

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

325 head, tail = uri.split() 

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

327 

328 

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

330 unittest.main()