BA / Mbed OS BaBoRo1
Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 import os
borlanic 0:380207fcb5c1 2 import json
borlanic 0:380207fcb5c1 3 import sys
borlanic 0:380207fcb5c1 4 import subprocess
borlanic 0:380207fcb5c1 5 import logging
borlanic 0:380207fcb5c1 6 import argparse
borlanic 0:380207fcb5c1 7 from os.path import dirname, abspath, join
borlanic 0:380207fcb5c1 8
borlanic 0:380207fcb5c1 9 # Be sure that the tools directory is in the search path
borlanic 0:380207fcb5c1 10 ROOT = abspath(join(dirname(__file__), "../.."))
borlanic 0:380207fcb5c1 11 sys.path.insert(0, ROOT)
borlanic 0:380207fcb5c1 12
borlanic 0:380207fcb5c1 13 from tools.utils import run_cmd, delete_dir_files, mkdir, copy_file
borlanic 0:380207fcb5c1 14
borlanic 0:380207fcb5c1 15 def del_file(name):
borlanic 0:380207fcb5c1 16 """ Delete the file in RTOS/CMSIS/features directory of mbed-os
borlanic 0:380207fcb5c1 17 Args:
borlanic 0:380207fcb5c1 18 name - name of the file
borlanic 0:380207fcb5c1 19 """
borlanic 0:380207fcb5c1 20 result = []
borlanic 0:380207fcb5c1 21 search_path = [join(ROOT, 'rtos'), join(ROOT, 'cmsis'), join(ROOT, 'features')]
borlanic 0:380207fcb5c1 22 for path in search_path:
borlanic 0:380207fcb5c1 23 for root, dirs, files in os.walk(path):
borlanic 0:380207fcb5c1 24 if name in files:
borlanic 0:380207fcb5c1 25 result.append(os.path.join(root, name))
borlanic 0:380207fcb5c1 26 for file in result:
borlanic 0:380207fcb5c1 27 os.remove(file)
borlanic 0:380207fcb5c1 28 rel_log.debug("Deleted: %s", os.path.relpath(file, ROOT))
borlanic 0:380207fcb5c1 29
borlanic 0:380207fcb5c1 30 def copy_folder(src, dest):
borlanic 0:380207fcb5c1 31 """ Copy contents of folder in mbed-os listed path
borlanic 0:380207fcb5c1 32 Args:
borlanic 0:380207fcb5c1 33 src - src folder path
borlanic 0:380207fcb5c1 34 dest - destination folder path
borlanic 0:380207fcb5c1 35 """
borlanic 0:380207fcb5c1 36 files = os.listdir(src)
borlanic 0:380207fcb5c1 37 for file in files:
borlanic 0:380207fcb5c1 38 abs_src_file = os.path.join(src, file)
borlanic 0:380207fcb5c1 39 if os.path.isfile(abs_src_file):
borlanic 0:380207fcb5c1 40 abs_dst_file = os.path.join(dest, file)
borlanic 0:380207fcb5c1 41 mkdir(os.path.dirname(abs_dst_file))
borlanic 0:380207fcb5c1 42 copy_file(abs_src_file, abs_dst_file)
borlanic 0:380207fcb5c1 43
borlanic 0:380207fcb5c1 44 def run_cmd_with_output(command, exit_on_failure=False):
borlanic 0:380207fcb5c1 45 """ Passes a command to the system and returns a True/False result once the
borlanic 0:380207fcb5c1 46 command has been executed, indicating success/failure. If the command was
borlanic 0:380207fcb5c1 47 successful then the output from the command is returned to the caller.
borlanic 0:380207fcb5c1 48 Commands are passed as a list of tokens.
borlanic 0:380207fcb5c1 49 E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
borlanic 0:380207fcb5c1 50
borlanic 0:380207fcb5c1 51 Args:
borlanic 0:380207fcb5c1 52 command - system command as a list of tokens
borlanic 0:380207fcb5c1 53 exit_on_failure - If True exit the program on failure (default = False)
borlanic 0:380207fcb5c1 54
borlanic 0:380207fcb5c1 55 Returns:
borlanic 0:380207fcb5c1 56 result - True/False indicating the success/failure of the command
borlanic 0:380207fcb5c1 57 output - The output of the command if it was successful, else empty string
borlanic 0:380207fcb5c1 58 """
borlanic 0:380207fcb5c1 59 rel_log.debug('[Exec] %s', ' '.join(command))
borlanic 0:380207fcb5c1 60 returncode = 0
borlanic 0:380207fcb5c1 61 output = ""
borlanic 0:380207fcb5c1 62 try:
borlanic 0:380207fcb5c1 63 output = subprocess.check_output(command, shell=True)
borlanic 0:380207fcb5c1 64 except subprocess.CalledProcessError as e:
borlanic 0:380207fcb5c1 65 returncode = e.returncode
borlanic 0:380207fcb5c1 66
borlanic 0:380207fcb5c1 67 if exit_on_failure:
borlanic 0:380207fcb5c1 68 rel_log.error("The command %s failed with return code: %s",
borlanic 0:380207fcb5c1 69 (' '.join(command)), returncode)
borlanic 0:380207fcb5c1 70 sys.exit(1)
borlanic 0:380207fcb5c1 71 return returncode, output
borlanic 0:380207fcb5c1 72
borlanic 0:380207fcb5c1 73 def get_curr_sha(repo_path):
borlanic 0:380207fcb5c1 74 """ Gets the latest SHA for the specified repo
borlanic 0:380207fcb5c1 75 Args:
borlanic 0:380207fcb5c1 76 repo_path - path to the repository
borlanic 0:380207fcb5c1 77
borlanic 0:380207fcb5c1 78 Returns:
borlanic 0:380207fcb5c1 79 sha - last commit SHA
borlanic 0:380207fcb5c1 80 """
borlanic 0:380207fcb5c1 81 cwd = os.getcwd()
borlanic 0:380207fcb5c1 82 os.chdir(abspath(repo_path))
borlanic 0:380207fcb5c1 83
borlanic 0:380207fcb5c1 84 cmd = "git log --pretty=format:%h -n 1"
borlanic 0:380207fcb5c1 85 _, sha = run_cmd_with_output(cmd, exit_on_failure=True)
borlanic 0:380207fcb5c1 86
borlanic 0:380207fcb5c1 87 os.chdir(cwd)
borlanic 0:380207fcb5c1 88 return sha
borlanic 0:380207fcb5c1 89
borlanic 0:380207fcb5c1 90 def branch_exists(name):
borlanic 0:380207fcb5c1 91 """ Check if branch already exists in mbed-os local repository.
borlanic 0:380207fcb5c1 92 It will not verify if branch is present in remote repository.
borlanic 0:380207fcb5c1 93 Args:
borlanic 0:380207fcb5c1 94 name - branch name
borlanic 0:380207fcb5c1 95 Returns:
borlanic 0:380207fcb5c1 96 True - If branch is already present
borlanic 0:380207fcb5c1 97 """
borlanic 0:380207fcb5c1 98
borlanic 0:380207fcb5c1 99 cmd = "git branch"
borlanic 0:380207fcb5c1 100 _, output = run_cmd_with_output(cmd, exit_on_failure=False)
borlanic 0:380207fcb5c1 101 if name in output:
borlanic 0:380207fcb5c1 102 return True
borlanic 0:380207fcb5c1 103 return False
borlanic 0:380207fcb5c1 104
borlanic 0:380207fcb5c1 105 def branch_checkout(name):
borlanic 0:380207fcb5c1 106 """
borlanic 0:380207fcb5c1 107 Checkout the required branch
borlanic 0:380207fcb5c1 108 Args:
borlanic 0:380207fcb5c1 109 name - branch name
borlanic 0:380207fcb5c1 110 """
borlanic 0:380207fcb5c1 111 cmd = "git checkout " + name
borlanic 0:380207fcb5c1 112 run_cmd_with_output(cmd, exit_on_failure=False)
borlanic 0:380207fcb5c1 113
borlanic 0:380207fcb5c1 114 def get_last_cherry_pick_sha(branch):
borlanic 0:380207fcb5c1 115 """
borlanic 0:380207fcb5c1 116 SHA of last cherry pick commit is returned. SHA should be added to all
borlanic 0:380207fcb5c1 117 cherry-pick commits with -x option.
borlanic 0:380207fcb5c1 118
borlanic 0:380207fcb5c1 119 Args:
borlanic 0:380207fcb5c1 120 branch - Hash to be verified.
borlanic 0:380207fcb5c1 121 Returns - SHA if found, else None
borlanic 0:380207fcb5c1 122 """
borlanic 0:380207fcb5c1 123 cmd = "git checkout " + branch
borlanic 0:380207fcb5c1 124 run_cmd_with_output(cmd, exit_on_failure=False)
borlanic 0:380207fcb5c1 125
borlanic 0:380207fcb5c1 126 sha = None
borlanic 0:380207fcb5c1 127 get_commit = "git log -n 1"
borlanic 0:380207fcb5c1 128 _, output = run_cmd_with_output(get_commit, exit_on_failure=True)
borlanic 0:380207fcb5c1 129 lines = output.split('\n')
borlanic 0:380207fcb5c1 130 for line in lines:
borlanic 0:380207fcb5c1 131 if 'cherry picked from' in line:
borlanic 0:380207fcb5c1 132 sha = line.split(' ')[-1]
borlanic 0:380207fcb5c1 133 return sha[:-1]
borlanic 0:380207fcb5c1 134 return sha
borlanic 0:380207fcb5c1 135
borlanic 0:380207fcb5c1 136 if __name__ == "__main__":
borlanic 0:380207fcb5c1 137
borlanic 0:380207fcb5c1 138 parser = argparse.ArgumentParser(description=__doc__,
borlanic 0:380207fcb5c1 139 formatter_class=argparse.RawDescriptionHelpFormatter)
borlanic 0:380207fcb5c1 140 parser.add_argument('-l', '--log-level',
borlanic 0:380207fcb5c1 141 help="Level for providing logging output",
borlanic 0:380207fcb5c1 142 default='INFO')
borlanic 0:380207fcb5c1 143 parser.add_argument('-r', '--repo-path',
borlanic 0:380207fcb5c1 144 help="Git Repository to be imported",
borlanic 0:380207fcb5c1 145 default=None,
borlanic 0:380207fcb5c1 146 required=True)
borlanic 0:380207fcb5c1 147 parser.add_argument('-c', '--config-file',
borlanic 0:380207fcb5c1 148 help="Configuration file",
borlanic 0:380207fcb5c1 149 default=None,
borlanic 0:380207fcb5c1 150 required=True)
borlanic 0:380207fcb5c1 151 args = parser.parse_args()
borlanic 0:380207fcb5c1 152 level = getattr(logging, args.log_level.upper())
borlanic 0:380207fcb5c1 153
borlanic 0:380207fcb5c1 154 # Set logging level
borlanic 0:380207fcb5c1 155 logging.basicConfig(level=level)
borlanic 0:380207fcb5c1 156 rel_log = logging.getLogger("Importer")
borlanic 0:380207fcb5c1 157
borlanic 0:380207fcb5c1 158 if (args.repo_path is None) or (args.config_file is None):
borlanic 0:380207fcb5c1 159 rel_log.error("Repository path and config file required as input. Use \"--help\" for more info.")
borlanic 0:380207fcb5c1 160 exit(1)
borlanic 0:380207fcb5c1 161
borlanic 0:380207fcb5c1 162 json_file = os.path.abspath(args.config_file)
borlanic 0:380207fcb5c1 163 if not os.path.isfile(json_file):
borlanic 0:380207fcb5c1 164 rel_log.error("%s not found.", args.config_file)
borlanic 0:380207fcb5c1 165 exit(1)
borlanic 0:380207fcb5c1 166
borlanic 0:380207fcb5c1 167 repo = os.path.abspath(args.repo_path)
borlanic 0:380207fcb5c1 168 if not os.path.exists(repo):
borlanic 0:380207fcb5c1 169 rel_log.error("%s not found.", args.repo_path)
borlanic 0:380207fcb5c1 170 exit(1)
borlanic 0:380207fcb5c1 171
borlanic 0:380207fcb5c1 172 sha = get_curr_sha(repo)
borlanic 0:380207fcb5c1 173 if not sha:
borlanic 0:380207fcb5c1 174 rel_log.error("Could not obtain latest SHA")
borlanic 0:380207fcb5c1 175 exit(1)
borlanic 0:380207fcb5c1 176 rel_log.info("%s SHA = %s", os.path.basename(repo), sha)
borlanic 0:380207fcb5c1 177
borlanic 0:380207fcb5c1 178 branch = 'feature_' + os.path.basename(repo) + '_' + sha
borlanic 0:380207fcb5c1 179 commit_msg = "[" + os.path.basename(repo) + "]" + ": Updated to " + sha
borlanic 0:380207fcb5c1 180
borlanic 0:380207fcb5c1 181 # Read configuration data
borlanic 0:380207fcb5c1 182 with open(json_file, 'r') as config:
borlanic 0:380207fcb5c1 183 json_data = json.load(config)
borlanic 0:380207fcb5c1 184
borlanic 0:380207fcb5c1 185 '''
borlanic 0:380207fcb5c1 186 Check if branch exists already, in case branch is present
borlanic 0:380207fcb5c1 187 we will skip all file transfer and merge operations and will
borlanic 0:380207fcb5c1 188 jump to cherry-pick
borlanic 0:380207fcb5c1 189 '''
borlanic 0:380207fcb5c1 190 if branch_exists(branch):
borlanic 0:380207fcb5c1 191 rel_log.info("Branch present = %s", branch)
borlanic 0:380207fcb5c1 192 else:
borlanic 0:380207fcb5c1 193 data_files = json_data["files"]
borlanic 0:380207fcb5c1 194 data_folders = json_data["folders"]
borlanic 0:380207fcb5c1 195
borlanic 0:380207fcb5c1 196 ## Remove all files listed in .json from mbed-os repo to avoid duplications
borlanic 0:380207fcb5c1 197 for file in data_files:
borlanic 0:380207fcb5c1 198 src_file = file['src_file']
borlanic 0:380207fcb5c1 199 del_file(os.path.basename(src_file))
borlanic 0:380207fcb5c1 200
borlanic 0:380207fcb5c1 201 for folder in data_folders:
borlanic 0:380207fcb5c1 202 dest_folder = folder['dest_folder']
borlanic 0:380207fcb5c1 203 delete_dir_files(dest_folder)
borlanic 0:380207fcb5c1 204 rel_log.debug("Deleted = %s", folder)
borlanic 0:380207fcb5c1 205
borlanic 0:380207fcb5c1 206 rel_log.info("Removed files/folders listed in json file")
borlanic 0:380207fcb5c1 207
borlanic 0:380207fcb5c1 208 ## Copy all the CMSIS files listed in json file to mbed-os
borlanic 0:380207fcb5c1 209 for file in data_files:
borlanic 0:380207fcb5c1 210 repo_file = os.path.join(repo, file['src_file'])
borlanic 0:380207fcb5c1 211 mbed_path = os.path.join(ROOT, file['dest_file'])
borlanic 0:380207fcb5c1 212 mkdir(os.path.dirname(mbed_path))
borlanic 0:380207fcb5c1 213 copy_file(repo_file, mbed_path)
borlanic 0:380207fcb5c1 214 rel_log.debug("Copied = %s", mbed_path)
borlanic 0:380207fcb5c1 215
borlanic 0:380207fcb5c1 216 for folder in data_folders:
borlanic 0:380207fcb5c1 217 repo_folder = os.path.join(repo, folder['src_folder'])
borlanic 0:380207fcb5c1 218 mbed_path = os.path.join(ROOT, folder['dest_folder'])
borlanic 0:380207fcb5c1 219 copy_folder(repo_folder, mbed_path)
borlanic 0:380207fcb5c1 220 rel_log.debug("Copied = %s", mbed_path)
borlanic 0:380207fcb5c1 221
borlanic 0:380207fcb5c1 222 ## Create new branch with all changes
borlanic 0:380207fcb5c1 223 create_branch = "git checkout -b "+ branch
borlanic 0:380207fcb5c1 224 run_cmd_with_output(create_branch, exit_on_failure=True)
borlanic 0:380207fcb5c1 225 rel_log.info("Branch created = %s", branch)
borlanic 0:380207fcb5c1 226
borlanic 0:380207fcb5c1 227 add_files = "git add -A"
borlanic 0:380207fcb5c1 228 run_cmd_with_output(add_files, exit_on_failure=True)
borlanic 0:380207fcb5c1 229
borlanic 0:380207fcb5c1 230 commit_branch = "git commit -m \"" + commit_msg + "\""
borlanic 0:380207fcb5c1 231 run_cmd_with_output(commit_branch, exit_on_failure=True)
borlanic 0:380207fcb5c1 232 rel_log.info("Commit added = %s", mbed_path)
borlanic 0:380207fcb5c1 233
borlanic 0:380207fcb5c1 234 ## Checkout the feature branch
borlanic 0:380207fcb5c1 235 branch_checkout(branch)
borlanic 0:380207fcb5c1 236 commit_sha = json_data["commit_sha"]
borlanic 0:380207fcb5c1 237 last_sha = get_last_cherry_pick_sha(branch)
borlanic 0:380207fcb5c1 238 if not last_sha:
borlanic 0:380207fcb5c1 239 ## Apply commits specific to mbed-os changes
borlanic 0:380207fcb5c1 240 for sha in commit_sha:
borlanic 0:380207fcb5c1 241 cherry_pick_sha = "git cherry-pick -x " + sha
borlanic 0:380207fcb5c1 242 run_cmd_with_output(cherry_pick_sha, exit_on_failure=True)
borlanic 0:380207fcb5c1 243 rel_log.info("Commit added = %s", cherry_pick_sha)
borlanic 0:380207fcb5c1 244 ## Few commits are already applied, check the next in sequence
borlanic 0:380207fcb5c1 245 ## and skip to last applied
borlanic 0:380207fcb5c1 246 else:
borlanic 0:380207fcb5c1 247 found = False
borlanic 0:380207fcb5c1 248 for sha in commit_sha:
borlanic 0:380207fcb5c1 249 if sha == last_sha:
borlanic 0:380207fcb5c1 250 found = True
borlanic 0:380207fcb5c1 251 continue
borlanic 0:380207fcb5c1 252 if found is True:
borlanic 0:380207fcb5c1 253 cherry_pick_sha = "git cherry-pick -x " + sha
borlanic 0:380207fcb5c1 254 run_cmd_with_output(cherry_pick_sha, exit_on_failure=True)