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