Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 #!/usr/bin/env python
switches 0:5c4d7b2438d3 2
switches 0:5c4d7b2438d3 3 import os
switches 0:5c4d7b2438d3 4 from os.path import dirname, abspath, basename
switches 0:5c4d7b2438d3 5 import sys
switches 0:5c4d7b2438d3 6 import argparse
switches 0:5c4d7b2438d3 7 import json
switches 0:5c4d7b2438d3 8 import subprocess
switches 0:5c4d7b2438d3 9 import shutil
switches 0:5c4d7b2438d3 10 import stat
switches 0:5c4d7b2438d3 11
switches 0:5c4d7b2438d3 12 ROOT = abspath(dirname(dirname(dirname(dirname(__file__)))))
switches 0:5c4d7b2438d3 13 sys.path.insert(0, ROOT)
switches 0:5c4d7b2438d3 14
switches 0:5c4d7b2438d3 15 import examples_lib as lib
switches 0:5c4d7b2438d3 16 from examples_lib import SUPPORTED_TOOLCHAINS
switches 0:5c4d7b2438d3 17
switches 0:5c4d7b2438d3 18 def run_cmd(command, print_warning_on_fail=True):
switches 0:5c4d7b2438d3 19 """ Takes the command specified and runs it in a sub-process, obtaining the return code.
switches 0:5c4d7b2438d3 20
switches 0:5c4d7b2438d3 21 Args:
switches 0:5c4d7b2438d3 22 command - command to run, provided as a list of individual fields which are combined into a
switches 0:5c4d7b2438d3 23 single command before passing to the sub-process call.
switches 0:5c4d7b2438d3 24 return_code - result of the command.
switches 0:5c4d7b2438d3 25
switches 0:5c4d7b2438d3 26 """
switches 0:5c4d7b2438d3 27 print('[Exec] %s' % ' '.join(command))
switches 0:5c4d7b2438d3 28 return_code = subprocess.call(command)
switches 0:5c4d7b2438d3 29
switches 0:5c4d7b2438d3 30 if return_code:
switches 0:5c4d7b2438d3 31 print("The command '%s' failed with return code: %s" % (' '.join(command), return_code))
switches 0:5c4d7b2438d3 32 print("Ignoring and moving on to the next example")
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34 return return_code
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36
switches 0:5c4d7b2438d3 37 def rmtree_readonly(directory):
switches 0:5c4d7b2438d3 38 """ Deletes a readonly directory tree.
switches 0:5c4d7b2438d3 39
switches 0:5c4d7b2438d3 40 Args:
switches 0:5c4d7b2438d3 41 directory - tree to delete
switches 0:5c4d7b2438d3 42 """
switches 0:5c4d7b2438d3 43 def remove_readonly(func, path, _):
switches 0:5c4d7b2438d3 44 os.chmod(path, stat.S_IWRITE)
switches 0:5c4d7b2438d3 45 func(path)
switches 0:5c4d7b2438d3 46
switches 0:5c4d7b2438d3 47 shutil.rmtree(directory, onerror=remove_readonly)
switches 0:5c4d7b2438d3 48
switches 0:5c4d7b2438d3 49 def find_all_examples(path):
switches 0:5c4d7b2438d3 50 """ Searches the path specified for sub-example folders, ie those containing an
switches 0:5c4d7b2438d3 51 mbed-os.lib file. If found adds the path to the sub-example to a list which is
switches 0:5c4d7b2438d3 52 then returned.
switches 0:5c4d7b2438d3 53
switches 0:5c4d7b2438d3 54 Args:
switches 0:5c4d7b2438d3 55 path - path to search.
switches 0:5c4d7b2438d3 56 examples - (returned) list of paths to example directories.
switches 0:5c4d7b2438d3 57
switches 0:5c4d7b2438d3 58 """
switches 0:5c4d7b2438d3 59 examples = []
switches 0:5c4d7b2438d3 60 for root, dirs, files in os.walk(path):
switches 0:5c4d7b2438d3 61 if 'mbed-os.lib' in files:
switches 0:5c4d7b2438d3 62 examples += [root]
switches 0:5c4d7b2438d3 63
switches 0:5c4d7b2438d3 64 return examples
switches 0:5c4d7b2438d3 65
switches 0:5c4d7b2438d3 66 def upgrade_single_example(example, tag, directory):
switches 0:5c4d7b2438d3 67 """ Updates the mbed-os.lib file in the example specified to correspond to the
switches 0:5c4d7b2438d3 68 version specified by the GitHub tag supplied. Also deals with
switches 0:5c4d7b2438d3 69 multiple sub-examples in the GitHub repo, updating them in the same way.
switches 0:5c4d7b2438d3 70
switches 0:5c4d7b2438d3 71 Args:
switches 0:5c4d7b2438d3 72 example - json example object containing the GitHub repo to update.
switches 0:5c4d7b2438d3 73 tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
switches 0:5c4d7b2438d3 74 directory - directory path for the example.
switches 0:5c4d7b2438d3 75 returns - True if the upgrade was successful, False otherwise.
switches 0:5c4d7b2438d3 76
switches 0:5c4d7b2438d3 77 """
switches 0:5c4d7b2438d3 78 print("Upgrading single example at path '%s'" % directory)
switches 0:5c4d7b2438d3 79 cwd = os.getcwd()
switches 0:5c4d7b2438d3 80 os.chdir(directory)
switches 0:5c4d7b2438d3 81
switches 0:5c4d7b2438d3 82 return_code = None
switches 0:5c4d7b2438d3 83
switches 0:5c4d7b2438d3 84 # Change directories to the mbed-os library
switches 0:5c4d7b2438d3 85 if not os.path.exists('mbed-os'):
switches 0:5c4d7b2438d3 86 print("'mbed-os' directory not found in the root of '%s'" % directory)
switches 0:5c4d7b2438d3 87 print("Ignoring and moving on to the next example")
switches 0:5c4d7b2438d3 88 os.chdir(cwd)
switches 0:5c4d7b2438d3 89 return False
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 os.chdir('mbed-os')
switches 0:5c4d7b2438d3 92
switches 0:5c4d7b2438d3 93 # Setup and run the update command
switches 0:5c4d7b2438d3 94 update_cmd = ['mbed', 'update', tag]
switches 0:5c4d7b2438d3 95 return_code = run_cmd(update_cmd)
switches 0:5c4d7b2438d3 96
switches 0:5c4d7b2438d3 97 if return_code:
switches 0:5c4d7b2438d3 98 os.chdir(cwd)
switches 0:5c4d7b2438d3 99 return False
switches 0:5c4d7b2438d3 100
switches 0:5c4d7b2438d3 101 os.chdir('../')
switches 0:5c4d7b2438d3 102
switches 0:5c4d7b2438d3 103 # Setup and run the add command
switches 0:5c4d7b2438d3 104 add_cmd = ['git', 'add', 'mbed-os.lib']
switches 0:5c4d7b2438d3 105 return_code = run_cmd(add_cmd)
switches 0:5c4d7b2438d3 106
switches 0:5c4d7b2438d3 107 os.chdir(cwd)
switches 0:5c4d7b2438d3 108 return not return_code
switches 0:5c4d7b2438d3 109
switches 0:5c4d7b2438d3 110 def upgrade_example(example, tag):
switches 0:5c4d7b2438d3 111 """ Clones the example specified from GitHub and updates the associated mbed-os.lib file
switches 0:5c4d7b2438d3 112 to correspond to the version specified by the GitHub tag supplied. Also deals with
switches 0:5c4d7b2438d3 113 multiple sub-examples in the GitHub repo, updating them in the same way.
switches 0:5c4d7b2438d3 114
switches 0:5c4d7b2438d3 115 Args:
switches 0:5c4d7b2438d3 116 example - json example object containing the GitHub repo to update.
switches 0:5c4d7b2438d3 117 tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
switches 0:5c4d7b2438d3 118
switches 0:5c4d7b2438d3 119 """
switches 0:5c4d7b2438d3 120 print("Updating example '%s'" % example['name'])
switches 0:5c4d7b2438d3 121 cwd = os.getcwd()
switches 0:5c4d7b2438d3 122
switches 0:5c4d7b2438d3 123 # Setup and run the import command
switches 0:5c4d7b2438d3 124 clone_cmd = ['git', 'clone', example['github']]
switches 0:5c4d7b2438d3 125 return_code = run_cmd(clone_cmd)
switches 0:5c4d7b2438d3 126
switches 0:5c4d7b2438d3 127 if return_code:
switches 0:5c4d7b2438d3 128 return False
switches 0:5c4d7b2438d3 129
switches 0:5c4d7b2438d3 130 # Find all examples
switches 0:5c4d7b2438d3 131 example_directories = find_all_examples(example['name'])
switches 0:5c4d7b2438d3 132
switches 0:5c4d7b2438d3 133 os.chdir(example['name'])
switches 0:5c4d7b2438d3 134
switches 0:5c4d7b2438d3 135 # Setup and run the update command
switches 0:5c4d7b2438d3 136 import_cmd = ['mbed', 'update']
switches 0:5c4d7b2438d3 137 return_code = run_cmd(import_cmd)
switches 0:5c4d7b2438d3 138 if return_code:
switches 0:5c4d7b2438d3 139 os.chdir(cwd)
switches 0:5c4d7b2438d3 140 return False
switches 0:5c4d7b2438d3 141
switches 0:5c4d7b2438d3 142 for example_directory in example_directories:
switches 0:5c4d7b2438d3 143 if not upgrade_single_example(example, tag, os.path.relpath(example_directory, example['name'])):
switches 0:5c4d7b2438d3 144 os.chdir(cwd)
switches 0:5c4d7b2438d3 145 return False
switches 0:5c4d7b2438d3 146
switches 0:5c4d7b2438d3 147 # Setup the default commit message
switches 0:5c4d7b2438d3 148 commit_message = 'Updating mbed-os to {{' + tag +'}}'
switches 0:5c4d7b2438d3 149
switches 0:5c4d7b2438d3 150 # Setup and run the commit command
switches 0:5c4d7b2438d3 151 commit_cmd = ['git', 'commit', '-m', commit_message]
switches 0:5c4d7b2438d3 152 return_code = run_cmd(commit_cmd)
switches 0:5c4d7b2438d3 153 if return_code:
switches 0:5c4d7b2438d3 154 if return_code == 1:
switches 0:5c4d7b2438d3 155 print("[WARNING] 'git commit' exited with a return code of 1. " + \
switches 0:5c4d7b2438d3 156 "This usually inidicates that no update was made. Still " + \
switches 0:5c4d7b2438d3 157 "attempting to create a tag.")
switches 0:5c4d7b2438d3 158 else:
switches 0:5c4d7b2438d3 159 os.chdir(cwd)
switches 0:5c4d7b2438d3 160 return False
switches 0:5c4d7b2438d3 161
switches 0:5c4d7b2438d3 162 # Setup and run the tag command
switches 0:5c4d7b2438d3 163 tag_cmd = ['git', 'tag', '-a', tag, '-m', tag]
switches 0:5c4d7b2438d3 164 return_code = run_cmd(tag_cmd)
switches 0:5c4d7b2438d3 165 if return_code:
switches 0:5c4d7b2438d3 166 os.chdir(cwd)
switches 0:5c4d7b2438d3 167 return False
switches 0:5c4d7b2438d3 168
switches 0:5c4d7b2438d3 169 # Setup and run the push command
switches 0:5c4d7b2438d3 170 push_cmd = ['git', 'push', 'origin', 'master']
switches 0:5c4d7b2438d3 171 return_code = run_cmd(push_cmd)
switches 0:5c4d7b2438d3 172
switches 0:5c4d7b2438d3 173 if return_code:
switches 0:5c4d7b2438d3 174 os.chdir(cwd)
switches 0:5c4d7b2438d3 175 return False
switches 0:5c4d7b2438d3 176
switches 0:5c4d7b2438d3 177 push_cmd = ['git', 'push', 'origin', tag]
switches 0:5c4d7b2438d3 178 return_code = run_cmd(push_cmd)
switches 0:5c4d7b2438d3 179
switches 0:5c4d7b2438d3 180 os.chdir(cwd)
switches 0:5c4d7b2438d3 181 return not return_code
switches 0:5c4d7b2438d3 182
switches 0:5c4d7b2438d3 183 def create_work_directory(path):
switches 0:5c4d7b2438d3 184 """ Create a new directory specified in 'path', overwrite if the directory already
switches 0:5c4d7b2438d3 185 exists.
switches 0:5c4d7b2438d3 186
switches 0:5c4d7b2438d3 187 Args:
switches 0:5c4d7b2438d3 188 path - directory path to be created.
switches 0:5c4d7b2438d3 189
switches 0:5c4d7b2438d3 190 """
switches 0:5c4d7b2438d3 191 if os.path.exists(path):
switches 0:5c4d7b2438d3 192 print("'%s' directory already exists. Deleting..." % path)
switches 0:5c4d7b2438d3 193 rmtree_readonly(path)
switches 0:5c4d7b2438d3 194
switches 0:5c4d7b2438d3 195 os.makedirs(path)
switches 0:5c4d7b2438d3 196
switches 0:5c4d7b2438d3 197 def test_compile(config, tag):
switches 0:5c4d7b2438d3 198 """ For each example repo identified in the config json object, clone, update mbed-os to
switches 0:5c4d7b2438d3 199 the specified tag and then compile for all supported toolchains.
switches 0:5c4d7b2438d3 200
switches 0:5c4d7b2438d3 201 Args:
switches 0:5c4d7b2438d3 202 config - the json object imported from the file.
switches 0:5c4d7b2438d3 203 tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
switches 0:5c4d7b2438d3 204 results - summary of compilation results.
switches 0:5c4d7b2438d3 205
switches 0:5c4d7b2438d3 206 """
switches 0:5c4d7b2438d3 207 # Create work directories
switches 0:5c4d7b2438d3 208 create_work_directory('test_compile')
switches 0:5c4d7b2438d3 209
switches 0:5c4d7b2438d3 210 # Loop through the examples
switches 0:5c4d7b2438d3 211 results = {}
switches 0:5c4d7b2438d3 212 os.chdir('test_compile')
switches 0:5c4d7b2438d3 213
switches 0:5c4d7b2438d3 214 lib.source_repos(config)
switches 0:5c4d7b2438d3 215 lib.update_mbedos_version(config, tag)
switches 0:5c4d7b2438d3 216 results = lib.compile_repos(config, SUPPORTED_TOOLCHAINS)
switches 0:5c4d7b2438d3 217 os.chdir("..")
switches 0:5c4d7b2438d3 218
switches 0:5c4d7b2438d3 219 return results
switches 0:5c4d7b2438d3 220
switches 0:5c4d7b2438d3 221
switches 0:5c4d7b2438d3 222 def main(arguments):
switches 0:5c4d7b2438d3 223
switches 0:5c4d7b2438d3 224 parser = argparse.ArgumentParser(description=__doc__,
switches 0:5c4d7b2438d3 225 formatter_class=argparse.RawDescriptionHelpFormatter)
switches 0:5c4d7b2438d3 226 parser.add_argument('tag', help="mbed-os tag to which all examples will be updated")
switches 0:5c4d7b2438d3 227 parser.add_argument('-c', '--config_file', help="Path to the configuration file (default is 'examples.json')", default='examples.json')
switches 0:5c4d7b2438d3 228
switches 0:5c4d7b2438d3 229 args = parser.parse_args(arguments)
switches 0:5c4d7b2438d3 230
switches 0:5c4d7b2438d3 231 cfg = os.path.join(os.path.dirname(__file__), args.config_file)
switches 0:5c4d7b2438d3 232
switches 0:5c4d7b2438d3 233 # Load the config file
switches 0:5c4d7b2438d3 234 config = json.load(open(os.path.join(os.path.dirname(__file__),
switches 0:5c4d7b2438d3 235 args.config_file)))
switches 0:5c4d7b2438d3 236
switches 0:5c4d7b2438d3 237 if not config:
switches 0:5c4d7b2438d3 238 print("Failed to load config file '%s'" % args.config_file)
switches 0:5c4d7b2438d3 239 sys.exit(1)
switches 0:5c4d7b2438d3 240
switches 0:5c4d7b2438d3 241 # Create work directories
switches 0:5c4d7b2438d3 242 create_work_directory('examples')
switches 0:5c4d7b2438d3 243
switches 0:5c4d7b2438d3 244 # Loop through the examples
switches 0:5c4d7b2438d3 245 failures = []
switches 0:5c4d7b2438d3 246 successes = []
switches 0:5c4d7b2438d3 247 not_compiled = []
switches 0:5c4d7b2438d3 248 results = {}
switches 0:5c4d7b2438d3 249 os.chdir('examples')
switches 0:5c4d7b2438d3 250
switches 0:5c4d7b2438d3 251 results = test_compile(config, args.tag)
switches 0:5c4d7b2438d3 252 lib.print_compilation_summary(results)
switches 0:5c4d7b2438d3 253
switches 0:5c4d7b2438d3 254 for example in config['examples']:
switches 0:5c4d7b2438d3 255 # Determine if this example should be updated
switches 0:5c4d7b2438d3 256
switches 0:5c4d7b2438d3 257 # Attempt to update if:
switches 0:5c4d7b2438d3 258 # group of examples passed compilation and
switches 0:5c4d7b2438d3 259 # auto update is set to True
switches 0:5c4d7b2438d3 260 # Note: results fields are [compiled flag, pass flag, successes list, failures list]
switches 0:5c4d7b2438d3 261 if not results[example['name']][0]:
switches 0:5c4d7b2438d3 262 # Example was not compiled
switches 0:5c4d7b2438d3 263 not_compiled += [example['name']]
switches 0:5c4d7b2438d3 264 else:
switches 0:5c4d7b2438d3 265 if results[example['name']][1] and example['auto-update']:
switches 0:5c4d7b2438d3 266 if upgrade_example(example, args.tag):
switches 0:5c4d7b2438d3 267 successes += [example['name']]
switches 0:5c4d7b2438d3 268 else:
switches 0:5c4d7b2438d3 269 failures += [example['name']]
switches 0:5c4d7b2438d3 270 else:
switches 0:5c4d7b2438d3 271 failures += [example['name']]
switches 0:5c4d7b2438d3 272
switches 0:5c4d7b2438d3 273 os.chdir('../')
switches 0:5c4d7b2438d3 274
switches 0:5c4d7b2438d3 275 # Finish the script and report the results
switches 0:5c4d7b2438d3 276 print(os.linesep + os.linesep +'Finished updating examples!' + os.linesep)
switches 0:5c4d7b2438d3 277
switches 0:5c4d7b2438d3 278 if successes:
switches 0:5c4d7b2438d3 279 print('The following examples updated successfully:')
switches 0:5c4d7b2438d3 280 for success in successes:
switches 0:5c4d7b2438d3 281 print(' - %s' % success)
switches 0:5c4d7b2438d3 282
switches 0:5c4d7b2438d3 283 if failures:
switches 0:5c4d7b2438d3 284 print('\nThe following examples were not updated:')
switches 0:5c4d7b2438d3 285 for fail in failures:
switches 0:5c4d7b2438d3 286 print(' - %s' % fail)
switches 0:5c4d7b2438d3 287
switches 0:5c4d7b2438d3 288 if not_compiled:
switches 0:5c4d7b2438d3 289 print('The following examples were skipped:')
switches 0:5c4d7b2438d3 290 for example in not_compiled:
switches 0:5c4d7b2438d3 291 print(' - %s' % example)
switches 0:5c4d7b2438d3 292
switches 0:5c4d7b2438d3 293
switches 0:5c4d7b2438d3 294 if __name__ == '__main__':
switches 0:5c4d7b2438d3 295 sys.exit(main(sys.argv[1:]))