Skip to content

Commit 18b1cb6

Browse files
Merge pull request #4531 from adbridge/update
Update branching option to branch from another branch.
2 parents 8f42c87 + d1f3eb6 commit 18b1cb6

File tree

3 files changed

+135
-93
lines changed

3 files changed

+135
-93
lines changed

tools/test/examples/examples.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
{
1+
{
2+
"update-config" : {
3+
"help" : "Update each example repo with a version of mbed-os identified by the tag",
4+
"via-fork" : {
5+
"help" : "-f cmd line option. Update a fork",
6+
"github-user" : "adbridge"
7+
},
8+
"via-branch" : {
9+
"help" : "-b cmd line option. Update dst branch, created from src branch",
10+
"src-branch" : "mbed-os-5.5.0-rc1-oob",
11+
"dst-branch" : "mbed-os-5.5.0-rc2-oob"
12+
},
13+
"tag" : "mbed-os-5.5.0-rc2"
14+
},
215
"examples": [
316
{
417
"name": "mbed-os-example-blinky",

tools/test/examples/pr.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Please test this PR
2+
3+
If successful then merge, otherwise provide a known issue.
4+
Once you get notification of the release being made public then tag Master with {{ tag }} .

tools/test/examples/update.py

Lines changed: 117 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,29 @@
1414
# 2) Update a different ARMmbed branch of the specified example
1515
#
1616
# A branch to update is specified. If it doesn't already exist then it is first created.
17-
# This branch will be updated and the change automatically pushed.
17+
# This branch will be updated and the change automatically pushed. The new branch will
18+
# be created from the specified source branch.
19+
#
20+
# The modes are controlled via configuration data in the json file.
21+
# E.g.
22+
#
23+
# "update-config" : {
24+
# "help" : "Update each example repo with a version of mbed-os identified by the tag",
25+
# "via-fork" : {
26+
# "help" : "-f cmd line option. Update a fork",
27+
# "github-user" : "adbridge"
28+
# },
29+
# "via-branch" : {
30+
# "help" : "-b cmd line option. Update dst branch, created from src branch",
31+
# "src-branch" : "mbed-os-5.5.0-rc1-oob",
32+
# "dst-branch" : "mbed-os-5.5.0-rc2-oob"
33+
# },
34+
# "tag" : "mbed-os-5.5.0-rc2"
35+
#
1836
#
1937
# Command usage:
2038
#
21-
# update.py -c <config file> - T <github_token> -l <logging level> -U <github user> -b <branch> <tag>
39+
# update.py -c <config file> - T <github_token> -l <logging level> -f -b
2240
#
2341
# Where:
2442
# -c <config file> - Optional path to an examples file.
@@ -27,16 +45,18 @@
2745
# -l <logging level> - Optional Level for providing logging output. Can be one of,
2846
# CRITICAL, ERROR, WARNING, INFO, DEBUG
2947
# If not provided the default is 'INFO'
30-
# -U <github_user> - GitHub user for forked repos
31-
# -b <branch> - Branch to be updated
32-
#
33-
# NOTE only one of -U or -b can be specified.
48+
# -f - Update forked repos. This will use the 'github-user' parameter in
49+
# the 'via-fork' section.
50+
# -b - Update branched repos. This will use the "src-branch" and
51+
# "dst-branch" parameters in the 'via-branch' section. The destination
52+
# branch is created from the source branch (if it doesn't already exist).
53+
#
54+
# The options -f and -b are mutually exlusive. Only one can be specified.
3455
#
35-
# <tag> mbed-os tag to which all examples will be updated
3656
#
3757

3858
import os
39-
from os.path import dirname, abspath, basename
59+
from os.path import dirname, abspath, basename, join
4060
import sys
4161
import logging
4262
import argparse
@@ -46,6 +66,8 @@
4666
import stat
4767
import re
4868
from github import Github, GithubException
69+
from jinja2 import FileSystemLoader, StrictUndefined
70+
from jinja2.environment import Environment
4971

5072
ROOT = abspath(dirname(dirname(dirname(dirname(__file__)))))
5173
sys.path.insert(0, ROOT)
@@ -216,7 +238,6 @@ def prepare_fork(arm_example):
216238
217239
Args:
218240
arm_example - Full GitHub repo path for original example
219-
ret - True if the fork was synchronised successfully, False otherwise
220241
221242
"""
222243

@@ -227,111 +248,89 @@ def prepare_fork(arm_example):
227248
['git', 'fetch', 'armmbed'],
228249
['git', 'reset', '--hard', 'armmbed/master'],
229250
['git', 'push', '-f', 'origin']]:
230-
if run_cmd(cmd):
231-
update_log.error("Fork preparation failed")
232-
return False
233-
return True
251+
run_cmd(cmd, exit_on_failure=True)
234252

235-
def prepare_branch(branch):
253+
def prepare_branch(src, dst):
236254
""" Set up at branch ready for use in updating examples
237255
238256
Description:
239257
240-
This function checks whether or not the supplied branch exists.
241-
If it does not, the branch is created and pushed to the origin.
258+
This function checks whether or not the supplied dst branch exists.
259+
If it does not, the branch is created from the src and pushed to the origin.
242260
The branch is then switched to.
243261
244262
Args:
245-
arm_example - Full GitHub repo path for original example
246-
ret - True if the fork was synchronised successfully, False otherwise
263+
src - branch to create the dst branch from
264+
dst - branch to update
247265
248266
"""
249267

250-
update_log.debug("Preparing branch: %s", branch)
268+
update_log.debug("Preparing branch: %s", dst)
251269

252270
# Check if branch already exists or not.
253271
cmd = ['git', 'branch']
254-
return_code, output = run_cmd_with_output(cmd)
272+
_, output = run_cmd_with_output(cmd, exit_on_failure=True)
255273

256-
if not branch in output:
257-
# OOB branch does not exist thus create it and then check it out
258-
cmd = ['git', 'checkout', '-b', branch]
259-
return_code = run_cmd(cmd)
260-
if not return_code:
274+
if not dst in output:
275+
276+
# OOB branch does not exist thus create it, first ensuring we are on
277+
# the src branch and then check it out
261278

262-
# Push new branch upstream
263-
cmd = ['git', 'push', '-u', 'origin', branch]
264-
return_code = run_cmd(cmd)
265-
else:
266-
cmd = ['git', 'checkout', branch]
267-
return_code = run_cmd(cmd)
279+
for cmd in [['git', 'checkout', src],
280+
['git', 'checkout', '-b', dst],
281+
['git', 'push', '-u', 'origin', dst]]:
268282

269-
if return_code:
270-
update_log.error("Failed to prepare branch: %s", branch)
271-
return False
272-
273-
return True
274-
275-
def upgrade_example(github, example, tag, ref,
276-
user='ARMmbed', branch='master'):
283+
run_cmd(cmd, exit_on_failure=True)
284+
285+
else:
286+
cmd = ['git', 'checkout', dst]
287+
run_cmd(cmd, exit_on_failure=True)
288+
289+
def upgrade_example(github, example, tag, ref, user, src, dst, template):
277290
""" Upgrade all versions of mbed-os.lib found in the specified example repo
278291
279292
Description:
280293
281294
Clone a version of the example specified and upgrade all versions of
282295
mbed-os.lib found within its tree. The version cloned and how it
283-
is upgraded depends on the user and branch specified. Only two options
284-
are valid:
285-
1) ARMmbed + non master branch
286-
This option will update the branch directly in the ARMmbed repo. If the
287-
branch does not exist it will be first created.
288-
289-
2) alternative user + master branch
290-
291-
This option assumes that a fork of the repo exists in the specified user's
292-
account. The fork will first be updated so that it is up to date with the
293-
upstream version , then the fork will be updated and a PR raised against
294-
the upstream ie ARMmbed repo.
295-
296+
is upgraded depends on the user, src and dst settings.
297+
1) user == None
298+
The destination branch will be updated with the version of mbed-os
299+
idenfied by the tag. If the destination branch does not exist then it
300+
will be created from the source branch.
301+
302+
2) user != None
303+
The master branch of a fork of the example will be updated with the
304+
version of mbed-os identified by the tag.
305+
296306
Args:
297307
github - GitHub instance to allow internal git commands to be run
298308
example - json example object containing the GitHub repo to update.
299309
tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
300310
ref - SHA corresponding to the tag
301-
user - GitHub user name (defaults to 'ARMmbed' if not supplied)
302-
branch - branch to update (defaults to 'master' if not supplied)
311+
user - GitHub user name
312+
src - branch to create the dst branch from
313+
dst - branch to update
303314
304315
returns True if the upgrade was successful, False otherwise
305316
"""
317+
318+
# If a user has not been specified then branch update will be used and thus
319+
# the git user will be ARMmbed.
320+
if not user:
321+
user = 'ARMmbed'
306322

307323
ret = False
308324
update_log.info("Updating example '%s'", example['name'])
309325
update_log.debug("User: %s", user)
310-
update_log.debug("Branch: %s", branch)
311-
312-
# First check validity of user/branch combination
313-
if ((user == 'ARMmbed' and branch == 'master') or
314-
(user != 'ARMmbed' and branch != 'master')):
315-
update_log.error("Invalid user/branch combination")
316-
return False
326+
update_log.debug("Src branch: %s", (src or "None"))
327+
update_log.debug("Dst branch: %s", (dst or "None"))
317328

318329
cwd = os.getcwd()
319330

320-
upstream_repo = 'ARMmbed/'+ example['name']
321331
update_repo = "https://github.com/" + user + '/' + example['name']
322-
323-
update_log.debug("Upstream repository: %s", upstream_repo)
324332
update_log.debug("Update repository: %s", update_repo)
325333

326-
# Check access to mbed-os repo
327-
try:
328-
repo = github.get_repo(upstream_repo, False)
329-
330-
except:
331-
update_log.error("Upstream repo: %s, does not exist - skipping", upstream_repo)
332-
return False
333-
334-
335334
# Clone the example repo
336335
clone_cmd = ['git', 'clone', update_repo]
337336
return_code = run_cmd(clone_cmd)
@@ -343,13 +342,11 @@ def upgrade_example(github, example, tag, ref,
343342

344343
os.chdir(example['name'])
345344

346-
# If the user is not the default, then a fork will be used. Thus
347-
# synchronise the user fork with the upstream
348-
if user != 'ARMmbed':
345+
# If the user is ARMmbed then a branch is used.
346+
if user == 'ARMmbed':
347+
prepare_branch(src, dst)
348+
else:
349349
prepare_fork(example['github'])
350-
351-
if branch != 'master':
352-
prepare_branch(branch)
353350

354351
for example_directory in example_directories:
355352
if not upgrade_single_example(example, tag, os.path.relpath(example_directory, example['name']), ref):
@@ -369,12 +366,28 @@ def upgrade_example(github, example, tag, ref,
369366
return_code = run_cmd(push_cmd)
370367

371368
if not return_code:
372-
if user != 'ARMmbed':
373-
body = "Please test/merge this PR and then tag Master with " + tag
369+
# If the user is not ARMmbed then a fork is being used
370+
if user != 'ARMmbed':
371+
372+
upstream_repo = 'ARMmbed/'+ example['name']
373+
update_log.debug("Upstream repository: %s", upstream_repo)
374+
# Check access to mbed-os repo
375+
try:
376+
repo = github.get_repo(upstream_repo, False)
377+
378+
except:
379+
update_log.error("Upstream repo: %s, does not exist - skipping", upstream_repo)
380+
return False
381+
382+
jinja_loader = FileSystemLoader(template)
383+
jinja_environment = Environment(loader=jinja_loader,
384+
undefined=StrictUndefined)
385+
pr_body = jinja_environment.get_template("pr.tmpl").render(tag=tag)
386+
374387
# Raise a PR from release-candidate to master
375388
user_fork = user + ':master'
376389
try:
377-
pr = repo.create_pull(title='Updating mbed-os to ' + tag, head=user_fork, base='master', body=body)
390+
pr = repo.create_pull(title='Updating mbed-os to ' + tag, head=user_fork, base='master', body=pr_body)
378391
ret = True
379392
except GithubException as e:
380393
# Default to False
@@ -409,16 +422,15 @@ def create_work_directory(path):
409422

410423
parser = argparse.ArgumentParser(description=__doc__,
411424
formatter_class=argparse.RawDescriptionHelpFormatter)
412-
parser.add_argument('tag', help="mbed-os tag to which all examples will be updated")
413425
parser.add_argument('-c', '--config_file', help="Path to the configuration file (default is 'examples.json')", default='examples.json')
414426
parser.add_argument('-T', '--github_token', help="GitHub token for secure access")
415427
parser.add_argument('-l', '--log-level',
416428
help="Level for providing logging output",
417429
default='INFO')
418430

419431
exclusive = parser.add_mutually_exclusive_group(required=True)
420-
exclusive.add_argument('-U', '--github_user', help="GitHub user for forked repos, mutually exclusive to branch option")
421-
exclusive.add_argument('-b', '--branch', help="Branch to be updated, mutually exclusive to user option")
432+
exclusive.add_argument('-f', '--fork', help="Update a fork", action='store_true')
433+
exclusive.add_argument('-b', '--branch', help="Update a branch", action='store_true')
422434

423435
args = parser.parse_args()
424436

@@ -441,30 +453,43 @@ def create_work_directory(path):
441453
create_work_directory('examples')
442454

443455
github = Github(args.github_token)
456+
config = json_data['update-config']
457+
tag = config['tag']
458+
459+
user = None
460+
src = "master"
461+
dst = None
462+
463+
if args.fork:
464+
user = config['via-fork']['github-user']
465+
elif args.branch:
466+
src = config['via-branch']['src-branch']
467+
dst = config['via-branch']['dst-branch']
468+
else:
469+
userlog.error("Must specify either -f or -b command line option")
470+
exit(1)
444471

445472
# Get the github sha corresponding to the specified mbed-os tag
446-
cmd = ['git', 'rev-list', '-1', args.tag]
473+
cmd = ['git', 'rev-list', '-1', tag]
447474
return_code, ref = run_cmd_with_output(cmd)
448475

449476
if return_code:
450-
update_log.error("Could not obtain SHA for tag: %s", args.tag)
477+
update_log.error("Could not obtain SHA for tag: %s", tag)
451478
sys.exit(1)
452479

453480
# Loop through the examples
454481
failures = []
455482
successes = []
456483
results = {}
484+
template = dirname(abspath(__file__))
485+
457486
os.chdir('examples')
458487

459488
for example in json_data['examples']:
460489
# Determine if this example should be updated and if so update any found
461490
# mbed-os.lib files.
462491

463-
# Only user or branch can be specified on the command line
464-
if args.github_user:
465-
result = upgrade_example(github, example, args.tag, ref, user=args.github_user)
466-
else:
467-
result = upgrade_example(github, example, args.tag, ref, branch=args.branch)
492+
result = upgrade_example(github, example, tag, ref, user, src, dst, template)
468493

469494
if result:
470495
successes += [example['name']]

0 commit comments

Comments
 (0)