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()
49 print(
';'.join(TRANSFORMATIONS))
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')
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):
61 cmd = [args.pyfortool] + TRANSFORMATIONS[args.transformation]
62 if os.environ.get(OPTS_ENV,
''):
63 cmd += [
'--optsByEnv', OPTS_ENV]
64 cmd += [args.input, tmp]
66 if args.unset_pythonpath:
67 env = dict(os.environ)
68 env.pop(
'PYTHONPATH',
None)
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)
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):
81 os.replace(tmp, args.output)