42
42
# -c <config file> - Optional path to an examples file.
43
43
# If not proved the default is 'examples.json'
44
44
# -T <github_token> - GitHub token for secure access (required)
45
- # -l <logging level> - Optional Level for providing logging output. Can be one of,
46
- # CRITICAL, ERROR, WARNING, INFO, DEBUG
47
- # If not provided the default is 'INFO'
48
45
# -f - Update forked repos. This will use the 'github-user' parameter in
49
46
# the 'via-fork' section.
50
47
# -b - Update branched repos. This will use the "src-branch" and
75
72
import examples_lib as lib
76
73
from examples_lib import SUPPORTED_TOOLCHAINS
77
74
75
+ userlog = logging .getLogger ("Update" )
76
+
77
+ # Set logging level
78
+ userlog .setLevel (logging .DEBUG )
79
+
80
+ # Everything is output to the log file
81
+ logfile = os .path .join (os .getcwd (), 'update.log' )
82
+ fh = logging .FileHandler (logfile )
83
+ fh .setLevel (logging .DEBUG )
84
+
85
+ # create console handler with a higher log level
86
+ ch = logging .StreamHandler ()
87
+ ch .setLevel (logging .INFO )
88
+
89
+ formatter = logging .Formatter ('%(name)s: %(levelname)s - %(message)s' )
90
+ ch .setFormatter (formatter )
91
+ fh .setFormatter (formatter )
92
+
93
+ # add the handlers to the logger
94
+ userlog .addHandler (fh )
95
+ userlog .addHandler (ch )
96
+
78
97
def run_cmd (command , exit_on_failure = False ):
79
- """ Run a system command and return the result status
98
+ """ Run a system command returning a status result
80
99
81
- Description:
100
+ This is just a wrapper for the run_cmd_with_output() function, but
101
+ only returns the status of the call.
82
102
83
- Passes a command to the system and returns a True/False result, once the
84
- command has been executed, indicating success/failure. Commands are passed
85
- as a list of tokens.
86
- E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
87
-
88
103
Args:
89
104
command - system command as a list of tokens
90
105
exit_on_failure - If True exit the program on failure (default = False)
91
106
92
107
Returns:
93
108
return_code - True/False indicating the success/failure of the command
94
109
"""
95
- update_log .debug ('[Exec] %s' , ' ' .join (command ))
96
- return_code = subprocess .call (command , shell = True )
97
-
98
- if return_code :
99
- update_log .warning ("Command '%s' failed with return code: %s" ,
100
- ' ' .join (command ), return_code )
101
- if exit_on_failure :
102
- sys .exit (1 )
103
-
110
+ return_code , _ = run_cmd_with_output (command , exit_on_failure )
104
111
return return_code
105
112
106
113
def run_cmd_with_output (command , exit_on_failure = False ):
107
- """ Run a system command and return the result status plus output
114
+ """ Run a system command returning a status result and any command output
108
115
109
- Description:
110
-
111
116
Passes a command to the system and returns a True/False result once the
112
117
command has been executed, indicating success/failure. If the command was
113
118
successful then the output from the command is returned to the caller.
114
- Commands are passed as a list of tokens .
115
- E.g. The command 'git remote -v' would be passed in as [' git', ' remote', '-v']
119
+ Commands are passed as a string .
120
+ E.g. The command 'git remote -v' would be passed in as " git remote -v"
116
121
117
122
Args:
118
- command - system command as a list of tokens
123
+ command - system command as a string
119
124
exit_on_failure - If True exit the program on failure (default = False)
120
125
121
126
Returns:
122
- returncode - True/False indicating the success/failure of the command
127
+ return_code - True/False indicating the success/failure of the command
123
128
output - The output of the command if it was successful, else empty string
124
129
"""
125
- update_log .debug ('[Exec] %s' , ' ' .join (command ))
130
+ text = '[Exec] ' + command
131
+ userlog .debug (text )
126
132
returncode = 0
127
133
output = ""
128
134
try :
129
135
output = subprocess .check_output (command , shell = True )
130
136
except subprocess .CalledProcessError as e :
131
- update_log . warning ( "Command '%s' failed with return code: %s" ,
132
- ' ' . join ( command ), e . returncode )
137
+ text = "The command " + str ( command ) + " failed with return code: " + str ( e . returncode )
138
+ userlog . warning ( text )
133
139
returncode = e .returncode
134
140
if exit_on_failure :
135
141
sys .exit (1 )
136
142
return returncode , output
137
143
144
+
138
145
def rmtree_readonly (directory ):
139
146
""" Deletes a readonly directory tree.
140
147
@@ -198,7 +205,7 @@ def upgrade_single_example(example, tag, directory, ref):
198
205
199
206
os .rename ("mbed-os.lib" , "mbed-os.lib_bak" )
200
207
else :
201
- update_log .error ("Failed to backup mbed-os.lib prior to updating." )
208
+ userlog .error ("Failed to backup mbed-os.lib prior to updating." )
202
209
return False
203
210
204
211
# mbed-os.lib file contains one line with the following format
@@ -221,7 +228,7 @@ def upgrade_single_example(example, tag, directory, ref):
221
228
222
229
if updated :
223
230
# Setup and run the git add command
224
- cmd = [ ' git' , ' add' , ' mbed-os.lib' ]
231
+ cmd = " git add mbed-os.lib"
225
232
return_code = run_cmd (cmd )
226
233
227
234
os .chdir (cwd )
@@ -242,12 +249,12 @@ def prepare_fork(arm_example):
242
249
"""
243
250
244
251
logstr = "In: " + os .getcwd ()
245
- update_log .debug (logstr )
252
+ userlog .debug (logstr )
246
253
247
- for cmd in [[ ' git' , ' remote' , ' add' , ' armmbed' , arm_example ] ,
248
- [ ' git' , ' fetch' , ' armmbed' ] ,
249
- [ ' git' , ' reset' , ' --hard' , ' armmbed/master' ] ,
250
- [ ' git' , ' push' , '-f' , ' origin' ] ]:
254
+ for cmd in [" git remote add armmbed " + str ( arm_example ) ,
255
+ " git fetch armmbed" ,
256
+ " git reset --hard armmbed/master" ,
257
+ " git push -f origin" ]:
251
258
run_cmd (cmd , exit_on_failure = True )
252
259
253
260
def prepare_branch (src , dst ):
@@ -265,7 +272,7 @@ def prepare_branch(src, dst):
265
272
266
273
"""
267
274
268
- update_log .debug ("Preparing branch: %s" , dst )
275
+ userlog .debug ("Preparing branch: %s" , dst )
269
276
270
277
# Check if branch already exists or not.
271
278
# We can use the 'git branch -r' command. This returns all the remote branches for
@@ -285,14 +292,14 @@ def prepare_branch(src, dst):
285
292
# OOB branch does not exist thus create it, first ensuring we are on
286
293
# the src branch and then check it out
287
294
288
- for cmd in [[ ' git' , ' checkout' , src ] ,
289
- [ ' git' , ' checkout' , '-b' , dst ] ,
290
- [ ' git' , ' push' , '-u' , ' origin' , dst ] ]:
295
+ for cmd in [" git checkout " + str ( src ) ,
296
+ " git checkout -b " + str ( dst ) ,
297
+ " git push -u origin " + str ( dst ) ]:
291
298
292
299
run_cmd (cmd , exit_on_failure = True )
293
300
294
301
else :
295
- cmd = [ ' git' , ' checkout' , dst ]
302
+ cmd = " git checkout " + str ( dst )
296
303
run_cmd (cmd , exit_on_failure = True )
297
304
298
305
def upgrade_example (github , example , tag , ref , user , src , dst , template ):
@@ -330,18 +337,18 @@ def upgrade_example(github, example, tag, ref, user, src, dst, template):
330
337
user = 'ARMmbed'
331
338
332
339
ret = False
333
- update_log .info ("Updating example '%s'" , example ['name' ])
334
- update_log .debug ("User: %s" , user )
335
- update_log .debug ("Src branch: %s" , (src or "None" ))
336
- update_log .debug ("Dst branch: %s" , (dst or "None" ))
340
+ userlog .info ("Updating example '%s'" , example ['name' ])
341
+ userlog .debug ("User: %s" , user )
342
+ userlog .debug ("Src branch: %s" , (src or "None" ))
343
+ userlog .debug ("Dst branch: %s" , (dst or "None" ))
337
344
338
345
cwd = os .getcwd ()
339
346
340
347
update_repo = "https://github.com/" + user + '/' + example ['name' ]
341
- update_log .debug ("Update repository: %s" , update_repo )
348
+ userlog .debug ("Update repository: %s" , update_repo )
342
349
343
350
# Clone the example repo
344
- clone_cmd = [ ' git' , ' clone' , update_repo ]
351
+ clone_cmd = " git clone " + str ( update_repo )
345
352
return_code = run_cmd (clone_cmd )
346
353
347
354
if not return_code :
@@ -362,30 +369,27 @@ def upgrade_example(github, example, tag, ref, user, src, dst, template):
362
369
os .chdir (cwd )
363
370
return False
364
371
365
- # Setup the default commit message
366
- commit_message = 'Updating mbed-os to ' + tag
367
-
368
372
# Setup and run the commit command
369
- commit_cmd = [ ' git' , ' commit' , '-m' , commit_message ]
373
+ commit_cmd = " git commit -m \" Updating mbed-os to " + tag + " \" "
370
374
return_code = run_cmd (commit_cmd )
371
375
if not return_code :
372
376
373
377
# Setup and run the push command
374
- push_cmd = [ ' git' , ' push' , ' origin' ]
378
+ push_cmd = " git push origin"
375
379
return_code = run_cmd (push_cmd )
376
380
377
381
if not return_code :
378
382
# If the user is not ARMmbed then a fork is being used
379
383
if user != 'ARMmbed' :
380
384
381
385
upstream_repo = 'ARMmbed/' + example ['name' ]
382
- update_log .debug ("Upstream repository: %s" , upstream_repo )
386
+ userlog .debug ("Upstream repository: %s" , upstream_repo )
383
387
# Check access to mbed-os repo
384
388
try :
385
389
repo = github .get_repo (upstream_repo , False )
386
390
387
391
except :
388
- update_log .error ("Upstream repo: %s, does not exist - skipping" , upstream_repo )
392
+ userlog .error ("Upstream repo: %s, does not exist - skipping" , upstream_repo )
389
393
return False
390
394
391
395
jinja_loader = FileSystemLoader (template )
@@ -400,15 +404,15 @@ def upgrade_example(github, example, tag, ref, user, src, dst, template):
400
404
ret = True
401
405
except GithubException as e :
402
406
# Default to False
403
- update_log .error ("Pull request creation failed with error: %s" , e )
407
+ userlog .error ("Pull request creation failed with error: %s" , e )
404
408
else :
405
409
ret = True
406
410
else :
407
- update_log .error ("Git push command failed." )
411
+ userlog .error ("Git push command failed." )
408
412
else :
409
- update_log .error ("Git commit command failed." )
413
+ userlog .error ("Git commit command failed." )
410
414
else :
411
- update_log .error ("Git clone %s failed" , update_repo )
415
+ userlog .error ("Git clone %s failed" , update_repo )
412
416
413
417
os .chdir (cwd )
414
418
return ret
@@ -422,7 +426,7 @@ def create_work_directory(path):
422
426
423
427
"""
424
428
if os .path .exists (path ):
425
- update_log .info ("'%s' directory already exists. Deleting..." , path )
429
+ userlog .info ("'%s' directory already exists. Deleting..." , path )
426
430
rmtree_readonly (path )
427
431
428
432
os .makedirs (path )
@@ -433,28 +437,17 @@ def create_work_directory(path):
433
437
formatter_class = argparse .RawDescriptionHelpFormatter )
434
438
parser .add_argument ('-c' , '--config_file' , help = "Path to the configuration file (default is 'examples.json')" , default = 'examples.json' )
435
439
parser .add_argument ('-T' , '--github_token' , help = "GitHub token for secure access" )
436
- parser .add_argument ('-l' , '--log-level' ,
437
- help = "Level for providing logging output" ,
438
- default = 'INFO' )
439
440
440
441
exclusive = parser .add_mutually_exclusive_group (required = True )
441
442
exclusive .add_argument ('-f' , '--fork' , help = "Update a fork" , action = 'store_true' )
442
443
exclusive .add_argument ('-b' , '--branch' , help = "Update a branch" , action = 'store_true' )
443
444
444
445
args = parser .parse_args ()
445
446
446
- default = getattr (logging , 'INFO' )
447
- level = getattr (logging , args .log_level .upper (), default )
448
-
449
- # Set logging level
450
- logging .basicConfig (level = level )
451
-
452
- update_log = logging .getLogger ("Update" )
453
-
454
447
# Load the config file
455
448
with open (os .path .join (os .path .dirname (__file__ ), args .config_file )) as config :
456
449
if not config :
457
- update_log .error ("Failed to load config file '%s'" , args .config_file )
450
+ userlog .error ("Failed to load config file '%s'" , args .config_file )
458
451
sys .exit (1 )
459
452
json_data = json .load (config )
460
453
@@ -479,11 +472,11 @@ def create_work_directory(path):
479
472
exit (1 )
480
473
481
474
# Get the github sha corresponding to the specified mbed-os tag
482
- cmd = [ ' git' , ' rev-list' , '-1' , tag ]
475
+ cmd = " git rev-list -1 " + tag
483
476
return_code , ref = run_cmd_with_output (cmd )
484
477
485
478
if return_code :
486
- update_log .error ("Could not obtain SHA for tag: %s" , tag )
479
+ userlog .error ("Could not obtain SHA for tag: %s" , tag )
487
480
sys .exit (1 )
488
481
489
482
# Loop through the examples
@@ -508,11 +501,11 @@ def create_work_directory(path):
508
501
os .chdir ('../' )
509
502
510
503
# Finish the script and report the results
511
- update_log .info ("Finished updating examples" )
504
+ userlog .info ("Finished updating examples" )
512
505
if successes :
513
506
for success in successes :
514
- update_log .info (" SUCCEEDED: %s" , success )
507
+ userlog .info (" SUCCEEDED: %s" , success )
515
508
516
509
if failures :
517
510
for fail in failures :
518
- update_log .info (" FAILED: %s" , fail )
511
+ userlog .info (" FAILED: %s" , fail )
0 commit comments