Source

pypy / pypy / tool / watchdog.py

import sys, os, signal
import threading

def getsignalname(n):
    for name, value in signal.__dict__.items():
        if value == n and name.startswith('SIG'):
            return name
    return 'signal %d' % (n,)

def childkill():
    global timedout
    timedout = True
    sys.stderr.write("==== test running for %d seconds ====\n" % timeout)
    sys.stderr.write("="*26 + "timedout" + "="*26 + "\n")
    try:
        os.kill(pid, signal.SIGTERM)
    except OSError:
        pass

if __name__ == '__main__':
    timeout = float(sys.argv[1])
    timedout = False

    pid = os.fork()
    if pid == 0:
        os.execvp(sys.argv[2], sys.argv[2:])
    else: # parent
        t = threading.Timer(timeout, childkill)
        t.start()
        while True:
            try:
                pid, status = os.waitpid(pid, 0)
            except KeyboardInterrupt:
                continue
            else:
                t.cancel()
                break
        if os.WIFEXITED(status):
            sys.exit(os.WEXITSTATUS(status))
        else:
            assert os.WIFSIGNALED(status)
            sign = os.WTERMSIG(status)
            if timedout and sign == signal.SIGTERM:
                sys.exit(1)
            signame = getsignalname(sign)
            sys.stderr.write("="*26 + "timedout" + "="*26 + "\n")        
            sys.stderr.write("="*25 + " %-08s " %  signame + "="*25 + "\n")
            sys.exit(1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.