Ejemplo n.º 1
0
def main():
    """Run the LockingProtocol."""
    args = parse_input()
    args.lock = True
    args.question = []
    args.all = False
    args.timeout = 0
    args.verbose = False
    args.interactive = False

    try:
        assign = assignment.load_config(args.config, args)
        assign.load()

        msgs = messages.Messages()

        lock.protocol(args, assign).run(msgs)
    except (ex.LoadingException, ex.SerializeException) as e:
        log.warning('Assignment could not instantiate', exc_info=True)
        print('Error: ' + str(e).strip())
        exit(1)
    except (KeyboardInterrupt, EOFError):
        log.info('Quitting...')
    else:
        assign.dump_tests()
Ejemplo n.º 2
0
def main():
    """Run the LockingProtocol."""
    args = parse_input()
    args.lock = True
    args.question = []
    args.all = False
    args.timeout = 0
    args.verbose = False
    args.interactive = False

    try:
        assign = assignment.load_config(args.config, args)
        assign.load()

        msgs = messages.Messages()

        lock.protocol(args, assign).run(msgs)
    except (ex.LoadingException, ex.SerializeException) as e:
        log.warning('Assignment could not instantiate', exc_info=True)
        print('Error: ' + str(e).strip())
        exit(1)
    except (KeyboardInterrupt, EOFError):
        log.info('Quitting...')
    else:
        assign.dump_tests()
Ejemplo n.º 3
0
def main():
    """Run all relevant aspects of ok.py."""
    args = parse_input()

    log.setLevel(logging.DEBUG if args.debug else logging.ERROR)
    log.debug(args)

    if args.version:
        print("okpy=={}".format(client.__version__))
        exit(0)
    elif args.update:
        print("Current version: {}".format(client.__version__))
        software_update.check_version(args.server,
                                      client.__version__,
                                      client.FILE_NAME,
                                      timeout=10)
        exit(0)

    assign = None
    try:
        if args.authenticate:
            auth.authenticate(True)

        # Instantiating assignment
        assign = assignment.load_config(args.config, args)
        assign.load()

        if args.tests:
            print('Available tests:')
            for name in assign.test_map:
                print('    ' + name)
            exit(0)

        msgs = messages.Messages()
        for name, proto in assign.protocol_map.items():
            log.info('Execute {}.run()'.format(name))
            proto.run(msgs)

        msgs['timestamp'] = str(datetime.now())

    except ex.LoadingException as e:
        log.warning('Assignment could not load', exc_info=True)
        print('Error loading assignment: ' + str(e))
    except ex.OkException as e:
        log.warning('General OK exception occurred', exc_info=True)
        print('Error: ' + str(e))
    except KeyboardInterrupt:
        log.info('KeyboardInterrupt received.')
    finally:
        if not args.no_update:
            try:
                software_update.check_version(args.server, client.__version__,
                                              client.FILE_NAME)
            except KeyboardInterrupt:
                pass

        if assign:
            assign.dump_tests()
Ejemplo n.º 4
0
def main():
    """Run all relevant aspects of ok.py."""
    args = parse_input()

    log.setLevel(logging.DEBUG if args.debug else logging.ERROR)
    log.debug(args)

    if args.version:
        print("okpy=={}".format(client.__version__))
        exit(0)
    elif args.update:
        print("Current version: {}".format(client.__version__))
        software_update.check_version(args.server, client.__version__,
                                      client.FILE_NAME, timeout=10)
        exit(0)

    assign = None
    try:
        if args.authenticate:
            auth.authenticate(True)

        # Instantiating assignment
        assign = assignment.load_config(args.config, args)
        assign.load()

        if args.tests:
            print('Available tests:')
            for name in assign.test_map:
                print('    ' + name)
            exit(0)

        msgs = messages.Messages()
        for name, proto in assign.protocol_map.items():
            log.info('Execute {}.run()'.format(name))
            proto.run(msgs)

        msgs['timestamp'] = str(datetime.now())

    except ex.LoadingException as e:
        log.warning('Assignment could not load', exc_info=True)
        print('Error loading assignment: ' + str(e))
    except ex.OkException as e:
        log.warning('General OK exception occurred', exc_info=True)
        print('Error: ' + str(e))
    except KeyboardInterrupt:
        log.info('KeyboardInterrupt received.')
    finally:
        if not args.no_update:
            try:
                software_update.check_version(args.server, client.__version__,
                                              client.FILE_NAME)
            except KeyboardInterrupt:
                pass

        if assign:
            assign.dump_tests()
