2 #-------------------------------------------------------------------------------
4 # | ____| _| |_ / __ \ /\ | \/ |
5 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
6 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
7 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
8 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
10 # FreeFOAM: The Cross-Platform CFD Toolkit
12 # Copyright (C) 2008-2010 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 2 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, write to the Free Software Foundation,
30 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 # Run a freefoam job in background and redirect the output to a log-file.
38 #------------------------------------------------------------------------------
40 """Usage: freefoam@PY_SCRIPT_SUFFIX@ job [-s] [-p] [-log <logfile>] [-fg]
41 [-h, -help] <application> [OPTIONS]
43 Run a freefoam job in background and redirect the output to a log-file.
47 -s Also send output to the terminal
48 -p Parallel run of processors
49 -log <logfile> Specify logfile name for output, defaults to 'log'
50 -fg Run the job in the foreground (i.e. don't background it)
51 -h, -help Display this help message
52 <application> The FreeFOAM application to run
53 [OPTIONS] Arguments and options to be passed to the application
57 # want to be future proof
58 from FreeFOAM
.compat
import *
83 sys
.stderr
.write('Error: The -log option requires an argument\n')
90 elif a
== '-h' or a
== '-help':
94 sys
.stderr
.write('Error: unknown option "%s"\n'%a
)
95 sys
.stderr
.write(__doc__
+'\n')
103 sys
.stderr
.write("Error: No application specified")
104 sys
.stderr
.write(__doc__
+'\n')
107 # now look for the -case option in the remaining arguments
109 for i
, a
in enumerate(args
):
115 # if we are to run in the background, fork (MUST BE FIRST!)
119 # this is the parent process
122 # this is the child process continuing here (if we forked)
124 # create the log file
126 logFile
= FreeFOAM
.util
.Tee(logName
, 'wt')
128 logFile
= open(logName
, 'wt')
130 # create the runner instance
132 runner
= FreeFOAM
.run
.ParallelRunner()
134 runner
= FreeFOAM
.run
.Runner()
136 # run the thing (needs a bit of hackery...)
137 runner
.run(application
, case
=case
, parallel
=parallel
, args
=args
,
138 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
, background
=True)
140 # retrieve the output and write it to the log file as long as the thing runs
142 lines
= runner
.getProcess().stdout
.readlines()
143 if len(lines
) and not isinstance(lines
[0], str):
144 lines
= list(map(lambda l
: l
.decode(), lines
))
145 logFile
.writelines(lines
)
146 returncode
= runner
.poll()
147 if returncode
!= None:
152 # ------------------- vim: set sw=3 sts=3 ft=python et: ------------ end-of-file