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 00035 LOG = logging.getLogger(__name__) 00036 LOG_FORMAT = '[%(levelname)s] %(asctime)s - %(name)s - %(message)s' 00037 00038 # Be sure that the tools directory is in the search path 00039 ROOT = abspath(join(dirname(__file__), "..")) 00040 sys.path.insert(0, ROOT) 00041 00042 from tools.config import Config 00043 from tools.options import extract_mcus 00044 00045 00046 class MbedExtendedArgs(MainArgumentParser): 00047 def _addCreateArgs(self, parser, exclusions=[]): 00048 if 'payload' not in exclusions: 00049 parser.add_argument( 00050 '-p', '--payload', 00051 help='Supply a local copy of the payload file.' 00052 'This option overrides any payload file supplied in a ' 00053 '`-i` argument.', 00054 metavar='FILE', 00055 type=argparse.FileType('rb') 00056 ) 00057 parser.add_argument('-m', '--mcu') 00058 parser.add_argument('-t', '--toolchain') 00059 parser.add_argument('--source', nargs='+', dest='source_dir') 00060 parser.add_argument('--build') 00061 exclusions.append('payload') 00062 super(MbedExtendedArgs, self)._addCreateArgs(parser, exclusions) 00063 00064 00065 def wrap_payload(func): 00066 def inner(options): 00067 if not options.payload and options.mcu and options.build: 00068 mcus = extract_mcus(MbedExtendedArgs(), options) 00069 sources = options.source_dir or ['.'] 00070 config = Config(mcus[0], sources) 00071 app_name = config.name or basename(abspath(sources[0])) 00072 output_ext = getattr(config.target, "OUTPUT_EXT", "bin") 00073 payload_name = join(options.build, "{}_application.{}".format( 00074 app_name, output_ext 00075 )) 00076 options.payload = open(payload_name, "rb") 00077 return func(options) 00078 return inner 00079 00080 00081 def wrap_init(func): 00082 def inner(options): 00083 if getattr(options, 'api_key', None): 00084 api_key = options.api_key 00085 else: 00086 api_key = getenv("MBED_CLOUD_SDK_API_KEY") 00087 if getattr(options, 'server_address', None): 00088 host_addr = options.server_address 00089 else: 00090 host_addr = getenv("MBED_CLOUD_SDK_HOST", 00091 "https://api.us-east-1.mbedcloud.com/") 00092 config = { 00093 "api_key": api_key, 00094 "host": host_addr, 00095 } 00096 accounts = AccountManagementAPI(config) 00097 certs = CertificatesAPI(config) 00098 api_key = accounts.list_api_keys(filter={ 00099 'key': api_key 00100 }).next() 00101 certificates_owned = list(certs.list_certificates()) 00102 dev_cert_info = None 00103 for certif in certificates_owned: 00104 if certif.type == "developer" and (certif.owner_id == api_key.owner_id or 00105 certif.owner_id == api_key.id): 00106 dev_cert_info = certs.get_certificate(certif.id) 00107 LOG.info("Found developer certificate named %s", 00108 dev_cert_info.name) 00109 break 00110 else: 00111 LOG.warning( 00112 "Could not find developer certificate for this account." 00113 " Generting a new developer certificate." 00114 ) 00115 dev_cert_info = CertificatesAPI().add_developer_certificate( 00116 "mbed-cli-auto {}".format(api_key.name), 00117 description="cetificate auto-generated by Mbed CLI" 00118 ) 00119 LOG.info("Writing developer certificate %s into c file " 00120 "mbed_cloud_dev_credentials.c", dev_cert_info.name) 00121 with open("mbed_cloud_dev_credentials.c", "w") as fout: 00122 fout.write(dev_cert_info.header_file) 00123 return func(options) 00124 return inner 00125 00126 00127 def main(): 00128 options = MbedExtendedArgs().parse_args().options 00129 00130 log_level = { 00131 'debug': logging.DEBUG, 00132 'info': logging.INFO, 00133 'warning': logging.WARNING, 00134 'exception': logging.CRITICAL, 00135 }[options.log_level] 00136 logging.basicConfig( 00137 level=log_level, 00138 format=LOG_FORMAT, 00139 datefmt='%Y-%m-%d %H:%M:%S', 00140 ) 00141 logging.addLevelName( 00142 logging.INFO, 00143 "\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO) 00144 ) 00145 logging.addLevelName( 00146 logging.WARNING, 00147 "\033[1;93m%s\033[1;0m" % logging.getLevelName(logging.WARNING) 00148 ) 00149 logging.addLevelName( 00150 logging.CRITICAL, 00151 "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL) 00152 ) 00153 LOG.debug('CLIDriver created. Arguments parsed and logging setup.') 00154 00155 rc = { 00156 "create": wrap_payload(create.main), 00157 "parse": parse.main, 00158 "verify": verify.main, 00159 "cert": cert.main, 00160 "init": wrap_init(init.main), 00161 "update": wrap_payload(update.main), 00162 }[options.action](options) or 0 00163 00164 sys.exit(rc) 00165 00166 if __name__ == "__main__": 00167 main()
Generated on Tue Jul 12 2022 17:12:47 by
