Example #1
0
def get_board(matching: Callable) -> Board:
    """Returns first `mbed_tools.targets.board.Board` for which `matching` is True.

    Uses database mode configured in the environment.

    Args:
        matching: A function which will be called to test matching conditions for each board in database.

    Raises:
        UnknownBoard: a board matching the criteria could not be found in the board database.
    """
    database_mode = _get_database_mode()

    if database_mode == _DatabaseMode.OFFLINE:
        logger.info("Using the offline database (only) to identify boards.")
        return Boards.from_offline_database().get_board(matching)

    if database_mode == _DatabaseMode.ONLINE:
        logger.info("Using the online database (only) to identify boards.")
        return Boards.from_online_database().get_board(matching)
    try:
        logger.info("Using the offline database to identify boards.")
        return Boards.from_offline_database().get_board(matching)
    except UnknownBoard:
        logger.info(
            "Unable to identify a board using the offline database, trying the online database."
        )
        try:
            return Boards.from_online_database().get_board(matching)
        except BoardDatabaseError:
            logger.error(
                "Unable to access the online database to identify a board.")
            raise UnknownBoard()
Example #2
0
def main(args: argparse.Namespace) -> int:
    """Main entry point."""
    set_log_level(args.verbose)
    pr_info = PullRequestInfo(
        repo="ARMMbed/mbed-targets",
        head_branch=args.head_branch,
        base_branch=args.base_branch,
        subject=args.pr_subject,
        body=args.pr_description,
    )
    try:
        online_boards = Boards.from_online_database()
        if BOARD_DATABASE_PATH.exists():
            offline_boards = Boards.from_offline_database()
            added, removed = get_boards_added_or_removed(
                offline_boards, online_boards)
            if not (added or removed):
                logger.info("No changes to commit. Exiting.")
                return 0

            news_file_path = write_news_file_from_boards(added, removed)
        else:
            news_file_path = create_news_file(
                "Offline board database updated.", NewsType.feature)

        save_board_database(online_boards.json_dump(), BOARD_DATABASE_PATH)
        git_commit_and_push([BOARD_DATABASE_PATH, news_file_path],
                            pr_info.head_branch, pr_info.subject)
        raise_github_pr(pr_info)
        return 0
    except ToolsError as tools_error:
        log_exception(logger, tools_error)
        return 1
Example #3
0
    def test_json_dump_from_raw_and_filtered_data(self, mocked_get_offline_board_data, mocked_get_online_board_data):
        raw_board_data = [
            {"attributes": {"product_code": "0200", "board": "test"}},
            {"attributes": {"product_code": "0100", "board": "test2"}},
        ]
        mocked_get_online_board_data.return_value = raw_board_data

        boards = [Board.from_online_board_entry(b) for b in raw_board_data]
        filtered_board_data = [asdict(board) for board in boards]
        mocked_get_offline_board_data.return_value = filtered_board_data

        # Boards.from_online_database handles "raw" board entries from the online db
        boards = Boards.from_online_database()
        json_str_from_raw = boards.json_dump()
        t1_raw, t2_raw = boards

        # Boards.from_offline_database expects the data to have been "filtered" through the Boards interface
        offline_boards = Boards.from_offline_database()
        json_str_from_filtered = offline_boards.json_dump()
        t1_filt, t2_filt = offline_boards

        self.assertEqual(
            json_str_from_raw,
            json.dumps([asdict(t1_raw), asdict(t2_raw)], indent=4),
            "JSON string should match serialised board __dict__.",
        )

        self.assertEqual(json_str_from_filtered, json.dumps([t1_filt.__dict__, t2_filt.__dict__], indent=4))
Example #4
0
def main(args: argparse.Namespace) -> int:
    """Main entry point."""
    set_log_level(args.verbose)
    try:
        online_boards = Boards.from_online_database()
        offline_boards = Boards.from_offline_database()
        result = compare_databases(offline_boards, online_boards)
        if not (result.boards_added or result.boards_removed or result.boards_modified):
            logger.info("No changes to commit. Exiting.")
            return 0

        news_file_text = create_news_file_text_from_result(result)
        create_news_file(news_file_text, NewsType.feature)
        save_board_database(online_boards.json_dump(), BOARD_DATABASE_PATH)
        return 0
    except ToolsError as tools_error:
        log_exception(logger, tools_error)
        return 1