summaryrefslogtreecommitdiff
path: root/bootloader/ota-dfu-python/unpacker.py
diff options
context:
space:
mode:
authorJF <jf@codingfield.com>2020-06-07 12:17:45 (GMT)
committerGitea <gitea@fake.local>2020-06-07 12:17:45 (GMT)
commita0e73f5c1a1e652aa6270b7e42a73aee3d12ded6 (patch)
tree6205dfb543bb22245d39a2f6e44d2c26cb381c10 /bootloader/ota-dfu-python/unpacker.py
parent8a94750e30399bfb204cbec59a769d9d1b6b5baa (diff)
parentdbdb26ae1fa45cec88f1b9ea0353b3d0a3c39f56 (diff)
Merge branch 'develop' of JF/PineTime into master
Diffstat (limited to 'bootloader/ota-dfu-python/unpacker.py')
-rw-r--r--bootloader/ota-dfu-python/unpacker.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/bootloader/ota-dfu-python/unpacker.py b/bootloader/ota-dfu-python/unpacker.py
new file mode 100644
index 0000000..960ef9d
--- /dev/null
+++ b/bootloader/ota-dfu-python/unpacker.py
@@ -0,0 +1,52 @@
+import os.path
+import zipfile
+import tempfile
+import random
+import string
+import shutil
+import re
+
+from os.path import basename
+
+class Unpacker(object):
+ #--------------------------------------------------------------------------
+ #
+ #--------------------------------------------------------------------------
+ def entropy(self, length):
+ return ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for i in range (length))
+
+ #--------------------------------------------------------------------------
+ #
+ #--------------------------------------------------------------------------
+ def unpack_zipfile(self, file):
+
+ if not os.path.isfile(file):
+ raise Exception("Error: file, not found!")
+
+ # Create unique working direction into which the zip file is expanded
+ self.unzip_dir = "{0}/{1}_{2}".format(tempfile.gettempdir(), os.path.splitext(basename(file))[0], self.entropy(6))
+
+ datfilename = ""
+ binfilename = ""
+
+ with zipfile.ZipFile(file, 'r') as zip:
+ files = [item.filename for item in zip.infolist()]
+ datfilename = [m.group(0) for f in files for m in [re.search('.*\.dat', f)] if m].pop()
+ binfilename = [m.group(0) for f in files for m in [re.search('.*\.bin', f)] if m].pop()
+
+ zip.extractall(r'{0}'.format(self.unzip_dir))
+
+ datfile = "{0}/{1}".format(self.unzip_dir, datfilename)
+ binfile = "{0}/{1}".format(self.unzip_dir, binfilename)
+
+ # print "DAT file: " + datfile
+ # print "BIN file: " + binfile
+
+ return binfile, datfile
+
+ #--------------------------------------------------------------------------
+ #
+ #--------------------------------------------------------------------------
+ def delete(self):
+ # delete self.unzip_dir and its contents
+ shutil.rmtree(self.unzip_dir)