2 #-------------------------------------------------------------------------------
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 #-------------------------------------------------------------------------------
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
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/>.
35 # Run a freefoam job in background and redirect the output to a log-file.
37 #------------------------------------------------------------------------------
39 """Usage: freefoam@PY_SCRIPT_SUFFIX@ job [-s] [-p] [-log <logfile>] [-fg]
40 [-h, -help] <application> [OPTIONS]
42 Run a freefoam job in background and redirect the output to a log-file.
46 -s Also send output to the terminal
47 -p Parallel run of processors
48 -log <logfile> Specify logfile name for output, defaults to 'log'
49 -fg Run the job in the foreground (i.e. don't background it)
50 -h, -help Display this help message
51 <application> The FreeFOAM application to run
52 [OPTIONS] Arguments and options to be passed to the application
61 # want to be future proof
62 sys
.path
.insert(0, '@FOAM_PYTHON_DIR@')
63 from FreeFOAM
.compat
import *
82 sys
.stderr
.write('Error: The -log option requires an argument\n')
89 elif a
== '-h' or a
== '-help':
93 sys
.stderr
.write('Error: unknown option "%s"\n'%a
)
94 sys
.stderr
.write(__doc__
+'\n')
102 sys
.stderr
.write("Error: No application specified")
103 sys
.stderr
.write(__doc__
+'\n')
106 # now look for the -case option in the remaining arguments
108 for i
, a
in enumerate(args
):
114 # if we are to run in the background, fork (MUST BE FIRST!)
118 # this is the parent process
121 # this is the child process continuing here (if we forked)
123 # create the log file
125 logFile
= FreeFOAM
.util
.Tee(logName
, 'wt')
127 logFile
= open(logName
, 'wt')
129 # create the runner instance
131 runner
= FreeFOAM
.run
.ParallelRunner()
133 runner
= FreeFOAM
.run
.Runner()
135 # run the thing (needs a bit of hackery...)
136 runner
.run(application
, case
=case
, parallel
=parallel
, args
=args
,
137 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
, background
=True)
139 # retrieve the output and write it to the log file as long as the thing runs
141 lines
= runner
.getProcess().stdout
.readlines()
142 if len(lines
) and not isinstance(lines
[0], str):
143 lines
= list(map(lambda l
: l
.decode(), lines
))
144 logFile
.writelines(lines
)
145 returncode
= runner
.poll()
146 if returncode
!= None:
151 # ------------------- vim: set sw=3 sts=3 ft=python et: ------------ end-of-file