29 from .
import PosixStorage, RepositoryCfg, safeFileIo, ParentsMismatch
30 from lsst.log
import Log
36 def write(cls, cfg, butlerLocation):
37 """Serialize a RepositoryCfg to a location.
39 When the location is the same as cfg.root, the RepositoryCfg is to be written at the root location of
40 the repository. In that case, root is not written in the serialized cfg; it is implicit in the
41 location of the cfg. This allows the cfg to move from machine to machine without modification.
45 cfg : RepositoryCfg instance
46 The RepositoryCfg to be serialized.
47 butlerLocation : ButlerLocation
48 The location to write the RepositoryCfg.
50 def setRoot(cfg, loc):
51 loc = os.path.split(loc)[0]
52 if loc
is None or cfg.root == loc:
60 loc = butlerLocation.storage.root
61 parseRes = urllib.parse.urlparse(loc
if loc
is not None else cfg.root)
62 loc = os.path.join(parseRes.path, butlerLocation.getLocations()[0])
64 with safeFileIo.SafeLockedFileForRead(loc)
as f:
65 existingCfg = RepositoryCfgPosixFormatter._read(f, parseRes.path)
66 if existingCfg == cfg:
70 if e.errno != errno.ENOENT:
73 existingCfg = RepositoryCfgPosixFormatter._read(f, parseRes.path)
74 if existingCfg
is None:
75 cfgToWrite = setRoot(cfg, loc)
77 if existingCfg == cfg:
81 existingCfg.extend(cfg)
82 cfgToWrite = setRoot(existingCfg, loc)
83 except ParentsMismatch
as e:
84 raise RuntimeError(
"Can not extend existing repository cfg because: {}".format(e))
85 yaml.dump(cfgToWrite, f)
89 def _read(cls, fileObject, uri):
90 """Get a persisted RepositoryCfg from an open file object.
94 fileObject : an open file object
95 the file that contains the RepositoryCfg.
99 A RepositoryCfg instance or None
101 repositoryCfg = yaml.load(fileObject)
102 if repositoryCfg
is not None:
103 if repositoryCfg.root
is None:
104 repositoryCfg.root = uri
110 loc = butlerLocation.storage.root
111 fileLoc = os.path.join(loc, butlerLocation.getLocations()[0])
113 with safeFileIo.SafeLockedFileForRead(fileLoc)
as f:
114 repositoryCfg = RepositoryCfgPosixFormatter._read(f, loc)
116 if e.errno != errno.ENOENT:
121 PosixStorage.registerFormatter(RepositoryCfg, RepositoryCfgPosixFormatter)