Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
device_management.py
00001 #! /usr/bin/env python2 00002 """ 00003 mbed SDK 00004 Copyright (c) 2011-2013 ARM Limited 00005 00006 Licensed under the Apache License, Version 2.0 (the "License"); 00007 you may not use this file except in compliance with the License. 00008 You may obtain a copy of the License at 00009 00010 http://www.apache.org/licenses/LICENSE-2.0 00011 00012 Unless required by applicable law or agreed to in writing, software 00013 distributed under the License is distributed on an "AS IS" BASIS, 00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 See the License for the specific language governing permissions and 00016 limitations under the License. 00017 00018 00019 device-management, dev-mgmt, and dm sub command 00020 """ 00021 from __future__ import print_function, absolute_import 00022 import logging 00023 import sys 00024 import argparse 00025 from os.path import join, abspath, dirname, basename 00026 from os import getenv 00027 00028 from manifesttool import create, parse, verify, cert, init, update 00029 from manifesttool.argparser import MainArgumentParser 00030 from mbed_cloud import AccountManagementAPI, CertificatesAPI 00031 import colorama 00032 colorama.init() 00033 00034 from utils import (generate_update_filename) 00035 00036 00037 LOG = logging.getLogger(__name__) 00038 LOG_FORMAT = '[%(levelname)s] %(asctime)s - %(name)s - %(message)s' 00039 00040 # Be sure that the tools directory is in the search path 00041 ROOT = abspath(join(dirname(__file__), "..")) 00042 sys.path.insert(0, ROOT) 00043 00044 from tools.config import Config 00045 from tools.options import extract_mcus 00046 00047 00048 class MbedExtendedArgs(MainArgumentParser): 00049 00050 def __init__(self, *args, **kwargs): 00051 MainArgumentParser.__init__(self, *args, **kwargs) 00052 self.parser.prog = "mbed device-management" 00053 self.parser.description = ( 00054 "Create or transform a manifest. " 00055 "Use {} [command] -h for help on each command." 00056 ).format(self.parser.prog) 00057 00058 def _addCreateArgs(self, parser, exclusions=[]): 00059 if 'payload' not in exclusions: 00060 parser.add_argument( 00061 '-p', '--payload', 00062 help='Supply a local copy of the payload file.' 00063 'This option overrides any payload file supplied in a ' 00064 '`-i` argument.', 00065 metavar='FILE', 00066 type=argparse.FileType('rb') 00067 ) 00068 parser.add_argument('-m', '--mcu') 00069 parser.add_argument('-t', '--toolchain') 00070 parser.add_argument('--source', nargs='+', dest='source_dir') 00071 parser.add_argument('--build') 00072 exclusions.append('payload') 00073 super(MbedExtendedArgs, self)._addCreateArgs(parser, exclusions) 00074 00075 00076 def wrap_payload(func): 00077 def inner(options): 00078 if not options.payload and options.mcu and options.build: 00079 mcus = extract_mcus(MbedExtendedArgs(), options) 00080 sources = options.source_dir or ['.'] 00081 config = Config(mcus[0], sources) 00082 app_name = config.name or basename(abspath(sources[0])) 00083 payload_name = join(options.build, generate_update_filename(app_name, config.target)) 00084 options.payload = open(payload_name, "rb") 00085 return func(options) 00086 return inner 00087 00088 00089 def wrap_init(func): 00090 def inner(options): 00091 if getattr(options, 'api_key', None): 00092 api_key = options.api_key 00093 else: 00094 api_key = getenv("MBED_CLOUD_SDK_API_KEY") 00095 if getattr(options, 'server_address', None): 00096 host_addr = options.server_address 00097 else: 00098 host_addr = getenv("MBED_CLOUD_SDK_HOST", 00099 "https://api.us-east-1.mbedcloud.com/") 00100 config = { 00101 "api_key": api_key, 00102 "host": host_addr, 00103 } 00104 accounts = AccountManagementAPI(config) 00105 certs = CertificatesAPI(config) 00106 api_key = accounts.list_api_keys(filter={ 00107 'key': api_key 00108 }).next() 00109 certificates_owned = list(certs.list_certificates()) 00110 dev_cert_info = None 00111 for certif in certificates_owned: 00112 if certif.type == "developer" and (certif.owner_id == api_key.owner_id or 00113 certif.owner_id == api_key.id): 00114 dev_cert_info = certs.get_certificate(certif.id) 00115 LOG.info("Found developer certificate named %s", 00116 dev_cert_info.name) 00117 break 00118 else: 00119 LOG.warning( 00120 "Could not find developer certificate for this account." 00121 " Generting a new developer certificate." 00122 ) 00123 dev_cert_info = CertificatesAPI().add_developer_certificate( 00124 "mbed-cli-auto {}".format(api_key.name), 00125 description="cetificate auto-generated by Mbed CLI" 00126 ) 00127 LOG.info("Writing developer certificate %s into c file " 00128 "mbed_cloud_dev_credentials.c", dev_cert_info.name) 00129 with open("mbed_cloud_dev_credentials.c", "w") as fout: 00130 fout.write(dev_cert_info.header_file) 00131 return func(options) 00132 return inner 00133 00134 00135 def main(): 00136 options = MbedExtendedArgs().parse_args().options 00137 00138 log_level = { 00139 'debug': logging.DEBUG, 00140 'info': logging.INFO, 00141 'warning': logging.WARNING, 00142 'exception': logging.CRITICAL, 00143 }[options.log_level] 00144 logging.basicConfig( 00145 level=log_level, 00146 format=LOG_FORMAT, 00147 datefmt='%Y-%m-%d %H:%M:%S', 00148 ) 00149 logging.addLevelName( 00150 logging.INFO, 00151 "\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO) 00152 ) 00153 logging.addLevelName( 00154 logging.WARNING, 00155 "\033[1;93m%s\033[1;0m" % logging.getLevelName(logging.WARNING) 00156 ) 00157 logging.addLevelName( 00158 logging.CRITICAL, 00159 "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL) 00160 ) 00161 LOG.debug('CLIDriver created. Arguments parsed and logging setup.') 00162 00163 rc = { 00164 "create": wrap_payload(create.main), 00165 "parse": parse.main, 00166 "verify": verify.main, 00167 "cert": cert.main, 00168 "init": wrap_init(init.main), 00169 "update": wrap_payload(update.main), 00170 }[options.action](options) or 0 00171 00172 sys.exit(rc) 00173 00174 if __name__ == "__main__": 00175 main()
Generated on Tue Aug 9 2022 00:37:05 by
1.7.2