|
20 | 20 | """
|
21 | 21 | from __future__ import print_function
|
22 | 22 | from builtins import str
|
| 23 | +import logging |
23 | 24 | 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' |
29 | 36 |
|
30 | 37 | # Be sure that the tools directory is in the search path
|
31 | 38 | ROOT = abspath(join(dirname(__file__), ".."))
|
32 | 39 | sys.path.insert(0, ROOT)
|
33 | 40 |
|
| 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 | + |
34 | 80 | 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) |
36 | 118 |
|
37 | 119 | if __name__ == "__main__":
|
38 | 120 | main()
|
0 commit comments