ENH: Update FreeFOAM contributions to GPL v3
[freefoam.git] / bin / freefoam.py.in
blob3d85cc47f7fb02576dc27563ca305f46111f778e
1 #!@PYTHON_EXECUTABLE@
2 #-------------------------------------------------------------------------------
3 # ______ _ ____ __ __
4 # | ____| _| |_ / __ \ /\ | \/ |
5 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
6 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
7 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
8 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
10 # FreeFOAM: The Cross-Platform CFD Toolkit
12 # Copyright (C) 2008-2012 Michael Wild <themiwi@users.sf.net>
13 # Gerber van der Graaf <gerber_graaf@users.sf.net>
14 #-------------------------------------------------------------------------------
15 # License
16 # This file is part of FreeFOAM.
18 # FreeFOAM is free software: you can redistribute it and/or modify it
19 # under the terms of the GNU General Public License as published by the
20 # Free Software Foundation, either version 3 of the License, or (at your
21 # option) any later version.
23 # FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
24 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 # for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with FreeFOAM. If not, see <http://www.gnu.org/licenses/>.
31 # Script
32 # freefoam
34 #------------------------------------------------------------------------------
36 """Usage: freefoam@PY_SCRIPT_SUFFIX@ [options] <appName> [<appOptions>]
38 Runs a specific version of a FreeFOAM executable with given arguments. Can also
39 be used for parallel runs.
41 Options
42 -------
43 -b, -basedir Use a different base directory to search for applications
44 (default is @FOAM_LIBEXEC_DIR@).
45 -P, -path Also use the system PATH variable (searched last).
46 -p, -print Instead of executing the command, print the command line that
47 would be used. Useful for command substitution for e.g. mpirun if
48 your system doesn't support the execve(2) (or similar) system
49 call.
50 -h, -help Display this help message.
51 -v, -version Display the FreeFOAM version and exit.
52 <appName> Name of the FreeFOAM application to run.
53 <appOptions> Options and arguments to be passed to <appName>.
55 Set FREEFOAM_PATH to specify an alternative list of paths (colon-separated)
56 where to search for applications.
58 If the -P option is supplied, the system PATH variable will be appended to the
59 search path.
61 """
63 import sys
64 # want to be future proof
65 sys.path.insert(0, '@FOAM_PYTHON_DIR@')
66 from FreeFOAM.compat import *
68 import FreeFOAM
69 import FreeFOAM.run
70 import FreeFOAM.path
72 # parse arguments (can't use optparse because of single-dash long options)
73 appName = None
74 useSysPath = False
75 baseDir = FreeFOAM.LIBEXEC_DIR
76 printOnly = False
77 args = sys.argv[1:]
78 while len(args) > 0:
79 a = args[0]
80 if a == '-b' or a == '-basedir':
81 if len(args) < 2:
82 sys.stderr.write('Error: The %s option requires an argument\n'%a)
83 sys.exit(1)
84 baseDir = args[1]
85 del args[0:2]
86 elif a == '-P' or a == '-path':
87 useSysPath = True
88 del args[0]
89 elif a == '-p' or a == '-print':
90 printOnly = True
91 del args[0]
92 elif a == '-v' or a == '-version':
93 print('FreeFOAM version '+FreeFOAM.VERSION_FULL)
94 sys.exit(0)
95 elif a == '-h' or a == '-help':
96 print(__doc__)
97 sys.exit(0)
98 else:
99 break
101 # get the application name and remove it from the argument list
102 if not len(args):
103 sys.stderr.write('Error: No FreeFOAM application specified\n')
104 sys.exit(1)
105 app = args[0]
106 del args[0]
108 # find -case and -parallel in arguments
109 case = None
110 if '-case' in args:
111 idx = args.index('-case')
112 if idx > len(args) - 2:
113 sys.stderr.write('Error: The -case option requires an argument\n')
114 sys.exit(1)
115 case = args[idx+1]
116 del args[idx:idx+2]
117 parallel = False
118 if '-parallel' in args:
119 parallel = True
120 args.remove('-parallel')
122 # construct search path
123 search_path = FreeFOAM.path.create_app_search_path(
124 basedir=baseDir, syspath=useSysPath)
126 # run the application
127 runner = FreeFOAM.run.Runner(search_path)
128 status = 0
129 try:
130 if printOnly:
131 print(runner.command_str(app, case=case, parallel=parallel, args=args))
132 else:
133 status = runner.run(app, case=case, parallel=parallel, args=args)
134 except FreeFOAM.run.ApplicationNotFoundError, e:
135 sys.stderr.write(str(e)+'\n')
136 status = 1
138 sys.exit(status)
140 # ------------------------- vim: set sw=3 sts=3 et: --------------- end-of-file