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