Coverage for src/km3dq_grl/fact-add.py: 0%

152 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-18 16:16 +0000

1#! /usr/bin/env python 

2############################################################################### 

3import os 

4import re 

5import time 

6import sys 

7import getpass 

8import readline # Mandatory for proper backspace handling. Do not remove 

9 

10from km3dq_grl import fact_path 

11 

12from km3dq_common.common_library import get_site 

13from km3dq_common.common_library import get_run_properties_from_db 

14 

15from km3dq_common.config_library import configure_dataquality_tag 

16from km3dq_common.config_library import configure_fact 

17 

18 

19############################################################################### 

20def get_current_git_branch(): 

21 """ Return the current git branch""" 

22 g_branch = os.popen("git branch") 

23 for i_branch in g_branch: 

24 if "*" in i_branch: 

25 rsb = re.compile(r"\*|\s|\n") 

26 return rsb.sub("", i_branch) 

27 

28 return "Unknown" 

29 

30 

31############################################################################### 

32# User inputs 

33 

34# Detector affected 

35answer = False 

36while answer is False: 

37 det = input("Detector ([A] / [O] for the current ARCA/ORCA detector): ") 

38 if det == "A": 

39 det = "D0ARCA028" 

40 elif det == "O": 

41 det = "D1ORCA019" 

42 site = get_site(det) 

43 

44 dq_tag = configure_dataquality_tag('default') 

45 

46 if det not in dq_tag['det'][site]: 

47 print("Unknown detector!") 

48 else: 

49 answer = True 

50 

51# Fact type 

52answer = False 

53print("Fact type: ") 

54avail_answer = [] 

55facts = configure_fact() 

56 

57for i_fact in facts['type'].keys(): 

58 avail_answer.append(i_fact) 

59 

60for i_fact, fact_type in enumerate(avail_answer): 

61 print(f"{i_fact}. {fact_type}") 

62 

63while answer is False: 

64 fact_type = input("") 

65 try: 

66 fact_type_int = int(fact_type) 

67 fact_type = avail_answer[fact_type_int] 

68 answer = True 

69 except ValueError: 

70 print("Please enter an integer") 

71 except IndexError: 

72 print(f"Please enter an integer between 0 and {len(avail_answer)-1}") 

73 

74# Run start 

75answer = False 

76while answer is False: 

77 run = input("Run start: ") 

78 try: 

79 run_start = int(run) 

80 answer = True 

81 except ValueError: 

82 print("Please enter an integer") 

83 

84# Run stop 

85answer = False 

86while answer is False: 

87 run = input("Run stop (if unknown or irrelevant, reply 0): ") 

88 try: 

89 if int(run) > 0: 

90 run_stop = int(run) 

91 else: 

92 run_stop = "Unknown / irrelevant" 

93 answer = True 

94 except ValueError: 

95 print("Please enter an integer") 

96 

97# Date 

98rp_db = get_run_properties_from_db(det, "") 

99try: 

100 date_start = int(rp_db[run_start]['UNIXSTARTTIME'])/1e3 

101 date_start_strf = time.strftime("%a, %d %b %Y %H:%M", 

102 time.localtime(int(date_start))) 

103 time_range = (f"{date_start_strf} - ") 

104except KeyError: 

105 print("Unable to retrieve the run start from the db") 

106 sys.exit() 

107 

108if run_stop != "Unknown / irrelevant": 

109 try: 

110 date_stop = int(rp_db[run_stop]['UNIXSTARTTIME'])/1e3 

111 date_stop_strf = time.strftime("%a, %d %b %Y %H:%M", 

112 time.localtime(date_stop)) 

113 time_range += date_stop_strf 

114 except KeyError: 

115 print("Unable to retrieve the run stop from the db") 

116 sys.exit() 

117 

118print(time_range) 

119 

120# Issue / comment 

121answer = False 

122while answer is False: 

123 issue_comment = input("Issue / Comment: ") 

124 if len(issue_comment) < 10: 

125 print("Entry too short... Please add more details") 

126 else: 

127 answer = True 

128 

129# Documentation 

130documentation = input("Documentation (elog, gitlab issue...): ") 

131 

132# Author 

133answer = False 

134whoami = getpass.getuser() 

