Hide keyboard shortcuts

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

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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 <https://www.gnu.org/licenses/>. 

21"""APIs for building software provenance from lsstsw.""" 

22 

23__all__ = ['LsstswRepos'] 

24 

25import os 

26 

27import yaml 

28try: 

29 # GitPython is an optional dependency, not part of the LSST Stack. 

30 import git 

31except ImportError: 

32 git = None 

33 

34 

35class LsstswRepos(object): 

36 """lsstsw package version information based on repos.yaml and 

37 checked out Git repositories. 

38 

39 Parameters 

40 ---------- 

41 dirname : `str` 

42 Path of an ``lsstsw`` directory. 

43 """ 

44 def __init__(self, dirname): 

45 self._dirname = dirname 

46 self._repos = self._load_repos_yaml() 

47 

48 def __contains__(self, package_name): 

49 """Test if a package is present in `lsstsw`'s repos.yaml dataset. 

50 """ 

51 return package_name in self._repos 

52 

53 def __len__(self): 

54 return len(self._repos) 

55 

56 @property 

57 def manifest_path(self): 

58 """Path of the manifest.txt file.""" 

59 return os.path.join(self._dirname, 'build', 'manifest.txt') 

60 

61 def get_package_repo_path(self, package_name): 

62 """Path to a EUPS package repository in lsstsw/build. 

63 

64 Parameters 

65 ---------- 

66 package_name : `str` 

67 Name of the EUPS package. 

68 

69 Returns 

70 ------- 

71 path : `str` 

72 Directory path of the package's Git repository in lsstsw/build. 

73 """ 

74 return os.path.join(self._dirname, 'build', package_name) 

75 

76 def get_package_branch(self, package_name): 

77 """Get the name of the checked-out branch of an EUPS package cloned in 

78 lsstsw/build. 

79 

80 Parameters 

81 ---------- 

82 package_name : `str` 

83 Name of the EUPS package. 

84 

85 Returns 

86 ------- 

87 branch : `str` 

88 Name of the checked-out Git branch. If GitPython is not 

89 installed, `None` is always returned instead. 

90 """ 

91 if git is not None: 

92 repo = git.Repo(self.get_package_repo_path(package_name)) 

93 return repo.active_branch.name 

94 else: 

95 return None 

96 

97 def get_package_commit_sha(self, package_name): 

98 """Get the hex SHA of the checked-out commit of an EUPS package 

99 cloned to lsstsw/build. 

100 

101 Parameters 

102 ---------- 

103 package_name : `str` 

104 Name of the EUPS package. 

105 

106 Returns 

107 ------- 

108 commit : `str` 

109 Hex SHA of the checkout-out Git commit. If GitPython is not 

110 installed, `None` is always returned instead. 

111 """ 

112 if git is not None: 

113 repo = git.Repo(self.get_package_repo_path(package_name)) 

114 return repo.active_branch.commit.hexsha 

115 else: 

116 return None 

117 

118 def get_package_repo_url(self, package_name): 

119 """URL of the package's Git repository. 

120 

121 This data is obtained from lsstsw/etc/repos.yaml. 

122 

123 Parameters 

124 ---------- 

125 package_name : `str` 

126 Name of the EUPS package. 

127 

128 Returns 

129 ------- 

130 repo_url : `str` 

131 Git origin URL of the package's Git repository. 

132 """ 

133 s = self._repos[package_name] 

134 if isinstance(s, str): 

135 return s 

136 else: 

137 # For packages that have sub-documents, rather than the value 

138 # as the URL. See repos.yaml for format documentation. 

139 return s['url'] 

140 

141 def _load_repos_yaml(self): 

142 """Load lsstsw's repos.yaml.""" 

143 yaml_path = os.path.join(self._dirname, 'etc', 'repos.yaml') 

144 with open(yaml_path) as f: 

145 repos = yaml.safe_load(f) 

146 return repos