test_bot 3.62 KB
Newer Older
1
#!/usr/bin/python
2
import errno, os, socket, subprocess, sys, time
3 4 5 6 7 8 9 10

def clean():
    for path, dir_list, file_list in os.walk('.'):
        for file in file_list:
            # delete *.pyc files so that deleted/moved files can not be imported
            if file[-4:] in ('.pyc', '.pyo'):
                os.remove(os.path.join(path, file))

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
class GitError(EnvironmentError):
    def __init__(self, err, out, returncode):
        EnvironmentError.__init__(self, err)
        self.stdout = out
        self.returncode = returncode

def _git(*args, **kw):
    p = subprocess.Popen(('git',) + args, **kw)
    out, err = p.communicate()
    if p.returncode:
        raise GitError(err, out, p.returncode)
    return out

def git(*args, **kw):
    out = _git(stdout=subprocess.PIPE, stderr=subprocess.PIPE, *args, **kw)
    return out.strip()

def getRevision(*path):
    return git('log', '-1', '--format=%H', '--', *path)

31 32 33
def main():
    if 'LANG' in os.environ:
      del os.environ['LANG']
34
    os.environ.setdefault('NEO_TEST_ZODB_FUNCTIONAL', '1')
35 36 37 38 39 40 41 42

    arg_count = 1
    while arg_count < len(sys.argv):
        arg = sys.argv[arg_count]
        if arg[:2] != '--':
            break
        arg_count += '=' in arg and 1 or 2

43
    branch = git('rev-parse', '--abbrev-ref', 'HEAD')
44 45 46 47 48 49 50 51
    test_bot = os.path.realpath(__file__).split(os.getcwd())[1][1:]
    test_bot_revision = getRevision(test_bot)
    revision = 0

    clean()
    delay = None
    while True:
        delay = delay and time.sleep(delay) or 1800
52
        s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
53
        try:
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
            while True:
                try:
                    s.bind("\0neo.tools.test_bot")
                    break
                except socket.error, e:
                    if e.errno != errno.EADDRINUSE:
                        raise
                    time.sleep(60)
            old_revision = revision
            try:
                _git('fetch')
                _git('reset', '--merge', '@{u}')
            except GitError, e:
                continue
            revision = getRevision()
            if revision == old_revision:
                continue
            if test_bot_revision != getRevision(test_bot):
                break
            delay = None
            for test_home in sys.argv[arg_count:]:
                test_home, tasks = test_home.rsplit('=', 1)
                tests = ''.join(x for x in tasks if x in 'fuz')
                bin = os.path.join(test_home, 'bin')
                if subprocess.call((os.path.join(bin, 'buildout'), '-v'),
79
                                   cwd=test_home):
80
                    continue
81 82 83
                title = '[%s:%s-g%s:%s]' % (branch,
                  git('rev-list', '--topo-order', '--count', revision),
                  revision[:7], os.path.basename(test_home))
84 85 86 87 88 89 90
                if tests:
                    subprocess.call([os.path.join(bin, 'neotestrunner'),
                                     '-' + tests, '--title',
                                     'NEO tests ' + title,
                                    ] + sys.argv[1:arg_count])
                if 'm' in tasks:
                    subprocess.call([os.path.join(bin, 'python'),
91
                                     'tools/matrix', '--repeat=2',
92 93 94 95
                                     '--min-storages=1', '--max-storages=24',
                                     '--min-replicas=0', '--max-replicas=3',
                                     '--title', 'Matrix ' + title,
                                    ] + sys.argv[1:arg_count])
96 97
        finally:
            s.close()
98
        clean()
99
    os.execvp(sys.argv[0], sys.argv)
100 101 102

if __name__ == '__main__':
    sys.exit(main())