Skip to content

Commit 70b57ea

Browse files
committed
Add option --mbedlib to new to allow creation of programs based on mbed library
1 parent 2080ec1 commit 70b57ea

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

mbed/mbed.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
# default mbed OS url
9191
mbed_os_url = 'https://github.com/ARMmbed/mbed-os'
9292

93+
# default mbed library url
94+
mbed_lib_url = 'https://mbed.org/users/mbed_official/code/mbed/builds/'
95+
9396
# mbed SDK tools needed for programs based on mbed SDK library
9497
mbed_sdk_tools_url = 'https://mbed.org/users/mbed_official/code/mbed-sdk-tools'
9598

@@ -243,7 +246,7 @@ def clone(url, path=None, depth=None, protocol=None):
243246
except Exception as e:
244247
error(e[1], e[0])
245248

246-
def fetch(url, rev):
249+
def fetch_rev(url, rev):
247250
tmp_file = '.rev-' + rev + '.zip'
248251
arch_dir = 'mbed-' + rev
249252
try:
@@ -271,22 +274,6 @@ def checkout(rev):
271274
m = Bld.isurl(url)
272275
if not m:
273276
raise ProcessException(1, "Not an mbed library build URL")
274-
275-
log("Checkout \"%s\" in %s" % (rev, os.path.basename(os.getcwd())))
276-
arch_url = m.group(1) + '/archive/' + rev + '.zip'
277-
arch_dir = m.group(7) + '-' + rev
278-
try:
279-
if not os.path.exists(arch_dir):
280-
Bld.fetch(arch_url, rev)
281-
except Exception as e:
282-
if os.path.exists(arch_dir):
283-
rmtree_readonly(arch_dir)
284-
error(e[1], e[0])
285-
Bld.seturl(url+'/'+rev)
286-
287-
def update(rev=None, clean=False, is_local=False):
288-
url = Bld.geturl()
289-
m = Bld.isurl(url)
290277
rev = Hg.remoteid(m.group(1), rev)
291278
if not rev:
292279
error("Unable to fetch late mbed library revision")
@@ -299,7 +286,21 @@ def update(rev=None, clean=False, is_local=False):
299286
os.remove(fl)
300287
else:
301288
shutil.rmtree(fl)
302-
return Bld.checkout(rev)
289+
290+
log("Checkout \"%s\" in %s" % (rev, os.path.basename(os.getcwd())))
291+
arch_url = m.group(1) + '/archive/' + rev + '.zip'
292+
arch_dir = m.group(7) + '-' + rev
293+
try:
294+
if not os.path.exists(arch_dir):
295+
Bld.fetch_rev(arch_url, rev)
296+
except Exception as e:
297+
if os.path.exists(arch_dir):
298+
rmtree_readonly(arch_dir)
299+
error(e[1], e[0])
300+
Bld.seturl(url+'/'+rev)
301+
302+
def update(rev=None, clean=False, is_local=False):
303+
return Bld.checkout(rev)
303304

304305
def untracked():
305306
return ""
@@ -794,7 +795,7 @@ def fromrepo(cls, path=None):
794795
path = os.getcwd()
795796
warning(
796797
"Could not find mbed program in current path. Assuming current dir.\n"
797-
"You can fix this by calling \"mbed new .\" in the root of your program")
798+
"You can fix this by calling \"mbed new .\" or \"mbed default root .\" in the root of your program.")
798799

799800
repo.path = os.path.abspath(path)
800801
repo.name = os.path.basename(repo.path)
@@ -1066,7 +1067,7 @@ def __init__(self, path=None, print_warning=False):
10661067
if self.is_cwd and print_warning:
10671068
warning(
10681069
"Could not find mbed program in current path. Assuming current dir.\n"
1069-
"You can fix this by calling \"mbed new .\" in the root of your program")
1070+
"You can fix this by calling \"mbed new .\" or \"mbed default root .\" in the root of your program.")
10701071

10711072
# Sets config value
10721073
def set_cfg(self, var, val):
@@ -1114,6 +1115,12 @@ def get_os_dir(self):
11141115
else:
11151116
return None
11161117

1118+
def get_mbedlib_dir(self):
1119+
if os.path.isdir(os.path.join(self.path, 'mbed')):
1120+
return os.path.join(self.path, 'mbed')
1121+
else:
1122+
return None
1123+
11171124
# Gets mbed tools dir (unified)
11181125
def get_tools_dir(self):
11191126
mbed_os_path = self.get_os_dir()
@@ -1259,10 +1266,11 @@ def thunk(parsed_args):
12591266
@subcommand('new',
12601267
dict(name='name', help='Destination name or path'),
12611268
dict(name='scm', nargs='?', help='Source control management. Currently supported: %s. Default: git' % ', '.join([s.name for s in scms.values()])),
1269+
dict(name='--mbedlib', action='store_true', help='Add the mbed library to the program (instead of mbed-os).'),
12621270
dict(name='--depth', nargs='?', help='Number of revisions to fetch the mbed OS repository when creating new program. Default: all revisions.'),
12631271
dict(name='--protocol', nargs='?', help='Transport protocol when fetching the mbed OS repository when creating new program. Supported: https, http, ssh, git. Default: inferred from URL.'),
12641272
help='Create a new program based on the specified source control management. Will create a new library when called from inside a local program. Supported SCMs: %s.' % (', '.join([s.name for s in scms.values()])))
1265-
def new(name, scm='git', depth=None, protocol=None):
1273+
def new(name, scm='git', mbedlib=False, depth=None, protocol=None):
12661274
global cwd_root
12671275

12681276
d_path = name or os.getcwd()
@@ -1292,14 +1300,16 @@ def new(name, scm='git', depth=None, protocol=None):
12921300

12931301
program = Program(d_path)
12941302
program.set_root()
1295-
if not program.get_os_dir():
1303+
if not program.get_os_dir() and not program.get_mbedlib_dir():
1304+
url = mbed_lib_url if mbedlib else mbed_os_url
12961305
try:
12971306
with cd(d_path):
1298-
add(mbed_os_url, depth=depth, protocol=protocol, top=False)
1307+
add(url, depth=depth, protocol=protocol, top=False)
12991308
except Exception as e:
1300-
if os.path.isdir(os.path.join(d_path, 'mbed-os')):
1301-
rmtree_readonly(os.path.join(d_path, 'mbed-os'))
1302-
raise e
1309+
d = 'mbed' if mbedlib else 'mbed-os'
1310+
if os.path.isdir(os.path.join(d_path, d)):
1311+
rmtree_readonly(os.path.join(d_path, d))
1312+
raise
13031313
if d_path:
13041314
os.chdir(d_path)
13051315

0 commit comments

Comments
 (0)