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

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."""
24from __future__ import annotations
26__all__ = ("BUTLER_ROOT_TAG", "replaceRoot")
28import os.path
29from typing import Optional, Union
30from ._butlerUri import ButlerURI
32BUTLER_ROOT_TAG = "<butlerRoot>"
33"""The special string to be used in configuration files to indicate that
34the butler root location should be used."""
37def replaceRoot(configRoot: str, butlerRoot: Optional[Union[ButlerURI, str]]) -> str:
38 """Update a configuration root with the butler root location.
40 No changes are made if the special root string is not found in the
41 configuration entry. The name of the tag is defined in
42 the module variable `~lsst.daf.butler.core.repoRelocation.BUTLER_ROOT_TAG`.
44 Parameters
45 ----------
46 configRoot : `str`
47 Directory root location as specified in a configuration file.
48 butlerRoot : `str`, `ButlerURI` or `None`
49 Butler root directory. Absolute path is inserted into the
50 ``configRoot`` where the
51 `~lsst.daf.butler.core.repoRelocation.BUTLER_ROOT_TAG` string is
52 found. Passing `None` here is allowed only as a convenient way to
53 raise an exception (`ValueError`).
55 Returns
56 -------
57 newRoot : `str`
58 New configuration string, with the root tag replaced with the butler
59 root if that tag was present in the supplied configuration root.
61 Raises
62 ------
63 ValueError
64 Raised if ``butlerRoot`` is not set but a value is required.
65 """
67 # Do nothing if there is nothing to be done
68 if BUTLER_ROOT_TAG not in configRoot:
69 return configRoot
71 # None or empty string indicate a problem
72 if not butlerRoot:
73 raise ValueError(f"Required to replace {BUTLER_ROOT_TAG} in '{configRoot}' "
74 "but a replacement has not been defined")
76 # Use absolute file path if this refers to a local file, else use
77 # unchanged since all other URI schemes are absolute
78 uri = ButlerURI(butlerRoot)
79 if uri.isLocal:
80 # This will be a local file with URI quoting removed
81 butlerRoot = os.path.abspath(uri.ospath)
83 assert butlerRoot is not None
84 return configRoot.replace(BUTLER_ROOT_TAG, str(butlerRoot))