Ejemplo n.º 1
0
async def eoc_websocket_run(args):
    import websockets
    import json

    mission = args.mission[0]
    filename = get_filename(args)
    if filename is None:
        filename = sync_single_mission(mission)
        if filename is None:
            print('Mission "{}" not found'.format(mission))
            return

    domain_data = conf.default_domain_data
    if 'key' not in domain_data:
        print('client is not configured. Please do $ checkio config')
        return

    logging.info('USING:' + filename)

    with open(filename, encoding="utf-8") as fh:
        code = code_for_check(fh.read())

    async with websockets.connect(domain_data['ws_url'],
                                  extra_headers=websockets.http.Headers({
                                      'Cookie':
                                      'apiKey=' + domain_data['key']
                                  })) as websocket:
        greeting = await websocket.recv()
        # print('greeting', greeting)

        await websocket.send(
            json.dumps({
                'action': 'run',
                'data': {
                    'code': code,
                    'env_name': domain_data['interpreter'],
                    'mission': mission,
                    'process': randint(10**5, 10**6)
                }
            }))

        while True:
            resp = await websocket.recv()
            resp = json.loads(resp)
            if resp['action'] == 'run':
                stream = resp['data'][
                    'type'] == 'stdout' and sys.stdout or sys.stderr
                print(resp['data']['result'], end='', file=stream, flush=True)
            elif resp['action'] == 'runDone':
                print('SUCCESS', resp['data']['success'])
                break
Ejemplo n.º 2
0
async def eoc_websocket_check(args):
    import websockets

    mission = args.mission[0]
    filename = get_filename(args)
    if filename is None:
        filename = sync_single_mission(mission)
        if filename is None:
            print('Mission "{}" not found'.format(mission))
            return

    domain_data = conf.default_domain_data
    if 'key' not in domain_data:
        print('client is not configured. Please do $ checkio config')
        return

    logging.info('USING: %s', filename)

    with open(filename, encoding="utf-8") as fh:
        code = code_for_check(fh.read())

    async with websockets.connect(domain_data['ws_url'],
                                  extra_headers=websockets.http.Headers({
                                      'Cookie':
                                      'apiKey=' + domain_data['key']
                                  })) as websocket:
        greeting = await websocket.recv()
        #print('greeting', greeting)

        await websocket.send(
            json.dumps({
                'action': 'check',
                'data': {
                    'code': code,
                    'env_name': domain_data['interpreter'],
                    'mission': mission,
                    'process': mission
                }
            }))

        while True:
            resp = await websocket.recv()
            resp = json.loads(resp)
            if resp['action'] == 'check':
                # stream = resp['data']['type'] == 'stdout' and sys.stdout or sys.stderr
                # print(resp['data']['result'], end='', file=stream, flush=True)
                c_data = resp['data']
                if c_data['type'] == 'pre_test':
                    print('\n - CALL', c_data['representation'])
                elif c_data['type'] == 'post_test':
                    if c_data['test_passed']:
                        print('\n - EXPECTED', c_data['expected_result'])
                    else:
                        print('\n - ACTUAL', c_data['actual_result'])
                        print('\n - EXPECTED', c_data['expected_result'])
                elif c_data['type'] == 'stdout':
                    print(c_data['result'],
                          file=sys.stdout,
                          flush=True,
                          end='')
                elif c_data['type'] == 'stderr':
                    print(c_data['result'],
                          file=sys.stderr,
                          flush=True,
                          end='')
                else:
                    print('UNKNOW', c_data)
            elif resp['action'] == 'checkDone':
                print('SUCCESS', resp['data']['success'])
                break