Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tesh: try harder to not loose the cwd between threads
[simgrid.git] / tools / tesh / tesh.py
index 5000d65..5ba29b8 100755 (executable)
@@ -5,7 +5,7 @@
 tesh -- testing shell
 ========================
 
-Copyright (c) 2012-2017. The SimGrid Team. All rights reserved.
+Copyright (c) 2012-2018. The SimGrid Team. All rights reserved.
 
 This program is free software; you can redistribute it and/or modify it
 under the terms of the license (GNU LGPL) which comes with this package.
@@ -39,14 +39,14 @@ if sys.version_info[0] == 3:
 else:
     raise "This program is expected to run with Python3 only"
 
-
-
 ##############
 #
 # Utilities
 #
 #
 
+def isWindows():
+    return sys.platform.startswith('win')
 
 # Singleton metaclass that works in Python 2 & 3
 # http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
@@ -65,7 +65,7 @@ SIGNALS_TO_NAMES_DICT = dict((getattr(signal, n), n) \
 
 
 #exit correctly
-def exit(errcode):
+def tesh_exit(errcode):
     #If you do not flush some prints are skipped
     sys.stdout.flush()
     #os._exit exit even when executed within a thread
@@ -74,7 +74,7 @@ def exit(errcode):
 
 def fatal_error(msg):
     print("[Tesh/CRITICAL] "+str(msg))
-    exit(1)
+    tesh_exit(1)
 
 
 #Set an environment variable.
@@ -98,6 +98,33 @@ except NameError:
     #py2
     FileNotFoundError = OSError
 
+##############
+#
+# Cleanup on signal
+#
+#
+
+# Global variable. Stores which process group should be killed (or None otherwise)
+pgtokill = None
+
+def kill_process_group(pgid):
+    if pgid is None: # Nobody to kill. We don't know who to kill on windows, or we don't have anyone to kill on signal handler
+        return
+
+    # print("Kill process group {}".format(pgid))
+    try:
+        os.killpg(pgid, signal.SIGTERM)
+    except OSError:
+        # os.killpg failed. OK. Some subprocesses may still be running.
+        pass
+
+def signal_handler(signal, frame):
+    print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal]))
+    if pgtokill is not None:
+        kill_process_group(pgtokill)
+    tesh_exit(5)
+
+
 
 ##############
 #
@@ -161,7 +188,7 @@ class TeshState(Singleton):
             t.acquire()
             t.release()
 
-#Command line object
+# Command line object
 class Cmd(object):
     def __init__(self):
         self.input_pipe = []
@@ -172,7 +199,8 @@ class Cmd(object):
         self.linenumber = -1
 
         self.background = False
-        self.cwd = None
+        # Python threads loose the cwd
+        self.cwd = os.getcwd()
 
         self.ignore_output = False
         self.expect_return = 0
@@ -224,7 +252,7 @@ class Cmd(object):
         except FileNotFoundError:
             print("Chdir to "+args[1]+" failed: No such file or directory")
             print("Test suite `"+FileReader().filename+"': NOK (system error)")
-            exit(4)
+            tesh_exit(4)
 
 
     #Run the Cmd if possible.
@@ -232,8 +260,6 @@ class Cmd(object):
     def run_if_possible(self):
         if self.can_run():
             if self.background:
-                #Python threads loose the cwd
-                self.cwd = os.getcwd()
                 lock = _thread.allocate_lock()
                 lock.acquire()
                 TeshState().add_thread(lock)
@@ -246,10 +272,8 @@ class Cmd(object):
 
 
     def _run(self, lock=None):
-        #Python threads loose the cwd
-        if self.cwd is not None:
-            os.chdir(self.cwd)
-            self.cwd = None
+        # Python threads loose the cwd
+        os.chdir(self.cwd)
 
         #retrocompatibility: support ${aaa:=.} variable format
         def replace_perl_variables(m):
@@ -289,23 +313,40 @@ class Cmd(object):
         args = shlex.split(self.args)
         #print (args)
 
+        global pgtokill
+
         try:
-            proc = subprocess.Popen(args, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
+            proc = subprocess.Popen(args, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, start_new_session=True)
+            try:
+                if not isWindows():
+                    pgtokill = os.getpgid(proc.pid)
+            except OSError:
+                # os.getpgid failed. OK. No cleanup.
+                pass
+        except PermissionError:
+            print("["+FileReader().filename+":"+str(self.linenumber)+"] Cannot start '"+args[0]+"': The binary is not executable.")
+            print("["+FileReader().filename+":"+str(self.linenumber)+"] Current dir: "+os.getcwd())
+            tesh_exit(3)            
+        except NotADirectoryError:
+            print("["+FileReader().filename+":"+str(self.linenumber)+"] Cannot start '"+args[0]+"': The path to binary does not exist.")
+            print("["+FileReader().filename+":"+str(self.linenumber)+"] Current dir: "+os.getcwd())
+            tesh_exit(3)
         except FileNotFoundError:
             print("["+FileReader().filename+":"+str(self.linenumber)+"] Cannot start '"+args[0]+"': File not found")
-            exit(3)
+            tesh_exit(3)
         except OSError as osE:
             if osE.errno == 8:
-                osE.strerror += "\nOSError: [Errno 8] Executed scripts should start with shebang line (like #!/bin/sh)"
+                osE.strerror += "\nOSError: [Errno 8] Executed scripts should start with shebang line (like #!/usr/bin/env sh)"
             raise osE
 
         cmdName = FileReader().filename+":"+str(self.linenumber)
         try:
             (stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout)
+            pgtokill = None
         except subprocess.TimeoutExpired:
             print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> timeout after "+str(self.timeout)+" sec)")
