Coverage for python/lsst/daf/butler/repo_relocation.py: 31%

20 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 02:53 -0700

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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

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

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

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

18# (at your option) any later version. 

19# 

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

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

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28"""Routines to support relocation of a file-based Butler repository.""" 

29 

30from __future__ import annotations 

31 

32__all__ = ("BUTLER_ROOT_TAG", "replaceRoot") 

33 

34import os.path 

35 

36from lsst.resources import ResourcePath, ResourcePathExpression 

37 

38BUTLER_ROOT_TAG = "<butlerRoot>" 

39"""The special string to be used in configuration files to indicate that 

40the butler root location should be used.""" 

41 

42 

43def replaceRoot(configRoot: str, butlerRoot: ResourcePathExpression | None) -> str: 

44 """Update a configuration root with the butler root location. 

45 

46 No changes are made if the special root string is not found in the 

47 configuration entry. The name of the tag is defined in 

48 the module variable `~lsst.daf.butler.repoRelocation.BUTLER_ROOT_TAG`. 

49 

50 Parameters 

51 ---------- 

52 configRoot : `str` 

53 Directory root location as specified in a configuration file. 

54 butlerRoot : `lsst.resources.ResourcePathExpression` or `None` 

55 Butler root directory. Absolute path is inserted into the 

56 ``configRoot`` where the 

57 `~lsst.daf.butler.repoRelocation.BUTLER_ROOT_TAG` string is 

58 found. Passing `None` here is allowed only as a convenient way to 

59 raise an exception (`ValueError`). 

60 

61 Returns 

62 ------- 

63 newRoot : `str` 

64 New configuration string, with the root tag replaced with the butler 

65 root if that tag was present in the supplied configuration root. 

66 

67 Raises 

68 ------ 

69 ValueError 

70 Raised if ``butlerRoot`` is not set but a value is required. 

71 """ 

72 # Do nothing if there is nothing to be done or if there is a type 

73 # mismatch. 

74 try: 

75 if BUTLER_ROOT_TAG not in configRoot: 

76 return configRoot 

77 except TypeError: 

78 return configRoot 

79 

80 # None or empty string indicate a problem 

81 if not butlerRoot: 

82 raise ValueError( 

83 f"Required to replace {BUTLER_ROOT_TAG} in '{configRoot}' but a replacement has not been defined" 

84 ) 

85 

86 # Use absolute file path if this refers to a local file, else use 

87 # unchanged since all other URI schemes are absolute 

88 uri = ResourcePath(butlerRoot) 

89 if uri.isLocal: 

90 # This will be a local file with URI quoting removed 

91 butlerRoot = os.path.abspath(uri.ospath) 

92 

93 assert butlerRoot is not None 

94 return configRoot.replace(BUTLER_ROOT_TAG, str(butlerRoot))