Skip to content

Commit 108250d

Browse files
committed
Update branching option to branch from another branch.
Previously if updating a branch in the ARMmbed version of an example repo, the branch would be created initially from master. This update allows the new branch to be created by any pre-existing branch. This update also moves the branch / fork / tag configuration data to the json config file. It thus simplifies the command line. -b on its own now indicates use the branch information in the config -f on its own now indicates use the fork information in the config
1 parent f31ea01 commit 108250d

File tree

2 files changed

+96
-66
lines changed

2 files changed

+96
-66
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/update.py

Lines changed: 82 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -232,106 +232,101 @@ def prepare_fork(arm_example):
232232
return False
233233
return True
234234

235-
def prepare_branch(branch):
235+
def prepare_branch(src, dst):
236236
""" Set up at branch ready for use in updating examples
237237
238238
Description:
239239
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.
240+
This function checks whether or not the supplied dst branch exists.
241+
If it does not, the branch is created from the src and pushed to the origin.
242242
The branch is then switched to.
243243
244244
Args:
245-
arm_example - Full GitHub repo path for original example
245+
src - branch to create the dst branch from
246+
dst - branch to update
247+
248+
Return:
246249
ret - True if the fork was synchronised successfully, False otherwise
247250
248251
"""
249252

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

252255
# Check if branch already exists or not.
253256
cmd = ['git', 'branch']
254257
return_code, output = run_cmd_with_output(cmd)
255258

256-
if not branch in output:
259+
if not dst in output:
260+
257261
# OOB branch does not exist thus create it and then check it out
258-
cmd = ['git', 'checkout', '-b', branch]
262+
263+
# First ensure we are on the src branch
264+
cmd = ['git', 'checkout', src]
259265
return_code = run_cmd(cmd)
260266
if not return_code:
261-
262-
# Push new branch upstream
263-
cmd = ['git', 'push', '-u', 'origin', branch]
267+
268+
cmd = ['git', 'checkout', '-b', dst]
264269
return_code = run_cmd(cmd)
270+
if not return_code:
271+
272+
# Push new branch upstream
273+
cmd = ['git', 'push', '-u', 'origin', dst]
274+
return_code = run_cmd(cmd)
265275
else:
266-
cmd = ['git', 'checkout', branch]
276+
cmd = ['git', 'checkout', dst]
267277
return_code = run_cmd(cmd)
268278

269279
if return_code:
270-
update_log.error("Failed to prepare branch: %s", branch)
280+
update_log.error("Failed to prepare branch: %s", dst)
271281
return False
272282

273283
return True
274284

275-
def upgrade_example(github, example, tag, ref,
276-
user='ARMmbed', branch='master'):
285+
def upgrade_example(github, example, tag, ref, user, src, dst):
277286
""" Upgrade all versions of mbed-os.lib found in the specified example repo
278287
279288
Description:
280289
281290
Clone a version of the example specified and upgrade all versions of
282291
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-
292+
is upgraded depends on the user, src and dst settings.
293+
1) user == None
294+
The destination branch will be updated with the version of mbed-os
295+
idenfied by the tag. If the destination branch does not exist then it
296+
will be created from the source branch.
297+
298+
2) user != None
299+
The master branch of a fork of the example will be updated with the
300+
version of mbed-os identified by the tag.
301+
296302
Args:
297303
github - GitHub instance to allow internal git commands to be run
298304
example - json example object containing the GitHub repo to update.
299305
tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
300306
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)
307+
user - GitHub user name
308+
src - branch to create the dst branch from
309+
dst - branch to update
303310
304311
returns True if the upgrade was successful, False otherwise
305312
"""
313+
314+
# If a user has not been specified then branch update will be used and thus
315+
# the git user will be ARMmbed.
316+
if not user:
317+
user = 'ARMmbed'
306318

307319
ret = False
308320
update_log.info("Updating example '%s'", example['name'])
309321
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
322+
update_log.debug("Src branch: %s", (src or "None"))
323+
update_log.debug("Dst branch: %s", (dst or "None"))
317324

