|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | import os.path |
4 | | -import zipfile |
5 | | -import tempfile |
6 | 4 | import random |
7 | | -import string |
8 | | -import shutil |
9 | 5 | import re |
| 6 | +import shutil |
| 7 | +import string |
| 8 | +import tempfile |
| 9 | +import zipfile |
10 | 10 |
|
11 | | -from os.path import basename |
12 | 11 |
|
13 | 12 | class Unpacker(object): |
14 | | - #-------------------------------------------------------------------------- |
15 | | - # |
16 | | - #-------------------------------------------------------------------------- |
17 | | - def entropy(self, length): |
18 | | - return ''.join(random.choice(string.lowercase) for i in range (length)) |
19 | | - |
20 | | - #-------------------------------------------------------------------------- |
21 | | - # |
22 | | - #-------------------------------------------------------------------------- |
23 | | - def unpack_zipfile(self, file): |
24 | | - |
25 | | - if not os.path.isfile(file): |
26 | | - raise Exception("Error: file, not found!") |
27 | | - |
28 | | - # Create unique working direction into which the zip file is expanded |
29 | | - self.unzip_dir = "{0}/{1}_{2}".format(tempfile.gettempdir(), os.path.splitext(basename(file))[0], self.entropy(6)) |
30 | | - |
31 | | - datfilename = "" |
32 | | - binfilename = "" |
33 | | - |
34 | | - with zipfile.ZipFile(file, 'r') as zip: |
35 | | - files = [item.filename for item in zip.infolist()] |
36 | | - datfilename = [m.group(0) for f in files for m in [re.search('.*\.dat', f)] if m].pop() |
37 | | - binfilename = [m.group(0) for f in files for m in [re.search('.*\.bin', f)] if m].pop() |
38 | | - |
39 | | - zip.extractall(r'{0}'.format(self.unzip_dir)) |
40 | | - |
41 | | - datfile = "{0}/{1}".format(self.unzip_dir, datfilename) |
42 | | - binfile = "{0}/{1}".format(self.unzip_dir, binfilename) |
43 | | - |
44 | | - # print "DAT file: " + datfile |
45 | | - # print "BIN file: " + binfile |
46 | | - |
47 | | - return binfile, datfile |
48 | | - |
49 | | - #-------------------------------------------------------------------------- |
50 | | - # |
51 | | - #-------------------------------------------------------------------------- |
52 | | - def delete(self): |
53 | | - # delete self.unzip_dir and its contents |
54 | | - shutil.rmtree(self.unzip_dir) |
| 13 | + """ |
| 14 | + Helper class to unpack a nRF5 Device Firmware Update (DFU) |
| 15 | + based .zip file in a temporary folder and locate the .bin |
| 16 | + and associated .dat files inside of it. |
| 17 | + Removes the temporary folder on desctruction of the instance. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self): |
| 21 | + self.unzip_dir = '' |
| 22 | + |
| 23 | + def unpack_zipfile(self, zip_fname): |
| 24 | + if not os.path.isfile(zip_fname): |
| 25 | + raise Exception("Error: file '{}' not found!".format(zip_fname)) |
| 26 | + # create unique working directory into which to expand the zip file |
| 27 | + rnd_str = ''.join(random.choice(string.ascii_lowercase) for i in range(6)) |
| 28 | + self.unzip_dir = '{0}/{1}_{2}'.format( \ |
| 29 | + tempfile.gettempdir(), \ |
| 30 | + os.path.splitext(os.path.basename(zip_fname))[0], \ |
| 31 | + rnd_str) |
| 32 | + bin_fname = '' |
| 33 | + dat_fname = '' |
| 34 | + with zipfile.ZipFile(zip_fname, 'r') as zf: |
| 35 | + files = [item.filename for item in zf.infolist()] |
| 36 | + bin_fname = [m.group(0) for f in files |
| 37 | + for m in [re.search('.*\.bin', f)] if m].pop() |
| 38 | + dat_fname = [m.group(0) for f in files |
| 39 | + for m in [re.search('.*\.dat', f)] if m].pop() |
| 40 | + zf.extractall(r'{0}'.format(self.unzip_dir)) |
| 41 | + bin_file = '{0}/{1}'.format(self.unzip_dir, bin_fname) |
| 42 | + dat_file = '{0}/{1}'.format(self.unzip_dir, dat_fname) |
| 43 | + return bin_file, dat_file |
| 44 | + |
| 45 | + def __del__(self): |
| 46 | + if self.unzip_dir: |
| 47 | + # delete self.unzip_dir and its contents |
| 48 | + shutil.rmtree(self.unzip_dir) |
| 49 | + |
| 50 | + def delete(self): |
| 51 | + # delete self.unzip_dir and its contents |
| 52 | + shutil.rmtree(self.unzip_dir) |
0 commit comments