示例#1
0
def start_task(task_id: str):
    session = session_maker()
    _processing_logs = session.query(TroubledLog).filter(TroubledLog.TASK_ID == task_id).filter(
        TroubledLog.STATE == TroubledLogState.PROCESSING).all()

    if len(_processing_logs) != 0:
        raise RuntimeError('当前有任务正在执行中')

    _task = session.query(Task).filter(Task.ID == task_id).one_or_none()
    if _task is None:
        raise RuntimeError('无法执行该条任务,因为该任务不存在!')
    session.add(
        TroubledLog(task_id=task_id, task_name=_task.NAME, state=TroubledLogState.PROCESSING, log_size=1000, offset=0))
    session.commit()

    _troubled_log = session.query(TroubledLog).filter(TroubledLog.TASK_ID == task_id).filter(
        TroubledLog.STATE == TroubledLogState.PROCESSING).one_or_none()

    _case_id = _task.CASES
    _cases = session.query(Case).filter(Case.ID.in_(_case_id.split(','))).all()

    os = OS.instance(_task.PLATFORM)
    os.start(_troubled_log, *_cases)

    session.query(TroubledLog).filter(TroubledLog.TASK_ID == task_id).filter(
        TroubledLog.STATE == TroubledLogState.PROCESSING).update({TroubledLog.STATE: TroubledLogState.DONE})
    session.commit()
    return Response.success()
示例#2
0
    def start(self, log: TroubledLog, *args: Case):
        session = session_maker()
        for case in args:
            count = case.TOTAL_COUNT
            while count > 0:
                super().init_log_detail(log, case)
                _is_crash = False
                _crash_log = ''
                _session = self.client.session('com.apple.mobilesafari', ['-u', case.SCHEMA])
                try:
                    _session(name=u'打开').click_exists()
                    time.sleep(5)
                except Exception as e:
                    _is_crash = True
                    _crash_log = str(e)
                finally:
                    _image = self.client.http.get('screenshot').value
                    session.query(TroubledLogDetail).filter(
                        TroubledLogDetail.LOG_ID == log.ID).filter(
                        TroubledLogDetail.STATE == TroubledLogDetailState.MONKEY_DONE).update(
                        {TroubledLogDetail.IS_CRASH: _is_crash,
                         TroubledLogDetail.CRASH_LOG: _crash_log,
                         TroubledLogDetail.SCREEN_SHOT: _image,
                         TroubledLogDetail.STATE: TroubledLogDetailState.DONE})
                    session.commit()
                    if _session(name=u"确认").click_exists():
                        time.sleep(1)

                    _session.tap(40, 44)
                    _session.close()
                count -= 1
示例#3
0
 def get_people_number(cls, course):
     with session_maker() as session:
         heartbeats = session.query(cls).filter(
             and_(cls.course == course,
                  cls.created_time + 30 > datetime.datetime.now())).all()
         res = set()
         for heartbeat in heartbeats:
             res.add(heartbeat.student_code)
         return len(res)
示例#4
0
def add_task():
    session = session_maker()
    parser = reqparse.RequestParser()
    parser.add_argument('name', type=str)
    parser.add_argument('team', type=str)
    parser.add_argument('platform', type=str)
    parser.add_argument('cases', type=str)
    parser.add_argument('comments', type=str)
    args = parser.parse_args()
    session.add(Task(args))
    session.commit()
    return Response.success()
示例#5
0
def add_case():
    session = session_maker()
    parser = reqparse.RequestParser()
    parser.add_argument('name', type=str)
    parser.add_argument('schema', type=str)
    parser.add_argument('keywords', type=str)
    parser.add_argument('response', type=str)
    parser.add_argument('total_count', type=int)
    parser.add_argument('comments', type=str)
    args = parser.parse_args()
    session.add(Case(**args))
    session.commit()
    return Response.success()
