Skip to content

Commit 4c5f940

Browse files
Qinghao ShiQinghao Shi
authored andcommitted
EXAMPLES: update examples_lib.py
- add logging function - update clone function - update deploy function - update source function - add symlink function
1 parent 640ea73 commit 4c5f940

File tree

1 file changed

+73
-50
lines changed

1 file changed

+73
-50
lines changed

tools/test/examples/examples_lib.py

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import subprocess
2323
from shutil import rmtree
2424
import json
25+
import logging
26+
27+
logging.basicConfig(level=logging.DEBUG, format='[EXAMPLES]> %(levelname)-8s %(message)s')
2528

2629
""" Import and bulid a bunch of example programs
2730
@@ -171,7 +174,7 @@ def get_repo_list(example):
171174
return repos
172175

173176

174-
def source_repos(config, examples):
177+
def source_repos(config, exp_filter):
175178
""" Imports each of the repos and its dependencies (.lib files) associated
176179
with the specific examples name from the json config file. Note if
177180
there is already a clone of the repo then it will first be removed to
@@ -182,22 +185,20 @@ def source_repos(config, examples):
182185
"""
183186
print("\nImporting example repos....\n")
184187
for example in config['examples']:
185-
for repo_info in get_repo_list(example):
186-
name = basename(repo_info['repo'])
187-
if name in examples:
188-
if os.path.exists(name):
189-
print("'%s' example directory already exists. Deleting..." % name)
190-
rmtree(name)
191-
192-
cmd = "mbed-cli import %s" %repo_info['repo']
193-
result = subprocess.call(cmd, shell=True)
194-
195-
if result:
196-
return result
197-
188+
name = example['name']
189+
if name in exp_filter:
190+
if os.path.exists(name):
191+
logging.warning("'%s' example directory already exists. Deleting..." % name)
192+
rmtree(name)
193+
194+
cmd = "mbed-cli import %s" % example['github']
195+
logging.info("Executing command '%s'..." % cmd)
196+
result = subprocess.call(cmd, shell=True)
197+
if result:
198+
return result
198199
return 0
199200

200-
def clone_repos(config, examples , retry = 3):
201+
def clone_repos(config, exp_filter , retry = 3):
201202
""" Clones each of the repos associated with the specific examples name from the
202203
json config file. Note if there is already a clone of the repo then it will first
203204
be removed to ensure a clean, up to date cloning.
@@ -207,22 +208,23 @@ def clone_repos(config, examples , retry = 3):
207208
"""
208209
print("\nCloning example repos....\n")
209210
for example in config['examples']:
210-
for repo_info in get_repo_list(example):
211-
name = basename(repo_info['repo'])
212-
if name in examples:
213-
if os.path.exists(name):
214-
print("'%s' example directory already exists. Deleting..." % name)
215-
rmtree(name)
216-
cmd = "%s clone %s" %(repo_info['type'], repo_info['repo'])
217-
for i in range(0, retry):
218-
if not subprocess.call(cmd, shell=True):
219-
break
220-
else:
221-
print("ERROR : unable to clone the repo {}".format(name))
222-
return 1
211+
name = example['name']
212+
if name in exp_filter:
213+
if os.path.exists(name):
214+
logging.warning("'%s' example directory already exists. Deleting..." % name)
215+
rmtree(name, onerror=remove_readonly)
216+
217+
cmd = "git clone %s" % example['github']
218+
for i in range(0, retry):
219+
logging.info("Executing command '%s'..." % cmd)
220+
if not subprocess.call(cmd, shell=True):
221+
break
222+
else:
223+
logging.error("unable to clone the repo '%s'" % name)
224+
return 1
223225
return 0
224226

225-
def deploy_repos(config, examples):
227+
def deploy_repos(config, exp_filter):
226228
""" If the example directory exists as provided by the json config file,
227229
pull in the examples dependencies by using `mbed-cli deploy`.
228230
Args:
@@ -231,19 +233,21 @@ def deploy_repos(config, examples):
231233
"""
232234
print("\nDeploying example repos....\n")
233235
for example in config['examples']:
234-
for repo_info in get_repo_list(example):
235-
name = basename(repo_info['repo'].strip('/'))
236-
if name in examples:
237-
if os.path.exists(name):
238-
os.chdir(name)
239-
result = subprocess.call("mbed-cli deploy", shell=True)
240-
os.chdir("..")
241-
if result:
242-
print("mbed-cli deploy command failed for '%s'" % name)
243-
return result
244-
else:
245-
print("'%s' example directory doesn't exist. Skipping..." % name)
246-
return 1
236+
name = example['name']
237+
if name in exp_filter:
238+
if os.path.exists(name):
239+
os.chdir(name)
240+
logging.info("In folder '%s'" % name)
241+
cmd = "mbed-cli deploy"
242+
logging.info("Executing command '%s'..." % cmd)
243+
result = subprocess.call(cmd, shell=True)
244+
os.chdir(CWD)
245+
if result:
246+
logging.error("mbed-cli deploy command failed for '%s'" % name)
247+
return result
248+
else:
249+
logging.info("'%s' example directory doesn't exist. Skipping..." % name)
250+
return 1
247251
return 0
248252

249253
def get_num_failures(results, export=False):
@@ -463,7 +467,7 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples, jobs=
463467
return results
464468

465469

466-
def update_mbedos_version(config, tag, examples):
470+
def update_mbedos_version(config, tag, exp_filter):
467471
""" For each example repo identified in the config json object, update the version of
468472
mbed-os to that specified by the supplied GitHub tag. This function assumes that each
469473
example repo has already been cloned.
@@ -473,20 +477,39 @@ def update_mbedos_version(config, tag, examples):
473477
tag - GitHub tag corresponding to a version of mbed-os to upgrade to.
474478
475479
"""
476-
print("Updating mbed-os in examples to version %s\n" % tag)
480+
print("\nUpdating mbed-os in examples to version '%s'\n" % tag)
477481
for example in config['examples']:
478-
if example['name'] not in examples:
482+
if example['name'] not in exp_filter:
479483
continue
480-
for repo_info in get_repo_list(example):
481-
update_dir = basename(repo_info['repo']) + "/mbed-os"
482-
print("\nChanging dir to %s\n" % update_dir)
484+
for name in get_sub_examples_list(example):
485+
update_dir = name + "/mbed-os"
483486
os.chdir(update_dir)
487+
logging.info("In folder '%s'" % name)
484488
cmd = "mbed-cli update %s --clean" %tag
489+
logging.info("Executing command '%s'..." % cmd)
485490
result = subprocess.call(cmd, shell=True)
486-
os.chdir("../..")
491+
os.chdir(CWD)
487492
if result:
488493
return result
489-
494+
return 0
495+
496+
def symlink_mbedos(config, path, exp_filter):
497+
"""
498+
"""
499+
print("\nCreating mbed-os Symbolic link to '%s'\n" % path)
500+
for example in config['examples']:
501+
if example['name'] not in exp_filter:
502+
continue
503+
for name in get_sub_examples_list(example):
504+
os.chdir(name)
505+
logging.info("In folder '%s'" % name)
506+
if os.path.exists("mbed-os.lib"):
507+
os.remove("mbed-os.lib")
508+
else:
509+
logging.warning("No 'mbed-os.lib' found in '%s'" % name)
510+
logging.info("Creating Symbolic link '%s'->'mbed-os'" % path)
511+
os.symlink(path, "mbed-os")
512+
os.chdir(CWD)
490513
return 0
491514

492515
def fetch_output_image(output):

0 commit comments

Comments
 (0)