135while answer is False: 

136 author = input(f"Author (default: {whoami}): ") 

137 if author == "": 

138 author = whoami 

139 

140 date = time.strftime("%d/%m/%y", time.localtime()) 

141 author += f" ({date})" 

142 answer = True 

143 

144############################################################################### 

145# Treatment if user inputs 

146 

147# Create the Facts directory if not yet existing 

148if os.path.exists(f"{site}/{det}/Facts/") is False: 

149 os.popen(f"mkdir {site}/{det}/Facts/") 

150 

151# New entries (one per run) 

152fact_file = fact_path(fact_type, det) 

153 

154new_entry = (f"{run_start} | " 

155 f"{run_stop} | " 

156 f"{time_range} | " 

157 f"{issue_comment} | " 

158 f"{documentation} | " 

159 f"{author}\n") 

160 

161# Git treatment 

162# If the current branch is main, propose to switch to new created branch 

163# Otherwise, leave the choice. 

164cur_git_branch = get_current_git_branch() 

165if cur_git_branch == "main": 

166 print("You are currently in main git branch. I will create a new one " 

167 "for your fact upload\n") 

168 git_branch = cur_git_branch 

169 new_branch = "y" 

170else: 

171 print(f"You are currently in {cur_git_branch} branch. You can either " 

172 "keep on working on this branch for a bulk upload or create a " 

173 "new one") 

174 

175 answer = False 

176 while answer is False: 

177 new_branch = input("Do you want to create a new branch?" 

178 "([y]es, [n]o):") 

179 if new_branch not in ("y", "n"): 

180 print("Type y or n") 

181 else: 

182 answer = True 

183 

184# Checks that the fact file exist and ask for a final confirmation 

185if os.path.exists(fact_file) is False: 

186 print(f"Fact file {fact_file} does not exists." 

187 "I am creating it and adding it to git repository.") 

188 with open(fact_file, "w", encoding="utf-8") as new_file: 

189 new_file.write("Start date | " 

190 "First affected run | " 

191 "Last affected run | " 

192 "Issue / Comment | " 

193 "Documentation | " 

194 "Author\n") 

195 cmd = f"git add {fact_file}" 

196 os.popen(cmd) 

197 

198print(f"I am about to add the following fact for the {det} detector:\n" 

199 f"{new_entry}\n" 

200 f"Fact file: {fact_file}") 

201 

202answer = False 

203while answer is False: 

204 confirm = input("Please confirm by typing [YES] or [NO]:") 

205 if confirm not in ("YES", "NO"): 

206 print("Type YES or NO") 

207 else: 

208 answer = True 

209 

210if confirm == "NO": 

211 sys.exit() 

212 

213# Create the new branch if requested 

214if new_branch == "y": 

215 branch_time = time.strftime("%d_%m_%y_%H_%M", time.localtime()) 

216 git_branch = f"main-fact-upload-{branch_time}" 

217 cmd = f"git checkout -b {git_branch}" 

218 os.popen(cmd) 

219 

220 print(f"You are now working in {git_branch} branch.") 

221else: 

222 git_branch = cur_git_branch 

223 

224# And finally update the file 

225with open(fact_file, "r", encoding="utf-8") as orig: 

226 with open(f"{fact_file}_tmp", "w", encoding="utf-8") as fin: 

227 lines = orig.readlines() 

228 first_line = True 

229 for i_line in lines: 

230 fin.write(i_line) 

231 if first_line: # New entry is at the top of the fact file 

232 fin.write(new_entry) 

233 first_line = False 

234 

235cmd = f"mv {fact_file}_tmp {fact_file}" 

236os.popen(cmd) 

237 

238cmd = f"git commit {fact_file} -m '{det} fact added by {author}'" 

239os.popen(cmd) 

240 

241print("The fact upload has been commited in the new branch.\n" 

242 "You can add more facts in the same branch.\n\n" 

243 "When your modifications are complete, proceed with:\n" 

244 f"- git push --set-upstream origin {git_branch}\n" 

245 "- Request the merging", 

246 "Once the branch is merged, the fact page will be automatically " 

247 "updated\n") 

248print("Do not forget to git fetch / git merge when you are back in " 

249 "the main branch")