Commit ac9239 python 3 fixes

1 file Authored and Committed by Richard Marko a year ago
python 3 fixes

    
esptool.py +43 -38
  1 @@ -159,7 +159,7 @@
  2   
  3       """ Try connecting repeatedly until successful, or giving up """
  4       def connect(self):
  5 -         print 'Connecting...'
  6 +         print('Connecting...')
  7   
  8           for _ in xrange(4):
  9               # issue reset-to-bootloader:
 10 @@ -243,7 +243,7 @@
 11           result = self.command(ESPROM.ESP_FLASH_BEGIN,
 12                                 struct.pack('<IIII', erase_size, num_blocks, ESPROM.ESP_FLASH_BLOCK, offset))[1]
 13           if size != 0:
 14 -             print "Took %.2fs to erase flash block" % (time.time() - t)
 15 +             print("Took %.2fs to erase flash block" % (time.time() - t))
 16           if result != "\0\0":
 17               raise FatalError.WithResult('Failed to enter Flash download mode (result "%s")', result)
 18           self._port.timeout = old_tmo
 19 @@ -479,7 +479,7 @@
 20                   raise FatalError('Invalid V2 image magic=%d' % (magic))
 21               if segments != 4:
 22                   # segment count is not really segment count here, but we expect to see '4'
 23 -                 print 'Warning: V2 header has unexpected "segment" count %d (usually 4)' % segments
 24 +                 print('Warning: V2 header has unexpected "segment" count %d (usually 4)' % segments)
 25   
 26               # irom segment comes before the second header
 27               self.load_segment(load_file, True)
 28 @@ -542,13 +542,15 @@
 29                   tool_nm = "xt-nm"
 30               proc = subprocess.Popen([tool_nm, self.name], stdout=subprocess.PIPE)
 31           except OSError:
 32 -             print "Error calling %s, do you have Xtensa toolchain in PATH?" % tool_nm
 33 +             print("Error calling %s, do you have Xtensa toolchain in PATH?" %
 34 +                     tool_nm)
 35               sys.exit(1)
 36           for l in proc.stdout:
 37               fields = l.strip().split()
 38               try:
 39                   if fields[0] == "U":
 40 -                     print "Warning: ELF binary has undefined symbol %s" % fields[1]
 41 +                     print("Warning: ELF binary has undefined symbol %s" %
 42 +                             fields[1])
 43                       continue
 44                   if fields[0] == "w":
 45                       continue  # can skip weak symbols
 46 @@ -567,7 +569,8 @@
 47           try:
 48               proc = subprocess.Popen([tool_readelf, "-h", self.name], stdout=subprocess.PIPE)
 49           except OSError:
 50 -             print "Error calling %s, do you have Xtensa toolchain in PATH?" % tool_readelf
 51 +             print("Error calling %s, do you have Xtensa toolchain in PATH?" %
 52 +                     tool_readelf)
 53               sys.exit(1)
 54           for l in proc.stdout:
 55               fields = l.strip().split()
 56 @@ -612,7 +615,7 @@
 57           try:
 58               return subprocess.check_output(["cygpath", "-w", p]).rstrip('\n')
 59           except subprocess.CalledProcessError:
 60 -             print "WARNING: Failed to call cygpath to sanitise Cygwin path."
 61 +             print("WARNING: Failed to call cygpath to sanitise Cygwin path.")
 62       return p
 63   
 64   
 65 @@ -647,9 +650,9 @@
 66   def load_ram(esp, args):
 67       image = LoadFirmwareImage(args.filename)
 68   
 69 -     print 'RAM boot...'
 70 +     print('RAM boot...')
 71       for (offset, size, data) in image.segments:
 72 -         print 'Downloading %d bytes at %08x...' % (size, offset),
 73 +         print('Downloading %d bytes at %08x...' % (size, offset))
 74           sys.stdout.flush()
 75           esp.mem_begin(size, div_roundup(size, esp.ESP_RAM_BLOCK), esp.ESP_RAM_BLOCK, offset)
 76   
 77 @@ -658,19 +661,20 @@
 78               esp.mem_block(data[0:esp.ESP_RAM_BLOCK], seq)
 79               data = data[esp.ESP_RAM_BLOCK:]
 80               seq += 1
 81 -         print 'done!'
 82 +         print( 'done!')
 83   
 84 -     print 'All segments done, executing at %08x' % image.entrypoint
 85 +     print('All segments done, executing at %08x' % image.entrypoint)
 86       esp.mem_finish(image.entrypoint)
 87   
 88   
 89   def read_mem(esp, args):
 90 -     print '0x%08x = 0x%08x' % (args.address, esp.read_reg(args.address))
 91 +     print('0x%08x = 0x%08x' % (args.address, esp.read_reg(args.address)))
 92   
 93   
 94   def write_mem(esp, args):
 95       esp.write_reg(args.address, args.value, args.mask, 0)
 96 -     print 'Wrote %08x, mask %08x to %08x' % (args.value, args.mask, args.address)
 97 +     print('Wrote %08x, mask %08x to %08x' % (args.value, args.mask,
 98 +         args.address))
 99   
