PHYEX 0.9.5
PHYsique EXternalisée
Loading...
Searching...
No Matches
phyex_transform.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Transformation of a PHYEX source file by pyfortool, executed at build time
4(see the top-level CMakeLists.txt, PHYEX_TRANSFORMATION option):
5 phyex_transform.py --transformation <name> [--pyfortool <exe>] <input> <output>
6
7The new source code is generated by pyfortool with arguments depending on the
8transformation, complemented by '--optsByEnv PYFT_OPTS' if the PYFT_OPTS
9environment variable is not empty. It is written in a temporary file which is
10renamed into <output> when the transformation succeeds.
11
12The list of available transformations is printed with --list.
13"""
14
15import argparse
16import os
17import subprocess
18import sys
19
20# Transformation name -> pyfortool arguments
21# Transformation order matters
22TRANSFORMATIONS = {
23 'IAL': ['--wrapH', '--shugradTransfo', '--mnhExpand', '--removeACC', '--splitLongLines'],
24 # IAL without the transformation of shuman/gradient functions into subroutine calls
25 'IAL_NOSHUGRAD': ['--wrapH', '--mnhExpand', '--removeACC', '--splitLongLines'],
26 # IAL without the expansion of the mnh_expand directives (code kept in array syntax)
27 'IAL_NOEXPAND': ['--wrapH', '--shugradTransfo', '--removeACC', '--splitLongLines'],
28}
29
30# Name of the environment variable containing additional pyfortool arguments
31OPTS_ENV = 'PYFT_OPTS'
32
33
34def main():
35 parser = argparse.ArgumentParser(description=__doc__,
36 formatter_class=argparse.RawDescriptionHelpFormatter)
37 parser.add_argument('--list', action='store_true',
38 help='Print the available transformations (;-separated) and exit')
39 parser.add_argument('--transformation', choices=list(TRANSFORMATIONS),
40 help='Transformation to apply')
41 parser.add_argument('--pyfortool', default='pyfortool', help='pyfortool executable')
42 parser.add_argument('--unset-pythonpath', action='store_true',
43 help='Unset PYTHONPATH when running pyfortool')
44 parser.add_argument('input', nargs='?', help='Original source file')
45 parser.add_argument('output', nargs='?', help='Transformed source file')
46 args = parser.parse_args()
47
48 if args.list:
49 print(';'.join(TRANSFORMATIONS))
50 return 0
51 if args.transformation is None or args.input is None or args.output is None:
52 parser.error('--transformation, input and output are needed')
53
54 outdir = os.path.dirname(os.path.abspath(args.output))
55 os.makedirs(outdir, exist_ok=True)
56 stem, ext = os.path.splitext(os.path.basename(args.output))
57 tmp = os.path.join(outdir, f'{stem}.pyft_tmp{ext}')
58 if os.path.exists(tmp):
59 os.remove(tmp)
60
61 cmd = [args.pyfortool] + TRANSFORMATIONS[args.transformation]
62 if os.environ.get(OPTS_ENV, ''):
63 cmd += ['--optsByEnv', OPTS_ENV]
64 cmd += [args.input, tmp]
65 env = None
66 if args.unset_pythonpath:
67 env = dict(os.environ)
68 env.pop('PYTHONPATH', None)
69 try:
70 status = subprocess.run(cmd, env=env, check=False).returncode
71 except OSError as err:
72 print(f'phyex_transform: unable to execute {cmd[0]}: {err}', file=sys.stderr)
73 status = 1
74 if status != 0 or not os.path.exists(tmp):
75 print(f'phyex_transform: transformation {args.transformation} of {args.input} failed: ' +
76 ' '.join(cmd), file=sys.stderr)
77 if os.path.exists(tmp):
78 os.remove(tmp)
79 return status or 1
80
81 os.replace(tmp, args.output)
82 return 0
83
84
85if __name__ == '__main__':
86 sys.exit(main())