Skip to content

Commit ad83e15

Browse files
committed
Support use of mbed CLI as python module
1 parent 1297332 commit ad83e15

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

mbed/mbed.py

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,11 +1386,6 @@ def formaturl(url, format="default"):
13861386
return url
13871387

13881388

1389-
# Help messages adapt based on current dir
1390-
cwd_root = os.getcwd()
1391-
cwd_type = Repo.pathtype(cwd_root)
1392-
cwd_dest = "program" if cwd_type == "directory" else "library"
1393-
13941389
# Subparser handling
13951390
parser = argparse.ArgumentParser(prog='mbed',
13961391
description="Command-line code management tool for ARM mbed OS - http://www.mbed.com\nversion %s\n\nUse 'mbed <command> -h|--help' for detailed help.\nOnline manual and guide available at https://github.com/ARMmbed/mbed-cli" % ver,
@@ -1522,8 +1517,8 @@ def new(name, scm='git', program=False, library=False, mbedlib=False, create_onl
15221517

15231518
# Import command
15241519
@subcommand('import',
1525-
dict(name='url', help='URL of the %s' % cwd_dest),
1526-
dict(name='path', nargs='?', help='Destination name or path. Default: current %s.' % cwd_type),
1520+
dict(name='url', help='URL of the program'),
1521+
dict(name='path', nargs='?', help='Destination name or path. Default: current directory.'),
15271522
dict(name=['-I', '--ignore'], action='store_true', help='Ignore errors related to cloning and updating.'),
15281523
dict(name='--depth', nargs='?', help='Number of revisions to fetch from the remote repository. Default: all revisions.'),
15291524
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
@@ -1664,11 +1659,11 @@ def deploy(ignore=False, depth=None, protocol=None, top=True):
16641659
dict(name=['-M', '--message'], dest='msg', type=str, nargs='?', help='Commit message. Default: prompts for commit message.'),
16651660
help='Publish program or library',
16661661
description=(
1667-
"Publishes this %s and all dependencies to their associated remote\nrepository URLs.\n"
1662+
"Publishes the current program or library and all dependencies to their\nassociated remote repository URLs.\n"
16681663
"This command performs various consistency checks for local uncommitted changes\n"
16691664
"and unpublished revisions and encourages to commit/push them.\n"
16701665
"Online guide about collaboration is available at:\n"
1671-
"www.mbed.com/collab_guide" % cwd_type))
1666+
"www.mbed.com/collab_guide"))
16721667
def publish(all_refs=None, msg=None, top=True):
16731668
if top:
16741669
action("Checking for local modifications...")
@@ -1716,13 +1711,16 @@ def publish(all_refs=None, msg=None, top=True):
17161711
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
17171712
help='Update to branch, tag, revision or latest',
17181713
description=(
1719-
"Updates this %s and its dependencies to specified branch, tag or revision.\n"
1714+
"Updates the current program or library and its dependencies to specified\nbranch, tag or revision.\n"
17201715
"Alternatively fetches from associated remote repository URL and updates to the\n"
1721-
"latest revision in the current branch." % cwd_type))
1716+
"latest revision in the current branch."))
17221717
def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=False, top=True, depth=None, protocol=None):
17231718
if top and clean:
17241719
sync()
17251720

1721+
cwd_type = Repo.pathtype(cwd_root)
1722+
cwd_dest = "program" if cwd_type == "directory" else "library"
1723+
17261724
repo = Repo.fromrepo()
17271725
# A copy of repo containing the .lib layout before updating
17281726
repo_orig = Repo.fromrepo()
@@ -1818,8 +1816,8 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
18181816
help='Synchronize library references',
18191817
description=(
18201818
"Synchronizes all library and dependency references (.lib files) in the\n"
1821-
"current %s.\n"
1822-
"Note that this will remove all invalid library references." % cwd_type))
1819+
"current program or library.\n"
1820+
"Note that this will remove all invalid library references."))
18231821
def sync(recursive=True, keep_refs=False, top=True):
18241822
if top and recursive:
18251823
action("Synchronizing dependency references...")
@@ -1868,6 +1866,7 @@ def sync(recursive=True, keep_refs=False, top=True):
18681866
sync(keep_refs=keep_refs, top=False)
18691867

