Skip to content

Commit ce71973

Browse files
mcsprhasenradball
authored andcommitted
Do not show python traceback when esptool fails (esp8266#8603)
Path separator already handled by python Empty strings are false, implicitly checking for length
1 parent fc5c6bf commit ce71973

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

tools/upload.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
# First parameter is pyserial path, second is esptool path, then a series of command arguments
77
# i.e. upload.py tools/pyserial tools/esptool write_flash file 0x0
88

9-
import sys
109
import os
10+
import sys
1111
import tempfile
1212

1313
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__))
1515
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
1818
import esptool # If this fails, we can't continue and will bomb below
19-
except Exception:
19+
except ImportError:
2020
sys.stderr.write("pyserial or esptool directories not found next to this upload.py tool.\n")
2121
sys.exit(1)
2222

@@ -26,7 +26,7 @@
2626
erase_addr = ''
2727
erase_len = ''
2828

29-
while len(sys.argv):
29+
while sys.argv:
3030
thisarg = sys.argv.pop(0)
3131

3232
# We silently replace the 921kbaud setting with 460k to enable backward
@@ -45,25 +45,30 @@
4545
elif thisarg == 'write_flash':
4646
write_addr = sys.argv.pop(0)
4747
binary = sys.argv.pop(0)
48-
elif len(thisarg):
48+
elif thisarg:
4949
cmdline = cmdline + [thisarg]
5050

5151
cmdline = cmdline + ['write_flash']
52-
if len(write_option):
52+
if write_option:
5353
cmdline = cmdline + [write_option]
5454
cmdline = cmdline + ['--flash_size', 'detect']
5555
cmdline = cmdline + [write_addr, binary]
5656

5757
erase_file = ''
58-
if len(erase_addr):
58+
if erase_addr:
5959
# Generate temporary empty (0xff) file
6060
eraser = tempfile.mkstemp()
6161
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)))
6363
os.close(eraser[0])
64-
cmdline = cmdline + [ erase_addr, erase_file ]
64+
cmdline = cmdline + [erase_addr, erase_file]
6565

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

Comments
 (0)