一个End-to-End的swgdb包装



  • import os
    import sys
    import re
    
    def print_help():
        print 'Eaiser way to launch swgdb'
        print 'Usage: easiergdb.py jobid rank'
    
    def check(exe):
        ret = os.wait()
        code = ret[1] >> 8
        if code:
            print '%s exited with code %d' % (exe, code)
            exit(1)
    try:
        jobid = int(sys.argv[1])
        rank = int(sys.argv[2])
    except Exception:
        print_help()
        exit(1)
    
    bdebug = os.popen('bdebug %d' % jobid)
    check('bdebug')
    
    bjobs = os.popen('bjobs -l %d' % jobid)
    check('bjobs')
    bjobs_output = bjobs.read()
    cmd_re = re.compile('Command<(?P<CMD>[^>]*)>')
    cwd_re = re.compile('CWD<(?P<CWD>[^>]*)>')
    cmd = cmd_re.search(bjobs_output).groupdict()['CMD']
    cwd = cwd_re.search(bjobs_output).groupdict()['CWD']
    
    exe = cmd.split(' ')[0]
    
    bjobinfo = os.popen('bjobinfo %d' % jobid)
    check('bjobinfo')
    
    node = '0'
    bjobinfo_lines = bjobinfo.read().split('\n')
    spaces = re.compile('\\s*')
    for line in bjobinfo_lines[4:-2]:
        r = spaces.split(line)[1]
        if int(r) == rank:
            node = spaces.split(line)[4]
            print '=========================================='
            print 'Target pid is %s' % spaces.split(line)[6]
            print '=========================================='
    if os.path.exists(cwd + os.path.sep + exe):
        path = cwd + os.path.sep + exe
    else:
        path = exe
    os.system("sed -e 's/gdbtui/gdb/g' `which swgdb`> /tmp/swgdb.%d" % os.getpid())
    os.system('sh /tmp/swgdb.%d %s %s' % (os.getpid(), path, node))
    os.system('rm /tmp/swgdb.%d' % (os.getpid()))
    

    这是一个用上去可能更简单的一点的包装。
    用法是python xxx.py 作业号 进程号。
    xxx.py是上面这段代码保存的路径。


Log in to reply