Skip to content

Commit f879a9f

Browse files
committed
Error handling added to the python scripts
1 parent e6e2e68 commit f879a9f

9 files changed

+514
-916
lines changed

FinaliseWebpages.py

+36-24
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,49 @@
44
import glob
55
from common import CommonWebPageFuncs
66

7-
#Initialise the common webpage functions (cw) which in turn initialises all relevant environment variables.
7+
# Get the logger from CommonWebPageFuncs
88
cw = CommonWebPageFuncs()
9+
logger = cw.logger
910

10-
#Checksout validation webpage branch
11-
cw.checkout_validation_webpage_branch()
11+
try:
12+
# Checks out validation webpage branch
13+
cw.checkout_validation_webpage_branch()
1214

13-
# More specific stuff here
14-
run_dir = os.path.join(cw.ValidationPath, "Webpage", cw.GIT_COMMIT)
15+
# More specific stuff here
1516

16-
for job_dir in glob.glob(f"{run_dir}/[0-9]*/"):
17-
try:
18-
jobnum = int(job_dir.rsplit('/', 2)[-2])
19-
except:
20-
#this happens if we have a directory that starts with a number, but has letters in it
21-
continue
22-
#If the job directory doesn't exist, die.
23-
if not os.path.isdir(job_dir):
24-
break # Changed from boolean as 'break' makes code easier to use imo.
17+
run_dir = os.path.join(cw.ValidationPath, "Webpage", cw.GIT_COMMIT)
2518

26-
with open(os.path.join(job_dir, "index.html"), "r") as f:
27-
colour = f.readlines()[-1]
19+
# Check if the run directory exists
20+
if not os.path.isdir(run_dir):
21+
raise FileNotFoundError(f"Run directory '{run_dir}' does not exist")
2822

29-
with open(os.path.join(run_dir, "index.html"), "r") as f:
30-
webpage_content = f.read()
23+
for job_dir in glob.glob(f"{run_dir}/[0-9]*/"):
24+
try:
25+
jobnum = int(job_dir.rsplit('/', 2)[-2])
26+
except ValueError:
27+
# This happens if we have a directory that starts with a number but has letters in it
28+
continue
3129

32-
jobnumpad = f"{jobnum:04d}"
33-
webpage_content = webpage_content.replace(f"'#00FFFF'COLOUR{jobnumpad}", colour) # Equivalent to the sed command used in the shell script.
30+
# If the job directory doesn't exist, skip it
31+
if not os.path.isdir(job_dir):
32+
continue
3433

35-
#Put it all together.
36-
with open(os.path.join(run_dir, "index.html"), "w") as f:
37-
f.write(webpage_content)
34+
with open(os.path.join(job_dir, "index.html"), "r") as f:
35+
colour = f.readlines()[-1]
3836

37+
with open(os.path.join(run_dir, "index.html"), "r") as f:
38+
webpage_content = f.read()
3939

40-
cw.update_webpage()
40+
jobnumpad = f"{jobnum:04d}"
41+
webpage_content = webpage_content.replace(f"'#00FFFF'COLOUR{jobnumpad}", colour) # Equivalent to the sed command used in the shell script.
42+
43+
# Put it all together.
44+
with open(os.path.join(run_dir, "index.html"), "w") as f:
45+
f.write(webpage_content)
46+
47+
cw.update_webpage()
48+
49+
except FileNotFoundError as e:
50+
logger.error(f"An unexpected FileNotFoundError occured: {e}")
51+
except Exception as e:
52+
logger.error(f"An unexpected error occurred: {e}")

0 commit comments

Comments
 (0)