mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

UserRevisionLine numberNew contents of line
be_bryan 0:b74591d5ab33 1 import argparse
be_bryan 0:b74591d5ab33 2 from os.path import basename
be_bryan 0:b74591d5ab33 3 from tools.arm_pack_manager import Cache
be_bryan 0:b74591d5ab33 4 from os.path import basename, join, dirname, exists
be_bryan 0:b74591d5ab33 5 from os import makedirs
be_bryan 0:b74591d5ab33 6 from itertools import takewhile
be_bryan 0:b74591d5ab33 7 from fuzzywuzzy import process
be_bryan 0:b74591d5ab33 8 from tools.arm_pack_manager import Cache
be_bryan 0:b74591d5ab33 9
be_bryan 0:b74591d5ab33 10 parser = argparse.ArgumentParser(description='A Handy little utility for keeping your cache of pack files up to date.')
be_bryan 0:b74591d5ab33 11 subparsers = parser.add_subparsers(title="Commands")
be_bryan 0:b74591d5ab33 12
be_bryan 0:b74591d5ab33 13 def subcommand(name, *args, **kwargs):
be_bryan 0:b74591d5ab33 14 def subcommand(command):
be_bryan 0:b74591d5ab33 15 subparser = subparsers.add_parser(name, **kwargs)
be_bryan 0:b74591d5ab33 16
be_bryan 0:b74591d5ab33 17 for arg in args:
be_bryan 0:b74591d5ab33 18 arg = dict(arg)
be_bryan 0:b74591d5ab33 19 opt = arg['name']
be_bryan 0:b74591d5ab33 20 del arg['name']
be_bryan 0:b74591d5ab33 21
be_bryan 0:b74591d5ab33 22 if isinstance(opt, basestring):
be_bryan 0:b74591d5ab33 23 subparser.add_argument(opt, **arg)
be_bryan 0:b74591d5ab33 24 else:
be_bryan 0:b74591d5ab33 25 subparser.add_argument(*opt, **arg)
be_bryan 0:b74591d5ab33 26
be_bryan 0:b74591d5ab33 27 subparser.add_argument("-v", "--verbose", action="store_true", dest="verbose", help="Verbose diagnostic output")
be_bryan 0:b74591d5ab33 28 subparser.add_argument("-vv", "--very_verbose", action="store_true", dest="very_verbose", help="Very verbose diagnostic output")
be_bryan 0:b74591d5ab33 29 subparser.add_argument("--no-timeouts", action="store_true", help="Remove all timeouts and try to download unconditionally")
be_bryan 0:b74591d5ab33 30 subparser.add_argument("--and", action="store_true", dest="intersection", help="combine search terms as if with an and")
be_bryan 0:b74591d5ab33 31 subparser.add_argument("--or", action="store_false", dest="intersection", help="combine search terms as if with an or")
be_bryan 0:b74591d5ab33 32 subparser.add_argument("--union", action="store_false", dest="intersection", help="combine search terms as if with a set union")
be_bryan 0:b74591d5ab33 33 subparser.add_argument("--intersection", action="store_true", dest="intersection", help="combine search terms as if with a set intersection")
be_bryan 0:b74591d5ab33 34
be_bryan 0:b74591d5ab33 35 def thunk(parsed_args):
be_bryan 0:b74591d5ab33 36 cache = Cache(not parsed_args.verbose, parsed_args.no_timeouts)
be_bryan 0:b74591d5ab33 37 argv = [arg['dest'] if 'dest' in arg else arg['name'] for arg in args]
be_bryan 0:b74591d5ab33 38 argv = [(arg if isinstance(arg, basestring) else arg[-1]).strip('-')
be_bryan 0:b74591d5ab33 39 for arg in argv]
be_bryan 0:b74591d5ab33 40 argv = {arg: vars(parsed_args)[arg] for arg in argv
be_bryan 0:b74591d5ab33 41 if vars(parsed_args)[arg] is not None}
be_bryan 0:b74591d5ab33 42
be_bryan 0:b74591d5ab33 43 return command(cache, **argv)
be_bryan 0:b74591d5ab33 44
be_bryan 0:b74591d5ab33 45 subparser.set_defaults(command=thunk)
be_bryan 0:b74591d5ab33 46 return command
be_bryan 0:b74591d5ab33 47 return subcommand
be_bryan 0:b74591d5ab33 48
be_bryan 0:b74591d5ab33 49 def user_selection (message, options) :
be_bryan 0:b74591d5ab33 50 print(message)
be_bryan 0:b74591d5ab33 51 for choice, index in zip(options, range(len(options))) :
be_bryan 0:b74591d5ab33 52 print("({}) {}".format(index, choice))
be_bryan 0:b74591d5ab33 53 pick = None
be_bryan 0:b74591d5ab33 54 while pick is None :
be_bryan 0:b74591d5ab33 55 stdout.write("please select an integer from 0 to {} or \"all\"".format(len(options)-1))
be_bryan 0:b74591d5ab33 56 input = raw_input()
be_bryan 0:b74591d5ab33 57 try :
be_bryan 0:b74591d5ab33 58 if input == "all" :
be_bryan 0:b74591d5ab33 59 pick = options
be_bryan 0:b74591d5ab33 60 else :
be_bryan 0:b74591d5ab33 61 pick = [options[int(input)]]
be_bryan 0:b74591d5ab33 62 except ValueError :
be_bryan 0:b74591d5ab33 63 print("I did not understand your input")
be_bryan 0:b74591d5ab33 64 return pick
be_bryan 0:b74591d5ab33 65
be_bryan 0:b74591d5ab33 66 def fuzzy_find(matches, urls) :
be_bryan 0:b74591d5ab33 67 choices = {}
be_bryan 0:b74591d5ab33 68 for match in matches :
be_bryan 0:b74591d5ab33 69 for key, value in process.extract(match, urls, limit=None) :
be_bryan 0:b74591d5ab33 70 choices.setdefault(key, 0)
be_bryan 0:b74591d5ab33 71 choices[key] += value
be_bryan 0:b74591d5ab33 72 choices = sorted([(v, k) for k, v in choices.iteritems()], reverse=True)
be_bryan 0:b74591d5ab33 73 if not choices : return []
be_bryan 0:b74591d5ab33 74 elif len(choices) == 1 : return [choices[0][1]]
be_bryan 0:b74591d5ab33 75 elif choices[0][0] > choices[1][0] : choices = choices[:1]
be_bryan 0:b74591d5ab33 76 else : choices = list(takewhile(lambda t: t[0] == choices[0][0], choices))
be_bryan 0:b74591d5ab33 77 return [v for k,v in choices]
be_bryan 0:b74591d5ab33 78
be_bryan 0:b74591d5ab33 79 @subcommand('cache',
be_bryan 0:b74591d5ab33 80 dict(name='matches', nargs="*",
be_bryan 0:b74591d5ab33 81 help="a bunch of things to search for in part names"),
be_bryan 0:b74591d5ab33 82 dict(name=['-e','--everything'], action="store_true",
be_bryan 0:b74591d5ab33 83 help="download everything possible"),
be_bryan 0:b74591d5ab33 84 dict(name=['-d','--descriptors'], action="store_true",
be_bryan 0:b74591d5ab33 85 help="download all descriptors"),
be_bryan 0:b74591d5ab33 86 dict(name=["-b","--batch"], action="store_true",
be_bryan 0:b74591d5ab33 87 help="don't ask for user input and assume download all"),
be_bryan 0:b74591d5ab33 88 help="Cache a group of PACK or PDSC files")
be_bryan 0:b74591d5ab33 89 def command_cache (cache, matches, everything=False, descriptors=False, batch=False, verbose= False, intersection=True) :
be_bryan 0:b74591d5ab33 90 if everything :
be_bryan 0:b74591d5ab33 91 cache.cache_everything()
be_bryan 0:b74591d5ab33 92 return True
be_bryan 0:b74591d5ab33 93 if descriptors :
be_bryan 0:b74591d5ab33 94 cache.cache_descriptors()
be_bryan 0:b74591d5ab33 95 return True
be_bryan 0:b74591d5ab33 96 if not matches :
be_bryan 0:b74591d5ab33 97 print("No action specified nothing to do")
be_bryan 0:b74591d5ab33 98 else :
be_bryan 0:b74591d5ab33 99 urls = cache.get_urls()
be_bryan 0:b74591d5ab33 100 if intersection :
be_bryan 0:b74591d5ab33 101 choices = fuzzy_find(matches, map(basename, urls))
be_bryan 0:b74591d5ab33 102 else :
be_bryan 0:b74591d5ab33 103 choices = sum([fuzzy_find([m], map(basename, urls)) for m in matches], [])
be_bryan 0:b74591d5ab33 104 if not batch and len(choices) > 1 :
be_bryan 0:b74591d5ab33 105 choices = user_selection("Please select a file to cache", choices)
be_bryan 0:b74591d5ab33 106 to_download = []
be_bryan 0:b74591d5ab33 107 for choice in choices :
be_bryan 0:b74591d5ab33 108 for url in urls :
be_bryan 0:b74591d5ab33 109 if choice in url :
be_bryan 0:b74591d5ab33 110 to_download.append(url)
be_bryan 0:b74591d5ab33 111 cache.cache_pack_list(to_download)
be_bryan 0:b74591d5ab33 112 return True
be_bryan 0:b74591d5ab33 113
be_bryan 0:b74591d5ab33 114
be_bryan 0:b74591d5ab33 115 @subcommand('find-part',
be_bryan 0:b74591d5ab33 116 dict(name='matches', nargs="+", help="words to match to processors"),
be_bryan 0:b74591d5ab33 117 dict(name=['-l',"--long"], action="store_true",
be_bryan 0:b74591d5ab33 118 help="print out part details with part"),
be_bryan 0:b74591d5ab33 119 dict(name=['-p', '--parts-only'], action="store_false", dest="print_aliases"),
be_bryan 0:b74591d5ab33 120 dict(name=['-a', '--aliases-only'], action="store_false", dest="print_parts"),
be_bryan 0:b74591d5ab33 121 help="Find a Part and it's description within the cache")
be_bryan 0:b74591d5ab33 122 def command_find_part (cache, matches, long=False, intersection=True,
be_bryan 0:b74591d5ab33 123 print_aliases=True, print_parts=True) :
be_bryan 0:b74591d5ab33 124 if long :
be_bryan 0:b74591d5ab33 125 import pprint
be_bryan 0:b74591d5ab33 126 pp = pprint.PrettyPrinter()
be_bryan 0:b74591d5ab33 127 parts = cache.index
be_bryan 0:b74591d5ab33 128 if intersection :
be_bryan 0:b74591d5ab33 129 choices = fuzzy_find(matches, parts.keys())
be_bryan 0:b74591d5ab33 130 aliases = fuzzy_find(matches, cache.aliases.keys())
be_bryan 0:b74591d5ab33 131 else :
be_bryan 0:b74591d5ab33 132 choices = sum([fuzzy_find([m], parts.keys()) for m in matches], [])
be_bryan 0:b74591d5ab33 133 aliases = sum([fuzzy_find([m], cache.aliases.keys()) for m in matches], [])
be_bryan 0:b74591d5ab33 134 if print_parts:
be_bryan 0:b74591d5ab33 135 for part in choices :
be_bryan 0:b74591d5ab33 136 print part
be_bryan 0:b74591d5ab33 137 if long :
be_bryan 0:b74591d5ab33 138 pp.pprint(cache.index[part])
be_bryan 0:b74591d5ab33 139 if print_aliases:
be_bryan 0:b74591d5ab33 140 for alias in aliases :
be_bryan 0:b74591d5ab33 141 print alias
be_bryan 0:b74591d5ab33 142 if long :
be_bryan 0:b74591d5ab33 143 pp.pprint(cache.index[cache.aliases[alias]])
be_bryan 0:b74591d5ab33 144
be_bryan 0:b74591d5ab33 145 @subcommand('dump-parts',
be_bryan 0:b74591d5ab33 146 dict(name='out', help='directory to dump to'),
be_bryan 0:b74591d5ab33 147 dict(name='parts', nargs='+', help='parts to dump'),
be_bryan 0:b74591d5ab33 148 help='Create a directory with an index.json describing the part and all of their associated flashing algorithms.'
be_bryan 0:b74591d5ab33 149 )
be_bryan 0:b74591d5ab33 150 def command_dump_parts (cache, out, parts, intersection=False) :
be_bryan 0:b74591d5ab33 151 index = {}
be_bryan 0:b74591d5ab33 152 if intersection :
be_bryan 0:b74591d5ab33 153 for part in fuzzy_find(parts, cache.index):
be_bryan 0:b74591d5ab33 154 index.update(cache.index[part])
be_bryan 0:b74591d5ab33 155 else :
be_bryan 0:b74591d5ab33 156 for part in parts :
be_bryan 0:b74591d5ab33 157 index.update(dict(cache.find_device(part)))
be_bryan 0:b74591d5ab33 158 for n, p in index.iteritems() :
be_bryan 0:b74591d5ab33 159 try :
be_bryan 0:b74591d5ab33 160 if not exists(join(out, dirname(p['algorithm']['file']))) :
be_bryan 0:b74591d5ab33 161 makedirs(join(out, dirname(p['algorithm']['file'])))
be_bryan 0:b74591d5ab33 162 with open(join(out, p['algorithm']['file']), "wb+") as fd :
be_bryan 0:b74591d5ab33 163 fd.write(cache.get_flash_algorthim_binary(n).read())
be_bryan 0:b74591d5ab33 164 except KeyError:
be_bryan 0:b74591d5ab33 165 print("[Warning] {} does not have an associated flashing algorithm".format(n))
be_bryan 0:b74591d5ab33 166 with open(join(out, "index.json"), "wb+") as fd :
be_bryan 0:b74591d5ab33 167 dump(index,fd)
be_bryan 0:b74591d5ab33 168
be_bryan 0:b74591d5ab33 169
be_bryan 0:b74591d5ab33 170 @subcommand('cache-part',
be_bryan 0:b74591d5ab33 171 dict(name='matches', nargs="+", help="words to match to devices"),
be_bryan 0:b74591d5ab33 172 help='Cache PACK files associated with the parts matching the provided words')
be_bryan 0:b74591d5ab33 173 def command_cache_part (cache, matches, intersection=True) :
be_bryan 0:b74591d5ab33 174 index = cache.index
be_bryan 0:b74591d5ab33 175 if intersection :
be_bryan 0:b74591d5ab33 176 choices = fuzzy_find(matches, index.keys())
be_bryan 0:b74591d5ab33 177 aliases = fuzzy_find(matches, cache.aliases.keys())
be_bryan 0:b74591d5ab33 178 else :
be_bryan 0:b74591d5ab33 179 choices = sum([fuzzy_find([m], index.keys()) for m in matches], [])
be_bryan 0:b74591d5ab33 180 aliases = sum([fuzzy_find([m], cache.aliases.keys()) for m in matches], [])
be_bryan 0:b74591d5ab33 181 urls = set([index[c]['pdsc_file'] for c in choices])
be_bryan 0:b74591d5ab33 182 urls += set([index[cache.aliasse[a]] for a in aliases])
be_bryan 0:b74591d5ab33 183 cache.cache_pack_list(list(urls))
be_bryan 0:b74591d5ab33 184
be_bryan 0:b74591d5ab33 185 def get_argparse() :
be_bryan 0:b74591d5ab33 186 return parser
be_bryan 0:b74591d5ab33 187
be_bryan 0:b74591d5ab33 188 def main() :
be_bryan 0:b74591d5ab33 189 args = parser.parse_args()
be_bryan 0:b74591d5ab33 190 args.command(args)
be_bryan 0:b74591d5ab33 191