示例#6
0
    def start(self, log: TroubledLog, *args: Case):
        session = session_maker()
        for case in args:
            for _ in range(case.TOTAL_COUNT):
                super().init_log_detail(log, case)
                _is_crash = False
                _crash_log = ''
                try:
                    os.popen(
                        'adb shell am start -n com.cmbchina.ccd.pluto.cmbActivity/.SplashActivity -d ' + case.SCHEMA)
                    #pid = os.getpid()
                    #time.sleep(30)

                    pid1 = pid2 = os.getpid()
                    i = 0
                    #从启动APP开始,如果pid没有改变则等待15秒关闭
                    while pid1 == pid2 and i < 20:
                        time.sleep(1)
                        i += 1
                        pid2 = os.getpid()
                        #print("get pid:" + str(pid2))

                except Exception as e:
                    print(e)
                finally:
                    new_pid = os.getpid()
                    if new_pid != pid1:
                        _is_crash = True
                        _crash_log = ''
                    # 截图
                    _image: str = ''

                    # _troubled_log_detail = session.query(TroubledLogDetail).filter(
                    #     TroubledLogDetail.LOG_ID == log.ID).filter(
                    #     TroubledLogDetail.STATE == TroubledLogDetailState.MONKEY_DONE).one_or_none()
                    #
                    # if _troubled_log_detail is not None:
                    #     _troubled_log_detail.IS_CRASH = _is_crash
                    #     _troubled_log_detail.CRASH_LOG = _crash_log
                    #     _troubled_log_detail.SCREEN_SHOT = _image
                    #     session.update(_troubled_log_detail)
                    session.query(TroubledLogDetail).filter(
                        TroubledLogDetail.LOG_ID == log.ID).filter(
                        TroubledLogDetail.STATE == TroubledLogDetailState.MONKEY_DONE).update(
                        {TroubledLogDetail.IS_CRASH: _is_crash,
                         TroubledLogDetail.CRASH_LOG: _crash_log,
                         TroubledLogDetail.SCREEN_SHOT: _image,
                         TroubledLogDetail.STATE: TroubledLogDetailState.DONE})
                    session.commit()

                    os.system("adb shell am force-stop com.cmbchina.ccd.pluto.cmbActivity")
示例#7
0
def bananas() -> str:
    session = session_maker()
    parser = reqparse.RequestParser()
    parser.add_argument('url', type=str)
    parser.add_argument('param', type=str)
    parser.add_argument('body', type=str)
    args = parser.parse_args()

    url: str = args.get('url')
    param: str = args.get('param')
    body: str = args.get('body')

    _troubled_log = session.query(TroubledLog).filter(
        TroubledLog.STATE == TroubledLogState.PROCESSING).one_or_none()

    _troubled_log_detail = session.query(TroubledLogDetail).filter(
        TroubledLogDetail.LOG_ID == _troubled_log.ID).filter(
            TroubledLogDetail.STATE ==
            TroubledLogDetailState.UI_START).one_or_none()

    if _troubled_log_detail is not None:

        _case_id = _troubled_log_detail.CASE_ID
        _case = session.query(Case).filter(Case.ID == _case_id).one_or_none()

        _keywords = _case.KEYWORDS

        if url.__contains__(_keywords) | param.__contains__(_keywords):
            _troubled_result = monkeys.troubled(args.get('url'),
                                                args.get('body'))
            _changed_response = _troubled_result.get('changed_response')
            session.query(TroubledLogDetail).filter(
                TroubledLogDetail.LOG_ID == _troubled_log.ID).filter(
                    TroubledLogDetail.STATE ==
                    TroubledLogDetailState.UI_START).update({
                        TroubledLogDetail.TROUBLED_STRATEGY:
                        _troubled_result.get('trouble_count'),
                        TroubledLogDetail.TROUBLED_RESPONSE:
                        _changed_response,
                        TroubledLogDetail.STATE:
                        TroubledLogDetailState.MONKEY_DONE
                    })

            session.commit()
            return _changed_response

        else:
            return body
    else:
        return body
示例#8
0
 def add(cls, client_msg: ClientMessage, course: str):
     with session_maker() as session:
         heartbeat = cls()
         heartbeat.student_code = client_msg.student_code
         heartbeat.course = course
         heartbeat.device = client_msg.device.dumps()
         heartbeat.device_id = client_msg.auth_license
         calc_device_id = Core.machine_code_auth(
             stu_code=client_msg.student_code,
             mac_addr=client_msg.device.mac_addr,
             c_volume_serial_number=client_msg.device.
             c_volume_serial_number,
             hostname=client_msg.device.hostname)
         if calc_device_id != client_msg.auth_license:
             Log.add(client_msg)
         heartbeat.created_time = datetime.datetime.now()
         session.add(heartbeat)
