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

21 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-09 02:11 -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 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/>. 

21 

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

23 

24from __future__ import annotations 

25 

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

27 

28import os.path 

29from typing import Optional 

30 

31from lsst.resources import ResourcePath, ResourcePathExpression 

32 

33BUTLER_ROOT_TAG = "<butlerRoot>" 

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

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

36 

37 

38def replaceRoot(configRoot: str, butlerRoot: Optional[ResourcePathExpression]) -> str: 

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

40 

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

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

43 the module variable `~lsst.daf.butler.core.repoRelocation.BUTLER_ROOT_TAG`. 

44 

45 Parameters 

46 ---------- 

47 configRoot : `str` 

48 Directory root location as specified in a configuration file. 

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

50 Butler root directory. Absolute path is inserted into the 

51 ``configRoot`` where the 

52 `~lsst.daf.butler.core.repoRelocation.BUTLER_ROOT_TAG` string is 

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

54 raise an exception (`ValueError`). 

55 

56 Returns 

57 ------- 

58 newRoot : `str` 

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

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

61 

62 Raises 

63 ------ 

64 ValueError 

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

66 """ 

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

68 # mismatch. 

69 try: 

70 if BUTLER_ROOT_TAG not in configRoot: 

71 return configRoot 

72 except TypeError: 

73 return configRoot 

74 

75 # None or empty string indicate a problem 

76 if not butlerRoot: 

77 raise ValueError( 

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

79 ) 

80 

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

82 # unchanged since all other URI schemes are absolute 

83 uri = ResourcePath(butlerRoot) 

84 if uri.isLocal: 

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

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

87 

88 assert butlerRoot is not None 

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