Clone of official tools

Committer:
Anders Blomdell
Date:
Thu Feb 04 17:17:13 2021 +0100
Revision:
47:21ae3e5a7128
Parent:
43:2a7da56ebd24
Add a few normpath calls

Who changed what in which revision?

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