37
37
38
38
import os
39
39
from os .path import dirname , abspath , basename
40
- import sys , logging
40
+ import sys
41
+ import logging
41
42
import argparse
42
43
import json
43
44
import subprocess
53
54
from examples_lib import SUPPORTED_TOOLCHAINS
54
55
55
56
def run_cmd (command , exit_on_failure = False ):
56
- """ Passes a command to the system and returns a True/False result once the
57
- command has been executed, indicating success/failure. Commands are passed
58
- as a list of tokens.
59
- E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
57
+ """ Run a system command and return the result status
58
+
59
+ Description:
60
+
61
+ Passes a command to the system and returns a True/False result, once the
62
+ command has been executed, indicating success/failure. Commands are passed
63
+ as a list of tokens.
64
+ E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
60
65
61
66
Args:
62
67
command - system command as a list of tokens
63
68
exit_on_failure - If True exit the program on failure (default = False)
64
69
65
70
Returns:
66
- result - True/False indicating the success/failure of the command
71
+ return_code - True/False indicating the success/failure of the command
67
72
"""
68
73
update_log .debug ('[Exec] %s' , ' ' .join (command ))
69
74
return_code = subprocess .call (command , shell = True )
@@ -77,18 +82,22 @@ def run_cmd(command, exit_on_failure=False):
77
82
return return_code
78
83
79
84
def run_cmd_with_output (command , exit_on_failure = False ):
80
- """ Passes a command to the system and returns a True/False result once the
81
- command has been executed, indicating success/failure. If the command was
82
- successful then the output from the command is returned to the caller.
83
- Commands are passed as a list of tokens.
84
- E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
85
+ """ Run a system command and return the result status plus output
86
+
87
+ Description:
88
+
89
+ Passes a command to the system and returns a True/False result once the
90
+ command has been executed, indicating success/failure. If the command was
91
+ successful then the output from the command is returned to the caller.
92
+ Commands are passed as a list of tokens.
93
+ E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
85
94
86
95
Args:
87
96
command - system command as a list of tokens
88
97
exit_on_failure - If True exit the program on failure (default = False)
89
98
90
99
Returns:
91
- result - True/False indicating the success/failure of the command
100
+ returncode - True/False indicating the success/failure of the command
92
101
output - The output of the command if it was successful, else empty string
93
102
"""
94
103
update_log .debug ('[Exec] %s' , ' ' .join (command ))
@@ -117,9 +126,13 @@ def remove_readonly(func, path, _):
117
126
shutil .rmtree (directory , onerror = remove_readonly )
118
127
119
128
def find_all_examples (path ):
120
- """ Searches the path specified for sub-example folders, ie those containing an
121
- mbed-os.lib file. If found adds the path to the sub-example to a list which is
122
- then returned.
129
+ """ Search the path for examples
130
+
131
+ Description:
132
+
133
+ Searches the path specified for sub-example folders, ie those containing an
134
+ mbed-os.lib file. If found adds the path to the sub-example to a list which is
135
+ then returned.
123
136
124
137
Args:
125
138
path - path to search.
@@ -134,9 +147,13 @@ def find_all_examples(path):
134
147
return examples
135
148
136
149
def upgrade_single_example (example , tag , directory , ref ):
137
- """ Updates the mbed-os.lib file in the example specified to correspond to the
138
- version specified by the GitHub tag supplied. Also deals with
139
- multiple sub-examples in the GitHub repo, updating them in the same way.
150
+ """ Update the mbed-os version for a single example
151
+
152
+ Description:
153
+
154
+ Updates the mbed-os.lib file in the example specified to correspond to the
155
+ version specified by the GitHub tag supplied. Also deals with
156
+ multiple sub-examples in the GitHub repo, updating them in the same way.
140
157
141
158
Args:
142
159
example - json example object containing the GitHub repo to update.
@@ -190,14 +207,21 @@ def upgrade_single_example(example, tag, directory, ref):
190
207
191
208
def prepare_fork (arm_example ):
192
209
""" Synchronises a cloned fork to ensure it is up to date with the original.
210
+
211
+ Description:
212
+
213
+ This function sets a fork of an ARMmbed repo to be up to date with the
214
+ repo it was forked from. It does this by hard resetting to the ARMmbed
215
+ master branch.
193
216
194
217
Args:
195
218
arm_example - Full GitHub repo path for original example
196
219
ret - True if the fork was synchronised successfully, False otherwise
197
220
198
221
"""
199
222
200
- update_log .debug ("In: " , os .getcwd ())
223
+ logstr = "In: " + os .getcwd ()
224
+ update_log .debug (logstr )
201
225
202
226
for cmd in [['git' , 'remote' , 'add' , 'armmbed' , arm_example ],
203
227
['git' , 'fetch' , 'armmbed' ],
@@ -209,6 +233,19 @@ def prepare_fork(arm_example):
209
233
return True
210
234
211
235
def prepare_branch (branch ):
236
+ """ Set up at branch ready for use in updating examples
237
+
238
+ Description:
239
+
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.
242
+ The branch is then switched to.
243
+
244
+ Args:
245
+ arm_example - Full GitHub repo path for original example
246
+ ret - True if the fork was synchronised successfully, False otherwise
247
+
248
+ """
212
249
213
250
update_log .debug ("Preparing branch: %s" , branch )
214
251
@@ -237,28 +274,34 @@ def prepare_branch(branch):
237
274
238
275
def upgrade_example (github , example , tag , ref ,
239
276
user = 'ARMmbed' , branch = 'master' ):
240
- """ Clone a version of the example specified and upgrade all versions of
241
- mbed-os.lib found within its tree. The version cloned and how it
242
- is upgraded depends on the user and branch specified. Only two options
243
- are valid:
244
- 1) ARMmbed + non master branch
245
- This option will update the branch directly in the ARMmbed repo. If the
246
- branch does not exist it will be first created.
247
-
248
- 2) alternative user + master branch
277
+ """ Upgrade all versions of mbed-os.lib found in the specified example repo
278
+
279
+ Description:
280
+
281
+ Clone a version of the example specified and upgrade all versions of
282
+ 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
249
290
250
- This option assumes that a fork of the repo exists in the specified user's
251
- account. The fork will first be updated so that it is up to date with the
252
- upstream version , then the fork will be updated and a PR raised against
253
- the upstream ie ARMmbed repo.
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.
254
295
255
296
Args:
256
297
github - GitHub instance to allow internal git commands to be run
257
298
example - json example object containing the GitHub repo to update.
258
299
tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
259
300
ref - SHA corresponding to the tag
260
- user - GitHub user name
261
- branch - branch to update
301
+ user - GitHub user name (defaults to 'ARMmbed' if not supplied)
302
+ branch - branch to update (defaults to 'master' if not supplied)
303
+
304
+ returns True if the upgrade was successful, False otherwise
262
305
"""
263
306
264
307
ret = False
@@ -362,21 +405,7 @@ def create_work_directory(path):
362
405
363
406
os .makedirs (path )
364
407
365
- def main (arguments ):
366
- """ Will update any mbed-os.lib files found in the example list specified by the config file.
367
- If no config file is specified the default 'examples.json' is used.
368
- The update is done by cloning a fork of each example (the fork must be present in the
369
- github account specified by the github user parameter). The fork is searched for any
370
- mbed-os.lib files and each one found is updated with the SHA corresponding to the supplied
371
- github tag. A pull request is then made from the fork to the original example repo.
372
-
373
- Args:
374
- tag - tag to update the mbed-os.lib to. E.g. mbed-os-5.3.1
375
- github_token - Pre-authorised token to allow github access
376
- github_user - github username whose account contains the example forks
377
- config_file - optional parameter to specify a list of examples
378
-
379
- """
408
+ if __name__ == '__main__' :
380
409
381
410
parser = argparse .ArgumentParser (description = __doc__ ,
382
411
formatter_class = argparse .RawDescriptionHelpFormatter )
@@ -391,7 +420,7 @@ def main(arguments):
391
420
exclusive .add_argument ('-U' , '--github_user' , help = "GitHub user for forked repos, mutually exclusive to branch option" )
392
421
exclusive .add_argument ('-b' , '--branch' , help = "Branch to be updated, mutually exclusive to user option" )
393
422
394
- args = parser .parse_args (arguments )
423
+ args = parser .parse_args ()
395
424
396
425
default = getattr (logging , 'INFO' )
397
426
level = getattr (logging , args .log_level .upper (), default )
@@ -426,8 +455,8 @@ def main(arguments):
426
455
successes = []
427
456
results = {}
428
457
os .chdir ('examples' )
429
-
430
- for example in config ['examples' ]:
458
+
459
+ for example in json_data ['examples' ]:
431
460
# Determine if this example should be updated and if so update any found
432
461
# mbed-os.lib files.
433
462
@@ -453,6 +482,3 @@ def main(arguments):
453
482
if failures :
454
483
for fail in failures :
455
484
update_log .info (" FAILED: %s" , fail )
456
-
457
- if __name__ == '__main__' :
458
- sys .exit (main (sys .argv [1 :]))
0 commit comments