Salome HOME
Add the test command first version
[tools/sat.git] / src / fork.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 import os
20 import sys
21 import time
22 import pickle
23 import subprocess
24
25 # Display progress
26 # ----------------
27 def show_progress(logger, top, delai, ss=""):
28     logger.write("\r%s\r%s %s / %s " % ((" " * 30), ss, top, (delai - top)), 4,
29                  False)
30     logger.flush()
31
32 def write_back(logger, message, level):
33     logger.write("\r%s\r%s" % ((" " * 40), message), level)
34
35 # Launch command
36 # --------------
37 def launch_command(cmd, logger, cwd, args=[], log=None):
38     if log:
39         log = file(log, "a")
40     logger.write("launch: %s\n" % cmd, 5, screenOnly=True)
41     for arg in args:
42         cmd += " " + arg
43     prs = subprocess.Popen(cmd,
44                            shell=True,
45                            stdout=log,
46                            stderr=subprocess.STDOUT,
47                            cwd=cwd,
48                            executable='/bin/bash')
49     return prs
50
51 # Launch a batch
52 # --------------
53 def batch(cmd, logger, cwd, args=[], log=None, delai=20, sommeil=1):
54     proc = launch_command(cmd, logger, cwd, args, log)
55     top = 0
56     sys.stdout.softspace = True
57     begin = time.time()
58     while proc.poll() is None:
59         if time.time() - begin >= 1:
60             show_progress(logger, top, delai, "batch:")
61             if top == delai:
62                 logger.write("batch: time out KILL\n", 3)
63                 import signal
64                 os.kill(proc.pid, signal.SIGTERM)
65                 break
66             else:
67                 begin = time.time()
68                 time.sleep(sommeil)
69                 top += 1
70         sys.stdout.flush()
71     else:
72         write_back(logger, "batch: exit (%s)\n" % str(proc.returncode), 5)
73     return (proc.returncode == 0), top
74
75 # Launch a salome process
76 # -----------------------
77 def batch_salome(cmd, logger, cwd, args, getTmpDir,
78     pendant="SALOME_Session_Server", fin="killSalome.py",
79     log=None, delai=20, sommeil=1, delaiapp=0):
80
81     beginTime = time.time()
82     launch_command(cmd, logger, cwd, args, log)
83
84     if delaiapp == 0:
85         delaiapp = delai
86
87     # first launch salome (looking for .pidict file)
88     top = 0
89     found = False
90     tmp_dir = getTmpDir()
91     while (not found and top < delaiapp):
92         if os.path.exists(tmp_dir):
93             listFile = os.listdir(tmp_dir)
94         else:
95             listFile = []
96
97         for file_name in listFile:
98             if file_name.endswith("pidict"):
99                 # sometime we get a old file that will be removed by runSalome.
100                 # So we test that we can read it.
101                 currentTime = None
102                 try:
103                     statinfo = os.stat(os.path.join(tmp_dir, file_name))
104                     currentTime = statinfo.st_mtime
105                 except: pass
106
107                 if currentTime and currentTime > beginTime:
108                     try:
109                         file_ = open(os.path.join(tmp_dir, file_name), "r")
110                         process_ids = pickle.load(file_)
111                         file_.close()
112                         for process_id in process_ids:
113                             for __, cmd in process_id.items():
114                                 if cmd == [pendant]:
115                                     found = True
116                                     pidictFile = file_name
117                     except:
118                         file_.close()
119
120         time.sleep(sommeil)
121         top += 1
122         show_progress(logger, top, delaiapp, "launching salome or appli:")
123
124     # continue or not
125     if found:
126         write_back(logger, "batch_salome: started\n", 5)
127     else:
128         logger.write("batch_salome: FAILED to launch salome or appli\n", 3)
129         return False, -1
130
131     # salome launched run the script
132     top = 0
133     code = None
134     while code is None:
135         show_progress(logger, top, delai, "running salome or appli:")
136
137         if not os.access(os.path.join(tmp_dir, pidictFile), os.F_OK):
138             write_back(logger, "batch_salome: exit\n", 5)
139             code = True
140         elif top >= delai:
141             # timeout kill the test
142             os.system(fin)
143             logger.write("batch_salome: time out KILL\n", 3)
144             code = False
145         else:
146             # still waiting
147             time.sleep(sommeil)
148             top = top + 1
149
150     return code, top