MacroRat / MouseCode

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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