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

# 

# LSST Data Management System 

# Copyright 2017 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 copy 

import errno 

import yaml 

import os 

import urllib 

from . import PosixStorage, RepositoryCfg, safeFileIo, ParentsMismatch 

 

 

__all__ = [] 

 

 

def _write(butlerLocation, cfg): 

"""Serialize a RepositoryCfg to a location. 

 

When the location is the same as cfg.root, the RepositoryCfg is to be written at the root location of 

the repository. In that case, root is not written in the serialized cfg; it is implicit in the 

location of the cfg. This allows the cfg to move from machine to machine without modification. 

 

Parameters 

---------- 

butlerLocation : ButlerLocation 

The location to write the RepositoryCfg. 

cfg : RepositoryCfg instance 

The RepositoryCfg to be serialized. 

""" 

def setRoot(cfg, loc): 

loc = os.path.split(loc)[0] # remove the `repoistoryCfg.yaml` file name 

if loc is None or cfg.root == loc: 

cfg = copy.copy(cfg) 

cfg.root = None 

return cfg 

 

# This class supports schema 'file' and also treats no schema as 'file'. 

# Split the URI and take only the path; remove the schema from loc if it's there. 

loc = butlerLocation.storage.root 

parseRes = urllib.parse.urlparse(loc if loc is not None else cfg.root) 

loc = os.path.join(parseRes.path, butlerLocation.getLocations()[0]) 

try: 

with safeFileIo.SafeLockedFileForRead(loc) as f: 

existingCfg = _doRead(f, parseRes.path) 

if existingCfg == cfg: 

cfg.dirty = False 

return 

except IOError as e: 

if e.errno != errno.ENOENT: # ENOENT is 'No such file or directory' 

raise 

with safeFileIo.SafeLockedFileForWrite(loc) as f: 

existingCfg = _doRead(f, parseRes.path) 

if existingCfg is None: 

cfgToWrite = setRoot(cfg, loc) 

else: 

if existingCfg == cfg: 

cfg.dirty = False 

return 

try: 

existingCfg.extend(cfg) 

cfgToWrite = setRoot(existingCfg, loc) 

except ParentsMismatch as e: 

raise RuntimeError("Can not extend existing repository cfg because: {}".format(e)) 

yaml.dump(cfgToWrite, f) 

cfg.dirty = False 

 

 

def _doRead(fileObject, uri): 

"""Get a persisted RepositoryCfg from an open file object. 

 

Parameters 

---------- 

fileObject : an open file object 

the file that contains the RepositoryCfg. 

uri : string 

path to the repositoryCfg 

 

Returns 

------- 

A RepositoryCfg instance or None 

""" 

repositoryCfg = yaml.load(fileObject) 

if repositoryCfg is not None: 

if repositoryCfg.root is None: 

repositoryCfg.root = uri 

return repositoryCfg 

 

 

def _read(butlerLocation): 

"""Deserialize a RepositoryCfg from a location. 

 

Parameters 

---------- 

butlerLocation : ButlerLocation 

The lcoation from which to read the RepositoryCfg. 

 

Returns 

------- 

RepositoryCfg 

The deserialized RepoistoryCfg. 

 

Raises 

------ 

IOError 

Raised if no repositoryCfg exists at the location. 

""" 

repositoryCfg = None 

loc = butlerLocation.storage.root 

fileLoc = os.path.join(loc, butlerLocation.getLocations()[0]) 

try: 

with safeFileIo.SafeLockedFileForRead(fileLoc) as f: 

repositoryCfg = _doRead(f, loc) 

except IOError as e: 

if e.errno != errno.ENOENT: # ENOENT is 'No such file or directory' 

raise 

return repositoryCfg 

 

 

PosixStorage.registerFormatters(RepositoryCfg, _read, _write)