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

20 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-12 10:56 -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 

29 

30from lsst.resources import ResourcePath, ResourcePathExpression 

31 

32BUTLER_ROOT_TAG = "<butlerRoot>" 

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

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

35 

36 

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

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

39 

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`. 

43 

44 Parameters 

45 ---------- 

46 configRoot : `str` 

47 Directory root location as specified in a configuration file. 

48 butlerRoot : `lsst.resources.ResourcePathExpression` 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`). 

54 

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. 

60 

61 Raises 

62 ------ 

63 ValueError 

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

65 """ 

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

67 # mismatch. 

68 try: 

69 if BUTLER_ROOT_TAG not in configRoot: 

70 return configRoot 

71 except TypeError: 

72 return configRoot 

73 

74 # None or empty string indicate a problem 

75 if not butlerRoot: 

76 raise ValueError( 

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

78 ) 

79 

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

81 # unchanged since all other URI schemes are absolute 

82 uri = ResourcePath(butlerRoot) 

83 if uri.isLocal: 

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

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

86 

87 assert butlerRoot is not None 

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