Amit Gandhi / mbed-dev

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Wed Apr 12 16:21:43 2017 +0100
Revision:
162:e13f6fdb2ac4
This updates the lib to the mbed lib v140

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 162:e13f6fdb2ac4 1 #!python3
<> 162:e13f6fdb2ac4 2 import os, shutil, json, pprint, sys, string, json, argparse
<> 162:e13f6fdb2ac4 3 from collections import OrderedDict
<> 162:e13f6fdb2ac4 4 from shutil import copyfile, copytree
<> 162:e13f6fdb2ac4 5
<> 162:e13f6fdb2ac4 6 def rename_sdk_old_dirs(path, dry_run = False):
<> 162:e13f6fdb2ac4 7 # I make assumption that all old sdk dirs have "sdk" names.
<> 162:e13f6fdb2ac4 8 sdk_dir_name = "sdk"
<> 162:e13f6fdb2ac4 9 path = "."
<> 162:e13f6fdb2ac4 10
<> 162:e13f6fdb2ac4 11 for root, dirs, files in os.walk(path):
<> 162:e13f6fdb2ac4 12 for name in dirs:
<> 162:e13f6fdb2ac4 13 if name == "sdk":
<> 162:e13f6fdb2ac4 14 full_path = root + "\\" + name
<> 162:e13f6fdb2ac4 15 new_full_path = root + "\\_old_" + name
<> 162:e13f6fdb2ac4 16 print("rename " + full_path + " ---> " + new_full_path)
<> 162:e13f6fdb2ac4 17 if not dry_run:
<> 162:e13f6fdb2ac4 18 os.rename(full_path, new_full_path)
<> 162:e13f6fdb2ac4 19 os.mkdir(full_path)
<> 162:e13f6fdb2ac4 20
<> 162:e13f6fdb2ac4 21 def rename_dirs(sdk_dirs_in_mbed, new_name, dry_run=False):
<> 162:e13f6fdb2ac4 22
<> 162:e13f6fdb2ac4 23 for dir_path in sdk_dirs_in_mbed:
<> 162:e13f6fdb2ac4 24 xdir_path = os.path.join('.',dir_path)
<> 162:e13f6fdb2ac4 25 new_dir_path = os.path.join(os.path.dirname(xdir_path), new_name)
<> 162:e13f6fdb2ac4 26 print("rename " + xdir_path + " ---> " + new_dir_path)
<> 162:e13f6fdb2ac4 27 if not dry_run:
<> 162:e13f6fdb2ac4 28 os.rename(xdir_path, new_dir_path)
<> 162:e13f6fdb2ac4 29
<> 162:e13f6fdb2ac4 30 def get_file_pathes_couples(path_sdk_componets, skip_dirs = [], skip_files = [], verbose = False):
<> 162:e13f6fdb2ac4 31 mbed_list = []
<> 162:e13f6fdb2ac4 32 cutted_roots = []
<> 162:e13f6fdb2ac4 33 cutted_files = []
<> 162:e13f6fdb2ac4 34
<> 162:e13f6fdb2ac4 35 path_sdk_componets = path_sdk_componets + '\\'
<> 162:e13f6fdb2ac4 36
<> 162:e13f6fdb2ac4 37 for root, dirs, files in os.walk(path_sdk_componets):
<> 162:e13f6fdb2ac4 38 procced = True
<> 162:e13f6fdb2ac4 39 cutted_root = root[len(path_sdk_componets):]
<> 162:e13f6fdb2ac4 40
<> 162:e13f6fdb2ac4 41 for skip_d in skip_dirs:
<> 162:e13f6fdb2ac4 42 if 0 == string.find(cutted_root, skip_d):
<> 162:e13f6fdb2ac4 43 cutted_roots.append(cutted_root)
<> 162:e13f6fdb2ac4 44 procced = False
<> 162:e13f6fdb2ac4 45
<> 162:e13f6fdb2ac4 46 if procced:
<> 162:e13f6fdb2ac4 47 for file_name in files:
<> 162:e13f6fdb2ac4 48
<> 162:e13f6fdb2ac4 49 procced = True
<> 162:e13f6fdb2ac4 50 for skip_f in skip_files:
<> 162:e13f6fdb2ac4 51 if (-1) != string.find(file_name, skip_f):
<> 162:e13f6fdb2ac4 52 cutted_files.append(file_name)
<> 162:e13f6fdb2ac4 53 procced = False
<> 162:e13f6fdb2ac4 54
<> 162:e13f6fdb2ac4 55 if procced:
<> 162:e13f6fdb2ac4 56 if file_name.endswith((".c", ".h")):
<> 162:e13f6fdb2ac4 57 #cutted_path = cutted_root + "\\" + file_name
<> 162:e13f6fdb2ac4 58 cutted_path = os.path.join(cutted_root, file_name)
<> 162:e13f6fdb2ac4 59 #full_path = root + "\\" + file_name
<> 162:e13f6fdb2ac4 60 full_path = os.path.join(root, file_name)
<> 162:e13f6fdb2ac4 61 item = {"full_path": full_path, "id": cutted_path, "cutted_root": cutted_root}
<> 162:e13f6fdb2ac4 62 #mbed_list.append([full_path, cutted_path])
<> 162:e13f6fdb2ac4 63 mbed_list.append(item)
<> 162:e13f6fdb2ac4 64
<> 162:e13f6fdb2ac4 65 if verbose:
<> 162:e13f6fdb2ac4 66 print("\r\nskipped directories: {0:#d}".format(len(cutted_roots)))
<> 162:e13f6fdb2ac4 67
<> 162:e13f6fdb2ac4 68 for xitem in cutted_roots:
<> 162:e13f6fdb2ac4 69 print(xitem)
<> 162:e13f6fdb2ac4 70
<> 162:e13f6fdb2ac4 71 print("\r\nskipped files: {0:#d}".format(len(cutted_files)))
<> 162:e13f6fdb2ac4 72
<> 162:e13f6fdb2ac4 73 for kitem in cutted_files:
<> 162:e13f6fdb2ac4 74 print(kitem)
<> 162:e13f6fdb2ac4 75
<> 162:e13f6fdb2ac4 76 return mbed_list
<> 162:e13f6fdb2ac4 77
<> 162:e13f6fdb2ac4 78 def apply_replacement_id(mbed_list, replacemet_couples):
<> 162:e13f6fdb2ac4 79 for item in mbed_list:
<> 162:e13f6fdb2ac4 80 splited = os.path.split(item["id"])
<> 162:e13f6fdb2ac4 81 result = string.find(splited[1], replacemet_couples["old"])
<> 162:e13f6fdb2ac4 82 if result != -1:
<> 162:e13f6fdb2ac4 83 new_tail = replacemet_couples["new"] + splited[1][len(replacemet_couples["old"]):]
<> 162:e13f6fdb2ac4 84 item["id"] = os.path.join(splited[0],new_tail)
<> 162:e13f6fdb2ac4 85 #print('bingo!')
<> 162:e13f6fdb2ac4 86 #print(item)
<> 162:e13f6fdb2ac4 87
<> 162:e13f6fdb2ac4 88 return mbed_list
<> 162:e13f6fdb2ac4 89
<> 162:e13f6fdb2ac4 90
<> 162:e13f6fdb2ac4 91 def get_copying_automatic_list(list_mbed, list_sdk, mbed_port_path = '', verbose = False):
<> 162:e13f6fdb2ac4 92 copy_list = [] #list of copy items
<> 162:e13f6fdb2ac4 93 orphan_list = []
<> 162:e13f6fdb2ac4 94
<> 162:e13f6fdb2ac4 95 licz = 0
<> 162:e13f6fdb2ac4 96 for pathes_mbed in list_mbed:
<> 162:e13f6fdb2ac4 97 empty = True
<> 162:e13f6fdb2ac4 98 for pathes_sdk in list_sdk:
<> 162:e13f6fdb2ac4 99 if pathes_mbed["id"] == pathes_sdk["id"]:
<> 162:e13f6fdb2ac4 100 dest_path = pathes_mbed["full_path"]
<> 162:e13f6fdb2ac4 101
<> 162:e13f6fdb2ac4 102 dest_path = dest_path[ (len(mbed_port_path)):]
<> 162:e13f6fdb2ac4 103
<> 162:e13f6fdb2ac4 104 item = {"id" : pathes_mbed["id"], "src_path": pathes_sdk["full_path"], "dest_path": dest_path, "old_path": pathes_mbed["full_path"]}
<> 162:e13f6fdb2ac4 105 copy_list.append(item)
<> 162:e13f6fdb2ac4 106
<> 162:e13f6fdb2ac4 107 empty = False;
<> 162:e13f6fdb2ac4 108
<> 162:e13f6fdb2ac4 109
<> 162:e13f6fdb2ac4 110 if empty:
<> 162:e13f6fdb2ac4 111 orphan_list.append(pathes_mbed["full_path"])
<> 162:e13f6fdb2ac4 112
<> 162:e13f6fdb2ac4 113 print("\r\nfitted files: {0:#d}".format(len(copy_list)))
<> 162:e13f6fdb2ac4 114
<> 162:e13f6fdb2ac4 115 if verbose:
<> 162:e13f6fdb2ac4 116 for item in copy_list:
<> 162:e13f6fdb2ac4 117 str_verbose = "{0} --> {1}"
<> 162:e13f6fdb2ac4 118 print(str_verbose.format(item["id"], item["dest_path"]))
<> 162:e13f6fdb2ac4 119
<> 162:e13f6fdb2ac4 120
<> 162:e13f6fdb2ac4 121 print("\r\norphaned files: {0:#d}".format(len(orphan_list)))
<> 162:e13f6fdb2ac4 122
<> 162:e13f6fdb2ac4 123 if verbose:
<> 162:e13f6fdb2ac4 124 for xitem in orphan_list:
<> 162:e13f6fdb2ac4 125 print(xitem)
<> 162:e13f6fdb2ac4 126
<> 162:e13f6fdb2ac4 127 return copy_list
<> 162:e13f6fdb2ac4 128
<> 162:e13f6fdb2ac4 129 def is_in_copying_list(copy_list, file_id):
<> 162:e13f6fdb2ac4 130 for pathes_copy in copy_list:
<> 162:e13f6fdb2ac4 131 if pathes_copy["id"] == file_id:
<> 162:e13f6fdb2ac4 132
<> 162:e13f6fdb2ac4 133 return False
<> 162:e13f6fdb2ac4 134
<> 162:e13f6fdb2ac4 135 return True
<> 162:e13f6fdb2ac4 136
<> 162:e13f6fdb2ac4 137
<> 162:e13f6fdb2ac4 138 def upgrade_copying_list(copy_list, pathes_sdk, dest_mbed_dir_path, print_list):
<> 162:e13f6fdb2ac4 139 splited = os.path.split(pathes_sdk["id"])
<> 162:e13f6fdb2ac4 140 dest_path = os.path.join(dest_mbed_dir_path, splited[1])
<> 162:e13f6fdb2ac4 141 item = {"id" : pathes_sdk["id"], "src_path": pathes_sdk["full_path"], "dest_path": dest_path} #, "old_path": pathes_mbed["full_path"]}
<> 162:e13f6fdb2ac4 142 copy_list.append(item)
<> 162:e13f6fdb2ac4 143 print_list.append(item)
<> 162:e13f6fdb2ac4 144
<> 162:e13f6fdb2ac4 145
<> 162:e13f6fdb2ac4 146
<> 162:e13f6fdb2ac4 147
<> 162:e13f6fdb2ac4 148 def upgrade_copying_list_by_dirs(copy_list, list_sdk, force_copy_dirs_list, port_relative_dir = '',verbose = False):
<> 162:e13f6fdb2ac4 149 print_list = []
<> 162:e13f6fdb2ac4 150
<> 162:e13f6fdb2ac4 151 for pathes_sdk in list_sdk:
<> 162:e13f6fdb2ac4 152 if is_in_copying_list(copy_list, pathes_sdk["id"]):
<> 162:e13f6fdb2ac4 153
<> 162:e13f6fdb2ac4 154 make_hard_copy = False
<> 162:e13f6fdb2ac4 155
<> 162:e13f6fdb2ac4 156 for hard_copy_dir in force_copy_dirs_list:
<> 162:e13f6fdb2ac4 157
<> 162:e13f6fdb2ac4 158 if 0 == string.find(pathes_sdk["cutted_root"], hard_copy_dir["sdk_dir"]):
<> 162:e13f6fdb2ac4 159
<> 162:e13f6fdb2ac4 160 make_hard_copy = True
<> 162:e13f6fdb2ac4 161
<> 162:e13f6fdb2ac4 162 post_path = os.path.relpath(pathes_sdk["cutted_root"], hard_copy_dir["sdk_dir"])
<> 162:e13f6fdb2ac4 163
<> 162:e13f6fdb2ac4 164 if post_path == '.':
<> 162:e13f6fdb2ac4 165 corect_hard_copy_dir = hard_copy_dir["mbed_dir"]
<> 162:e13f6fdb2ac4 166 if post_path != '.': # destynation is a nested directory
<> 162:e13f6fdb2ac4 167 corect_hard_copy_dir = os.path.join(hard_copy_dir["mbed_dir"], post_path)
<> 162:e13f6fdb2ac4 168
<> 162:e13f6fdb2ac4 169 corect_hard_copy_dir = os.path.join(port_relative_dir, corect_hard_copy_dir)
<> 162:e13f6fdb2ac4 170
<> 162:e13f6fdb2ac4 171 upgrade_copying_list(copy_list, pathes_sdk, corect_hard_copy_dir, print_list)
<> 162:e13f6fdb2ac4 172 break
<> 162:e13f6fdb2ac4 173
<> 162:e13f6fdb2ac4 174
<> 162:e13f6fdb2ac4 175 print("\r\nforced copy of files by directories: {0:#d}".format(len(print_list)))
<> 162:e13f6fdb2ac4 176
<> 162:e13f6fdb2ac4 177 if verbose:
<> 162:e13f6fdb2ac4 178 for item in print_list:
<> 162:e13f6fdb2ac4 179 str_verbose = "{0} --> {1}"
<> 162:e13f6fdb2ac4 180 print(str_verbose.format(item["id"], item["dest_path"]))
<> 162:e13f6fdb2ac4 181
<> 162:e13f6fdb2ac4 182 def upgrade_copying_list_by_files(copy_list, list_sdk, force_copy_files_list, port_relative_dir ='',verbose = False):
<> 162:e13f6fdb2ac4 183 print_list = []
<> 162:e13f6fdb2ac4 184
<> 162:e13f6fdb2ac4 185 for pathes_sdk in list_sdk:
<> 162:e13f6fdb2ac4 186 if is_in_copying_list(copy_list, pathes_sdk["id"]):
<> 162:e13f6fdb2ac4 187
<> 162:e13f6fdb2ac4 188 make_hard_copy = False
<> 162:e13f6fdb2ac4 189
<> 162:e13f6fdb2ac4 190 for hard_copy_file in force_copy_files_list:
<> 162:e13f6fdb2ac4 191 if pathes_sdk["id"] == hard_copy_file["sdk_file"]:
<> 162:e13f6fdb2ac4 192 make_hard_copy = True
<> 162:e13f6fdb2ac4 193
<> 162:e13f6fdb2ac4 194 corect_hard_copy_dir = os.path.join(port_relative_dir, hard_copy_file["mbed_dir"])
<> 162:e13f6fdb2ac4 195
<> 162:e13f6fdb2ac4 196 upgrade_copying_list(copy_list, pathes_sdk, corect_hard_copy_dir, print_list)
<> 162:e13f6fdb2ac4 197 break
<> 162:e13f6fdb2ac4 198
<> 162:e13f6fdb2ac4 199 print("\r\nforced copy of files by files: {0:#d}".format(len(print_list)))
<> 162:e13f6fdb2ac4 200
<> 162:e13f6fdb2ac4 201 if verbose:
<> 162:e13f6fdb2ac4 202 for item in print_list:
<> 162:e13f6fdb2ac4 203 str_verbose = "{0} --> {1}"
<> 162:e13f6fdb2ac4 204 print(str_verbose.format(item["id"], item["dest_path"]))
<> 162:e13f6fdb2ac4 205
<> 162:e13f6fdb2ac4 206 def copy_one_file(src, dest, verbose=False,dry_run=False):
<> 162:e13f6fdb2ac4 207 dirs_to_created = os.path.dirname(dest)
<> 162:e13f6fdb2ac4 208
<> 162:e13f6fdb2ac4 209 if not os.path.exists(dirs_to_created):
<> 162:e13f6fdb2ac4 210 if not dry_run:
<> 162:e13f6fdb2ac4 211 os.makedirs(dirs_to_created)
<> 162:e13f6fdb2ac4 212
<> 162:e13f6fdb2ac4 213 if verbose:
<> 162:e13f6fdb2ac4 214 print('makerdirs: {0}'.format(dirs_to_created))
<> 162:e13f6fdb2ac4 215
<> 162:e13f6fdb2ac4 216 if not dry_run:
<> 162:e13f6fdb2ac4 217 shutil.copyfile(src, dest)
<> 162:e13f6fdb2ac4 218
<> 162:e13f6fdb2ac4 219 if verbose:
<> 162:e13f6fdb2ac4 220 print('copy: {0} --> {1}'.format(src, dest))
<> 162:e13f6fdb2ac4 221
<> 162:e13f6fdb2ac4 222
<> 162:e13f6fdb2ac4 223
<> 162:e13f6fdb2ac4 224 if __name__ == '__main__':
<> 162:e13f6fdb2ac4 225 argument_parser = argparse.ArgumentParser()
<> 162:e13f6fdb2ac4 226 argument_parser.add_argument('-r', '--run', help='run', action='store_true')
<> 162:e13f6fdb2ac4 227 argument_parser.add_argument('-v', '--verbose', help='Verbose mode', action='store_true')
<> 162:e13f6fdb2ac4 228 #argument_parser.add_argument('-r', '--rename_only', help='rename only', action='store_true')
<> 162:e13f6fdb2ac4 229
<> 162:e13f6fdb2ac4 230 parser_args = vars(argument_parser.parse_args())
<> 162:e13f6fdb2ac4 231
<> 162:e13f6fdb2ac4 232 verbose = False
<> 162:e13f6fdb2ac4 233
<> 162:e13f6fdb2ac4 234 if parser_args['verbose'] or not parser_args['run']:
<> 162:e13f6fdb2ac4 235 verbose = True
<> 162:e13f6fdb2ac4 236
<> 162:e13f6fdb2ac4 237 with open('update_desc.json') as data_file:
<> 162:e13f6fdb2ac4 238 update_desc = json.load(data_file)
<> 162:e13f6fdb2ac4 239
<> 162:e13f6fdb2ac4 240
<> 162:e13f6fdb2ac4 241 #if not parser_args ['rename_only']:
<> 162:e13f6fdb2ac4 242 ignore_file_list = update_desc['ignore_file_list']
<> 162:e13f6fdb2ac4 243 ignore_dirs_list = update_desc['ignore_dirs_list']
<> 162:e13f6fdb2ac4 244 id_replacements = update_desc['id_replacements']
<> 162:e13f6fdb2ac4 245 force_copy_files_list = update_desc['force_copy_files_list']
<> 162:e13f6fdb2ac4 246 force_copy_dirs_list = update_desc['force_copy_dirs_list']
<> 162:e13f6fdb2ac4 247 sdk_dirs_in_mbed = update_desc['sdk_dirs_in_mbed']
<> 162:e13f6fdb2ac4 248 sdk_component_path = update_desc['sdk_component_path']
<> 162:e13f6fdb2ac4 249 port_relative_dir = update_desc['port_relative_dir_in_mbed']
<> 162:e13f6fdb2ac4 250
<> 162:e13f6fdb2ac4 251 list_sdk = get_file_pathes_couples(sdk_component_path,
<> 162:e13f6fdb2ac4 252 ignore_dirs_list,
<> 162:e13f6fdb2ac4 253 ignore_file_list,
<> 162:e13f6fdb2ac4 254 verbose)
<> 162:e13f6fdb2ac4 255 list_mbed = []
<> 162:e13f6fdb2ac4 256 for directory in sdk_dirs_in_mbed:
<> 162:e13f6fdb2ac4 257 list_mbed.extend(get_file_pathes_couples(directory))
<> 162:e13f6fdb2ac4 258
<> 162:e13f6fdb2ac4 259 list_mbed = apply_replacement_id(list_mbed, id_replacements)
<> 162:e13f6fdb2ac4 260
<> 162:e13f6fdb2ac4 261 mbed_port_path = ''
<> 162:e13f6fdb2ac4 262
<> 162:e13f6fdb2ac4 263 copy_list = get_copying_automatic_list(list_mbed, list_sdk, mbed_port_path, verbose)
<> 162:e13f6fdb2ac4 264
<> 162:e13f6fdb2ac4 265 upgrade_copying_list_by_dirs(copy_list, list_sdk, force_copy_dirs_list, port_relative_dir, verbose)
<> 162:e13f6fdb2ac4 266 upgrade_copying_list_by_files(copy_list, list_sdk, force_copy_files_list, port_relative_dir, verbose)
<> 162:e13f6fdb2ac4 267
<> 162:e13f6fdb2ac4 268 rename_dirs(sdk_dirs_in_mbed, '_old_sdk', not parser_args['run'])
<> 162:e13f6fdb2ac4 269
<> 162:e13f6fdb2ac4 270 for copy_item in copy_list:
<> 162:e13f6fdb2ac4 271 src = os.path.join('.',copy_item["src_path"])
<> 162:e13f6fdb2ac4 272 dest = os.path.join('.',copy_item["dest_path"])
<> 162:e13f6fdb2ac4 273
<> 162:e13f6fdb2ac4 274 copy_one_file(src, dest, verbose, not parser_args['run'])
<> 162:e13f6fdb2ac4 275
<> 162:e13f6fdb2ac4 276 with open('sdk_update_result.json', 'w') as fp:
<> 162:e13f6fdb2ac4 277 json.dump(copy_list, fp)