示例#9
0
    def add(cls, client_msg: ClientMessage, content=None):

        with session_maker() as session:
            log = cls()
            log.student_code = client_msg.student_code
            log.content = content
            log.message = client_msg.context.message
            log.body = json.dumps(client_msg.context.body,
                                  ensure_ascii=False,
                                  separators=(',', ':'))
            log.device_id = client_msg.auth_license
            log.device = client_msg.device.dumps(with_license=True)
            log.msg_sign = client_msg.sign
            log.msg_timestamp = client_msg.timestamp
            log.msg_type = client_msg.context.context_type.value
            log.created_time = datetime.datetime.now()
            session.add(log)
示例#10
0
class OS:
    @staticmethod
    def instance(os_type: str):
        if os_type == OSType.IOS:
            os = IOS()
        elif os_type == OSType.ANDROID:
            os = Android()
        else:
            raise RuntimeError('未知的平台:' + os_type)
        os.init()
        return os

    def init(self):
        pass

    def start(self, log: TroubledLog, *args: Case):
        pass

    def init_log_detail(self, log: TroubledLog, case: Case):
        session = session_maker()
        _troubled_log_detail = TroubledLogDetail(log_id=log.ID, case_id=case.ID, case_name=case.NAME,
                                                 state=TroubledLogDetailState.UI_START)
        session.add(_troubled_log_detail)
        session.commit()
示例#11
0
    def add(cls, client_msg: ClientMessage):
        with session_maker() as session:
            device = cls()

            stu_codes = session.query(Devices.student_codes).filter(
                Devices.device_id == client_msg.device.device_id).first()

            if stu_codes:
                stu_codes = json.loads(stu_codes[0]) if stu_codes else []
                stu_codes = list(set([client_msg.student_code] + stu_codes))
                stu_codes = json.dumps(stu_codes,
                                       ensure_ascii=False,
                                       separators=(',', ':'))
                session.query(Devices).filter(
                    Devices.device_id == client_msg.device.device_id).update(
                        {Devices.student_codes: stu_codes})
            else:
                stu_codes = json.dumps([client_msg.student_code],
                                       ensure_ascii=False,
                                       separators=(',', ':'))
                device.student_codes = stu_codes
                device.hostname = client_msg.device.hostname
                device.mac_addr = client_msg.device.mac_addr
                device.c_volume_serial_number = client_msg.device.c_volume_serial_number
                device.device_id = client_msg.device.device_id
                device.created_time = datetime.datetime.now()
                session.add(device)

            calc_auth_license = Core.machine_code_auth(
                stu_code=client_msg.student_code,
                mac_addr=client_msg.device.mac_addr,
                c_volume_serial_number=client_msg.device.
                c_volume_serial_number,
                hostname=client_msg.device.hostname)
            if calc_auth_license != client_msg.auth_license:
                Log.add(client_msg)
示例#12
0
def get_log(log_id: str):
    session = session_maker()
    values = session.query(TroubledLogDetail).filter(
        TroubledLogDetail.LOG_ID == log_id).all()
    return Response.success(values)
示例#13
0
def get_logs():
    session = session_maker()
    values = session.query(TroubledLog).all()
    return Response.success(values)
示例#14
0
def get_tasks():
    session = session_maker()
    values = session.query(Task).all()
    return Response.success(values)
示例#15
0
from wtforms import StringField, PasswordField, BooleanField, SubmitField, IntegerField, DateTimeField
from wtforms.widgets import TextArea
from wtforms.validators import DataRequired
from wtforms.fields.html5 import DateTimeLocalField
from werkzeug.security import generate_password_hash, check_password_hash
from sqlalchemy import literal
import markdown
import markdown.extensions.fenced_code
import markdown.extensions.codehilite
from pygments.formatters import HtmlFormatter
from datetime import datetime, timedelta
import requests
from itsdangerous import URLSafeTimedSerializer
from models import User, Forum, Message, Event, session_maker, Bots

session = session_maker()

app = Flask(__name__, template_folder="templates/")
app.config['SECRET_KEY'] = 'blin_is_not_a_pancake'
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config["MAIL_SERVER"] = "smtp.yandex.ru"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = '******'
app.config["MAIL_PASSWORD"] = '******'
app.config["MAIL_DEFAULT_SENDER"] = "*****@*****.**"
serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
login_manager = LoginManager()
login_manager.init_app(app)
api = Api(app)
socketio = SocketIO(app)
示例#16
0
def get_cases():
    session = session_maker()
    values = session.query(Case).all()
    return Response.success(values)