forked from ImperialCollegeLondon/multifluids_icferst
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_check_options.py
executable file
·86 lines (63 loc) · 1.88 KB
/
make_check_options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import re
import glob
import hashlib
from io import StringIO
def safe_decode(x):
return x.decode('utf8') if type(x) is bytes else x
# File header
header="""
subroutine check_options
"""
footer="""
end subroutine check_options
"""
outfile='preprocessor/check_options.F90'
# get sha1 digest of existing generated file. Can't use 'rw' here
# because it updates the modtime of the file, which we're trying to
# avoid doing.
orig=hashlib.sha1()
try:
f=open(outfile, 'r')
orig.update(f.read().encode("utf8"))
except IOError:
pass
else:
f.close()
# Now read module files to generate potential new data
output=StringIO()
output.write(safe_decode(header))
# List of fortran source files.
fortran_files=glob.glob("*/*.F")+glob.glob("*/*.F90")
module_re=re.compile(r"^\s*module\s+(\w+)\s*$",re.IGNORECASE|re.MULTILINE)
module_list=[]
for filename in fortran_files:
fortran=open(filename,"rb").read().decode("utf-8")
modules=module_re.findall(fortran)
for module in modules:
if re.search(r"^\s*subroutine\s+"+module+"_check_options\S*\s*$",\
fortran,\
re.IGNORECASE|re.MULTILINE):
module_list.append(module)
for module in module_list:
output.write(safe_decode(" use "+module+", only: "+module+"_check_options\n"))
# Ensure that the subroutine is legal in the trivial case.
output.write(safe_decode("""
continue
"""))
for module in module_list:
output.write(safe_decode(" call "+module+"_check_options\n"))
output.write(safe_decode(footer))
new=hashlib.sha1()
new.update(output.getvalue().encode("utf8"))
# Only write file if sha1sums differ
if new.digest() != orig.digest():
try:
f=open(outfile, 'w')
f.write(output.getvalue())
except IOError:
# Fixme, this should fail better
pass
else:
f.close()
output.close()