Coverage for python/lsst/daf/butler/script/retrieveArtifacts.py: 50%

16 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-11 02:31 -0800

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 

22from __future__ import annotations 

23 

24__all__ = ("retrieveArtifacts",) 

25 

26import logging 

27from typing import TYPE_CHECKING 

28 

29from .._butler import Butler 

30 

31if TYPE_CHECKING: 31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never true

32 from lsst.resources import ResourcePath 

33 

34log = logging.getLogger(__name__) 

35 

36 

37def retrieveArtifacts( 

38 repo: str, 

39 destination: str, 

40 dataset_type: tuple[str, ...], 

41 collections: tuple[str, ...], 

42 where: str, 

43 find_first: bool, 

44 transfer: str, 

45 preserve_path: bool, 

46 clobber: bool, 

47) -> list[ResourcePath]: 

48 """Parameters are those required for querying datasets plus a destination 

49 URI. 

50 

51 Parameters 

52 ---------- 

53 repo : `str` 

54 URI string of the Butler repo to use. 

55 destination : `str` 

56 URI string of the directory to write the artifacts. 

57 dataset_type : `tuple` of `str` 

58 Dataset type names. An empty tuple implies all dataset types. 

59 collections : `tuple` of `str` 

60 Names of collection globs to match. An empty tuple implies all 

61 collections. 

62 where : `str` 

63 Query modification string. 

64 find_first : `bool` 

65 Whether only the first match should be used. 

66 transfer : `str` 

67 Transfer mode to use when placing artifacts in the destination. 

68 preserve_path : `bool` 

69 If `True` the full datastore path will be retained within the 

70 destination directory, else only the filename will be used. 

71 clobber : `bool` 

72 If `True` allow transfers to overwrite files at the destination. 

73 

74 Returns 

75 ------- 

76 transferred : `list` of `lsst.resources.ResourcePath` 

77 The destination URIs of every transferred artifact. 

78 """ 

79 query_types = dataset_type if dataset_type else ... 

80 query_collections = collections if collections else ... 

81 

82 butler = Butler(repo, writeable=False) 

83 

84 # Need to store in list so we can count the number to give some feedback 

85 # to caller. 

86 refs = list( 

87 butler.registry.queryDatasets( 

88 datasetType=query_types, collections=query_collections, where=where, findFirst=find_first 

89 ) 

90 ) 

91 

92 log.info("Number of datasets matching query: %d", len(refs)) 

93 

94 transferred = butler.retrieveArtifacts( 

95 refs, destination=destination, transfer=transfer, preserve_path=preserve_path, overwrite=clobber 

96 ) 

97 return transferred