Clone of official tools
arm_pack_manager/__init__.py@35:da9c89f8be7d, 2017-02-15 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Wed Feb 15 13:53:18 2017 -0600
- Revision:
- 35:da9c89f8be7d
- Parent:
- 31:8ea194f6145b
- Child:
- 36:96847d42f010
Update tools to mbed-os 5.3.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
The Other Jimmy |
31:8ea194f6145b | 1 | from urllib2 import urlopen, URLError |
The Other Jimmy |
31:8ea194f6145b | 2 | from bs4 import BeautifulSoup |
The Other Jimmy |
31:8ea194f6145b | 3 | from os.path import join, dirname, basename |
The Other Jimmy |
31:8ea194f6145b | 4 | from os import makedirs |
The Other Jimmy |
31:8ea194f6145b | 5 | from errno import EEXIST |
The Other Jimmy |
31:8ea194f6145b | 6 | from threading import Thread |
The Other Jimmy |
31:8ea194f6145b | 7 | from Queue import Queue |
The Other Jimmy |
31:8ea194f6145b | 8 | from re import compile, sub |
The Other Jimmy |
31:8ea194f6145b | 9 | from sys import stderr, stdout |
The Other Jimmy |
31:8ea194f6145b | 10 | from fuzzywuzzy import process |
The Other Jimmy |
31:8ea194f6145b | 11 | from itertools import takewhile |
The Other Jimmy |
31:8ea194f6145b | 12 | import argparse |
The Other Jimmy |
31:8ea194f6145b | 13 | from json import dump, load |
The Other Jimmy |
31:8ea194f6145b | 14 | from zipfile import ZipFile |
The Other Jimmy |
31:8ea194f6145b | 15 | from tempfile import gettempdir |
The Other Jimmy |
31:8ea194f6145b | 16 | |
The Other Jimmy |
31:8ea194f6145b | 17 | RootPackURL = "http://www.keil.com/pack/index.idx" |
The Other Jimmy |
31:8ea194f6145b | 18 | |
The Other Jimmy |
31:8ea194f6145b | 19 | LocalPackDir = dirname(__file__) |
The Other Jimmy |
31:8ea194f6145b | 20 | LocalPackIndex = join(LocalPackDir, "index.json") |
The Other Jimmy |
31:8ea194f6145b | 21 | LocalPackAliases = join(LocalPackDir, "aliases.json") |
The Other Jimmy |
31:8ea194f6145b | 22 | |
The Other Jimmy |
31:8ea194f6145b | 23 | |
The Other Jimmy |
31:8ea194f6145b | 24 | protocol_matcher = compile("\w*://") |
The Other Jimmy |
31:8ea194f6145b | 25 | def strip_protocol(url) : |
The Other Jimmy |
31:8ea194f6145b | 26 | return protocol_matcher.sub("", str(url)) |
The Other Jimmy |
31:8ea194f6145b | 27 | |
The Other Jimmy |
31:8ea194f6145b | 28 | def largest_version(content) : |
The Other Jimmy |
35:da9c89f8be7d | 29 | return sorted([t['version'] for t in content.package.releases('release')], |
The Other Jimmy |
35:da9c89f8be7d | 30 | reverse=True, key=lambda v: map(int, v.split(".")))[0] |
The Other Jimmy |
31:8ea194f6145b | 31 | |
The Other Jimmy |
31:8ea194f6145b | 32 | def do_queue(Class, function, interable) : |
The Other Jimmy |
31:8ea194f6145b | 33 | q = Queue() |
The Other Jimmy |
31:8ea194f6145b | 34 | threads = [Class(q, function) for each in range(20)] |
The Other Jimmy |
31:8ea194f6145b | 35 | for each in threads : |
The Other Jimmy |
31:8ea194f6145b | 36 | each.setDaemon(True) |
The Other Jimmy |
31:8ea194f6145b | 37 | each.start() |
The Other Jimmy |
31:8ea194f6145b | 38 | for thing in interable : |
The Other Jimmy |
31:8ea194f6145b | 39 | q.put(thing) |
The Other Jimmy |
31:8ea194f6145b | 40 | q.join() |
The Other Jimmy |
31:8ea194f6145b | 41 | |
The Other Jimmy |
31:8ea194f6145b | 42 | class Reader (Thread) : |
The Other Jimmy |
31:8ea194f6145b | 43 | def __init__(self, queue, func) : |
The Other Jimmy |
31:8ea194f6145b | 44 | Thread.__init__(self) |
The Other Jimmy |
31:8ea194f6145b | 45 | self.queue = queue |
The Other Jimmy |
31:8ea194f6145b | 46 | self.func = func |
The Other Jimmy |
31:8ea194f6145b | 47 | def run(self) : |
The Other Jimmy |
31:8ea194f6145b | 48 | while True : |
The Other Jimmy |
31:8ea194f6145b | 49 | url = self.queue.get() |
The Other Jimmy |
31:8ea194f6145b | 50 | self.func(url) |
The Other Jimmy |
31:8ea194f6145b | 51 | self.queue.task_done() |
The Other Jimmy |
31:8ea194f6145b | 52 | |
The Other Jimmy |
31:8ea194f6145b | 53 | |
The Other Jimmy |
31:8ea194f6145b | 54 | class Cache () : |
The Other Jimmy |
31:8ea194f6145b | 55 | """ The Cache object is the only relevant API object at the moment |
The Other Jimmy |
31:8ea194f6145b | 56 | |
The Other Jimmy |
31:8ea194f6145b | 57 | Constructing the Cache object does not imply any caching. |
The Other Jimmy |
31:8ea194f6145b | 58 | A user of the API must explicitly call caching functions. |
The Other Jimmy |
31:8ea194f6145b | 59 | |
The Other Jimmy |
31:8ea194f6145b | 60 | :param silent: A boolean that, when True, significantly reduces the printing of this Object |
The Other Jimmy |
31:8ea194f6145b | 61 | :type silent: bool |
The Other Jimmy |
31:8ea194f6145b | 62 | :param no_timeouts: A boolean that, when True, disables the default connection timeout and low speed timeout for downloading things. |
The Other Jimmy |
31:8ea194f6145b | 63 | :type no_timeouts: bool |
The Other Jimmy |
31:8ea194f6145b | 64 | """ |
The Other Jimmy |
31:8ea194f6145b | 65 | def __init__ (self, silent, no_timeouts) : |
The Other Jimmy |
31:8ea194f6145b | 66 | self.silent = silent |
The Other Jimmy |
31:8ea194f6145b | 67 | self.counter = 0 |
The Other Jimmy |
31:8ea194f6145b | 68 | self.total = 1 |
The Other Jimmy |
31:8ea194f6145b | 69 | self._index = {} |
The Other Jimmy |
31:8ea194f6145b | 70 | self._aliases = {} |
The Other Jimmy |
31:8ea194f6145b | 71 | self.urls = None |
The Other Jimmy |
31:8ea194f6145b | 72 | self.no_timeouts = no_timeouts |
The Other Jimmy |
31:8ea194f6145b | 73 | self.data_path = gettempdir() |
The Other Jimmy |
31:8ea194f6145b | 74 | |
The Other Jimmy |
31:8ea194f6145b | 75 | def display_counter (self, message) : |
The Other Jimmy |
31:8ea194f6145b | 76 | stdout.write("{} {}/{}\r".format(message, self.counter, self.total)) |
The Other Jimmy |
31:8ea194f6145b | 77 | stdout.flush() |
The Other Jimmy |
31:8ea194f6145b | 78 | |
The Other Jimmy |
31:8ea194f6145b | 79 | def cache_file (self, url) : |
The Other Jimmy |
31:8ea194f6145b | 80 | """Low level interface to caching a single file. |
The Other Jimmy |
31:8ea194f6145b | 81 | |
The Other Jimmy |
31:8ea194f6145b | 82 | :param url: The URL to cache. |
The Other Jimmy |
31:8ea194f6145b | 83 | :type url: str |
The Other Jimmy |
31:8ea194f6145b | 84 | :rtype: None |
The Other Jimmy |
31:8ea194f6145b | 85 | """ |
The Other Jimmy |
31:8ea194f6145b | 86 | if not self.silent : print("Caching {}...".format(url)) |
The Other Jimmy |
31:8ea194f6145b | 87 | dest = join(self.data_path, strip_protocol(url)) |
The Other Jimmy |
31:8ea194f6145b | 88 | try : |
The Other Jimmy |
31:8ea194f6145b | 89 | makedirs(dirname(dest)) |
The Other Jimmy |
31:8ea194f6145b | 90 | except OSError as exc : |
The Other Jimmy |
31:8ea194f6145b | 91 | if exc.errno == EEXIST : pass |
The Other Jimmy |
31:8ea194f6145b | 92 | else : raise |
The Other Jimmy |
31:8ea194f6145b | 93 | try: |
The Other Jimmy |
31:8ea194f6145b | 94 | with open(dest, "wb+") as fd : |
The Other Jimmy |
31:8ea194f6145b | 95 | fd.write(urlopen(url).read()) |
The Other Jimmy |
31:8ea194f6145b | 96 | except URLError as e: |
The Other Jimmy |
31:8ea194f6145b | 97 | stderr.write(e.reason) |
The Other Jimmy |
31:8ea194f6145b | 98 | self.counter += 1 |
The Other Jimmy |
31:8ea194f6145b | 99 | self.display_counter("Caching Files") |
The Other Jimmy |
31:8ea194f6145b | 100 | |
The Other Jimmy |
31:8ea194f6145b | 101 | def pdsc_to_pack (self, url) : |
The Other Jimmy |
31:8ea194f6145b | 102 | """Find the URL of the specified pack file described by a PDSC. |
The Other Jimmy |
31:8ea194f6145b | 103 | |
The Other Jimmy |
31:8ea194f6145b | 104 | The PDSC is assumed to be cached and is looked up in the cache by its URL. |
The Other Jimmy |
31:8ea194f6145b | 105 | |
The Other Jimmy |
31:8ea194f6145b | 106 | :param url: The url used to look up the PDSC. |
The Other Jimmy |
31:8ea194f6145b | 107 | :type url: str |
The Other Jimmy |
31:8ea194f6145b | 108 | :return: The url of the PACK file. |
The Other Jimmy |
31:8ea194f6145b | 109 | :rtype: str |
The Other Jimmy |
31:8ea194f6145b | 110 | """ |
The Other Jimmy |
31:8ea194f6145b | 111 | content = self.pdsc_from_cache(url) |
The Other Jimmy |
31:8ea194f6145b | 112 | new_url = content.package.url.get_text() |
The Other Jimmy |
31:8ea194f6145b | 113 | if not new_url.endswith("/") : |
The Other Jimmy |
31:8ea194f6145b | 114 | new_url = new_url + "/" |
The Other Jimmy |
31:8ea194f6145b | 115 | return (new_url + content.package.vendor.get_text() + "." + |
The Other Jimmy |
31:8ea194f6145b | 116 | content.package.find('name').get_text() + "." + |
The Other Jimmy |
31:8ea194f6145b | 117 | largest_version(content) + ".pack") |
The Other Jimmy |
31:8ea194f6145b | 118 | |
The Other Jimmy |
31:8ea194f6145b | 119 | def cache_pdsc_and_pack (self, url) : |
The Other Jimmy |
31:8ea194f6145b | 120 | self.cache_file(url) |
The Other Jimmy |
31:8ea194f6145b | 121 | try : |
The Other Jimmy |
31:8ea194f6145b | 122 | self.cache_file(self.pdsc_to_pack(url)) |
The Other Jimmy |
31:8ea194f6145b | 123 | except AttributeError : |
The Other Jimmy |
31:8ea194f6145b | 124 | stderr.write("[ ERROR ] {} does not appear to be a conforming .pdsc file\n".format(url)) |
The Other Jimmy |
31:8ea194f6145b | 125 | self.counter += 1 |
The Other Jimmy |
31:8ea194f6145b | 126 | |
The Other Jimmy |
31:8ea194f6145b | 127 | def get_urls(self): |
The Other Jimmy |
31:8ea194f6145b | 128 | """Extract the URLs of all know PDSC files. |
The Other Jimmy |
31:8ea194f6145b | 129 | |
The Other Jimmy |
31:8ea194f6145b | 130 | Will pull the index from the internet if it is not cached. |
The Other Jimmy |
31:8ea194f6145b | 131 | |
The Other Jimmy |
31:8ea194f6145b | 132 | :return: A list of all PDSC URLs |
The Other Jimmy |
31:8ea194f6145b | 133 | :rtype: [str] |
The Other Jimmy |
31:8ea194f6145b | 134 | """ |
The Other Jimmy |
31:8ea194f6145b | 135 | if not self.urls : |
The Other Jimmy |
31:8ea194f6145b | 136 | try : root_data = self.pdsc_from_cache(RootPackURL) |
The Other Jimmy |
31:8ea194f6145b | 137 | except IOError : root_data = self.cache_and_parse(RootPackURL) |
The Other Jimmy |
31:8ea194f6145b | 138 | self.urls = ["/".join([pdsc.get('url').strip("/"), |
The Other Jimmy |
31:8ea194f6145b | 139 | pdsc.get('name').strip("/")]) |
The Other Jimmy |
31:8ea194f6145b | 140 | for pdsc in root_data.find_all("pdsc")] |
The Other Jimmy |
31:8ea194f6145b | 141 | return self.urls |
The Other Jimmy |
31:8ea194f6145b | 142 | |
The Other Jimmy |
31:8ea194f6145b | 143 | def _extract_dict(self, device, filename, pack) : |
The Other Jimmy |
31:8ea194f6145b | 144 | to_ret = dict(pdsc_file=filename, pack_file=pack) |
The Other Jimmy |
31:8ea194f6145b | 145 | try : to_ret["memory"] = dict([(m["id"], dict(start=m["start"], |
The Other Jimmy |
31:8ea194f6145b | 146 | size=m["size"])) |
The Other Jimmy |
31:8ea194f6145b | 147 | for m in device("memory")]) |
The Other Jimmy |
31:8ea194f6145b | 148 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 149 | try: algorithms = device("algorithm") |
The Other Jimmy |
31:8ea194f6145b | 150 | except: |
The Other Jimmy |
31:8ea194f6145b | 151 | try: algorithms = device.parent("algorithm") |
The Other Jimmy |
31:8ea194f6145b | 152 | except: pass |
The Other Jimmy |
31:8ea194f6145b | 153 | else: |
The Other Jimmy |
31:8ea194f6145b | 154 | if not algorithms: |
The Other Jimmy |
31:8ea194f6145b | 155 | try: algorithms = device.parent("algorithm") |
The Other Jimmy |
31:8ea194f6145b | 156 | except: pass |
The Other Jimmy |
31:8ea194f6145b | 157 | try : to_ret["algorithm"] = dict([(algo.get("name").replace('\\','/'), |
The Other Jimmy |
31:8ea194f6145b | 158 | dict(start=algo["start"], |
The Other Jimmy |
31:8ea194f6145b | 159 | size=algo["size"], |
The Other Jimmy |
31:8ea194f6145b | 160 | ramstart=algo.get("ramstart",None), |
The Other Jimmy |
31:8ea194f6145b | 161 | ramsize=algo.get("ramsize",None), |
The Other Jimmy |
31:8ea194f6145b | 162 | default=algo.get("default",1))) |
The Other Jimmy |
31:8ea194f6145b | 163 | for algo in algorithms]) |
The Other Jimmy |
31:8ea194f6145b | 164 | except (KeyError, TypeError, IndexError) as e: pass |
The Other Jimmy |
31:8ea194f6145b | 165 | try: to_ret["debug"] = device.parent.parent.debug["svd"] |
The Other Jimmy |
31:8ea194f6145b | 166 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 167 | try: to_ret["debug"] = device.parent.debug["svd"] |
The Other Jimmy |
31:8ea194f6145b | 168 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 169 | try: to_ret["debug"] = device.debug["svd"] |
The Other Jimmy |
31:8ea194f6145b | 170 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 171 | |
The Other Jimmy |
31:8ea194f6145b | 172 | to_ret["compile"] = {} |
The Other Jimmy |
31:8ea194f6145b | 173 | try: compile_l1 = device.parent("compile") |
The Other Jimmy |
31:8ea194f6145b | 174 | except (KeyError, TypeError, IndexError) as e : compile_l1 = [] |
The Other Jimmy |
31:8ea194f6145b | 175 | try: compile_l2 = device.parent.parent("compile") |
The Other Jimmy |
31:8ea194f6145b | 176 | except (KeyError, TypeError, IndexError) as e : compile_l2 = [] |
The Other Jimmy |
31:8ea194f6145b | 177 | compile = compile_l2 + compile_l1 |
The Other Jimmy |
31:8ea194f6145b | 178 | for c in compile: |
The Other Jimmy |
31:8ea194f6145b | 179 | try: to_ret["compile"]["header"] = c["header"] |
The Other Jimmy |
31:8ea194f6145b | 180 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 181 | try: to_ret["compile"]["define"] = c["define"] |
The Other Jimmy |
31:8ea194f6145b | 182 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 183 | |
The Other Jimmy |
31:8ea194f6145b | 184 | try: to_ret["core"] = device.parent.processor['dcore'] |
The Other Jimmy |
31:8ea194f6145b | 185 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 186 | try: to_ret["core"] = device.parent.parent.processor['dcore'] |
The Other Jimmy |
31:8ea194f6145b | 187 | except (KeyError, TypeError, IndexError) as e : pass |
The Other Jimmy |
31:8ea194f6145b | 188 | |
The Other Jimmy |
31:8ea194f6145b | 189 | to_ret["processor"] = {} |
The Other Jimmy |
31:8ea194f6145b | 190 | try: proc_l1 = device("processor") |
The Other Jimmy |
31:8ea194f6145b | 191 | except (KeyError, TypeError, IndexError) as e: proc_l1 = [] |
The Other Jimmy |
31:8ea194f6145b | 192 | try: proc_l2 = device.parent("processor") |
The Other Jimmy |
31:8ea194f6145b | 193 | except (KeyError, TypeError, IndexError) as e: proc_l2 = [] |
The Other Jimmy |
31:8ea194f6145b | 194 | try: proc_l3 = device.parent.parent("processor") |
The Other Jimmy |
31:8ea194f6145b | 195 | except (KeyError, TypeError, IndexError) as e: proc_l3 = [] |
The Other Jimmy |
31:8ea194f6145b | 196 | proc = proc_l3 + proc_l2 + proc_l1 |
The Other Jimmy |
31:8ea194f6145b | 197 | for p in proc: |
The Other Jimmy |
31:8ea194f6145b | 198 | try: to_ret["processor"]["fpu"] = p['dfpu'] |
The Other Jimmy |
31:8ea194f6145b | 199 | except (KeyError, TypeError, IndexError) as e: pass |
The Other Jimmy |
31:8ea194f6145b | 200 | try: to_ret["processor"]["endianness"] = p['dendian'] |
The Other Jimmy |
31:8ea194f6145b | 201 | except (KeyError, TypeError, IndexError) as e: pass |
The Other Jimmy |
31:8ea194f6145b | 202 | try: to_ret["processor"]["clock"] = p['dclock'] |
The Other Jimmy |
31:8ea194f6145b | 203 | except (KeyError, TypeError, IndexError) as e: pass |
The Other Jimmy |
31:8ea194f6145b | 204 | |
The Other Jimmy |
31:8ea194f6145b | 205 | try: to_ret["vendor"] = device.parent['dvendor'] |
The Other Jimmy |
31:8ea194f6145b | 206 | except (KeyError, TypeError, IndexError) as e: pass |
The Other Jimmy |
31:8ea194f6145b | 207 | try: to_ret["vendor"] = device.parent.parent['dvendor'] |
The Other Jimmy |
31:8ea194f6145b | 208 | except (KeyError, TypeError, IndexError) as e: pass |
The Other Jimmy |
31:8ea194f6145b | 209 | |
The Other Jimmy |
31:8ea194f6145b | 210 | if not to_ret["processor"]: |
The Other Jimmy |
31:8ea194f6145b | 211 | del to_ret["processor"] |
The Other Jimmy |
31:8ea194f6145b | 212 | |
The Other Jimmy |
31:8ea194f6145b | 213 | if not to_ret["compile"]: |
The Other Jimmy |
31:8ea194f6145b | 214 | del to_ret["compile"] |
The Other Jimmy |
31:8ea194f6145b | 215 | |
The Other Jimmy |
31:8ea194f6145b | 216 | to_ret['debug-interface'] = [] |
The Other Jimmy |
31:8ea194f6145b | 217 | |
The Other Jimmy |
31:8ea194f6145b | 218 | return to_ret |
The Other Jimmy |
31:8ea194f6145b | 219 | |
The Other Jimmy |
31:8ea194f6145b | 220 | def _generate_index_helper(self, d) : |
The Other Jimmy |
31:8ea194f6145b | 221 | try : |
The Other Jimmy |
31:8ea194f6145b | 222 | pack = self.pdsc_to_pack(d) |
The Other Jimmy |
31:8ea194f6145b | 223 | self._index.update(dict([(dev['dname'], self._extract_dict(dev, d, pack)) for dev in |
The Other Jimmy |
31:8ea194f6145b | 224 | (self.pdsc_from_cache(d)("device"))])) |
The Other Jimmy |
31:8ea194f6145b | 225 | except AttributeError as e : |
The Other Jimmy |
31:8ea194f6145b | 226 | stderr.write("[ ERROR ] file {}\n".format(d)) |
The Other Jimmy |
31:8ea194f6145b | 227 | print(e) |
The Other Jimmy |
31:8ea194f6145b | 228 | self.counter += 1 |
The Other Jimmy |
31:8ea194f6145b | 229 | self.display_counter("Generating Index") |
The Other Jimmy |
31:8ea194f6145b | 230 | |
The Other Jimmy |
31:8ea194f6145b | 231 | def _generate_aliases_helper(self, d) : |
The Other Jimmy |
31:8ea194f6145b | 232 | try : |
The Other Jimmy |
31:8ea194f6145b | 233 | mydict = [] |
The Other Jimmy |
31:8ea194f6145b | 234 | for dev in self.pdsc_from_cache(d)("board"): |
The Other Jimmy |
31:8ea194f6145b | 235 | try : |
The Other Jimmy |
31:8ea194f6145b | 236 | mydict.append((dev['name'], dev.mounteddevice['dname'])) |
The Other Jimmy |
31:8ea194f6145b | 237 | except (KeyError, TypeError, IndexError) as e: |
The Other Jimmy |
31:8ea194f6145b | 238 | pass |
The Other Jimmy |
31:8ea194f6145b | 239 | self._aliases.update(dict(mydict)) |
The Other Jimmy |
31:8ea194f6145b | 240 | except (AttributeError, TypeError) as e : |
The Other Jimmy |
31:8ea194f6145b | 241 | pass |
The Other Jimmy |
31:8ea194f6145b | 242 | self.counter += 1 |
The Other Jimmy |
31:8ea194f6145b | 243 | self.display_counter("Scanning for Aliases") |
The Other Jimmy |
31:8ea194f6145b | 244 | |
The Other Jimmy |
31:8ea194f6145b | 245 | def get_flash_algorthim_binary(self, device_name) : |
The Other Jimmy |
31:8ea194f6145b | 246 | """Retrieve the flash algorithm file for a particular part. |
The Other Jimmy |
31:8ea194f6145b | 247 | |
The Other Jimmy |
31:8ea194f6145b | 248 | Assumes that both the PDSC and the PACK file associated with that part are in the cache. |
The Other Jimmy |
31:8ea194f6145b | 249 | |
The Other Jimmy |
31:8ea194f6145b | 250 | :param device_name: The exact name of a device |
The Other Jimmy |
31:8ea194f6145b | 251 | :type device_name: str |
The Other Jimmy |
31:8ea194f6145b | 252 | :return: A file-like object that, when read, is the ELF file that describes the flashing algorithm |
The Other Jimmy |
31:8ea194f6145b | 253 | :rtype: ZipExtFile |
The Other Jimmy |
31:8ea194f6145b | 254 | """ |
The Other Jimmy |
35:da9c89f8be7d | 255 | device = self.index[device_name] |
The Other Jimmy |
35:da9c89f8be7d | 256 | pack = self.pack_from_cache(device) |
The Other Jimmy |
35:da9c89f8be7d | 257 | return pack.open(device['algorithm'].keys()[0]) |
The Other Jimmy |
31:8ea194f6145b | 258 | |
The Other Jimmy |
31:8ea194f6145b | 259 | def get_svd_file(self, device_name) : |
The Other Jimmy |
31:8ea194f6145b | 260 | """Retrieve the flash algorithm file for a particular part. |
The Other Jimmy |
31:8ea194f6145b | 261 | |
The Other Jimmy |
31:8ea194f6145b | 262 | Assumes that both the PDSC and the PACK file associated with that part are in the cache. |
The Other Jimmy |
31:8ea194f6145b | 263 | |
The Other Jimmy |
31:8ea194f6145b | 264 | :param device_name: The exact name of a device |
The Other Jimmy |
31:8ea194f6145b | 265 | :type device_name: str |
The Other Jimmy |
31:8ea194f6145b | 266 | :return: A file-like object that, when read, is the ELF file that describes the flashing algorithm |
The Other Jimmy |
31:8ea194f6145b | 267 | :rtype: ZipExtFile |
The Other Jimmy |
31:8ea194f6145b | 268 | """ |
The Other Jimmy |
35:da9c89f8be7d | 269 | device = self.index[device_name] |
The Other Jimmy |
35:da9c89f8be7d | 270 | pack = self.pack_from_cache(device) |
The Other Jimmy |
31:8ea194f6145b | 271 | return pack.open(device['debug']) |
The Other Jimmy |
31:8ea194f6145b | 272 | |
The Other Jimmy |
31:8ea194f6145b | 273 | def generate_index(self) : |
The Other Jimmy |
31:8ea194f6145b | 274 | self._index = {} |
The Other Jimmy |
31:8ea194f6145b | 275 | self.counter = 0 |
The Other Jimmy |
31:8ea194f6145b | 276 | do_queue(Reader, self._generate_index_helper, self.get_urls()) |
The Other Jimmy |
31:8ea194f6145b | 277 | with open(LocalPackIndex, "wb+") as out: |
The Other Jimmy |
31:8ea194f6145b | 278 | self._index["version"] = "0.1.0" |
The Other Jimmy |
31:8ea194f6145b | 279 | dump(self._index, out) |
The Other Jimmy |
31:8ea194f6145b | 280 | stdout.write("\n") |
The Other Jimmy |
31:8ea194f6145b | 281 | |
The Other Jimmy |
31:8ea194f6145b | 282 | def generate_aliases(self) : |
The Other Jimmy |
31:8ea194f6145b | 283 | self._aliases = {} |
The Other Jimmy |
31:8ea194f6145b | 284 | self.counter = 0 |
The Other Jimmy |
31:8ea194f6145b | 285 | do_queue(Reader, self._generate_aliases_helper, self.get_urls()) |
The Other Jimmy |
31:8ea194f6145b | 286 | with open(LocalPackAliases, "wb+") as out: |
The Other Jimmy |
31:8ea194f6145b | 287 | dump(self._aliases, out) |
The Other Jimmy |
31:8ea194f6145b | 288 | stdout.write("\n") |
The Other Jimmy |
31:8ea194f6145b | 289 | |
The Other Jimmy |
31:8ea194f6145b | 290 | def find_device(self, match) : |
The Other Jimmy |
31:8ea194f6145b | 291 | choices = process.extract(match, self.index.keys(), limit=len(self.index)) |
The Other Jimmy |
31:8ea194f6145b | 292 | choices = sorted([(v, k) for k, v in choices], reverse=True) |
The Other Jimmy |
31:8ea194f6145b | 293 | if choices : choices = list(takewhile(lambda t: t[0] == choices[0][0], choices)) |
The Other Jimmy |
31:8ea194f6145b | 294 | return [(v, self.index[v]) for k,v in choices] |
The Other Jimmy |
31:8ea194f6145b | 295 | |
The Other Jimmy |
31:8ea194f6145b | 296 | def dump_index_to_file(self, file) : |
The Other Jimmy |
31:8ea194f6145b | 297 | with open(file, "wb+") as out: |
The Other Jimmy |
31:8ea194f6145b | 298 | dump(self.index, out) |
The Other Jimmy |
31:8ea194f6145b | 299 | |
The Other Jimmy |
31:8ea194f6145b | 300 | @property |
The Other Jimmy |
31:8ea194f6145b | 301 | def index(self) : |
The Other Jimmy |
31:8ea194f6145b | 302 | """An index of most of the important data in all cached PDSC files. |
The Other Jimmy |
31:8ea194f6145b | 303 | |
The Other Jimmy |
31:8ea194f6145b | 304 | :Example: |
The Other Jimmy |
31:8ea194f6145b | 305 | |
The Other Jimmy |
31:8ea194f6145b | 306 | >>> from ArmPackManager import Cache |
The Other Jimmy |
31:8ea194f6145b | 307 | >>> a = Cache() |
The Other Jimmy |
31:8ea194f6145b | 308 | >>> a.index["LPC1768"] |
The Other Jimmy |
31:8ea194f6145b | 309 | {u'algorithm': {u'RAMsize': u'0x0FE0', |
The Other Jimmy |
31:8ea194f6145b | 310 | u'RAMstart': u'0x10000000', |
The Other Jimmy |
31:8ea194f6145b | 311 | u'name': u'Flash/LPC_IAP_512.FLM', |
The Other Jimmy |
31:8ea194f6145b | 312 | u'size': u'0x80000', |
The Other Jimmy |
31:8ea194f6145b | 313 | u'start': u'0x00000000'}, |
The Other Jimmy |
31:8ea194f6145b | 314 | u'compile': [u'Device/Include/LPC17xx.h', u'LPC175x_6x'], |
The Other Jimmy |
31:8ea194f6145b | 315 | u'debug': u'SVD/LPC176x5x.svd', |
The Other Jimmy |
31:8ea194f6145b | 316 | u'pdsc_file': u'http://www.keil.com/pack/Keil.LPC1700_DFP.pdsc', |
The Other Jimmy |
31:8ea194f6145b | 317 | u'memory': {u'IRAM1': {u'size': u'0x8000', u'start': u'0x10000000'}, |
The Other Jimmy |
31:8ea194f6145b | 318 | u'IRAM2': {u'size': u'0x8000', u'start': u'0x2007C000'}, |
The Other Jimmy |
31:8ea194f6145b | 319 | u'IROM1': {u'size': u'0x80000', u'start': u'0x00000000'}}} |
The Other Jimmy |
31:8ea194f6145b | 320 | |
The Other Jimmy |
31:8ea194f6145b | 321 | |
The Other Jimmy |
31:8ea194f6145b | 322 | """ |
The Other Jimmy |
31:8ea194f6145b | 323 | if not self._index : |
The Other Jimmy |
31:8ea194f6145b | 324 | with open(LocalPackIndex) as i : |
The Other Jimmy |
31:8ea194f6145b | 325 | self._index = load(i) |
The Other Jimmy |
31:8ea194f6145b | 326 | return self._index |
The Other Jimmy |
31:8ea194f6145b | 327 | @property |
The Other Jimmy |
31:8ea194f6145b | 328 | def aliases(self) : |
The Other Jimmy |
31:8ea194f6145b | 329 | """An index of most of the important data in all cached PDSC files. |
The Other Jimmy |
31:8ea194f6145b | 330 | |
The Other Jimmy |
31:8ea194f6145b | 331 | :Example: |
The Other Jimmy |
31:8ea194f6145b | 332 | |
The Other Jimmy |
31:8ea194f6145b | 333 | >>> from ArmPackManager import Cache |
The Other Jimmy |
31:8ea194f6145b | 334 | >>> a = Cache() |
The Other Jimmy |
31:8ea194f6145b | 335 | >>> a.index["LPC1768"] |
The Other Jimmy |
31:8ea194f6145b | 336 | {u'algorithm': {u'RAMsize': u'0x0FE0', |
The Other Jimmy |
31:8ea194f6145b | 337 | u'RAMstart': u'0x10000000', |
The Other Jimmy |
31:8ea194f6145b | 338 | u'name': u'Flash/LPC_IAP_512.FLM', |
The Other Jimmy |
31:8ea194f6145b | 339 | u'size': u'0x80000', |
The Other Jimmy |
31:8ea194f6145b | 340 | u'start': u'0x00000000'}, |
The Other Jimmy |
31:8ea194f6145b | 341 | u'compile': [u'Device/Include/LPC17xx.h', u'LPC175x_6x'], |
The Other Jimmy |
31:8ea194f6145b | 342 | u'debug': u'SVD/LPC176x5x.svd', |
The Other Jimmy |
31:8ea194f6145b | 343 | u'pdsc_file': u'http://www.keil.com/pack/Keil.LPC1700_DFP.pdsc', |
The Other Jimmy |
31:8ea194f6145b | 344 | u'memory': {u'IRAM1': {u'size': u'0x8000', u'start': u'0x10000000'}, |
The Other Jimmy |
31:8ea194f6145b | 345 | u'IRAM2': {u'size': u'0x8000', u'start': u'0x2007C000'}, |
The Other Jimmy |
31:8ea194f6145b | 346 | u'IROM1': {u'size': u'0x80000', u'start': u'0x00000000'}}} |
The Other Jimmy |
31:8ea194f6145b | 347 | |
The Other Jimmy |
31:8ea194f6145b | 348 | |
The Other Jimmy |
31:8ea194f6145b | 349 | """ |
The Other Jimmy |
31:8ea194f6145b | 350 | if not self._aliases : |
The Other Jimmy |
31:8ea194f6145b | 351 | with open(join(self.data_path, "aliases.json")) as i : |
The Other Jimmy |
31:8ea194f6145b | 352 | self._aliases = load(i) |
The Other Jimmy |
31:8ea194f6145b | 353 | return self._aliases |
The Other Jimmy |
31:8ea194f6145b | 354 | |
The Other Jimmy |
31:8ea194f6145b | 355 | def cache_everything(self) : |
The Other Jimmy |
31:8ea194f6145b | 356 | """Cache every PACK and PDSC file known. |
The Other Jimmy |
31:8ea194f6145b | 357 | |
The Other Jimmy |
31:8ea194f6145b | 358 | Generates an index afterwards. |
The Other Jimmy |
31:8ea194f6145b | 359 | |
The Other Jimmy |
31:8ea194f6145b | 360 | .. note:: This process may use 4GB of drive space and take upwards of 10 minutes to complete. |
The Other Jimmy |
31:8ea194f6145b | 361 | """ |
The Other Jimmy |
31:8ea194f6145b | 362 | self.cache_pack_list(self.get_urls()) |
The Other Jimmy |
31:8ea194f6145b | 363 | self.generate_index() |
The Other Jimmy |
31:8ea194f6145b | 364 | self.generate_aliases() |
The Other Jimmy |
31:8ea194f6145b | 365 | |
The Other Jimmy |
31:8ea194f6145b | 366 | def cache_descriptors(self) : |
The Other Jimmy |
31:8ea194f6145b | 367 | """Cache every PDSC file known. |
The Other Jimmy |
31:8ea194f6145b | 368 | |
The Other Jimmy |
31:8ea194f6145b | 369 | Generates an index afterwards. |
The Other Jimmy |
31:8ea194f6145b | 370 | |
The Other Jimmy |
31:8ea194f6145b | 371 | .. note:: This process may use 11MB of drive space and take upwards of 1 minute. |
The Other Jimmy |
31:8ea194f6145b | 372 | """ |
The Other Jimmy |
31:8ea194f6145b | 373 | self.cache_descriptor_list(self.get_urls()) |
The Other Jimmy |
31:8ea194f6145b | 374 | self.generate_index() |
The Other Jimmy |
31:8ea194f6145b | 375 | self.generate_aliases() |
The Other Jimmy |
31:8ea194f6145b | 376 | |
The Other Jimmy |
31:8ea194f6145b | 377 | def cache_descriptor_list(self, list) : |
The Other Jimmy |
31:8ea194f6145b | 378 | """Cache a list of PDSC files. |
The Other Jimmy |
31:8ea194f6145b | 379 | |
The Other Jimmy |
31:8ea194f6145b | 380 | :param list: URLs of PDSC files to cache. |
The Other Jimmy |
31:8ea194f6145b | 381 | :type list: [str] |
The Other Jimmy |
31:8ea194f6145b | 382 | """ |
The Other Jimmy |
31:8ea194f6145b | 383 | self.total = len(list) |
The Other Jimmy |
31:8ea194f6145b | 384 | self.display_counter("Caching Files") |
The Other Jimmy |
31:8ea194f6145b | 385 | do_queue(Reader, self.cache_file, list) |
The Other Jimmy |
31:8ea194f6145b | 386 | stdout.write("\n") |
The Other Jimmy |
31:8ea194f6145b | 387 | |
The Other Jimmy |
31:8ea194f6145b | 388 | def cache_pack_list(self, list) : |
The Other Jimmy |
31:8ea194f6145b | 389 | """Cache a list of PACK files, referenced by their PDSC URL |
The Other Jimmy |
31:8ea194f6145b | 390 | |
The Other Jimmy |
31:8ea194f6145b | 391 | :param list: URLs of PDSC files to cache. |
The Other Jimmy |
31:8ea194f6145b | 392 | :type list: [str] |
The Other Jimmy |
31:8ea194f6145b | 393 | """ |
The Other Jimmy |
31:8ea194f6145b | 394 | self.total = len(list) * 2 |
The Other Jimmy |
31:8ea194f6145b | 395 | self.display_counter("Caching Files") |
The Other Jimmy |
31:8ea194f6145b | 396 | do_queue(Reader, self.cache_pdsc_and_pack, list) |
The Other Jimmy |
31:8ea194f6145b | 397 | stdout.write("\n") |
The Other Jimmy |
31:8ea194f6145b | 398 | |
The Other Jimmy |
31:8ea194f6145b | 399 | def pdsc_from_cache(self, url) : |
The Other Jimmy |
31:8ea194f6145b | 400 | """Low level inteface for extracting a PDSC file from the cache. |
The Other Jimmy |
31:8ea194f6145b | 401 | |
The Other Jimmy |
31:8ea194f6145b | 402 | Assumes that the file specified is a PDSC file and is in the cache. |
The Other Jimmy |
31:8ea194f6145b | 403 | |
The Other Jimmy |
31:8ea194f6145b | 404 | :param url: The URL of a PDSC file. |
The Other Jimmy |
31:8ea194f6145b | 405 | :type url: str |
The Other Jimmy |
31:8ea194f6145b | 406 | :return: A parsed representation of the PDSC file. |
The Other Jimmy |
31:8ea194f6145b | 407 | :rtype: BeautifulSoup |
The Other Jimmy |
31:8ea194f6145b | 408 | """ |
The Other Jimmy |
31:8ea194f6145b | 409 | dest = join(self.data_path, strip_protocol(url)) |
The Other Jimmy |
31:8ea194f6145b | 410 | with open(dest, "r") as fd : |
The Other Jimmy |
31:8ea194f6145b | 411 | return BeautifulSoup(fd, "html.parser") |
The Other Jimmy |
31:8ea194f6145b | 412 | |
The Other Jimmy |
35:da9c89f8be7d | 413 | def pack_from_cache(self, device) : |
The Other Jimmy |
31:8ea194f6145b | 414 | """Low level inteface for extracting a PACK file from the cache. |
The Other Jimmy |
31:8ea194f6145b | 415 | |
The Other Jimmy |
31:8ea194f6145b | 416 | Assumes that the file specified is a PACK file and is in the cache. |
The Other Jimmy |
31:8ea194f6145b | 417 | |
The Other Jimmy |
31:8ea194f6145b | 418 | :param url: The URL of a PACK file. |
The Other Jimmy |
31:8ea194f6145b | 419 | :type url: str |
The Other Jimmy |
31:8ea194f6145b | 420 | :return: A parsed representation of the PACK file. |
The Other Jimmy |
31:8ea194f6145b | 421 | :rtype: ZipFile |
The Other Jimmy |
31:8ea194f6145b | 422 | """ |
The Other Jimmy |
31:8ea194f6145b | 423 | return ZipFile(join(self.data_path, |
The Other Jimmy |
31:8ea194f6145b | 424 | strip_protocol(device['pack_file']))) |
The Other Jimmy |
31:8ea194f6145b | 425 | |
The Other Jimmy |
31:8ea194f6145b | 426 | def gen_dict_from_cache() : |
The Other Jimmy |
31:8ea194f6145b | 427 | pdsc_files = pdsc_from_cache(RootPackUrl) |
The Other Jimmy |
31:8ea194f6145b | 428 | |
The Other Jimmy |
31:8ea194f6145b | 429 | def cache_and_parse(self, url) : |
The Other Jimmy |
31:8ea194f6145b | 430 | """A low level shortcut that Caches and Parses a PDSC file. |
The Other Jimmy |
31:8ea194f6145b | 431 | |
The Other Jimmy |
31:8ea194f6145b | 432 | :param url: The URL of the PDSC file. |
The Other Jimmy |
31:8ea194f6145b | 433 | :type url: str |
The Other Jimmy |
31:8ea194f6145b | 434 | :return: A parsed representation of the PDSC file. |
The Other Jimmy |
31:8ea194f6145b | 435 | :rtype: BeautifulSoup |
The Other Jimmy |
31:8ea194f6145b | 436 | """ |
The Other Jimmy |
31:8ea194f6145b | 437 | self.cache_file(url) |
The Other Jimmy |
31:8ea194f6145b | 438 | return self.pdsc_from_cache(url) |
The Other Jimmy |
31:8ea194f6145b | 439 |