InfiniTime.git

ref: fc5424cb72e477c5f1bbfaeddb5c50b851a965ae

bootloader/ota-dfu-python/unpacker.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)