Coverage for  / opt / hostedtoolcache / Python / 3.11.14 / x64 / lib / python3.11 / site-packages / rattlesnake / cicd / utilities.py: 100%

70 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-27 18:22 +0000

1#!/usr/bin/env python3 

2""" 

3Utilities for CICD processes. 

4""" 

5 

6import re 

7from datetime import datetime 

8 

9import pytz 

10 

11 

12def get_score_color_lint(pylint_score: str) -> str: 

13 """ 

14 Determine color based on pylint score. 

15 

16 Args: 

17 pylint_score: The pylint score as string, e.g., "8.5", "7.0", etc. 

18 

19 Returns: 

20 Hex color code for the score, as a string. 

21 """ 

22 try: 

23 score_val: float = float(pylint_score) 

24 if score_val >= 8.0: 

25 return "brightgreen" 

26 elif score_val >= 6.0: 

27 return "yellow" 

28 elif score_val >= 4.0: 

29 return "orange" 

30 else: 

31 return "red" 

32 except ValueError: 

33 return "gray" 

34 

35 

36def get_score_color_coverage(coverage_score: str) -> str: 

37 """ 

38 Determines the color based on a pytest coverage score. 

39 

40 Args: 

41 coverage_score: The coverage score as a string, e.g., "92.5" 

42 

43 Returns: 

44 The color for the badge as a string. 

45 """ 

46 try: 

47 score_val: float = float(coverage_score) 

48 if score_val >= 90: 

49 return "brightgreen" 

50 elif score_val >= 80: 

51 return "green" 

52 elif score_val >= 70: 

53 return "yellow" 

54 elif score_val >= 60: 

55 return "orange" 

56 else: 

57 return "red" 

58 except ValueError: 

59 return "gray" 

60 

61 

62def get_timestamp() -> str: 

63 """ 

64 Get formatted timestamp with UTC, EST, and MST times. 

65 

66 Returns: 

67 Formatted timestamp string 

68 """ 

69 # Get the current UTC time 

70 utc_now: datetime = datetime.now(pytz.utc) 

71 

72 # Define the time zones 

73 timezone_est: pytz.BaseTzInfo = pytz.timezone("America/New_York") 

74 timezone_mst: pytz.BaseTzInfo = pytz.timezone("America/Denver") 

75 

76 # Convert UTC time to EST and MST 

77 est_now: datetime = utc_now.astimezone(timezone_est) 

78 mst_now: datetime = utc_now.astimezone(timezone_mst) 

79 

80 # Format the output 

81 df: str = "%Y-%m-%d %H:%M:%S " # Date format 

82 utc: str = utc_now.strftime(df + "UTC") 

83 est: str = est_now.strftime(df + "EST") 

84 mst: str = mst_now.strftime(df + "MST") 

85 

86 # Combine the formatted times 

87 timestamp: str = f"{utc} ({est} / {mst})" 

88 

89 return timestamp 

90 

91 

92def extend_timestamp(short: str) -> str: 

93 """ 

94 Given a timestamp string from CI/CD in the form of 

95 20250815_211112_UTC, extend the timestamp to include EST and MST times 

96 and return the extended string, so it look like, for example, 

97 2025-08-15 21:11:12 UTC (2025-08-15 17:11:12 EST / 2025-08-15 15:11:12 MST) 

98 

99 Args: 

100 short: the UTC bash string, for example: 20250815_211112_UTC 

101 

102 Returns: 

103 Extended timestamp, for example 

104 2025-08-15 21:11:12 UTC (2025-08-15 17:11:12 EST / 2025-08-15 15:11:12 MST) 

105 """ 

106 # Regex pattern to match the required format: YYYYMMDD_HHMMSS_TZ 

107 # The timezone abbreviation must be 'UTC', 'GMT', or 'Z' 

108 pattern: re.Pattern = re.compile(r"^(\d{8})_(\d{6})_(UTC|GMT|Z)$") 

109 match = pattern.match(short) 

110 

111 if not match: 

112 raise ValueError( 

113 f"Invalid timestamp format: '{short}'. " 

114 f"Expected format is YYYYMMDD_HHMMSS_TZ, where TZ is one of UTC, GMT, or Z." 

115 ) 

116 

117 # Extract the date and time parts from the regex match 

118 date_part, time_part, _ = match.groups() 

119 

120 # Combine the parts into a format that can be parsed by datetime 

121 datetime_str: str = f"{date_part}_{time_part}_UTC" 

122 input_format: str = "%Y%m%d_%H%M%S_%Z" 

123 

124 # Convert the input string to a datetime object 

125 utc_datetime: datetime = datetime.strptime(datetime_str, input_format) 

126 

127 # Make the datetime object timezone-aware 

128 utc_now: datetime = pytz.utc.localize(utc_datetime) 

129 

130 # Define the time zones 

131 timezone_est: pytz.BaseTzInfo = pytz.timezone("America/New_York") 

132 timezone_mst: pytz.BaseTzInfo = pytz.timezone("America/Denver") 

133 

134 # Convert UTC time to EST and MST 

135 est_now: datetime = utc_now.astimezone(timezone_est) 

136 mst_now: datetime = utc_now.astimezone(timezone_mst) 

137 

138 # Format the output 

139 df: str = "%Y-%m-%d %H:%M:%S" # Date format without trailing space 

140 utc: str = utc_now.strftime(df) 

141 est: str = est_now.strftime(df) 

142 mst: str = mst_now.strftime(df) 

143 

144 # Use timezone abbreviations from the datetime objects 

145 utc_tz_abbr: str = utc_now.strftime("%Z") 

146 # est_tz_abbr: str = est_now.strftime("%Z") 

147 est_tz_abbr: str = "EST" 

148 # mst_tz_abbr: str = mst_now.strftime("%Z") 

149 mst_tz_abbr: str = "MST" 

150 

151 # Combine the formatted times 

152 timestamp: str = f"{utc} {utc_tz_abbr} ({est} {est_tz_abbr} / {mst} {mst_tz_abbr})" 

153 

154 return timestamp 

155 

156 

157def write_report(html_content: str, output_file: str) -> None: 

158 """ 

159 Write HTML content to file. 

160 

161 Args: 

162 html_content: The HTML content to write 

163 output_file: Path for the output HTML file 

164 

165 Raises: 

166 IOError: If the file cannot be written. 

167 """ 

168 try: 

169 with open(output_file, "w", encoding="utf-8") as f: 

170 f.write(html_content) 

171 except IOError as e: 

172 raise IOError(f'Error writing output file "{output_file}": {e}') from e