DOC: Download instructions for ThirdParty libs
[freefoam.git] / bin / freefoam-job.py.in
blob5313465ef440051b3b906c3ffebc45a39d63c851
1 #!@PYTHON_EXECUTABLE@
2 #-------------------------------------------------------------------------------
3 # ______ _ ____ __ __
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 #-------------------------------------------------------------------------------
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 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
26 # for more details.
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
32 # Script
33 # freefoam-job
35 # Description
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.
45 Options
46 -------
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
55 """
57 # want to be future proof
58 from FreeFOAM.compat import *
60 import FreeFOAM.util
61 import FreeFOAM.run
62 import os
63 import sys
64 import subprocess
66 # parse options
67 screen = False
68 parallel = False
69 logName = 'log'
70 foreground = False
71 application = None
72 args = sys.argv[1:]
73 while len(args) > 0:
74 a = args[0]
75 if a == '-s':
76 screen = True
77 del args[0]
78 elif a == '-p':
79 parallel = True
80 del args[0]
81 elif a == '-log':
82 if len(args) < 2:
83 sys.stderr.write('Error: The -log option requires an argument\n')
84 sys.exit(1)
85 logName = args[1]
86 del args[0:2]
87 elif a == '-fg':
88 foreground = True
89 del args[0]
90 elif a == '-h' or a == '-help':
91 print(__doc__)
92 sys.exit(0)
93 elif a[0] == '-':
94 sys.stderr.write('Error: unknown option "%s"\n'%a)
95 sys.stderr.write(__doc__+'\n')
96 sys.exit(1)
97 else:
98 application = a
99 del args[0]
100 break
102 if not application:
103 sys.stderr.write("Error: No application specified")
104 sys.stderr.write(__doc__+'\n')
105 sys.exit(1)
107 # now look for the -case option in the remaining arguments
108 case = '.'
109 for i, a in enumerate(args):
110 if a == '-case':
111 case = args[i+1]
112 del args[i:i+2]
113 break
115 # if we are to run in the background, fork (MUST BE FIRST!)
116 if not foreground:
117 pid = os.fork()
118 if pid:
119 # this is the parent process
120 sys.exit(0)
122 # this is the child process continuing here (if we forked)
124 # create the log file
125 if screen:
126 logFile = FreeFOAM.util.Tee(logName, 'wt')
127 else:
128 logFile = open(logName, 'wt')
130 # create the runner instance
131 if parallel:
132 runner = FreeFOAM.run.ParallelRunner()
133 else:
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
141 while True:
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:
148 break
150 sys.exit(returncode)
152 # ------------------- vim: set sw=3 sts=3 ft=python et: ------------ end-of-file