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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

# 

# LSST Data Management System 

# Copyright 2016 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import pickle 

import os 

import shutil 

import unittest 

import tempfile 

 

import yaml 

 

import lsst.utils.tests 

import lsst.daf.persistence as dp 

from lsst.daf.persistence import Policy 

 

# Define the root of the tests relative to this file 

ROOT = os.path.abspath(os.path.dirname(__file__)) 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class PosixPickleStringHanlder: 

 

@staticmethod 

def get(butlerLocation): 

if butlerLocation.storageName != "PickleStorage": 

raise TypeError("PosixStoragePickleMapper only supports PickleStorage") 

location = butlerLocation.getLocations()[0] # should never be more than 1 location 

with open(location, 'rb') as f: 

ret = pickle.load(f) 

return ret 

 

@staticmethod 

def put(obj, butlerLocation): 

if butlerLocation.storageName != "PickleStorage": 

raise TypeError("PosixStoragePickleMapper only supports PickleStorage") 

for location in butlerLocation.getLocations(): 

with open(location, 'wb') as f: 

pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL) 

 

 

class MapperTestCfg(Policy, yaml.YAMLObject): 

yaml_tag = u"!MapperTestCfg" 

 

def __init__(self, cls, root): 

super().__init__({'root': root, 'cls': cls}) 

 

 

class MapperTest(dp.Mapper): 

 

@classmethod 

def cfg(cls, root=None): 

return MapperTestCfg(cls=cls, root=root) 

 

def __init__(self, cfg): 

super().__init__() 

# self.root = cfg['root'] 

self.storage = cfg['storage'] 

self.cfg = cfg 

 

def __repr__(self): 

return 'MapperTest(cfg=%s)' % self.cfg 

 

def map_str(self, dataId, write): 

template = "strfile_%(strId)s.pickle" 

path = template % dataId 

if not write: 

if not self.storage.exists(path): 

return None 

location = self.storage.locationWithRoot(path) 

return dp.ButlerLocation(pythonType=PosixPickleStringHanlder, cppType=None, 

storageName='PickleStorage', locationList=location, 

dataId=dataId, mapper=self, storage=self.storage) 

 

 

class ReposInButler(unittest.TestCase): 

 

def setUp(self): 

self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='ReposInButler-') 

 

def tearDown(self): 

if os.path.exists(self.testDir): 

shutil.rmtree(self.testDir) 

 

# disable this test until we work more on repo of repos; starting with DM-6227 

@unittest.expectedFailure 

def test(self): 

repoMapperPolicy = { 

'repositories': { 

'cfg': { 

'template': 'repos/repo_v%(version)s/repoCfg.yaml', 

'python': 'lsst.daf.persistence.RepositoryCfg', 

'storage': 'YamlStorage' 

} 

} 

} 

 

# create a cfg of a repository for our repositories 

storageCfg = dp.PosixStorage.cfg(root=self.testDir) 

accessCfg = dp.Access.cfg(storageCfg=storageCfg) 

self.assertIsNotNone(accessCfg) 

mapperCfg = dp.RepositoryMapper.cfg(policy=repoMapperPolicy) 

self.assertIsNotNone(mapperCfg) 

# Note that right now a repo is either input OR output, there is no input-output repo, this design 

# is result of butler design conversations. Right now, if a user wants to write to and then read from 

# a repo, a repo can have a parent repo with the same access (and mapper) parameters as itself. 

repoOfRepoCfg = dp.Repository.cfg(mode='rw', 

storageCfg=dp.PosixStorage.cfg(root=self.testDir), 

mapper=dp.RepositoryMapper.cfg(policy=repoMapperPolicy)) 

 

repoButler = dp.Butler(outputs=repoOfRepoCfg) 

# create a cfg of a repository we'd like to use. Note that we don't create the root of the cfg. 

# this will get populated by the repoOfRepos template. 

repoCfg = dp.Repository.cfg(mode='rw', storageCfg=dp.PosixStorage.cfg(), mapper=MapperTest.cfg()) 

# and put that config into the repoOfRep 

repoButler.put(repoCfg, 'cfg', dataId={'version': 123}) 

 

# get the cfg back out of the butler. This will return a cfg with the root location populated. 

# i.e. repoCfg['accessCfg.storageCfg.root'] is populated. 

repoCfg = repoButler.get('cfg', dataId={'version': 123}, immediate=True) 

butler = dp.Butler(outputs=repoCfg) 

 

obj = 'abc' 

butler.put(obj, 'str', {'strId': 'a'}) 

reloadedObj = butler.get('str', {'strId': 'a'}) 

self.assertEqual(obj, reloadedObj) 

self.assertIsNot(obj, reloadedObj) # reloaded object should be a new instance. 

 

# explicitly release some objects; these names will be reused momentarily. 

del butler, repoCfg, obj, reloadedObj 

 

# Create another repository, and put it in the repo of repos, with a new version number. 

repoCfg = dp.Repository.cfg(mode='rw', storageCfg=dp.PosixStorage.cfg(), mapper=MapperTest.cfg()) 

repoButler.put(repoCfg, 'cfg', dataId={'version': 124}) 

repoCfg = repoButler.get('cfg', dataId={'version': 124}, immediate=True) 

butler = dp.Butler(outputs=repoCfg) 

# create an object that is slightly different than the object in repo version 123, and give it the 

# same dataId as the object in repo 123. Put it, and get it to verify. 

obj = 'abcd' 

butler.put(obj, 'str', {'strId': 'a'}) 

reloadedObj = butler.get('str', {'strId': 'a'}) 

self.assertEqual(obj, reloadedObj) 

self.assertIsNot(obj, reloadedObj) 

 

# release the objects for repo version 124 

del butler, repoCfg, obj, reloadedObj 

 

# from the repo butler, get the cfgs for both repo versions, create butlers from each of them, get 

# the objects, and verify correct values. 

repo123Cfg = repoButler.get('cfg', dataId={'version': 123}, immediate=True) 

repo124Cfg = repoButler.get('cfg', dataId={'version': 124}, immediate=True) 

butler123 = dp.Butler(inputs=repo123Cfg) 

butler124 = dp.Butler(inputs=repo124Cfg) 

obj123 = butler123.get('str', {'strId': 'a'}) 

obj124 = butler124.get('str', {'strId': 'a'}) 

self.assertEqual(obj123, 'abc') 

self.assertEqual(obj124, 'abcd') 

 

 

class MemoryTester(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

185 ↛ 186line 185 didn't jump to line 186, because the condition on line 185 was never trueif __name__ == '__main__': 

lsst.utils.tests.init() 

unittest.main()