Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Wed Oct 10 00:33:53 2018 +0000
Revision:
0:8fdf9a60065b
how to make mbed librry

Who changed what in which revision?

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