|
| 1 | +import os, json, stat, sys, shutil, errno, subprocess |
| 2 | +from os.path import dirname, abspath, basename, join |
| 3 | + |
| 4 | +ROOT = abspath(join(dirname(__file__), "../..")) |
| 5 | +CMSIS_REPO = "CMSIS_Repo" |
| 6 | +CMSIS_PATH = abspath(join(dirname(__file__), CMSIS_REPO)) |
| 7 | +RTOS_UPDATE_BRANCH = "rtos_update" |
| 8 | + |
| 9 | +def del_file(name): |
| 10 | + result = [] |
| 11 | + search_path = [join(ROOT, 'rtos'), join(ROOT, 'cmsis')] |
| 12 | + for path in search_path: |
| 13 | + for root, dirs, files in os.walk(path): |
| 14 | + if name in files: |
| 15 | + result.append(os.path.join(root, name)) |
| 16 | + for file in result: |
| 17 | + print os.path.relpath(file, ROOT) |
| 18 | + os.remove(file) |
| 19 | + |
| 20 | +def rmtree(top): |
| 21 | + for root, dirs, files in os.walk(top, topdown=False): |
| 22 | + for name in files: |
| 23 | + filename = os.path.join(root, name) |
| 24 | + os.chmod(filename, stat.S_IWUSR) |
| 25 | + os.remove(filename) |
| 26 | + for name in dirs: |
| 27 | + os.rmdir(os.path.join(root, name)) |
| 28 | + os.rmdir(top) |
| 29 | + |
| 30 | +def copy_file(file, path): |
| 31 | + try: |
| 32 | + shutil.copy(file, path) |
| 33 | + except IOError as e: |
| 34 | + if e.errno != errno.ENOENT: |
| 35 | + raise |
| 36 | + ## Create directories |
| 37 | + os.makedirs(os.path.dirname(path)) |
| 38 | + shutil.copy(file, path) |
| 39 | + print os.path.relpath(path, ROOT) |
| 40 | + |
| 41 | +def copy_folder(folder, path): |
| 42 | + files = os.listdir(folder) |
| 43 | + for file in files: |
| 44 | + abs_src_file = os.path.join(folder, file) |
| 45 | + if os.path.isfile(abs_src_file): |
| 46 | + abs_dst_file = os.path.join(path, file) |
| 47 | + copy_file(abs_src_file, abs_dst_file) |
| 48 | + |
| 49 | +def run_cmd(command, exit_on_failure=False): |
| 50 | + """ Passes a command to the system and returns a True/False result once the |
| 51 | + command has been executed, indicating success/failure. Commands are passed |
| 52 | + as a list of tokens. |
| 53 | + E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v'] |
| 54 | + """ |
| 55 | + return_code = subprocess.call(command, shell=True) |
| 56 | + |
| 57 | + if return_code: |
| 58 | + print("The command %s failed with return code: %s" |
| 59 | + %(' '.join(command), return_code)) |
| 60 | + if exit_on_failure: |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + return return_code |
| 64 | + |
| 65 | +def remove_repo(folder): |
| 66 | + os.chdir(abspath(dirname(__file__))) |
| 67 | + if os.path.exists(folder): |
| 68 | + rmtree(folder) |
| 69 | + |
| 70 | +def get_repo(repo, branch, folder): |
| 71 | + """ Get the Repository files from git, at depth level 1 |
| 72 | + repo - Git repository link |
| 73 | + branch - repository branch |
| 74 | + folder - folder at which repo will be cloned |
| 75 | + """ |
| 76 | + remove_repo(folder) |
| 77 | + clone_cmd = ['git', 'clone', repo, "-b", branch, "--depth", '1', folder] |
| 78 | + run_cmd(clone_cmd, exit_on_failure=True) |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + |
| 82 | + # Read configuration data |
| 83 | + with open(os.path.join(os.path.dirname(__file__), "cmsis_importer.json"), 'r') as config: |
| 84 | + json_data = json.load(config) |
| 85 | + config = json_data["config"] |
| 86 | + cmsis_repo = config['cmsis_repo'] |
| 87 | + cmsis_branch = config['cmsis_branch'] |
| 88 | + data_files = json_data["files"] |
| 89 | + data_folders = json_data["folders"] |
| 90 | + |
| 91 | + print "Fetching git repo" |
| 92 | + get_repo(cmsis_repo, cmsis_branch, CMSIS_REPO) |
| 93 | + |
| 94 | + ## Remove all files listed in .json from mbed-os repo to avoid duplications |
| 95 | + print "Cleaning up:" |
| 96 | + for file in data_files: |
| 97 | + cmsis_file = file['cmsis_file'] |
| 98 | + del_file(os.path.basename(cmsis_file)) |
| 99 | + |
| 100 | + for folder in data_folders: |
| 101 | + cmsis_folder = os.path.join(CMSIS_PATH, folder['cmsis_folder']) |
| 102 | + files = os.listdir(cmsis_folder) |
| 103 | + for file in files: |
| 104 | + del_file(os.path.basename(file)) |
| 105 | + |
| 106 | + ## Copy all the CMSIS files listed in json file to mbed-os |
| 107 | + print "Files Copied:" |
| 108 | + for file in data_files: |
| 109 | + cmsis_file = os.path.join(CMSIS_PATH, file['cmsis_file']) |
| 110 | + mbed_path = os.path.join(ROOT, file['mbed_file']) |
| 111 | + copy_file(cmsis_file, mbed_path) |
| 112 | + |
| 113 | + for folder in data_folders: |
| 114 | + cmsis_folder = os.path.join(CMSIS_PATH, folder['cmsis_folder']) |
| 115 | + mbed_path = os.path.join(ROOT, folder['mbed_folder']) |
| 116 | + copy_folder(cmsis_folder, mbed_path) |
| 117 | + |
| 118 | + #Remove CMSIS Repo |
| 119 | + remove_repo(CMSIS_REPO) |
| 120 | + |
| 121 | + ## Create new branch with all changes |
| 122 | + create_branch = ['git', 'checkout', '-b', RTOS_UPDATE_BRANCH] |
| 123 | + run_cmd(create_branch, exit_on_failure=True) |
| 124 | + |
| 125 | + add_files = ['git', 'add', '-A'] |
| 126 | + run_cmd(add_files, exit_on_failure=True) |
| 127 | + |
| 128 | + commit_branch = ['git', 'commit', '-m', "CMSIS/RTX: Update CMSIS/RTX"] |
| 129 | + run_cmd(commit_branch, exit_on_failure=True) |
| 130 | + |
| 131 | + ## Apply commits specific to mbed-os changes |
| 132 | + mbed_sha = json_data["Mbed_sha"] |
| 133 | + |
| 134 | + for sha in mbed_sha: |
| 135 | + cherry_pick_sha = ['git', 'cherry-pick', sha] |
| 136 | + run_cmd(cherry_pick_sha, exit_on_failure=True) |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
0 commit comments