forked from rolfhm/fyppscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfyppscript.py
More file actions
executable file
·99 lines (78 loc) · 2.46 KB
/
fyppscript.py
File metadata and controls
executable file
·99 lines (78 loc) · 2.46 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
from argparse import ArgumentParser
from pathlib import Path
import fypp
def input_parser():
"""
Use argparse to parse command line arguments
"""
parser = ArgumentParser()
parser.add_argument(
"src_dir",
help = "directory to search for fypp files",
nargs = 1,
type = str,
metavar = 'src dir',
)
parser.add_argument(
"-m",
"--modules",
help = "modules needed by fypp to process any files",
nargs = "*",
type = str,
default = [],
)
parser.add_argument(
"-M",
"--module-directories",
help = "directories to search for modules",
nargs = "*",
type = str,
default = [],
)
parser.add_argument(
"-f",
"--force",
help = "Process all fypp files, even if there are newer .F90 files",
action = "store_true"
)
args = parser.parse_args()
return args
def process_dir(directory, modules=None, module_directories=None, force=False):
"""
Find all fypp files in directory and its subdirectories
and process them with Fypp.
"""
#Create Path object from directory
fyppdir = Path(directory).resolve()
#Generate FyppOptions based on input
options = fypp.FyppOptions()
options.modules = modules
options.moduledirs = module_directories
#Generate fypp tool
tool = fypp.Fypp(options)
#Loop over all fypp files in directory and its subdirectories
for fyppfile in fyppdir.rglob('*.fypp'):
#Generate a Path to a .F90 file in the same directory
fortranfile = fyppfile.with_suffix('.F90')
if force or (not fortranfile.exists()):
#Always process if force is true or the Fortran file does not exist
process = True
else:
#Else, check if the fypp file was modified after the fortran file
process = fyppfile.stat().st_mtime >= fortranfile.stat().st_mtime
#If true, process the fyppfile and write to fortranfile
if process:
print('Process: ', fyppfile)
tool.process_file(str(fyppfile), str(fortranfile))
else:
print('Skip: ', fyppfile)
print()
def main():
"""
Read input directory from command line and call fypp script
"""
args = input_parser()
process_dir(args.src_dir[0], args.modules, args.module_directories, args.force)
if __name__ == "__main__":
main()