示例#1
0
def upload_board_info():
    """
    upload board info xml file
    :return: the upload status
    """
    if request.method == 'POST':
        if 'file' not in request.files:
            return {'status': 'Error: no file uploaded'}
        file = request.files['file']
        if file and '.' in file.filename and file.filename.rsplit('.', 1)[1] in ['xml']:
            filename = secure_filename(file.filename)
            tmp_filename = 'tmp_' + filename
            save_tmp_board_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                               'res', tmp_filename)
            file.save(save_tmp_board_path)

            board_type_list = []
            config_path = current_app.config.get('CONFIG_PATH')
            for config_name in os.listdir(config_path):
                if os.path.isdir(os.path.join(config_path, config_name)) \
                        and config_name != 'generic':
                    board_type_list.append(config_name)

            res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res')
            if not os.path.isdir(res_path):
                os.makedirs(res_path)

            board_info_config = XmlConfig(res_path)
            board_info_config.set_curr(tmp_filename.rsplit('.', 1)[0])
            board_info_root = board_info_config.get_curr_root()
            board_type = None
            if board_info_root and 'board' in board_info_root.attrib \
                and 'scenario' not in board_info_root.attrib \
                    and 'uos_launcher' not in board_info_root.attrib:
                board_type = board_info_root.attrib['board']
            if not board_type:
                os.remove(save_tmp_board_path)
                return {'status': 'Error on parsing Board info\n'
                                  'check the xml syntax and whether there is only the board '
                                  'attribute in the board info file'}

            os.rename(save_tmp_board_path,
                      os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                   'res', filename))
            info = 'updated'
            if board_type not in board_type_list:
                info = board_type
                os.makedirs(os.path.join(config_path, board_type))
                for generic_name in os.listdir(os.path.join(config_path, 'generic')):
                    generic_file = os.path.join(config_path, 'generic', generic_name)
                    if os.path.isfile(generic_file):
                        new_file = os.path.join(config_path, board_type, generic_name)
                        copyfile(generic_file, new_file)
                        xml_config = XmlConfig(os.path.join(current_app.config.get('CONFIG_PATH'),
                                                            board_type))
                        xml_config.set_curr(generic_name[:-4])
                        xml_config.set_curr_attr('board', board_type)
                        xml_config.save(generic_name[:-4], user_defined=False)

            board_info = os.path.splitext(file.filename)[0]
            current_app.config.update(BOARD_INFO=board_info)
            current_app.config.update(BOARD_TYPE=board_type)

            return {'status': 'success', 'info': info}

    return {'status': 'Error: upload failed'}
示例#2
0
def upload_board_info():
    """
    upload board info xml file
    :return: the upload status
    """
    if request.method == 'POST':
        if 'file' not in request.files:
            return {'status': 'Error: no file uploaded'}
        file = request.files['file']
        if file and '.' in file.filename and file.filename.rsplit('.', 1)[1] in ['xml']:
            filename = secure_filename(file.filename)
            tmp_filename = 'tmp_' + filename
            save_tmp_board_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                               'res', tmp_filename)
            file.save(save_tmp_board_path)

            board_type_list = []
            config_path = current_app.config.get('CONFIG_PATH')
            for config_name in os.listdir(config_path):
                if os.path.isdir(os.path.join(config_path, config_name)) \
                        and config_name != 'generic':
                    board_type_list.append(config_name)

            res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res')
            if not os.path.isdir(res_path):
                os.makedirs(res_path)

            board_info_config = XmlConfig(res_path)
            board_info_config.set_curr(tmp_filename.rsplit('.', 1)[0])
            board_info_root = board_info_config.get_curr_root()
            board_type = None
            if board_info_root and 'board' in board_info_root.attrib \
                and 'scenario' not in board_info_root.attrib \
                    and 'uos_launcher' not in board_info_root.attrib:
                board_type = board_info_root.attrib['board']
            if not board_type:
                os.remove(save_tmp_board_path)
                return {'status': 'Error on parsing Board info\n'
                                  'check the xml syntax and whether there is only the board '
                                  'attribute in the board info file'}

            os.rename(save_tmp_board_path,
                      os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                   'res', filename))
            info = 'updated'
            if board_type not in board_type_list:
                info = board_type
                os.makedirs(os.path.join(config_path, board_type))
                for generic_name in os.listdir(os.path.join(config_path, 'generic')):
                    generic_file = os.path.join(config_path, 'generic', generic_name)
                    if os.path.isfile(generic_file):
                        new_file = os.path.join(config_path, board_type, generic_name)
                        copyfile(generic_file, new_file)
                        xml_config = XmlConfig(os.path.join(current_app.config.get('CONFIG_PATH'),
                                                            board_type))
                        xml_config.set_curr(generic_name.rsplit('.', 1)[0])
                        xml_config.set_curr_attr('board', board_type)
                        # update RDT->CLOS_MASK according to board xml
                        xml_config_root = xml_config.get_curr_root()
                        if 'board' in xml_config_root.attrib and 'scenario' in xml_config_root.attrib \
                                and 'uos_launcher' not in xml_config_root.attrib:
                            cdp_enabled = xml_config.get_curr_value('hv', 'FEATURES', 'RDT', 'CDP_ENABLED')
                            (num_clos_mask, num_mba_delay) = \
                                get_num_of_rdt_res(filename.rsplit('.', 1)[0], cdp_enabled)
                            elem_clos_max = xml_config.get_curr_elem('hv', 'FEATURES', 'RDT', 'CLOS_MASK')
                            elem_mba_delay = xml_config.get_curr_elem('hv', 'FEATURES', 'RDT', 'MBA_DELAY')
                            xml_config.delete_curr_elem('hv', 'FEATURES', 'RDT', 'CLOS_MASK')
                            xml_config.delete_curr_elem('hv', 'FEATURES', 'RDT', 'MBA_DELAY')
                            for i in range(num_clos_mask):
                                xml_config.clone_curr_elem(elem_clos_max, 'hv', 'FEATURES', 'RDT')
                            for i in range(num_mba_delay):
                                xml_config.clone_curr_elem(elem_mba_delay, 'hv', 'FEATURES', 'RDT')
                        xml_config.save(generic_name.rsplit('.', 1)[0], user_defined=False)

            board_info = os.path.splitext(file.filename)[0]
            current_app.config.update(BOARD_INFO=board_info)
            current_app.config.update(BOARD_TYPE=board_type)

            return {'status': 'success', 'info': info}

    return {'status': 'Error: upload failed'}