Coverage for src / CSET / cset_workflow / app / finish_website / bin / finish_website.py: 100%

71 statements  

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

1#!/usr/bin/env python3 

2# © Crown copyright, Met Office (2022-2025) and CSET contributors. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15 

16""" 

17Create the CSET diagnostic viewing website. 

18 

19Copies the static files that make up the web interface, constructs the plot 

20index, and updates the workflow status on the front page of the 

21web interface. 

22""" 

23 

24import json 

25import logging 

26import os 

27import shutil 

28import sys 

29import time 

30from importlib.metadata import version 

31from pathlib import Path 

32 

33from CSET._common import combine_dicts, sort_dict 

34 

35logging.basicConfig( 

36 level=os.getenv("LOGLEVEL", "INFO"), 

37 format="%(asctime)s %(levelname)s %(message)s", 

38 stream=sys.stdout, 

39) 

40logger = logging.getLogger(__name__) 

41 

42 

43def install_website_skeleton(www_root_link: Path, www_content: Path): 

44 """Copy static website files and create symlink from web document root.""" 

45 # Remove existing link to output ahead of creating new symlink. 

46 logger.info("Removing any existing output link at %s.", www_root_link) 

47 www_root_link.unlink(missing_ok=True) 

48 

49 logger.info("Installing website files to %s.", www_content) 

50 # Create directory for web content. 

51 www_content.mkdir(parents=True, exist_ok=True) 

52 # Copy static HTML/CSS/JS. 

53 html_source = Path.cwd() / "html" 

54 shutil.copytree(html_source, www_content, dirs_exist_ok=True) 

55 # Create directory for plots. 

56 plot_dir = www_content / "plots" 

57 plot_dir.mkdir(exist_ok=True) 

58 

59 logger.info("Linking %s to web content.", www_root_link) 

60 # Ensure parent directories of WEB_DIR exist. 

61 www_root_link.parent.mkdir(parents=True, exist_ok=True) 

62 # Create symbolic link to web directory. 

63 # NOTE: While good for space, it means `cylc clean` removes output. 

64 www_root_link.symlink_to(www_content) 

65 

66 

67def construct_index(www_content: Path): 

68 """Construct the plot index.""" 

69 plots_dir = www_content / "plots" 

70 index = {} 

71 # Loop over all diagnostics and append to index. 

72 for metadata_file in plots_dir.glob("**/*/meta.json"): 

73 try: 

74 with open(metadata_file, "rt", encoding="UTF-8") as fp: 

75 plot_metadata = json.load(fp) 

76 

77 category = plot_metadata["category"] 

78 case_date = plot_metadata.get("case_date", "") 

79 relative_url = str(metadata_file.parent.relative_to(plots_dir)) 

80 

81 record = { 

82 category: { 

83 case_date if case_date else "Aggregation": { 

84 relative_url: plot_metadata["title"].strip() 

85 } 

86 } 

87 } 

88 except (json.JSONDecodeError, KeyError, TypeError) as err: 

89 logging.error("%s is invalid, skipping.\n%s", metadata_file, err) 

90 continue 

91 index = combine_dicts(index, record) 

92 

93 # Sort index of diagnostics. 

94 index = sort_dict(index) 

95 

96 # Write out website index. 

97 with open(plots_dir / "index.json", "wt", encoding="UTF-8") as fp: 

98 json.dump(index, fp, indent=2) 

99 

100 

101def bust_cache(www_content: Path): 

102 """Add a unique query string to static requests to avoid stale caches. 

103 

104 We only need to do this for static resources referenced from the index page, 

105 as each plot already uses a unique filename based on the recipe. 

106 """ 

107 # Search and replace the string "CACHEBUSTER". 

108 CACHEBUSTER = str(int(time.time())) 

109 with open(www_content / "index.html", "r+t") as fp: 

110 content = fp.read() 

111 new_content = content.replace("CACHEBUSTER", CACHEBUSTER) 

112 fp.seek(0) 

113 fp.truncate() 

114 fp.write(new_content) 

115 

116 # Move plots directory so it has a unique name. 

117 os.rename(www_content / "plots", www_content / f"plots-{CACHEBUSTER}") 

118 

119 

120def update_workflow_status(www_content: Path): 

121 """Update the workflow status on the front page of the web interface.""" 

122 with open(www_content / "placeholder.html", "r+t") as fp: 

123 content = fp.read() 

124 finish_time = time.strftime("%Y-%m-%d %H:%M", time.localtime()) 

125 status = f"Completed at {finish_time} using CSET v{version('CSET')}" 

126 new_content = content.replace( 

127 '<p id="workflow-status">Unknown</p>', 

128 f'<p id="workflow-status">{status}</p>', 

129 ) 

130 fp.seek(0) 

131 fp.truncate() 

132 fp.write(new_content) 

133 

134 

135def copy_rose_config(www_content: Path): 

136 """Copy the rose-suite.conf file to add to output web directory.""" 

137 rose_suite_conf = Path(os.environ["CYLC_WORKFLOW_RUN_DIR"]) / "rose-suite.conf" 

138 web_conf_file = www_content / "rose-suite.conf" 

139 shutil.copyfile(rose_suite_conf, web_conf_file) 

140 

141 

142def run(): 

143 """Do the final steps to finish the website.""" 

144 # Strip trailing slashes in case they have been added in the config. 

145 # Otherwise they break the symlinks. 

146 www_root_link = Path(os.environ["WEB_DIR"].rstrip("/")) 

147 www_content = Path(os.environ["CYLC_WORKFLOW_SHARE_DIR"] + "/web") 

148 

149 install_website_skeleton(www_root_link, www_content) 

150 copy_rose_config(www_content) 

151 construct_index(www_content) 

152 bust_cache(www_content) 

153 update_workflow_status(www_content) 

154 

155 

156if __name__ == "__main__": # pragma: no cover 

157 run()