Ejemplo n.º 5
0
def main():
    """Run the LockingProtocol."""
    args = parse_input()

    log.setLevel(logging.DEBUG if args.debug else logging.ERROR)
    log.debug(args)

    try:
        assign = assignment.load_config(args.config, args)
        assign.load()

        grading.protocol(args, assign).on_interact()
        scoring.protocol(args, assign).on_interact()
    except (ex.LoadingException, ex.SerializeException) as e:
        log.warning('Assignment could not instantiate', exc_info=True)
        print('Error: ' + str(e).strip())
        exit(1)
    except (KeyboardInterrupt, EOFError):
        log.info('Quitting...')
Ejemplo n.º 6
0
def main():
    """Run GradingProtocol and ScoringProtocol."""
    args = ok.parse_input()

    log.setLevel(logging.DEBUG if args.debug else logging.ERROR)
    log.debug(args)

    try:
        assign = assignment.load_config(args.config, args)
        assign.load()

        msgs = messages.Messages()

        grading.protocol(args, assign).run(msgs)
        scoring.protocol(args, assign).run(msgs)
    except (ex.LoadingException, ex.SerializeException) as e:
        log.warning('Assignment could not instantiate', exc_info=True)
        print('Error: ' + str(e).strip())
        exit(1)
    except (KeyboardInterrupt, EOFError):
        log.info('Quitting...')
Ejemplo n.º 7
0
def main():
    """Run all relevant aspects of ok.py."""
    args = parse_input()

    log.setLevel(logging.DEBUG if args.debug else logging.ERROR)
    log.debug(args)

    if args.version:
        print("okpy=={}".format(client.__version__))
        exit(0)

    # Check if ssl is available
    if not args.local and not args.insecure:
        try:
            import ssl
        except:
            log.warning('Error importing ssl', stack_info=True)
            sys.exit("SSL Bindings are not installed. You can install python3 SSL bindings or \nrun ok locally with python3 ok --local")

    # Load assignment
    try:
        assign = assignment.load_config(args.config, args)
    except (ex.LoadingException, ex.SerializeException) as e:
        print(str(e))
        exit(1)
    except KeyboardInterrupt:
        print("Quitting ok.")

    # Load backup files
    try:
        with open(BACKUP_FILE, 'rb') as fp:
            msg_list = pickle.load(fp)
            log.info('Loaded %d backed up messages from %s',
                     len(msg_list), BACKUP_FILE)
    except (IOError, EOFError) as e:
        log.info('Error reading from ' + BACKUP_FILE \
                + ', assume nothing backed up')
        msg_list = []
    except KeyboardInterrupt:
        print("Quitting ok.")

    try:
        # Run protocol.on_start
        start_messages = dict()
        for name, proto in assign.protocol_map.items():
            log.info('Execute {}.on_start()'.format(name))
            start_messages[name] = proto.on_start()
        start_messages['timestamp'] = str(datetime.now())

        # Run protocol.on_interact
        interact_msg = {}
        for name, proto in assign.protocol_map.items():
            log.info('Execute {}.on_interact()'.format(name))
            interact_msg[name] = proto.on_interact()
        interact_msg['timestamp'] = str(datetime.now())
    except KeyboardInterrupt:
        print("Quitting ok.")
    finally:
        # Running protocols is the only task that modifies the assignment, so
        # dumping is only necessary here.
        assign.dump_tests()


    # Send request to server
    try:
        # TODO(denero) Print server responses.
        if not args.local:
            msg_list.append(interact_msg)

            try:
                access_token = auth.authenticate(args.authenticate)
                log.info('Authenticated with access token %s', access_token)

                msg_list.append(start_messages)
                print("Backing up your work...")
                response = network.dump_to_server(access_token, msg_list,
                                   assign.endpoint, args.server, args.insecure,
                                   client.__version__, log, send_all=args.submit)

                if response:
                    # Hardcode course id- we need to return it from the server at some point...
                    print("Back-up successful: https://ok-server.appspot.com/#/5165212546105344/submission/{0}".format(response['data']['key']))

            except error.URLError as e:
                log.warning('on_start messages not sent to server: %s', str(e))

            with open(BACKUP_FILE, 'wb') as fp:
                log.info('Save %d unsent messages to %s', len(msg_list),
                         BACKUP_FILE)

                pickle.dump(msg_list, fp)
                os.fsync(fp)

            if len(msg_list) == 0:
                print("Server submission successful")
    except KeyboardInterrupt:
        print("Quitting ok.")
