Coverage for src/CSET/cset_workflow/app/send_email/bin/send_email.py: 100%
25 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-05 21:08 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-05 21:08 +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.
16"""
17Send a notification email linking to the output page.
19Sends a notification email to the user of the workflow when it has completed,
20with a link to the output webpage.
21"""
23import os
24import subprocess
27def get_home_page_address():
28 """Derive the address of the output page based directory structure."""
29 try:
30 web_dir = os.environ["WEB_DIR"]
31 except KeyError as err:
32 raise ValueError("WEB_DIR not set.") from err
33 try:
34 web_addr = os.environ["WEB_ADDR"]
35 except KeyError as err:
36 raise ValueError("WEB_ADDR not set.") from err
38 path = web_dir.split("/public_html/")
39 if len(path) == 1:
40 path = web_dir.split("/Public/")
41 if len(path) == 1:
42 raise ValueError("Cannot determine web address.")
43 base_address = web_addr.strip("/")
44 return f"{base_address}/{path[-1]}"
47def run():
48 """Run workflow script."""
49 subject = "CSET webpage ready"
50 try:
51 body = f"The webpage for your run of CSET is now ready. You can view it here:\n{get_home_page_address()}"
52 except ValueError:
53 body = "The webpage for your run of CSET is now ready, though the address could not be determined.\nCheck that WEB_ADDR and WEB_DIR are set correctly, then consider filing a bug report at https://github.com/MetOffice/CSET"
54 subprocess.run(
55 f'printf "{body}" | mail -s "{subject}" -S "from=notifications" "$USER"',
56 check=True,
57 shell=True,
58 )
61if __name__ == "__main__": # pragma: no cover
62 run()