Skip to content

Commit 563ee0d

Browse files
Jimmy Brissontheotherjimmy
authored andcommitted
Implement device management subcommand
1 parent ebb016e commit 563ee0d

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ jsonschema>=2.6
1616
future>=0.16.0
1717
six>=1.11.0
1818
git+https://github.com/armmbed/[email protected]
19+
mbed-cloud-sdk==2.0.0

tools/device_management.py

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,101 @@
2020
"""
2121
from __future__ import print_function
2222
from builtins import str
23+
import logging
2324
import sys
24-
import json
25-
from time import sleep
26-
from shutil import copy
27-
from os.path import join, abspath, dirname
28-
from json import load, dump
25+
import argparse
26+
from os.path import join, abspath, dirname, basename
27+
28+
from manifesttool import create, parse, verify, cert, init, update
29+
from manifesttool.argparser import MainArgumentParser
30+
import colorama
31+
colorama.init()
32+
33+
34+
LOG = logging.getLogger(__name__)
35+
LOG_FORMAT='[%(levelname)s] %(asctime)s - %(name)s - %(message)s'
2936

3037
# Be sure that the tools directory is in the search path
3138
ROOT = abspath(join(dirname(__file__), ".."))
3239
sys.path.insert(0, ROOT)
3340

41+
from tools.config import Config
42+
from tools.options import extract_mcus
43+
44+
class MbedExtendedArgs(MainArgumentParser):
45+
def _addCreateArgs(self, parser, exclusions=[]):
46+
if 'payload' not in exclusions:
47+
parser.add_argument('-p', '--payload',
48+
help='Supply a local copy of the payload file.'
49+
'This option overrides any payload file supplied in a `-i` argument.',
50+
metavar='FILE',
51+
type=argparse.FileType('rb')
52+
)
53+
parser.add_argument('-m', '--mcu')
54+
parser.add_argument('-t', '--toolchain')
55+
parser.add_argument('--source', nargs='+', dest='source_dir')
56+
parser.add_argument('--build')
57+
exclusions.append('payload')
58+
super(MbedExtendedArgs, self)._addCreateArgs(parser, exclusions)
59+
60+
61+
def wrap_payload(func):
62+
def inner(options):
63+
if (not options.payload and
64+
options.mcu and
65+
options.build
66+
):
67+
mcus = extract_mcus(MbedExtendedArgs(), options)
68+
sources = options.source_dir or ['.']
69+
config = Config(mcus[0], sources)
70+
app_name = config.name or basename(abspath(sources[0]))
71+
output_ext = getattr(config.target, "OUTPUT_EXT", "bin")
72+
payload_name = join(options.build, "{}_application.{}".format(
73+
app_name, output_ext
74+
))
75+
options.payload = open(payload_name, "rb")
76+
return func(options)
77+
return inner
78+
79+
3480
def main():
35-
print("device management!")
81+
options = MbedExtendedArgs().parse_args().options
82+
83+
log_level = {
84+
'debug': logging.DEBUG,
85+
'info': logging.INFO,
86+
'warning': logging.WARNING,
87+
'exception': logging.CRITICAL,
88+
}[options.log_level]
89+
logging.basicConfig(
90+
level=log_level,
91+
format=LOG_FORMAT,
92+
datefmt='%Y-%m-%d %H:%M:%S',
93+
)
94+
logging.addLevelName(
95+
logging.INFO,
96+
"\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO)
97+
)
98+
logging.addLevelName(
99+
logging.WARNING,
100+
"\033[1;93m%s\033[1;0m" % logging.getLevelName(logging.WARNING)
101+
)
102+
logging.addLevelName(
103+
logging.CRITICAL,
104+
"\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL)
105+
)
106+
LOG.debug('CLIDriver created. Arguments parsed and logging setup.')
107+
108+
rc = {
109+
"create": wrap_payload(create.main),
110+
"parse": parse.main,
111+
"verify": verify.main,
112+
"cert": cert.main,
113+
"init": init.main,
114+
"update" : wrap_payload(update.main),
115+
}[options.action](options) or 0
116+
117+
sys.exit(rc)
36118

37119
if __name__ == "__main__":
38120
main()

0 commit comments

Comments
 (0)