Ejemplo n.º 8
0
def main():
    """Run all relevant aspects of ok.py."""
    args = parse_input()

    log.setLevel(logging.DEBUG if args.debug else logging.ERROR)
    log.debug(args)

    if args.version:
        print("okpy=={}".format(client.__version__))
        exit(0)

    # Instantiating assignment
    try:
        assign = assignment.load_config(args.config, args)
    except ex.LoadingException as e:
        log.warning('Assignment could not instantiate', exc_info=True)
        print('Error: ' + str(e).strip())
        exit(1)

    # Load backup files
    msg_list = []
    try:
        with open(BACKUP_FILE, 'rb') as fp:
            msg_list = pickle.load(fp)
            log.info('Loaded %d backed up messages from %s',
                     len(msg_list), BACKUP_FILE)
    except (IOError, EOFError) as e:
        log.info('Error reading from ' + BACKUP_FILE \
                + ', assume nothing backed up')
    except KeyboardInterrupt:
        log.warning('Backup messages were not loaded due to KeyboardInterrupt')


    try:
        # Load tests and protocols
        assign.load()

        if args.tests:
            print('Available tests:')
            for name in assign.test_map:
                print('    ' + name)
            exit(0)

        # Run protocol.on_start
        start_messages = dict()
        for name, proto in assign.protocol_map.items():
            log.info('Execute {}.on_start()'.format(name))
            start_messages[name] = proto.on_start()
        # TODO(albert): doesn't AnalyticsProtocol store the timestamp?
        start_messages['timestamp'] = str(datetime.now())
        msg_list.append(start_messages)

        # Run protocol.on_interact
        interact_msg = {}
        for name, proto in assign.protocol_map.items():
            log.info('Execute {}.on_interact()'.format(name))
            interact_msg[name] = proto.on_interact()
        # TODO(albert): doesn't AnalyticsProtocol store the timestamp?
        interact_msg['timestamp'] = str(datetime.now())
        msg_list.append(interact_msg)
    except ex.LoadingException as e:
        log.warning('Assignment could not load', exc_info=True)
        print('Error loading assignment: ' + str(e))
    except ex.OkException as e:
        log.warning('General OK exception occurred', exc_info=True)
        print('Error: ' + str(e))
    except KeyboardInterrupt:
        log.info('Quitting protocols')
        assign.dump_tests()
    else:
        assign.dump_tests()

    if args.local:
        return

    # Send request to server
    try:
        # TODO(denero) Print server responses.

        # Check if ssl is available
        if not args.insecure:
            try:
                import ssl
            except:
                log.warning('Error importing ssl', stack_info=True)
                sys.exit("SSL Bindings are not installed. You can install python3 SSL bindings or \nrun ok locally with python3 ok --local")

        try:
            access_token = auth.authenticate(args.authenticate)
            log.info('Authenticated with access token %s', access_token)

            print("Backing up your work...")
            response = network.dump_to_server(access_token, msg_list,
                               assign.endpoint, args.server, args.insecure,
                               client.__version__, log,
                               send_all=args.submit or args.backup)

            if isinstance(response, dict):
                print("Backup successful for user: {0}".format(response['data']['email']))
                if args.submit or args.backup:
                    print("URL: https://ok-server.appspot.com/#/{0}/submission/{1}".format(response['data']['course'], response['data']['key']))
                if args.backup:
                    print('NOTE: this is only a backup. '
                          'To submit your assignment, run ok with --submit.')

            else:
                print('Unable to complete backup.')
                log.warning('network.dump_to_server returned {}'.format(response))

        except error.URLError as e:
            log.warning('on_start messages not sent to server: %s', str(e))

        with open(BACKUP_FILE, 'wb') as fp:
            log.info('Save %d unsent messages to %s', len(msg_list),
                     BACKUP_FILE)

            pickle.dump(msg_list, fp)
            os.fsync(fp)
    except KeyboardInterrupt:
        print("Quitting ok.")