Clone of official tools

Revision:
43:2a7da56ebd24
Parent:
36:96847d42f010
diff -r 2cf3f29fece1 -r 2a7da56ebd24 arm_pack_manager/__init__.py
--- a/arm_pack_manager/__init__.py	Mon Nov 06 13:17:14 2017 -0600
+++ b/arm_pack_manager/__init__.py	Tue Sep 25 13:43:09 2018 -0500
@@ -1,10 +1,16 @@
-from urllib2 import urlopen, URLError
+try:
+    from urllib2 import urlopen, URLError
+except ImportError:
+    from urllib.request import urlopen, URLError
 from bs4 import BeautifulSoup
 from os.path import join, dirname, basename
 from os import makedirs
 from errno import EEXIST
 from threading import Thread
-from Queue import Queue
+try:
+    from Queue import Queue
+except ImportError:
+    from queue import Queue
 from re import compile, sub
 from sys import stderr, stdout
 from itertools import takewhile
@@ -15,6 +21,8 @@
 import warnings
 from distutils.version import LooseVersion
 
+from tools.flash_algo import PackFlashAlgo
+
 warnings.filterwarnings("ignore")
 
 from fuzzywuzzy import process
@@ -145,12 +153,41 @@
                          for pdsc in root_data.find_all("pdsc")]
         return self.urls
 
+    def _get_sectors(self, device):
+        """Extract sector sizes from device FLM algorithm
+
+        Will return None if there is no algorithm, pdsc URL formatted in correctly
+
+        :return: A list tuples of sector start and size
+        :rtype: [list]
+        """
+        try:
+            pack = self.pack_from_cache(device)
+            ret = []
+            for filename in device['algorithm'].keys():
+                try:
+                    flm = pack.open(filename)
+                    flash_alg = PackFlashAlgo(flm.read())
+                    sectors = [(flash_alg.flash_start + offset, size)
+                               for offset, size in flash_alg.sector_sizes]
+                    ret.extend(sectors)
+                except Exception:
+                    pass
+            ret.sort(key=lambda sector: sector[0])
+            return ret
+        except Exception:
+            return None
+
     def _extract_dict(self, device, filename, pack) :
         to_ret = dict(pdsc_file=filename, pack_file=pack)
         try : to_ret["memory"] = dict([(m["id"], dict(start=m["start"],
                                                       size=m["size"]))
                                        for m in device("memory")])
-        except (KeyError, TypeError, IndexError) as e : pass
+        except (KeyError, TypeError, IndexError) as e:
+            try : to_ret["memory"] = dict([(m["name"], dict(start=m["start"],
+                                                          size=m["size"]))
+                                           for m in device("memory")])
+            except (KeyError, TypeError, IndexError) as e : pass
         try: algorithms = device("algorithm")
         except:
             try: algorithms = device.parent("algorithm")
@@ -219,6 +256,7 @@
             del to_ret["compile"]
 
         to_ret['debug-interface'] = []
+        to_ret['sectors'] = self._get_sectors(to_ret)
 
         return to_ret
 
@@ -445,4 +483,3 @@
         """
         self.cache_file(url)
         return self.pdsc_from_cache(url)
-