18701868
# Update the .lib reference in the parent repository
1869+
cwd_type = Repo.pathtype(cwd_root)
18711870
if top and cwd_type == "library":
18721871
repo = Repo.fromrepo()
18731872
repo.write()
@@ -1879,7 +1878,7 @@ def sync(recursive=True, keep_refs=False, top=True):
18791878
dict(name=['-I', '--ignore'], action='store_true', help='Ignore errors related to missing libraries.'),
18801879
help='View dependency tree',
18811880
description=(
1882-
"View the dependency tree in this %s." % cwd_type))
1881+
"View the dependency tree of the current program or library."))
18831882
def list_(detailed=False, prefix='', p_path=None, ignore=False):
18841883
repo = Repo.fromrepo()
18851884
print prefix + (relpath(p_path, repo.path) if p_path else repo.name), '(%s)' % ((repo.url+('#'+str(repo.rev)[:12] if repo.rev else '') if detailed else str(repo.rev)[:12]) or 'no revision')
@@ -1901,7 +1900,7 @@ def list_(detailed=False, prefix='', p_path=None, ignore=False):
19011900
dict(name=['-I', '--ignore'], action='store_true', help='Ignore errors related to missing libraries.'),
19021901
help='Show version control status\n\n',
19031902
description=(
1904-
"Show uncommitted changes in this %s and its dependencies." % cwd_type))
1903+
"Show uncommitted changes a program or library and its dependencies."))
19051904
def status_(ignore=False):
19061905
repo = Repo.fromrepo()
19071906
if repo.dirty():
@@ -1918,7 +1917,7 @@ def status_(ignore=False):
19181917
@subcommand('compile',
19191918
dict(name=['-t', '--toolchain'], help='Compile toolchain. Example: ARM, uARM, GCC_ARM, IAR'),
19201919
dict(name=['-m', '--mcu'], help='Compile target. Example: K64F, NUCLEO_F401RE, NRF51822...'),
1921-
dict(name='--library', dest='compile_library', action='store_true', help='Compile the current %s as a static library.' % cwd_type),
1920+
dict(name='--library', dest='compile_library', action='store_true', help='Compile the current program or library as a static library.'),
19221921
dict(name='--config', dest='compile_config', action='store_true', help='Show run-time compile configuration'),
19231922
dict(name='--prefix', dest='config_prefix', action='append', help='Restrict listing to parameters that have this prefix'),
19241923
dict(name='--source', action='append', help='Source directory. Default: . (current dir)'),
@@ -2229,39 +2228,44 @@ def toolchain_(name=None, global_cfg=False, supported=False):
22292228
def help_():
22302229
return parser.print_help()
22312230

2232-
# Parse/run command
2233-
if len(sys.argv) <= 1:
2234-
help_()
2235-
sys.exit(1)
2236-
2237-
if '--version' in sys.argv:
2238-
print ver
2239-
sys.exit(0)
2240-
2241-
pargs, remainder = parser.parse_known_args()
2242-
status = 1
2243-
2244-
try:
2245-
very_verbose = pargs.very_verbose
2246-
verbose = very_verbose or pargs.verbose
2247-
log('Working path \"%s\" (%s)' % (os.getcwd(), cwd_type))
2248-
status = pargs.command(pargs)
2249-
except ProcessException as e:
2250-
error(
2251-
"\"%s\" returned error code %d.\n"
2252-
"Command \"%s\" in \"%s\"" % (e[1], e[0], e[2], e[3]), e[0])
2253-
except OSError as e:
2254-
if e[0] == errno.ENOENT:
2231+
# Help messages adapt based on current dir
2232+
cwd_root = os.getcwd()
2233+
2234+
2235+
if __name__ == "__main__":
2236+
# Parse/run command
2237+
if len(sys.argv) <= 1:
2238+
help_()
2239+
sys.exit(1)
2240+
2241+
if '--version' in sys.argv:
2242+
print ver
2243+
sys.exit(0)
2244+
2245+
pargs, remainder = parser.parse_known_args()
2246+
status = 1
2247+
2248+
try:
2249+
very_verbose = pargs.very_verbose
2250+
verbose = very_verbose or pargs.verbose
2251+
log('Working path \"%s\" (%s)' % (os.getcwd(), Repo.pathtype(cwd_root)))
2252+
status = pargs.command(pargs)
2253+
except ProcessException as e:
22552254
error(
2256-
"Could not detect one of the command-line tools.\n"
2257-
"You could retry the last command with \"-v\" flag for verbose output\n", e[0])
2258-
else:
2259-
error('OS Error: %s' % e[1], e[0])
2260-
except KeyboardInterrupt as e:
2261-
log('User aborted!', -1)
2262-
sys.exit(255)
2263-
except Exception as e:
2264-
if very_verbose:
2265-
traceback.print_exc(file=sys.stdout)
2266-
error("Unknown Error: %s" % e, 255)
2267-
sys.exit(status or 0)
2255+
"\"%s\" returned error code %d.\n"
2256+
"Command \"%s\" in \"%s\"" % (e[1], e[0], e[2], e[3]), e[0])
2257+
except OSError as e:
2258+
if e[0] == errno.ENOENT:
2259+
error(
2260+
"Could not detect one of the command-line tools.\n"
2261+
"You could retry the last command with \"-v\" flag for verbose output\n", e[0])
2262+
else:
2263+
error('OS Error: %s' % e[1], e[0])
2264+
except KeyboardInterrupt as e:
2265+
log('User aborted!', -1)
2266+
sys.exit(255)
2267+
except Exception as e:
2268+
if very_verbose:
2269+
traceback.print_exc(file=sys.stdout)
2270+
error("Unknown Error: %s" % e, 255)
2271+
sys.exit(status or 0)

0 commit comments

Comments
 (0)