|
6 | 6 | # First parameter is pyserial path, second is esptool path, then a series of command arguments
|
7 | 7 | # i.e. upload.py tools/pyserial tools/esptool write_flash file 0x0
|
8 | 8 |
|
9 |
| -import sys |
10 | 9 | import os
|
| 10 | +import sys |
11 | 11 | import tempfile
|
12 | 12 |
|
13 | 13 | sys.argv.pop(0) # Remove executable name
|
14 |
| -toolspath = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/') # CWD in UNIX format |
| 14 | +toolspath = os.path.dirname(os.path.realpath(__file__)) |
15 | 15 | try:
|
16 |
| - sys.path.insert(0, toolspath + "/pyserial") # Add pyserial dir to search path |
17 |
| - sys.path.insert(0, toolspath + "/esptool") # Add esptool dir to search path |
| 16 | + sys.path.insert(0, os.path.join(toolspath, "pyserial")) # Add pyserial dir to search path |
| 17 | + sys.path.insert(0, os.path.join(toolspath, "esptool")) # Add esptool dir to search path |
18 | 18 | import esptool # If this fails, we can't continue and will bomb below
|
19 |
| -except Exception: |
| 19 | +except ImportError: |
20 | 20 | sys.stderr.write("pyserial or esptool directories not found next to this upload.py tool.\n")
|
21 | 21 | sys.exit(1)
|
22 | 22 |
|
|
26 | 26 | erase_addr = ''
|
27 | 27 | erase_len = ''
|
28 | 28 |
|
29 |
| -while len(sys.argv): |
| 29 | +while sys.argv: |
30 | 30 | thisarg = sys.argv.pop(0)
|
31 | 31 |
|
32 | 32 | # We silently replace the 921kbaud setting with 460k to enable backward
|
|
45 | 45 | elif thisarg == 'write_flash':
|
46 | 46 | write_addr = sys.argv.pop(0)
|
47 | 47 | binary = sys.argv.pop(0)
|
48 |
| - elif len(thisarg): |
| 48 | + elif thisarg: |
49 | 49 | cmdline = cmdline + [thisarg]
|
50 | 50 |
|
51 | 51 | cmdline = cmdline + ['write_flash']
|
52 |
| -if len(write_option): |
| 52 | +if write_option: |
53 | 53 | cmdline = cmdline + [write_option]
|
54 | 54 | cmdline = cmdline + ['--flash_size', 'detect']
|
55 | 55 | cmdline = cmdline + [write_addr, binary]
|
56 | 56 |
|
57 | 57 | erase_file = ''
|
58 |
| -if len(erase_addr): |
| 58 | +if erase_addr: |
59 | 59 | # Generate temporary empty (0xff) file
|
60 | 60 | eraser = tempfile.mkstemp()
|
61 | 61 | erase_file = eraser[1]
|
62 |
| - os.write(eraser[0], bytearray([255] * int(erase_len, 0))) |
| 62 | + os.write(eraser[0], bytearray([0xff] * int(erase_len, 0))) |
63 | 63 | os.close(eraser[0])
|
64 |
| - cmdline = cmdline + [ erase_addr, erase_file ] |
| 64 | + cmdline = cmdline + [erase_addr, erase_file] |
65 | 65 |
|
66 |
| -esptool.main(cmdline) |
67 |
| - |
68 |
| -if len(erase_file): |
69 |
| - os.remove(erase_file) |
| 66 | +try: |
| 67 | + esptool.main(cmdline) |
| 68 | +except esptool.FatalError as e: |
| 69 | + sys.stderr.write('\nA fatal esptool.py error occurred: %s' % e) |
| 70 | +finally: |
| 71 | + if erase_file: |
| 72 | + os.remove(erase_file) |
| 73 | + if sys.exc_info: |
| 74 | + sys.exit(2) |
0 commit comments