100   
101   def dump_mem(esp, args):
102 @@ -679,10 +683,10 @@
103           d = esp.read_reg(args.address + (i * 4))
104           f.write(struct.pack('<I', d))
105           if f.tell() % 1024 == 0:
106 -             print '\r%d bytes read... (%d %%)' % (f.tell(),
107 -                                                   f.tell() * 100 / args.size),
108 +             print('\r%d bytes read... (%d %%)' % (f.tell(),
109 +                                                   f.tell() * 100 / args.size))
110           sys.stdout.flush()
111 -     print 'Done!'
112 +     print('Done!')
113   
114   
115   def write_flash(esp, args):
116 @@ -694,7 +698,7 @@
117       for address, argfile in args.addr_filename:
118           image = argfile.read()
119           argfile.seek(0)  # in case we need it again
120 -         print 'Erasing flash...'
121 +         print('Erasing flash...')
122           blocks = div_roundup(len(image), esp.ESP_FLASH_BLOCK)
123           esp.flash_begin(blocks * esp.ESP_FLASH_BLOCK, address)
124           seq = 0
125 @@ -702,7 +706,8 @@
126           t = time.time()
127           header_block = None
128           while len(image) > 0:
129 -             print '\rWriting at 0x%08x... (%d %%)' % (address + seq * esp.ESP_FLASH_BLOCK, 100 * (seq + 1) / blocks),
130 +             print('\rWriting at 0x%08x... (%d %%)' % (address + seq *
131 +                 esp.ESP_FLASH_BLOCK, 100 * (seq + 1) / blocks))
132               sys.stdout.flush()
133               block = image[0:esp.ESP_FLASH_BLOCK]
134               # Pad the last block
135 @@ -716,15 +721,15 @@
136               seq += 1
137               written += len(block)
138           t = time.time() - t
139 -         print '\rWrote %d bytes at 0x%08x in %.1f seconds (%.1f kbit/s)...' % (written, address, t, written / t * 8 / 1000)
140 -     print '\nLeaving...'
141 +         print('\rWrote %d bytes at 0x%08x in %.1f seconds (%.1f kbit/s)...' % (written, address, t, written / t * 8 / 1000))
142 +     print('\nLeaving...')
143       if args.flash_mode == 'dio':
144           esp.flash_unlock_dio()
145       else:
146           esp.flash_begin(0, 0)
147           esp.flash_finish(False)
148       if args.verify:
149 -         print 'Verifying just-written flash...'
150 +         print('Verifying just-written flash...')
151           verify_flash(esp, args, header_block)
152   
153   
154 @@ -732,17 +737,17 @@
155       image = LoadFirmwareImage(args.filename)
156       print('Image version: %d' % image.version)
157       print('Entry point: %08x' % image.entrypoint) if image.entrypoint != 0 else 'Entry point not set'
158 -     print '%d segments' % len(image.segments)
159 -     print
160 +     print('%d segments' % len(image.segments))
161 +     print()
162       checksum = ESPROM.ESP_CHECKSUM_MAGIC
163       for (idx, (offset, size, data)) in enumerate(image.segments):
164           if image.version == 2 and idx == 0:
165 -             print 'Segment 1: %d bytes IROM0 (no load address)' % size
166 +             print('Segment 1: %d bytes IROM0 (no load address)' % size)
167           else:
168 -             print 'Segment %d: %5d bytes at %08x' % (idx + 1, size, offset)
169 +             print('Segment %d: %5d bytes at %08x' % (idx + 1, size, offset))
170               checksum = ESPROM.checksum(data, checksum)
171 -     print
172 -     print 'Checksum: %02x (%s)' % (image.checksum, 'valid' if image.checksum == checksum else 'invalid!')
173 +     print()
174 +     print('Checksum: %02x (%s)' % (image.checksum, 'valid' if image.checksum == checksum else 'invalid!'))
175   
176   
177   def make_image(args):
178 @@ -794,16 +799,16 @@
179   
180   def read_mac(esp, args):
181       mac = esp.read_mac()
182 -     print 'MAC: %s' % ':'.join(map(lambda x: '%02x' % x, mac))
183 +     print('MAC: %s' % ':'.join(map(lambda x: '%02x' % x, mac)))
184   
185   
186   def chip_id(esp, args):
187       chipid = esp.chip_id()
188 -     print 'Chip ID: 0x%08x' % chipid
189 +     print('Chip ID: 0x%08x' % chipid)
190   
191   
192   def erase_flash(esp, args):
193 -     print 'Erasing flash (this may take a while)...'
194 +     print('Erasing flash (this may take a while)...')
195       esp.flash_erase()
196   
197   
198 @@ -813,12 +818,12 @@
199   
200   def flash_id(esp, args):
201       flash_id = esp.flash_id()
202 -     print 'Manufacturer: %02x' % (flash_id & 0xff)
203 -     print 'Device: %02x%02x' % ((flash_id >> 8) & 0xff, (flash_id >> 16) & 0xff)
204 +     print('Manufacturer: %02x' % (flash_id & 0xff))
205 +     print('Device: %02x%02x' % ((flash_id >> 8) & 0xff, (flash_id >> 16) & 0xff))
206   
207   
208   def read_flash(esp, args):
209 -     print 'Please wait...'
210 +     print('Please wait...')
211       file(args.filename, 'wb').write(esp.flash_read(args.address, 1024, div_roundup(args.size, 1024), args.progress)[:args.size])
212   
213   
214 @@ -832,18 +837,18 @@
215               image = header_block + image[esp.ESP_FLASH_BLOCK:]
216           argfile.seek(0)  # rewind in case we need it again
217           image_size = len(image)
218 -         print 'Verifying 0x%x (%d) bytes @ 0x%08x in flash against %s...' % (image_size, image_size, address, argfile.name)
219 +         print('Verifying 0x%x (%d) bytes @ 0x%08x in flash against %s...' % (image_size, image_size, address, argfile.name))
220           flash = esp.flash_read(address, 1024, div_roundup(image_size, 1024))[:image_size]
221           if flash == image:
222 -             print '-- verify OK'
223 +             print('-- verify OK')
224           else:
225               differences = True
226               diff = [i for i in xrange(image_size) if flash[i] != image[i]]
227 -             print '-- verify FAILED: %d differences, first @ 0x%08x' % (len(diff), address + diff[0])
228 +             print('-- verify FAILED: %d differences, first @ 0x%08x' % (len(diff), address + diff[0]))
229               try:
230                   if args.diff == 'yes':
231                       for d in diff:
232 -                         print '   %08x %02x %02x' % (address + d, ord(flash[d]), ord(image[d]))
233 +                         print('   %08x %02x %02x' % (address + d, ord(flash[d]), ord(image[d])))
234               except AttributeError:
235                   pass  # if performing write_flash --verify, there is no .diff attribute
236       if differences:
237 @@ -1018,5 +1023,5 @@
238       try:
239           main()
240       except FatalError as e:
241 -         print '\nA fatal error occurred: %s' % e
242 +         print('\nA fatal error occurred: %s' % e)
243           sys.exit(2)