-            proc.kill()
-            exit(3)
+            kill_process_group(pgtokill)
+            tesh_exit(3)
 
         if self.output_display:
             print(stdout_data)
@@ -363,7 +404,7 @@ class Cmd(object):
                         f.write("> "+line+"\n")
                     f.close()
                     print("Obtained output kept as requested: "+os.path.abspath("obtained"))
-                exit(2)
+                tesh_exit(2)
 
         #print ((proc.returncode, self.expect_return))
 
@@ -371,11 +412,11 @@ class Cmd(object):
             if proc.returncode >= 0:
                 print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> returned code "+str(proc.returncode)+")")
                 if lock is not None: lock.release()
-                exit(2)
+                tesh_exit(2)
             else:
                 print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> got signal "+SIGNALS_TO_NAMES_DICT[-proc.returncode]+")")
                 if lock is not None: lock.release()
-                exit(-proc.returncode)
+                tesh_exit(-proc.returncode)
 
         if lock is not None: lock.release()
 
@@ -396,41 +437,47 @@ class Cmd(object):
 
 
 if __name__ == '__main__':
+    signal.signal(signal.SIGINT, signal_handler)
+    signal.signal(signal.SIGTERM, signal_handler)
 
     parser = argparse.ArgumentParser(description='tesh -- testing shell', add_help=True)
     group1 = parser.add_argument_group('Options')
     group1.add_argument('teshfile', nargs='?', help='Name of teshfile, stdin if omitted')
     group1.add_argument('--cd', metavar='some/directory', help='ask tesh to switch the working directory before launching the tests')
     group1.add_argument('--setenv', metavar='var=value', action='append', help='set a specific environment variable')
-    group1.add_argument('--cfg', metavar='arg', help='add parameter --cfg=arg to each command line')
-    group1.add_argument('--log', metavar='arg', help='add parameter --log=arg to each command line')
+    group1.add_argument('--cfg', metavar='arg', action='append', help='add parameter --cfg=arg to each command line')
+    group1.add_argument('--log', metavar='arg', action='append', help='add parameter --log=arg to each command line')
     group1.add_argument('--ignore-jenkins', action='store_true', help='ignore all cruft generated on SimGrid continous integration servers')
     group1.add_argument('--wrapper', metavar='arg', help='Run each command in the provided wrapper (eg valgrind)')
     group1.add_argument('--keep', action='store_true', help='Keep the obtained output when it does not match the expected one')
 
     try:
         options = parser.parse_args()
-    except:
-        exit(1)
+    except SystemExit:
+        tesh_exit(1)
 
     if options.cd is not None:
+        print("[Tesh/INFO] change directory to " + options.cd)
         os.chdir(options.cd)
 
     if options.ignore_jenkins:
         print("Ignore all cruft seen on SimGrid's continous integration servers")
+        # Note: regexps should match at the beginning of lines
         TeshState().ignore_regexps_common = [
-           re.compile("^profiling:"),
-           re.compile(".*WARNING: ASan doesn\'t fully support"),
-           re.compile("Unable to clean temporary file C:.*"),
-           re.compile(".*Configuration change: Set \'contexts/"),
-           re.compile(".*Picked up JAVA_TOOL_OPTIONS.*"),
-           re.compile("Picked up _JAVA_OPTIONS: .*"),
-
-           re.compile("==WARNING: ASan is ignoring requested __asan_handle_no_return: stack top:"),
-           re.compile("False positive error reports may follow"),
-           re.compile("For details see http://code.google.com/p/address-sanitizer/issues/detail?id=189"),
-
-           re.compile("Python runtime initialized with LC_CTYPE=C .*"),
+           re.compile(r"profiling:"),
+           re.compile(r"Unable to clean temporary file C:"),
+           re.compile(r".*Configuration change: Set 'contexts/"),
+           re.compile(r"Picked up JAVA_TOOL_OPTIONS: "),
+           re.compile(r"Picked up _JAVA_OPTIONS: "),
+           re.compile(r"==[0-9]+== ?WARNING: ASan doesn't fully support"),
+           re.compile(r"==[0-9]+== ?WARNING: ASan is ignoring requested __asan_handle_no_return: stack top:"),
+           re.compile(r"False positive error reports may follow"),
+           re.compile(r"For details see http://code.google.com/p/address-sanitizer/issues/detail\?id=189"),
+           re.compile(r"For details see https://github.com/google/sanitizers/issues/189"),
+           re.compile(r"Python runtime initialized with LC_CTYPE=C .*"),
+           re.compile(r"cmake: /usr/local/lib/libcurl\.so\.4: no version information available \(required by cmake\)"), # Seen on CircleCI
+           re.compile(r".*mmap broken on FreeBSD, but dlopen\+thread broken too. Switching to dlopen\+raw contexts\."),
+           re.compile(r".*dlopen\+thread broken on Apple and BSD\. Switching to raw contexts\."),
            ]
         TeshState().jenkins = True # This is a Jenkins build
 
@@ -440,7 +487,7 @@ if __name__ == '__main__':
     else:
         if not os.path.isfile(options.teshfile):
             print("Cannot open teshfile '"+options.teshfile+"': File not found")
-            exit(3)
+            tesh_exit(3)
         f = FileReader(options.teshfile)
         print("Test suite '"+f.abspath+"'")
 
@@ -449,9 +496,11 @@ if __name__ == '__main__':
             setenv(e)
 
     if options.cfg is not None:
-        TeshState().args_suffix += " --cfg="+options.cfg
+        for c in options.cfg:
+            TeshState().args_suffix += " --cfg=" + c
     if options.log is not None:
-        TeshState().args_suffix += " --log="+options.log
+        for l in options.log:
+            TeshState().args_suffix += " --log=" + l
 
     if options.wrapper is not None:
         TeshState().wrapper = options.wrapper