gen_esp32part: Make compatible with both python2.7 and python3.

Merges https://github.com/espressif/esp-idf/pull/577
This commit is contained in:
David M. Palmer 2017-05-06 12:47:26 -06:00 committed by Angus Gratton
parent 15a6145961
commit beffcd6468

View file

@ -80,7 +80,7 @@ class PartitionTable(list):
p.verify() p.verify()
# check for overlaps # check for overlaps
last = None last = None
for p in sorted(self): for p in sorted(self, key=lambda x:x.offset):
if p.offset < 0x5000: if p.offset < 0x5000:
raise InputError("Partition offset 0x%x is below 0x5000" % p.offset) raise InputError("Partition offset 0x%x is below 0x5000" % p.offset)
if last is not None and p.offset < last.offset + last.size: if last is not None and p.offset < last.offset + last.size:
@ -100,10 +100,10 @@ class PartitionTable(list):
raise InputError("Partition table is missing an end-of-table marker") raise InputError("Partition table is missing an end-of-table marker")
def to_binary(self): def to_binary(self):
result = "".join(e.to_binary() for e in self) result = b"".join(e.to_binary() for e in self)
if len(result )>= MAX_PARTITION_LENGTH: if len(result )>= MAX_PARTITION_LENGTH:
raise InputError("Binary partition table length (%d) longer than max" % len(result)) raise InputError("Binary partition table length (%d) longer than max" % len(result))
result += "\xFF" * (MAX_PARTITION_LENGTH - len(result)) # pad the sector, for signing result += b"\xFF" * (MAX_PARTITION_LENGTH - len(result)) # pad the sector, for signing
return result return result
def to_csv(self, simple_formatting=False): def to_csv(self, simple_formatting=False):
@ -137,7 +137,7 @@ class PartitionDefinition(object):
}, },
} }
MAGIC_BYTES = "\xAA\x50" MAGIC_BYTES = b"\xAA\x50"
ALIGNMENT = { ALIGNMENT = {
APP_TYPE : 0x1000, APP_TYPE : 0x1000,
@ -267,7 +267,7 @@ class PartitionDefinition(object):
self.MAGIC_BYTES, self.MAGIC_BYTES,
self.type, self.subtype, self.type, self.subtype,
self.offset, self.size, self.offset, self.size,
self.name, self.name.encode(),
flags) flags)
def to_csv(self, simple_formatting=False): def to_csv(self, simple_formatting=False):
@ -346,9 +346,11 @@ def main():
if input_is_binary: if input_is_binary:
output = table.to_csv() output = table.to_csv()
with sys.stdout if args.output == '-' else open(args.output, 'w') as f:
f.write(output)
else: else:
output = table.to_binary() output = table.to_binary()
with sys.stdout if args.output == '-' else open(args.output, 'w') as f: with sys.stdout.buffer if args.output == '-' else open(args.output, 'wb') as f:
f.write(output) f.write(output)
if __name__ == '__main__': if __name__ == '__main__':