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