Example #1
0
def main():
    
    r'''加载配置文件,如果没有,就选默认的robot.cnf'''
    conf_path = os.getenv('vtest.robot.cnf', 'robot.cnf')
    robot_conf = {}
    with open(conf_path) as f :
        for line in f :
            if line.startswith('#') :
                continue
            if line.find('=') :
                line = line[0:-1]
                key = line[0:line.find('=')]
                value = line[(line.find('=') + 1):]
                robot_conf[key] = value
    log.info('Robot conf :\n',json.dumps(robot_conf, indent=2))
    if not robot_conf.get('tmp') :
        robot_conf['tmp'] = '/tmp/'
    db_info = {
               'host' : robot_conf.get('db_host','127.0.0.1'),
               'port' : int(robot_conf.get('db_port','3306')),
               'user' : robot_conf.get('db_username','root'),
               'password' : robot_conf.get('db_password','123456'),
               'database' : robot_conf.get('db_name','vtest'),
               'charset'       : 'utf8',
               'use_unicode'   : True,
               'get_warnings'  : False
               }
    from vtest.client.helper import init_log
    init_log(None, '%s/vtest_main.log' % robot_conf['tmp'])
    
    if not robot_conf.get('rid') :
        log.info('No robot id found, create it!!')
        conn, cur = DB.connect(db_info)
        sql = '''insert into t_robot(ipv4, hnm, prid, lm) values('%s', '%s', %d, now())''' % (socket.gethostbyname(socket.gethostname()),
                                             socket.gethostname(),
                                             os.getpid())
        print sql
        cur.execute(sql)
        conn.commit()
        robot_conf['rid'] = cur.getlastrowid()
        conn.close()
    else :
        renew_robot_status(robot_conf['rid'], db_info)
    log.info('Robot id = %d' % robot_conf['rid'])
    
    while 1 :
        log.info('Scan new task ...')
        conn = None
        try :
            conn, cur = DB.connect(db_info)
            cur.execute('select id,detail from t_task where stat < 2 order by rand() limit 1')
            res = cur.fetchall()
            if res :
                record = {'id' : res[0][0], 'detail' : res[0][1]}
                log.info('Found a task , id=%d', record['id'])
                log.info("========================================================================")
                cur.execute('update t_task set lm=now() where id=%d' % record['id'])
                conn.commit()
                conn.close()
                start_task(record, robot_conf, db_info)
                log.info('Task is Done, great!!')
                log.info("========================================================================")
            else :
                log.info('No task found ....')
        except :
            log.error('Fail to execute task?!!', exc_info=1)
            DB.rollback(conn)
        finally:
            DB.close(conn)
        
        renew_robot_status(robot_conf['rid'], db_info)
        
        log.info('Sleep 1s ... ... ...')
        time.sleep(1)
Example #2
0
'''
Created on 2011-12-2

@author: MingMing
'''
import unittest
import os
from vtest.client.handlers import BaseHandler 
from vtest.client.handlers import SUCCESS 
import json

with open('meta/base.json') as f :
    h = BaseHandler(json.load(f), {'rid' : -1})
    from vtest.client.helper import init_log
    init_log()

class Test(unittest.TestCase):

    def test_img_make(self):
        img_path = '_img_make.bmp'
        if os.path.exists(img_path):
            os.remove(img_path)
        h.img_make(img_path, 800, 480, "#FF0033")
        assert os.path.exists(img_path)
        assert os.path.getsize(img_path) > 1024
    
    def test_extends(self):
        type_method = '''
def abc(self, name) :
    print name
    return True