Skip to content

Commit 9ea605b

Browse files
committed
Update.py: Tidy up Fn headers, make logger work globally
The function headers have been updated to follow the standard format that should be being used for tools in mbed. This is a one line summary followed by a descriptive block with more detail. Updated the handling of the main function so that the logger becomes global and thus works across all the functions. This has been tested with both the fork and branch options, and for levels INFO and DEBUG.
1 parent 6850ff0 commit 9ea605b

File tree

1 file changed

+81
-55
lines changed

1 file changed

+81
-55
lines changed

tools/test/examples/update.py

Lines changed: 81 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
import os
3939
from os.path import dirname, abspath, basename
40-
import sys, logging
40+
import sys
41+
import logging
4142
import argparse
4243
import json
4344
import subprocess
@@ -53,17 +54,21 @@
5354
from examples_lib import SUPPORTED_TOOLCHAINS
5455

5556
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']
6065
6166
Args:
6267
command - system command as a list of tokens
6368
exit_on_failure - If True exit the program on failure (default = False)
6469
6570
Returns:
66-
result - True/False indicating the success/failure of the command
71+
return_code - True/False indicating the success/failure of the command
6772
"""
6873
update_log.debug('[Exec] %s', ' '.join(command))
6974
return_code = subprocess.call(command, shell=True)
@@ -77,18 +82,22 @@ def run_cmd(command, exit_on_failure=False):
7782
return return_code
7883

7984
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']
8594
8695
Args:
8796
command - system command as a list of tokens
8897
exit_on_failure - If True exit the program on failure (default = False)
8998
9099
Returns:
91-
result - True/False indicating the success/failure of the command
100+
returncode - True/False indicating the success/failure of the command
92101
output - The output of the command if it was successful, else empty string
93102
"""
94103
update_log.debug('[Exec] %s', ' '.join(command))
@@ -117,9 +126,13 @@ def remove_readonly(func, path, _):
117126
shutil.rmtree(directory, onerror=remove_readonly)
118127

119128
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.
123136
124137
Args:
125138
path - path to search.
@@ -134,9 +147,13 @@ def find_all_examples(path):
134147
return examples
135148

136149
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.
140157
141158
Args:
142159
example - json example object containing the GitHub repo to update.
@@ -190,14 +207,21 @@ def upgrade_single_example(example, tag, directory, ref):
190207

191208
def prepare_fork(arm_example):
192209
""" 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.
193216
194217
Args:
195218
arm_example - Full GitHub repo path for original example
196219
ret - True if the fork was synchronised successfully, False otherwise
197220
198221
"""
199222

200-
update_log.debug("In: ", os.getcwd())
223+
logstr = "In: " + os.getcwd()
224+
update_log.debug(logstr)
201225

202226
for cmd in [['git', 'remote', 'add', 'armmbed', arm_example],
203227
['git', 'fetch', 'armmbed'],
@@ -209,6 +233,19 @@ def prepare_fork(arm_example):
209233
return True
210234

211235
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+
"""
212249

213250
update_log.debug("Preparing branch: %s", branch)
214251

@@ -237,28 +274,34 @@ def prepare_branch(branch):
237274

238275
def upgrade_example(github, example, tag, ref,
239276
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
249290
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.
254295
255296
Args:
256297
github - GitHub instance to allow internal git commands to be run
257298
example - json example object containing the GitHub repo to update.
258299
tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
259300
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
262305
"""
263306

264307
ret = False
@@ -362,21 +405,7 @@ def create_work_directory(path):
362405

363406
os.makedirs(path)
364407

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__':
380409

381410
parser = argparse.ArgumentParser(description=__doc__,
382411
formatter_class=argparse.RawDescriptionHelpFormatter)
@@ -391,7 +420,7 @@ def main(arguments):
391420
exclusive.add_argument('-U', '--github_user', help="GitHub user for forked repos, mutually exclusive to branch option")
392421
exclusive.add_argument('-b', '--branch', help="Branch to be updated, mutually exclusive to user option")
393422

394-
args = parser.parse_args(arguments)
423+
args = parser.parse_args()
395424

396425
default = getattr(logging, 'INFO')
397426
level = getattr(logging, args.log_level.upper(), default)
@@ -426,8 +455,8 @@ def main(arguments):
426455
successes = []
427456
results = {}
428457
os.chdir('examples')
429-
430-
for example in config['examples']:
458+
459+
for example in json_data['examples']:
431460
# Determine if this example should be updated and if so update any found
432461
# mbed-os.lib files.
433462

@@ -453,6 +482,3 @@ def main(arguments):
453482
if failures:
454483
for fail in failures:
455484
update_log.info(" FAILED: %s", fail)
456-
457-
if __name__ == '__main__':
458-
sys.exit(main(sys.argv[1:]))

0 commit comments

Comments
 (0)