diff options
author | Sylvain Munaut <tnt@246tNt.com> | 2018-09-21 14:59:38 -0700 |
---|---|---|
committer | Sylvain Munaut <tnt@246tNt.com> | 2019-12-08 15:22:38 +0100 |
commit | 6fd953942f8c144adabb6163d24744c38de99ab2 (patch) | |
tree | c19a7e307f9aae90d13b746c8090e4ecc7f2d0cf /lib | |
parent | fa6761afbf8c2658782e0c7fc5d51063679b7ae4 (diff) |
fosphor: Convert mkresources to Python 3
Py3 only ... upgrade your system folks !
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/fosphor/mkresources.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/lib/fosphor/mkresources.py b/lib/fosphor/mkresources.py index 7274e12..d8a5a3b 100755 --- a/lib/fosphor/mkresources.py +++ b/lib/fosphor/mkresources.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Packs a given file list into a .c file to be included in the executable @@ -21,6 +21,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ +import binascii import sys @@ -60,21 +61,21 @@ class ResourcePacker(object): def _wrap_str(self, s): m = { - '\n' : '\\n', - '\t' : '\\t', - '\0' : '\\0', - '\\' : '\\\\', - '"' : '\\"', + ord(b'\n') : '\\n', + ord(b'\t') : '\\t', + ord(b'\0') : '\\0', + ord(b'\\') : '\\\\', + ord(b'"') : '\\"', } sa = [] for c in s: if c in m: sa.append(m[c]) - elif ord(c) < 32: - sa.append('\\x%02x' % ord(c)) + elif (c < 32) | (c >= 128): + sa.append('\\x%02x' % c) else: - sa.append(c) + sa.append(chr(c)) return ''.join(sa) @@ -93,12 +94,12 @@ class ResourcePacker(object): ] def file_binary_slug(self, name): - return 'data_' + name.encode('hex') + return 'data_' + binascii.hexlify(name.encode('utf-8')).decode('utf-8') def file_binary_data(self, name, content): dl = [ 'static const char %s[] = {' % self.file_binary_slug(name) ] for i in range(0, len(content), 8): - dl.append('\t' + ' '.join(['0x%02x,' % ord(c) for c in content[i:i+8]])) + dl.append('\t' + ' '.join(['0x%02x,' % c for c in content[i:i+8]])) dl.append('};'); return dl @@ -112,7 +113,7 @@ class ResourcePacker(object): for f in filenames: fh = open(f, 'rb') c = fh.read() - if '\x00' in c or len(c) > 65535: + if b'\x00' in c or len(c) > 65535: rp.extend(self.file_binary(f, c)) data.extend(self.file_binary_data(f, c)) else: @@ -129,7 +130,7 @@ class ResourcePacker(object): def main(self, *files): p = ResourcePacker() r = p.process(files) - print '\n'.join(r) + print('\n'.join(r)) if __name__ == '__main__': |