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
28 def show_progress(logger, top, delai, ss=""):
29 """shortcut function to display the progression
31 :param logger Logger: The logging instance
32 :param top int: the number to display
33 :param delai int: the number max
34 :param ss str: the string to display
36 logger.write("\r%s\r%s %s / %s " % ((" " * 30), ss, top, (delai - top)), 4,
40 def write_back(logger, message, level):
41 """shortcut function to write at the begin of the line
43 :param logger Logger: The logging instance
44 :param message str: the text to display
45 :param level int: the level of verbosity
47 logger.write("\r%s\r%s" % ((" " * 40), message), level)
51 def launch_command(cmd, logger, cwd, args=[], log=None):
54 logger.write("launch: %s\n" % cmd, 5, screenOnly=True)
59 if src.architecture.is_windows():
60 prs = subprocess.Popen(cmd,
63 stderr=subprocess.STDOUT,
67 prs = subprocess.Popen(cmd,
70 stderr=subprocess.STDOUT,
72 executable='/bin/bash')
79 def batch(cmd, logger, cwd, args=[], log=None, delai=20, sommeil=1):
80 proc = launch_command(cmd, logger, cwd, args, log)
82 sys.stdout.softspace = True
84 while proc.poll() is None:
85 if time.time() - begin >= 1:
86 show_progress(logger, top, delai, "batch:")
88 logger.write("batch: time out KILL\n", 3)
90 os.kill(proc.pid, signal.SIGTERM)
98 write_back(logger, "batch: exit (%s)\n" % str(proc.returncode), 5)
99 return (proc.returncode == 0), top
101 # Launch a salome process
102 # -----------------------
103 def batch_salome(cmd, logger, cwd, args, getTmpDir,
104 pendant="SALOME_Session_Server", fin="killSalome.py",
105 log=None, delai=20, sommeil=1, delaiapp=0):
107 beginTime = time.time()
108 launch_command(cmd, logger, cwd, args, log)
113 # first launch salome (looking for .pidict file)
116 tmp_dir = getTmpDir()
117 while (not found and top < delaiapp):
118 if os.path.exists(tmp_dir):
119 listFile = os.listdir(tmp_dir)
123 for file_name in listFile:
124 if file_name.endswith("pidict"):
125 # sometime we get a old file that will be removed by runSalome.
126 # So we test that we can read it.
129 statinfo = os.stat(os.path.join(tmp_dir, file_name))
130 currentTime = statinfo.st_mtime
133 if currentTime and currentTime > beginTime:
135 file_ = open(os.path.join(tmp_dir, file_name), "r")
136 process_ids = pickle.load(file_)
138 for process_id in process_ids:
139 for __, cmd in process_id.items():
142 pidictFile = file_name
148 show_progress(logger, top, delaiapp, "launching salome or appli:")
152 write_back(logger, "batch_salome: started\n", 5)
154 logger.write("batch_salome: FAILED to launch salome or appli\n", 3)
157 # salome launched run the script
161 show_progress(logger, top, delai, "running salome or appli:")
163 if not os.access(os.path.join(tmp_dir, pidictFile), os.F_OK):
164 write_back(logger, "batch_salome: exit\n", 5)
167 # timeout kill the test
169 logger.write("batch_salome: time out KILL\n", 3)