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