3 # Copyright (C) 2010-2013 CEA/DEN
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.
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.
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
25 def show_progress(logger, top, delai, ss=""):
26 """shortcut function to display the progression
28 :param logger Logger: The logging instance
29 :param top int: the number to display
30 :param delai int: the number max
31 :param ss str: the string to display
33 logger.write("\r%s\r%s %s / %s " % ((" " * 30), ss, top, (delai - top)), 4,
37 def write_back(logger, message, level):
38 """shortcut function to write at the begin of the line
40 :param logger Logger: The logging instance
41 :param message str: the text to display
42 :param level int: the level of verbosity
44 logger.write("\r%s\r%s" % ((" " * 40), message), level)
48 def launch_command(cmd, logger, cwd, args=[], log=None):
51 logger.write("launch: %s\n" % cmd, 5, screenOnly=True)
54 prs = subprocess.Popen(cmd,
57 stderr=subprocess.STDOUT,
59 executable='/bin/bash')
64 def batch(cmd, logger, cwd, args=[], log=None, delai=20, sommeil=1):
65 proc = launch_command(cmd, logger, cwd, args, log)
67 sys.stdout.softspace = True
69 while proc.poll() is None:
70 if time.time() - begin >= 1:
71 show_progress(logger, top, delai, "batch:")
73 logger.write("batch: time out KILL\n", 3)
75 os.kill(proc.pid, signal.SIGTERM)
83 write_back(logger, "batch: exit (%s)\n" % str(proc.returncode), 5)
84 return (proc.returncode == 0), top
86 # Launch a salome process
87 # -----------------------
88 def batch_salome(cmd, logger, cwd, args, getTmpDir,
89 pendant="SALOME_Session_Server", fin="killSalome.py",
90 log=None, delai=20, sommeil=1, delaiapp=0):
92 beginTime = time.time()
93 launch_command(cmd, logger, cwd, args, log)
98 # first launch salome (looking for .pidict file)
101 tmp_dir = getTmpDir()
102 while (not found and top < delaiapp):
103 if os.path.exists(tmp_dir):
104 listFile = os.listdir(tmp_dir)
108 for file_name in listFile:
109 if file_name.endswith("pidict"):
110 # sometime we get a old file that will be removed by runSalome.
111 # So we test that we can read it.
114 statinfo = os.stat(os.path.join(tmp_dir, file_name))
115 currentTime = statinfo.st_mtime
118 if currentTime and currentTime > beginTime:
120 file_ = open(os.path.join(tmp_dir, file_name), "r")
121 process_ids = pickle.load(file_)
123 for process_id in process_ids:
124 for __, cmd in process_id.items():
127 pidictFile = file_name
133 show_progress(logger, top, delaiapp, "launching salome or appli:")
137 write_back(logger, "batch_salome: started\n", 5)
139 logger.write("batch_salome: FAILED to launch salome or appli\n", 3)
142 # salome launched run the script
146 show_progress(logger, top, delai, "running salome or appli:")
148 if not os.access(os.path.join(tmp_dir, pidictFile), os.F_OK):
149 write_back(logger, "batch_salome: exit\n", 5)
152 # timeout kill the test
154 logger.write("batch_salome: time out KILL\n", 3)