Coverage for python/lsst/daf/butler/core/repoRelocation.py : 30%

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/>.
22"""Routines to support relocation of a file-based Butler repository."""
24__all__ = ("BUTLER_ROOT_TAG", "replaceRoot")
26import os.path
27from .location import ButlerURI
29BUTLER_ROOT_TAG = "<butlerRoot>"
30"""The special string to be used in configuration files to indicate that
31the butler root location should be used."""
34def replaceRoot(configRoot, butlerRoot):
35 """Update a configuration root with the butler root location.
37 No changes are made if the special root string is not found in the
38 configuration entry. The name of the tag is defined in
39 the module variable `~lsst.daf.butler.core.repoRelocation.BUTLER_ROOT_TAG`.
41 Parameters
42 ----------
43 configRoot : `str`
44 Directory root location as specified in a configuration file.
45 butlerRoot : `str`
46 Butler root directory. Absolute path is inserted into the
47 ``configRoot`` where the
48 `~lsst.daf.butler.core.repoRelocation.BUTLER_ROOT_TAG` string is
49 found.
51 Returns
52 -------
53 newRoot : `str`
54 New configuration string, with the root tag replaced with the butler
55 root if that tag was present in the supplied configuration root.
57 Raises
58 ------
59 ValueError
60 Raised if ``butlerRoot`` is not set but a value is required.
61 """
63 # Do nothing if there is nothing to be done
64 if BUTLER_ROOT_TAG not in configRoot:
65 return configRoot
67 # None or empty string indicate a problem
68 if not butlerRoot:
69 raise ValueError(f"Required to replace {BUTLER_ROOT_TAG} in '{configRoot}' "
70 "but a replacement has not been defined")
72 # Use absolute file path if this uses file scheme, else use unchanged
73 uri = ButlerURI(butlerRoot)
74 if not uri.scheme or uri.scheme == "file":
75 butlerRoot = os.path.abspath(uri.path)
77 return configRoot.replace(BUTLER_ROOT_TAG, butlerRoot)