14
14
# 2) Update a different ARMmbed branch of the specified example
15
15
#
16
16
# 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
+ #
18
36
#
19
37
# Command usage:
20
38
#
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
22
40
#
23
41
# Where:
24
42
# -c <config file> - Optional path to an examples file.
27
45
# -l <logging level> - Optional Level for providing logging output. Can be one of,
28
46
# CRITICAL, ERROR, WARNING, INFO, DEBUG
29
47
# 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.
34
55
#
35
- # <tag> mbed-os tag to which all examples will be updated
36
56
#
37
57
38
58
import os
39
- from os .path import dirname , abspath , basename
59
+ from os .path import dirname , abspath , basename , join
40
60
import sys
41
61
import logging
42
62
import argparse
46
66
import stat
47
67
import re
48
68
from github import Github , GithubException
69
+ from jinja2 import FileSystemLoader , StrictUndefined
70
+ from jinja2 .environment import Environment
49
71
50
72
ROOT = abspath (dirname (dirname (dirname (dirname (__file__ )))))
51
73
sys .path .insert (0 , ROOT )
@@ -216,7 +238,6 @@ def prepare_fork(arm_example):
216
238
217
239
Args:
218
240
arm_example - Full GitHub repo path for original example
219
- ret - True if the fork was synchronised successfully, False otherwise
220
241
221
242
"""
222
243
@@ -227,111 +248,89 @@ def prepare_fork(arm_example):
227
248
['git' , 'fetch' , 'armmbed' ],
228
249
['git' , 'reset' , '--hard' , 'armmbed/master' ],
229
250
['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 )
234
252
235
- def prepare_branch (branch ):
253
+ def prepare_branch (src , dst ):
236
254
""" Set up at branch ready for use in updating examples
237
255
238
256
Description:
239
257
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.
242
260
The branch is then switched to.
243
261
244
262
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
247
265
248
266
"""
249
267
250
- update_log .debug ("Preparing branch: %s" , branch )
268
+ update_log .debug ("Preparing branch: %s" , dst )
251
269
252
270
# Check if branch already exists or not.
253
271
cmd = ['git' , 'branch' ]
254
- return_code , output = run_cmd_with_output (cmd )
272
+ _ , output = run_cmd_with_output (cmd , exit_on_failure = True )
255
273
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
261
278
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 ]]:
268
282
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 ):
277
290
""" Upgrade all versions of mbed-os.lib found in the specified example repo
278
291
279
292
Description:
280
293
281
294
Clone a version of the example specified and upgrade all versions of
282
295
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
+
296
306
Args:
297
307
github - GitHub instance to allow internal git commands to be run
298
308
example - json example object containing the GitHub repo to update.
299
309
tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
300
310
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
303
314
304
315
returns True if the upgrade was successful, False otherwise
305
316
"""
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'
306
322
307
323
ret = False
308
324
update_log .info ("Updating example '%s'" , example ['name' ])
309
325
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" ))
317
328
318
329
cwd = os .getcwd ()
319
330
320
- upstream_repo = 'ARMmbed/' + example ['name' ]
321
331
update_repo = "https://github.com/" + user + '/' + example ['name' ]
322
-
323
- update_log .debug ("Upstream repository: %s" , upstream_repo )
324
332
update_log .debug ("Update repository: %s" , update_repo )
325
333
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
-
335
334
# Clone the example repo
336
335
clone_cmd = ['git' , 'clone' , update_repo ]
337
336
return_code = run_cmd (clone_cmd )
@@ -343,13 +342,11 @@ def upgrade_example(github, example, tag, ref,
343
342
344
343
os .chdir (example ['name' ])
345
344
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 :
349
349
prepare_fork (example ['github' ])
350
-
351
- if branch != 'master' :
352
- prepare_branch (branch )
353
350
354
351
for example_directory in example_directories :
355
352
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,
369
366
return_code = run_cmd (push_cmd )
370
367
371
368
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
+
374
387
# Raise a PR from release-candidate to master
375
388
user_fork = user + ':master'
376
389
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 )
378
391
ret = True
379
392
except GithubException as e :
380
393
# Default to False
@@ -409,16 +422,15 @@ def create_work_directory(path):
409
422
410
423
parser = argparse .ArgumentParser (description = __doc__ ,
411
424
formatter_class = argparse .RawDescriptionHelpFormatter )
412
- parser .add_argument ('tag' , help = "mbed-os tag to which all examples will be updated" )
413
425
parser .add_argument ('-c' , '--config_file' , help = "Path to the configuration file (default is 'examples.json')" , default = 'examples.json' )
414
426
parser .add_argument ('-T' , '--github_token' , help = "GitHub token for secure access" )
415
427
parser .add_argument ('-l' , '--log-level' ,
416
428
help = "Level for providing logging output" ,
417
429
default = 'INFO' )
418
430
419
431
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' )
422
434
423
435
args = parser .parse_args ()
424
436
@@ -441,30 +453,43 @@ def create_work_directory(path):
441
453
create_work_directory ('examples' )
442
454
443
455
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 )
444
471
445
472
# 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 ]
447
474
return_code , ref = run_cmd_with_output (cmd )
448
475
449
476
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 )
451
478
sys .exit (1 )
452
479
453
480
# Loop through the examples
454
481
failures = []
455
482
successes = []
456
483
results = {}
484
+ template = dirname (abspath (__file__ ))
485
+
457
486
os .chdir ('examples' )
458
487
459
488
for example in json_data ['examples' ]:
460
489
# Determine if this example should be updated and if so update any found
461
490
# mbed-os.lib files.
462
491
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 )
468
493
469
494
if result :
470
495
successes += [example ['name' ]]
0 commit comments