Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers package_release_files.py Source File

package_release_files.py

00001 #
00002 # DAPLink Interface Firmware
00003 # Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00004 # SPDX-License-Identifier: Apache-2.0
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00007 # not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 # http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00014 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 #
00018 
00019 import os
00020 import sys
00021 import shutil
00022 import argparse
00023 import yaml
00024 import subprocess
00025 
00026 self_path = os.path.abspath(__file__)
00027 tools_dir = os.path.dirname(self_path)
00028 daplink_dir = os.path.dirname(tools_dir)
00029 test_dir = os.path.join(daplink_dir, "test")
00030 sys.path.append(test_dir)
00031 
00032 import info
00033 
00034 from make_update_yml import DefaultList
00035 from make_update_yml import TargetList
00036 from make_update_yml import InstructionsText
00037 from make_update_yml import make_update_yml_file
00038 
00039 import zipfile
00040 
00041 def make_bin_zip(dir, name):
00042     working_dir = os.getcwd()
00043     os.chdir(dir)
00044     with zipfile.ZipFile(name, mode='w') as zipf:
00045         for file in os.listdir("./"):
00046             if file.endswith(".bin") or file.endswith(".hex"):
00047                 zipf.write(os.path.join("./", file))
00048     #go back
00049     os.chdir(working_dir)
00050 
00051 def package_release_files(source, dest, version, toolchain):
00052 
00053     proj_dir = source
00054     output_dir = dest
00055     build_number = "%04i" % version
00056 
00057     update_yml_entries = [{'default':DefaultList([
00058             ('website', 'http://os.mbed.com/platforms'),
00059             ('fw_version', "'" + build_number + "'"),
00060             ('image_format', '.bin'),
00061             ('instructions', InstructionsText['default'])
00062             ]) }]
00063 
00064     if os.path.exists(output_dir):
00065         shutil.rmtree(output_dir)
00066         print (output_dir + ' existed and deleted!!')
00067 
00068     os.mkdir(output_dir)
00069 
00070     for prj_name, legacy, offset, extension in info.PROJECT_RELEASE_INFO:
00071         legacy_str = "_legacy" if legacy else ""
00072         source_offset_str = "_0x%04x" % offset if legacy else ""
00073         source_name = prj_name + "_crc" + legacy_str + source_offset_str + "." + extension
00074         source_dir_part = os.path.join(proj_dir, prj_name, toolchain).upper()
00075         source_path = os.path.join(source_dir_part, source_name)
00076         if not os.path.isfile(source_path):
00077             print("Warning %s not added to release" % prj_name)
00078             continue
00079         items = prj_name.split('_')  # "k20dx_frdmk22f_if" -> ("k20dx", "frdmk22f", "if")
00080         assert items[-1] == "if", "Unexpected string: %s" % items[2]
00081         host_mcu = items[0]
00082         base_name = '_'.join(items[1:-1])
00083         dest_offset_str = "_0x%04x" % offset
00084         dest_name = build_number + "_" + host_mcu + "_" + base_name + dest_offset_str + "." + extension
00085         dest_path = os.path.join(output_dir, dest_name)
00086         shutil.copyfile(source_path, dest_path)
00087 
00088         product_code = 'NOT SUPPORTED'
00089         for board_id, family_id, fimware, bootloader, target in info.SUPPORTED_CONFIGURATIONS:
00090             if fimware == prj_name:
00091                 product_code = board_id
00092                 if target is not None:
00093                     target_name = target
00094                 else:
00095                     target_name = base_name.upper()
00096 
00097                 fw_instuction = InstructionsText['default']
00098                 for fw_name_key in InstructionsText:
00099                     if fw_name_key in dest_name.lower():
00100                         fw_instuction = InstructionsText[fw_name_key]
00101                         break;
00102 
00103                 if extension == 'bin':
00104                     update_yml_entries.append({target_name:TargetList([
00105                         ('name', target_name),
00106                         ('product_code', "'" + format(product_code, '04x') + "'"),
00107                         ('fw_name', host_mcu + "_" + base_name + dest_offset_str),
00108                         ('instructions', fw_instuction)
00109                         ])});
00110 
00111     make_bin_zip(output_dir, build_number + '_release_package_' + subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip() + '.zip')
00112 
00113     make_update_yml_file(os.path.join(output_dir, 'update.yml'), update_yml_entries, explicit_start=True)
00114 
00115 if __name__ == "__main__":
00116     parser = argparse.ArgumentParser(description='Package a release for distribution')
00117     parser.add_argument('source', help='Release directory to grab files from')
00118     parser.add_argument('dest', help='Directory to create and place files in')
00119     parser.add_argument('version', type=int, help='Version number of this release')
00120     parser.add_argument('--toolchain', type=str, default='', help='Toolchain directory if present')
00121     args = parser.parse_args()
00122 
00123     print("args",args.source,args.dest,args.version,args.toolchain)
00124     
00125     package_release_files(args.source,args.dest,args.version,args.toolchain)