140681896Sopenharmony_ci#!/usr/bin/env python3
240681896Sopenharmony_ci# -*- coding: utf-8 -*-
340681896Sopenharmony_ci
440681896Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
540681896Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
640681896Sopenharmony_ci# you may not use this file except in compliance with the License.
740681896Sopenharmony_ci# You may obtain a copy of the License at
840681896Sopenharmony_ci#
940681896Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
1040681896Sopenharmony_ci#
1140681896Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1240681896Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1340681896Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1440681896Sopenharmony_ci# See the License for the specific language governing permissions and
1540681896Sopenharmony_ci# limitations under the License.
1640681896Sopenharmony_ci"""
1740681896Sopenharmony_ciDescription : Create script file for updater
1840681896Sopenharmony_ci"""
1940681896Sopenharmony_ci
2040681896Sopenharmony_ciimport os
2140681896Sopenharmony_ciimport re
2240681896Sopenharmony_ciimport tempfile
2340681896Sopenharmony_cifrom decimal import getcontext
2440681896Sopenharmony_cifrom decimal import Decimal
2540681896Sopenharmony_ci
2640681896Sopenharmony_cifrom log_exception import VendorExpandError
2740681896Sopenharmony_cifrom log_exception import UPDATE_LOGGER
2840681896Sopenharmony_cifrom utils import OPTIONS_MANAGER
2940681896Sopenharmony_cifrom utils import PARTITION_FILE
3040681896Sopenharmony_cifrom utils import TOTAL_SCRIPT_FILE_NAME
3140681896Sopenharmony_cifrom utils import SCRIPT_FILE_NAME
3240681896Sopenharmony_cifrom utils import SCRIPT_KEY_LIST
3340681896Sopenharmony_cifrom utils import UPDATE_BIN_FILE_NAME
3440681896Sopenharmony_ci
3540681896Sopenharmony_ci
3640681896Sopenharmony_ciclass Script:
3740681896Sopenharmony_ci
3840681896Sopenharmony_ci    def __init__(self):
3940681896Sopenharmony_ci        self.script = []
4040681896Sopenharmony_ci        self.version = 0
4140681896Sopenharmony_ci        self.info = {}
4240681896Sopenharmony_ci
4340681896Sopenharmony_ci    def add_command(self, cmd=None):
4440681896Sopenharmony_ci        """
4540681896Sopenharmony_ci        Add command content to the script.
4640681896Sopenharmony_ci        :param cmd: command content
4740681896Sopenharmony_ci        :return:
4840681896Sopenharmony_ci        """
4940681896Sopenharmony_ci        self.script.append(cmd)
5040681896Sopenharmony_ci
5140681896Sopenharmony_ci    def get_script(self):
5240681896Sopenharmony_ci        """
5340681896Sopenharmony_ci        Get the script list.
5440681896Sopenharmony_ci        :return: script list
5540681896Sopenharmony_ci        """
5640681896Sopenharmony_ci        return self.script
5740681896Sopenharmony_ci
5840681896Sopenharmony_ci    def sha_check(self, *args, **kwargs):
5940681896Sopenharmony_ci        raise VendorExpandError(type(self), 'sha_check')
6040681896Sopenharmony_ci
6140681896Sopenharmony_ci    def image_sha_check(self, *args, **kwargs):
6240681896Sopenharmony_ci        raise VendorExpandError(type(self), 'image_sha_check')
6340681896Sopenharmony_ci
6440681896Sopenharmony_ci    def first_block_check(self, *args, **kwargs):
6540681896Sopenharmony_ci        raise VendorExpandError(type(self), 'first_block_check')
6640681896Sopenharmony_ci
6740681896Sopenharmony_ci    def image_patch(self, *args, **kwargs):
6840681896Sopenharmony_ci        raise VendorExpandError(type(self), 'image_patch')
6940681896Sopenharmony_ci
7040681896Sopenharmony_ci    def abort(self, *args, **kwargs):
7140681896Sopenharmony_ci        raise VendorExpandError(type(self), 'abort')
7240681896Sopenharmony_ci
7340681896Sopenharmony_ci    def show_progress(self, *args, **kwargs):
7440681896Sopenharmony_ci        raise VendorExpandError(type(self), 'show_progress')
7540681896Sopenharmony_ci
7640681896Sopenharmony_ci    def block_update(self, *args, **kwargs):
7740681896Sopenharmony_ci        raise VendorExpandError(type(self), 'block_update')
7840681896Sopenharmony_ci
7940681896Sopenharmony_ci    def raw_image_write(self, *args, **kwargs):
8040681896Sopenharmony_ci        raise VendorExpandError(type(self), 'raw_image_write')
8140681896Sopenharmony_ci
8240681896Sopenharmony_ci    def get_status(self, *args, **kwargs):
8340681896Sopenharmony_ci        raise VendorExpandError(type(self), 'get_status')
8440681896Sopenharmony_ci
8540681896Sopenharmony_ci    def set_status(self, *args, **kwargs):
8640681896Sopenharmony_ci        raise VendorExpandError(type(self), 'set_status')
8740681896Sopenharmony_ci
8840681896Sopenharmony_ci    def reboot_now(self, *args, **kwargs):
8940681896Sopenharmony_ci        raise VendorExpandError(type(self), 'reboot_now')
9040681896Sopenharmony_ci
9140681896Sopenharmony_ci    def updater_partitions(self, *args, **kwargs):
9240681896Sopenharmony_ci        raise VendorExpandError(type(self), 'updater_partitions')
9340681896Sopenharmony_ci
9440681896Sopenharmony_ci
9540681896Sopenharmony_ciclass PreludeScript(Script):
9640681896Sopenharmony_ci    def __init__(self):
9740681896Sopenharmony_ci        super().__init__()
9840681896Sopenharmony_ci
9940681896Sopenharmony_ci
10040681896Sopenharmony_ciclass VerseScript(Script):
10140681896Sopenharmony_ci    def __init__(self):
10240681896Sopenharmony_ci        super().__init__()
10340681896Sopenharmony_ci
10440681896Sopenharmony_ci    def sha_check(self, ranges_str, expected_sha, partition):
10540681896Sopenharmony_ci        """
10640681896Sopenharmony_ci        Get the sha_check command.
10740681896Sopenharmony_ci        :param ranges_str: ranges string
10840681896Sopenharmony_ci        :param expected_sha: hash value
10940681896Sopenharmony_ci        :param partition: image name
11040681896Sopenharmony_ci        :return:
11140681896Sopenharmony_ci        """
11240681896Sopenharmony_ci        cmd = ('sha_check("/{partition}", "{ranges_str}", '
11340681896Sopenharmony_ci               '"{expected_sha}")').format(
11440681896Sopenharmony_ci            ranges_str=ranges_str,
11540681896Sopenharmony_ci            expected_sha=expected_sha, partition=partition)
11640681896Sopenharmony_ci        return cmd
11740681896Sopenharmony_ci
11840681896Sopenharmony_ci    def image_sha_check(self, partition, src_size, src_hash,
11940681896Sopenharmony_ci        dst_size, dst_hash):
12040681896Sopenharmony_ci        """
12140681896Sopenharmony_ci        Get the image_sha_check command.
12240681896Sopenharmony_ci        :param ranges_str: ranges string
12340681896Sopenharmony_ci        :param expected_sha: hash value
12440681896Sopenharmony_ci        :param partition: image name
12540681896Sopenharmony_ci        :return:
12640681896Sopenharmony_ci        """
12740681896Sopenharmony_ci        cmd = ('image_sha_check("/{partition}", '
12840681896Sopenharmony_ci                '"{src_size}", "{src_hash}", '
12940681896Sopenharmony_ci                '"{dst_size}", "{dst_hash}")').format(
13040681896Sopenharmony_ci                partition=partition, src_size=src_size, src_hash=src_hash,
13140681896Sopenharmony_ci                dst_size=dst_size, dst_hash=dst_hash)
13240681896Sopenharmony_ci        return cmd
13340681896Sopenharmony_ci
13440681896Sopenharmony_ci    def first_block_check(self, partition):
13540681896Sopenharmony_ci        """
13640681896Sopenharmony_ci        Get the first_block_check command.
13740681896Sopenharmony_ci        :param partition: image name
13840681896Sopenharmony_ci        :return:
13940681896Sopenharmony_ci        """
14040681896Sopenharmony_ci        cmd = 'first_block_check("/{partition}")'.format(
14140681896Sopenharmony_ci            partition=partition)
14240681896Sopenharmony_ci        return cmd
14340681896Sopenharmony_ci
14440681896Sopenharmony_ci    def abort(self, partition):
14540681896Sopenharmony_ci        """
14640681896Sopenharmony_ci        Get the abort command.
14740681896Sopenharmony_ci        :param partition: image name
14840681896Sopenharmony_ci        :return:
14940681896Sopenharmony_ci        """
15040681896Sopenharmony_ci        cmd = 'abort("ERROR: {partition} partition ' \
15140681896Sopenharmony_ci              'fails to incremental check!");\n'.format(
15240681896Sopenharmony_ci                partition=partition)
15340681896Sopenharmony_ci        return cmd
15440681896Sopenharmony_ci
15540681896Sopenharmony_ci    def show_progress(self, start_progress, dur):
15640681896Sopenharmony_ci        """
15740681896Sopenharmony_ci        Get the show_progress command.
15840681896Sopenharmony_ci        'dur' may be zero to advance the progress via SetProgress
15940681896Sopenharmony_ci        :param start_progress: start progress
16040681896Sopenharmony_ci        :param dur: seconds
16140681896Sopenharmony_ci        :return:
16240681896Sopenharmony_ci        """
16340681896Sopenharmony_ci        cmd = 'show_progress({start_progress}, {dur});\n'.format(
16440681896Sopenharmony_ci            start_progress=float(start_progress), dur=float(dur))
16540681896Sopenharmony_ci        return cmd
16640681896Sopenharmony_ci
16740681896Sopenharmony_ci    def image_patch(self, partition, src_size, src_hash, target_size, target_hash):
16840681896Sopenharmony_ci        """
16940681896Sopenharmony_ci        Get the image_patch command.
17040681896Sopenharmony_ci        :param partition:  image name
17140681896Sopenharmony_ci        :return:
17240681896Sopenharmony_ci        """
17340681896Sopenharmony_ci        cmd = 'image_patch("/{partition}", "{src_size}", ' \
17440681896Sopenharmony_ci              '"{src_hash}", "{target_size}", "{target_hash}", ' \
17540681896Sopenharmony_ci              '"{partition}.patch.dat");\n'.format(partition=partition, src_size=src_size,
17640681896Sopenharmony_ci              src_hash=src_hash, target_size=target_size, target_hash=target_hash)
17740681896Sopenharmony_ci        return cmd
17840681896Sopenharmony_ci
17940681896Sopenharmony_ci    def block_update(self, partition):
18040681896Sopenharmony_ci        """
18140681896Sopenharmony_ci        Get the block_update command.
18240681896Sopenharmony_ci        :param partition:  image name
18340681896Sopenharmony_ci        :return:
18440681896Sopenharmony_ci        """
18540681896Sopenharmony_ci        cmd = 'block_update("/{partition}", ' \
18640681896Sopenharmony_ci              '"{partition}.transfer.list", "{partition}.new.dat", ' \
18740681896Sopenharmony_ci              '"{partition}.patch.dat");\n'.format(partition=partition)
18840681896Sopenharmony_ci        return cmd
18940681896Sopenharmony_ci
19040681896Sopenharmony_ci    def image_write(self, partition, image_name, image_path):
19140681896Sopenharmony_ci        return self.raw_image_write(partition, image_name)
19240681896Sopenharmony_ci
19340681896Sopenharmony_ci    def raw_image_write(self, partition, image_name):
19440681896Sopenharmony_ci        """
19540681896Sopenharmony_ci        Get the raw_image_write command.
19640681896Sopenharmony_ci        :param partition:  image name
19740681896Sopenharmony_ci        :return:
19840681896Sopenharmony_ci        """
19940681896Sopenharmony_ci        cmd = 'raw_image_write("/%s", "/%s");\n' % (partition, image_name)
20040681896Sopenharmony_ci        return cmd
20140681896Sopenharmony_ci
20240681896Sopenharmony_ci    def get_status(self):
20340681896Sopenharmony_ci        """
20440681896Sopenharmony_ci        Get the get_status command.
20540681896Sopenharmony_ci        :return:
20640681896Sopenharmony_ci        """
20740681896Sopenharmony_ci        cmd = 'get_status("/misc")'
20840681896Sopenharmony_ci        return cmd
20940681896Sopenharmony_ci
21040681896Sopenharmony_ci    def set_status(self, status_value):
21140681896Sopenharmony_ci        """
21240681896Sopenharmony_ci        Get the set_status command.
21340681896Sopenharmony_ci        :param status_value: status value to be set
21440681896Sopenharmony_ci        :return:
21540681896Sopenharmony_ci        """
21640681896Sopenharmony_ci        cmd = 'set_status("/misc", %s);\n' % status_value
21740681896Sopenharmony_ci        return cmd
21840681896Sopenharmony_ci
21940681896Sopenharmony_ci    def reboot_now(self):
22040681896Sopenharmony_ci        """
22140681896Sopenharmony_ci        Get the reboot_now command.
22240681896Sopenharmony_ci        :return:
22340681896Sopenharmony_ci        """
22440681896Sopenharmony_ci        cmd = 'reboot_now();\n'
22540681896Sopenharmony_ci        return cmd
22640681896Sopenharmony_ci
22740681896Sopenharmony_ci    def updater_partitions(self):
22840681896Sopenharmony_ci        """
22940681896Sopenharmony_ci        Get the updater_partitions command.
23040681896Sopenharmony_ci        :return:
23140681896Sopenharmony_ci        """
23240681896Sopenharmony_ci        cmd = 'update_partitions("/%s");\n' % PARTITION_FILE
23340681896Sopenharmony_ci        return cmd
23440681896Sopenharmony_ci
23540681896Sopenharmony_ci    def full_image_update(self, update_file_name):
23640681896Sopenharmony_ci        cmd = 'update_from_bin("%s");\n' % update_file_name
23740681896Sopenharmony_ci        return cmd
23840681896Sopenharmony_ci
23940681896Sopenharmony_ci    def pkg_extract(self, pkg_file_name, dest_path):
24040681896Sopenharmony_ci        cmd = 'pkg_extract("%s", "%s");\n' % (pkg_file_name, dest_path)
24140681896Sopenharmony_ci        return cmd
24240681896Sopenharmony_ci
24340681896Sopenharmony_ci
24440681896Sopenharmony_ciclass RefrainScript(Script):
24540681896Sopenharmony_ci    def __init__(self):
24640681896Sopenharmony_ci        super().__init__()
24740681896Sopenharmony_ci
24840681896Sopenharmony_ci
24940681896Sopenharmony_ciclass EndingScript(Script):
25040681896Sopenharmony_ci    def __init__(self):
25140681896Sopenharmony_ci        super().__init__()
25240681896Sopenharmony_ci
25340681896Sopenharmony_ci
25440681896Sopenharmony_cidef write_script(script_content, opera_name):
25540681896Sopenharmony_ci    """
25640681896Sopenharmony_ci    Generate the {opera}script.
25740681896Sopenharmony_ci    :param script_content: script content
25840681896Sopenharmony_ci    :param opera_name: Opera phase names corresponding to the script content
25940681896Sopenharmony_ci                    'prelude', 'verse', 'refrain', and 'ending'.
26040681896Sopenharmony_ci    :return:
26140681896Sopenharmony_ci    """
26240681896Sopenharmony_ci    script_file = tempfile.NamedTemporaryFile(mode='w+')
26340681896Sopenharmony_ci    script_file.write(script_content)
26440681896Sopenharmony_ci    script_file.seek(0)
26540681896Sopenharmony_ci    script_file_name = ''.join([opera_name.title(), SCRIPT_FILE_NAME])
26640681896Sopenharmony_ci    OPTIONS_MANAGER.opera_script_file_name_dict[opera_name].\
26740681896Sopenharmony_ci        append((script_file_name, script_file))
26840681896Sopenharmony_ci    UPDATE_LOGGER.print_log("%s generation complete!" % script_file_name)
26940681896Sopenharmony_ci
27040681896Sopenharmony_ci
27140681896Sopenharmony_cidef generate_total_script():
27240681896Sopenharmony_ci    """
27340681896Sopenharmony_ci    Generate the overall script.
27440681896Sopenharmony_ci    """
27540681896Sopenharmony_ci    content_list = []
27640681896Sopenharmony_ci    for each_key, each_value in \
27740681896Sopenharmony_ci            OPTIONS_MANAGER.opera_script_file_name_dict.items():
27840681896Sopenharmony_ci        for each in each_value:
27940681896Sopenharmony_ci            each_content = "LoadScript(\"%s\", %s);" % \
28040681896Sopenharmony_ci                           (each[0], SCRIPT_KEY_LIST.index(each_key))
28140681896Sopenharmony_ci            content_list.append(each_content)
28240681896Sopenharmony_ci    script_total = tempfile.NamedTemporaryFile(mode='w+')
28340681896Sopenharmony_ci    script_total.write('\n'.join(content_list))
28440681896Sopenharmony_ci    script_total.seek(0)
28540681896Sopenharmony_ci    OPTIONS_MANAGER.total_script_file_obj = script_total
28640681896Sopenharmony_ci    UPDATE_LOGGER.print_log("%s generation complete!" % TOTAL_SCRIPT_FILE_NAME)
28740681896Sopenharmony_ci
28840681896Sopenharmony_ci
28940681896Sopenharmony_cidef get_progress_value(distributable_value=100):
29040681896Sopenharmony_ci    """
29140681896Sopenharmony_ci    Allocate a progress value to each image update.
29240681896Sopenharmony_ci    :param distributable_value: distributable value
29340681896Sopenharmony_ci    :return:
29440681896Sopenharmony_ci    """
29540681896Sopenharmony_ci    progress_value_dict = {}
29640681896Sopenharmony_ci    full_img_list = OPTIONS_MANAGER.full_img_list
29740681896Sopenharmony_ci    incremental_img_list = OPTIONS_MANAGER.incremental_img_list
29840681896Sopenharmony_ci    file_size_list = []
29940681896Sopenharmony_ci    each_img_size = 0
30040681896Sopenharmony_ci    if len(full_img_list) == 0 and len(incremental_img_list) == 0:
30140681896Sopenharmony_ci        UPDATE_LOGGER.print_log(
30240681896Sopenharmony_ci            "get progress value failed! > getting progress value failed!",
30340681896Sopenharmony_ci            UPDATE_LOGGER.ERROR_LOG)
30440681896Sopenharmony_ci        return False
30540681896Sopenharmony_ci    for partition in incremental_img_list:
30640681896Sopenharmony_ci        # Obtain the size of the incremental image file.
30740681896Sopenharmony_ci        if partition in OPTIONS_MANAGER.incremental_image_file_obj_dict:
30840681896Sopenharmony_ci            file_obj = OPTIONS_MANAGER.incremental_image_file_obj_dict[partition]
30940681896Sopenharmony_ci            each_img_size = os.path.getsize(file_obj.name)
31040681896Sopenharmony_ci        elif partition in OPTIONS_MANAGER.incremental_block_file_obj_dict:
31140681896Sopenharmony_ci            new_dat_file_obj, patch_dat_file_obj, transfer_list_file_obj =\
31240681896Sopenharmony_ci                OPTIONS_MANAGER.incremental_block_file_obj_dict[partition].get_file_obj()
31340681896Sopenharmony_ci            each_img_size = os.path.getsize(new_dat_file_obj.name) + os.path.getsize(patch_dat_file_obj.name)
31440681896Sopenharmony_ci        file_size_list.append(each_img_size)
31540681896Sopenharmony_ci
31640681896Sopenharmony_ci    total_full_img_size = 0
31740681896Sopenharmony_ci    for idx, _ in enumerate(full_img_list):
31840681896Sopenharmony_ci        # Obtain the size of the full image file.
31940681896Sopenharmony_ci        file_obj = OPTIONS_MANAGER.full_image_file_obj_list[idx]
32040681896Sopenharmony_ci        total_full_img_size += os.path.getsize(file_obj.name)
32140681896Sopenharmony_ci    file_size_list.append(total_full_img_size)
32240681896Sopenharmony_ci
32340681896Sopenharmony_ci    proportion_value_list = get_proportion_value_list(
32440681896Sopenharmony_ci        file_size_list, distributable_value=distributable_value)
32540681896Sopenharmony_ci
32640681896Sopenharmony_ci    adjusted_proportion_value_list = adjust_proportion_value_list(
32740681896Sopenharmony_ci        proportion_value_list, distributable_value)
32840681896Sopenharmony_ci
32940681896Sopenharmony_ci    all_img_list = incremental_img_list + [UPDATE_BIN_FILE_NAME]
33040681896Sopenharmony_ci    current_progress = 40
33140681896Sopenharmony_ci    for idx, each_img in enumerate(all_img_list):
33240681896Sopenharmony_ci        temp_progress = current_progress + adjusted_proportion_value_list[idx]
33340681896Sopenharmony_ci        progress_value_dict[each_img] = (current_progress, temp_progress)
33440681896Sopenharmony_ci        current_progress = temp_progress
33540681896Sopenharmony_ci    return progress_value_dict
33640681896Sopenharmony_ci
33740681896Sopenharmony_ci
33840681896Sopenharmony_cidef get_proportion_value_list(file_size_list, distributable_value=100):
33940681896Sopenharmony_ci    """
34040681896Sopenharmony_ci    Obtain the calculated progress proportion value list
34140681896Sopenharmony_ci    (proportion_value_list).
34240681896Sopenharmony_ci    :param file_size_list: file size list
34340681896Sopenharmony_ci    :param distributable_value: distributable value
34440681896Sopenharmony_ci    :return proportion_value_list: progress proportion value list
34540681896Sopenharmony_ci    """
34640681896Sopenharmony_ci    sum_size = sum(file_size_list)
34740681896Sopenharmony_ci    getcontext().prec = 2
34840681896Sopenharmony_ci    proportion_value_list = []
34940681896Sopenharmony_ci    for each_size_value in file_size_list:
35040681896Sopenharmony_ci        proportion = Decimal(str(float(each_size_value))) / Decimal(
35140681896Sopenharmony_ci            str(float(sum_size)))
35240681896Sopenharmony_ci        proportion_value = int(
35340681896Sopenharmony_ci            Decimal(str(proportion)) *
35440681896Sopenharmony_ci            Decimal(str(float(distributable_value))))
35540681896Sopenharmony_ci        if proportion_value == 0:
35640681896Sopenharmony_ci            proportion_value = 1
35740681896Sopenharmony_ci        proportion_value_list.append(proportion_value)
35840681896Sopenharmony_ci    return proportion_value_list
35940681896Sopenharmony_ci
36040681896Sopenharmony_ci
36140681896Sopenharmony_cidef adjust_proportion_value_list(proportion_value_list, distributable_value):
36240681896Sopenharmony_ci    """
36340681896Sopenharmony_ci    Adjust the calculated progress proportion value list to ensure that
36440681896Sopenharmony_ci    sum is equal to distributable_value.
36540681896Sopenharmony_ci    :param proportion_value_list: calculated progress proportion value list
36640681896Sopenharmony_ci    :param distributable_value: number of distributable progress values
36740681896Sopenharmony_ci    :return proportion_value_list: new progress proportion value list
36840681896Sopenharmony_ci    """
36940681896Sopenharmony_ci    if len(proportion_value_list) == 0:
37040681896Sopenharmony_ci        return []
37140681896Sopenharmony_ci    sum_proportion_value = sum(proportion_value_list)
37240681896Sopenharmony_ci    if sum_proportion_value > distributable_value:
37340681896Sopenharmony_ci        max_value = max(proportion_value_list)
37440681896Sopenharmony_ci        max_idx = proportion_value_list.index(max_value)
37540681896Sopenharmony_ci        proportion_value_list[max_idx] = \
37640681896Sopenharmony_ci            max_value - (sum_proportion_value - distributable_value)
37740681896Sopenharmony_ci    elif sum_proportion_value < distributable_value:
37840681896Sopenharmony_ci        min_value = min(proportion_value_list)
37940681896Sopenharmony_ci        min_idx = proportion_value_list.index(min_value)
38040681896Sopenharmony_ci        proportion_value_list[min_idx] = \
38140681896Sopenharmony_ci            min_value + (distributable_value - sum_proportion_value)
38240681896Sopenharmony_ci    return proportion_value_list
38340681896Sopenharmony_ci
38440681896Sopenharmony_ci
38540681896Sopenharmony_cidef create_script(prelude_script, verse_script,
38640681896Sopenharmony_ci                  refrain_script, ending_script):
38740681896Sopenharmony_ci    """
38840681896Sopenharmony_ci    Generate the script file.
38940681896Sopenharmony_ci    :param prelude_script: prelude script
39040681896Sopenharmony_ci    :param verse_script: verse script
39140681896Sopenharmony_ci    :param refrain_script: refrain script
39240681896Sopenharmony_ci    :param ending_script: ending script
39340681896Sopenharmony_ci    :return:
39440681896Sopenharmony_ci    """
39540681896Sopenharmony_ci    # Generate the prelude script.
39640681896Sopenharmony_ci    prelude_script.add_command("\n# ---- prelude ----\n")
39740681896Sopenharmony_ci
39840681896Sopenharmony_ci    # Get the distribution progress.
39940681896Sopenharmony_ci    progress_value_dict = get_progress_value()
40040681896Sopenharmony_ci    if progress_value_dict is False:
40140681896Sopenharmony_ci        return False
40240681896Sopenharmony_ci    verse_script_content_list = verse_script.get_script()
40340681896Sopenharmony_ci    updater_content = []
40440681896Sopenharmony_ci    verse_script_content = '\n'.join(verse_script_content_list)
40540681896Sopenharmony_ci
40640681896Sopenharmony_ci    for key, value in progress_value_dict.items():
40740681896Sopenharmony_ci        show_progress_content = \
40840681896Sopenharmony_ci            verse_script.show_progress((value[1] - value[0]) / 100, 0)
40940681896Sopenharmony_ci        verse_script_content = \
41040681896Sopenharmony_ci            re.sub(r'%s_WRITE_FLAG' % key, '%s' % show_progress_content,
41140681896Sopenharmony_ci                   verse_script_content, count=1)
41240681896Sopenharmony_ci    # Generate the verse script.
41340681896Sopenharmony_ci    write_script(verse_script_content, 'verse')
41440681896Sopenharmony_ci    # Generate the refrain script.
41540681896Sopenharmony_ci    refrain_script.add_command("\n# ---- refrain ----\n")
41640681896Sopenharmony_ci    # Generate the ending script.
41740681896Sopenharmony_ci    ending_script.add_command("\n# ---- ending ----\n")
41840681896Sopenharmony_ci
41940681896Sopenharmony_ci    generate_total_script()
420