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