318325
cwd = os.getcwd()
319326

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

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-
335330
# Clone the example repo
336331
clone_cmd = ['git', 'clone', update_repo]
337332
return_code = run_cmd(clone_cmd)
@@ -343,13 +338,11 @@ def upgrade_example(github, example, tag, ref,
343338

344339
os.chdir(example['name'])
345340

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':
341+
# If the user is ARMmbed then a branch is used.
342+
if user == 'ARMmbed':
343+
prepare_branch(src, dst)
344+
else:
349345
prepare_fork(example['github'])
350-
351-
if branch != 'master':
352-
prepare_branch(branch)
353346

354347
for example_directory in example_directories:
355348
if not upgrade_single_example(example, tag, os.path.relpath(example_directory, example['name']), ref):
@@ -369,8 +362,22 @@ def upgrade_example(github, example, tag, ref,
369362
return_code = run_cmd(push_cmd)
370363

371364
if not return_code:
372-
if user != 'ARMmbed':
373-
body = "Please test/merge this PR and then tag Master with " + tag
365+
# If the user is not ARMmbed then a fork is being used
366+
if user != 'ARMmbed':
367+
368+
upstream_repo = 'ARMmbed/'+ example['name']
369+
update_log.debug("Upstream repository: %s", upstream_repo)
370+
# Check access to mbed-os repo
371+
try:
372+
repo = github.get_repo(upstream_repo, False)
373+
374+
except:
375+
update_log.error("Upstream repo: %s, does not exist - skipping", upstream_repo)
376+
return False
377+
378+
body = "Please test this PR.\n"
379+
body += "If successful then merge, otherwise provide a known issue.\n"
380+
body += "Once you get notification of the release being made public then tag Master with " + tag
374381
# Raise a PR from release-candidate to master
375382
user_fork = user + ':master'
376383
try:
@@ -409,16 +416,15 @@ def create_work_directory(path):
409416

410417
parser = argparse.ArgumentParser(description=__doc__,
411418
formatter_class=argparse.RawDescriptionHelpFormatter)
412-
parser.add_argument('tag', help="mbed-os tag to which all examples will be updated")
413419
parser.add_argument('-c', '--config_file', help="Path to the configuration file (default is 'examples.json')", default='examples.json')
414420
parser.add_argument('-T', '--github_token', help="GitHub token for secure access")
415421
parser.add_argument('-l', '--log-level',
416422
help="Level for providing logging output",
417423
default='INFO')
418424

419425
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")
426+
exclusive.add_argument('-f', '--fork', help="Update a fork", action='store_true')
427+
exclusive.add_argument('-b', '--branch', help="Update a branch", action='store_true')
422428

423429
args = parser.parse_args()
424430

@@ -441,9 +447,24 @@ def create_work_directory(path):
441447
create_work_directory('examples')
442448

443449
github = Github(args.github_token)
450+
config = json_data['update-config']
451+
tag = config['tag']
452+
453+
user = None
454+
src = "master"
455+
dst = None
456+
457+
if args.fork:
458+
user = config['via-fork']['github-user']
459+
elif args.branch:
460+
src = config['via-branch']['src-branch']
461+
dst = config['via-branch']['dst-branch']
462+
else:
463+
userlog.error("Must specify either -f or -b command line option")
464+
exit(1)
444465

445466
# Get the github sha corresponding to the specified mbed-os tag
446-
cmd = ['git', 'rev-list', '-1', args.tag]
467+
cmd = ['git', 'rev-list', '-1', tag]
447468
return_code, ref = run_cmd_with_output(cmd)
448469

449470
if return_code:
@@ -460,11 +481,7 @@ def create_work_directory(path):
460481
# Determine if this example should be updated and if so update any found
461482
# mbed-os.lib files.
462483

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)
484+
result = upgrade_example(github, example, tag, ref, user, src, dst)
468485

469486
if result:
470487
successes += [example['name']]

0 commit comments

Comments
 (0)