mbed-os for GR-LYCHEE
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
tools/test/examples/update.py@0:f782d9c66c49, 2018-02-02 (annotated)
- Committer:
- dkato
- Date:
- Fri Feb 02 05:42:23 2018 +0000
- Revision:
- 0:f782d9c66c49
mbed-os for GR-LYCHEE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:f782d9c66c49 | 1 | #!/usr/bin/env python |
dkato | 0:f782d9c66c49 | 2 | |
dkato | 0:f782d9c66c49 | 3 | import os |
dkato | 0:f782d9c66c49 | 4 | from os.path import dirname, abspath, basename |
dkato | 0:f782d9c66c49 | 5 | import sys |
dkato | 0:f782d9c66c49 | 6 | import argparse |
dkato | 0:f782d9c66c49 | 7 | import json |
dkato | 0:f782d9c66c49 | 8 | import subprocess |
dkato | 0:f782d9c66c49 | 9 | import shutil |
dkato | 0:f782d9c66c49 | 10 | import stat |
dkato | 0:f782d9c66c49 | 11 | import re |
dkato | 0:f782d9c66c49 | 12 | from github import Github, GithubException |
dkato | 0:f782d9c66c49 | 13 | |
dkato | 0:f782d9c66c49 | 14 | ROOT = abspath(dirname(dirname(dirname(dirname(__file__))))) |
dkato | 0:f782d9c66c49 | 15 | sys.path.insert(0, ROOT) |
dkato | 0:f782d9c66c49 | 16 | |
dkato | 0:f782d9c66c49 | 17 | import examples_lib as lib |
dkato | 0:f782d9c66c49 | 18 | from examples_lib import SUPPORTED_TOOLCHAINS |
dkato | 0:f782d9c66c49 | 19 | |
dkato | 0:f782d9c66c49 | 20 | def run_cmd(command, print_warning_on_fail=True): |
dkato | 0:f782d9c66c49 | 21 | """ Takes the command specified and runs it in a sub-process, obtaining the return code. |
dkato | 0:f782d9c66c49 | 22 | |
dkato | 0:f782d9c66c49 | 23 | Args: |
dkato | 0:f782d9c66c49 | 24 | command - command to run, provided as a list of individual fields which are combined into a |
dkato | 0:f782d9c66c49 | 25 | single command before passing to the sub-process call. |
dkato | 0:f782d9c66c49 | 26 | return_code - result of the command. |
dkato | 0:f782d9c66c49 | 27 | |
dkato | 0:f782d9c66c49 | 28 | """ |
dkato | 0:f782d9c66c49 | 29 | print('[Exec] %s' % ' '.join(command)) |
dkato | 0:f782d9c66c49 | 30 | return_code = subprocess.call(command) |
dkato | 0:f782d9c66c49 | 31 | |
dkato | 0:f782d9c66c49 | 32 | if return_code: |
dkato | 0:f782d9c66c49 | 33 | print("The command '%s' failed with return code: %s" % (' '.join(command), return_code)) |
dkato | 0:f782d9c66c49 | 34 | print("Ignoring and moving on to the next example") |
dkato | 0:f782d9c66c49 | 35 | |
dkato | 0:f782d9c66c49 | 36 | return return_code |
dkato | 0:f782d9c66c49 | 37 | |
dkato | 0:f782d9c66c49 | 38 | def run_cmd_with_output(command, print_warning_on_fail=True): |
dkato | 0:f782d9c66c49 | 39 | """ Takes the command specified and runs it in a sub-process, obtaining the return code |
dkato | 0:f782d9c66c49 | 40 | and the returned output. |
dkato | 0:f782d9c66c49 | 41 | |
dkato | 0:f782d9c66c49 | 42 | Args: |
dkato | 0:f782d9c66c49 | 43 | command - command to run, provided as a list of individual fields which are combined into a |
dkato | 0:f782d9c66c49 | 44 | single command before passing to the sub-process call. |
dkato | 0:f782d9c66c49 | 45 | return_code - result of the command. |
dkato | 0:f782d9c66c49 | 46 | output - the output of the command |
dkato | 0:f782d9c66c49 | 47 | |
dkato | 0:f782d9c66c49 | 48 | """ |
dkato | 0:f782d9c66c49 | 49 | print('[Exec] %s' % ' '.join(command)) |
dkato | 0:f782d9c66c49 | 50 | returncode = 0 |
dkato | 0:f782d9c66c49 | 51 | output = None |
dkato | 0:f782d9c66c49 | 52 | try: |
dkato | 0:f782d9c66c49 | 53 | output = subprocess.check_output(command) |
dkato | 0:f782d9c66c49 | 54 | except subprocess.CalledProcessError as e: |
dkato | 0:f782d9c66c49 | 55 | print("The command '%s' failed with return code: %s" % (' '.join(command), e.returncode)) |
dkato | 0:f782d9c66c49 | 56 | returncode = e.returncode |
dkato | 0:f782d9c66c49 | 57 | return returncode, output |
dkato | 0:f782d9c66c49 | 58 | |
dkato | 0:f782d9c66c49 | 59 | def rmtree_readonly(directory): |
dkato | 0:f782d9c66c49 | 60 | """ Deletes a readonly directory tree. |
dkato | 0:f782d9c66c49 | 61 | |
dkato | 0:f782d9c66c49 | 62 | Args: |
dkato | 0:f782d9c66c49 | 63 | directory - tree to delete |
dkato | 0:f782d9c66c49 | 64 | """ |
dkato | 0:f782d9c66c49 | 65 | def remove_readonly(func, path, _): |
dkato | 0:f782d9c66c49 | 66 | os.chmod(path, stat.S_IWRITE) |
dkato | 0:f782d9c66c49 | 67 | func(path) |
dkato | 0:f782d9c66c49 | 68 | |
dkato | 0:f782d9c66c49 | 69 | shutil.rmtree(directory, onerror=remove_readonly) |
dkato | 0:f782d9c66c49 | 70 | |
dkato | 0:f782d9c66c49 | 71 | def find_all_examples(path): |
dkato | 0:f782d9c66c49 | 72 | """ Searches the path specified for sub-example folders, ie those containing an |
dkato | 0:f782d9c66c49 | 73 | mbed-os.lib file. If found adds the path to the sub-example to a list which is |
dkato | 0:f782d9c66c49 | 74 | then returned. |
dkato | 0:f782d9c66c49 | 75 | |
dkato | 0:f782d9c66c49 | 76 | Args: |
dkato | 0:f782d9c66c49 | 77 | path - path to search. |
dkato | 0:f782d9c66c49 | 78 | examples - (returned) list of paths to example directories. |
dkato | 0:f782d9c66c49 | 79 | |
dkato | 0:f782d9c66c49 | 80 | """ |
dkato | 0:f782d9c66c49 | 81 | examples = [] |
dkato | 0:f782d9c66c49 | 82 | for root, dirs, files in os.walk(path): |
dkato | 0:f782d9c66c49 | 83 | if 'mbed-os.lib' in files: |
dkato | 0:f782d9c66c49 | 84 | examples += [root] |
dkato | 0:f782d9c66c49 | 85 | |
dkato | 0:f782d9c66c49 | 86 | return examples |
dkato | 0:f782d9c66c49 | 87 | |
dkato | 0:f782d9c66c49 | 88 | def upgrade_single_example(example, tag, directory, ref): |
dkato | 0:f782d9c66c49 | 89 | """ Updates the mbed-os.lib file in the example specified to correspond to the |
dkato | 0:f782d9c66c49 | 90 | version specified by the GitHub tag supplied. Also deals with |
dkato | 0:f782d9c66c49 | 91 | multiple sub-examples in the GitHub repo, updating them in the same way. |
dkato | 0:f782d9c66c49 | 92 | |
dkato | 0:f782d9c66c49 | 93 | Args: |
dkato | 0:f782d9c66c49 | 94 | example - json example object containing the GitHub repo to update. |
dkato | 0:f782d9c66c49 | 95 | tag - GitHub tag corresponding to a version of mbed-os to upgrade to. |
dkato | 0:f782d9c66c49 | 96 | directory - directory path for the example. |
dkato | 0:f782d9c66c49 | 97 | ref - SHA corresponding to the supplied tag |
dkato | 0:f782d9c66c49 | 98 | returns - True if the upgrade was successful, False otherwise. |
dkato | 0:f782d9c66c49 | 99 | |
dkato | 0:f782d9c66c49 | 100 | """ |
dkato | 0:f782d9c66c49 | 101 | cwd = os.getcwd() |
dkato | 0:f782d9c66c49 | 102 | os.chdir(directory) |
dkato | 0:f782d9c66c49 | 103 | |
dkato | 0:f782d9c66c49 | 104 | return_code = False |
dkato | 0:f782d9c66c49 | 105 | |
dkato | 0:f782d9c66c49 | 106 | if os.path.isfile("mbed-os.lib"): |
dkato | 0:f782d9c66c49 | 107 | # Rename command will fail on some OS's if the target file already exist, |
dkato | 0:f782d9c66c49 | 108 | # so ensure if it does, it is deleted first. |
dkato | 0:f782d9c66c49 | 109 | if os.path.isfile("mbed-os.lib_bak"): |
dkato | 0:f782d9c66c49 | 110 | os.remove("mbed-os.lib_bak") |
dkato | 0:f782d9c66c49 | 111 | |
dkato | 0:f782d9c66c49 | 112 | os.rename("mbed-os.lib", "mbed-os.lib_bak") |
dkato | 0:f782d9c66c49 | 113 | else: |
dkato | 0:f782d9c66c49 | 114 | print("!! Error trying to backup mbed-os.lib prior to updating.") |
dkato | 0:f782d9c66c49 | 115 | return False |
dkato | 0:f782d9c66c49 | 116 | |
dkato | 0:f782d9c66c49 | 117 | # mbed-os.lib file contains one line with the following format |
dkato | 0:f782d9c66c49 | 118 | # e.g. https://github.com/ARMmbed/mbed-os/#0789928ee7f2db08a419fa4a032fffd9bd477aa7 |
dkato | 0:f782d9c66c49 | 119 | lib_re = re.compile('https://github.com/ARMmbed/mbed-os/#[A-Za-z0-9]+') |
dkato | 0:f782d9c66c49 | 120 | updated = False |
dkato | 0:f782d9c66c49 | 121 | |
dkato | 0:f782d9c66c49 | 122 | # Scan through mbed-os.lib line by line |
dkato | 0:f782d9c66c49 | 123 | with open('mbed-os.lib_bak', 'r') as ip, open('mbed-os.lib', 'w') as op: |
dkato | 0:f782d9c66c49 | 124 | for line in ip: |
dkato | 0:f782d9c66c49 | 125 | |
dkato | 0:f782d9c66c49 | 126 | opline = line |
dkato | 0:f782d9c66c49 | 127 | |
dkato | 0:f782d9c66c49 | 128 | regexp = lib_re.match(line) |
dkato | 0:f782d9c66c49 | 129 | if regexp: |
dkato | 0:f782d9c66c49 | 130 | opline = 'https://github.com/ARMmbed/mbed-os/#' + ref |
dkato | 0:f782d9c66c49 | 131 | updated = True |
dkato | 0:f782d9c66c49 | 132 | |
dkato | 0:f782d9c66c49 | 133 | op.write(opline) |
dkato | 0:f782d9c66c49 | 134 | |
dkato | 0:f782d9c66c49 | 135 | if updated: |
dkato | 0:f782d9c66c49 | 136 | # Setup and run the git add command |
dkato | 0:f782d9c66c49 | 137 | cmd = ['git', 'add', 'mbed-os.lib'] |
dkato | 0:f782d9c66c49 | 138 | return_code = run_cmd(cmd) |
dkato | 0:f782d9c66c49 | 139 | |
dkato | 0:f782d9c66c49 | 140 | os.chdir(cwd) |
dkato | 0:f782d9c66c49 | 141 | return not return_code |
dkato | 0:f782d9c66c49 | 142 | |
dkato | 0:f782d9c66c49 | 143 | def prepare_fork(arm_example): |
dkato | 0:f782d9c66c49 | 144 | """ Synchronises a cloned fork to ensure it is up to date with the original. |
dkato | 0:f782d9c66c49 | 145 | |
dkato | 0:f782d9c66c49 | 146 | Args: |
dkato | 0:f782d9c66c49 | 147 | arm_example - Full GitHub repo path for original example |
dkato | 0:f782d9c66c49 | 148 | ret - True if the fork was synchronised successfully, False otherwise |
dkato | 0:f782d9c66c49 | 149 | |
dkato | 0:f782d9c66c49 | 150 | """ |
dkato | 0:f782d9c66c49 | 151 | |
dkato | 0:f782d9c66c49 | 152 | print "In " + os.getcwd() |
dkato | 0:f782d9c66c49 | 153 | |
dkato | 0:f782d9c66c49 | 154 | for cmd in [['git', 'remote', 'add', 'armmbed', arm_example], |
dkato | 0:f782d9c66c49 | 155 | ['git', 'fetch', 'armmbed'], |
dkato | 0:f782d9c66c49 | 156 | ['git', 'reset', '--hard', 'armmbed/master'], |
dkato | 0:f782d9c66c49 | 157 | ['git', 'push', '-f', 'origin']]: |
dkato | 0:f782d9c66c49 | 158 | if run_cmd(cmd): |
dkato | 0:f782d9c66c49 | 159 | print("preparation of the fork failed!") |
dkato | 0:f782d9c66c49 | 160 | return False |
dkato | 0:f782d9c66c49 | 161 | return True |
dkato | 0:f782d9c66c49 | 162 | |
dkato | 0:f782d9c66c49 | 163 | |
dkato | 0:f782d9c66c49 | 164 | def upgrade_example(github, example, tag, user, ref): |
dkato | 0:f782d9c66c49 | 165 | """ Clone a fork of the example specified. |
dkato | 0:f782d9c66c49 | 166 | Ensures the fork is up to date with the original and then and updates the associated |
dkato | 0:f782d9c66c49 | 167 | mbed-os.lib file on that fork to correspond to the version specified by the GitHub tag supplied. |
dkato | 0:f782d9c66c49 | 168 | Also deals with multiple sub-examples in the GitHub repo, updating them in the same way. |
dkato | 0:f782d9c66c49 | 169 | The updates are pushed to the forked repo. |
dkato | 0:f782d9c66c49 | 170 | Finally a PR is raised against the original example repo for the changes. |
dkato | 0:f782d9c66c49 | 171 | |
dkato | 0:f782d9c66c49 | 172 | Args: |
dkato | 0:f782d9c66c49 | 173 | github - GitHub instance to allow internal git commands to be run |
dkato | 0:f782d9c66c49 | 174 | example - json example object containing the GitHub repo to update. |
dkato | 0:f782d9c66c49 | 175 | tag - GitHub tag corresponding to a version of mbed-os to upgrade to. |
dkato | 0:f782d9c66c49 | 176 | user - GitHub user name |
dkato | 0:f782d9c66c49 | 177 | ref - SHA corresponding to the tag |
dkato | 0:f782d9c66c49 | 178 | |
dkato | 0:f782d9c66c49 | 179 | """ |
dkato | 0:f782d9c66c49 | 180 | ret = False |
dkato | 0:f782d9c66c49 | 181 | print("\nUpdating example '%s'" % example['name']) |
dkato | 0:f782d9c66c49 | 182 | cwd = os.getcwd() |
dkato | 0:f782d9c66c49 | 183 | |
dkato | 0:f782d9c66c49 | 184 | full_repo_name = 'ARMmbed/'+ example['name'] |
dkato | 0:f782d9c66c49 | 185 | fork = "https://github.com/" + user + '/' + example['name'] |
dkato | 0:f782d9c66c49 | 186 | |
dkato | 0:f782d9c66c49 | 187 | # Check access to mbed-os repo |
dkato | 0:f782d9c66c49 | 188 | try: |
dkato | 0:f782d9c66c49 | 189 | repo = github.get_repo(full_repo_name, False) |
dkato | 0:f782d9c66c49 | 190 | |
dkato | 0:f782d9c66c49 | 191 | except: |
dkato | 0:f782d9c66c49 | 192 | print("\t\t!! Repo does not exist - skipping\n") |
dkato | 0:f782d9c66c49 | 193 | return False |
dkato | 0:f782d9c66c49 | 194 | |
dkato | 0:f782d9c66c49 | 195 | |
dkato | 0:f782d9c66c49 | 196 | # Clone the forked example repo |
dkato | 0:f782d9c66c49 | 197 | clone_cmd = ['git', 'clone', fork] |
dkato | 0:f782d9c66c49 | 198 | return_code = run_cmd(clone_cmd) |
dkato | 0:f782d9c66c49 | 199 | |
dkato | 0:f782d9c66c49 | 200 | if not return_code: |
dkato | 0:f782d9c66c49 | 201 | |
dkato | 0:f782d9c66c49 | 202 | # Find all examples |
dkato | 0:f782d9c66c49 | 203 | example_directories = find_all_examples(example['name']) |
dkato | 0:f782d9c66c49 | 204 | |
dkato | 0:f782d9c66c49 | 205 | os.chdir(example['name']) |
dkato | 0:f782d9c66c49 | 206 | |
dkato | 0:f782d9c66c49 | 207 | # checkout and synchronise the release-candidate branch |
dkato | 0:f782d9c66c49 | 208 | prepare_fork(example['github']) |
dkato | 0:f782d9c66c49 | 209 | |
dkato | 0:f782d9c66c49 | 210 | for example_directory in example_directories: |
dkato | 0:f782d9c66c49 | 211 | if not upgrade_single_example(example, tag, os.path.relpath(example_directory, example['name']), ref): |
dkato | 0:f782d9c66c49 | 212 | os.chdir(cwd) |
dkato | 0:f782d9c66c49 | 213 | return False |
dkato | 0:f782d9c66c49 | 214 | |
dkato | 0:f782d9c66c49 | 215 | # Setup the default commit message |
dkato | 0:f782d9c66c49 | 216 | commit_message = 'Updating mbed-os to ' + tag |
dkato | 0:f782d9c66c49 | 217 | |
dkato | 0:f782d9c66c49 | 218 | # Setup and run the commit command |
dkato | 0:f782d9c66c49 | 219 | commit_cmd = ['git', 'commit', '-m', commit_message] |
dkato | 0:f782d9c66c49 | 220 | return_code = run_cmd(commit_cmd) |
dkato | 0:f782d9c66c49 | 221 | if not return_code: |
dkato | 0:f782d9c66c49 | 222 | |
dkato | 0:f782d9c66c49 | 223 | # Setup and run the push command |
dkato | 0:f782d9c66c49 | 224 | push_cmd = ['git', 'push', 'origin'] |
dkato | 0:f782d9c66c49 | 225 | return_code = run_cmd(push_cmd) |
dkato | 0:f782d9c66c49 | 226 | |
dkato | 0:f782d9c66c49 | 227 | if not return_code: |
dkato | 0:f782d9c66c49 | 228 | body = "Please test/merge this PR and then tag Master with " + tag |
dkato | 0:f782d9c66c49 | 229 | # Raise a PR from release-candidate to master |
dkato | 0:f782d9c66c49 | 230 | user_fork = user + ':master' |
dkato | 0:f782d9c66c49 | 231 | try: |
dkato | 0:f782d9c66c49 | 232 | pr = repo.create_pull(title='Updating mbed-os to ' + tag, head=user_fork, base='master', body=body) |
dkato | 0:f782d9c66c49 | 233 | ret = True |
dkato | 0:f782d9c66c49 | 234 | except GithubException as e: |
dkato | 0:f782d9c66c49 | 235 | # Default to False |
dkato | 0:f782d9c66c49 | 236 | print("Creation of Pull Request from release-candidate to master failed with the following error!") |
dkato | 0:f782d9c66c49 | 237 | print e |
dkato | 0:f782d9c66c49 | 238 | else: |
dkato | 0:f782d9c66c49 | 239 | print("!!! Git push command failed.") |
dkato | 0:f782d9c66c49 | 240 | else: |
dkato | 0:f782d9c66c49 | 241 | print("!!! Git commit command failed.") |
dkato | 0:f782d9c66c49 | 242 | else: |
dkato | 0:f782d9c66c49 | 243 | print("!!! Could not clone user fork %s\n" % fork) |
dkato | 0:f782d9c66c49 | 244 | |
dkato | 0:f782d9c66c49 | 245 | |
dkato | 0:f782d9c66c49 | 246 | os.chdir(cwd) |
dkato | 0:f782d9c66c49 | 247 | return ret |
dkato | 0:f782d9c66c49 | 248 | |
dkato | 0:f782d9c66c49 | 249 | def create_work_directory(path): |
dkato | 0:f782d9c66c49 | 250 | """ Create a new directory specified in 'path', overwrite if the directory already |
dkato | 0:f782d9c66c49 | 251 | exists. |
dkato | 0:f782d9c66c49 | 252 | |
dkato | 0:f782d9c66c49 | 253 | Args: |
dkato | 0:f782d9c66c49 | 254 | path - directory path to be created. |
dkato | 0:f782d9c66c49 | 255 | |
dkato | 0:f782d9c66c49 | 256 | """ |
dkato | 0:f782d9c66c49 | 257 | if os.path.exists(path): |
dkato | 0:f782d9c66c49 | 258 | print("'%s' directory already exists. Deleting..." % path) |
dkato | 0:f782d9c66c49 | 259 | rmtree_readonly(path) |
dkato | 0:f782d9c66c49 | 260 | |
dkato | 0:f782d9c66c49 | 261 | os.makedirs(path) |
dkato | 0:f782d9c66c49 | 262 | |
dkato | 0:f782d9c66c49 | 263 | def test_compile(config, tag): |
dkato | 0:f782d9c66c49 | 264 | """ For each example repo identified in the config json object, clone, update mbed-os to |
dkato | 0:f782d9c66c49 | 265 | the specified tag and then compile for all supported toolchains. |
dkato | 0:f782d9c66c49 | 266 | |
dkato | 0:f782d9c66c49 | 267 | Args: |
dkato | 0:f782d9c66c49 | 268 | config - the json object imported from the file. |
dkato | 0:f782d9c66c49 | 269 | tag - GitHub tag corresponding to a version of mbed-os to upgrade to. |
dkato | 0:f782d9c66c49 | 270 | results - summary of compilation results. |
dkato | 0:f782d9c66c49 | 271 | |
dkato | 0:f782d9c66c49 | 272 | """ |
dkato | 0:f782d9c66c49 | 273 | # Create work directories |
dkato | 0:f782d9c66c49 | 274 | create_work_directory('test_compile') |
dkato | 0:f782d9c66c49 | 275 | |
dkato | 0:f782d9c66c49 | 276 | # Loop through the examples |
dkato | 0:f782d9c66c49 | 277 | results = {} |
dkato | 0:f782d9c66c49 | 278 | os.chdir('test_compile') |
dkato | 0:f782d9c66c49 | 279 | |
dkato | 0:f782d9c66c49 | 280 | lib.source_repos(config) |
dkato | 0:f782d9c66c49 | 281 | lib.update_mbedos_version(config, tag) |
dkato | 0:f782d9c66c49 | 282 | results = lib.compile_repos(config, SUPPORTED_TOOLCHAINS) |
dkato | 0:f782d9c66c49 | 283 | os.chdir("..") |
dkato | 0:f782d9c66c49 | 284 | |
dkato | 0:f782d9c66c49 | 285 | return results |
dkato | 0:f782d9c66c49 | 286 | |
dkato | 0:f782d9c66c49 | 287 | |
dkato | 0:f782d9c66c49 | 288 | def main(arguments): |
dkato | 0:f782d9c66c49 | 289 | """ Will update any mbed-os.lib files found in the example list specified by the config file. |
dkato | 0:f782d9c66c49 | 290 | If no config file is specified the default 'examples.json' is used. |
dkato | 0:f782d9c66c49 | 291 | The update is done by cloning a fork of each example (the fork must be present in the |
dkato | 0:f782d9c66c49 | 292 | github account specified by the github user parameter). The fork is searched for any |
dkato | 0:f782d9c66c49 | 293 | mbed-os.lib files and each one found is updated with the SHA corresponding to the supplied |
dkato | 0:f782d9c66c49 | 294 | github tag. A pull request is then made from the fork to the original example repo. |
dkato | 0:f782d9c66c49 | 295 | |
dkato | 0:f782d9c66c49 | 296 | Args: |
dkato | 0:f782d9c66c49 | 297 | tag - tag to update the mbed-os.lib to. E.g. mbed-os-5.3.1 |
dkato | 0:f782d9c66c49 | 298 | github_token - Pre-authorised token to allow github access |
dkato | 0:f782d9c66c49 | 299 | github_user - github username whose account contains the example forks |
dkato | 0:f782d9c66c49 | 300 | config_file - optional parameter to specify a list of examples |
dkato | 0:f782d9c66c49 | 301 | |
dkato | 0:f782d9c66c49 | 302 | """ |
dkato | 0:f782d9c66c49 | 303 | |
dkato | 0:f782d9c66c49 | 304 | parser = argparse.ArgumentParser(description=__doc__, |
dkato | 0:f782d9c66c49 | 305 | formatter_class=argparse.RawDescriptionHelpFormatter) |
dkato | 0:f782d9c66c49 | 306 | parser.add_argument('tag', help="mbed-os tag to which all examples will be updated") |
dkato | 0:f782d9c66c49 | 307 | parser.add_argument('-c', '--config_file', help="Path to the configuration file (default is 'examples.json')", default='examples.json') |
dkato | 0:f782d9c66c49 | 308 | parser.add_argument('-T', '--github_token', help="GitHub token for secure access") |
dkato | 0:f782d9c66c49 | 309 | parser.add_argument('-U', '--github_user', help="GitHub user for forked repos") |
dkato | 0:f782d9c66c49 | 310 | |
dkato | 0:f782d9c66c49 | 311 | args = parser.parse_args(arguments) |
dkato | 0:f782d9c66c49 | 312 | |
dkato | 0:f782d9c66c49 | 313 | cfg = os.path.join(os.path.dirname(__file__), args.config_file) |
dkato | 0:f782d9c66c49 | 314 | |
dkato | 0:f782d9c66c49 | 315 | # Load the config file |
dkato | 0:f782d9c66c49 | 316 | config = json.load(open(os.path.join(os.path.dirname(__file__), |
dkato | 0:f782d9c66c49 | 317 | args.config_file))) |
dkato | 0:f782d9c66c49 | 318 | |
dkato | 0:f782d9c66c49 | 319 | if not config: |
dkato | 0:f782d9c66c49 | 320 | print("Failed to load config file '%s'" % args.config_file) |
dkato | 0:f782d9c66c49 | 321 | sys.exit(1) |
dkato | 0:f782d9c66c49 | 322 | |
dkato | 0:f782d9c66c49 | 323 | # Create working directory |
dkato | 0:f782d9c66c49 | 324 | create_work_directory('examples') |
dkato | 0:f782d9c66c49 | 325 | |
dkato | 0:f782d9c66c49 | 326 | github = Github(args.github_token) |
dkato | 0:f782d9c66c49 | 327 | |
dkato | 0:f782d9c66c49 | 328 | # Get the github sha corresponding to the specified mbed-os tag |
dkato | 0:f782d9c66c49 | 329 | cmd = ['git', 'rev-list', '-1', args.tag] |
dkato | 0:f782d9c66c49 | 330 | return_code, ref = run_cmd_with_output(cmd) |
dkato | 0:f782d9c66c49 | 331 | |
dkato | 0:f782d9c66c49 | 332 | if return_code: |
dkato | 0:f782d9c66c49 | 333 | print("Could not obtain SHA for tag: %s\n" % args.tag) |
dkato | 0:f782d9c66c49 | 334 | sys.exit(1) |
dkato | 0:f782d9c66c49 | 335 | |
dkato | 0:f782d9c66c49 | 336 | # Loop through the examples |
dkato | 0:f782d9c66c49 | 337 | failures = [] |
dkato | 0:f782d9c66c49 | 338 | successes = [] |
dkato | 0:f782d9c66c49 | 339 | results = {} |
dkato | 0:f782d9c66c49 | 340 | os.chdir('examples') |
dkato | 0:f782d9c66c49 | 341 | |
dkato | 0:f782d9c66c49 | 342 | for example in config['examples']: |
dkato | 0:f782d9c66c49 | 343 | # Determine if this example should be updated and if so update any found |
dkato | 0:f782d9c66c49 | 344 | # mbed-os.lib files. |
dkato | 0:f782d9c66c49 | 345 | |
dkato | 0:f782d9c66c49 | 346 | if upgrade_example(github, example, args.tag, args.github_user, ref): |
dkato | 0:f782d9c66c49 | 347 | successes += [example['name']] |
dkato | 0:f782d9c66c49 | 348 | else: |
dkato | 0:f782d9c66c49 | 349 | failures += [example['name']] |
dkato | 0:f782d9c66c49 | 350 | |
dkato | 0:f782d9c66c49 | 351 | os.chdir('../') |
dkato | 0:f782d9c66c49 | 352 | |
dkato | 0:f782d9c66c49 | 353 | # Finish the script and report the results |
dkato | 0:f782d9c66c49 | 354 | print(os.linesep + os.linesep +'Finished updating examples!' + os.linesep) |
dkato | 0:f782d9c66c49 | 355 | |
dkato | 0:f782d9c66c49 | 356 | if successes: |
dkato | 0:f782d9c66c49 | 357 | print('\nThe following examples updated successfully:') |
dkato | 0:f782d9c66c49 | 358 | for success in successes: |
dkato | 0:f782d9c66c49 | 359 | print(' - %s' % success) |
dkato | 0:f782d9c66c49 | 360 | |
dkato | 0:f782d9c66c49 | 361 | if failures: |
dkato | 0:f782d9c66c49 | 362 | print('\nThe following examples were not updated:') |
dkato | 0:f782d9c66c49 | 363 | for fail in failures: |
dkato | 0:f782d9c66c49 | 364 | print(' - %s' % fail) |
dkato | 0:f782d9c66c49 | 365 | |
dkato | 0:f782d9c66c49 | 366 | if __name__ == '__main__': |
dkato | 0:f782d9c66c49 | 367 | sys.exit(main(sys.argv[1:])) |